Index: trunk/tools/TS_ASCII_netCDF.py
===================================================================
--- trunk/tools/TS_ASCII_netCDF.py	(revision 927)
+++ trunk/tools/TS_ASCII_netCDF.py	(revision 928)
@@ -6,5 +6,5 @@
 import numpy as np
 from netCDF4 import Dataset as NetCDFFile
-import nc_var_tools as ncvar
+import re
 
 main = 'TS_ASCII_netCDF.py'
@@ -13,4 +13,151 @@
 
 fillValue = 1.e20
+
+def searchInlist(listname, nameFind):
+    """ Function to search a value within a list
+    listname = list
+    nameFind = value to find
+    >>> searInlist(['1', '2', '3', '5'], '5')
+    True
+    """
+    for x in listname:
+      if x == nameFind:
+        return True
+    return False
+
+def reduce_spaces(string):
+    """ Function to give words of a line of text removing any extra space
+    """
+    values = string.replace('\n','').split(' ')
+    vals = []
+    for val in values:
+         if len(val) > 0:
+             vals.append(val)
+
+    return vals
+
+def reduce_last_spaces(string):
+    """ Function to reduce the last right spaces from a string
+      string= string to remove the spaces at the righ side of the string
+    >>> reduce_last_spaces('Hola Lluis      ')
+    'Hola Lluis'
+    """
+    fname = 'reduce_last_spaces'
+
+    Lstring = len(string)
+
+    firstspace = -1
+    for istr in range(Lstring-1,0,-1):
+        if string[istr:istr+1] == ' ':
+            firstspace = istr
+        else:
+            break
+
+    if firstspace == -1:
+        print errormsg
+        print '  ' + fname + ": string '" + string + "' is not ended by spaces!!"
+        print '    char. number of last character:',ord(string[Lstring:Lstring+1])
+        quit(-1)
+
+    newstr = string[0:firstspace]
+
+    return newstr
+
+def set_attribute(ncv, attrname, attrvalue):
+    """ Sets a value of an attribute of a netCDF variable. Removes previous attribute value if exists
+    ncv = object netcdf variable
+    attrname = name of the attribute
+    attrvalue = value of the attribute
+    """
+
+    attvar = ncv.ncattrs()
+    if searchInlist(attvar, attrname):
+        attr = ncv.delncattr(attrname)
+
+    attr = ncv.setncattr(attrname, attrvalue)
+
+    return attr
+
+def rmNOnum(string):
+    """ Removing from a string all that characters which are not numbers
+    # From: http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string
+    """
+    fname = 'rmNOnum'
+
+    newstr = str(re.findall("[-+]?\d+[\.]?\d*", string)[0])
+
+    return newstr
+
+def basicvardef(varobj, vstname, vlname, vunits):
+    """ Function to give the basic attributes to a variable
+    varobj= netCDF variable object
+    vstname= standard name of the variable
+    vlname= long name of the variable
+    vunits= units of the variable
+    """
+    attr = varobj.setncattr('standard_name', vstname)
+    attr = varobj.setncattr('long_name', vlname)
+    attr = varobj.setncattr('units', vunits)
+
+    return
+
+def values_fortran_fmt(lin,fFMT):
+    """ Function to give back the values of an ASCII line according to its fortran printing format
+      lin= ASCII line
+      fFMT= list with the fortran FORMAT formats
+    forline = 'Natchitoches (RGNL)        1 11 0011  ( 31.733, -93.100) (  28, 157) ( 31.761, -93.113)   41.2 meters'
+    formats = ['A26', 'I2', 'I3', 'A6', 'A2', 'F7.3', 'A1', 'F8.3', 'A3', 'I4', 'A1', 'I4',
+      'A3', 'F7.3', 'A1', 'F8.3', 'A2', 'F6.1', 'A7']
+    >>> values_fortran_fmt(forline, formats)
+    ['Natchitoches (RGNL)        ', 1, 11, ' 0011  ', ' ( ', 31.733, ', ', -93.1, ') ( ', 28, ', ', 157, ')
+      ( ', 31.761, ', ', -93.113, ')  ', 41.2, ' meters']
+    """
+    fname = 'values_fortran_fmt'
+
+    afmts = ['A', 'D', 'F', 'I']
+
+    if lin == 'h':
+        print fname + '_____________________________________________________________'
+        print values_fortran_fmt.__doc__
+        quit()
+
+    fvalues = []
+    ichar=0
+    ival = 0
+    for ft in fFMT:
+        Lft = len(ft)
+        if ft[0:1] == 'A' or ft[0:1] == 'a':
+            Lfmt = int(ft[1:Lft+1])
+            fvalues.append(lin[ichar:ichar+Lfmt+1])
+            ichar = ichar + Lfmt
+        elif ft[0:1] == 'F' or ft[0:1] == 'f':
+            if ft.find('.') != -1:
+                Lft = len(ft.split('.')[0])
+            Lfmt = int(ft[1:Lft])
+            fvalues.append(np.float(rmNOnum(lin[ichar:ichar+Lfmt+1])))
+            ichar = ichar + Lfmt
+        elif ft[0:1] == 'D' or ft[0:1] == 'd':
+            if ft.find('.') != -1:
+                Lft = len(ft.split('.')[0])
+            Lfmt = int(ft[1:Lft])
+            fvalues.append(np.float64(rmNOnum(lin[ichar:ichar+Lfmt+1])))
+            ichar = ichar + Lfmt
+        elif ft[0:1] == 'I' or ft[0:1] == 'i':
+            Lfmt = int(ft[1:Lft+1])
+            fvalues.append(int(rmNOnum(lin[ichar:ichar+Lfmt+1])))
+            ichar = ichar + Lfmt
+        elif ft.find('X') != -1 or ft.find('x') != -1:
+            print '    ' + fname + ': skipping space'
+            ichar = ichar + int(rmNOnum(ft))
+        else:
+            print errormsg
+            print '  ' + fname + ": format '" + ft[0:1] + "' not ready!!"
+            print '    Available formats:',afmts
+            quit(-1)
+
+        ival = ival + 1
+
+    return fvalues
+
 
 def ts_header(ln):
@@ -24,7 +171,96 @@
       'A3', 'F7.3', 'A1', 'F8.3', 'A2', 'F6.1', 'A7']
 
-    headervalues = ncvar.values_fortran_fmt(ln,fmts)
+    headervalues = values_fortran_fmt(ln,fmts)
 
     return headervalues
+
+def variables_values(varName):
+    """ Function to provide values to plot the different variables values from ASCII file
+      'variables_values.dat'
+    variables_values(varName)
+      [varName]= name of the variable
+        return: [var name], [std name], [minimum], [maximum], 
+          [long name]('|' for spaces), [units], [color palette] (following: 
+          http://matplotlib.org/1.3.1/examples/color/colormaps_reference.html)
+     [varn]: original name of the variable
+       NOTE: It might be better doing it with an external ASII file. But then we 
+         got an extra dependency...
+    >>> variables_values('WRFght')
+    ['z', 'geopotential_height', 0.0, 80000.0, 'geopotential|height', 'm2s-2', 'rainbow']
+    """
+    import subprocess as sub
+
+    fname='variables_values'
+
+    if varName == 'h':
+        print fname + '_____________________________________________________________'
+        print variables_values.__doc__
+        quit()
+
+# This does not work....
+#    folderins = sub.Popen(["pwd"], stdout=sub.PIPE)
+#    folder = list(folderins.communicate())[0].replace('\n','')
+# From http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python
+    folder = os.path.dirname(os.path.realpath(__file__))
+
+    infile = folder + '/variables_values.dat'
+
+    if not os.path.isfile(infile):
+        print errormsg
+        print '  ' + fname + ": File '" + infile + "' does not exist !!"
+        quit(-1)
+
+# Variable name might come with a statistical surname...
+    stats=['min','max','mean','stdv', 'sum']
+
+# Variables with a statistical section on their name...
+    NOstatsvars = ['zmaxth', 'zmax_th', 'lmax_th', 'lmaxth']
+
+    ifst = False
+    if not searchInlist(NOstatsvars, varName.lower()):
+        for st in stats:
+            if varName.find(st) > -1:
+                print '    '+ fname + ": varibale '" + varName + "' with a " +       \
+                  "statistical surname: '",st,"' !!"
+                Lst = len(st)
+                LvarName = len(varName)
+                varn = varName[0:LvarName - Lst]
+                ifst = True
+                break
+    if not ifst:
+        varn = varName
+
+    ncf = open(infile, 'r')
+
+    for line in ncf:
+        if line[0:1] != '#':
+            values = line.replace('\n','').split(',')
+            if len(values) != 8:
+                print errormsg
+                print "problem in varibale:'", values[0],                            \
+                  'it should have 8 values and it has',len(values)
+                quit(-1)
+
+            if varn[0:6] == 'varDIM': 
+# Variable from a dimension (all with 'varDIM' prefix)
+                Lvarn = len(varn)
+                varvals = [varn[6:Lvarn+1], varn[6:Lvarn+1], 0., 1.,                 \
+                  "variable|from|size|of|dimension|'" + varn[6:Lvarn+1] + "'", '1',  \
+                   'rainbow']
+            else:
+                varvals = [values[1].replace(' ',''), values[2].replace(' ',''),     \
+                  np.float(values[3]), np.float(values[4]),values[5].replace(' ',''),\
+                  values[6].replace(' ',''), values[7].replace(' ','')]
+            if values[0] == varn:
+                ncf.close()
+                return varvals
+                break
+
+    print errormsg
+    print '  ' + fname + ": variable '" + varn + "' not defined !!!"
+    ncf.close()
+    quit(-1)
+
+    return 
 
 ####### ###### ##### #### ### ## #
@@ -44,4 +280,5 @@
 
 tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1']
+
 
 #######    #######
@@ -76,12 +313,12 @@
 objofile.createDimension('time',None)
 
-ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell')
-ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique')
-ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie')
-ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique')
-ncvar.set_attribute(objofile, 'country', 'France')
-ncvar.set_attribute(objofile, 'city', 'Paris')
-ncvar.set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py')
-ncvar.set_attribute(objofile, 'version', '1.0')
+set_attribute(objofile, 'author', 'Lluis Fita Borrell')
+set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique')
+set_attribute(objofile, 'university', 'University Pierre et Marie Curie')
+set_attribute(objofile, 'center', 'Centre national de la recherche scientifique')
+set_attribute(objofile, 'country', 'France')
+set_attribute(objofile, 'city', 'Paris')
+set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py')
+set_attribute(objofile, 'version', '1.0')
 
 time_step = []
@@ -96,5 +333,5 @@
 itz = 0
 for line in objlfile:
-    values = ncvar.reduce_spaces(line)
+    values = reduce_spaces(line)
 #    print iline, values[0], dimz, Searchdimz
 # Writting general information
@@ -103,16 +340,16 @@
         valueshead = ts_header(line)
 
-        ncvar.set_attribute(newvar, 'name', ncvar.reduce_last_spaces(valueshead[0]))
-        ncvar.set_attribute(newvar, 'acronym',valueshead[3].replace(' ',''))
-
-        ncvar.set_attribute(newvar, 'real_lon', valueshead[5])
-        ncvar.set_attribute(newvar, 'real_lat', valueshead[7])
-
-        ncvar.set_attribute(newvar, 'x_grid_point', valueshead[9])
-        ncvar.set_attribute(newvar, 'y_grid_point', valueshead[11])
-
-        ncvar.set_attribute(newvar, 'model_lon', valueshead[13])
-        ncvar.set_attribute(newvar, 'model_lat', valueshead[15])
-        ncvar.set_attribute(newvar, 'model_height', valueshead[17])
+        set_attribute(newvar, 'name', reduce_last_spaces(valueshead[0]))
+        set_attribute(newvar, 'acronym',valueshead[3].replace(' ',''))
+
+        set_attribute(newvar, 'real_lon', valueshead[5])
+        set_attribute(newvar, 'real_lat', valueshead[7])
+
+        set_attribute(newvar, 'x_grid_point', valueshead[9])
+        set_attribute(newvar, 'y_grid_point', valueshead[11])
+
+        set_attribute(newvar, 'model_lon', valueshead[13])
+        set_attribute(newvar, 'model_lat', valueshead[15])
+        set_attribute(newvar, 'model_height', valueshead[17])
         simstarttime = refdate
     else:
@@ -145,7 +382,7 @@
 newvar = objofile.createVariable('time','f8',('time'))
 newvar[:] = time_stepv*3600.
-newattr = ncvar.basicvardef(newvar, 'time', 'time', 'seconds since ' +               \
+newattr = basicvardef(newvar, 'time', 'time', 'seconds since ' +               \
   simstarttime.replace('_',' '))
-ncvar.set_attribute(newvar, 'calendar', 'standard')
+set_attribute(newvar, 'calendar', 'standard')
 
 dt = time_stepv[1] - time_stepv[0]
@@ -155,9 +392,9 @@
     if tsvn[iv] == 't' or tsvn[iv] == 'u' or tsvn[iv] == 'v' or tsvn[iv] == 'q': 
       varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar =                \
-      ncvar.variables_values('TS' + tsvn[iv])
+      variables_values('TS' + tsvn[iv])
       tsu = unitsvar
     else:
       varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar =                \
-      ncvar.variables_values(tsvn[iv])
+      variables_values(tsvn[iv])
       tsu = tsvu[iv]
 
@@ -165,7 +402,7 @@
     newvar[:] = tsvaluesv[:,iv]
 
-    newattr = ncvar.basicvardef(newvar, stdname, longname.replace('|',' '), tsu)
-    newattr = ncvar.set_attribute(newvar, 'wrfTSname', tsvn[iv])
-    newattr = ncvar.set_attribute(newvar, 'wrfTSdesc', tsvln[iv])
+    newattr = basicvardef(newvar, stdname, longname.replace('|',' '), tsu)
+    newattr = set_attribute(newvar, 'wrfTSname', tsvn[iv])
+    newattr = set_attribute(newvar, 'wrfTSdesc', tsvln[iv])
 
 # Extra vars
@@ -175,9 +412,9 @@
 varvals[1:dimt] = pracc[1:dimt] - pracc[0:dimt-1]
 varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar =                      \
-      ncvar.variables_values('RAINTOT')
+      variables_values('RAINTOT')
 
 newvar = objofile.createVariable(varname, 'f4', ('time'))
 newvar[:] = varvals / dt
-newattr = ncvar.basicvardef(newvar, stdname, longname.replace('|',' '), unitsvar )
+newattr = basicvardef(newvar, stdname, longname.replace('|',' '), unitsvar )
 
 objofile.sync()
