0

I want to get quantiles of each cell across layers on a stacked set of RasterLayers on R.

My RasterStack looks like this

dimensions : 721, 1440, 1038240, 18628 (nrow, ncol, ncell, nlayers)

I run the following line to get a layer whose values are 25th and 75th percentiles for each cell

qtiles <- raster::calc(RasterStack, fun=function(x){quantile(x, probs= c(0.25,0.75), na.rm=TRUE)})

I get the following error:

Error in R_nc4_open: NETCDF: HDF error
Error in ncdf4::nc_open(x@file@name, suppress_dimvals = TRUE) :
Error in nc_open trying to open file U:path/file1.nc (return_on_error= FALSE )

I suspect it's because of the number of layers since when I try it on a subset of netcdf files, it works fine, and I also have no problems in opening file1.nc separately.

Any suggestions?

0

1 Answer 1

0

I would recommend moving your analysis to the terra package as raster is being depreciated. You can read in your ncdf raster stack using terra::rast, there are no longer different object classes (ie., raster, brick, stack) for single or multiband rasters. Then use the terra::quantile function to calculate the percentiles. If you are still receiving an error along the lines of what you posted, you likely have a corrupt layer in the NetCDF file.

1
  • thanks, terra did make things much faster! One weird thing is that if I compute quantiles in probs= c(0.9,0.95,0.99) I get what I want. But if I compute quantiles in probs= c(0.01,0.05,0.1), I obtain three layers of zeros anywhere (which is impossible). Any idea?
    – gcoder
    Commented Feb 2, 2023 at 8:32

Your Answer

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

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