source: lmdz_wrf/trunk/tools/generic.py @ 805

Last change on this file since 805 was 805, checked in by lfita, 9 years ago

Adding: grid_combinations, PolyArea?, rmNOnum, significant_decomposition, squared_radial

File size: 4.7 KB
Line 
1# Wrapper for the generic functions written in python from 'generic_tools.py'
2# L. Fita, LMD. June 2016
3
4from optparse import OptionParser
5import numpy as np
6import datetime as dt
7import generic_tools as gen
8
9main = 'generic_tools.py'
10errormsg = 'ERROR -- error -- ERROR -- error'
11warnmsg = 'WARNING -- warning --WARNING -- warning'
12import os
13import re
14
15# Character to split passed values
16cS = ','
17# Character to split serie of values
18cV = '@'
19# List of available operations
20operations=['grid_combinations', 'PolyArea', 'rmNOnum',                              \
21  'significant_decomposition', 'squared_radial',                                     \
22  'unitsDate']
23
24## e.g. # generic.py -o grid_combinations -S 1,2
25## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5
26## e.g. # generic.py -o rmNOnum -S LMD123IPSL
27## e.g. # generic.py -o significant_decomposition -S 3.576,-2
28## e.g. # generic.py -o secondsDate -S '19490101000000,19760217082932,second'
29## e.g. # generic.py -o squared_radial -S 3
30
31operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
32valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\
33  "' for list of values)"
34
35parser = OptionParser()
36parser.add_option("-o", "--operation", type='choice', dest="operation", 
37  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
38parser.add_option("-S", "--valueS (when applicable)", dest="values", 
39  help=valuesinf, metavar="VALUES")
40(opts, args) = parser.parse_args()
41
42#######    #######
43## MAIN
44    #######
45oper = opts.operation
46
47if oper == 'list_operations':
48# From: http://www.diveintopython.net/power_of_introspection/all_together.html
49    object = gen
50    for opern in operations:
51        if  opern != 'list_operations': 
52            print opern + '_______ ______ _____ ____ ___ __ _'
53            print getattr(object, opern).__doc__
54
55elif oper == 'grid_combinations':
56    Nvals = 2
57    vals = opts.values.split(cS)
58    if vals[0] == 'h':
59        print gen.grid_combinations.__doc__
60        quit(-1)
61    else:
62        if len(vals) != Nvals:
63            print errormsg
64            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
65              len(vals), ' has passed!!'
66            print gen.grid_combinations.__doc__
67            quit(-1)
68
69        print gen.grid_combinations(np.int(vals[0]), np.int(vals[1]))
70
71elif oper == 'PolyArea':
72    Nvals = 2
73    vals = opts.values.split(cS)
74    if vals[0] == 'h':
75        print gen.PolyArea.__doc__
76        quit(-1)
77    else:
78        if len(vals) != Nvals:
79            print errormsg
80            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
81              len(vals), ' has passed!!'
82            print gen.PolyArea.__doc__
83            quit(-1)
84        xvals = np.array(vals[0].split(cV), dtype=np.float)
85        yvals = np.array(vals[1].split(cV), dtype=np.float)
86
87        print gen.PolyArea(xvals, yvals)
88
89elif oper == 'rmNOnum':
90    Nvals = 1
91    vals = opts.values.split(cS)
92    if vals[0] == 'h':
93        print gen.rmNOnum.__doc__
94        quit(-1)
95    else:
96        if len(vals) != Nvals:
97            print errormsg
98            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
99              len(vals), ' has passed!!'
100            print gen.rmNOnum.__doc__
101            quit(-1)
102        print gen.rmNOnum(vals[0])
103
104elif oper == 'significant_decomposition':
105    Nvals = 2
106    vals = opts.values.split(cS)
107    if vals[0] == 'h':
108        print gen.significant_decomposition.__doc__
109        quit(-1)
110    else:
111        if len(vals) != Nvals:
112            print errormsg
113            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
114              len(vals), ' has passed!!'
115            print gen.significant_decomposition.__doc__
116            quit(-1)
117        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
118
119elif oper == 'squared_radial':
120    Nvals = 1
121    vals = opts.values.split(cS)
122    if vals[0] == 'h':
123        print gen.squared_radial.__doc__
124        quit(-1)
125    else:
126        if len(vals) != Nvals:
127            print errormsg
128            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
129              len(vals), ' has passed!!'
130            print gen.squared_radial.__doc__
131            quit(-1)
132        print gen.squared_radial(int(vals[0]))
133
134elif oper == 'unitsDate':
135    Nvals = 3
136    vals = opts.values.split(cS)
137    if vals[0] == 'h':
138        print gen.unitsDate.__doc__
139        quit(-1)
140    else:
141        if len(vals) != Nvals:
142            print errormsg
143            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
144              len(vals), ' has passed!!'
145            print gen.unitsDate.__doc__
146            quit(-1)
147        print gen.unitsDate(vals[0], vals[1], vals[2])
148
Note: See TracBrowser for help on using the repository browser.