Stacking an Existing Rasterstack Multiple Times

Stacking an existing RasterStack multiple times

You might like mget here, since it takes a character vector of object names and returns the object, so you can do this:

big.stack <- stack( mget( rep( "s" , 12 ) ) )

nlayers( big.stack )
#[1] 144

Or using replicate() to put them in a list if you don't like using mget(), and then stacking the list as a list of rasterLayers is valid input to stack()...

ll <- replicate( 12 , s )

big.stack2 <- stack( ll )

identical( big.stack , big.stack2 )
#[1] TRUE

copy rasters within stack

Something like this?

# example data
r <- raster(ncol=10, nrow=10)
r[]=1:ncell(r)
x <- brick(r,r,r,r,r,r)
x <- x * 1:6

y <- list()
for (i in 1:nlayers(x)) {
r <- raster(x, i)
y <- c(y, r, r, r)
}
s <- stack(y)

Computing weights from latitude to apply to raster stack values

I think this approach should work.

library(raster)
r <- landCO2[[1]] # Extracts one layer of the raster brick to pull info from. Might not be necessary

## Create raster layer that has the latitude values for each cell. using method from <https://stackoverflow.com/questions/22848836/r-assigning-x-or-y-coordinate-to-cells-of-a-raster-to-perform-calculations>
lats <- raster(matrix(yFromCell(r, 1:length(r)), nrow = 64, byrow = TRUE), #get latitude from existing raster
xmn = -181.4062, xmx= 178.5938, ymn = -89.25846, ymx = 89.25846 , # Set the extents of the raster
crs = crs(r)) # Using projection of existing raster
weights <- cos(lats*(pi/180)) #Feed this raster into your weighting formula.
plot(weights) # to see what we're looking at

Sample Image

This looks reasonable with weights around 1 near the equator and falling off sharply as you approach the poles.

You can now use raster algebra to apply the weights to your RasterBrick.\

CO2weighted <- landCO2 * weights # this should be a RasterBrick with 138 layers. 


Related Topics



Leave a reply



Submit