How to subsett a 3D NetCDF file?

27 Views Asked by At

I'm having trouble reading a 8Gb NetCDF file and need to subset it to a given area. Here is my code:

Setup:

library(ncdf4)

nc_data <- nc_open('~/spei48.nc')
print(nc_data) 

lat <- ncvar_get(nc_data,"lat")
lon <- ncvar_get(nc_data, "lon")

Getting the coordinates for Nigeria only:

lat_n <- lat[lat >= 4.32 & lat <= 13.73] 
lon_n <- lon[lon >= 2.73 & lon <= 14.22]

The time variable:

time <- ncvar_get(nc_data,"time")
tunits <- ncatt_get(nc_data,"time","units")
time_obs <- as.Date(time -1, origin = "1900-01-01")

Subsetting:

Nigeria <- ncvar_get(nc_data, "spei", 
     start=c(lat_n[1],lon_n[1]), time,
     count=c(length(lat_n),length(lon_n), 1))

However I get the following error:

Error in if (verbose) print(paste("ncvar_get: entering for read from file",  : 
the condition has length > 1

Any help would be much appreciated.

I tried the code above following advice from this question.

0

There are 0 best solutions below