Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 1375)
+++ /trunk/tools/nc_var_tools.py	(revision 1376)
@@ -22,4 +22,5 @@
 # add_varattrs: Function to add variables attributes from a given netCDF object to other variables in another one
 # add_vars: Function to add variables from a given netCDF object to another one
+# addVar: Operation to add a new variable in a file using exisintg dimensions
 # addvals: Function to add values to a given variable at a given dimension
 # attrinvar: Check existence of an attribute in a netCDF variable object
@@ -104,4 +105,5 @@
 # remapnn_old: Function to remap to the nearest neightbor a variable using projection from another fil
 # reproject: Function to reproject values to another one
+# rm_FillValue: Operation to remove the _FillValue from a variable inside a netCDF file
 # seasmean: Function to compute the seasonal mean of a variable
 # sellonlatbox: Function to select a lotlan box from a data-set
@@ -112,4 +114,5 @@
 # set_attributek: Sets a value of an attribute of a netCDF variable with a kind. Removes previous attribute value if exists
 # setvar_asciivalues: Function to set de values of a variable with an ASCII file (common Fortran-like format)
+# setvar_nc: Operation to set values of a variable from values of an other one
 # SliceVar: Function to slice a given variable throughout a given list of dimensions
 # SliceVarDict: Function to slice a given variable throughout a dictionary
@@ -14633,7 +14636,13 @@
 def VarVal_FillValue(values, filen, varn):
     """ Function to transform a given value from a given variable to _FillValue in a netCDF file
-      values= [value],[fillVal]
+      values= [value],[oper],[fillVal]
         [value]: value to pass to '_FillValue'
-        [fillVal]: value for '_FillValue', 'std' for the standard value
+        [oper]: operation to follow with [value] to transform to `_FillValue'
+          'eq': equal than [value]
+          'ge': greater equal than [value]
+          'gt': greater than [value]
+          'le': less equal than [value]
+          'lt': less than [value]
+        [fillVal]: value for '_FillValue', 'std' for the standard value which means:
            Float = 1.e20
            Character = '-'
@@ -14646,8 +14655,17 @@
     fname = 'VarVal_FillValue'
 
+    availopers = ['eq', 'ge', 'gt', 'le', 'lt']
+
     if values == 'h':
         print fname + '_____________________________________________________________'
         print VarVal_FillValue.__doc__
         quit()
+
+    arguments = '[value],[oper],[fillVal]'
+    gen.check_arguments(fname, values, arguments, ',')
+
+    value = values.split(',')[0]
+    oper = values.split(',')[1]
+    fval0 = values.split(',')[2]
 
     onc = NetCDFFile(filen, 'a')
@@ -14657,7 +14675,4 @@
           "variable '" + varn + "' !!"
         quit(-1)
-
-    value = values.split(',')[0]
-    fval0 = values.split(',')[1]
 
     varo = onc.variables[varn]
@@ -14683,5 +14698,20 @@
 
     fval = gen.fillvalue_kind(vartype, fval0)
-    newvarvals = np.where(varvals==valchk, fval, varvals)
+
+    if oper == 'eq':
+        newvarvals = np.where(varvals == valchk, fval, varvals)
+    elif oper == 'ge':
+        newvarvals = np.where(varvals >= valchk, fval, varvals)
+    elif oper == 'gt':
+        newvarvals = np.where(varvals > valchk, fval, varvals)
+    elif oper == 'le':
+        newvarvals = np.where(varvals <= valchk, fval, varvals)
+    elif oper == 'lt':
+        newvarvals = np.where(varvals < valchk, fval, varvals)
+    else:
+        print errormsg
+        print '  ' + fname + ": operation '" + oper + "' not ready !!"
+        print '    available ones:', availopers
+        quit(-1)
 
     if gen.searchInlist(varo.ncattrs(), 'missing_value'):
@@ -14708,5 +14738,5 @@
     return
 
-#VarVal_FillValue('0.,std', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/new_lai2D.nc', 'LAI')
+#VarVal_FillValue('0.,eq,std', '/home/lluis/etudes/DYNAMICO/ORCHIDEE/interpolation/data/new_lai2D.nc', 'LAI')
 
 def lonlatProj(dxres, dyres, project, wfile):
@@ -18117,3 +18147,213 @@
     return
 
+def addVar(values, ncfile, varn):
+    """ Operation to add a new variable in a file using exisintg dimensions
+      values= [dnames]|[varattributes]|[kind]
+        [dnames]: [dimn1],...,[dimnN], ',' list of name of dimensions for the new variable (already existing)
+        [attributes]: [std_name]@[long_name]@[units], standard name, long name and units of the variable
+          ('!' for spaces)
+        [kind]: type of variable (standard netCDF4/C-like values, 'c', 'i', 'f', 'f8',...)
+      ncfile= name of the file
+      varn= name of the new variable
+    """
+    fname = 'addVar'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print addVar.__doc__
+        quit()
+
+    expectargs = '[dimensions]|[varattributes]|[kind]'
+ 
+    gen.check_arguments(fname,values,expectargs,'|')
+
+    dnames = values.split('|')[0].split(',')
+    attributes = values.split('|')[1].replace('!',' ')
+    kind = values.split('|')[2]
+
+    onc = NetCDFFile(ncfile, 'a')
+
+    dimns = onc.dimensions
+    varns = onc.variables.keys()
+
+    # Checking dimensions
+    dsize = []
+    for dimn in dnames:
+        if not gen.searchInlist(dimns, dimn):
+            print errormsg
+            print '  ' +fname+ ": file '" + ncfile + "' does not have dimension '" + \
+                dimn + "' !!"
+            print '    existing ones:', dimns
+            quit(-1)
+        else:
+            dsize.append(len(onc.dimensions[dimn]))
+
+    # Checking new varname
+    if gen.searchInlist(varns, varn):
+        print errormsg
+        print '  ' +fname+ ": file '" + ncfile + "' already has variable '" + varn + \
+            "' !!"
+        print '    existing ones:', varns
+        quit(-1)
+
+# Variable
+    if kind == 'c':
+        newvar = onc.createVariable(varn, 'c', tuple(dnames))
+#        newvar[:] = np.zeros(tuple(dsize), dtype=np.float)
+    elif kind == 'f' or kind == 'f4':
+        newvar = onc.createVariable(varn, 'f4', tuple(dnames),                       \
+          fill_value=gen.fillValueF)
+        newvar[:] = np.zeros(tuple(dsize), dtype=np.float)
+    elif kind == 'f8':
+        newvar = onc.createVariable(varn, 'f8', tuple(dnames),                       \
+          fill_value=gen.fillValueD)
+        newvar[:] = np.zeros(tuple(dsize), dtype=np.float64)
+    elif kind == 'i':
+        newvar = onc.createVariable(varn, 'i', tuple(dnames),                        \
+          fill_value=gen.fillValueI)
+        newvar[:] = np.zeros(tuple(dsize), dtype=int)
+    else:
+        print errormsg
+        print '  ' + fname + ": variable kind '" + kind + "' not ready!!"
+        quit(-1)
+
+    sname = attributes.split('@')[0]
+    lname = attributes.split('@')[1]
+    u = attributes.split('@')[2]
+    
+    newattr = basicvardef(newvar, sname, lname, u)
+
+    onc.sync()
+    onc.close()
+
+    return
+
+def setvar_nc(values, ncfile, varn):
+    """ Operation to set values of a variable from values of an other one
+      values= [infile],[invar]
+        [infile]: name of the file with the reference variable
+        [invar]: name of the variable in [infile]
+        [dims]: '|' list of [dimn];[dimval] dimension and value to define slice of values to use from [invar]
+          * [integer]: which value of the dimension
+          * -1: all along the dimension
+          * -9: last value of the dimension
+          * [beg]:[end]:[frq] slice from [beg] to [end] every [frq]
+          * NOTE, no dim name all the dimension size
+      ncfile= name of the file with the variable to fill
+      varn= name of the variable to fill
+    """
+    fname = 'addVar'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print addVar.__doc__
+        quit()
+
+    expectargs = '[infile],[invar],[dims]'
+ 
+    gen.check_arguments(fname,values,expectargs,',')
+
+    infile = values.split(',')[0]
+    invar = values.split(',')[1]
+    dims = values.split(',')[2].split('|')
+
+    oinc = NetCDFFile(infile, 'r')
+    onc = NetCDFFile(ncfile, 'a')
+
+    if not onc.variables.has_key(varn):
+        print errormsg
+        print '  '+fname+": file '" + ncfile + "' does not have variable '" + varn + \
+          "' to be filled !!"
+        print '    existing ones:', onc.variables.keys()
+        quit(-1)
+
+    if not oinc.variables.has_key(invar):
+        print errormsg
+        print '  '+fname+": file '" + infile + "' does not have variable '" + invar +\
+          "' to use for filling !!"
+        print '    existing ones:', oinc.variables.keys()
+        quit(-1)
+
+    oinvar = oinc.variables[invar]
+    ovar = onc.variables[varn]
+
+    # Slicing input varibale
+    slicedict = {}
+    for dimns in dims:
+        dimn = dimns.split(';')[0]
+        dimv = dimns.split(';')[1]
+        if dimv.find(':') != -1:
+            slicedict[dimn] = np.array(dimv.split(':'), dtype=int)
+        else:
+            slicedict[dimn] = int(dimv)
+
+    invarslice, invardims = SliceVarDict(oinvar, slicedict)
+    invals = oinvar[tuple(invarslice)]
+
+    # Checking same shapes
+    gen.same_shape(invals, ovar[:])
+
+    ovar[:] = invals[:]
+
+    oinc.close()
+
+    onc.sync()
+    onc.close()
+
+    return
+
+def rm_FillValue(values, ncfile, varn):
+    """ Operation to remove the _FillValue from a variable inside a netCDF file
+      values= [newvalue]
+        [newvalue]: value to use to replace '_FillValue'
+      ncfile= name of the file with the variable to fill
+      varn= name of the variable to fill
+    """
+    import numpy.ma as ma
+    fname = 'rm_FillValue'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print rm_FillValue.__doc__
+        quit()
+
+#    expectargs = '[newvalue]'
+ 
+#    gen.check_arguments(fname,values,expectargs,',')
+
+    newvalue = values
+
+    onc = NetCDFFile(ncfile, 'a')
+
+    if not onc.variables.has_key(varn):
+        print errormsg
+        print '  ' + fname + ": file '" + ncfile + "' does not have variable '" +    \
+          varn + "' !!"
+        print '    available ones:'. onc.variables.keys()
+        quit(-1)
+
+    ovar = onc.variables[varn]
+    varattrs = ovar.ncattrs()
+
+    # Checking for _FillValue
+    if not gen.searchInlist(varattrs, '_FillValue'):
+        print warnmsg
+        print '  ' + fname + ": varibale '" + varn + "' does not have _FillValue " + \
+          "attribute !!"
+        print '    attributes:', varattrs
+        print "    Use `nc_var.py -o valmod -f " + ncfile + " -S '[...]' -v " +      \
+          varn + " instead !!"
+        quit(-1)
+
+    varattrs.remove('_FillValue')
+    maskvarvals = ovar[:]
+    newvals = maskvarvals.filled(gen.retype(newvalue,ovar.dtype))
+    attr = ovar.delncattr('_FillValue')
+
+    ovar[:] = newvals[:]
+    onc.sync()
+    onc.close()
+
+    return
+
 #quit()
