【发布时间】:2019-09-26 05:44:09
【问题描述】:
我是 R 的用户,希望得到以下帮助: 我有两个 netcdf 文件(每个尺寸为 30x30x365)和一个 30x30x366。这 3 个文件包含一年的每日数据,其中最后一个维度是指时间维度。我想将它们分开组合,即我希望输出文件包含 30x30x1096。
注意:我看到了一个类似的问题,但输出结果是我不想要的平均值(即 30x30x3)。
【问题讨论】:
标签: r netcdf cdo-climate
我是 R 的用户,希望得到以下帮助: 我有两个 netcdf 文件(每个尺寸为 30x30x365)和一个 30x30x366。这 3 个文件包含一年的每日数据,其中最后一个维度是指时间维度。我想将它们分开组合,即我希望输出文件包含 30x30x1096。
注意:我看到了一个类似的问题,但输出结果是我不想要的平均值(即 30x30x3)。
【问题讨论】:
标签: r netcdf cdo-climate
从我在下面看到的评论中,您似乎想在时间维度上合并 3 个文件。作为 R 的替代方案,您可以使用 cdo(气候数据运算符)从命令行快速执行此操作:
cdo mergetime file1.nc file2.nc file3.nc mergedfile.nc
或使用通配符:
cdo mergetime file?.nc mergedfile.nc
在ubuntu下cdo很容易安装:
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
【讨论】: