Created
October 7, 2014 10:55
-
-
Save j08lue/0da1902911a84c622549 to your computer and use it in GitHub Desktop.
Quickly open a (multi-file) netCDF dataset into ipython from command line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/env python | |
""" | |
This script can be executed as is or via an alias, e.g. | |
alias nclookipy='ipython -i ~/path/to/nclook.py' | |
to quickly open a NetCDF file in Python. | |
""" | |
import netCDF4 | |
import argparse | |
import traceback | |
parser = argparse.ArgumentParser(description="Open one or several netCDF files from command line") | |
parser.add_argument('ncfiles', nargs='+', help='path to netCDF file') | |
args = parser.parse_args() | |
try: | |
ds = netCDF4.MFDataset(args.ncfiles) | |
dsvar = ds.variables | |
print('opened {} netCDF file into namespace as \'ds\''.format(len(args.ncfiles))) | |
except RuntimeError: | |
traceback.print_exc() | |
print('Warning: File(s) could not be opened: {}'.format(args.ncfiles)) | |
def print_varmeta(varn): | |
"""Print variable meta information""" | |
print(ds.variables[varn]) | |
def list_variables(units=True,ndim=None,shapes=True): | |
"""List variables in netCDF *dataset* including units if *units* for variables in *ndim* dimensions only, if specified""" | |
def _get_variable_list(ds): | |
for varn in ds.variables.keys(): | |
if ndim is not None: | |
if len(ds.variables[varn].shape) != ndim: | |
continue | |
s = '' | |
try: | |
s += '{} :'.format(varn) | |
except: | |
pass | |
try: | |
s += ' {}'.format(ds.variables[varn].long_name) | |
except: | |
pass | |
try: | |
varunits = ds.variables[varn].units | |
if units and varunits: | |
s += ' [{}]'.format(varunits) | |
except: | |
pass | |
if shapes: | |
try: | |
s += ' {}'.format(ds.variables[varn].shape) | |
except: | |
pass | |
# print output | |
if s: | |
print(s) | |
try: | |
with netCDF4.Dataset(ds) as ds_new: | |
_get_variable_list(ds_new) | |
except: | |
_get_variable_list(ds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment