Skip to content

Commit

Permalink
utility code in its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
serapred committed Feb 21, 2019
1 parent b1261ca commit 9c5e96d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Empty file added utils/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions utils/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import numpy as np


# Convert non-json-encodable types to built-in types


def json_encode(val):

if isinstance(val, np.integer):
return int(val)
elif isinstance(val, np.floating):
return float(val)
elif isinstance(val, np.ndarray):
return val.tolist()
else:
return val
57 changes: 57 additions & 0 deletions utils/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# parser for netcdf_to_zarr

import argparse


def configure(args):

class toList(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(toList, self).__init__(option_strings, dest, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values.split(' '))

desc = ('This script converts netCDF4 into zarr files\n'
'It is possible to specify a list of netCDF4 files\n'
'to merge them into a single zarr file.\n'
'to do so, the variables in the netCD4 file\n'
'must have a common shape and run along the same dimension.\n'
'\nExamples:\n'
' -convert.py src.nc dst.nc [-options]\n'
' -convert.py "src1.nc src2.nc src3.nc" dst.zarr [-options]\n')

parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('src',
help='source netCDF4 file/files',
action=toList,
default=[])

parser.add_argument('dst',
help='target zarr file',
default='')

parser.add_argument('-a',
'--axis',
metavar='axis',
help='axis name to append along',
default=None)

parser.add_argument('-n',
'--nested',
help='chunks are located into a nested path',
action='store_true',
default=False)

parser.add_argument('-m',
'--mode',
metavar='mode',
help='use processes or threads',
choices=('threads', 'processes'),
default='serial')

return parser.parse_args(args)

0 comments on commit 9c5e96d

Please sign in to comment.