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

Last change on this file since 2168 was 2092, checked in by lfita, 7 years ago

Fixing `datetimeStr_conversion'

File size: 18.3 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.py'
18errormsg = 'ERROR -- error -- ERROR -- error'
19warnmsg = 'WARNING -- warning --WARNING -- warning'
20import os
21import re
22
23# ASCIIfile_stats: Function to provide the statistics of a series of values from an ASCII file
24# coincident_CFtimes: Function to make coincident times for two different sets of CFtimes
25# count_cond: Function to count values of a variable which attain a condition
26# create_LateX_figs: Function to create a LaTeX from a folder with multiple plots from different values
27# datetimeStr_conversion: Function to transform a string date to an another date object
28# grid_combinations: Function to provide all the possible grid points combination for a given pair of values
29#   x,y= pair of grid points
30# inf_operSlist: Function to provide information from a string as a list separated by
31#   a given character followig a given operation and a set of values
32# interpolate_locs: Function to provide interpolate locations on a given axis
33# PolyArea: Function to compute the area of the polygon following 'Shoelace formula'
34# radial_points: Function to provide a number of grid point positions for a given angle
35# radius_dist: Function to generate a matrix with the distance at a given point
36# rmNOnum: Removing from a string all that characters which are not numbers
37# running_mean: Function to compute a running mean of a series of values
38# significant_decomposition: Function to decompose a given number by its signifcant potencies
39# squared_radial: Function to provide the series of radii as composite of pairs (x,y) of gid cells
40#   Npt= largest amount of grid points on x and y directions
41# table_tex_file: Function to write into a file a LaTeX tabular from a table of values
42# unitsDate: Function to know how many units of time are from a given pair of dates
43# variables_values: Function to provide values to plot the different variables values from ASCII file
44# wdismean: Function to compute the mean value weighted to its 4 distances
45# WRFsetup: Function to check the set-up of a series of model namelist
46
47# Character to split passed values
48cS = ','
49# Character to split a serie of values
50cV = '@'
51# Character for spaces
52cE = '!'
53
54# List of available operations
55operations=['ASCIIfile_stats', 'coincident_CFtimes', 'count_cond',                   \
56  'create_LateX_figs', 'datetimeStr_conversion',                                     \
57  'grid_combinations', 'inf_operSlist',                                              \
58  'interpolate_locs', 'latex_fig_array', 'list_operations', 'PolyArea',              \
59  'radial_points', 'radius_dist',                                                    \
60  'rmNOnum', 'running_mean',                                                         \
61  'significant_decomposition', 'squared_radial',                                     \
62  'table_tex_file', 'unitsDate', 'variables_values', 'wdismean', 'WRFsetup']
63
64hundredvals = '0'
65for i in range(1,100): hundredvals = hundredvals + cV + str(i)
66
67vs100 = '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'
68vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49'
69vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71'
70va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93'
71va100 = vs100 + '@94@95@96@97@98@99'
72
73## 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'
74## e.g. # generic.py -o count_cond -S 0@1@2@3@4@5@6@7@8@9,4,le
75## e.g. # generic.py -o datetimeStr_conversion -S '1976-02-17_08:32:05,Y-m-d_H:M:S,matYmdHMS'
76## e.g. # generic.py -o grid_combinations -S 1,2
77## e.g. # generic.py -o interpolate_locs -S -1.2@2.4@5.6@7.8@12.0,0.5@2.5,lin
78## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5
79## e.g. # generic.py -o radial_points -S 0.785398163397,5
80## e.g. # generic.py -o radius_dist -S 3,5,2,2
81## e.g. # generic.py -o rmNOnum -S LMD123IPSL
82## e.g. # generic.py -o significant_decomposition -S 3.576,-2
83## 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'
84## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second'
85## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10
86## e.g. # generic.py -o squared_radial -S 3
87## e.g. # generic.py -o variables_values -S 'hus'
88## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3.
89## e.g. # generic.py -o WRFsetup -S '/home/lluis/estudios/RELAMPAGO/SimCoor/UBA/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/SMN/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/NOA-IERSD/namelist.input@/home/lluis/estudios/RELAMPAGO/SimCoor/UBAmili/namelist.input,basic,textabrow'
90## e.g. # generic.py -o ASCIIfile_stats -S times_CDXWRF1.dat,#,R
91## e.g. # generic.py -o inf_operSlist -S '-98.21:1.2:3.45:100.1:34321.1,threshold,:,3.'
92## e.g. # generic.py -o create_LateX_figs -S '/home/lluis/estudios/FPS_ALPS/additional/IOP/analysis/figs,WindRose_obs_@SkewT-logP_obs_ta-tda@SkewT-logP_obs_evol@WindRose_obs-sim_step@SkewT-logP_obs-sim_step@SkewT-logP_obs-sim_evol,10868@16080@16144@16546,png'
93
94operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
95valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\
96  "' for list of values)"
97
98parser = OptionParser()
99parser.add_option("-o", "--operation", type='choice', dest="operation", 
100  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
101parser.add_option("-S", "--valueS (when applicable)", dest="values", 
102  help=valuesinf, metavar="VALUES")
103(opts, args) = parser.parse_args()
104
105#######    #######
106## MAIN
107    #######
108oper = opts.operation
109
110if oper == 'list_operations':
111# From: http://www.diveintopython.net/power_of_introspection/all_together.html
112    object = gen
113    for opern in operations:
114        if  opern != 'list_operations': 
115            print opern + '_______ ______ _____ ____ ___ __ _'
116            print getattr(object, opern).__doc__
117
118elif oper == 'coincident_CFtimes':
119    Nvals = 3
120    vals = opts.values.split(cS)
121    if vals[0] == 'h':
122        print gen.coincident_CFtimes.__doc__
123        quit(-1)
124    else:
125        if len(vals) != Nvals:
126            print errormsg
127            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
128              len(vals), ' has passed!!'
129            print gen.coincident_CFtimes.__doc__
130            quit(-1)
131        vals0 = np.array(vals[0].split(cV), dtype=np.float)
132        print gen.coincident_CFtimes(vals0, vals[1], vals[2])
133
134elif oper == 'count_cond':
135    Nvals = 3
136    vals = opts.values.split(cS)
137    if vals[0] == 'h':
138        print gen.count_cond.__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.count_cond.__doc__
146            quit(-1)
147        vals0 = np.array(vals[0].split(cV), dtype=np.float)
148        print gen.count_cond(np.array(vals[0].split(cV), dtype=np.float),           \
149          np.float(vals[1]), vals[2])
150
151
152elif oper == 'create_LateX_figs':
153    Nvals = 4
154    vals = opts.values.split(cS)
155    if vals[0] == 'h':
156        print gen.create_LateX_figs.__doc__
157        quit(-1)
158    else:
159        if len(vals) != Nvals:
160            print errormsg
161            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
162              len(vals), ' has passed!!'
163            print gen.create_LateX_figs.__doc__
164            quit(-1)
165        vals0 = vals[0]
166        vals1 = vals[1].split('@')
167        vals2 = vals[2].split('@')
168        vals3 = vals[3]
169       
170        print gen.create_LateX_figs(vals0, vals1, vals2, vals3)
171
172elif oper == 'datetimeStr_conversion':
173    Nvals = 3
174    vals = opts.values.split(cS)
175    if vals[0] == 'h':
176        print gen.datetimeStr_conversion.__doc__
177        quit(-1)
178
179    newvals = []
180    if vals[1] == 'cfTime':
181        newvals.append(vals[0])
182        newvals.append(vals[1]+','+vals[2])
183        if vals[2] == 'cfTime': newvals.append(vals[3]+','+vals[4].replace('!',' '))
184        vals = list(newvals)
185    if vals[2] == 'cfTime':
186        newvals.append(vals[0])
187        newvals.append(vals[1])
188        newvals.append(vals[2]+','+vals[3].replace('!',' '))
189        vals = list(newvals)
190    if len(vals) != Nvals:
191        print errormsg
192        print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
193          len(vals), ' has passed!!'
194        print gen.datetimeStr_conversion.__doc__
195        quit(-1)
196    print gen.datetimeStr_conversion(vals[0], vals[1], vals[2])
197
198#'days_period'
199
200elif oper == 'grid_combinations':
201    Nvals = 2
202    vals = opts.values.split(cS)
203    if vals[0] == 'h':
204        print gen.grid_combinations.__doc__
205        quit(-1)
206    else:
207        if len(vals) != Nvals:
208            print errormsg
209            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
210              len(vals), ' has passed!!'
211            print gen.grid_combinations.__doc__
212            quit(-1)
213
214        print gen.grid_combinations(np.int(vals[0]), np.int(vals[1]))
215
216elif oper == 'latex_fig_array':
217    vals = opts.values.split(cS)
218    if vals[0] == 'h':
219        print gen.latex_fig_array.__doc__
220        print "  NOTE: first argument as existing LaTeX file"
221        print "  figs: passing list of figures as '@' separated list"
222        print "  caption: using '!' for spaces"
223        quit(-1)
224    else:
225        expectargs = '[latexfile],[figs],[figcaption],[figlabel],[dist],[refsize],'+ \
226         '[width],[height],[dorest]'
227        gen.check_arguments(oper,opts.values,expectargs,cS)
228
229        objf = open(vals[0], 'a')
230
231        figs = vals[1].split('@')
232        caption = vals[2].replace('!',' ')
233        gen.latex_fig_array(figs, objf, caption, vals[3], dist=vals[4],              \
234          refsize=vals[5], width=vals[6], height=vals[7], dorest=vals[8])
235        objf.write('\\end{document}\n')
236        objf.close()
237 
238elif oper == 'inf_operSlist':
239    Nvals = 4
240    vals = opts.values.split(cS)
241    if vals[0] == 'h':
242        print gen.inf_operSlist.__doc__
243        quit(-1)
244    else:
245        if len(vals) != Nvals:
246            print errormsg
247            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
248              len(vals), ' has passed!!'
249            print gen.inf_operSlist.__doc__
250            quit(-1)
251        vals0 = vals[0]
252        vals1 = vals[1]
253        vals2 = vals[2]
254        vals3 = vals[3]
255
256        print gen.inf_operSlist(vals0, vals1, char=vals2, values=vals3)
257 
258elif oper == 'interpolate_locs':
259    Nvals = 3
260    vals = opts.values.split(cS)
261    if vals[0] == 'h':
262        print gen.interpolate_locs.__doc__
263        quit(-1)
264    else:
265        if len(vals) != Nvals:
266            print errormsg
267            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
268              len(vals), ' has passed!!'
269            print gen.interpolate_locs.__doc__
270            quit(-1)
271        vals0 = np.array(vals[0].split(cV), dtype=np.float)
272        vals1 = np.array(vals[1].split(cV), dtype=np.float)
273
274        print gen.interpolate_locs(vals0, vals1, vals[2])
275
276elif oper == 'PolyArea':
277    Nvals = 2
278    vals = opts.values.split(cS)
279    if vals[0] == 'h':
280        print gen.PolyArea.__doc__
281        quit(-1)
282    else:
283        if len(vals) != Nvals:
284            print errormsg
285            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
286              len(vals), ' has passed!!'
287            print gen.PolyArea.__doc__
288            quit(-1)
289        xvals = np.array(vals[0].split(cV), dtype=np.float)
290        yvals = np.array(vals[1].split(cV), dtype=np.float)
291
292        print gen.PolyArea(xvals, yvals)
293
294elif oper == 'radial_points':
295    Nvals = 2
296    vals = opts.values.split(cS)
297    if vals[0] == 'h':
298        print gen.radial_points.__doc__
299        quit(-1)
300    else:
301        if len(vals) != Nvals:
302            print errormsg
303            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
304              len(vals), ' has passed!!'
305            print gen.radial_points.__doc__
306            quit(-1)
307        print gen.radial_points(np.float(vals[0]), int(vals[1]))
308
309elif oper == 'radius_dist':
310    Nvals = 1
311    vals = opts.values.split(cS)
312    if vals[0] == 'h':
313        print gen.radius_dist.__doc__
314        quit(-1)
315    else:
316        if len(vals) != Nvals:
317            print errormsg
318            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
319              len(vals), ' has passed!!'
320            print gen.radius_dist.__doc__
321            quit(-1)
322        print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2]))
323
324elif oper == 'rmNOnum':
325    Nvals = 1
326    vals = opts.values.split(cS)
327    if vals[0] == 'h':
328        print gen.rmNOnum.__doc__
329        quit(-1)
330    else:
331        if len(vals) != Nvals:
332            print errormsg
333            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
334              len(vals), ' has passed!!'
335            print gen.rmNOnum.__doc__
336            quit(-1)
337        print gen.rmNOnum(vals[0])
338
339elif oper == 'running_mean':
340    Nvals = 2
341    vals = opts.values.split(cS)
342    if vals[0] == 'h':
343        print gen.running_mean.__doc__
344        quit(-1)
345    else:
346        if len(vals) != Nvals:
347            print errormsg
348            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
349              len(vals), ' has passed!!'
350            print gen.running_mean.__doc__
351            quit(-1)
352        print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1]))
353
354elif oper == 'significant_decomposition':
355    Nvals = 2
356    vals = opts.values.split(cS)
357    if vals[0] == 'h':
358        print gen.significant_decomposition.__doc__
359        quit(-1)
360    else:
361        if len(vals) != Nvals:
362            print errormsg
363            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
364              len(vals), ' has passed!!'
365            print gen.significant_decomposition.__doc__
366            quit(-1)
367        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
368
369elif oper == 'squared_radial':
370    Nvals = 1
371    vals = opts.values.split(cS)
372    if vals[0] == 'h':
373        print gen.squared_radial.__doc__
374        quit(-1)
375    else:
376        if len(vals) != Nvals:
377            print errormsg
378            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
379              len(vals), ' has passed!!'
380            print gen.squared_radial.__doc__
381            quit(-1)
382        print gen.squared_radial(int(vals[0]))
383
384elif oper == 'table_tex_file':
385    Nvals = 6
386    vals = opts.values.split(cS)
387    if vals[0] == 'h':
388        print gen.table_tex_file.__doc__
389        quit(-1)
390    else:
391        if len(vals) != Nvals:
392            print errormsg
393            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
394              len(vals), ' has passed!!'
395            print gen.table_tex_file.__doc__
396            quit(-1)
397        vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]),     \
398          int(vals[1]))
399        vals3 = vals[3].replace(cE,' ').split(cV)
400        vals4 = vals[4].replace(cE,' ').split(cV)
401
402        print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4,    \
403          vals[5])
404
405elif oper == 'unitsDate':
406    Nvals = 3
407    vals = opts.values.split(cS)
408    if vals[0] == 'h':
409        print gen.unitsDate.__doc__
410        quit(-1)
411    else:
412        if len(vals) != Nvals:
413            print errormsg
414            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
415              len(vals), ' has passed!!'
416            print gen.unitsDate.__doc__
417            quit(-1)
418        print gen.unitsDate(vals[0], vals[1], vals[2])
419
420elif oper == 'variables_values':
421    Nvals = 1
422    vals = opts.values.split(cS)
423    if vals[0] == 'h':
424        print gen.variables_values.__doc__
425        quit(-1)
426    else:
427        if len(vals) != Nvals:
428            print errormsg
429            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
430              len(vals), ' has passed!!'
431            print gen.variables_values.__doc__
432            quit(-1)
433        result = gen.variables_values(vals[0])
434        print gen.numVector_String(result,':')
435
436elif oper == 'wdismean':
437    Nvals = 2
438    vals = opts.values.split(cS)
439    if vals[0] == 'h':
440        print gen.wdismean.__doc__
441        quit(-1)
442    else:
443        if len(vals) != Nvals:
444            print errormsg
445            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
446              len(vals), ' has passed!!'
447            print gen.wdismean.__doc__
448            quit(-1)
449        vals0 = np.array(vals[0].split(cV), dtype=np.float)
450        vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2)
451
452        print gen.wdismean(vals0, vals1)
453
454elif oper == 'WRFsetup':
455    Nvals = 3
456    vals = opts.values.split(cS)
457    if vals[0] == 'h':
458        print gen.WRFsetup.__doc__
459        quit(-1)
460    else:
461        if len(vals) != Nvals:
462            print errormsg
463            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
464              len(vals), ' has passed!!'
465            print gen.WRFsetup.__doc__
466            quit(-1)
467        vals0 = vals[0].replace('@', ',')
468        vals1 = vals[1]
469        vals2 = vals[2]
470       
471        print gen.WRFsetup(vals0, vals1, vals2)
472
473elif oper == 'ASCIIfile_stats':
474    Nvals = 3
475    vals = opts.values.split(cS)
476    if vals[0] == 'h':
477        print gen.ASCIIfile_stats.__doc__
478        quit(-1)
479    else:
480        if len(vals) != Nvals:
481            print errormsg
482            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
483              len(vals), ' has passed!!'
484            print gen.ASCIIfile_stats.__doc__
485            quit(-1)
486        vals0 = vals[0]
487        vals1 = vals[1]
488        vals2 = vals[2]
489       
490        print gen.ASCIIfile_stats(vals0, vals1, vals2)
491
Note: See TracBrowser for help on using the repository browser.