| 1 | # Wrapper for the generic functions written in python from 'generic_tools.py' |
|---|
| 2 | # L. Fita, LMD. June 2016 |
|---|
| 3 | |
|---|
| 4 | from optparse import OptionParser |
|---|
| 5 | import numpy as np |
|---|
| 6 | import datetime as dt |
|---|
| 7 | import generic_tools as gen |
|---|
| 8 | |
|---|
| 9 | main = 'generic_tools.py' |
|---|
| 10 | errormsg = 'ERROR -- error -- ERROR -- error' |
|---|
| 11 | warnmsg = 'WARNING -- warning --WARNING -- warning' |
|---|
| 12 | import os |
|---|
| 13 | import re |
|---|
| 14 | |
|---|
| 15 | # Character to split passed values |
|---|
| 16 | cS = ',' |
|---|
| 17 | # Character to split serie of values |
|---|
| 18 | cV = '@' |
|---|
| 19 | # Character for spaces |
|---|
| 20 | cE = '!' |
|---|
| 21 | |
|---|
| 22 | # List of available operations |
|---|
| 23 | operations=['coincident_CFtimes', 'count_cond', 'datetimeStr_conversion', \ |
|---|
| 24 | 'grid_combinations', \ |
|---|
| 25 | 'interpolate_locs', 'PolyArea', \ |
|---|
| 26 | 'radial_points', 'radius_dist', \ |
|---|
| 27 | 'rmNOnum', 'running_mean', \ |
|---|
| 28 | 'significant_decomposition', 'squared_radial', \ |
|---|
| 29 | 'table_tex_file', 'unitsDate', 'variables_values', 'wdismean'] |
|---|
| 30 | |
|---|
| 31 | hundredvals = '0' |
|---|
| 32 | for i in range(1,100): hundredvals = hundredvals + cV + str(i) |
|---|
| 33 | |
|---|
| 34 | 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' |
|---|
| 35 | vs100 = vs100 + '@28@29@30@31@32@33@34@35@36@37@38@39@40@41@42@43@44@45@46@47@48@49' |
|---|
| 36 | vs100 = vs100 + '@50@51@52@53@54@55@56@57@58@59@60@61@62@63@64@65@66@67@68@69@70@71' |
|---|
| 37 | va100 = vs100 + '@72@73@74@75@76@77@78@79@80@81@82@83@84@85@86@87@88@89@90@91@92@93' |
|---|
| 38 | va100 = vs100 + '@94@95@96@97@98@99' |
|---|
| 39 | |
|---|
| 40 | ## 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' |
|---|
| 41 | ## e.g. # generic.py -o count_cond -S 0@1@2@3@4@5@6@7@8@9,4,le |
|---|
| 42 | ## e.g. # generic.py -o datetimeStr_conversion -S '1976-02-17_08:32:05,Y-m-d_H:M:S,matYmdHMS' |
|---|
| 43 | ## e.g. # generic.py -o grid_combinations -S 1,2 |
|---|
| 44 | ## e.g. # generic.py -o interpolate_locs -S -1.2@2.4@5.6@7.8@12.0,0.5@2.5,lin |
|---|
| 45 | ## e.g. # generic.py -o PolyArea -S -0.5@0.5@0.5@-0.5,0.5@0.5@-0.5@-0.5 |
|---|
| 46 | ## e.g. # generic.py -o radial_points -S 0.785398163397,5 |
|---|
| 47 | ## e.g. # generic.py -o radius_dist -S 3,5,2,2 |
|---|
| 48 | ## e.g. # generic.py -o rmNOnum -S LMD123IPSL |
|---|
| 49 | ## e.g. # generic.py -o significant_decomposition -S 3.576,-2,0@1@2@3@4@5@6@7@8@9@10@11@12@13@14,a@b@c@d@e,i@ii@iii,table.tex |
|---|
| 50 | ## 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' |
|---|
| 51 | ## e.g. # generic.py -o unitsDate -S '19490101000000,19760217082932,second' |
|---|
| 52 | ## e.g. # generic.py -o running_mean -S 0@1@2@3@4@5@6@7@8@9,10 |
|---|
| 53 | ## e.g. # generic.py -o squared_radial -S 3 |
|---|
| 54 | ## e.g. # generic.py -o variables_values -S 'hus' |
|---|
| 55 | ## e.g. # generic.py -o wdismean -S 0.005@0.005,0.@1.@2.@3. |
|---|
| 56 | |
|---|
| 57 | operationnames = "'" + gen.numVector_String(operations, "', '") + "'" |
|---|
| 58 | valuesinf = "'" + cS + "' list of values to use according to the operation ('" + cV +\ |
|---|
| 59 | "' for list of values)" |
|---|
| 60 | |
|---|
| 61 | parser = OptionParser() |
|---|
| 62 | parser.add_option("-o", "--operation", type='choice', dest="operation", |
|---|
| 63 | choices=operations, help="operation to make: " + operationnames, metavar="OPER") |
|---|
| 64 | parser.add_option("-S", "--valueS (when applicable)", dest="values", |
|---|
| 65 | help=valuesinf, metavar="VALUES") |
|---|
| 66 | (opts, args) = parser.parse_args() |
|---|
| 67 | |
|---|
| 68 | ####### ####### |
|---|
| 69 | ## MAIN |
|---|
| 70 | ####### |
|---|
| 71 | oper = opts.operation |
|---|
| 72 | |
|---|
| 73 | if oper == 'list_operations': |
|---|
| 74 | # From: http://www.diveintopython.net/power_of_introspection/all_together.html |
|---|
| 75 | object = gen |
|---|
| 76 | for opern in operations: |
|---|
| 77 | if opern != 'list_operations': |
|---|
| 78 | print opern + '_______ ______ _____ ____ ___ __ _' |
|---|
| 79 | print getattr(object, opern).__doc__ |
|---|
| 80 | |
|---|
| 81 | elif oper == 'coincident_CFtimes': |
|---|
| 82 | Nvals = 3 |
|---|
| 83 | vals = opts.values.split(cS) |
|---|
| 84 | if vals[0] == 'h': |
|---|
| 85 | print gen.coincident_CFtimes.__doc__ |
|---|
| 86 | quit(-1) |
|---|
| 87 | else: |
|---|
| 88 | if len(vals) != Nvals: |
|---|
| 89 | print errormsg |
|---|
| 90 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 91 | len(vals), ' has passed!!' |
|---|
| 92 | print gen.coincident_CFtimes.__doc__ |
|---|
| 93 | quit(-1) |
|---|
| 94 | vals0 = np.array(vals[0].split(cV), dtype=np.float) |
|---|
| 95 | print gen.coincident_CFtimes(vals0, vals[1], vals[2]) |
|---|
| 96 | |
|---|
| 97 | elif oper == 'count_cond': |
|---|
| 98 | Nvals = 3 |
|---|
| 99 | vals = opts.values.split(cS) |
|---|
| 100 | if vals[0] == 'h': |
|---|
| 101 | print gen.count_cond.__doc__ |
|---|
| 102 | quit(-1) |
|---|
| 103 | else: |
|---|
| 104 | if len(vals) != Nvals: |
|---|
| 105 | print errormsg |
|---|
| 106 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 107 | len(vals), ' has passed!!' |
|---|
| 108 | print gen.count_cond.__doc__ |
|---|
| 109 | quit(-1) |
|---|
| 110 | vals0 = np.array(vals[0].split(cV), dtype=np.float) |
|---|
| 111 | print gen.count_cond(np.array(vals[0].split(cV), dtype=np.float), \ |
|---|
| 112 | np.float(vals[1]), vals[2]) |
|---|
| 113 | |
|---|
| 114 | elif oper == 'datetimeStr_conversion': |
|---|
| 115 | Nvals = 3 |
|---|
| 116 | vals = opts.values.split(cS) |
|---|
| 117 | if vals[0] == 'h': |
|---|
| 118 | print gen.datetimeStr_conversion.__doc__ |
|---|
| 119 | quit(-1) |
|---|
| 120 | else: |
|---|
| 121 | if len(vals) != Nvals: |
|---|
| 122 | print errormsg |
|---|
| 123 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 124 | len(vals), ' has passed!!' |
|---|
| 125 | print gen.datetimeStr_conversion.__doc__ |
|---|
| 126 | quit(-1) |
|---|
| 127 | print gen.datetimeStr_conversion(vals[0], vals[1], vals[2]) |
|---|
| 128 | |
|---|
| 129 | #'days_period' |
|---|
| 130 | |
|---|
| 131 | elif oper == 'grid_combinations': |
|---|
| 132 | Nvals = 2 |
|---|
| 133 | vals = opts.values.split(cS) |
|---|
| 134 | if vals[0] == 'h': |
|---|
| 135 | print gen.grid_combinations.__doc__ |
|---|
| 136 | quit(-1) |
|---|
| 137 | else: |
|---|
| 138 | if len(vals) != Nvals: |
|---|
| 139 | print errormsg |
|---|
| 140 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 141 | len(vals), ' has passed!!' |
|---|
| 142 | print gen.grid_combinations.__doc__ |
|---|
| 143 | quit(-1) |
|---|
| 144 | |
|---|
| 145 | print gen.grid_combinations(np.int(vals[0]), np.int(vals[1])) |
|---|
| 146 | |
|---|
| 147 | elif oper == 'interpolate_locs': |
|---|
| 148 | Nvals = 3 |
|---|
| 149 | vals = opts.values.split(cS) |
|---|
| 150 | if vals[0] == 'h': |
|---|
| 151 | print gen.interpolate_locs.__doc__ |
|---|
| 152 | quit(-1) |
|---|
| 153 | else: |
|---|
| 154 | if len(vals) != Nvals: |
|---|
| 155 | print errormsg |
|---|
| 156 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 157 | len(vals), ' has passed!!' |
|---|
| 158 | print gen.interpolate_locs.__doc__ |
|---|
| 159 | quit(-1) |
|---|
| 160 | vals0 = np.array(vals[0].split(cV), dtype=np.float) |
|---|
| 161 | vals1 = np.array(vals[1].split(cV), dtype=np.float) |
|---|
| 162 | |
|---|
| 163 | print gen.interpolate_locs(vals0, vals1, vals[2]) |
|---|
| 164 | |
|---|
| 165 | elif oper == 'PolyArea': |
|---|
| 166 | Nvals = 2 |
|---|
| 167 | vals = opts.values.split(cS) |
|---|
| 168 | if vals[0] == 'h': |
|---|
| 169 | print gen.PolyArea.__doc__ |
|---|
| 170 | quit(-1) |
|---|
| 171 | else: |
|---|
| 172 | if len(vals) != Nvals: |
|---|
| 173 | print errormsg |
|---|
| 174 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 175 | len(vals), ' has passed!!' |
|---|
| 176 | print gen.PolyArea.__doc__ |
|---|
| 177 | quit(-1) |
|---|
| 178 | xvals = np.array(vals[0].split(cV), dtype=np.float) |
|---|
| 179 | yvals = np.array(vals[1].split(cV), dtype=np.float) |
|---|
| 180 | |
|---|
| 181 | print gen.PolyArea(xvals, yvals) |
|---|
| 182 | |
|---|
| 183 | elif oper == 'radial_points': |
|---|
| 184 | Nvals = 2 |
|---|
| 185 | vals = opts.values.split(cS) |
|---|
| 186 | if vals[0] == 'h': |
|---|
| 187 | print gen.radial_points.__doc__ |
|---|
| 188 | quit(-1) |
|---|
| 189 | else: |
|---|
| 190 | if len(vals) != Nvals: |
|---|
| 191 | print errormsg |
|---|
| 192 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 193 | len(vals), ' has passed!!' |
|---|
| 194 | print gen.radial_points.__doc__ |
|---|
| 195 | quit(-1) |
|---|
| 196 | print gen.radial_points(np.float(vals[0]), int(vals[1])) |
|---|
| 197 | |
|---|
| 198 | elif oper == 'radius_dist': |
|---|
| 199 | Nvals = 1 |
|---|
| 200 | vals = opts.values.split(cS) |
|---|
| 201 | if vals[0] == 'h': |
|---|
| 202 | print gen.radius_dist.__doc__ |
|---|
| 203 | quit(-1) |
|---|
| 204 | else: |
|---|
| 205 | if len(vals) != Nvals: |
|---|
| 206 | print errormsg |
|---|
| 207 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 208 | len(vals), ' has passed!!' |
|---|
| 209 | print gen.radius_dist.__doc__ |
|---|
| 210 | quit(-1) |
|---|
| 211 | print gen.radius_dist(int(vals[0]), int(vals[1]), int(vals[2]), int(vals[2])) |
|---|
| 212 | |
|---|
| 213 | elif oper == 'rmNOnum': |
|---|
| 214 | Nvals = 1 |
|---|
| 215 | vals = opts.values.split(cS) |
|---|
| 216 | if vals[0] == 'h': |
|---|
| 217 | print gen.rmNOnum.__doc__ |
|---|
| 218 | quit(-1) |
|---|
| 219 | else: |
|---|
| 220 | if len(vals) != Nvals: |
|---|
| 221 | print errormsg |
|---|
| 222 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 223 | len(vals), ' has passed!!' |
|---|
| 224 | print gen.rmNOnum.__doc__ |
|---|
| 225 | quit(-1) |
|---|
| 226 | print gen.rmNOnum(vals[0]) |
|---|
| 227 | |
|---|
| 228 | elif oper == 'running_mean': |
|---|
| 229 | Nvals = 2 |
|---|
| 230 | vals = opts.values.split(cS) |
|---|
| 231 | if vals[0] == 'h': |
|---|
| 232 | print gen.running_mean.__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.running_mean.__doc__ |
|---|
| 240 | quit(-1) |
|---|
| 241 | print gen.running_mean(np.array(vals[0].split(cV), dtype=np.float), int(vals[1])) |
|---|
| 242 | |
|---|
| 243 | elif oper == 'significant_decomposition': |
|---|
| 244 | Nvals = 2 |
|---|
| 245 | vals = opts.values.split(cS) |
|---|
| 246 | if vals[0] == 'h': |
|---|
| 247 | print gen.significant_decomposition.__doc__ |
|---|
| 248 | quit(-1) |
|---|
| 249 | else: |
|---|
| 250 | if len(vals) != Nvals: |
|---|
| 251 | print errormsg |
|---|
| 252 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 253 | len(vals), ' has passed!!' |
|---|
| 254 | print gen.significant_decomposition.__doc__ |
|---|
| 255 | quit(-1) |
|---|
| 256 | print gen.significant_decomposition(np.float(vals[0]), int(vals[1])) |
|---|
| 257 | |
|---|
| 258 | elif oper == 'squared_radial': |
|---|
| 259 | Nvals = 1 |
|---|
| 260 | vals = opts.values.split(cS) |
|---|
| 261 | if vals[0] == 'h': |
|---|
| 262 | print gen.squared_radial.__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.squared_radial.__doc__ |
|---|
| 270 | quit(-1) |
|---|
| 271 | print gen.squared_radial(int(vals[0])) |
|---|
| 272 | |
|---|
| 273 | elif oper == 'table_tex_file': |
|---|
| 274 | Nvals = 6 |
|---|
| 275 | vals = opts.values.split(cS) |
|---|
| 276 | if vals[0] == 'h': |
|---|
| 277 | print gen.table_tex_file.__doc__ |
|---|
| 278 | quit(-1) |
|---|
| 279 | else: |
|---|
| 280 | if len(vals) != Nvals: |
|---|
| 281 | print errormsg |
|---|
| 282 | print ' ' + main + ": operation '" + oper + "' requires", Nvals, 'and', \ |
|---|
| 283 | len(vals), ' has passed!!' |
|---|
| 284 | print gen.table_tex_file.__doc__ |
|---|
| 285 | quit(-1) |
|---|
| 286 | vals2 = np.array(vals[2].split(cV),dtype=np.float).reshape(int(vals[0]), \ |
|---|
| 287 | int(vals[1])) |
|---|
| 288 | vals3 = vals[3].replace(cE,' ').split(cV) |
|---|
| 289 | vals4 = vals[4].replace(cE,' ').split(cV) |
|---|
| 290 | |
|---|
| 291 | print gen.table_tex_file(int(vals[0]), int(vals[1]), vals2, vals3, vals4, \ |
|---|
| 292 | vals[5]) |
|---|
| 293 | |
|---|
| 294 | elif oper == 'unitsDate': |
|---|
| 295 | Nvals = 3 |
|---|
| 296 | vals = opts.values.split(cS) |
|---|
| 297 | if vals[0] == 'h': |
|---|
| 298 | print gen.unitsDate.__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.unitsDate.__doc__ |
|---|
| 306 | quit(-1) |
|---|
| 307 | print gen.unitsDate(vals[0], vals[1], vals[2]) |
|---|
| 308 | |
|---|
| 309 | elif oper == 'variables_values': |
|---|
| 310 | Nvals = 1 |
|---|
| 311 | vals = opts.values.split(cS) |
|---|
| 312 | if vals[0] == 'h': |
|---|
| 313 | print gen.variables_values.__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.variables_values.__doc__ |
|---|
| 321 | quit(-1) |
|---|
| 322 | result = gen.variables_values(vals[0]) |
|---|
| 323 | print gen.numVector_String(result,':') |
|---|
| 324 | |
|---|
| 325 | elif oper == 'wdismean': |
|---|
| 326 | Nvals = 2 |
|---|
| 327 | vals = opts.values.split(cS) |
|---|
| 328 | if vals[0] == 'h': |
|---|
| 329 | print gen.wdismean.__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.wdismean.__doc__ |
|---|
| 337 | quit(-1) |
|---|
| 338 | vals0 = np.array(vals[0].split(cV), dtype=np.float) |
|---|
| 339 | vals1 = np.array(vals[1].split(cV), dtype=np.float).reshape(2,2) |
|---|
| 340 | |
|---|
| 341 | print gen.wdismean(vals0, vals1) |
|---|
| 342 | |
|---|