[803] | 1 | # Wrapper for the generic functions written in python from 'generic_tools.py' |
---|
| 2 | # L. Fita, LMD. June 2016 |
---|
[1217] | 3 | # Python to manage netCDF files. |
---|
| 4 | # From L. Fita work in different places: LMD (France) |
---|
[1219] | 5 | # More information at: http://www.xn--llusfb-5va.cat/python/PyNCplot |
---|
[1217] | 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 | # |
---|
[803] | 11 | |
---|
| 12 | from optparse import OptionParser |
---|
| 13 | import numpy as np |
---|
| 14 | import datetime as dt |
---|
| 15 | import generic_tools as gen |
---|
| 16 | |
---|
[1326] | 17 | main = 'generic.py' |
---|
[803] | 18 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
| 19 | warnmsg = 'WARNING -- warning --WARNING -- warning' |
---|
| 20 | import os |
---|
| 21 | import re |
---|
| 22 | |
---|
[1221] | 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 |
---|
[803] | 41 | # Character to split passed values |
---|
[1221] | 42 | |
---|
[803] | 43 | cS = ',' |
---|
[805] | 44 | # Character to split serie of values |
---|
| 45 | cV = '@' |
---|
[809] | 46 | # Character for spaces |
---|
| 47 | cE = '!' |
---|
| 48 | |
---|
[803] | 49 | # List of available operations |
---|
[809] | 50 | operations=['coincident_CFtimes', 'count_cond', 'datetimeStr_conversion', \ |
---|
| 51 | 'grid_combinations', \ |
---|
[1326] | 52 | 'interpolate_locs', 'latex_fig_array', 'list_operations', 'PolyArea', \ |
---|
[809] | 53 | 'radial_points', 'radius_dist', \ |
---|
[807] | 54 | 'rmNOnum', 'running_mean', \ |
---|
[805] | 55 | 'significant_decomposition', 'squared_radial', \ |
---|
[892] | 56 | 'table_tex_file', 'unitsDate', 'variables_values', 'wdismean'] |
---|
[803] | 57 | |
---|
[807] | 58 | hundredvals = '0' |
---|
[809] | 59 | for i in range(1,100): hundredvals = hundredvals + cV + str(i) |
---|
[807] | 60 | |
---|
| 61 | vs100 = '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' |
---|
| 62 | vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49' |
---|
| 63 | vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71' |
---|
| 64 | va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93' |
---|
| 65 | va100 = 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 |
---|
[809] | 69 | ## e.g. # generic.py -o datetimeStr_conversion -S '1976-02-17_08:32:05,Y-m-d_H:M:S,matYmdHMS' |
---|
[805] | 70 | ## e.g. # generic.py -o grid_combinations -S 1,2 |
---|
[809] | 71 | ## e.g. # generic.py -o interpolate_locs -S -1.2@2.4@5.6@7.8@12.0,0.5@2.5,lin |
---|
[805] | 72 | ## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5 |
---|
[807] | 73 | ## e.g. # generic.py -o radial_points -S 0.785398163397,5 |
---|
[809] | 74 | ## e.g. # generic.py -o radius_dist -S 3,5,2,2 |
---|
[805] | 75 | ## e.g. # generic.py -o rmNOnum -S LMD123IPSL |
---|
[1222] | 76 | ## e.g. # generic.py -o significant_decomposition -S 3.576,-2 |
---|
[809] | 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' |
---|
[806] | 78 | ## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second' |
---|
[807] | 79 | ## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10 |
---|
[805] | 80 | ## e.g. # generic.py -o squared_radial -S 3 |
---|
[892] | 81 | ## e.g. # generic.py -o variables_values -S 'hus' |
---|
[807] | 82 | ## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3. |
---|
[803] | 83 | |
---|
| 84 | operationnames = "'" + gen.numVector_String(operations, "', '") + "'" |
---|
[805] | 85 | valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\ |
---|
| 86 | "' for list of values)" |
---|
[803] | 87 | |
---|
| 88 | parser = OptionParser() |
---|
| 89 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
---|
| 90 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
---|
| 91 | parser.add_option("-S", "--valueS (when applicable)", dest="values", |
---|
[805] | 92 | help=valuesinf, metavar="VALUES") |
---|
[803] | 93 | (opts, args) = parser.parse_args() |
---|
| 94 | |
---|
| 95 | ####### ####### |
---|
| 96 | ## MAIN |
---|
| 97 | ####### |
---|
| 98 | oper = opts.operation |
---|
| 99 | |
---|
| 100 | if 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__ |
---|
[805] | 107 | |
---|
[807] | 108 | elif 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) |
---|
[809] | 121 | vals0 = np.array(vals[0].split(cV), dtype=np.float) |
---|
[807] | 122 | print gen.coincident_CFtimes(vals0, vals[1], vals[2]) |
---|
| 123 | |
---|
| 124 | elif 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) |
---|
[809] | 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), \ |
---|
[807] | 139 | np.float(vals[1]), vals[2]) |
---|
| 140 | |
---|
[809] | 141 | elif 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 | |
---|
[828] | 156 | #'days_period' |
---|
[818] | 157 | |
---|
[805] | 158 | elif 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 | |
---|
[1326] | 174 | elif 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 | |
---|
[809] | 196 | elif 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 | |
---|
[805] | 214 | elif 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 | |
---|
[807] | 232 | elif 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 | |
---|
[809] | 247 | elif 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 | |
---|
[805] | 262 | elif 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 | |
---|
[807] | 277 | elif 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) |
---|
[809] | 290 | print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1])) |
---|
[807] | 291 | |
---|
[805] | 292 | elif 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 | |
---|
| 307 | elif 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 | |
---|
[809] | 322 | elif 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 | |
---|
[803] | 343 | elif 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: |
---|
[805] | 350 | if len(vals) != Nvals: |
---|
[803] | 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 | |
---|
[892] | 358 | elif 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 | |
---|
[807] | 374 | elif 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) |
---|
[809] | 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) |
---|
[807] | 389 | |
---|
| 390 | print gen.wdismean(vals0, vals1) |
---|
| 391 | |
---|