1 | # -*- coding: iso-8859-15 -*- |
---|
2 | # Generic program to transfrom ASCII observational data in columns to a netcdf |
---|
3 | # L. Fita, LMD February 2015 |
---|
4 | ## e.g. # create_OBSnetcdf.py -c '#' -d ACAR/description.dat -e space -f ACAR/2012/10/ACAR_121018.asc -g true -t 19491201000000,seconds -k trajectory |
---|
5 | |
---|
6 | import numpy as np |
---|
7 | from netCDF4 import Dataset as NetCDFFile |
---|
8 | import os |
---|
9 | import re |
---|
10 | from optparse import OptionParser |
---|
11 | |
---|
12 | # version |
---|
13 | version=1.2 |
---|
14 | |
---|
15 | # Filling values for floats, integer and string |
---|
16 | fillValueF = 1.e20 |
---|
17 | fillValueI = -99999 |
---|
18 | fillValueS = '---' |
---|
19 | |
---|
20 | # Length of the string variables |
---|
21 | StringLength = 200 |
---|
22 | |
---|
23 | # Size of the map for the complementary variables/maps |
---|
24 | Ndim2D = 100 |
---|
25 | |
---|
26 | main = 'create_OBSnetcdf.py' |
---|
27 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
28 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
---|
29 | |
---|
30 | fillValue = 1.e20 |
---|
31 | |
---|
32 | def searchInlist(listname, nameFind): |
---|
33 | """ Function to search a value within a list |
---|
34 | listname = list |
---|
35 | nameFind = value to find |
---|
36 | >>> searInlist(['1', '2', '3', '5'], '5') |
---|
37 | True |
---|
38 | """ |
---|
39 | for x in listname: |
---|
40 | if x == nameFind: |
---|
41 | return True |
---|
42 | return False |
---|
43 | |
---|
44 | |
---|
45 | def typemod(value, typeval): |
---|
46 | """ Function to give back a value in a given dtype |
---|
47 | >>> print(typemod(8.2223, 'np.float64')) |
---|
48 | <type 'numpy.float64'> |
---|
49 | >>> print(typemod(8.2223, 'tuple')) |
---|
50 | <type 'tuple'> |
---|
51 | """ |
---|
52 | fname='typemod' |
---|
53 | |
---|
54 | if typeval == 'int' or typeval == 'I': |
---|
55 | return int(value) |
---|
56 | elif typeval == 'long': |
---|
57 | return long(value) |
---|
58 | elif typeval == 'float' or typeval == 'F' or typeval == 'R': |
---|
59 | return float(value) |
---|
60 | elif typeval == 'complex': |
---|
61 | return complex(value) |
---|
62 | elif typeval == 'str' or typeval == 'S': |
---|
63 | return str(value) |
---|
64 | elif typeval == 'bool': |
---|
65 | return bool(value) |
---|
66 | elif typeval == 'B': |
---|
67 | return Str_Bool(value) |
---|
68 | elif typeval == 'list': |
---|
69 | newval = [] |
---|
70 | newval.append(value) |
---|
71 | return newval |
---|
72 | elif typeval == 'dic': |
---|
73 | newval = {} |
---|
74 | newval[value] = value |
---|
75 | return newval |
---|
76 | elif typeval == 'tuple': |
---|
77 | newv = [] |
---|
78 | newv.append(value) |
---|
79 | newval = tuple(newv) |
---|
80 | return newval |
---|
81 | elif typeval == 'np.int8': |
---|
82 | return np.int8(value) |
---|
83 | elif typeval == 'np.int16': |
---|
84 | return np.int16(value) |
---|
85 | elif typeval == 'np.int32': |
---|
86 | return np.int32(value) |
---|
87 | elif typeval == 'np.int64': |
---|
88 | return np.int64(value) |
---|
89 | elif typeval == 'np.uint8': |
---|
90 | return np.uint8(value) |
---|
91 | elif typeval == 'np.uint16': |
---|
92 | return np.uint16(value) |
---|
93 | elif typeval == 'np.np.uint32': |
---|
94 | return np.uint32(value) |
---|
95 | elif typeval == 'np.uint64': |
---|
96 | return np.uint64(value) |
---|
97 | elif typeval == 'np.float' or typeval == 'R': |
---|
98 | return np.float(value) |
---|
99 | elif typeval == 'np.float16': |
---|
100 | return np.float16(value) |
---|
101 | elif typeval == 'np.float32': |
---|
102 | return np.float32(value) |
---|
103 | elif typeval == 'float32': |
---|
104 | return np.float32(value) |
---|
105 | elif typeval == 'np.float64' or typeval == 'D': |
---|
106 | return np.float64(value) |
---|
107 | elif typeval == 'float64': |
---|
108 | return np.float64(value) |
---|
109 | elif typeval == 'np.complex': |
---|
110 | return np.complex(value) |
---|
111 | elif typeval == 'np.complex64': |
---|
112 | return np.complex64(value) |
---|
113 | elif typeval == 'np.complex128': |
---|
114 | return np.complex128(value) |
---|
115 | else: |
---|
116 | print errormsg |
---|
117 | print fname + ': data type "' + typeval + '" is not ready !' |
---|
118 | print errormsg |
---|
119 | quit(-1) |
---|
120 | |
---|
121 | return |
---|
122 | |
---|
123 | def str_list_k(string, cdiv, kind): |
---|
124 | """ Function to obtain a list of types of values from a string giving a split character |
---|
125 | string= String from which to obtain a list ('None' for None) |
---|
126 | cdiv= character to use to split the string |
---|
127 | kind= kind of desired value (as string like: 'np.float', 'int', 'np.float64', ....) |
---|
128 | >>> str_list_k('1:@#:$:56', ':', 'S') |
---|
129 | ['1', '@#', '$', '56'] |
---|
130 | >>> str_list_k('1:3.4:12.3', ':', 'np.float64') |
---|
131 | [1.0, 3.3999999999999999, 12.300000000000001] |
---|
132 | """ |
---|
133 | fname = 'str_list' |
---|
134 | |
---|
135 | if string.find(cdiv) != -1: |
---|
136 | listv = string.split(cdiv) |
---|
137 | else: |
---|
138 | if string == 'None': |
---|
139 | listv = None |
---|
140 | else: |
---|
141 | listv = [string] |
---|
142 | |
---|
143 | if listv is not None: |
---|
144 | finalist = [] |
---|
145 | for lv in listv: |
---|
146 | finalist.append(typemod(lv, kind)) |
---|
147 | else: |
---|
148 | finalist = None |
---|
149 | |
---|
150 | return finalist |
---|
151 | |
---|
152 | def stringS_dictvar(stringv, Dc=',', DVs=':'): |
---|
153 | """ Function to provide a dictionary from a String which list of DVs separated |
---|
154 | [key] [value] couples |
---|
155 | stringv: String with a dictionary content as [key1][DVs][val1][Dc][key2][DVs][Val2][Dc]... |
---|
156 | Sc: character to separate key values |
---|
157 | DVs: character to separate key-value couples |
---|
158 | >>> stringS_dictvar('i:1,iv:4,vii:7',',',':') |
---|
159 | {'i': '1', 'vii': '7', 'iv': '4'} |
---|
160 | """ |
---|
161 | fname = 'stringS_dictvar' |
---|
162 | |
---|
163 | if stringv.count(Dc) != 0: |
---|
164 | strvv = stringv.split(Dc) |
---|
165 | else: |
---|
166 | strvv = [stringv] |
---|
167 | |
---|
168 | dictv = {} |
---|
169 | for Svals in strvv: |
---|
170 | keyn = Svals.split(DVs)[0] |
---|
171 | valn = Svals.split(DVs)[1] |
---|
172 | dictv[keyn] = valn |
---|
173 | |
---|
174 | return dictv |
---|
175 | |
---|
176 | def set_attribute(ncvar, attrname, attrvalue): |
---|
177 | """ Sets a value of an attribute of a netCDF variable. Removes previous attribute value if exists |
---|
178 | ncvar = object netcdf variable |
---|
179 | attrname = name of the attribute |
---|
180 | attrvalue = value of the attribute |
---|
181 | """ |
---|
182 | import numpy as np |
---|
183 | from netCDF4 import Dataset as NetCDFFile |
---|
184 | |
---|
185 | attvar = ncvar.ncattrs() |
---|
186 | if searchInlist(attvar, attrname): |
---|
187 | attr = ncvar.delncattr(attrname) |
---|
188 | |
---|
189 | attr = ncvar.setncattr(attrname, attrvalue) |
---|
190 | |
---|
191 | return ncvar |
---|
192 | |
---|
193 | def set_attributek(ncv, attrname, attrval, attrkind): |
---|
194 | """ Sets a value of an attribute of a netCDF variable with a kind. Removes previous attribute value if exists |
---|
195 | ncvar = object netcdf variable |
---|
196 | attrname = name of the attribute |
---|
197 | attrvalue = value of the attribute |
---|
198 | atrtrkind = kind of attribute: 'S', string ('!' as spaces); 'U', unicode ('!' as spaces); 'I', integer; |
---|
199 | 'Inp32', numpy integer 32; 'R', ot 'F' real; ' npfloat', np.float; 'D', np.float64 |
---|
200 | """ |
---|
201 | fname = 'set_attributek' |
---|
202 | validk = {'S': 'string', 'U': 'unicode', 'I': 'integer', \ |
---|
203 | 'Inp32': 'integer long (np.int32)', 'F': 'float', 'R': 'float', \ |
---|
204 | 'npfloat': 'np.float', 'D': 'float long (np.float64)'} |
---|
205 | |
---|
206 | if type(attrkind) == type('s'): |
---|
207 | if attrkind == 'S': |
---|
208 | attrvalue = str(attrval.replace('!', ' ')) |
---|
209 | elif attrkind == 'U': |
---|
210 | attrvalue = unicode(attrval.replace('!',' ')) |
---|
211 | elif attrkind == 'I': |
---|
212 | attrvalue = np.int(attrval) |
---|
213 | elif attrkind == 'R' or attrkind == 'F' : |
---|
214 | attrvalue = float(attrval) |
---|
215 | elif attrkind == 'npfloat': |
---|
216 | attrvalue = np.float(attrval) |
---|
217 | elif attrkind == 'D': |
---|
218 | attrvalue = np.float64(attrval) |
---|
219 | else: |
---|
220 | print errormsg |
---|
221 | print ' ' + fname + ": '" + attrkind + "' kind of attribute is not ready!" |
---|
222 | print ' valid values: _______' |
---|
223 | for key in validk.keys(): |
---|
224 | print ' ', key,':', validk[key] |
---|
225 | quit(-1) |
---|
226 | else: |
---|
227 | if attrkind == type(str('a')): |
---|
228 | attrvalue = str(attrval.replace('!', ' ')) |
---|
229 | elif attrkind == type(unicode('a')): |
---|
230 | attrvalue = unicode(attrval.replace('!',' ')) |
---|
231 | elif attrkind == type(np.int(1)): |
---|
232 | attrvalue = np.int(attrval) |
---|
233 | elif attrkind == np.dtype('i'): |
---|
234 | attrvalue = np.int32(attrval) |
---|
235 | elif attrkind == type(float(1.)): |
---|
236 | attrvalue = float(attrval) |
---|
237 | elif attrkind == type(np.float(1.)): |
---|
238 | attrvalue = np.float(attrval) |
---|
239 | elif attrkind == np.dtype('float32'): |
---|
240 | attrvalue = np.array(attrval, dtype='f') |
---|
241 | elif attrkind == type(np.float32(1.)): |
---|
242 | attrvalue = np.float32(attrval) |
---|
243 | elif attrkind == type(np.float64(1.)): |
---|
244 | attrvalue = np.float64(attrval) |
---|
245 | elif attrkind == type(np.array([1.,2.])): |
---|
246 | attrvalue = attrval |
---|
247 | else: |
---|
248 | print errormsg |
---|
249 | print ' ' + fname + ": '" + attrkind + "' kind of attribute is not ready!" |
---|
250 | print ' valid values: _______' |
---|
251 | for key in validk.keys(): |
---|
252 | print ' ', key,':', validk[key] |
---|
253 | quit(-1) |
---|
254 | |
---|
255 | attvar = ncv.ncattrs() |
---|
256 | if searchInlist(attvar, attrname): |
---|
257 | attr = ncv.delncattr(attrname) |
---|
258 | |
---|
259 | if attrname == 'original_subroutines_author': |
---|
260 | attrvalue = 'Cindy Bruyere' |
---|
261 | attr = ncv.setncattr(attrname, attrvalue) |
---|
262 | |
---|
263 | return attr |
---|
264 | |
---|
265 | |
---|
266 | def basicvardef(varobj, vstname, vlname, vunits): |
---|
267 | """ Function to give the basic attributes to a variable |
---|
268 | varobj= netCDF variable object |
---|
269 | vstname= standard name of the variable |
---|
270 | vlname= long name of the variable |
---|
271 | vunits= units of the variable |
---|
272 | """ |
---|
273 | attr = varobj.setncattr('standard_name', vstname) |
---|
274 | attr = varobj.setncattr('long_name', vlname) |
---|
275 | attr = varobj.setncattr('units', vunits) |
---|
276 | |
---|
277 | return |
---|
278 | |
---|
279 | def remove_NONascii(string): |
---|
280 | """ Function to remove that characters which are not in the standard 127 ASCII |
---|
281 | string= string to transform |
---|
282 | >>> remove_NONascii('LluÃs') |
---|
283 | Lluis |
---|
284 | """ |
---|
285 | fname = 'remove_NONascii' |
---|
286 | |
---|
287 | newstring = string |
---|
288 | |
---|
289 | RTFchar= ['á', 'é', 'Ã', 'ó', 'ú', 'à ', 'Ú', 'ì', 'ò', 'ù', 'â', 'ê', 'î', 'ÃŽ', \ |
---|
290 | 'û', 'À', 'ë', 'ï', 'ö', 'ÃŒ', 'ç', 'ñ','Ê', 'Å', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', \ |
---|
291 | 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã',\ |
---|
292 | 'Ã', 'Å', '\n', '\t'] |
---|
293 | ASCchar= ['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', \ |
---|
294 | 'u', 'a', 'e', 'i', 'o', 'u', 'c', 'n','ae', 'oe', 'A', 'E', 'I', 'O', 'U', 'A', \ |
---|
295 | 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'C', 'N',\ |
---|
296 | 'AE', 'OE', '', ' '] |
---|
297 | |
---|
298 | Nchars = len(RTFchar) |
---|
299 | for ichar in range(Nchars): |
---|
300 | foundchar = string.find(RTFchar[ichar]) |
---|
301 | if foundchar != -1: |
---|
302 | newstring = newstring.replace(RTFchar[ichar], ASCchar[ichar]) |
---|
303 | |
---|
304 | return newstring |
---|
305 | |
---|
306 | def read_description(fdobs, dbg): |
---|
307 | """ reads the description file of the observational data-set |
---|
308 | fdobs= descriptive observational data-set |
---|
309 | dbg= boolean argument for debugging |
---|
310 | * Each station should have a 'description.dat' file with: |
---|
311 | institution=Institution who creates the data |
---|
312 | department=Department within the institution |
---|
313 | scientists=names of the data producers |
---|
314 | contact=contact of the data producers |
---|
315 | description=description of the observations |
---|
316 | acknowledgement=sentence of acknowlegement |
---|
317 | comment=comment for the measurements |
---|
318 | |
---|
319 | MissingValue='|' list of ASCII values for missing values within the data |
---|
320 | (as they appear!, 'empty' for no value at all) |
---|
321 | comment=comments |
---|
322 | |
---|
323 | varN='|' list of variable names |
---|
324 | varLN='|' list of long variable names |
---|
325 | varU='|' list units of the variables |
---|
326 | varBUFR='|' list BUFR code of the variables |
---|
327 | varTYPE='|' list of variable types ('D', 'F', 'I', 'I64', 'S') |
---|
328 | varOPER='|' list of operations to do to the variables to meet their units ([oper],[val]) |
---|
329 | [oper]: |
---|
330 | -, nothing |
---|
331 | sumc, add [val] |
---|
332 | subc, rest [val] |
---|
333 | mulc, multiply by [val] |
---|
334 | divc, divide by [val] |
---|
335 | rmchar,[val],[pos], remove [val] characters from [pos]='B', beginning, 'E', end |
---|
336 | repl,[char1]:[val1];...;[charM]:[valM], replace a given character [chari] |
---|
337 | by a value [vali] |
---|
338 | NAMElon=name of the variable with the longitude (x position) |
---|
339 | NAMElat=name of the variable with the latitude (y position) |
---|
340 | NAMEheight=ind_alt |
---|
341 | NAMEtime=name of the varibale with the time |
---|
342 | FMTtime=format of the time (as in 'C', 'CFtime' for already CF-like time) |
---|
343 | |
---|
344 | """ |
---|
345 | fname = 'read_description' |
---|
346 | |
---|
347 | descobj = open(fdobs, 'r') |
---|
348 | desc = {} |
---|
349 | |
---|
350 | namevalues = [] |
---|
351 | |
---|
352 | for line in descobj: |
---|
353 | if line[0:1] != '#' and len(line) > 1 : |
---|
354 | descn = remove_NONascii(line.split('=')[0]) |
---|
355 | descv = remove_NONascii(line.split('=')[1]) |
---|
356 | namevalues.append(descn) |
---|
357 | if descn[0:3] != 'var': |
---|
358 | if descn != 'MissingValue': |
---|
359 | desc[descn] = descv |
---|
360 | else: |
---|
361 | desc[descn] = [] |
---|
362 | for dn in descv.split('|'): desc[descn].append(dn) |
---|
363 | print ' ' + fname + ': missing values found:',desc[descn] |
---|
364 | elif descn[0:3] == 'var': |
---|
365 | desc[descn] = descv.split('|') |
---|
366 | elif descn[0:4] == 'NAME': |
---|
367 | desc[descn] = descv |
---|
368 | elif descn[0:3] == 'FMT': |
---|
369 | desc[descn] = descv |
---|
370 | if not desc.has_key('varOPER'): desc['varOPER'] = None |
---|
371 | |
---|
372 | if dbg: |
---|
373 | Nvars = len(desc['varN']) |
---|
374 | print ' ' + fname + ": description content of '" + fdobs + "'______________" |
---|
375 | for varn in namevalues: |
---|
376 | if varn[0:3] != 'var': |
---|
377 | print ' ' + varn + ':',desc[varn] |
---|
378 | elif varn == 'varN': |
---|
379 | print ' * Variables:' |
---|
380 | for ivar in range(Nvars): |
---|
381 | varname = desc['varN'][ivar] |
---|
382 | varLname = desc['varLN'][ivar] |
---|
383 | varunits = desc['varU'][ivar] |
---|
384 | if desc.has_key('varBUFR'): |
---|
385 | varbufr = desc['varBUFR'][ivar] |
---|
386 | else: |
---|
387 | varbufr = None |
---|
388 | if desc['varOPER'] is not None: |
---|
389 | opv = desc['varOPER'][ivar] |
---|
390 | else: |
---|
391 | opv = None |
---|
392 | print ' ', ivar, varname+':',varLname,'[',varunits, \ |
---|
393 | ']','bufr code:',varbufr,'oper:',opv |
---|
394 | |
---|
395 | descobj.close() |
---|
396 | |
---|
397 | return desc |
---|
398 | |
---|
399 | def value_fmt(val, miss, op, fmt): |
---|
400 | """ Function to transform an ASCII value to a given format |
---|
401 | val= value to transform |
---|
402 | miss= list of possible missing values |
---|
403 | op= operation to perform to the value |
---|
404 | fmt= format to take: |
---|
405 | 'D': float double precission |
---|
406 | 'F': float |
---|
407 | 'I': integer |
---|
408 | 'I64': 64-bits integer |
---|
409 | 'S': string |
---|
410 | >>> value_fmt('9876.12', '-999', 'F') |
---|
411 | 9876.12 |
---|
412 | """ |
---|
413 | |
---|
414 | fname = 'value_fmt' |
---|
415 | |
---|
416 | aopers = ['sumc','subc','mulc','divc', 'rmchar', 'repl'] |
---|
417 | |
---|
418 | fmts = ['D', 'F', 'I', 'I64', 'S'] |
---|
419 | Nfmts = len(fmts) |
---|
420 | |
---|
421 | if not searchInlist(miss,val): |
---|
422 | if searchInlist(miss,'empty') and len(val) == 0: |
---|
423 | newval = None |
---|
424 | else: |
---|
425 | if op != '-': |
---|
426 | opern = op.split(',')[0] |
---|
427 | operv = op.split(',')[1] |
---|
428 | |
---|
429 | if not searchInlist(aopers,opern): |
---|
430 | print errormsg |
---|
431 | print ' ' + fname + ": operation '" + opern + "' not ready!!" |
---|
432 | print ' availables:',aopers |
---|
433 | quit(-1) |
---|
434 | else: |
---|
435 | opern = 'sumc' |
---|
436 | operv = '0' |
---|
437 | |
---|
438 | if not searchInlist(fmts, fmt): |
---|
439 | print errormsg |
---|
440 | print ' ' + fname + ": format '" + fmt + "' not ready !!" |
---|
441 | quit(-1) |
---|
442 | else: |
---|
443 | if fmt == 'D': |
---|
444 | opv = np.float32(operv) |
---|
445 | if opern == 'sumc': newval = np.float32(val) + opv |
---|
446 | elif opern == 'subc': newval = np.float32(val) - opv |
---|
447 | elif opern == 'mulc': newval = np.float32(val) * opv |
---|
448 | elif opern == 'divc': newval = np.float32(val) / opv |
---|
449 | elif opern == 'rmchar': |
---|
450 | opv = int(operv) |
---|
451 | Lval = len(val) |
---|
452 | if op.split(',')[2] == 'B': |
---|
453 | newval = np.float32(val[opv:Lval+1]) |
---|
454 | elif op.split(',')[2] == 'E': |
---|
455 | newval = np.float32(val[Lval-opv:Lval]) |
---|
456 | else: |
---|
457 | print errormsg |
---|
458 | print ' ' + fname + ": operation '" + opern + "' not " +\ |
---|
459 | " work with '" + op.split(',')[2] + "' !!" |
---|
460 | quit(-1) |
---|
461 | elif opern == 'repl': |
---|
462 | replvals = stringS_dictvar(operv, Dc=';', DVs=':') |
---|
463 | replaced = False |
---|
464 | for repls in replvals.keys(): |
---|
465 | if val == repls: |
---|
466 | newval = np.float32(replvals[val]) |
---|
467 | replaced = True |
---|
468 | break |
---|
469 | if not replaced: newval = np.float32(val) |
---|
470 | |
---|
471 | elif fmt == 'F': |
---|
472 | opv = np.float(operv) |
---|
473 | if opern == 'sumc': newval = np.float(val) + opv |
---|
474 | elif opern == 'subc': newval = np.float(val) - opv |
---|
475 | elif opern == 'mulc': newval = np.float(val) * opv |
---|
476 | elif opern == 'divc': newval = np.float(val) / opv |
---|
477 | elif opern == 'rmchar': |
---|
478 | opv = int(operv) |
---|
479 | Lval = len(val) |
---|
480 | if op.split(',')[2] == 'B': |
---|
481 | newval = np.float(val[opv:Lval+1]) |
---|
482 | elif op.split(',')[2] == 'E': |
---|
483 | newval = np.float(val[0:Lval-opv]) |
---|
484 | else: |
---|
485 | print errormsg |
---|
486 | print ' ' + fname + ": operation '" + opern + "' not " +\ |
---|
487 | " work with '" + op.split(',')[2] + "' !!" |
---|
488 | quit(-1) |
---|
489 | elif opern == 'repl': |
---|
490 | replvals = stringS_dictvar(operv, Dc=';', DVs=':') |
---|
491 | replaced = False |
---|
492 | for repls in replvals.keys(): |
---|
493 | if val == repls: |
---|
494 | newval = np.float(replvals[val]) |
---|
495 | replaced = True |
---|
496 | break |
---|
497 | if not replaced: newval = np.float(val) |
---|
498 | |
---|
499 | elif fmt == 'I': |
---|
500 | if opern == 'sumc': newval = int(val) + int(operv) |
---|
501 | elif opern == 'subc': newval = int(val) - int(operv) |
---|
502 | elif opern == 'mulc': newval = int(val) * int(operv) |
---|
503 | elif opern == 'divc': newval = int(val) / int(operv) |
---|
504 | elif opern == 'rmchar': |
---|
505 | opv = int(operv) |
---|
506 | Lval = len(val) |
---|
507 | if op.split(',')[2] == 'B': |
---|
508 | newval = int(val[opv:Lval+1]) |
---|
509 | elif op.split(',')[2] == 'E': |
---|
510 | newval = int(val[0:Lval-opv]) |
---|
511 | else: |
---|
512 | print errormsg |
---|
513 | print ' ' + fname + ": operation '" + opern + "' not " +\ |
---|
514 | " work with '" + op.split(',')[2] + "' !!" |
---|
515 | quit(-1) |
---|
516 | elif opern == 'repl': |
---|
517 | replvals = stringS_dictvar(operv, Dc=';', DVs=':') |
---|
518 | replaced = False |
---|
519 | for repls in replvals.keys(): |
---|
520 | if val == repls: |
---|
521 | newval = int(replvals[val]) |
---|
522 | replaced = True |
---|
523 | break |
---|
524 | if not replaced: newval = int(val) |
---|
525 | |
---|
526 | elif fmt == 'I64': |
---|
527 | opv = np.int64(operv) |
---|
528 | if opern == 'sumc': newval = np.int64(val) + opv |
---|
529 | elif opern == 'subc': newval = np.int64(val) - opv |
---|
530 | elif opern == 'mulc': newval = np.int64(val) * opv |
---|
531 | elif opern == 'divc': newval = np.int64(val) / opv |
---|
532 | elif opern == 'rmchar': |
---|
533 | opv = int(operv) |
---|
534 | Lval = len(val) |
---|
535 | if op.split(',')[2] == 'B': |
---|
536 | newval = np.int64(val[opv:Lval+1]) |
---|
537 | elif op.split(',')[2] == 'E': |
---|
538 | newval = np.int64(val[0:Lval-opv]) |
---|
539 | else: |
---|
540 | print errormsg |
---|
541 | print ' ' + fname + ": operation '" + opern + "' not " +\ |
---|
542 | " work with '" + op.split(',')[2] + "' !!" |
---|
543 | quit(-1) |
---|
544 | elif opern == 'repl': |
---|
545 | replvals = stringS_dictvar(operv, Dc=';', DVs=':') |
---|
546 | replaced = False |
---|
547 | for repls in replvals.keys(): |
---|
548 | if val == repls: |
---|
549 | newval = np.int64(replvals[val]) |
---|
550 | replaced = True |
---|
551 | break |
---|
552 | if not replaced: newval = np.int64(val) |
---|
553 | |
---|
554 | elif fmt == 'S': |
---|
555 | if opern == 'rmchar': |
---|
556 | opv = int(operv) |
---|
557 | Lval = len(val) |
---|
558 | if op.split(',')[2] == 'B': |
---|
559 | newval = val[opv:Lval+1] |
---|
560 | elif op.split(',')[2] == 'E': |
---|
561 | newval = val[0:Lval-opv] |
---|
562 | else: |
---|
563 | print errormsg |
---|
564 | print ' ' + fname + ": operation '" + opern + "' not " +\ |
---|
565 | " work with '" + op.split(',')[2] + "' !!" |
---|
566 | quit(-1) |
---|
567 | elif opern == 'repl': |
---|
568 | replvals = stringS_dictvar(operv, Dc=';', DVs=':') |
---|
569 | replaced = False |
---|
570 | for repls in replvals.keys(): |
---|
571 | if val == repls: |
---|
572 | newval = replvals[val] |
---|
573 | replaced = True |
---|
574 | break |
---|
575 | if not replaced: newval = val |
---|
576 | |
---|
577 | else: |
---|
578 | newval = val |
---|
579 | |
---|
580 | else: |
---|
581 | newval = None |
---|
582 | |
---|
583 | return newval |
---|
584 | |
---|
585 | def getting_fixedline(line, cuts, types, dbg=False): |
---|
586 | """ Function to get the values from a line of text with fixed lenght of different values |
---|
587 | line: line with values |
---|
588 | cuts: character number where a value ends |
---|
589 | types: consecutive type of values |
---|
590 | 'I': integer |
---|
591 | 'R': real |
---|
592 | 'D': float64 |
---|
593 | 'S': string |
---|
594 | 'B': boolean |
---|
595 | dbg: debug mode (default False) |
---|
596 | >>> Sline=' 87007 03012015 25.6 6.4 9.4 5 15' |
---|
597 | >>> getting_fixedline(Sline, [8, 17, 23, 29, 36, 40, 45, 50], ['I', 'R', 'R', 'R', 'I', 'R', 'R', 'R']) |
---|
598 | [87007, 3012015.0, 25.6, 6.4, -99999, -99999, 9.4, 5.0, 15.0] |
---|
599 | """ |
---|
600 | fname = 'getting_fixedline' |
---|
601 | |
---|
602 | if len(cuts) + 1 != len(types): |
---|
603 | print errormsg |
---|
604 | print ' ' + fname + ': The number of types :', len(types), 'must be +1', \ |
---|
605 | 'number of cuts:', len(cuts) |
---|
606 | quit(-1) |
---|
607 | |
---|
608 | values = [] |
---|
609 | val = line[0:cuts[0]] |
---|
610 | if len(val.replace(' ','')) >= 1: |
---|
611 | values.append(typemod(val, types[0])) |
---|
612 | else: |
---|
613 | if types[0] == 'I': values.append(fillValueI) |
---|
614 | elif types[0] == 'R': values.append(fillValueF) |
---|
615 | elif types[0] == 'D': values.append(fillValueF) |
---|
616 | elif types[0] == 'S': values.append(fillValueS) |
---|
617 | elif types[0] == 'B': values.append(fillValueB) |
---|
618 | |
---|
619 | Ncuts = len(cuts) |
---|
620 | for ic in range(1,Ncuts): |
---|
621 | val = line[cuts[ic-1]:cuts[ic]] |
---|
622 | if dbg: print ic, ':', val, '-->', types[ic] |
---|
623 | if len(val.replace(' ','')) >= 1: |
---|
624 | values.append(typemod(val, types[ic])) |
---|
625 | else: |
---|
626 | if types[ic] == 'I': values.append(fillValueI) |
---|
627 | elif types[ic] == 'R': values.append(fillValueF) |
---|
628 | elif types[ic] == 'D': values.append(fillValueF) |
---|
629 | elif types[ic] == 'S': values.append(fillValueS) |
---|
630 | elif types[ic] == 'B': values.append(fillValueB) |
---|
631 | |
---|
632 | # Last value |
---|
633 | Lline = len(line) |
---|
634 | val = line[cuts[Ncuts-1]:Lline] |
---|
635 | if len(val.replace(' ','')) >= 1: |
---|
636 | values.append(typemod(val, types[Ncuts])) |
---|
637 | else: |
---|
638 | if types[Ncuts] == 'I': values.append(fillValueI) |
---|
639 | elif types[Ncuts] == 'R': values.append(fillValueF) |
---|
640 | elif types[Ncuts] == 'D': values.append(fillValueF) |
---|
641 | elif types[Ncuts] == 'S': values.append(fillValueS) |
---|
642 | elif types[Ncuts] == 'B': values.append(fillValueB) |
---|
643 | |
---|
644 | return values |
---|
645 | |
---|
646 | |
---|
647 | def getting_fixedline_NOk(line, cuts, miss, dbg=False): |
---|
648 | """ Function to get the values from a line of text with fixed lenght of |
---|
649 | different values without types |
---|
650 | line: line with values |
---|
651 | cuts: character number where a value ends |
---|
652 | miss: character form issed values |
---|
653 | dbg: debug mode (default False) |
---|
654 | >>> Sline=' 87007 03012015 25.6 6.4 9.4 5 15' |
---|
655 | >>> getting_fixedline_NOk(Sline, [8, 17, 23, 29, 36, 40, 45, 50], '#') |
---|
656 | ['87007', '03012015', '25.6', '6.4', '#', '#', '9.4', '5', '15'] |
---|
657 | """ |
---|
658 | fname = 'getting_fixedline_NOk' |
---|
659 | |
---|
660 | values = [] |
---|
661 | val = line[0:cuts[0]] |
---|
662 | if len(val.replace(' ','')) >= 1: |
---|
663 | values.append(val.replace(' ','')) |
---|
664 | else: |
---|
665 | values.append(miss) |
---|
666 | |
---|
667 | Ncuts = len(cuts) |
---|
668 | for ic in range(1,Ncuts): |
---|
669 | val = line[cuts[ic-1]:cuts[ic]] |
---|
670 | if dbg: print ic, ':', val |
---|
671 | if len(val.replace(' ','')) >= 1: |
---|
672 | values.append(val.replace(' ','')) |
---|
673 | else: |
---|
674 | values.append(miss) |
---|
675 | |
---|
676 | # Last value |
---|
677 | Lline = len(line) |
---|
678 | val = line[cuts[Ncuts-1]:Lline] |
---|
679 | if len(val.replace(' ','')) >= 1: |
---|
680 | values.append(val.replace(' ','')) |
---|
681 | else: |
---|
682 | values.append(miss) |
---|
683 | |
---|
684 | return values |
---|
685 | |
---|
686 | def read_datavalues(dataf, comchar, colchar, fmt, jBl, oper, miss, varns, dbg): |
---|
687 | """ Function to read from an ASCII file values in column |
---|
688 | dataf= data file |
---|
689 | comchar= list of the characters indicating comment in the file |
---|
690 | colchar= character which indicate end of value in the column |
---|
691 | dbg= debug mode or not |
---|
692 | fmt= list of kind of values to be found |
---|
693 | jBl= number of lines to jump from the beginning of file |
---|
694 | oper= list of operations to perform |
---|
695 | miss= missing value |
---|
696 | varns= list of name of the variables to find |
---|
697 | """ |
---|
698 | |
---|
699 | fname = 'read_datavalues' |
---|
700 | |
---|
701 | ofile = open(dataf, 'r') |
---|
702 | Nvals = len(fmt) |
---|
703 | |
---|
704 | if oper is None: |
---|
705 | opers = [] |
---|
706 | for ioper in range(Nvals): |
---|
707 | opers.append('-') |
---|
708 | else: |
---|
709 | opers = oper |
---|
710 | |
---|
711 | finalvalues = {} |
---|
712 | |
---|
713 | iline = 0 |
---|
714 | for line in ofile: |
---|
715 | line = line.replace('\n','').replace(chr(13),'') |
---|
716 | if not searchInlist(comchar,line[0:1]) and len(line) > 1 and iline > jBl-1: |
---|
717 | # Getting values |
---|
718 | if colchar[0:4] != 'None': |
---|
719 | values0 = line.split(colchar) |
---|
720 | else: |
---|
721 | ltypes=str_list_k(colchar[5:len(colchar)+1],',','I') |
---|
722 | values0 = getting_fixedline_NOk(line, ltypes, miss[0], dbg=dbg) |
---|
723 | # Removing no-value columns |
---|
724 | values = [] |
---|
725 | for iv in values0: |
---|
726 | if len(iv) > 0: values.append(iv) |
---|
727 | |
---|
728 | Nvalues = len(values) |
---|
729 | # Checkings for wierd characters at the end of lines (use it to check) |
---|
730 | # if values[Nvalues-1][0:4] == '-999': |
---|
731 | # print line,'last v:',values[Nvalues-1],'len:',len(values[Nvalues-1]) |
---|
732 | # for ic in range(len(values[Nvalues-1])): |
---|
733 | # print ic,ord(values[Nvalues-1][ic:ic+1]) |
---|
734 | # quit() |
---|
735 | |
---|
736 | if len(values[Nvalues-1]) == 0: |
---|
737 | Nvalues = Nvalues - 1 |
---|
738 | |
---|
739 | if Nvalues != Nvals: |
---|
740 | print warnmsg |
---|
741 | print ' ' + fname + ': number of formats:',Nvals,' and number of ', \ |
---|
742 | 'values:',Nvalues,' with split character *' + colchar + \ |
---|
743 | '* does not coincide!!' |
---|
744 | print ' * what is found is ________' |
---|
745 | if Nvalues > Nvals: |
---|
746 | Nshow = Nvals |
---|
747 | for ivar in range(Nshow): |
---|
748 | print ' ',varns[ivar],'fmt:',fmt[ivar],'value:',values[ivar] |
---|
749 | print ' missing formats for:',values[Nshow:Nvalues+1] |
---|
750 | print ' values not considered, continue' |
---|
751 | else: |
---|
752 | Nshow = Nvalues |
---|
753 | for ivar in range(Nshow): |
---|
754 | print ' ',varns[ivar],'fmt:',fmt[ivar],'value:',values[ivar] |
---|
755 | print ' excess of formats:',fmt[Nshow:Nvals+1] |
---|
756 | quit(-1) |
---|
757 | |
---|
758 | # Reading and transforming values |
---|
759 | if dbg: print ' ' + fname + ': values found _________' |
---|
760 | |
---|
761 | for ivar in range(Nvals): |
---|
762 | if dbg: |
---|
763 | print iline, ',', ivar, ' ', varns[ivar],'value:',values[ivar], \ |
---|
764 | miss,opers[ivar], fmt[ivar] |
---|
765 | |
---|
766 | if iline == 0: |
---|
767 | listvals = [] |
---|
768 | listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ |
---|
769 | fmt[ivar])) |
---|
770 | finalvalues[varns[ivar]] = listvals |
---|
771 | else: |
---|
772 | listvals = finalvalues[varns[ivar]] |
---|
773 | listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ |
---|
774 | fmt[ivar])) |
---|
775 | finalvalues[varns[ivar]] = listvals |
---|
776 | else: |
---|
777 | # First line without values |
---|
778 | if iline == 0: iline = -1 |
---|
779 | |
---|
780 | iline = iline + 1 |
---|
781 | |
---|
782 | ofile.close() |
---|
783 | |
---|
784 | return finalvalues |
---|
785 | |
---|
786 | def writing_str_nc(varo, values, Lchar): |
---|
787 | """ Function to write string values in a netCDF variable as a chain of 1char values |
---|
788 | varo= netCDF variable object |
---|
789 | values = list of values to introduce |
---|
790 | Lchar = length of the string in the netCDF file |
---|
791 | """ |
---|
792 | |
---|
793 | Nvals = len(values) |
---|
794 | |
---|
795 | for iv in range(Nvals): |
---|
796 | stringv=values[iv] |
---|
797 | charvals = np.chararray(Lchar) |
---|
798 | Lstr = len(stringv) |
---|
799 | charvals[Lstr:Lchar] = '' |
---|
800 | |
---|
801 | for ich in range(Lstr): |
---|
802 | charvals[ich] = stringv[ich:ich+1] |
---|
803 | |
---|
804 | varo[iv,:] = charvals |
---|
805 | |
---|
806 | return |
---|
807 | |
---|
808 | def Stringtimes_CF(tvals, fmt, Srefdate, tunits, dbg): |
---|
809 | """ Function to transform a given data in String formats to a CF date |
---|
810 | tvals= string temporal values |
---|
811 | fmt= format of the the time values |
---|
812 | Srefdate= reference date in [YYYY][MM][DD][HH][MI][SS] format |
---|
813 | tunits= units to use ('weeks', 'days', 'hours', 'minutes', 'seconds') |
---|
814 | dbg= debug |
---|
815 | >>> Stringtimes_CF(['19760217082712','20150213101837'], '%Y%m%d%H%M%S', |
---|
816 | '19491201000000', 'hours', False) |
---|
817 | [229784.45333333 571570.31027778] |
---|
818 | """ |
---|
819 | import datetime as dt |
---|
820 | |
---|
821 | fname = 'Stringtimes' |
---|
822 | |
---|
823 | dimt = len(tvals) |
---|
824 | |
---|
825 | yrref = int(Srefdate[0:4]) |
---|
826 | monref = int(Srefdate[4:6]) |
---|
827 | dayref = int(Srefdate[6:8]) |
---|
828 | horref = int(Srefdate[8:10]) |
---|
829 | minref = int(Srefdate[10:12]) |
---|
830 | secref = int(Srefdate[12:14]) |
---|
831 | refdate = dt.datetime( yrref, monref, dayref, horref, minref, secref) |
---|
832 | |
---|
833 | cftimes = np.zeros((dimt), dtype=np.float) |
---|
834 | |
---|
835 | Nfmt=len(fmt.split('%')) |
---|
836 | |
---|
837 | if dbg: print ' ' + fname + ': fmt=',fmt,'refdate:',Srefdate,'uits:',tunits, \ |
---|
838 | 'date dt_days dt_time deltaseconds CFtime _______' |
---|
839 | for it in range(dimt): |
---|
840 | |
---|
841 | # Removing excess of mili-seconds (up to 6 decimals) |
---|
842 | if fmt.split('%')[Nfmt-1] == 'f': |
---|
843 | tpoints = tvals[it].split('.') |
---|
844 | if len(tpoints[len(tpoints)-1]) > 6: |
---|
845 | milisec = '{0:.6f}'.format(np.float('0.'+tpoints[len(tpoints)-1]))[0:7] |
---|
846 | newtval = '' |
---|
847 | for ipt in range(len(tpoints)-1): |
---|
848 | newtval = newtval + tpoints[ipt] + '.' |
---|
849 | newtval = newtval + str(milisec)[2:len(milisec)+1] |
---|
850 | else: |
---|
851 | newtval = tvals[it] |
---|
852 | tval = dt.datetime.strptime(newtval, fmt) |
---|
853 | else: |
---|
854 | tval = dt.datetime.strptime(tvals[it], fmt) |
---|
855 | |
---|
856 | deltatime = tval - refdate |
---|
857 | deltaseconds = deltatime.days*24.*3600. + deltatime.seconds + \ |
---|
858 | deltatime.microseconds/100000. |
---|
859 | if tunits == 'weeks': |
---|
860 | deltat = 7.*24.*3600. |
---|
861 | elif tunits == 'days': |
---|
862 | deltat = 24.*3600. |
---|
863 | elif tunits == 'hours': |
---|
864 | deltat = 3600. |
---|
865 | elif tunits == 'minutes': |
---|
866 | deltat = 60. |
---|
867 | elif tunits == 'seconds': |
---|
868 | deltat = 1. |
---|
869 | else: |
---|
870 | print errormsg |
---|
871 | print ' ' + fname + ": time units '" + tunits + "' not ready !!" |
---|
872 | quit(-1) |
---|
873 | |
---|
874 | cftimes[it] = deltaseconds / deltat |
---|
875 | if dbg: |
---|
876 | print ' ' + tvals[it], deltatime, deltaseconds, cftimes[it] |
---|
877 | |
---|
878 | return cftimes |
---|
879 | |
---|
880 | def adding_complementary(onc, dscn, okind, dvalues, tvals, refCFt, CFtu, Nd, dbg): |
---|
881 | """ Function to add complementary variables as function of the observational type |
---|
882 | onc= netcdf objecto file to add the variables |
---|
883 | dscn= description dictionary |
---|
884 | okind= observational kind |
---|
885 | dvalues= values |
---|
886 | tvals= CF time values |
---|
887 | refCFt= reference time of CF time (in [YYYY][MM][DD][HH][MI][SS]) |
---|
888 | CFtu= CF time units |
---|
889 | Nd= size of the domain |
---|
890 | dbg= debugging flag |
---|
891 | """ |
---|
892 | import numpy.ma as ma |
---|
893 | |
---|
894 | fname = 'adding_complementary' |
---|
895 | |
---|
896 | # Kind of observations which require de integer lon/lat (for the 2D map) |
---|
897 | map2D=['multi-points', 'trajectory'] |
---|
898 | |
---|
899 | SrefCFt = refCFt[0:4] +'-'+ refCFt[4:6] +'-'+ refCFt[6:8] + ' ' + refCFt[8:10] + \ |
---|
900 | ':'+ refCFt[10:12] +':'+ refCFt[12:14] |
---|
901 | |
---|
902 | if dscn['NAMElon'] == '-' or dscn['NAMElat'] == '-': |
---|
903 | print errormsg |
---|
904 | print ' ' + fname + ": to complement a '" + okind + "' observation kind " + \ |
---|
905 | " a given longitude ('NAMElon':",dscn['NAMElon'],") and latitude ('" + \ |
---|
906 | "'NAMElat:'", dscn['NAMElat'],') from the data has to be provided and ' + \ |
---|
907 | 'any are given !!' |
---|
908 | quit(-1) |
---|
909 | |
---|
910 | if not dvalues.has_key(dscn['NAMElon']) or not dvalues.has_key(dscn['NAMElat']): |
---|
911 | print errormsg |
---|
912 | print ' ' + fname + ": observations do not have 'NAMElon':", \ |
---|
913 | dscn['NAMElon'],"and/or 'NAMElat:'", dscn['NAMElat'],' !!' |
---|
914 | print ' available data:',dvalues.keys() |
---|
915 | quit(-1) |
---|
916 | |
---|
917 | if okind == 'trajectory': |
---|
918 | if dscn['NAMEheight'] == '-': |
---|
919 | print warnmsg |
---|
920 | print ' ' + fname + ": to complement a '" + okind + "' observation " + \ |
---|
921 | "kind a given height ('NAMEheight':",dscn['NAMEheight'],"') might " + \ |
---|
922 | 'be provided and any is given !!' |
---|
923 | quit(-1) |
---|
924 | |
---|
925 | if not dvalues.has_key(dscn['NAMEheight']): |
---|
926 | print errormsg |
---|
927 | print ' ' + fname + ": observations do not have 'NAMEtime':", \ |
---|
928 | dscn['NAMEtime'],' !!' |
---|
929 | print ' available data:',dvalues.keys() |
---|
930 | quit(-1) |
---|
931 | |
---|
932 | if searchInlist(map2D, okind): |
---|
933 | # A new 2D map with the number of observation will be added for that 'NAMElon' |
---|
934 | # and 'NAMElat' are necessary. A NdxNd domain space size will be used. |
---|
935 | objfile.createDimension('lon2D',Nd) |
---|
936 | objfile.createDimension('lat2D',Nd) |
---|
937 | lonvals = ma.masked_equal(dvalues[dscn['NAMElon']], None) |
---|
938 | latvals = ma.masked_equal(dvalues[dscn['NAMElat']], None) |
---|
939 | |
---|
940 | minlon = min(lonvals) |
---|
941 | maxlon = max(lonvals) |
---|
942 | minlat = min(latvals) |
---|
943 | maxlat = max(latvals) |
---|
944 | |
---|
945 | blon = (maxlon - minlon)/(Nd-1) |
---|
946 | blat = (maxlat - minlat)/(Nd-1) |
---|
947 | |
---|
948 | newvar = onc.createVariable( 'lon2D', 'f8', ('lon2D')) |
---|
949 | basicvardef(newvar, 'longitude', 'longitude map observations','degrees_East') |
---|
950 | newvar[:] = minlon + np.arange(Nd)*blon |
---|
951 | newattr = set_attribute(newvar, 'axis', 'X') |
---|
952 | |
---|
953 | newvar = onc.createVariable( 'lat2D', 'f8', ('lat2D')) |
---|
954 | basicvardef(newvar, 'latitude', 'latitude map observations', 'degrees_North') |
---|
955 | newvar[:] = minlat + np.arange(Nd)*blat |
---|
956 | newattr = set_attribute(newvar, 'axis', 'Y') |
---|
957 | |
---|
958 | if dbg: |
---|
959 | print ' ' + fname + ': minlon=',minlon,'maxlon=',maxlon |
---|
960 | print ' ' + fname + ': minlat=',minlat,'maxlat=',maxlat |
---|
961 | print ' ' + fname + ': precission on x-axis=', blon*(Nd-1), 'y-axis=', \ |
---|
962 | blat*(Nd-1) |
---|
963 | |
---|
964 | if okind == 'multi-points': |
---|
965 | map2D = np.ones((Nd, Nd), dtype=np.float)*fillValueI |
---|
966 | |
---|
967 | dimt = len(tvals) |
---|
968 | Nlost = 0 |
---|
969 | for it in range(dimt): |
---|
970 | lon = dvalues[dscn['NAMElon']][it] |
---|
971 | lat = dvalues[dscn['NAMElat']][it] |
---|
972 | if lon is not None and lat is not None: |
---|
973 | ilon = int((Nd-1)*(lon - minlon)/(maxlon - minlon)) |
---|
974 | ilat = int((Nd-1)*(lat - minlat)/(maxlat - minlat)) |
---|
975 | |
---|
976 | if map2D[ilat,ilon] == fillValueI: |
---|
977 | map2D[ilat,ilon] = 1 |
---|
978 | else: |
---|
979 | map2D[ilat,ilon] = map2D[ilat,ilon] + 1 |
---|
980 | if dbg: print it, lon, lat, ilon, ilat, map2D[ilat,ilon] |
---|
981 | |
---|
982 | newvar = onc.createVariable( 'mapobs', 'f4', ('lat2D', 'lon2D'), \ |
---|
983 | fill_value = fillValueI) |
---|
984 | basicvardef(newvar, 'map_observations', 'number of observations', '-') |
---|
985 | newvar[:] = map2D |
---|
986 | newattr = set_attribute(newvar, 'coordinates', 'lon2D lat2D') |
---|
987 | |
---|
988 | elif okind == 'trajectory': |
---|
989 | # A new 2D map with the trajectory 'NAMElon' and 'NAMElat' and maybe 'NAMEheight' |
---|
990 | # are necessary. A NdxNdxNd domain space size will be used. Using time as |
---|
991 | # reference variable |
---|
992 | if dscn['NAMEheight'] == '-': |
---|
993 | # No height |
---|
994 | map2D = np.ones((Nd, Nd), dtype=np.float)*fillValueI |
---|
995 | |
---|
996 | dimt = len(tvals) |
---|
997 | Nlost = 0 |
---|
998 | if dbg: print ' time-step lon lat ix iy passes _______' |
---|
999 | for it in range(dimt): |
---|
1000 | lon = dvalues[dscn['NAMElon']][it] |
---|
1001 | lat = dvalues[dscn['NAMElat']][it] |
---|
1002 | if lon is not None and lat is not None: |
---|
1003 | ilon = int((Nd-1)*(lon - minlon)/(maxlon - minlon)) |
---|
1004 | ilat = int((Nd-1)*(lat - minlat)/(maxlat - minlat)) |
---|
1005 | |
---|
1006 | if map2D[ilat,ilon] == fillValueI: |
---|
1007 | map2D[ilat,ilon] = 1 |
---|
1008 | else: |
---|
1009 | map2D[ilat,ilon] = map2D[ilat,ilon] + 1 |
---|
1010 | if dbg: print it, lon, lat, ilon, ilat, map2D[ilat,ilon] |
---|
1011 | |
---|
1012 | newvar = onc.createVariable( 'trjobs', 'i', ('lat2D', 'lon2D'), \ |
---|
1013 | fill_value = fillValueI) |
---|
1014 | basicvardef(newvar, 'trajectory_observations', 'number of passes', '-' ) |
---|
1015 | newvar[:] = map2D |
---|
1016 | newattr = set_attribute(newvar, 'coordinates', 'lon2D lat2D') |
---|
1017 | |
---|
1018 | else: |
---|
1019 | ivn = 0 |
---|
1020 | for vn in dscn['varN']: |
---|
1021 | if vn == dscn['NAMEheight']: |
---|
1022 | zu = dscn['varU'][ivn] |
---|
1023 | break |
---|
1024 | ivn = ivn + 1 |
---|
1025 | |
---|
1026 | objfile.createDimension('z2D',Nd) |
---|
1027 | zvals = ma.masked_equal(dvalues[dscn['NAMEheight']], None) |
---|
1028 | minz = min(zvals) |
---|
1029 | maxz = max(zvals) |
---|
1030 | |
---|
1031 | bz = (maxz - minz)/(Nd-1) |
---|
1032 | |
---|
1033 | newvar = onc.createVariable( 'z2D', 'f8', ('z2D')) |
---|
1034 | basicvardef(newvar, 'z2D', 'z-coordinate map observations', zu) |
---|
1035 | newvar[:] = minz + np.arange(Nd)*bz |
---|
1036 | newattr = set_attribute(newvar, 'axis', 'Z') |
---|
1037 | |
---|
1038 | if dbg: |
---|
1039 | print ' ' + fname + ': zmin=',minz,zu,'zmax=',maxz,zu |
---|
1040 | print ' ' + fname + ': precission on z-axis=', bz*(Nd-1), zu |
---|
1041 | |
---|
1042 | map3D = np.ones((Nd, Nd, Nd), dtype=int)*fillValueI |
---|
1043 | dimt = len(tvals) |
---|
1044 | Nlost = 0 |
---|
1045 | if dbg: print ' time-step lon lat z ix iy iz passes _______' |
---|
1046 | for it in range(dimt): |
---|
1047 | lon = dvalues[dscn['NAMElon']][it] |
---|
1048 | lat = dvalues[dscn['NAMElat']][it] |
---|
1049 | z = dvalues[dscn['NAMEheight']][it] |
---|
1050 | if lon is not None and lat is not None and z is not None: |
---|
1051 | ilon = int((Nd-1)*(lon - minlon)/(maxlon - minlon)) |
---|
1052 | ilat = int((Nd-1)*(lat - minlat)/(maxlat - minlat)) |
---|
1053 | iz = int((Nd-1)*(z - minz)/(maxz - minz)) |
---|
1054 | |
---|
1055 | if map3D[iz,ilat,ilon] == fillValueI: |
---|
1056 | map3D[iz,ilat,ilon] = 1 |
---|
1057 | else: |
---|
1058 | map3D[iz,ilat,ilon] = map3D[iz,ilat,ilon] + 1 |
---|
1059 | if dbg: print it, lon, lat, z, ilon, ilat, iz, map3D[iz,ilat,ilon] |
---|
1060 | |
---|
1061 | newvar = onc.createVariable( 'trjobs', 'i', ('z2D', 'lat2D', 'lon2D'), \ |
---|
1062 | fill_value = fillValueI) |
---|
1063 | basicvardef(newvar, 'trajectory_observations', 'number of passes', '-') |
---|
1064 | newvar[:] = map3D |
---|
1065 | newattr = set_attribute(newvar, 'coordinates', 'lon2D lat2D z2D') |
---|
1066 | |
---|
1067 | onc.sync() |
---|
1068 | return |
---|
1069 | |
---|
1070 | def adding_station_desc(onc,stdesc): |
---|
1071 | """ Function to add a station description in a netCDF file |
---|
1072 | onc= netCDF object |
---|
1073 | stdesc= station description name, lon, lat, height |
---|
1074 | """ |
---|
1075 | fname = 'adding_station_desc' |
---|
1076 | |
---|
1077 | newdim = onc.createDimension('nst',1) |
---|
1078 | |
---|
1079 | newvar = objfile.createVariable( 'station', 'c', ('nst','StrLength')) |
---|
1080 | writing_str_nc(newvar, [stdesc[0].replace('!', ' ')], StringLength) |
---|
1081 | |
---|
1082 | newvar = objfile.createVariable( 'lonstGDM', 'c', ('nst','StrLength')) |
---|
1083 | Gv = int(stdesc[1]) |
---|
1084 | Dv = int((stdesc[1] - Gv)*60.) |
---|
1085 | Mv = int((stdesc[1] - Gv - Dv/60.)*3600.) |
---|
1086 | writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) |
---|
1087 | |
---|
1088 | if onc.variables.has_key('lon'): |
---|
1089 | print warnmsg |
---|
1090 | print ' ' + fname + ": variable 'lon' already exist !!" |
---|
1091 | print " renaming it as 'lonst'" |
---|
1092 | lonname = 'lonst' |
---|
1093 | else: |
---|
1094 | lonname = 'lon' |
---|
1095 | |
---|
1096 | newvar = objfile.createVariable( lonname, 'f4', ('nst')) |
---|
1097 | basicvardef(newvar, lonname, 'longitude', 'degrees_West' ) |
---|
1098 | newvar[:] = stdesc[1] |
---|
1099 | |
---|
1100 | newvar = objfile.createVariable( 'latstGDM', 'c', ('nst','StrLength')) |
---|
1101 | Gv = int(stdesc[2]) |
---|
1102 | Dv = int((stdesc[2] - Gv)*60.) |
---|
1103 | Mv = int((stdesc[2] - Gv - Dv/60.)*3600.) |
---|
1104 | writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) |
---|
1105 | |
---|
1106 | if onc.variables.has_key('lat'): |
---|
1107 | print warnmsg |
---|
1108 | print ' ' + fname + ": variable 'lat' already exist !!" |
---|
1109 | print " renaming it as 'latst'" |
---|
1110 | latname = 'latst' |
---|
1111 | else: |
---|
1112 | latname = 'lat' |
---|
1113 | |
---|
1114 | newvar = objfile.createVariable( latname, 'f4', ('nst')) |
---|
1115 | basicvardef(newvar, lonname, 'latitude', 'degrees_North' ) |
---|
1116 | newvar[:] = stdesc[2] |
---|
1117 | |
---|
1118 | if onc.variables.has_key('height'): |
---|
1119 | print warnmsg |
---|
1120 | print ' ' + fname + ": variable 'height' already exist !!" |
---|
1121 | print " renaming it as 'heightst'" |
---|
1122 | heightname = 'heightst' |
---|
1123 | else: |
---|
1124 | heightname = 'height' |
---|
1125 | |
---|
1126 | newvar = objfile.createVariable( heightname, 'f4', ('nst')) |
---|
1127 | basicvardef(newvar, heightname, 'height above sea level', 'm' ) |
---|
1128 | newvar[:] = stdesc[3] |
---|
1129 | |
---|
1130 | return |
---|
1131 | |
---|
1132 | def oper_values(dvals, opers): |
---|
1133 | """ Function to operate the values according to given parameters |
---|
1134 | dvals= datavalues |
---|
1135 | opers= operations |
---|
1136 | """ |
---|
1137 | fname = 'oper_values' |
---|
1138 | |
---|
1139 | newdvals = {} |
---|
1140 | varnames = dvals.keys() |
---|
1141 | |
---|
1142 | aopers = ['sumc','subc','mulc','divc'] |
---|
1143 | |
---|
1144 | Nopers = len(opers) |
---|
1145 | for iop in range(Nopers): |
---|
1146 | vn = varnames[iop] |
---|
1147 | print vn,'oper:',opers[iop] |
---|
1148 | if opers[iop] != '-': |
---|
1149 | opern = opers[iop].split(',')[0] |
---|
1150 | operv = np.float(opers[iop].split(',')[1]) |
---|
1151 | |
---|
1152 | vvals = np.array(dvals[vn]) |
---|
1153 | |
---|
1154 | if opern == 'sumc': |
---|
1155 | newvals = np.where(vvals is None, None, vvals+operv) |
---|
1156 | elif opern == 'subc': |
---|
1157 | newvals = np.where(vvals is None, None, vvals-operv) |
---|
1158 | elif opern == 'mulc': |
---|
1159 | newvals = np.where(vvals is None, None, vvals*operv) |
---|
1160 | elif opern == 'divc': |
---|
1161 | newvals = np.where(vvals is None, None, vvals/operv) |
---|
1162 | else: |
---|
1163 | print errormsg |
---|
1164 | print ' ' + fname + ": operation '" + opern + "' not ready!!" |
---|
1165 | print ' availables:',aopers |
---|
1166 | quit(-1) |
---|
1167 | |
---|
1168 | newdvals[vn] = list(newvals) |
---|
1169 | else: |
---|
1170 | newdvals[vn] = dvals[vn] |
---|
1171 | |
---|
1172 | return newdvals |
---|
1173 | |
---|
1174 | def WMOcodevar(unitsn, onc, Lstrcode=1024): |
---|
1175 | """ Function to add a variabe providing description of a units based on WMO code |
---|
1176 | unitsn= name of the units (all WMO codes derived units must be labelled as |
---|
1177 | 'wmo_code_[num]' associated to a file valled wmo_[num].code) |
---|
1178 | onc= netCDF object file to add the description |
---|
1179 | Lstrcode= length of description of codes |
---|
1180 | |
---|
1181 | wmo_[num].code must have the structure: ('#' for comments) |
---|
1182 | reference|web page, document reference with the code |
---|
1183 | short_description|main description of the code |
---|
1184 | long_description|long description of the code |
---|
1185 | codeTYPE|type of the code (andy of 'D', 'F', 'I', 'S') |
---|
1186 | @| Values line giving the start of the values |
---|
1187 | [val1]|[meaning of first value] |
---|
1188 | (...) |
---|
1189 | [valN]|[meaning of last value] |
---|
1190 | """ |
---|
1191 | fname = 'WMOcodevar' |
---|
1192 | |
---|
1193 | # From http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python |
---|
1194 | folder = os.path.dirname(os.path.realpath(__file__)) |
---|
1195 | |
---|
1196 | code = unitsn.split('_')[2] |
---|
1197 | |
---|
1198 | infile = folder + '/wmo_' + code +'.code' |
---|
1199 | |
---|
1200 | if not os.path.isfile(infile): |
---|
1201 | print warnmsg |
---|
1202 | print ' ' + fname + ": WMO code file '" + infile + "' does not exist !!" |
---|
1203 | return |
---|
1204 | # main expected values |
---|
1205 | descvals = ['wmo_code', 'reference', 'short_description', 'long_description', \ |
---|
1206 | 'codeTYPE', '@'] |
---|
1207 | |
---|
1208 | availcodetype = ['D', 'F', 'I', 'S'] |
---|
1209 | |
---|
1210 | codevalues = {} |
---|
1211 | ocode = open(infile, 'r') |
---|
1212 | inivals = False |
---|
1213 | codvals = [] |
---|
1214 | codmeanings = [] |
---|
1215 | for line in ocode: |
---|
1216 | if len(line) > 1 and line[0:1] != '#': |
---|
1217 | linev = line.replace('\n','').replace('\t',' ').replace('\r','') |
---|
1218 | if not inivals: |
---|
1219 | Sv = linev.split('|')[0] |
---|
1220 | Vv = linev.split('|')[1] |
---|
1221 | if searchInlist(descvals, Sv): |
---|
1222 | if Sv != '@': codevalues[Sv] = Vv |
---|
1223 | else: inivals = True |
---|
1224 | else: |
---|
1225 | Svv = linev.split('|')[0] |
---|
1226 | Vvv = linev.split('|')[1] |
---|
1227 | codvals.append(Svv) |
---|
1228 | codmeanings.append(Vvv) |
---|
1229 | |
---|
1230 | # Creating variable |
---|
1231 | if not searchInlist(onc.dimensions, 'Lstringcode'): |
---|
1232 | print ' ' + fname + ": Adding string length dimension 'Lstringcode' for " + \ |
---|
1233 | " code descriptions" |
---|
1234 | newdim = onc.createDimension('Lstringcode', Lstrcode) |
---|
1235 | Ncodes = len(codvals) |
---|
1236 | codedimn = 'wmo_code_' + str(code) |
---|
1237 | if not searchInlist(onc.dimensions, codedimn): |
---|
1238 | print ' ' + fname + ": Adding '" + codedimn + "' dimension for code " + \ |
---|
1239 | "description" |
---|
1240 | newdim = onc.createDimension(codedimn, Ncodes) |
---|
1241 | onc.sync() |
---|
1242 | |
---|
1243 | # Variable with the value of the code |
---|
1244 | if not onc.variables.has_key(codedimn): |
---|
1245 | if codevalues['codeTYPE'] == 'D': |
---|
1246 | newvar = onc.createVariable(codedimn, 'f8', (codedimn)) |
---|
1247 | for iv in range(Ncodes): newvar[iv] = np.float64(codvals[iv]) |
---|
1248 | elif codevalues['codeTYPE'] == 'F': |
---|
1249 | newvar = onc.createVariable(codedimn, 'f', (codedimn)) |
---|
1250 | for iv in range(Ncodes): newvar[iv] = np.float(codvals[iv]) |
---|
1251 | elif codevalues['codeTYPE'] == 'I': |
---|
1252 | newvar = onc.createVariable(codedimn, 'i', (codedimn)) |
---|
1253 | for iv in range(Ncodes): newvar[iv] = int(codvals[iv]) |
---|
1254 | elif codevalues['codeTYPE'] == 'S': |
---|
1255 | newvar = onc.createVariable(codedimn, 'c', (codedimn, 'Lstringcode')) |
---|
1256 | writing_str_nc(newvar, codvals, Lstrcode) |
---|
1257 | else: |
---|
1258 | print errormsg |
---|
1259 | print ' ' + fname + ": codeTYPE '" + codevalues['codeTYPE'] + "' not" + \ |
---|
1260 | " ready !!" |
---|
1261 | print ' available ones:', availcodetype |
---|
1262 | quit(-1) |
---|
1263 | |
---|
1264 | for descv in descvals: |
---|
1265 | if descv != '@' and descv != 'codeTYPE': |
---|
1266 | newvar.setncattr(descv, codevalues[descv]) |
---|
1267 | # Variable with the meaning of the code |
---|
1268 | if not onc.variables.has_key(codedimn+'_meaning'): |
---|
1269 | print ' '+fname+": Adding '" + codedimn + "_meaning' variable for code " + \ |
---|
1270 | "description" |
---|
1271 | newvar = onc.createVariable(codedimn+'_meaning','c',(codedimn,'Lstringcode')) |
---|
1272 | writing_str_nc(newvar, codmeanings, Lstrcode) |
---|
1273 | newvar.setncattr('description', 'meaning of WMO code ' + str(code)) |
---|
1274 | |
---|
1275 | onc.sync() |
---|
1276 | |
---|
1277 | return |
---|
1278 | |
---|
1279 | def EXTRAcodevar(unitsn, onc, Lstrcode=1024): |
---|
1280 | """ Function to add a variabe providing description of a units based on an extra |
---|
1281 | code |
---|
1282 | unitsn= name of the units (all codes derived units must be labelled as |
---|
1283 | 'extra_code_[ref]' associated to a file valled extra_[ref].code) |
---|
1284 | onc= netCDF object file to add the description |
---|
1285 | Lstrcode= length of description of codes |
---|
1286 | |
---|
1287 | extra_[ref].code must have the structure: ('#' for comments) |
---|
1288 | reference|web page, document reference with the code |
---|
1289 | short_description|main description of the code |
---|
1290 | long_description|long description of the code |
---|
1291 | codeTYPE|type of the code (andy of 'D', 'F', 'I', 'S') |
---|
1292 | @| Values line giving the start of the values |
---|
1293 | [val1]|[meaning of first value] |
---|
1294 | (...) |
---|
1295 | [valN]|[meaning of last value] |
---|
1296 | """ |
---|
1297 | fname = 'EXTRAcodevar' |
---|
1298 | |
---|
1299 | # From http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python |
---|
1300 | folder = os.path.dirname(os.path.realpath(__file__)) |
---|
1301 | |
---|
1302 | code = unitsn.split('_')[2] |
---|
1303 | |
---|
1304 | infile = folder + '/extra_' + code +'.code' |
---|
1305 | |
---|
1306 | if not os.path.isfile(infile): |
---|
1307 | print warnmsg |
---|
1308 | print ' ' + fname + ": EXTRA code file '" + infile + "' does not exist !!" |
---|
1309 | return |
---|
1310 | # main expected values |
---|
1311 | descvals = ['reference', 'short_description', 'long_description', \ |
---|
1312 | 'codeTYPE', '@'] |
---|
1313 | |
---|
1314 | availcodetype = ['D', 'F', 'I', 'S'] |
---|
1315 | |
---|
1316 | codevalues = {} |
---|
1317 | ocode = open(infile, 'r') |
---|
1318 | inivals = False |
---|
1319 | codvals = [] |
---|
1320 | codmeanings = [] |
---|
1321 | for line in ocode: |
---|
1322 | if len(line) > 1 and line[0:1] != '#': |
---|
1323 | linev = line.replace('\n','').replace('\t',' ').replace('\r','') |
---|
1324 | if not inivals: |
---|
1325 | Sv = linev.split('|')[0] |
---|
1326 | Vv = linev.split('|')[1] |
---|
1327 | if searchInlist(descvals, Sv): |
---|
1328 | if Sv != '@': codevalues[Sv] = Vv |
---|
1329 | else: inivals = True |
---|
1330 | else: |
---|
1331 | Svv = linev.split('|')[0] |
---|
1332 | Vvv = linev.split('|')[1] |
---|
1333 | codvals.append(Svv) |
---|
1334 | codmeanings.append(Vvv) |
---|
1335 | |
---|
1336 | # Creating variable |
---|
1337 | if not searchInlist(onc.dimensions, 'Lstringcode'): |
---|
1338 | print ' ' + fname + ": Adding string length dimension 'Lstringcode' for " + \ |
---|
1339 | " code descriptions" |
---|
1340 | newdim = onc.createDimension('Lstringcode', Lstrcode) |
---|
1341 | Ncodes = len(codvals) |
---|
1342 | codedimn = 'extra_code_' + str(code) |
---|
1343 | if not searchInlist(onc.dimensions, codedimn): |
---|
1344 | print ' ' + fname + ": Adding '" + codedimn + "' dimension for code " + \ |
---|
1345 | "description" |
---|
1346 | newdim = onc.createDimension(codedimn, Ncodes) |
---|
1347 | onc.sync() |
---|
1348 | |
---|
1349 | # Variable with the value of the code |
---|
1350 | if not onc.variables.has_key(codedimn): |
---|
1351 | if codevalues['codeTYPE'] == 'D': |
---|
1352 | newvar = onc.createVariable(codedimn, 'f8', (codedimn)) |
---|
1353 | for iv in range(Ncodes): newvar[iv] = np.float64(codvals[iv]) |
---|
1354 | elif codevalues['codeTYPE'] == 'F': |
---|
1355 | newvar = onc.createVariable(codedimn, 'f', (codedimn)) |
---|
1356 | for iv in range(Ncodes): newvar[iv] = np.float(codvals[iv]) |
---|
1357 | elif codevalues['codeTYPE'] == 'I': |
---|
1358 | newvar = onc.createVariable(codedimn, 'i', (codedimn)) |
---|
1359 | for iv in range(Ncodes): newvar[iv] = int(codvals[iv]) |
---|
1360 | elif codevalues['codeTYPE'] == 'S': |
---|
1361 | newvar = onc.createVariable(codedimn, 'c', (codedimn, 'Lstringcode')) |
---|
1362 | writing_str_nc(newvar, codvals, Lstrcode) |
---|
1363 | else: |
---|
1364 | print errormsg |
---|
1365 | print ' ' + fname + ": codeTYPE '" + codevalues['codeTYPE'] + "' not" + \ |
---|
1366 | " ready !!" |
---|
1367 | print ' available ones:', availcodetype |
---|
1368 | quit(-1) |
---|
1369 | |
---|
1370 | for descv in descvals: |
---|
1371 | if descv != '@' and descv != 'codeTYPE': |
---|
1372 | newvar.setncattr(descv, codevalues[descv]) |
---|
1373 | # Variable with the meaning of the code |
---|
1374 | if not onc.variables.has_key(codedimn+'_meaning'): |
---|
1375 | print ' '+fname+": Adding '" + codedimn + "_meaning' variable for code " + \ |
---|
1376 | "description" |
---|
1377 | newvar = onc.createVariable(codedimn+'_meaning','c',(codedimn,'Lstringcode')) |
---|
1378 | writing_str_nc(newvar, codmeanings, Lstrcode) |
---|
1379 | newvar.setncattr('description', 'meaning of EXTRA code ' + str(code)) |
---|
1380 | |
---|
1381 | onc.sync() |
---|
1382 | |
---|
1383 | return |
---|
1384 | |
---|
1385 | def add_global_PyNCplot(ObjFile, pyscript, funcname, version): |
---|
1386 | """ Function to add global attributes from 'PyNCplot' to a given netCDF |
---|
1387 | ObjFile= netCDF file object to which add the global attributes |
---|
1388 | funcname= name of the function from which file comes from |
---|
1389 | version= version of the function |
---|
1390 | """ |
---|
1391 | fname = 'add_global_PyNCplot' |
---|
1392 | |
---|
1393 | # Global values |
---|
1394 | ObjFile.setncattr('file_creation', '--------') |
---|
1395 | ObjFile.setncattr('file_author', 'L. Fita') |
---|
1396 | newattr = set_attributek(ObjFile, 'file_a_institution', unicode('Centro de ' + \ |
---|
1397 | 'Investigaciones del Mar y la Atm' + unichr(243) + 'sfera (CIMA)'), 'U') |
---|
1398 | newattr = set_attributek(ObjFile, 'file_a_institution2', unicode('Instituto ' + \ |
---|
1399 | 'Franco-Argentino sobre Estudios de Clima y sus Impactos (CNRS, UMI-3351-' + \ |
---|
1400 | 'IFAECI'), 'U') |
---|
1401 | newattr = set_attributek(ObjFile, 'center', unicode('Consejo Nacional de ' + \ |
---|
1402 | 'Investigaciones Cient' + unichr(237) + 'ficas y T' + unichr(233) + \ |
---|
1403 | 'cnicas (CONICET)'), 'U') |
---|
1404 | ObjFile.setncattr('university', 'Universidad de Buenos Aires (UBA)') |
---|
1405 | ObjFile.setncattr('city', 'C. A. Buenos Aires') |
---|
1406 | ObjFile.setncattr('country', 'Argentina') |
---|
1407 | ObjFile.setncattr('tool', 'PyNCplot') |
---|
1408 | ObjFile.setncattr('url', 'http://www.xn--llusfb-5va.cat/python/PyNCplot') |
---|
1409 | ObjFile.setncattr('script', pyscript) |
---|
1410 | if funcname is not None: |
---|
1411 | ObjFile.setncattr('function', funcname) |
---|
1412 | ObjFile.setncattr('version', version) |
---|
1413 | |
---|
1414 | ObjFile.sync() |
---|
1415 | |
---|
1416 | return |
---|
1417 | |
---|
1418 | ####### ###### ##### #### ### ## # |
---|
1419 | |
---|
1420 | strCFt="Refdate,tunits (CF reference date [YYYY][MM][DD][HH][MI][SS] format and " + \ |
---|
1421 | " and time units: 'weeks', 'days', 'hours', 'miuntes', 'seconds')" |
---|
1422 | |
---|
1423 | kindobs=['stations-map','multi-points', 'single-station', 'trajectory'] |
---|
1424 | strkObs="kind of observations; 'multi-points': multiple individual punctual obs " + \ |
---|
1425 | "(e.g., lightning strikes), 'single-station': single station on a fixed position,"+\ |
---|
1426 | "'trajectory': following a trajectory" |
---|
1427 | |
---|
1428 | parser = OptionParser() |
---|
1429 | parser.add_option("-c", "--comments", dest="charcom", |
---|
1430 | help="':', list of characters used for comments", metavar="VALUES") |
---|
1431 | parser.add_option("-d", "--descriptionfile", dest="fdesc", |
---|
1432 | help="description file to use" + read_description.__doc__, metavar="FILE") |
---|
1433 | parser.add_option("-e", "--end_column", dest="endcol", |
---|
1434 | help="character to indicate end of the column ('space', for ' ', or 'None,[fixcoljumps]' for fixed size columns with [fixcoljumps]: ',' separated list of positions of column ends within line)", metavar="VALUE") |
---|
1435 | parser.add_option("-f", "--file", dest="obsfile", |
---|
1436 | help="observational file to use", metavar="FILE") |
---|
1437 | parser.add_option("-j", "--jumpBlines", dest="jumpBlines", |
---|
1438 | help="number of lines to jump from the beggining of file", metavar="VALUE") |
---|
1439 | parser.add_option("-g", "--debug", dest="debug", |
---|
1440 | help="whether debug is required ('false', 'true')", metavar="VALUE") |
---|
1441 | parser.add_option("-k", "--kindObs", dest="obskind", type='choice', choices=kindobs, |
---|
1442 | help=strkObs, metavar="VALUE") |
---|
1443 | parser.add_option("-s", "--stationLocation", dest="stloc", |
---|
1444 | help="name ('!' for spaces), longitude, latitude and height of the station (only for 'single-station')", |
---|
1445 | metavar="FILE") |
---|
1446 | parser.add_option("-t", "--CFtime", dest="CFtime", help=strCFt, metavar="VALUE") |
---|
1447 | |
---|
1448 | (opts, args) = parser.parse_args() |
---|
1449 | |
---|
1450 | ####### ####### |
---|
1451 | ## MAIN |
---|
1452 | ####### |
---|
1453 | |
---|
1454 | ofile='OBSnetcdf.nc' |
---|
1455 | |
---|
1456 | if opts.charcom is None: |
---|
1457 | print warnmsg |
---|
1458 | print ' ' + main + ': No list of comment characters provided!!' |
---|
1459 | print ' assuming no need!' |
---|
1460 | charcomments = [] |
---|
1461 | else: |
---|
1462 | charcomments = opts.charcom.split(':') |
---|
1463 | |
---|
1464 | if opts.jumpBlines is None: |
---|
1465 | print warnmsg |
---|
1466 | print ' ' + main + ': No number of lines to jump from beggining of file provided!!' |
---|
1467 | print ' assuming no need!' |
---|
1468 | jumpBlines = 0 |
---|
1469 | else: |
---|
1470 | jumpBlines = int(opts.jumpBlines) |
---|
1471 | |
---|
1472 | |
---|
1473 | if opts.fdesc is None: |
---|
1474 | print errormsg |
---|
1475 | print ' ' + main + ': No description file for the observtional data provided!!' |
---|
1476 | quit(-1) |
---|
1477 | |
---|
1478 | if opts.endcol is None: |
---|
1479 | print warnmsg |
---|
1480 | print ' ' + main + ': No list of comment characters provided!!' |
---|
1481 | print " assuming 'space'" |
---|
1482 | endcol = ' ' |
---|
1483 | else: |
---|
1484 | if opts.endcol == 'space': |
---|
1485 | endcol = ' ' |
---|
1486 | else: |
---|
1487 | endcol = opts.endcol |
---|
1488 | |
---|
1489 | if opts.obsfile is None: |
---|
1490 | print errormsg |
---|
1491 | print ' ' + main + ': No observations file provided!!' |
---|
1492 | quit(-1) |
---|
1493 | |
---|
1494 | if opts.debug is None: |
---|
1495 | print warnmsg |
---|
1496 | print ' ' + main + ': No debug provided!!' |
---|
1497 | print " assuming 'False'" |
---|
1498 | debug = False |
---|
1499 | else: |
---|
1500 | if opts.debug == 'true': |
---|
1501 | debug = True |
---|
1502 | else: |
---|
1503 | debug = False |
---|
1504 | |
---|
1505 | if not os.path.isfile(opts.fdesc): |
---|
1506 | print errormsg |
---|
1507 | print ' ' + main + ": description file '" + opts.fdesc + "' does not exist !!" |
---|
1508 | quit(-1) |
---|
1509 | |
---|
1510 | if not os.path.isfile(opts.obsfile): |
---|
1511 | print errormsg |
---|
1512 | print ' ' + main + ": observational file '" + opts.obsfile + "' does not exist !!" |
---|
1513 | quit(-1) |
---|
1514 | |
---|
1515 | if opts.CFtime is None: |
---|
1516 | print warnmsg |
---|
1517 | print ' ' + main + ': No CFtime criteria are provided !!' |
---|
1518 | print " either time is already in CF-format ('timeFMT=CFtime') in '" + \ |
---|
1519 | opts.fdesc + "'" |
---|
1520 | print " or assuming refdate: '19491201000000' and time units: 'hours'" |
---|
1521 | referencedate = '19491201000000' |
---|
1522 | timeunits = 'hours' |
---|
1523 | else: |
---|
1524 | referencedate = opts.CFtime.split(',')[0] |
---|
1525 | timeunits = opts.CFtime.split(',')[1] |
---|
1526 | |
---|
1527 | if opts.obskind is None: |
---|
1528 | print warnmsg |
---|
1529 | print ' ' + main + ': No kind of observations provided !!' |
---|
1530 | print " assuming 'single-station': single station on a fixed position at 0,0,0" |
---|
1531 | obskind = 'single-station' |
---|
1532 | stationdesc = [0.0, 0.0, 0.0, 0.0] |
---|
1533 | else: |
---|
1534 | obskind = opts.obskind |
---|
1535 | if obskind == 'single-station': |
---|
1536 | if opts.stloc is None: |
---|
1537 | print errormsg |
---|
1538 | print ' ' + main + ': No station location provided !!' |
---|
1539 | quit(-1) |
---|
1540 | else: |
---|
1541 | stvals = opts.stloc.split(',') |
---|
1542 | stationdesc = [stvals[0], np.float(stvals[1]), np.float(stvals[2]), \ |
---|
1543 | np.float(stvals[3])] |
---|
1544 | else: |
---|
1545 | obskind = opts.obskind |
---|
1546 | |
---|
1547 | # Reading description file |
---|
1548 | ## |
---|
1549 | description = read_description(opts.fdesc, debug) |
---|
1550 | |
---|
1551 | Nvariables=len(description['varN']) |
---|
1552 | NlongN=len(description['varLN']) |
---|
1553 | NvarU=len(description['varU']) |
---|
1554 | formats = description['varTYPE'] |
---|
1555 | |
---|
1556 | if Nvariables != NlongN: |
---|
1557 | print errormsg |
---|
1558 | print ' ' + main + ': number of variables:', Nvariables,' and number of ' + \ |
---|
1559 | 'long names', NlongN,' does not coincide!!' |
---|
1560 | print ' * what is found is _______' |
---|
1561 | if Nvariables > NlongN: |
---|
1562 | Nshow = NlongN |
---|
1563 | for ivar in range(Nshow): |
---|
1564 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1565 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1566 | print ' missing values for:', description['varN'][Nshow:Nvariables+1] |
---|
1567 | else: |
---|
1568 | Nshow = Nvariables |
---|
1569 | for ivar in range(Nshow): |
---|
1570 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1571 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1572 | print ' excess of long names for :', description['varLN'][Nshow:NlongN+1] |
---|
1573 | |
---|
1574 | quit(-1) |
---|
1575 | |
---|
1576 | if Nvariables != NvarU: |
---|
1577 | print errormsg |
---|
1578 | print ' ' + main + ': number of variables:', Nvariables,' and number of ' + \ |
---|
1579 | 'units', NvarU,' does not coincide!!' |
---|
1580 | print ' * what is found is _______' |
---|
1581 | if Nvariables > NvarU: |
---|
1582 | Nshow = NvarU |
---|
1583 | for ivar in range(Nshow): |
---|
1584 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1585 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1586 | print ' missing values for:', description['varN'][Nshow:Nvariables+1] |
---|
1587 | else: |
---|
1588 | Nshow = Nvariables |
---|
1589 | for ivar in range(Nshow): |
---|
1590 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1591 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1592 | print ' excess of units for :', description['varU'][Nshow:NvarU+1] |
---|
1593 | |
---|
1594 | quit(-1) |
---|
1595 | |
---|
1596 | print 'Number of variables', Nvariables |
---|
1597 | print 'Number of long names', len(description['varLN']) |
---|
1598 | |
---|
1599 | if len(formats) != Nvariables: |
---|
1600 | print errormsg |
---|
1601 | print ' ' + main + ': number of formats:',len(formats),' and number of ' + \ |
---|
1602 | 'variables', Nvariables,' does not coincide!!' |
---|
1603 | print ' * what is found is _______' |
---|
1604 | if Nvariables > len(formats): |
---|
1605 | Nshow = len(formats) |
---|
1606 | for ivar in range(Nshow): |
---|
1607 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1608 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1609 | print ' missing values for:', description['varN'][Nshow:Nvariables+1] |
---|
1610 | else: |
---|
1611 | Nshow = Nvariables |
---|
1612 | for ivar in range(Nshow): |
---|
1613 | print ivar,' ',description['varN'][ivar],':', description['varLN'][ivar],\ |
---|
1614 | '[', description['varU'][ivar], '] fmt:', formats[ivar] |
---|
1615 | print ' excess of formats for:', formats[Nshow:len(formats)+1] |
---|
1616 | |
---|
1617 | # quit(-1) |
---|
1618 | |
---|
1619 | # Reading values |
---|
1620 | ## |
---|
1621 | datavalues = read_datavalues(opts.obsfile, charcomments, endcol, formats, jumpBlines, |
---|
1622 | description['varOPER'], description['MissingValue'], description['varN'], debug) |
---|
1623 | |
---|
1624 | # Total number of values |
---|
1625 | Ntvalues = len(datavalues[description['varN'][0]]) |
---|
1626 | if obskind == 'stations-map': |
---|
1627 | print main + ': total values found:',Ntvalues |
---|
1628 | else: |
---|
1629 | print main + ': total temporal values found:',Ntvalues |
---|
1630 | |
---|
1631 | objfile = NetCDFFile(ofile, 'w') |
---|
1632 | |
---|
1633 | # Creation of dimensions |
---|
1634 | ## |
---|
1635 | if obskind == 'stations-map': |
---|
1636 | rowsdim = 'Npoints' |
---|
1637 | dimlength = Ntvalues |
---|
1638 | else: |
---|
1639 | rowsdim = 'time' |
---|
1640 | dimlength = None |
---|
1641 | |
---|
1642 | objfile.createDimension(rowsdim,dimlength) |
---|
1643 | objfile.createDimension('StrLength',StringLength) |
---|
1644 | |
---|
1645 | # Creation of variables |
---|
1646 | ## |
---|
1647 | for ivar in range(Nvariables): |
---|
1648 | varn = description['varN'][ivar] |
---|
1649 | print " including: '" + varn + "' ... .. ." |
---|
1650 | |
---|
1651 | if formats[ivar] == 'D': |
---|
1652 | newvar = objfile.createVariable(varn, 'f32', (rowsdim), fill_value=fillValueF) |
---|
1653 | basicvardef(newvar, varn, description['varLN'][ivar], \ |
---|
1654 | description['varU'][ivar]) |
---|
1655 | newvar[:] = np.where(datavalues[varn] is None, fillValueF, datavalues[varn]) |
---|
1656 | elif formats[ivar] == 'F': |
---|
1657 | newvar = objfile.createVariable(varn, 'f', (rowsdim), fill_value=fillValueF) |
---|
1658 | basicvardef(newvar, varn, description['varLN'][ivar], \ |
---|
1659 | description['varU'][ivar]) |
---|
1660 | newvar[:] = np.where(datavalues[varn] is None, fillValueF, datavalues[varn]) |
---|
1661 | elif formats[ivar] == 'I': |
---|
1662 | newvar = objfile.createVariable(varn, 'i', (rowsdim), fill_value=fillValueI) |
---|
1663 | basicvardef(newvar, varn, description['varLN'][ivar], \ |
---|
1664 | description['varU'][ivar]) |
---|
1665 | # Why is not wotking with integers? |
---|
1666 | vals = np.array(datavalues[varn]) |
---|
1667 | for iv in range(Ntvalues): |
---|
1668 | if vals[iv] is None: vals[iv] = fillValueI |
---|
1669 | newvar[:] = vals |
---|
1670 | elif formats[ivar] == 'S': |
---|
1671 | newvar = objfile.createVariable(varn, 'c', (rowsdim,'StrLength')) |
---|
1672 | basicvardef(newvar, varn, description['varLN'][ivar], \ |
---|
1673 | description['varU'][ivar]) |
---|
1674 | vals = datavalues[varn] |
---|
1675 | for iv in range(Ntvalues): |
---|
1676 | if vals[iv] is None: vals[iv] = fillValueS |
---|
1677 | writing_str_nc(newvar, vals, StringLength) |
---|
1678 | |
---|
1679 | # Getting new variables to describe certain units as codeWMO_[num] from an |
---|
1680 | # external file |
---|
1681 | if description['varU'][ivar][0:8] == 'wmo_code': |
---|
1682 | WMOcodevar(description['varU'][ivar], objfile) |
---|
1683 | # Getting new variables to describe certain units as codeEXTRA_[ref] from an |
---|
1684 | # external file |
---|
1685 | if description['varU'][ivar][0:10] == 'extra_code': |
---|
1686 | EXTRAcodevar(description['varU'][ivar], objfile) |
---|
1687 | |
---|
1688 | # Extra variable descriptions/attributes |
---|
1689 | if description.has_key('varBUFR'): |
---|
1690 | set_attribute(newvar,'bufr_code',description['varBUFR'][ivar]) |
---|
1691 | |
---|
1692 | objfile.sync() |
---|
1693 | |
---|
1694 | # Time variable in CF format |
---|
1695 | ## |
---|
1696 | if description['FMTtime'] == 'CFtime': |
---|
1697 | timevals = datavalues[description['NAMEtime']] |
---|
1698 | iv = 0 |
---|
1699 | for ivn in description['varN']: |
---|
1700 | if ivn == description['NAMEtime']: |
---|
1701 | tunits = description['varU'][iv] |
---|
1702 | break |
---|
1703 | iv = iv + 1 |
---|
1704 | else: |
---|
1705 | # Time as a composition of different columns |
---|
1706 | tcomposite = description['NAMEtime'].find('@') |
---|
1707 | if tcomposite != -1: |
---|
1708 | timevars = description['NAMEtime'].split('@') |
---|
1709 | |
---|
1710 | print warnmsg |
---|
1711 | print ' ' + main + ': time values as combination of different columns!' |
---|
1712 | print ' combining:',timevars,' with a final format: ',description['FMTtime'] |
---|
1713 | |
---|
1714 | timeSvals = [] |
---|
1715 | if debug: print ' ' + main + ': creating times _______' |
---|
1716 | for it in range(Ntvalues): |
---|
1717 | tSvals = '' |
---|
1718 | for tvar in timevars: |
---|
1719 | tSvals = tSvals + datavalues[tvar][it] + ' ' |
---|
1720 | |
---|
1721 | timeSvals.append(tSvals[0:len(tSvals)-1]) |
---|
1722 | if debug: print it, '*' + timeSvals[it] + '*' |
---|
1723 | |
---|
1724 | timevals = Stringtimes_CF(timeSvals, description['FMTtime'].replace('@',' '),\ |
---|
1725 | referencedate, timeunits, debug) |
---|
1726 | else: |
---|
1727 | timevals = Stringtimes_CF(datavalues[description['NAMEtime']], \ |
---|
1728 | description['FMTtime'], referencedate, timeunits, debug) |
---|
1729 | |
---|
1730 | CFtimeRef = referencedate[0:4] +'-'+ referencedate[4:6] +'-'+ referencedate[6:8] + \ |
---|
1731 | ' ' + referencedate[8:10] +':'+ referencedate[10:12] +':'+ referencedate[12:14] |
---|
1732 | tunits = timeunits + ' since ' + CFtimeRef |
---|
1733 | |
---|
1734 | if objfile.variables.has_key('time'): |
---|
1735 | print warnmsg |
---|
1736 | print ' ' + main + ": variable 'time' already exist !!" |
---|
1737 | print " renaming it as 'CFtime'" |
---|
1738 | timeCFname = 'CFtime' |
---|
1739 | newdim = objfile.renameDimension('time','CFtime') |
---|
1740 | newvar = objfile.createVariable( timeCFname, 'f8', ('CFtime')) |
---|
1741 | basicvardef(newvar, timeCFname, 'time', tunits ) |
---|
1742 | else: |
---|
1743 | if not searchInlist(objfile.dimensions, 'time'): |
---|
1744 | newdim = objfile.createDimension('time',None) |
---|
1745 | timeCFname = 'time' |
---|
1746 | newvar = objfile.createVariable( timeCFname, 'f8', ('time')) |
---|
1747 | newvar[:] = np.zeros(timevals.shape[0]) |
---|
1748 | basicvardef(newvar, timeCFname, 'time', tunits ) |
---|
1749 | |
---|
1750 | set_attribute(newvar, 'calendar', 'standard') |
---|
1751 | if obskind == 'stations-map': |
---|
1752 | newvar[:] = timevals[0] |
---|
1753 | else: |
---|
1754 | newvar[:] = timevals |
---|
1755 | |
---|
1756 | # Global attributes |
---|
1757 | ## |
---|
1758 | for descn in description.keys(): |
---|
1759 | if descn[0:3] != 'var' and descn[0:4] != 'NAME' and descn[0:3] != 'FMT': |
---|
1760 | set_attribute(objfile, descn, description[descn]) |
---|
1761 | |
---|
1762 | add_global_PyNCplot(objfile, main, 'main', version) |
---|
1763 | |
---|
1764 | objfile.sync() |
---|
1765 | |
---|
1766 | # Adding new variables as function of the observational type |
---|
1767 | ## 'multi-points', 'single-station', 'trajectory' |
---|
1768 | |
---|
1769 | if obskind != 'single-station': |
---|
1770 | adding_complementary(objfile, description, obskind, datavalues, timevals, \ |
---|
1771 | referencedate, timeunits, Ndim2D, debug) |
---|
1772 | else: |
---|
1773 | # Adding three variables with the station location, longitude, latitude and height |
---|
1774 | adding_station_desc(objfile,stationdesc) |
---|
1775 | |
---|
1776 | objfile.sync() |
---|
1777 | objfile.close() |
---|
1778 | |
---|
1779 | print main + ": Successfull generation of netcdf observational file '" + ofile + "' !!" |
---|