简体   繁体   中英

Unable to extract a netcdf variable in R that I see in Panoply

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
  1. 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)
  2. Bound: Similar list than above with the variable “vals” = [1,2] so, no coord limits here
  3. Lat (see a screenshot of this one here )
  4. 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):
  1. 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
  2. Lat_bnds: I've done a screenshot of this one here )
  3. Lon_bnds: No screenshot, similar than for lat
  4. 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!

Extracting the variables using the appropriate function ncdf4::ncvar_get seems to work:

df <- ncdf4::nc_open(filename = "MIROC-ES2L_LGM_moclim_tas.nc")
ncdf4::ncvar_get(df, "lat_bnds")

          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]      [,9]     [,10]     [,11]     [,12]     [,13]
[1,] -90.00000 -86.57775 -83.75703 -80.95502 -78.15835 -75.36394 -72.57070 -69.77815 -66.98603 -64.19420 -61.40258 -58.61111 -55.81976
[2,] -86.57775 -83.75703 -80.95502 -78.15835 -75.36394 -72.57070 -69.77815 -66.98603 -64.19420 -61.40258 -58.61111 -55.81976 -53.02849
         [,14]     [,15]     [,16]     [,17]     [,18]     [,19]     [,20]     [,21]     [,22]     [,23]     [,24]     [,25]     [,26]
[1,] -53.02849 -50.23730 -47.44615 -44.65506 -41.86400 -39.07297 -36.28196 -33.49098 -30.70002 -27.90906 -25.11813 -22.32720 -19.53628
[2,] -50.23730 -47.44615 -44.65506 -41.86400 -39.07297 -36.28196 -33.49098 -30.70002 -27.90906 -25.11813 -22.32720 -19.53628 -16.74537
         [,27]     [,28]      [,29]     [,30]     [,31]    [,32]   [,33]    [,34]    [,35]     [,36]    [,37]    [,38]    [,39]
[1,] -16.74537 -13.95447 -11.163569 -8.372673 -5.581781 -2.79089 0.00000 2.790890 5.581781  8.372673 11.16357 13.95447 16.74537
[2,] -13.95447 -11.16357  -8.372673 -5.581781 -2.790890  0.00000 2.79089 5.581781 8.372673 11.163569 13.95447 16.74537 19.53628
        [,40]    [,41]    [,42]    [,43]    [,44]    [,45]    [,46]    [,47]    [,48]    [,49]    [,50]    [,51]    [,52]    [,53]
[1,] 19.53628 22.32720 25.11813 27.90906 30.70002 33.49098 36.28196 39.07297 41.86400 44.65506 47.44615 50.23730 53.02849 55.81976
[2,] 22.32720 25.11813 27.90906 30.70002 33.49098 36.28196 39.07297 41.86400 44.65506 47.44615 50.23730 53.02849 55.81976 58.61111
        [,54]    [,55]    [,56]    [,57]    [,58]    [,59]    [,60]    [,61]    [,62]    [,63]    [,64]
[1,] 58.61111 61.40258 64.19420 66.98603 69.77815 72.57070 75.36394 78.15835 80.95502 83.75703 86.57775
[2,] 61.40258 64.19420 66.98603 69.77815 72.57070 75.36394 78.15835 80.95502 83.75703 86.57775 90.00000

ncdf4::ncvar_get(df, "lat")

[1] -87.863799 -85.096527 -82.312913 -79.525607 -76.736900 -73.947515 -71.157752 -68.367756 -65.577607 -62.787352 -59.997020
[12] -57.206632 -54.416200 -51.625734 -48.835241 -46.044727 -43.254195 -40.463648 -37.673090 -34.882521 -32.091944 -29.301360
[23] -26.510769 -23.720174 -20.929574 -18.138971 -15.348365 -12.557756  -9.767146  -6.976534  -4.185921  -1.395307   1.395307
[34]   4.185921   6.976534   9.767146  12.557756  15.348365  18.138971  20.929574  23.720174  26.510769  29.301360  32.091944
[45]  34.882521  37.673090  40.463648  43.254195  46.044727  48.835241  51.625734  54.416200  57.206632  59.997020  62.787352
[56]  65.577607  68.367756  71.157752  73.947515  76.736900  79.525607  82.312913  85.096527  87.863799

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM