Skip to content

Instantly share code, notes, and snippets.

@guziy
Last active June 30, 2022 04:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save guziy/8543562 to your computer and use it in GitHub Desktop.
Save guziy/8543562 to your computer and use it in GitHub Desktop.
A quick way to copy variables and metadata from one netcdf file to another using netcdf4-python
# -*- coding: utf-8 -*-
from netCDF4 import Dataset
#input file
dsin = Dataset("crop.nc")
#output file
dsout = Dataset("crop.nc3", "w", format="NETCDF3_CLASSIC")
#Copy dimensions
for dname, the_dim in dsin.dimensions.iteritems():
print dname, len(the_dim)
dsout.createDimension(dname, len(the_dim) if not the_dim.isunlimited() else None)
# Copy variables
for v_name, varin in dsin.variables.iteritems():
outVar = dsout.createVariable(v_name, varin.datatype, varin.dimensions)
print varin.datatype
# Copy variable attributes
outVar.setncatts({k: varin.getncattr(k) for k in varin.ncattrs()})
outVar[:] = varin[:]
# close the output file
dsout.close()
@guziy
Copy link
Author

guziy commented Jul 19, 2020

I have just tested a python3 version of the script on a file I have:

https://github.com/guziy/PyNotebooks/blob/master/netcdf/test_copy.ipynb

Is it possible for you to share the problematic file? Just a small part of it that causes the error.

@ksaha79
Copy link

ksaha79 commented Jul 19, 2020

The input files I am using can be found here: ftp://ftp.nodc.noaa.gov/pub/data.nodc/ghrsst/GDS2/L4/GLOB/NCEI/AVHRR_OI/v2/2020/085/20200325120000-NCEI-L4_GHRSST-SSTblend-AVHRR_OI-GLOB-v02.0-fv02.0.nc

@guziy
Copy link
Author

guziy commented Jul 19, 2020

I have updated the example, by adding a special treatment for the _FillValue attribute.

https://github.com/guziy/PyNotebooks/blob/master/netcdf/test_copy.ipynb

I wanted to point out that there is maybe a more elegant way of doing it now with xarray:

with xarray.open_dataset("test.nc") as ds:
       # do things with ds1 = f(ds)
       ds1.to_netcdf("copy_test.nc")

@ksaha79
Copy link

ksaha79 commented Jul 19, 2020

Thanks That worked for me.
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment