您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用python复制netcdf文件

使用python复制netcdf文件

我在python netcdf上找到了此问题的答案:制作了所有变量和属性的一个副本,但一个副本,但我需要对其进行更改以使其与我的python / netCDF4版本(Python 2.7.6 / 1.0.4)一起使用。如果需要添加或减去元素,则可以进行适当的修改

import netCDF4 as nc

def create_file_from_source(src_file, trg_file):
    src = nc.Dataset(src_file)
    trg = nc.Dataset(trg_file, mode='w')

    # Create the dimensions of the file
    for name, dim in src.dimensions.items():
        trg.createDimension(name, len(dim) if not dim.isunlimited() else None)

    # Copy the global attributes
    trg.setncatts({a:src.getncattr(a) for a in src.ncattrs()})

    # Create the variables in the file
    for name, var in src.variables.items():
        trg.createVariable(name, var.dtype, var.dimensions)

        # Copy the variable attributes
        trg.variables[name].setncatts({a:var.getncattr(a) for a in var.ncattrs()})

        # Copy the variables values (as 'f4' eventually)
        trg.variables[name][:] = src.variables[name][:]

    # Save the file
    trg.close()

create_file_from_source('in.nc', 'out.nc')

代码段已经过测试。

python 2022/1/1 18:27:52 有192人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶