1

I'm currently working on climatic raster files. I have oceanographic data for 3 years during the months of january and february at a daily resolution. I have a loop reading .nc files and converting them into rasterstacks. Then, I use the function 'calc' to compute the mean value of all layers of my rasterstack. It worked yesterday when I used it, but today I have an error saying:

Error in (function (classes, fdef, mtable): unable to find an inherited method for function 'calc' for signature '"RasterStack", "RasterLayer"'

I cannot import data as it is heavier than 50Go, but here is the part of my script where I use the 'calc' function:

for(file in nc_files){
 rast <- raster::raster(x = paste("./Data/Raw/Phy/", file, sep=""), varname = var)
 rast <- raster::resample(x = rast, y = r)
 stack <- raster::addLayer(stack, rast)
 }
mean_r <- calc(stack, fun = mean)
sd_r <- calc(stack, fun = sd)

I have not changed my script or my files since yesterday nor have I made a package update, so I don't understand why it's not working today. Thank you very much for your help!

Edit:

It seems to be more difficult than just a question of function. When I launch this code chunk, then there is no issue with the calc function:

nc_varname <- c("thetao", "so", "mlotst", "uo", "vo")
## Climato de surface ----
nc_files <- list.files(path = "./Data/Raw/Phy/", pattern = ".nc")
r <- raster(xmn=40, xmx=90, ymn=-60, ymx=-20, resolution = 0.08333333)
for(var in nc_varname){
 
 stack <- r
 stack_grad <- r
 
 for(file in nc_files){
 rast <- raster::raster(x = paste("./Data/Raw/Phy/", file, sep=""), varname = var)
 rast <- raster::resample(x = rast, y = r)
 stack <- raster::addLayer(stack, rast)
 grad <- grec::getGradients(x = rast, method = "BelkinOReilly2009")
 stack_grad <- addLayer(stack_grad, grad)
 }
 
 # Writing raw data stack
 writeRaster(x = stack, filename = paste("./Data/Treated/Phy/", 
 var, " stack", sep=""), 
 format = "raster", overwrite = T)
 
 # Mean
 print(class(stack))
 mean_r <- calc(stack, fun = mean)
 writeRaster(x = mean_r, filename = paste("./Data/Treated/Phy/",
 var, " mean raster", sep=""), 
 format = "raster", overwrite = T)
 
}

The code chunk above reads each .nc file into a RasterLayer format and add it to the RasterStack, and then compute, using the calc function, the mean, sd, etc... on all the data. But when I run the chunk below on the same files in order to compute the monthly mean and , that is where the error occurs, even if it's still a RasterStack object:

## Raster par année et par mois ----
nc_files <- list.files(path = "./Data/Raw/Phy/", pattern = ".nc")
r <- raster(xmn=40, xmx=90, ymn=-60, ymx=-20, resolution = 0.08333333)
nc_varname <- c("thetao", "so", "uo", "vo")
prof <- c("0.49", "186")
prof_name <- c("0m", "200m")
years <- c("2017", "2018", "2019")
months <- c("janv", "fev")
for(var in nc_varname){
 stack <- r
 stack_grad <- r
 
 for(year in years){
 file_list <- nc_files[grep(pattern = year, x = nc_files)]
 
 for(month in months){
 file_list <- file_list[grep(pattern = month, x = file_list)]
 
 for(p in prof){
 
 for(file in file_list){
 rast <- brick(x = paste("./Data/Raw/Phy/", file, sep=""), 
 varname = var)
 
 rast <- rast[[grep(pattern = p, names(rast))]]
 rast <- raster::resample(x = rast, y = r)
 stack <- raster::addLayer(stack, rast)
 grad <- grec::getGradients(x = rast, method = "BelkinOReilly2009")
 stack_grad <- addLayer(stack_grad, grad)
 print(length(names(stack_grad)))
 
 }
 
 writeRaster(x = stack, 
 filename = paste("./Data/Treated/Phy/", 
 var, " ", year, " ", 
 month, " ", 
 prof_name[grep(pattern = p, x = prof)],
 " raster layers", 
 sep=""), 
 format = "raster", overwrite = T)
 
 # Mean
 mean <- calc(stack, fun = mean)
 writeRaster(x = mean, 
 filename = paste("./Data/Treated/Phy/", 
 var, " ", year, " ", 
 month, "", 
 prof_name[grep(pattern = p, x = prof)], 
 " mean raster", 
 sep=""),
 format = "raster", overwrite = T)
 
 }
 }
 }
}

And when I try to run the first chunk after running the second, I also have the same error popping up, even though I don't if I run the first chunk first. Does anyone encountered a similar issue ?

1 Answer 1

1

The error message is pretty clear when you carefully consider it

Error in (function (classes, fdef, mtable): unable to find an inherited method for function 'calc' for signature '"RasterStack", "RasterLayer"'

If you look at the manual you can see that calc is defined for

S4 method for signature 'Raster,function'

Apparently, you have a RasterLayer called mean and/or sd (you do not show where the error occurs so I cannot say; and that is what you are using as argument to calc where you mean to pass the function with that name. One way to avoid that confusion is to not give data objects the same name as functions. Another approach would be to use

mean_r <- calc(stack, fun = "mean")
sd_r <- calc(stack, fun = "sd")

or

mean_r <- calc(stack, fun = base::mean)
sd_r <- calc(stack, fun = stats::sd)
answered Jan 11, 2024 at 18:42
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Robert, thank you for your answer! The error only showed when running the second chunk, so that's surely the reason why it didn't work compared to the first chunk. I swapped to the terra package using the app function, which does the same thing as the calc function from the raster package. Thank u for your help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.