使用栅格包中的 brick 函数提取具有 4 个以上维度的 NetCDF 变量

Extract NetCDF variable that have more than 4 dimension using brick function in raster package

我想从具有五个维度(i、j、tile、k、时间)的 netCDF 文件中提取一个名为 NVEL 的变量 其中 i 是经度,j 是纬度,k 是深度级别 我想提取 NVEL(i, j, tile=3, k=1st level, time) 输入文件可以从这里下载 https://drive.google.com/file/d/12NQp_uLr_IZLLU6Fzr555gKGGJlrRE4H/view?usp=sharing

NVEL<- brick("NVEL_1992_01.nc", varname= "NVEL", lvar=1, nl=1)
NVEL <- NVEL[[which(getZ(NVEL) == 3)]]

这不起作用。 如何处理一个5维的变量?

我看到这个 returns 50 (k) * 13 (tiles) * 1 (time) = 650 layers

library(terra)
f <- "NVEL_1992_01.nc"
x <- rast(f)
x
#class       : SpatRaster 
#dimensions  : 90, 90, 650  (nrow, ncol, nlyr)
#resolution  : 1, 1  (x, y)
#extent      : -0.5, 89.5, -0.5, 89.5  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#data source : NVEL_1992_01.nc 
#names       : NVE_1, NVE_2, NVE_3, NVE_4, NVE_5, NVE_6, ... 

顺序是 k-wise(和瓦片内的瓦片式)。请参阅

的(相当冗长的)输出
terra::describe(f)

您可以像这样提取该信息:

d <- describe(f, print=FALSE)
d <- unlist(strsplit(d, "\n"))
i <- grep("NETCDF_DIM_k=", d)
j <- grep("NETCDF_DIM_tile=", d)
k <- sapply(strsplit(d[i], "="), function(x) x[2])
tile <- sapply(strsplit(d[j], "="), function(x) x[2])
kt <- paste0("k", k, "_tile", tile)
names(x) <- kt
x
#class       : SpatRaster 
#dimensions  : 90, 90, 650  (nrow, ncol, nlyr)
#resolution  : 1, 1  (x, y)
#extent      : -0.5, 89.5, -0.5, 89.5  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#data source : NVEL_1992_01.nc 
#names       : k0_tile0, k0_tile1, k0_tile2, k0_tile3, k0_tile4, k0_tile5, ... 

这应该会在未来的版本中自动发生。您可以继续 terra(与 raster 非常相似)或通过执行

将数据返回到 RasterBrick
b <- brick(x*1) 

(乘以从文件中获取值)