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

Last change on this file since 1333 was 1326, checked in by lfita, 8 years ago

Adding `latex_fig_array' on 'generic.py'

File size: 14.5 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# 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', 'latex_fig_array', '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 == 'latex_fig_array':
175    vals = opts.values.split(cS)
176    if vals[0] == 'h':
177        print gen.latex_fig_array.__doc__
178        print "  NOTE: first argument as existing LaTeX file"
179        print "  figs: passing list of figures as '@' separated list"
180        print "  caption: using '!' for spaces"
181        quit(-1)
182    else:
183        expectargs = '[latexfile],[figs],[figcaption],[figlabel],[dist],[refsize],'+ \
184         '[width],[height],[dorest]'
185        gen.check_arguments(oper,opts.values,expectargs,cS)
186
187        objf = open(vals[0], 'a')
188
189        figs = vals[1].split('@')
190        caption = vals[2].replace('!',' ')
191        gen.latex_fig_array(figs, objf, caption, vals[3], dist=vals[4],              \
192          refsize=vals[5], width=vals[6], height=vals[7], dorest=vals[8])
193        objf.write('\\end{document}\n')
194        objf.close()
195 
196elif oper == 'interpolate_locs':
197    Nvals = 3
198    vals = opts.values.split(cS)
199    if vals[0] == 'h':
200        print gen.interpolate_locs.__doc__
201        quit(-1)
202    else:
203        if len(vals) != Nvals:
204            print errormsg
205            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
206              len(vals), ' has passed!!'
207            print gen.interpolate_locs.__doc__
208            quit(-1)
209        vals0 = np.array(vals[0].split(cV), dtype=np.float)
210        vals1 = np.array(vals[1].split(cV), dtype=np.float)
211
212        print gen.interpolate_locs(vals0, vals1, vals[2])
213
214elif oper == 'PolyArea':
215    Nvals = 2
216    vals = opts.values.split(cS)
217    if vals[0] == 'h':
218        print gen.PolyArea.__doc__
219        quit(-1)
220    else:
221        if len(vals) != Nvals:
222            print errormsg
223            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
224              len(vals), ' has passed!!'
225            print gen.PolyArea.__doc__
226            quit(-1)
227        xvals = np.array(vals[0].split(cV), dtype=np.float)
228        yvals = np.array(vals[1].split(cV), dtype=np.float)
229
230        print gen.PolyArea(xvals, yvals)
231
232elif oper == 'radial_points':
233    Nvals = 2
234    vals = opts.values.split(cS)
235    if vals[0] == 'h':
236        print gen.radial_points.__doc__
237        quit(-1)
238    else:
239        if len(vals) != Nvals:
240            print errormsg
241            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
242              len(vals), ' has passed!!'
243            print gen.radial_points.__doc__
244            quit(-1)
245        print gen.radial_points(np.float(vals[0]), int(vals[1]))
246
247elif oper == 'radius_dist':
248    Nvals = 1
249    vals = opts.values.split(cS)
250    if vals[0] == 'h':
251        print gen.radius_dist.__doc__
252        quit(-1)
253    else:
254        if len(vals) != Nvals:
255            print errormsg
256            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
257              len(vals), ' has passed!!'
258            print gen.radius_dist.__doc__
259            quit(-1)
260        print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2]))
261
262elif oper == 'rmNOnum':
263    Nvals = 1
264    vals = opts.values.split(cS)
265    if vals[0] == 'h':
266        print gen.rmNOnum.__doc__
267        quit(-1)
268    else:
269        if len(vals) != Nvals:
270            print errormsg
271            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
272              len(vals), ' has passed!!'
273            print gen.rmNOnum.__doc__
274            quit(-1)
275        print gen.rmNOnum(vals[0])
276
277elif oper == 'running_mean':
278    Nvals = 2
279    vals = opts.values.split(cS)
280    if vals[0] == 'h':
281        print gen.running_mean.__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.running_mean.__doc__
289            quit(-1)
290        print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1]))
291
292elif oper == 'significant_decomposition':
293    Nvals = 2
294    vals = opts.values.split(cS)
295    if vals[0] == 'h':
296        print gen.significant_decomposition.__doc__
297        quit(-1)
298    else:
299        if len(vals) != Nvals:
300            print errormsg
301            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
302              len(vals), ' has passed!!'
303            print gen.significant_decomposition.__doc__
304            quit(-1)
305        print gen.significant_decomposition(np.float(vals[0]), int(vals[1]))
306
307elif oper == 'squared_radial':
308    Nvals = 1
309    vals = opts.values.split(cS)
310    if vals[0] == 'h':
311        print gen.squared_radial.__doc__
312        quit(-1)
313    else:
314        if len(vals) != Nvals:
315            print errormsg
316            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
317              len(vals), ' has passed!!'
318            print gen.squared_radial.__doc__
319            quit(-1)
320        print gen.squared_radial(int(vals[0]))
321
322elif oper == 'table_tex_file':
323    Nvals = 6
324    vals = opts.values.split(cS)
325    if vals[0] == 'h':
326        print gen.table_tex_file.__doc__
327        quit(-1)
328    else:
329        if len(vals) != Nvals:
330            print errormsg
331            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
332              len(vals), ' has passed!!'
333            print gen.table_tex_file.__doc__
334            quit(-1)
335        vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]),     \
336          int(vals[1]))
337        vals3 = vals[3].replace(cE,' ').split(cV)
338        vals4 = vals[4].replace(cE,' ').split(cV)
339
340        print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4,    \
341          vals[5])
342
343elif oper == 'unitsDate':
344    Nvals = 3
345    vals = opts.values.split(cS)
346    if vals[0] == 'h':
347        print gen.unitsDate.__doc__
348        quit(-1)
349    else:
350        if len(vals) != Nvals:
351            print errormsg
352            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
353              len(vals), ' has passed!!'
354            print gen.unitsDate.__doc__
355            quit(-1)
356        print gen.unitsDate(vals[0], vals[1], vals[2])
357
358elif oper == 'variables_values':
359    Nvals = 1
360    vals = opts.values.split(cS)
361    if vals[0] == 'h':
362        print gen.variables_values.__doc__
363        quit(-1)
364    else:
365        if len(vals) != Nvals:
366            print errormsg
367            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
368              len(vals), ' has passed!!'
369            print gen.variables_values.__doc__
370            quit(-1)
371        result = gen.variables_values(vals[0])
372        print gen.numVector_String(result,':')
373
374elif oper == 'wdismean':
375    Nvals = 2
376    vals = opts.values.split(cS)
377    if vals[0] == 'h':
378        print gen.wdismean.__doc__
379        quit(-1)
380    else:
381        if len(vals) != Nvals:
382            print errormsg
383            print '  ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \
384              len(vals), ' has passed!!'
385            print gen.wdismean.__doc__
386            quit(-1)
387        vals0 = np.array(vals[0].split(cV), dtype=np.float)
388        vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2)
389
390        print gen.wdismean(vals0, vals1)
391
Note: See TracBrowser for help on using the repository browser.