# Wrapper for the generic functions written in python from 'generic_tools.py' # L. Fita, LMD. June 2016 from optparse import OptionParser import numpy as np import datetime as dt import generic_tools as gen main = 'generic_tools.py' errormsg = 'ERROR -- error -- ERROR -- error' warnmsg = 'WARNING -- warning --WARNING -- warning' import os import re # Character to split passed values cS = ',' # Character to split serie of values cV = '@' # List of available operations operations=['grid_combinations', 'PolyArea', 'rmNOnum', \ 'significant_decomposition', 'squared_radial', \ 'unitsDate'] ## e.g. # generic.py -o grid_combinations -S 1,2 ## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5 ## e.g. # generic.py -o rmNOnum -S LMD123IPSL ## e.g. # generic.py -o significant_decomposition -S 3.576,-2 ## e.g. # generic.py -o secondsDate -S '19490101000000,19760217082932,second' ## e.g. # generic.py -o squared_radial -S 3 operationnames = "'" + gen.numVector_String(operations, "', '") + "'" valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\ "' for list of values)" parser = OptionParser() parser.add_option("-o", "--operation", type='choice', dest="operation", choices=operations, help="operation to make: " + operationnames, metavar="OPER") parser.add_option("-S", "--valueS (when applicable)", dest="values", help=valuesinf, metavar="VALUES") (opts, args) = parser.parse_args() ####### ####### ## MAIN ####### oper = opts.operation if oper == 'list_operations': # From: http://www.diveintopython.net/power_of_introspection/all_together.html object = gen for opern in operations: if opern != 'list_operations': print opern + '_______ ______ _____ ____ ___ __ _' print getattr(object, opern).__doc__ elif oper == 'grid_combinations': Nvals = 2 vals = opts.values.split(cS) if vals[0] == 'h': print gen.grid_combinations.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.grid_combinations.__doc__ quit(-1) print gen.grid_combinations(np.int(vals[0]), np.int(vals[1])) elif oper == 'PolyArea': Nvals = 2 vals = opts.values.split(cS) if vals[0] == 'h': print gen.PolyArea.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.PolyArea.__doc__ quit(-1) xvals = np.array(vals[0].split(cV), dtype=np.float) yvals = np.array(vals[1].split(cV), dtype=np.float) print gen.PolyArea(xvals, yvals) elif oper == 'rmNOnum': Nvals = 1 vals = opts.values.split(cS) if vals[0] == 'h': print gen.rmNOnum.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.rmNOnum.__doc__ quit(-1) print gen.rmNOnum(vals[0]) elif oper == 'significant_decomposition': Nvals = 2 vals = opts.values.split(cS) if vals[0] == 'h': print gen.significant_decomposition.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.significant_decomposition.__doc__ quit(-1) print gen.significant_decomposition(np.float(vals[0]), int(vals[1])) elif oper == 'squared_radial': Nvals = 1 vals = opts.values.split(cS) if vals[0] == 'h': print gen.squared_radial.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.squared_radial.__doc__ quit(-1) print gen.squared_radial(int(vals[0])) elif oper == 'unitsDate': Nvals = 3 vals = opts.values.split(cS) if vals[0] == 'h': print gen.unitsDate.__doc__ quit(-1) else: if len(vals) != Nvals: print errormsg print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ len(vals), ' has passed!!' print gen.unitsDate.__doc__ quit(-1) print gen.unitsDate(vals[0], vals[1], vals[2])