Stand With Ukraine

Tuesday, January 21, 2014

netcdf4-python copying variables from one netcdf file to another

This has arisen as a problem of converting NETCDF4 to NETCDF3_CLASSIC which is still used by some old software. I did try nc4tonc3 provided with netCDF4 but that one also wanted the input file in NETCDF4_CLASSIC format... That is why I wrote this little script to actually copy variables and dimensions. There were no attributes in the files, so I did not care to handle attributes.

From a comment by Nikolay Koldunov, there is a utility for this tasks nccopy ...

3 comments:

Unknown said...

Thanks a lot, I needed this for my script :-)

Unknown said...

I get syntax error at line

outVar.setncatts({k: varin.getncattr(k) for k in varin.ncattrs()})
^

SyntaxError: invalid syntax

I'm using python 2.6, could this be the problem?

Sasha said...

Ivano:

Yes Python 2.6 was your problem, dictionary comprehensions appeared in Python 2.7

Cheers