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

Last change on this file since 2074 was 2035, checked in by lfita, 6 years ago

Adding:

  • `create_LateX_figs': Function to create a LaTeX from a folder with multiple plots from different values
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    newvals = []
176    if vals[1] == 'cfTime':
177        newvals.append(vals[0])
178        newvals.append(vals[1]+','+vals[2])
179        if vals[2] == 'cfTime': newvals.append(vals[3]+','+vals[4])
180        vals = list(newvals)
181    if vals[2] == 'cfTime':
182        newvals.append(vals[0])
183        newvals.append(vals[1])
184        newvals.append(vals[2]+','+vals[3])
185        vals = list(newvals)
186
187    if vals[0] == 'h':
188        print gen.datetimeStr_conversion.__doc__
189        quit(-1)
190    else:
191        if len(vals) != Nvals:
192            print errormsg
193            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
194              len(vals), ' has passed!!'
195            print gen.datetimeStr_conversion.__doc__
196            quit(-1)
197        print gen.datetimeStr_conversion(vals[0], vals[1], vals[2])
198
199#'days_period'
200
201elif oper == 'grid_combinations':
202    Nvals = 2
203    vals = opts.values.split(cS)
204    if vals[0] == 'h':
205        print gen.grid_combinations.__doc__
206        quit(-1)
207    else:
208        if len(vals) != Nvals:
209            print errormsg
210            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
211              len(vals), ' has passed!!'
212            print gen.grid_combinations.__doc__
213            quit(-1)
214
215        print gen.grid_combinations(np.int(vals[0]), np.int(vals[1]))
216
217elif oper == 'latex_fig_array':
218    vals = opts.values.split(cS)
219    if vals[0] == 'h':
220        print gen.latex_fig_array.__doc__
221        print "  NOTE: first argument as existing LaTeX file"
222        print "  figs: passing list of figures as '@' separated list"
223        print "  caption: using '!' for spaces"
224        quit(-1)
225    else:
226        expectargs = '[latexfile],[figs],[figcaption],[figlabel],[dist],[refsize],'+ \
227         '[width],[height],[dorest]'
228        gen.check_arguments(oper,opts.values,expectargs,cS)
229
230        objf = open(vals[0], 'a')
231
232        figs = vals[1].split('@')
233        caption = vals[2].replace('!',' ')
234        gen.latex_fig_array(figs, objf, caption, vals[3], dist=vals[4],              \
235          refsize=vals[5], width=vals[6], height=vals[7], dorest=vals[8])
236        objf.write('\\end{document}\n')
237        objf.close()
238 
239elif oper == 'inf_operSlist':
240    Nvals = 4
241    vals = opts.values.split(cS)
242    if vals[0] == 'h':
243        print gen.inf_operSlist.__doc__
244        quit(-1)
245    else:
246        if len(vals) != Nvals:
247            print errormsg
248            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
249              len(vals), ' has passed!!'
250            print gen.inf_operSlist.__doc__
251            quit(-1)
252        vals0 = vals[0]
253        vals1 = vals[1]
254        vals2 = vals[2]
255        vals3 = vals[3]
256
257        print gen.inf_operSlist(vals0, vals1, char=vals2, values=vals3)
258 
259elif oper == 'interpolate_locs':
260    Nvals = 3
261    vals = opts.values.split(cS)
262    if vals[0] == 'h':
263        print gen.interpolate_locs.__doc__
264        quit(-1)
265    else:
266        if len(vals) != Nvals:
267            print errormsg
268            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
269              len(vals), ' has passed!!'
270            print gen.interpolate_locs.__doc__
271            quit(-1)
272        vals0 = np.array(vals[0].split(cV), dtype=np.float)
273        vals1 = np.array(vals[1].split(cV), dtype=np.float)
274
275        print gen.interpolate_locs(vals0, vals1, vals[2])
276
277elif oper == 'PolyArea':
278    Nvals = 2
279    vals = opts.values.split(cS)
280    if vals[0] == 'h':
281        print gen.PolyArea.__doc__
282        quit(-1)
283    else:
284        if len(vals) != Nvals:
285            print errormsg
286            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
287              len(vals), ' has passed!!'
288            print gen.PolyArea.__doc__
289            quit(-1)
290        xvals = np.array(vals[0].split(cV), dtype=np.float)
291        yvals = np.array(vals[1].split(cV), dtype=np.float)
292
293        print gen.PolyArea(xvals, yvals)
294
295elif oper == 'radial_points':
296    Nvals = 2
297    vals = opts.values.split(cS)
298    if vals[0] == 'h':
299        print gen.radial_points.__doc__
300        quit(-1)
301    else:
302        if len(vals) != Nvals:
303            print errormsg
304            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
305              len(vals), ' has passed!!'
306            print gen.radial_points.__doc__
307            quit(-1)
308        print gen.radial_points(np.float(vals[0]), int(vals[1]))
309
310elif oper == 'radius_dist':
311    Nvals = 1
312    vals = opts.values.split(cS)
313    if vals[0] == 'h':
314        print gen.radius_dist.__doc__
315        quit(-1)
316    else:
317        if len(vals) != Nvals:
318            print errormsg
319            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
320              len(vals), ' has passed!!'
321            print gen.radius_dist.__doc__
322            quit(-1)
323        print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2]))
324
325elif oper == 'rmNOnum':
326    Nvals = 1
327    vals = opts.values.split(cS)
328    if vals[0] == 'h':
329        print gen.rmNOnum.__doc__
330        quit(-1)
331    else:
332        if len(vals) != Nvals:
333            print errormsg
334            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
335              len(vals), ' has passed!!'
336            print gen.rmNOnum.__doc__
337            quit(-1)
338        print gen.rmNOnum(vals[0])
339
340elif oper == 'running_mean':
341    Nvals = 2
342    vals = opts.values.split(cS)
343    if vals[0] == 'h':
344        print gen.running_mean.__doc__
345        quit(-1)
346    else:
347        if len(vals) != Nvals:
348            print errormsg
349            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
350              len(vals), ' has passed!!'
351            print gen.running_mean.__doc__
352            quit(-1)
353        print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1]))
354
355elif oper == 'significant_decomposition':
356    Nvals = 2
357    vals = opts.values.split(cS)
358    if vals[0] == 'h':
359        print gen.significant_decomposition.__doc__
360        quit(-1)
361    else:
362        if len(vals) != Nvals:
363            print errormsg
364            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
365              len(vals), ' has passed!!'
366            print gen.significant_decomposition.__doc__
367            quit(-1)
368        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
369
370elif oper == 'squared_radial':
371    Nvals = 1
372    vals = opts.values.split(cS)
373    if vals[0] == 'h':
374        print gen.squared_radial.__doc__
375        quit(-1)
376    else:
377        if len(vals) != Nvals:
378            print errormsg
379            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
380              len(vals), ' has passed!!'
381            print gen.squared_radial.__doc__
382            quit(-1)
383        print gen.squared_radial(int(vals[0]))
384
385elif oper == 'table_tex_file':
386    Nvals = 6
387    vals = opts.values.split(cS)
388    if vals[0] == 'h':
389        print gen.table_tex_file.__doc__
390        quit(-1)
391    else:
392        if len(vals) != Nvals:
393            print errormsg
394            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
395              len(vals), ' has passed!!'
396            print gen.table_tex_file.__doc__
397            quit(-1)
398        vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]),     \
399          int(vals[1]))
400        vals3 = vals[3].replace(cE,' ').split(cV)
401        vals4 = vals[4].replace(cE,' ').split(cV)
402
403        print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4,    \
404          vals[5])
405
406elif oper == 'unitsDate':
407    Nvals = 3
408    vals = opts.values.split(cS)
409    if vals[0] == 'h':
410        print gen.unitsDate.__doc__
411        quit(-1)
412    else:
413        if len(vals) != Nvals:
414            print errormsg
415            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
416              len(vals), ' has passed!!'
417            print gen.unitsDate.__doc__
418            quit(-1)
419        print gen.unitsDate(vals[0], vals[1], vals[2])
420
421elif oper == 'variables_values':
422    Nvals = 1
423    vals = opts.values.split(cS)
424    if vals[0] == 'h':
425        print gen.variables_values.__doc__
426        quit(-1)
427    else:
428        if len(vals) != Nvals:
429            print errormsg
430            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
431              len(vals), ' has passed!!'
432            print gen.variables_values.__doc__
433            quit(-1)
434        result = gen.variables_values(vals[0])
435        print gen.numVector_String(result,':')
436
437elif oper == 'wdismean':
438    Nvals = 2
439    vals = opts.values.split(cS)
440    if vals[0] == 'h':
441        print gen.wdismean.__doc__
442        quit(-1)
443    else:
444        if len(vals) != Nvals:
445            print errormsg
446            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
447              len(vals), ' has passed!!'
448            print gen.wdismean.__doc__
449            quit(-1)
450        vals0 = np.array(vals[0].split(cV), dtype=np.float)
451        vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2)
452
453        print gen.wdismean(vals0, vals1)
454
455elif oper == 'WRFsetup':
456    Nvals = 3
457    vals = opts.values.split(cS)
458    if vals[0] == 'h':
459        print gen.WRFsetup.__doc__
460        quit(-1)
461    else:
462        if len(vals) != Nvals:
463            print errormsg
464            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
465              len(vals), ' has passed!!'
466            print gen.WRFsetup.__doc__
467            quit(-1)
468        vals0 = vals[0].replace('@', ',')
469        vals1 = vals[1]
470        vals2 = vals[2]
471       
472        print gen.WRFsetup(vals0, vals1, vals2)
473
474elif oper == 'ASCIIfile_stats':
475    Nvals = 3
476    vals = opts.values.split(cS)
477    if vals[0] == 'h':
478        print gen.ASCIIfile_stats.__doc__
479        quit(-1)
480    else:
481        if len(vals) != Nvals:
482            print errormsg
483            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
484              len(vals), ' has passed!!'
485            print gen.ASCIIfile_stats.__doc__
486            quit(-1)
487        vals0 = vals[0]
488        vals1 = vals[1]
489        vals2 = vals[2]
490       
491        print gen.ASCIIfile_stats(vals0, vals1, vals2)
492
Note: See TracBrowser for help on using the repository browser.