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:
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:
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