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

Last change on this file since 1292 was 1222, checked in by lfita, 8 years ago

Fixing `significant_decomposition' by introducing an error message when minimal potence is higher than the potence of the value

File size: 13.7 KB
Line 
1# Wrapper for the generic functions written in python from 'generic_tools.py'
2# L. Fita, LMD. June 2016
3# Python to manage netCDF files.
4# From L. Fita work in different places: LMD (France)
5# More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot
6#
7# pyNCplot and its component generic.py comes with ABSOLUTELY NO WARRANTY.
8# This work is licendes under a Creative Commons
9#   Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0)
10#
11
12from optparse import OptionParser
13import numpy as np
14import datetime as dt
15import generic_tools as gen
16
17main = 'generic_tools.py'
18errormsg = 'ERROR -- error -- ERROR -- error'
19warnmsg = 'WARNING -- warning --WARNING -- warning'
20import os
21import re
22
23# coincident_CFtimes: Function to make coincident times for two different sets of CFtimes
24# count_cond: Function to count values of a variable which attain a condition
25# datetimeStr_conversion: Function to transform a string date to an another date object
26# grid_combinations: Function to provide all the possible grid points combination for a given pair of values
27#   x,y= pair of grid points
28# interpolate_locs: Function to provide interpolate locations on a given axis
29# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
30# radial_points: Function to provide a number of grid point positions for a given angle
31# radius_dist: Function to generate a matrix with the distance at a given point
32# rmNOnum: Removing from a string all that characters which are not numbers
33# running_mean: Function to compute a running mean of a series of values
34# significant_decomposition: Function to decompose a given number by its signifcant potencies
35# squared_radial: Function to provide the series of radii as composite of pairs (x,y) of gid cells
36#   Npt= largest amount of grid points on x and y directions
37# table_tex_file: Function to write into a file a LaTeX tabular from a table of values
38# unitsDate: Function to know how many units of time are from a given pair of dates
39# variables_values: Function to provide values to plot the different variables values from ASCII file
40# wdismean: Function to compute the mean value weighted to its 4 distances
41# Character to split passed values
42
43cS = ','
44# Character to split serie of values
45cV = '@'
46# Character for spaces
47cE = '!'
48
49# List of available operations
50operations=['coincident_CFtimes', 'count_cond', 'datetimeStr_conversion',            \
51  'grid_combinations',                                                               \
52  'interpolate_locs', 'list_operations', 'PolyArea',                                 \
53  'radial_points', 'radius_dist',                                                    \
54  'rmNOnum', 'running_mean',                                                         \
55  'significant_decomposition', 'squared_radial',                                     \
56  'table_tex_file', 'unitsDate', 'variables_values', 'wdismean']
57
58hundredvals = '0'
59for i in range(1,100): hundredvals = hundredvals + cV + str(i)
60
61vs100 = '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'
62vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49'
63vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71'
64va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93'
65va100 = vs100 + '@94@95@96@97@98@99'
66
67## 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'
68## e.g. # generic.py -o count_cond -S 0@1@2@3@4@5@6@7@8@9,4,le
69## e.g. # generic.py -o datetimeStr_conversion -S '1976-02-17_08:32:05,Y-m-d_H:M:S,matYmdHMS'
70## e.g. # generic.py -o grid_combinations -S 1,2
71## e.g. # generic.py -o interpolate_locs -S -1.2@2.4@5.6@7.8@12.0,0.5@2.5,lin
72## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5
73## e.g. # generic.py -o radial_points -S 0.785398163397,5
74## e.g. # generic.py -o radius_dist -S 3,5,2,2
75## e.g. # generic.py -o rmNOnum -S LMD123IPSL
76## e.g. # generic.py -o significant_decomposition -S 3.576,-2
77## e.g. # generic.py -o table_tex_file -S '5,3,0@5@10@1@6@11@2@7@12@3@8@13@4@9@14,!@a@b@c@d@e,i@ii@iii,table.tex'
78## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second'
79## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10
80## e.g. # generic.py -o squared_radial -S 3
81## e.g. # generic.py -o variables_values -S 'hus'
82## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3.
83
84operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
85valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\
86  "' for list of values)"
87
88parser = OptionParser()
89parser.add_option("-o", "--operation", type='choice', dest="operation", 
90  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
91parser.add_option("-S", "--valueS (when applicable)", dest="values", 
92  help=valuesinf, metavar="VALUES")
93(opts, args) = parser.parse_args()
94
95#######    #######
96## MAIN
97    #######
98oper = opts.operation
99
100if oper == 'list_operations':
101# From: http://www.diveintopython.net/power_of_introspection/all_together.html
102    object = gen
103    for opern in operations:
104        if  opern != 'list_operations': 
105            print opern + '_______ ______ _____ ____ ___ __ _'
106            print getattr(object, opern).__doc__
107
108elif oper == 'coincident_CFtimes':
109    Nvals = 3
110    vals = opts.values.split(cS)
111    if vals[0] == 'h':
112        print gen.coincident_CFtimes.__doc__
113        quit(-1)
114    else:
115        if len(vals) != Nvals:
116            print errormsg
117            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
118              len(vals), ' has passed!!'
119            print gen.coincident_CFtimes.__doc__
120            quit(-1)
121        vals0 = np.array(vals[0].split(cV), dtype=np.float)
122        print gen.coincident_CFtimes(vals0, vals[1], vals[2])
123
124elif oper == 'count_cond':
125    Nvals = 3
126    vals = opts.values.split(cS)
127    if vals[0] == 'h':
128        print gen.count_cond.__doc__
129        quit(-1)
130    else:
131        if len(vals) != Nvals:
132            print errormsg
133            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
134              len(vals), ' has passed!!'
135            print gen.count_cond.__doc__
136            quit(-1)
137        vals0 = np.array(vals[0].split(cV), dtype=np.float)
138        print gen.count_cond(np.array(vals[0].split(cV), dtype=np.float),           \
139          np.float(vals[1]), vals[2])
140
141elif oper == 'datetimeStr_conversion':
142    Nvals = 3
143    vals = opts.values.split(cS)
144    if vals[0] == 'h':
145        print gen.datetimeStr_conversion.__doc__
146        quit(-1)
147    else:
148        if len(vals) != Nvals:
149            print errormsg
150            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
151              len(vals), ' has passed!!'
152            print gen.datetimeStr_conversion.__doc__
153            quit(-1)
154        print gen.datetimeStr_conversion(vals[0], vals[1], vals[2])
155
156#'days_period'
157
158elif oper == 'grid_combinations':
159    Nvals = 2
160    vals = opts.values.split(cS)
161    if vals[0] == 'h':
162        print gen.grid_combinations.__doc__
163        quit(-1)
164    else:
165        if len(vals) != Nvals:
166            print errormsg
167            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
168              len(vals), ' has passed!!'
169            print gen.grid_combinations.__doc__
170            quit(-1)
171
172        print gen.grid_combinations(np.int(vals[0]), np.int(vals[1]))
173
174elif oper == 'interpolate_locs':
175    Nvals = 3
176    vals = opts.values.split(cS)
177    if vals[0] == 'h':
178        print gen.interpolate_locs.__doc__
179        quit(-1)
180    else:
181        if len(vals) != Nvals:
182            print errormsg
183            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
184              len(vals), ' has passed!!'
185            print gen.interpolate_locs.__doc__
186            quit(-1)
187        vals0 = np.array(vals[0].split(cV), dtype=np.float)
188        vals1 = np.array(vals[1].split(cV), dtype=np.float)
189
190        print gen.interpolate_locs(vals0, vals1, vals[2])
191
192elif oper == 'PolyArea':
193    Nvals = 2
194    vals = opts.values.split(cS)
195    if vals[0] == 'h':
196        print gen.PolyArea.__doc__
197        quit(-1)
198    else:
199        if len(vals) != Nvals:
200            print errormsg
201            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
202              len(vals), ' has passed!!'
203            print gen.PolyArea.__doc__
204            quit(-1)
205        xvals = np.array(vals[0].split(cV), dtype=np.float)
206        yvals = np.array(vals[1].split(cV), dtype=np.float)
207
208        print gen.PolyArea(xvals, yvals)
209
210elif oper == 'radial_points':
211    Nvals = 2
212    vals = opts.values.split(cS)
213    if vals[0] == 'h':
214        print gen.radial_points.__doc__
215        quit(-1)
216    else:
217        if len(vals) != Nvals:
218            print errormsg
219            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
220              len(vals), ' has passed!!'
221            print gen.radial_points.__doc__
222            quit(-1)
223        print gen.radial_points(np.float(vals[0]), int(vals[1]))
224
225elif oper == 'radius_dist':
226    Nvals = 1
227    vals = opts.values.split(cS)
228    if vals[0] == 'h':
229        print gen.radius_dist.__doc__
230        quit(-1)
231    else:
232        if len(vals) != Nvals:
233            print errormsg
234            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
235              len(vals), ' has passed!!'
236            print gen.radius_dist.__doc__
237            quit(-1)
238        print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2]))
239
240elif oper == 'rmNOnum':
241    Nvals = 1
242    vals = opts.values.split(cS)
243    if vals[0] == 'h':
244        print gen.rmNOnum.__doc__
245        quit(-1)
246    else:
247        if len(vals) != Nvals:
248            print errormsg
249            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
250              len(vals), ' has passed!!'
251            print gen.rmNOnum.__doc__
252            quit(-1)
253        print gen.rmNOnum(vals[0])
254
255elif oper == 'running_mean':
256    Nvals = 2
257    vals = opts.values.split(cS)
258    if vals[0] == 'h':
259        print gen.running_mean.__doc__
260        quit(-1)
261    else:
262        if len(vals) != Nvals:
263            print errormsg
264            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
265              len(vals), ' has passed!!'
266            print gen.running_mean.__doc__
267            quit(-1)
268        print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1]))
269
270elif oper == 'significant_decomposition':
271    Nvals = 2
272    vals = opts.values.split(cS)
273    if vals[0] == 'h':
274        print gen.significant_decomposition.__doc__
275        quit(-1)
276    else:
277        if len(vals) != Nvals:
278            print errormsg
279            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
280              len(vals), ' has passed!!'
281            print gen.significant_decomposition.__doc__
282            quit(-1)
283        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
284
285elif oper == 'squared_radial':
286    Nvals = 1
287    vals = opts.values.split(cS)
288    if vals[0] == 'h':
289        print gen.squared_radial.__doc__
290        quit(-1)
291    else:
292        if len(vals) != Nvals:
293            print errormsg
294            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
295              len(vals), ' has passed!!'
296            print gen.squared_radial.__doc__
297            quit(-1)
298        print gen.squared_radial(int(vals[0]))
299
300elif oper == 'table_tex_file':
301    Nvals = 6
302    vals = opts.values.split(cS)
303    if vals[0] == 'h':
304        print gen.table_tex_file.__doc__
305        quit(-1)
306    else:
307        if len(vals) != Nvals:
308            print errormsg
309            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
310              len(vals), ' has passed!!'
311            print gen.table_tex_file.__doc__
312            quit(-1)
313        vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]),     \
314          int(vals[1]))
315        vals3 = vals[3].replace(cE,' ').split(cV)
316        vals4 = vals[4].replace(cE,' ').split(cV)
317
318        print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4,    \
319          vals[5])
320
321elif oper == 'unitsDate':
322    Nvals = 3
323    vals = opts.values.split(cS)
324    if vals[0] == 'h':
325        print gen.unitsDate.__doc__
326        quit(-1)
327    else:
328        if len(vals) != Nvals:
329            print errormsg
330            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
331              len(vals), ' has passed!!'
332            print gen.unitsDate.__doc__
333            quit(-1)
334        print gen.unitsDate(vals[0], vals[1], vals[2])
335
336elif oper == 'variables_values':
337    Nvals = 1
338    vals = opts.values.split(cS)
339    if vals[0] == 'h':
340        print gen.variables_values.__doc__
341        quit(-1)
342    else:
343        if len(vals) != Nvals:
344            print errormsg
345            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
346              len(vals), ' has passed!!'
347            print gen.variables_values.__doc__
348            quit(-1)
349        result = gen.variables_values(vals[0])
350        print gen.numVector_String(result,':')
351
352elif oper == 'wdismean':
353    Nvals = 2
354    vals = opts.values.split(cS)
355    if vals[0] == 'h':
356        print gen.wdismean.__doc__
357        quit(-1)
358    else:
359        if len(vals) != Nvals:
360            print errormsg
361            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
362              len(vals), ' has passed!!'
363            print gen.wdismean.__doc__
364            quit(-1)
365        vals0 = np.array(vals[0].split(cV), dtype=np.float)
366        vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2)
367
368        print gen.wdismean(vals0, vals1)
369
Note: See TracBrowser for help on using the repository browser.