我是R的用户,并希望在以下方面提供一些帮助:我有两个netcdf文件(每个尺寸为30x30x365)和另一个30x30x366。这3个文件包含一年的每日数据,其中最后一个维度是指时间维度。我想单独组合它们,即我希望输出文件包含30x30x1096。
注意:我看到了类似的问题,但输出结果是平均值(即30x30x3),我不想要。
从我在下面看到的评论你似乎想要在时间维度合并3个文件。作为R的替代方案,您可以使用cdo(气候数据运算符)从命令行快速完成此操作:
cdo mergetime file1.nc file2.nc file3.nc mergedfile.nc
或使用通配符:
cdo mergetime file?.nc mergedfile.nc
cdo很容易在ubuntu下安装:
sudo apt install cdo
如果不确切知道您拥有的维度和变量,这可能足以让您入门:
library(ncdf4)
output_data <- array(dim = c(30, 30, 1096))
files <- c('file1.nc', 'file2.nc', 'file3.nc')
days <- c(365, 365, 366)
# Open each file and add it to the final output array
for (i in seq_along(files)) {
nc <- nc_open(files[i])
input_arr <- ncvar_get(nc, varid='var_name')
nc_close(nc)
# Calculate the indices where each file's data should go
if (i > 1) {
day_idx <- (1:days[i]) + sum(days[1:(i-1)])
} else {
day_idx <- 1:days[i]
}
output_data[ , , day_idx] <- input_arr
}
# Write out output_data to a NetCDF. How exactly this should be done depends on what
# dimensions and variables you have.
# See here for more:
# https://publicwiki.deltares.nl/display/OET/Creating+a+netCDF+file+with+R