【问题标题】:How to merge netcdf files separately in R?如何在 R 中分别合并 netcdf 文件?
【发布时间】:2019-09-26 05:44:09
【问题描述】:

我是 R 的用户,希望得到以下帮助: 我有两个 netcdf 文件(每个尺寸为 30x30x365)和一个 30x30x366。这 3 个文件包含一年的每日数据,其中最后一个维度是指时间维度。我想将它们分开组合,即我希望输出文件包含 30x30x1096。

注意:我看到了一个类似的问题,但输出结果是我不想要的平均值(即 30x30x3)。

【问题讨论】:

标签: r netcdf cdo-climate


【解决方案1】:

从我在下面看到的评论中,您似乎想在时间维度上合并 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 

【讨论】:

    【解决方案2】:

    在不确切知道您拥有哪些维度和变量的情况下,这可能足以让您入门:

    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
    

    【讨论】:

    • @C. Braun 我忘了说,文件名是 file1.nc、file 2.nc 和 file3.nc。变量名称是“sst”,其中 30x30 是经度 x 纬度,365/366 表示天数。请问可以更新吗? (我这里其实是初学者)
    猜你喜欢
    • 2017-12-19
    • 2021-01-02
    • 2013-06-28
    • 1970-01-01
    • 2013-12-04
    • 2015-05-11
    • 2021-01-06
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多