Search code examples
rraster

loop through multiple column and make raster of each column using given extent in r


I have 36 years' worth of data (yearly annual temp), its latitude, and longitude. I want to make yearly raster (tif) for each year. I can make one by one, but could not wrap around for a loop. Any help is appreciated.

    library(raster)

setwd("C:/NEW DSW/Scenarios")
tas_yearly_ssp126_15_21<-read.table("tas_yearly_ssp126_15_21.csv", header = TRUE, sep=",")

head(tas_yearly_ssp126_15_21)
STID      Long      Lat    Y2015    Y2016    Y2017    Y2018    Y2019    Y2020    Y2021    Y2022    Y2023
1  St1 -88.12124 37.99907 16.15833 16.26667 14.80833 14.20000 14.54167 14.75000 14.83333 15.06667 14.20000
2  St2 -88.09833 37.99759 16.21667 16.33333 14.87500 14.28333 14.62500 14.80000 14.92500 15.11667 14.24167
3  St3 -88.07541 37.99611 16.21667 16.33333 14.87500 14.28333 14.62500 14.80000 14.92500 15.11667 14.24167
4  St4 -88.05250 37.99462 16.22500 16.33333 14.89167 14.30833 14.65833 14.80000 14.92500 15.12500 14.23333
5  St5 -88.02959 37.99313 16.22500 16.33333 14.89167 14.30833 14.65833 14.80000 14.92500 15.12500 14.23333
6  St6 -88.00668 37.99163 16.25833 16.36667 14.94167 14.39167 14.72500 14.84167 14.93333 15.16667 14.2583

lat_long<-SpatialPoints(data.frame(tas_yearly_ssp126_15_21[2:3]))
tas_yearly_ssp126_15_21<-data.frame(tas_yearly_ssp126_15_21[4:39])
ras_extent<-extent(lat_long)
ras_extent
class      : Extent 
xmin       : -89.99975 
xmax       : -87.99981 
ymin       : 36.00081 
ymax       : 38.00013 

dummy_ras<-raster(ras_extent,ncol=84,nrow=103,crs="+proj=longlat +datum=WGS84")

###For a single column rasterize###
fillv<-tas_yearly_ssp126_15_21[1]

rasterize(lat_long, dummy_ras, fillv, fun='last', background=NA,mask=FALSE, update=FALSE, updateValue='all', filename="Year2015.tif", na.rm=TRUE)

class      : RasterLayer 
dimensions : 103, 84, 8652  (nrow, ncol, ncell)
resolution : 0.02380881, 0.01941086  (x, y)
extent     : -89.99975, -87.99981, 36.00081, 38.00013  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : C:/NEW DSW/Scenarios/Year2015.tif 
names      : Year2015 
values     : 15.21667, 18.31667  (min, max)

How do I automate and make tif of respective years? and save as an individual tif in a specified folder

Thanks


Solution

  • Using the head of you data:

    df <- structure(list(STID = 1:6, Long = c(-88.12124, -88.09833, -88.07541, 
    -88.0525, -88.02959, -88.00668), Lat = c(37.99907, 37.99759, 
    37.99611, 37.99462, 37.99313, 37.99163), Y2015 = c(16.15833, 
    16.21667, 16.21667, 16.225, 16.225, 16.25833), Y2016 = c(16.26667, 
    16.33333, 16.33333, 16.33333, 16.33333, 16.36667), Y2017 = c(14.80833, 
    14.875, 14.875, 14.89167, 14.89167, 14.94167), Y2018 = c(14.2, 
    14.28333, 14.28333, 14.30833, 14.30833, 14.39167), Y2019 = c(14.54167, 
    14.625, 14.625, 14.65833, 14.65833, 14.725), Y2020 = c(14.75, 
    14.8, 14.8, 14.8, 14.8, 14.84167), Y2021 = c(14.83333, 14.925, 
    14.925, 14.925, 14.925, 14.93333), Y2022 = c(15.06667, 15.11667, 
    15.11667, 15.125, 15.125, 15.16667), Y2023 = c(14.2, 14.24167, 
    14.24167, 14.23333, 14.23333, 14.2583)), class = "data.frame", row.names = c(NA, 
    -6L))
    

    the simplest way is to wrap your code into a function and then lapply over indices of columns.

    lat_long<-SpatialPoints(data.frame(df[2:3]))
    tas_yearly_ssp126_15_21<-data.frame(df[4:12])
    ras_extent<-extent(lat_long)
    
    dummy_ras<-raster(ras_extent,ncol=84,nrow=103,crs="+proj=longlat +datum=WGS84")
    
    myfun <- function(x){
    fillv<-tas_yearly_ssp126_15_21[x]
    rasterize(lat_long, dummy_ras, fillv, fun='last', background=NA,mask=FALSE, 
    update=FALSE, updateValue='all', filename=paste0(names(fillv),".tif"), na.rm=TRUE)
    }
    
    lapply(1:ncol(tas_yearly_ssp126_15_21), myfun)
    

    here the outputs are saved as .tif in your working directory (in getwd()). You can change this, by add the path of the folder to the paste0 call. Es:

    rasterize(lat_long, dummy_ras, fillv, fun='last', background=NA,mask=FALSE, 
    update=FALSE, updateValue='all', filename=paste0("D:/myfolder/",names(fillv),".tif"), na.rm=TRUE)