Hi again ! We have another question…
We have a Raster Layer with 371 layers:
class : SpatRaster
dimensions : 35, 17, 372 (nrow, ncol, nlyr)
resolution : 2.5, 1.267606 (x, y)
extent : -1.25, 41.25, 27.25352, 71.61972 (xmin, xmax, ymin, ymax)
coord. ref. :
source(s) : memory
names : clt_301, clt_302, clt_303, clt_304, clt_305, clt_306, ...
min values : 18.14771, 11.93175, 5.477096, 14.04819, 4.086611, 0.00000, ...
max values : 99.79733, 99.92499, 99.412560, 98.52460, 95.505341, 91.06673, ...
time (days) : 2040-01-16 to 2070-12-16
Each represents a month. So the whole raster file contains data for a total of 31 years. The layers are not named by months but in ascending chronology from hurs_301 to hurs_672. Now we need to calculate the average per month over all years (January, February, March, April, May, June, July, August, September, October, November, December). The final goal is to generate a raster file that contains 12 layers, per layer the average of one month ( 1: layer average January over all 31 years, 2nd layer average February over all years etc.). We have thought of the following steps: I create a loop that names the layers from 1-12 and then calculate the average of the layers per month and stack them to a raster.
ipsl_hurs_126_future <- raster(ipsl_hurs_126_future) #read in ispl_hurs_126_future as a raster
# Create a vector of month names
month_names <- c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December")
# Create an empty RasterBrick to store the monthly means
monthly_means <- raster(nrow=nrow(ipsl_hurs_126_future), ncol=ncol(ipsl_hurs_126_future))
# Loop through each month
for (month_idx in 1:12) {
# Initialize an empty RasterLayer to store the averaged result
avg_layer <- NULL
# Get the month's layer indices
month_layers <- seq(month_idx, 371, 12)
# Loop through the month's layers and calculate the average
for (layer_idx in month_layers) {
current_layer <- ipsl_hurs_126_future[[layer_idx]]
if (is.null(avg_layer)) {
avg_layer <- current_layer
} else {
avg_layer <- avg_layer + current_layer
}
}
# Divide the accumulated sum by the number of layers to get the average
avg_layer <- avg_layer / length(month_layers)
# Set a meaningful name for the averaged layer
averaged_layer_name <- paste("avg_hurs_", month_names[month_idx], sep = "")
# Assign the averaged layer to the RasterBrick
monthly_means[[month_idx]] <- avg_layer
}
# Print the resulting monthly means SpatRaster
print(monthly_means)
class(monthly_means)
Unfortunately i end up with this:
class : RasterLayer
dimensions : 35, 17, 595 (nrow, ncol, ncell)
resolution : 21.17647, 5.142857 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
Which is not what we want. Do you know how to solve this problem or how to do it correctly? Thanks a lot in advance! Also we give you access to the new project blina/templatecloud-forrest (it is the same but we had to redo one bc Renku was bugging too much). The name of the file is delta_change_method.R