Skip to content

Commit

Permalink
Make the to_netcdf be more general. Ref #40.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexamici committed Dec 27, 2018
1 parent 054dc23 commit 80ade61
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cfgrib/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import click

import cf2cdm
from . import eccodes
# NOTE: imports are executed inside functions so missing dependencies don't break all commands


@click.group()
Expand All @@ -32,21 +31,29 @@ def cfgrib_cli():

@cfgrib_cli.command('selfcheck')
def selfcheck():
from . import eccodes

print("Found: ecCodes v%s." % eccodes.codes_get_api_version())
print("Your system is ready.")


@cfgrib_cli.command('to_netcdf')
@click.argument('inpath')
@click.argument('inpaths', nargs=-1)
@click.option('--outpath', '-o', default=None)
@click.option('--cdm', '-c', default=None)
def to_netcdf(inpath, outpath, cdm):
@click.option('--engine', '-e', default='cfgrib')
def to_netcdf(inpaths, outpath, cdm, engine):
import xarray as xr
import cf2cdm

# NOTE: noop if no input argument
if len(inpaths) == 0:
return

if not outpath:
outpath = os.path.splitext(inpath)[0] + '.nc'
outpath = os.path.splitext(inpaths[0])[0] + '.nc'

ds = xr.open_dataset(inpath, engine='cfgrib')
ds = xr.open_mfdataset(inpaths, engine=engine)
if cdm:
coord_model = getattr(cf2cdm, cdm)
ds = cf2cdm.translate_coords(ds, coord_model=coord_model)
Expand Down

0 comments on commit 80ade61

Please sign in to comment.