I have NetCDF files from NCEP/DOE Reanalysis 2, which contains data for the entire globe. That makes me want to load the values like this.
library(ncdf4)
file1 <- nc_open("uwnd.2017.nc")
ncvar_get(file1, "lon")
This outputs a vector of 144 longitude values from 0 to 357.5, (intervals = 2.5). I want longitudes ranging from 70°W to 10°E (respective indexes: 117 and 5) and other dimensions are fixed.
u.vals <- ncvar_get(file1 ,"uwnd",
start = c(117, lat.idx, lev.idx, t.idx),
count = c(abs(117-5)+1, 1, 1, 1))
I was expecting the function to wrap around the dimension vector - jumping from the last longitude index (144) to 1 again and read normally until index 5.
However, it returns these:
Error in Rsx_nc4_get_vara_double: NetCDF: Start+count exceeds dimension bound Var: uwnd Ndims: 4 Start: 164,5,32,116 Count: 1,1,1,113
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, : C function R_nc4_get_vara_double returned error
The opposite direction of reading (5 to 117) will wrap around the earth in the wrong direction, missing my area completely.