Skip to content

Instantly share code, notes, and snippets.

@vilterp
Created September 9, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vilterp/2df8c693afabb9974076 to your computer and use it in GitHub Desktop.
Save vilterp/2df8c693afabb9974076 to your computer and use it in GitHub Desktop.
import netCDF4 as nc
def ncdump(path):
ds = nc.Dataset(path)
def dim_repr(dim):
return ['\t{0} = {1} ; {2}'.format(dim._name, 'UNLIMITED' if dim.isunlimited() else len(dim),
'// {0} currently'.format(len(dim)) if dim.isunlimited() else '')]
def var_repr(var):
header = '\t{0} {1}({2}) ;'.format(var.dtype, var._name, ', '.join(var.dimensions))
attrs = ['\t\t{0}:{1} = "{2}" ;'.format(var._name, attr, var.getncattr(attr)) for attr in var.ncattrs()]
return [header] + attrs
return '\n'.join(['dimensions:'] +
flatten([dim_repr(ds.dimensions[dim]) for dim in ds.dimensions]) +
['variables:'] +
flatten([var_repr(ds.variables[var]) for var in ds.variables]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment