source: lmdz_wrf/trunk/tools/create_OBSnetcdf.py @ 2684

Last change on this file since 2684 was 2684, checked in by lfita, 6 years ago

Adding

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