I have a netcdf file with climate data. I've explored it with panoply (https://www.giss.nasa.gov/tools/panoply/) where I see the following variables:
- lat (1D) - list of referenced latitude in each gridcell
- lat_bnds (2D) - two columns of min/max latitude for each gridcell
- lon (1D) - same as lat but for longitude
- lon_bnds (2D) - same as lat_bnds for longitude
- tas (Geo2D; Near-Surface Air Temperature)
- time (1D): list of time steps in the 3rd dimension of the variable tas
- time_bnds (2D): two columns with max/min temporal ranges of the tas' 3rd dimension
I now need to extract these variables using R.
ncpath <- ("[PATH WHERE FILE IS SAVED]")
ncfname <-paste(ncpath,'[FILENAME].nc',sep = '')
ncin <- nc_open(ncfname) # open netcdf file
view(ncin) # to see the structure of the netcdf, which is messier than what appears in panoply
Summarised output:
- Filename (a string with the path+filename)
- Writeable (TRUE/FALSE)
- Id (a number that I don’t know what it is)
- Safemode (TRUE/FALSE)
- Format (“NC_FORMAT_NETCDF4_CLASSIC”)
- Is_GMT (TRUE/FALSE)
- Groups (a list with other variables): Id/name/ndims/nvars/natts/dimid/fqgn (there’s one single integer per variable; no idea what they are exactly)
- Fqgn2Rindex (a list with other variables): There’s just one variable “[1]” with the integer 4 (no idea what this is)
- Ndims (number of dimensions: 4)
- Natts (number of attributes: 1)
- Dim (list with other variables): -> this is where time/lat/lon are stored
- Time: Name/len/unlim/group_index/group_id/id/units/calendar/vals/create_dimvar/dimvarid (the last one is another group of variables with what seems just clutter)
- Bound: Similar list than above with the variable “vals” = [1,2] so, no coord limits here
- Lat (see a screenshot of this one here)
- Lon (No screenshot, but it’s similar than for lat)
- Unlimdimid (dimension of something: 1)
- Nvars (number of variables: 4 -> in this case I’ve only extracted temperature with the general time/lat/lon)
- Var (group with all the variables):
- Time_bnds: this is another set of variables with nothing more than the dimensions and some extra clutter – not sure why this is under “var” instead of under “time” above
- Lat_bnds: I’ve done a screenshot of this one here)
- Lon_bnds: No screenshot, similar than for lat
- Tas (the data itself without lat/lons)
Using Panoply: The variable lat_bnds (the one I'm mostly interested in), when extracted as CSV, looks like: two columns of min/max latitude ranges. Size: 128*2
in R:
ncin[["dim"]][["lat"]][["vals"]]”
-87.863799 -85.096527 -82.312913 -79.525607 -76.736900 -73.947515 -71.157752 -68.367756 -65.577607 -62.787352 -59.997020 -57.206632 -54.416200 -51.625734 -48.835241 -46.044727 -43.254195 -40.463648 -37.673090 -34.882521 -32.091944 -29.301360 -26.510769 -23.720174 -20.929574 -18.138971 -15.348365 -12.557756 -9.767146 -6.976534 -4.185921 -1.395307 1.395307 4.185921 6.976534 9.767146 12.557756 15.348365 18.138971 20.929574 23.720174 26.510769 29.301360 32.091944 34.882521 37.673090 40.463648 43.254195 46.044727 48.835241 51.625734 54.416200 57.206632 59.997020 62.787352 65.577607 68.367756 71.157752 73.947515 76.736900 79.525607 82.312913 85.096527 87.863799
ncin[["var"]][["lat_bnds"]][["dim"]][[2]][["vals"]]
-87.863799 -85.096527 -82.312913 -79.525607 -76.736900 -73.947515 -71.157752 -68.367756 -65.577607 -62.787352 -59.997020 -57.206632 -54.416200 -51.625734 -48.835241 -46.044727 -43.254195 -40.463648 -37.673090 -34.882521 -32.091944 -29.301360 -26.510769 -23.720174 -20.929574 -18.138971 -15.348365 -12.557756 -9.767146 -6.976534 -4.185921 -1.395307 1.395307 4.185921 6.976534 9.767146 12.557756 15.348365 18.138971 20.929574 23.720174 26.510769 29.301360 32.091944 34.882521 37.673090 40.463648 43.254195 46.044727 48.835241 51.625734 54.416200 57.206632 59.997020 62.787352 65.577607 68.367756 71.157752 73.947515 76.736900 79.525607 82.312913 85.096527 87.863799
Problem: I'm not able to extract the lat_bnds variable in R as I only get the same string of latitudes than for the lat variable in panoply (and in R - as seen in the first extraction above). Any help on what I'm doing wrong would be much appreciated!