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

Last change on this file since 1906 was 1897, checked in by lfita, 7 years ago

Converting 'WRFsetup' to a multiple namelist table output function

File size: 15.8 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# 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# WRFsetup: Function to check the set-up of a series of model namelist
42
43# Character to split passed values
44cS = ','
45# Character to split a serie of values
46cV = '@'
47# Character for spaces
48cE = '!'
49
50# List of available operations
51operations=['coincident_CFtimes', 'count_cond', 'datetimeStr_conversion',            \
52  'grid_combinations',                                                               \
53  'interpolate_locs', 'latex_fig_array', 'list_operations', 'PolyArea',              \
54  'radial_points', 'radius_dist',                                                    \
55  'rmNOnum', 'running_mean',                                                         \
56  'significant_decomposition', 'squared_radial',                                     \
57  'table_tex_file', 'unitsDate', 'variables_values', 'wdismean', 'WRFsetup']
58
59hundredvals = '0'
60for i in range(1,100): hundredvals = hundredvals + cV + str(i)
61
62vs100 = '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'
63vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49'
64vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71'
65va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93'
66va100 = vs100 + '@94@95@96@97@98@99'
67
68## 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'
69## e.g. # generic.py -o count_cond -S 0@1@2@3@4@5@6@7@8@9,4,le
70## e.g. # generic.py -o datetimeStr_conversion -S '1976-02-17_08:32:05,Y-m-d_H:M:S,matYmdHMS'
71## e.g. # generic.py -o grid_combinations -S 1,2
72## e.g. # generic.py -o interpolate_locs -S -1.2@2.4@5.6@7.8@12.0,0.5@2.5,lin
73## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5
74## e.g. # generic.py -o radial_points -S 0.785398163397,5
75## e.g. # generic.py -o radius_dist -S 3,5,2,2
76## e.g. # generic.py -o rmNOnum -S LMD123IPSL
77## e.g. # generic.py -o significant_decomposition -S 3.576,-2
78## 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'
79## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second'
80## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10
81## e.g. # generic.py -o squared_radial -S 3
82## e.g. # generic.py -o variables_values -S 'hus'
83## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3.
84## 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'
85
86operationnames = "'" + gen.numVector_String(operations, "', '") + "'"
87valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\
88  "' for list of values)"
89
90parser = OptionParser()
91parser.add_option("-o", "--operation", type='choice', dest="operation", 
92  choices=operations, help="operation to make: " + operationnames, metavar="OPER")
93parser.add_option("-S", "--valueS (when applicable)", dest="values", 
94  help=valuesinf, metavar="VALUES")
95(opts, args) = parser.parse_args()
96
97#######    #######
98## MAIN
99    #######
100oper = opts.operation
101
102if oper == 'list_operations':
103# From: http://www.diveintopython.net/power_of_introspection/all_together.html
104    object = gen
105    for opern in operations:
106        if  opern != 'list_operations': 
107            print opern + '_______ ______ _____ ____ ___ __ _'
108            print getattr(object, opern).__doc__
109
110elif oper == 'coincident_CFtimes':
111    Nvals = 3
112    vals = opts.values.split(cS)
113    if vals[0] == 'h':
114        print gen.coincident_CFtimes.__doc__
115        quit(-1)
116    else:
117        if len(vals) != Nvals:
118            print errormsg
119            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
120              len(vals), ' has passed!!'
121            print gen.coincident_CFtimes.__doc__
122            quit(-1)
123        vals0 = np.array(vals[0].split(cV), dtype=np.float)
124        print gen.coincident_CFtimes(vals0, vals[1], vals[2])
125
126elif oper == 'count_cond':
127    Nvals = 3
128    vals = opts.values.split(cS)
129    if vals[0] == 'h':
130        print gen.count_cond.__doc__
131        quit(-1)
132    else:
133        if len(vals) != Nvals:
134            print errormsg
135            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
136              len(vals), ' has passed!!'
137            print gen.count_cond.__doc__
138            quit(-1)
139        vals0 = np.array(vals[0].split(cV), dtype=np.float)
140        print gen.count_cond(np.array(vals[0].split(cV), dtype=np.float),           \
141          np.float(vals[1]), vals[2])
142
143elif oper == 'datetimeStr_conversion':
144    Nvals = 3
145    vals = opts.values.split(cS)
146    newvals = []
147    if vals[1] == 'cfTime':
148        newvals.append(vals[0])
149        newvals.append(vals[1]+','+vals[2])
150        if vals[2] == 'cfTime': newvals.append(vals[3]+','+vals[4])
151        vals = list(newvals)
152    if vals[2] == 'cfTime':
153        newvals.append(vals[0])
154        newvals.append(vals[1])
155        newvals.append(vals[2]+','+vals[3])
156        vals = list(newvals)
157
158    if vals[0] == 'h':
159        print gen.datetimeStr_conversion.__doc__
160        quit(-1)
161    else:
162        if len(vals) != Nvals:
163            print errormsg
164            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
165              len(vals), ' has passed!!'
166            print gen.datetimeStr_conversion.__doc__
167            quit(-1)
168        print gen.datetimeStr_conversion(vals[0], vals[1], vals[2])
169
170#'days_period'
171
172elif oper == 'grid_combinations':
173    Nvals = 2
174    vals = opts.values.split(cS)
175    if vals[0] == 'h':
176        print gen.grid_combinations.__doc__
177        quit(-1)
178    else:
179        if len(vals) != Nvals:
180            print errormsg
181            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
182              len(vals), ' has passed!!'
183            print gen.grid_combinations.__doc__
184            quit(-1)
185
186        print gen.grid_combinations(np.int(vals[0]), np.int(vals[1]))
187
188elif oper == 'latex_fig_array':
189    vals = opts.values.split(cS)
190    if vals[0] == 'h':
191        print gen.latex_fig_array.__doc__
192        print "  NOTE: first argument as existing LaTeX file"
193        print "  figs: passing list of figures as '@' separated list"
194        print "  caption: using '!' for spaces"
195        quit(-1)
196    else:
197        expectargs = '[latexfile],[figs],[figcaption],[figlabel],[dist],[refsize],'+ \
198         '[width],[height],[dorest]'
199        gen.check_arguments(oper,opts.values,expectargs,cS)
200
201        objf = open(vals[0], 'a')
202
203        figs = vals[1].split('@')
204        caption = vals[2].replace('!',' ')
205        gen.latex_fig_array(figs, objf, caption, vals[3], dist=vals[4],              \
206          refsize=vals[5], width=vals[6], height=vals[7], dorest=vals[8])
207        objf.write('\\end{document}\n')
208        objf.close()
209 
210elif oper == 'interpolate_locs':
211    Nvals = 3
212    vals = opts.values.split(cS)
213    if vals[0] == 'h':
214        print gen.interpolate_locs.__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.interpolate_locs.__doc__
222            quit(-1)
223        vals0 = np.array(vals[0].split(cV), dtype=np.float)
224        vals1 = np.array(vals[1].split(cV), dtype=np.float)
225
226        print gen.interpolate_locs(vals0, vals1, vals[2])
227
228elif oper == 'PolyArea':
229    Nvals = 2
230    vals = opts.values.split(cS)
231    if vals[0] == 'h':
232        print gen.PolyArea.__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.PolyArea.__doc__
240            quit(-1)
241        xvals = np.array(vals[0].split(cV), dtype=np.float)
242        yvals = np.array(vals[1].split(cV), dtype=np.float)
243
244        print gen.PolyArea(xvals, yvals)
245
246elif oper == 'radial_points':
247    Nvals = 2
248    vals = opts.values.split(cS)
249    if vals[0] == 'h':
250        print gen.radial_points.__doc__
251        quit(-1)
252    else:
253        if len(vals) != Nvals:
254            print errormsg
255            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
256              len(vals), ' has passed!!'
257            print gen.radial_points.__doc__
258            quit(-1)
259        print gen.radial_points(np.float(vals[0]), int(vals[1]))
260
261elif oper == 'radius_dist':
262    Nvals = 1
263    vals = opts.values.split(cS)
264    if vals[0] == 'h':
265        print gen.radius_dist.__doc__
266        quit(-1)
267    else:
268        if len(vals) != Nvals:
269            print errormsg
270            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
271              len(vals), ' has passed!!'
272            print gen.radius_dist.__doc__
273            quit(-1)
274        print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2]))
275
276elif oper == 'rmNOnum':
277    Nvals = 1
278    vals = opts.values.split(cS)
279    if vals[0] == 'h':
280        print gen.rmNOnum.__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.rmNOnum.__doc__
288            quit(-1)
289        print gen.rmNOnum(vals[0])
290
291elif oper == 'running_mean':
292    Nvals = 2
293    vals = opts.values.split(cS)
294    if vals[0] == 'h':
295        print gen.running_mean.__doc__
296        quit(-1)
297    else:
298        if len(vals) != Nvals:
299            print errormsg
300            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
301              len(vals), ' has passed!!'
302            print gen.running_mean.__doc__
303            quit(-1)
304        print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1]))
305
306elif oper == 'significant_decomposition':
307    Nvals = 2
308    vals = opts.values.split(cS)
309    if vals[0] == 'h':
310        print gen.significant_decomposition.__doc__
311        quit(-1)
312    else:
313        if len(vals) != Nvals:
314            print errormsg
315            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
316              len(vals), ' has passed!!'
317            print gen.significant_decomposition.__doc__
318            quit(-1)
319        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
320
321elif oper == 'squared_radial':
322    Nvals = 1
323    vals = opts.values.split(cS)
324    if vals[0] == 'h':
325        print gen.squared_radial.__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.squared_radial.__doc__
333            quit(-1)
334        print gen.squared_radial(int(vals[0]))
335
336elif oper == 'table_tex_file':
337    Nvals = 6
338    vals = opts.values.split(cS)
339    if vals[0] == 'h':
340        print gen.table_tex_file.__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.table_tex_file.__doc__
348            quit(-1)
349        vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]),     \
350          int(vals[1]))
351        vals3 = vals[3].replace(cE,' ').split(cV)
352        vals4 = vals[4].replace(cE,' ').split(cV)
353
354        print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4,    \
355          vals[5])
356
357elif oper == 'unitsDate':
358    Nvals = 3
359    vals = opts.values.split(cS)
360    if vals[0] == 'h':
361        print gen.unitsDate.__doc__
362        quit(-1)
363    else:
364        if len(vals) != Nvals:
365            print errormsg
366            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
367              len(vals), ' has passed!!'
368            print gen.unitsDate.__doc__
369            quit(-1)
370        print gen.unitsDate(vals[0], vals[1], vals[2])
371
372elif oper == 'variables_values':
373    Nvals = 1
374    vals = opts.values.split(cS)
375    if vals[0] == 'h':
376        print gen.variables_values.__doc__
377        quit(-1)
378    else:
379        if len(vals) != Nvals:
380            print errormsg
381            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
382              len(vals), ' has passed!!'
383            print gen.variables_values.__doc__
384            quit(-1)
385        result = gen.variables_values(vals[0])
386        print gen.numVector_String(result,':')
387
388elif oper == 'wdismean':
389    Nvals = 2
390    vals = opts.values.split(cS)
391    if vals[0] == 'h':
392        print gen.wdismean.__doc__
393        quit(-1)
394    else:
395        if len(vals) != Nvals:
396            print errormsg
397            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
398              len(vals), ' has passed!!'
399            print gen.wdismean.__doc__
400            quit(-1)
401        vals0 = np.array(vals[0].split(cV), dtype=np.float)
402        vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2)
403
404        print gen.wdismean(vals0, vals1)
405
406elif oper == 'WRFsetup':
407    Nvals = 3
408    vals = opts.values.split(cS)
409    if vals[0] == 'h':
410        print gen.WRFsetup.__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.WRFsetup.__doc__
418            quit(-1)
419        vals0 = vals[0].replace('@', ',')
420        vals1 = vals[1]
421        vals2 = vals[2]
422       
423        print gen.WRFsetup(vals0, vals1, vals2)
424
Note: See TracBrowser for help on using the repository browser.