Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 1531)
+++ /trunk/tools/nc_var_tools.py	(revision 1532)
@@ -3933,5 +3933,5 @@
       [maskvalue]= value to use for maskking ('mask' value for using an already masked variable)
     filename= netCF file name
-    varparameters= [varn]|[dimsmask]
+    varparameters= [varn]:[dimsmask]
       [varn]: variable name
       [samedimsmask]: ',' list of name of dimensions which fits [maskvarn] slice
@@ -4098,4 +4098,214 @@
 
     return
+
+def maskvarVal(values, filename, varparameters):
+    """ Function to mask a variable as combination of values from other variables from another file and using 
+      a list of resultant variables to mask a variable by inter-matching values
+    values= [maskfilename]:[maskvarns]:[dimsmask]
+      [maskfilename]= file with the mask
+      [maskvarns]= ',' list of [maskvarn1]|maskval1]|[comp],[...[maskvarnN]|[maskvalN]|[compN]] triplets of 
+        variable name, value and comparison to use to mask
+          [comp]: comparisons
+            'eq:' masking all values equal than [maskvali]
+            'ge:' masking all values bigger or equal than [maskvali]
+            'gt:' masking all values bigger than [maskvali]
+            'le:' masking all values smaller or equal than [maskvali]
+            'lt:' masking all values smaller than [maskvali]
+      [dimsmask]= ',' list of [dimn]|[sec] to use for slicing all [maskvari]
+        [sec]: section
+          * [integer]: which value of the dimension
+          * -1: all along the dimension
+          * -9: last value of the dimension
+          * [beg]@[end] slice from [beg] to [end]
+          * NOTE: no value provided is for all dimension range
+       [matchingmaskvars]: ',' of name of variables from masking file, the non-masked values of which will be 
+         used to mask the variable
+    filename= netCF file name
+    varparameters= [varn]:[dimsmask]
+      [varns]: ',' list of variable names in the same order than [matchingmaskvars] to search for coincidencies
+      [applydimsmask]: ',' list of name of dimensions along which apply the mask
+    """
+    fname='maskvarVal'
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print maskvarVal.__doc__
+        quit()
+
+    expectargs = '[maskfilename]:[maskvarns]:[dimsmask]'
+    gen.check_arguments(fname,values,expectargs,':')
+
+    maskfilename = values.split(':')[0]
+    maskvarns = gen.str_list(values.split(':')[1], ',')
+    dimsmask = values.split(':')[2]
+
+    varns = gen.str_list(varparameters.split(':')[0], ',')
+    applydimsmask = gen.str_list(varparameters.split(':')[1], ',')
+
+    ofile = 'mask_' + '-'.join(varns) + '.nc'
+
+    # Masking variables
+    ncfmask = NetCDFFile(maskfilename,'r')
+
+    applymasks = []
+    for mvarns in maskvarns:
+        maskvarn = mvarns.split('|')[0]
+        maskval = mvarns.split('|')[1]
+        maskcomp = mvarns.split('|')[2]
+        if not ncfmask.variables.has_key(maskvarn):
+            print errormsg
+            print '    ' + fname + ": File  '" + maskfilename + "' does not have " + \
+              " variable mask '" + maskvarn + "' !!"
+            print errormsg
+            ncf.close()
+            ncfmask.close()
+            quit(-1)
+
+        varmaskobj = ncfmask.variables[maskvarn]
+        varmaskinf = variable_inf(varmaskobj)
+        mval = gen.retype(maskval,varmaskinf.type)
+        maskvalues, maskdims = slice_variable(varmaskobj, dimsmask.replace('|',':'). \
+          replace(',','|'))
+        if maskcomp == 'eq':
+            applymasks[ma.masked_equal(maskvalues, mval)]
+        elif maskcomp == 'ge':
+            applymasks[ma.masked_equal(maskvalues, mval)]
+
+    print 'NOT finished !!!!'
+    quit(-1)
+
+    ncfmask.close()
+
+
+
+
+    ncf = NetCDFFile(filename,'r')
+
+
+
+
+    for varn in varns:
+        if not ncf.variables.has_key(varn):
+            print errormsg
+            print '    ' + fname + ": File  '" + filename + "' does not have " +     \
+              "variable '" + varn + '" !!!!'
+            print errormsg
+            ncf.close()
+        quit(-1)
+
+    varobj = ncf.variables[varn]
+    varinf = variable_inf(varobj)
+
+
+    # Looking for variable & mask compatibility
+    runningsize = 1
+    rundims = []
+    testslicevar = []
+    runshape = []
+    for dimn in varobj.dimensions:
+        Ldim = len(ncf.dimensions[dimn])
+        if gen.searchInlist(samedimsmask,dimn):
+            testslicevar.append(slice(0,Ldim,None))
+        else:
+            runningsize = runningsize*Ldim
+            rundims.append(dimn)
+            runshape.append(Ldim)
+            testslicevar.append(0)
+    testvar = varobj[tuple(testslicevar)]
+
+    gen.same_shape(testvar,maskvalues)
+
+# Creation of the new file
+##
+    ncfnew = NetCDFFile(ofile,'w')
+   
+    for idim in range(varinf.Ndims):
+        newdimv = ncf.dimensions[varinf.dimns[idim]]
+        if newdimv.isunlimited():
+            ncfnew.createDimension(varinf.dimns[idim], None)
+        else:
+            ncfnew.createDimension(varinf.dimns[idim], varinf.dims[idim])
+    # FillVallue
+    if varinf.dtype == type(np.int(1)):
+        fillValue = gen.fillValueI
+    elif varinf.dtype == type('c'):
+        fillValue = gen.fillValueC
+    elif varinf.dtype == type(np.int16(1)) or varinf.dtype == type(np.int32(1)):
+        fillValue = gen.fillValueI16
+    elif varinf.dtype == type(np.float(1.)):
+        fillValue = gen.fillValueF
+    elif varinf.dtype == type(np.float32(1.)) or varinf.dtype == type(np.float64(1.)):
+        fillValue = gen.fillValueF64
+    else:
+        print errormsg
+        print '  ' + fname + ': type of variable:', varinf.dtype, 'not ready!!'
+        quit(-1)
+
+    if maskvalue != 'mask':
+        maskval = gen.retype(maskvalue,varinf.dtype)
+    else:
+        print infmsg
+        print '  ' + fname + ': maskgin with already masked variable'
+        varmaskv = maskvalues.mask
+
+    newvar = ncfnew.createVariable(varn, varinf.dtype, varinf.dimns,                 \
+      fill_value=fillValue)
+    for attrn in varinf.attributes:
+        if not attrn == '_FillValue':
+            attr = newvar.setncattr(attrn, varobj.getncattr(attrn))
+
+    attr = newvar.setncattr('mask', 'variable masked using ' + values.split(':')[2]+ \
+      ' of variable ' + maskvarn + ' from file ' + maskfilename)
+    newvar[:] = varobj[:]
+
+    ncfnew.sync()
+    ncfnew.close()
+
+    for varns in ncf.variables:
+        if not varns == varn:
+            try:
+                with gen.Capturing() as output:
+                    fvaradd(filename + ',' + varns, ofile)
+            except:
+                print errmsg
+                print 'fvaradd(' + filename + ', ' + varns + ',' + ofilen + ')'
+                for s1out in output: print s1out
+                quit(-1)
+
+    ncfnew = NetCDFFile(ofile,'a')
+    varobj = ncfnew.variables[varn]
+
+    # Masking following that dimensions which are not coincident with the mask
+
+    # Check if there are some coincident dimensional names between both variables
+    vdimns = list(varinf.dimns)
+    mdimns = maskdims
+
+    for dimn in mdimns:
+        if gen.searchInlist(vdimns,dimn): vdimns.remove(dimn)
+
+    #slices = gen.provide_slices(varinf.dimns, varinf.dims, list(varmaskinf.dimns))
+    slices = gen.provide_slices(varinf.dimns, varinf.dims, vdimns)
+
+    if maskvalue != 'mask':
+        maskTOuse = ma.masked_equal(maskvalues, maskval)
+        maskTOusev = maskTOuse.mask
+    else:
+        maskTOusev = maskvalues.mask
+
+    for ir in range(runningsize):
+        islice = slices[ir]
+        varvals = varobj[tuple(islice)]
+        maskvarvals = np.where(maskTOusev, fillValue, varvals)
+        newvar[tuple(islice)] = maskvarvals
+
+    ncfnew.sync()
+    ncfnew.close()
+    ncf.close()
+
+    print 'masked file "' + ofile + '" generated !!!'
+
+    return
+
 
 def chgtimestep(values, origfile, varN):
