0

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.

  • Not directly addressing the subset locally under `ncdf4`, [all data](https://psl.noaa.gov/repository/entry/show?entryid=synth%3Ae570c8f9-ec09-4e89-93b4-babd5651e7a9%3AL25jZXAucmVhbmFseXNpcy9wcmVzc3VyZS91d25kLjIwMTcubmM%3D)but you can [subset grid](https://psl.noaa.gov/repository/entry/show?entryid=synth%3Ae570c8f9-ec09-4e89-93b4-babd5651e7a9%3AL25jZXAucmVhbmFseXNpcy9wcmVzc3VyZS91d25kLjIwMTcubmM%3D&output=data.gridsubset.form) prior to download, as a way to keep you going and see if you can delimit to your study area of interest, also reducing file size. Gui upon OpenLDAP, presumably. – Chris Oct 06 '22 at 14:25
  • I tried and it still didn't work. The longitudes came from 0 to the most eastern. I'm trying a new way to solve this outside R (temporarily) and I'll keep y'all informed if it works. – Dan Sid Oct 06 '22 at 16:19
  • I've made a subset of the global data in GrADS and it worked as I intended in R. The solution seems to be this: to do the same "crop" in R and make a new NetCDF subset to work with (as it should have its own new dimension IDs that won't need to wrap around), I will be working on this after I finish this task, which is urgent. – Dan Sid Oct 10 '22 at 03:16
  • Look forward to seeing the solution. – Chris Oct 10 '22 at 03:47

0 Answers0