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