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=['coincident_CFtimes', 'count_cond', 'grid_combinations', 'PolyArea', \ |
---|
21 | 'radial_points', \ |
---|
22 | 'rmNOnum', 'running_mean', \ |
---|
23 | 'significant_decomposition', 'squared_radial', \ |
---|
24 | 'unitsDate', 'wdismean'] |
---|
25 | |
---|
26 | hundredvals = '0' |
---|
27 | for i in range(1,100): hundredvals = hundredvals + '@' + str(i) |
---|
28 | |
---|
29 | vs100 = '0@1@2@3@4@5@6@7@8@9@10@11@12@13@14@15@16@17@18@19@20@21@22@23@24@25@26@27' |
---|
30 | vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49' |
---|
31 | vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71' |
---|
32 | va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93' |
---|
33 | va100 = vs100 + '@94@95@96@97@98@99' |
---|
34 | |
---|
35 | ## e.g. # generic.py -o 'coincident_CFtimes' -S '0@1@2@3@4@5@6@7@8@9,seconds since 1949-12-01 00:00:00,hours since 1949-12-01 00:00:00' |
---|
36 | ## e.g. # generic.py -o count_cond -S 0@1@2@3@4@5@6@7@8@9,4,le |
---|
37 | ## e.g. # generic.py -o grid_combinations -S 1,2 |
---|
38 | ## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5 |
---|
39 | ## e.g. # generic.py -o radial_points -S 0.785398163397,5 |
---|
40 | ## e.g. # generic.py -o rmNOnum -S LMD123IPSL |
---|
41 | ## e.g. # generic.py -o significant_decomposition -S 3.576,-2 |
---|
42 | ## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second' |
---|
43 | ## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10 |
---|
44 | ## e.g. # generic.py -o squared_radial -S 3 |
---|
45 | ## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3. |
---|
46 | |
---|
47 | operationnames = "'" + gen.numVector_String(operations, "', '") + "'" |
---|
48 | valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\ |
---|
49 | "' for list of values)" |
---|
50 | |
---|
51 | parser = OptionParser() |
---|
52 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
53 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
---|
54 | parser.add_option("-S", "--valueS (when applicable)", dest="values", |
---|
55 | help=valuesinf, metavar="VALUES") |
---|
56 | (opts, args) = parser.parse_args() |
---|
57 | |
---|
58 | ####### ####### |
---|
59 | ## MAIN |
---|
60 | ####### |
---|
61 | oper = opts.operation |
---|
62 | |
---|
63 | if oper == 'list_operations': |
---|
64 | # From: http://www.diveintopython.net/power_of_introspection/all_together.html |
---|
65 | object = gen |
---|
66 | for opern in operations: |
---|
67 | if opern != 'list_operations': |
---|
68 | print opern + '_______ ______ _____ ____ ___ __ _' |
---|
69 | print getattr(object, opern).__doc__ |
---|
70 | |
---|
71 | elif oper == 'coincident_CFtimes': |
---|
72 | Nvals = 3 |
---|
73 | vals = opts.values.split(cS) |
---|
74 | if vals[0] == 'h': |
---|
75 | print gen.coincident_CFtimes.__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.coincident_CFtimes.__doc__ |
---|
83 | quit(-1) |
---|
84 | vals0 = np.array(vals[0].split('@'), dtype=np.float) |
---|
85 | print gen.coincident_CFtimes(vals0, vals[1], vals[2]) |
---|
86 | |
---|
87 | elif oper == 'count_cond': |
---|
88 | Nvals = 3 |
---|
89 | vals = opts.values.split(cS) |
---|
90 | if vals[0] == 'h': |
---|
91 | print gen.count_cond.__doc__ |
---|
92 | quit(-1) |
---|
93 | else: |
---|
94 | if len(vals) != Nvals: |
---|
95 | print errormsg |
---|
96 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
97 | len(vals), ' has passed!!' |
---|
98 | print gen.count_cond.__doc__ |
---|
99 | quit(-1) |
---|
100 | vals0 = np.array(vals[0].split('@'), dtype=np.float) |
---|
101 | print gen.count_cond(np.array(vals[0].split('@'), dtype=np.float), \ |
---|
102 | np.float(vals[1]), vals[2]) |
---|
103 | |
---|
104 | elif oper == 'grid_combinations': |
---|
105 | Nvals = 2 |
---|
106 | vals = opts.values.split(cS) |
---|
107 | if vals[0] == 'h': |
---|
108 | print gen.grid_combinations.__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.grid_combinations.__doc__ |
---|
116 | quit(-1) |
---|
117 | |
---|
118 | print gen.grid_combinations(np.int(vals[0]), np.int(vals[1])) |
---|
119 | |
---|
120 | elif oper == 'PolyArea': |
---|
121 | Nvals = 2 |
---|
122 | vals = opts.values.split(cS) |
---|
123 | if vals[0] == 'h': |
---|
124 | print gen.PolyArea.__doc__ |
---|
125 | quit(-1) |
---|
126 | else: |
---|
127 | if len(vals) != Nvals: |
---|
128 | print errormsg |
---|
129 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
130 | len(vals), ' has passed!!' |
---|
131 | print gen.PolyArea.__doc__ |
---|
132 | quit(-1) |
---|
133 | xvals = np.array(vals[0].split(cV), dtype=np.float) |
---|
134 | yvals = np.array(vals[1].split(cV), dtype=np.float) |
---|
135 | |
---|
136 | print gen.PolyArea(xvals, yvals) |
---|
137 | |
---|
138 | elif oper == 'radial_points': |
---|
139 | Nvals = 2 |
---|
140 | vals = opts.values.split(cS) |
---|
141 | if vals[0] == 'h': |
---|
142 | print gen.radial_points.__doc__ |
---|
143 | quit(-1) |
---|
144 | else: |
---|
145 | if len(vals) != Nvals: |
---|
146 | print errormsg |
---|
147 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
148 | len(vals), ' has passed!!' |
---|
149 | print gen.radial_points.__doc__ |
---|
150 | quit(-1) |
---|
151 | print gen.radial_points(np.float(vals[0]), int(vals[1])) |
---|
152 | |
---|
153 | elif oper == 'rmNOnum': |
---|
154 | Nvals = 1 |
---|
155 | vals = opts.values.split(cS) |
---|
156 | if vals[0] == 'h': |
---|
157 | print gen.rmNOnum.__doc__ |
---|
158 | quit(-1) |
---|
159 | else: |
---|
160 | if len(vals) != Nvals: |
---|
161 | print errormsg |
---|
162 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
163 | len(vals), ' has passed!!' |
---|
164 | print gen.rmNOnum.__doc__ |
---|
165 | quit(-1) |
---|
166 | print gen.rmNOnum(vals[0]) |
---|
167 | |
---|
168 | elif oper == 'running_mean': |
---|
169 | Nvals = 2 |
---|
170 | vals = opts.values.split(cS) |
---|
171 | if vals[0] == 'h': |
---|
172 | print gen.running_mean.__doc__ |
---|
173 | quit(-1) |
---|
174 | else: |
---|
175 | if len(vals) != Nvals: |
---|
176 | print errormsg |
---|
177 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
178 | len(vals), ' has passed!!' |
---|
179 | print gen.running_mean.__doc__ |
---|
180 | quit(-1) |
---|
181 | print gen.running_mean(np.array(vals[0].split('@'), dtype=np.float), int(vals[1])) |
---|
182 | |
---|
183 | elif oper == 'significant_decomposition': |
---|
184 | Nvals = 2 |
---|
185 | vals = opts.values.split(cS) |
---|
186 | if vals[0] == 'h': |
---|
187 | print gen.significant_decomposition.__doc__ |
---|
188 | quit(-1) |
---|
189 | else: |
---|
190 | if len(vals) != Nvals: |
---|
191 | print errormsg |
---|
192 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
193 | len(vals), ' has passed!!' |
---|
194 | print gen.significant_decomposition.__doc__ |
---|
195 | quit(-1) |
---|
196 | print gen.significant_decomposition(np.float(vals[0]), int(vals[1])) |
---|
197 | |
---|
198 | elif oper == 'squared_radial': |
---|
199 | Nvals = 1 |
---|
200 | vals = opts.values.split(cS) |
---|
201 | if vals[0] == 'h': |
---|
202 | print gen.squared_radial.__doc__ |
---|
203 | quit(-1) |
---|
204 | else: |
---|
205 | if len(vals) != Nvals: |
---|
206 | print errormsg |
---|
207 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
208 | len(vals), ' has passed!!' |
---|
209 | print gen.squared_radial.__doc__ |
---|
210 | quit(-1) |
---|
211 | print gen.squared_radial(int(vals[0])) |
---|
212 | |
---|
213 | elif oper == 'unitsDate': |
---|
214 | Nvals = 3 |
---|
215 | vals = opts.values.split(cS) |
---|
216 | if vals[0] == 'h': |
---|
217 | print gen.unitsDate.__doc__ |
---|
218 | quit(-1) |
---|
219 | else: |
---|
220 | if len(vals) != Nvals: |
---|
221 | print errormsg |
---|
222 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
223 | len(vals), ' has passed!!' |
---|
224 | print gen.unitsDate.__doc__ |
---|
225 | quit(-1) |
---|
226 | print gen.unitsDate(vals[0], vals[1], vals[2]) |
---|
227 | |
---|
228 | elif oper == 'wdismean': |
---|
229 | Nvals = 2 |
---|
230 | vals = opts.values.split(cS) |
---|
231 | if vals[0] == 'h': |
---|
232 | print gen.wdismean.__doc__ |
---|
233 | quit(-1) |
---|
234 | else: |
---|
235 | if len(vals) != Nvals: |
---|
236 | print errormsg |
---|
237 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
---|
238 | len(vals), ' has passed!!' |
---|
239 | print gen.wdismean.__doc__ |
---|
240 | quit(-1) |
---|
241 | vals0 = np.array(vals[0].split('@'), dtype=np.float) |
---|
242 | vals1 = np.array(vals[1].split('@'), dtype=np.float).reshape(2,2) |
---|
243 | |
---|
244 | print gen.wdismean(vals0, vals1) |
---|
245 | |
---|