1

I have raster bricks of monthly max temperature, min temperature, and precipitation for various time periods (e.g., from 2090-2100, in this example).

> tmax
class      : RasterBrick 
dimensions : 3875, 8017, 31065875, 132  (nrow, ncol, ncell, nlayers)
resolution : 0.04490319, 0.04490319  (x, y)
extent     : -180.0393, 179.9495, -90.04088, 83.95898  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 
source     : Z:/CHELSAcmip5ts_tasmax_ACCESS1-3_rcp85_2090-2100.nc 
names      : X2090.01.15, X2090.02.15, X2090.03.15, X2090.04.15, X2090.05.15, X2090.06.15, X2090.07.15, X2090.08.15, X2090.09.15, X2090.10.15, X2090.11.15, X2090.12.15, X2091.01.15, X2091.02.15, X2091.03.15, ... 
Date       : 2090-01-15, 2100-12-15 (min, max)
varname    : air_temperature 

I need to (a) create individual raster stacks consisting of 12 layers for each year in the period. I know I can do the following to subset and save the individual rasters:

sub_ras <- subset(tmax,  grep("X2090", names(tmax)))  
writeRaster(sub_ras, file="tmax2090", format="GTiff")

But I am not sure how to loop this? I'll eventually need to do this for a raster brick of monthly data from 1970 to 2100.

Once I have my individual raster stacks for each year, I'll then need to read them in individually into the biovars function to create bioclim variables for each year. Shall I then use mapply to do this as follows:

pre = c("/path/pre2090.tif", "/path/pre2091.tif", ..., "/path/prec2100.tif")
tmax = c("/path/tmax2090.tif", "/path/tmax2091.tif", ..., "/path/tmax2100.tif")
tmin = c("/path/tmin2090.tif", "/path/tmin2091.tif", ..., "/path/tmin2100.tif")

output <- mapply(FUN = biovars, pre, tmin, tmax)

Will this give me a biovars raster for each year?

Or, does someone have a more elegant suggestion for this process?

1
  • 1
    Please use code formatted text rather than a screenshot. – Aaron Oct 23 '20 at 20:24
2

As for the loop part, the following code iterates between 90 and 100 writing a layer for each year; the layers will have bands for each month:

library(raster)
library(dismo)

for(i in 90:100){ 
sub_ras <- subset(tmax,  grep(sprintf("X2%03d", i), names(tmax)))  
writeRaster(sub_ras, file=sprintf("tmax2%03d.tif", i))
}

To get all 12 layers for each year we've written to file you may use stack("tmax2090.tif"). And instead of writing each file name you may use:

tmax_names = list.files("path", "tmax", full.names = T)

Then you may use the mapply function as you intended.

The same objective may be reached if you directly calculate the biovars in the for loop, this way you may avoid duplicating the files, for instance:

for(i in 90:100){ 
sub_tmax <- subset(tmax,  grep(sprintf("X2%03d", i), names(tmax)))  
sub_tmin <- subset(tmin,  grep(sprintf("X2%03d", i), names(tmin)))
sub_prec <- subset(prec,  grep(sprintf("X2%03d", i), names(prec)))
bv_temp = biovars(sub_prec, sub_tmin, sub_tmax)  
writeRaster(bv_temp, file=sprintf("biovars2%03d.tif", i))
}

Furthermore, if you want to stack them together (all the 12*20):

bv = stack()
for(i in 90:100){ 
sub_tmax <- subset(tmax,  grep(sprintf("X2%03d", i), names(tmax)))  
sub_tmin <- subset(tmin,  grep(sprintf("X2%03d", i), names(tmin)))
sub_prec <- subset(prec,  grep(sprintf("X2%03d", i), names(prec)))
bv_temp = biovars(sub_prec, sub_tmin, sub_tmax)  
colnames(bv_temp) = paste0(sprintf("X2%03d_", i), colnames(bv_temp))
bv = stack(bv, bv_temp)
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.