1 | # Wrapper for the generic functions written in python from 'generic_tools.py' |
---|
2 | # L. Fita, LMD. June 2016 |
---|
3 | |
---|
4 | from optparse import OptionParser |
---|
5 | import numpy as np |
---|
6 | import datetime as dt |
---|
7 | import generic_tools as gen |
---|
8 | |
---|
9 | main = 'generic_tools.py' |
---|
10 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
11 | warnmsg = 'WARNING -- warning --WARNING -- warning' |
---|
12 | import os |
---|
13 | import re |
---|
14 | |
---|
15 | # Character to split passed values |
---|
16 | cS = ',' |
---|
17 | # Character to split serie of values |
---|
18 | cV = '@' |
---|
19 | # List of available operations |
---|
20 | operations=['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 | |
---|
31 | operationnames = "'" + gen.numVector_String(operations, "', '") + "'" |
---|
32 | valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\ |
---|
33 | "' for list of values)" |
---|
34 | |
---|
35 | parser = OptionParser() |
---|
36 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
37 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
---|
38 | parser.add_option("-S", "--valueS (when applicable)", dest="values", |
---|
39 | help=valuesinf, metavar="VALUES") |
---|
40 | (opts, args) = parser.parse_args() |
---|
41 | |
---|
42 | ####### ####### |
---|
43 | ## MAIN |
---|
44 | ####### |
---|
45 | oper = opts.operation |
---|
46 | |
---|
47 | if 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 | |
---|
55 | elif 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 | |
---|
71 | elif 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 | |
---|
89 | elif 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 | |
---|
104 | elif 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 | |
---|
119 | elif 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 | |
---|
134 | elif 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 | |
---|