python netcdf: making a copy of all variables and attributes but one

后端 未结 5 1026
南笙
南笙 2020-12-09 10:42

I need to process a single variable in a netcdf file that actually contains many attributes and variable. I think it is not possible to update a netcdf file (see question Ho

5条回答
  •  旧时难觅i
    2020-12-09 11:32

    I know this is an old question, but as an alternative, you can use the library netcdf and shutil:

    import shutil
    from netcdf import netcdf as nc
    
    def processing(infile, variable, outfile):
        shutil.copyfile(infile, outfile)
        with nc.loader(infile) as in_root, nc.loader(outfile) as out_root:
            data = nc.getvar(in_root, variable)
            # do your processing with data and save them as memory "values"...
            values = data[:] * 3
            new_var = nc.getvar(out_root, variable, source=data)
            new_var[:] = values
    

提交回复
热议问题