无法提取我在 Panoply 中看到的 R 中的 netcdf 变量
Unable to extract a netcdf variable in R that I see in Panoply
我有一个包含气候数据的 netcdf 文件。我用 panoply (https://www.giss.nasa.gov/tools/panoply/) 探索了它,我看到了以下变量:
- lat (1D) - 每个网格单元中参考纬度的列表
- lat_bnds (2D) - 每个网格单元的两列 min/max 纬度
- lon (1D) - 与纬度相同,但经度
- lon_bnds (2D) - 与经度 lat_bnds 相同
- tas(Geo2D;近地表气温)
- time (1D): 变量tas的第3维时间步列表
- time_bnds (2D):两列具有 max/min tas 第三维度的时间范围
我现在需要使用 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
汇总输出:
- 文件名(路径+文件名的字符串)
- 可写(TRUE/FALSE)
- Id(一个不知道是什么的数字)
- 安全模式 (TRUE/FALSE)
- 格式(“NC_FORMAT_NETCDF4_CLASSIC”)
- Is_GMT (TRUE/FALSE)
- 组(包含其他变量的列表):Id/name/ndims/nvars/natts/dimid/fqgn(每个变量有一个整数;不知道它们到底是什么)
- Fqgn2Rindex(包含其他变量的列表):只有一个变量“[1]”,整数为 4(不知道这是什么)
- Ndims(维数:4)
- Natts(属性数:1)
- Dim(包含其他变量的列表):-> 这是存储 time/lat/lon 的地方
- 时间:Name/len/unlim/group_index/group_id/id/units/calendar/vals/create_dimvar/dimvarid(最后一个是另一组看似杂乱的变量)
- 绑定:与上面的列表类似,变量“vals”= [1,2] 所以,这里没有坐标限制
- 纬度(看这张截图here)
- Lon(无截图,但与纬度相似)
- Unlimdimid(某物的维度:1)
- Nvars(变量数:4 -> 在这种情况下,我只使用一般 time/lat/lon 提取温度)
- Var(包含所有变量的组):
- Time_bnds:这是另一组变量,除了维度和一些额外的混乱之外什么都没有——不知道为什么这是在上面的“var”下面而不是在“time”下面
- Lat_bnds: 这个我已经截图了here)
- Lon_bnds: 没有截图,与 lat
相似
- Tas(数据本身不含lat/lons)
使用 Panoply:
变量 lat_bnds(我最感兴趣的那个)在提取为 CSV 格式时如下所示:
two columns of min/max latitude ranges. Size: 128*2
在 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
问题: 我无法在 R 中提取 lat_bnds 变量,因为我只得到与 panoply 中的 lat 变量相同的纬度字符串(在 R 中 - 如上面的第一次提取所示)。任何对我做错的帮助将不胜感激!
使用适当的函数提取变量 ncdf4::ncvar_get
似乎可行:
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
我有一个包含气候数据的 netcdf 文件。我用 panoply (https://www.giss.nasa.gov/tools/panoply/) 探索了它,我看到了以下变量:
- lat (1D) - 每个网格单元中参考纬度的列表
- lat_bnds (2D) - 每个网格单元的两列 min/max 纬度
- lon (1D) - 与纬度相同,但经度
- lon_bnds (2D) - 与经度 lat_bnds 相同
- tas(Geo2D;近地表气温)
- time (1D): 变量tas的第3维时间步列表
- time_bnds (2D):两列具有 max/min tas 第三维度的时间范围
我现在需要使用 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
汇总输出:
- 文件名(路径+文件名的字符串)
- 可写(TRUE/FALSE)
- Id(一个不知道是什么的数字)
- 安全模式 (TRUE/FALSE)
- 格式(“NC_FORMAT_NETCDF4_CLASSIC”)
- Is_GMT (TRUE/FALSE)
- 组(包含其他变量的列表):Id/name/ndims/nvars/natts/dimid/fqgn(每个变量有一个整数;不知道它们到底是什么)
- Fqgn2Rindex(包含其他变量的列表):只有一个变量“[1]”,整数为 4(不知道这是什么)
- Ndims(维数:4)
- Natts(属性数:1)
- Dim(包含其他变量的列表):-> 这是存储 time/lat/lon 的地方
- 时间:Name/len/unlim/group_index/group_id/id/units/calendar/vals/create_dimvar/dimvarid(最后一个是另一组看似杂乱的变量)
- 绑定:与上面的列表类似,变量“vals”= [1,2] 所以,这里没有坐标限制
- 纬度(看这张截图here)
- Lon(无截图,但与纬度相似)
- Unlimdimid(某物的维度:1)
- Nvars(变量数:4 -> 在这种情况下,我只使用一般 time/lat/lon 提取温度)
- Var(包含所有变量的组):
- Time_bnds:这是另一组变量,除了维度和一些额外的混乱之外什么都没有——不知道为什么这是在上面的“var”下面而不是在“time”下面
- Lat_bnds: 这个我已经截图了here)
- Lon_bnds: 没有截图,与 lat 相似
- Tas(数据本身不含lat/lons)
使用 Panoply: 变量 lat_bnds(我最感兴趣的那个)在提取为 CSV 格式时如下所示: two columns of min/max latitude ranges. Size: 128*2
在 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
问题: 我无法在 R 中提取 lat_bnds 变量,因为我只得到与 panoply 中的 lat 变量相同的纬度字符串(在 R 中 - 如上面的第一次提取所示)。任何对我做错的帮助将不胜感激!
使用适当的函数提取变量 ncdf4::ncvar_get
似乎可行:
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