Skip to content

Instantly share code, notes, and snippets.

@raphaeldussin
Created June 29, 2017 22:44
Show Gist options
  • Save raphaeldussin/ddaac2b051f2491a680c06c5c724473d to your computer and use it in GitHub Desktop.
Save raphaeldussin/ddaac2b051f2491a680c06c5c724473d to your computer and use it in GitHub Desktop.
cloning a netcdf file (base for modifying it)
def clone_ncfile(self,filein,fileout):
fidin = nc.Dataset(filein,'r')
fidout = nc.Dataset(fileout,'w',format='NETCDF3_CLASSIC')
fidout.description = ''
#Copy dimensions
for dname, the_dim in fidin.dimensions.iteritems():
fidout.createDimension(dname, len(the_dim) if not the_dim.isunlimited() else None)
# Copy variables
for v_name, varin in fidin.variables.iteritems():
outVar = fidout.createVariable(v_name, varin.datatype, varin.dimensions)
# Copy variable attributes
outVar.setncatts({k: varin.getncattr(k) for k in varin.ncattrs()})
outVar[:] = varin[:]
fidin.close()
fidout.close()
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment