Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 2376)
+++ /trunk/tools/nc_var_tools.py	(revision 2377)
@@ -370,5 +370,5 @@
     """ Function to add variables from a given netCDF object to another one
       oc: source netcdfile object
-      nc: netcdfile object to add dimensions
+      nc: netcdfile object to add variables
       vnames: list of name of variables to add
     """
@@ -378,4 +378,13 @@
         if not gen.searchInlist(nc.variables,vn):
             print '  ' + fname + ': adding var:',vn,' ...'
+
+            if not oc.variables.has_key(vn):
+                print errormsg
+                print '  '+fname+ ": input netCDF object does not have variable '" + \
+                  vn + "' !!"
+                Varns = list(oc.variables.keys())
+                Varns.sort()
+                print '    available ones:', Varns
+                quit(-1)
 
             varo = oc.variables[vn]
@@ -30409,5 +30418,4 @@
 #except_fillValue('orog:range,500.,7000.:None:value,0', '/home/lluis/estudios/Andes/cmip_data/fx/orog_fx_ACCESS1-0_historical_r0i0p0.nc', 'all')
 
-
 def usefile_compute_slices_stats_areaweighted(values, ncfile, variable):
     """ Function to compute stats of variables of a file splitting variables by 
@@ -30800,4 +30808,141 @@
 #usefile_compute_slices_stats_areaweighted(values, '/media/lluis/ExtDiskC_ext4/bkup/llamp/estudios/dominios/SA5k/geo_em.d01.nc', 'LANDUSEF')
 
+def compress_data(values, ncfile, variable):
+    """ Function to compress (only provide values for a criteria) the values of a 2D 
+          file following a given criteria from one of its variables
+      values=[compressvar]:[slicecompressvar]:[criteria]:[axesdimvars]
+        [compressvarn]: name of variable to use to define the criteria
+        [slicecompressvar]: ',' list of selection values of [dimn]|[val] for only the
+          dimensions of [compressvar] in order to construct a 2D variable to use 
+          for the compression ('all', to use all dimensions)
+        [criteria]: criteria to use for the compression following [compresssvar] values
+          'singlevalue',[value]: only the points where [compressvar] = [value]
+          'range',[begval],[endval]: only the points where [begval] <= [compressvar] 
+            <= [value]
+        [axesdimvars]: ',' separated list of [AxisN]|[dimnN]|[vardimnN]
+          dimension name [dimn] and its associated dimension-variable for the X, Y, Z 
+          and T axes, (use additional capital letters for extra axes)
+      ncfile= netCDF file to use
+      variable: ',' list of variables to compress ('all' for all variables)
+    """
+    fname = 'compress_data'
+
+    availcrit = ["'singlevalue',[value]" , "'range',[begval],[endval]"]
+
+    if values == 'h':
+        print fname + '_____________________________________________________________'
+        print compress_data.__doc__
+        quit()
+
+    expectargs = '[compressvarn]:[slicecompressvar]:[criteria]:[axesdimvars]'
+    gen.check_arguments(fname, values, expectargs, ':')
+
+    compressvarn = values.split(':')[0]
+    slicecompressvar = values.split(':')[1]
+    criteria = values.split(':')[2]
+    axesdimvars = values.split(':')[3].split(',')
+
+    onc = NetCDFFile(ncfile, 'r')
+
+    if not onc.variables.has_key(compressvarn):
+        print errormsg
+        print '  ' + fname + ": file '" + ncfile + "' does not have compress " +     \
+          "variable '" + compressvarn + "' !!"
+        Varns = onc.variables.keys()
+        Varns.sort()
+        print '    available ones:', Varns
+        onc.close()
+        quit(-1)
+
+    ocompressvar = onc.variables[compressvarn]
+    if slicecompressvar == 'all':
+        compressvar = ocompressvar[:]
+    else:
+        slicevar = []
+        slicedims = gen.stringS_dictvar(slicecompressvar, ',', '|')
+        vardims = ocompressvar.dimensions
+        for vd in vardims:
+            if slicedims.has_key(vd): slicevar.append(slicedims[vd])
+            else: slicevar.append(slice(0,len(onc.dimensions[vd])))
+        compressvar = ocompressvar[tuple(slicevar)]
+
+    if len(compressvar.shape) != 2:
+        print errormsg
+        print '  ' + fname + ": slice from the compressed var '" + compressvarn +    \
+          "' must be of rank 2 and it has a shape:", compressvar.shape, '!!'
+        print "    provide a ',' list of '[dimn]|[value]' as 'slicecompressvar' " +  \
+          "variable in order to obtain a 2D slice of the var to use to compress"
+        print '    dimensions of the compress variable:', ocompressvar.dimensions
+        onc.close()
+        quit(-1)
+
+    crit = criteria.split(',')
+    if crit[0] == 'single':
+        compressv = gen.typemod(crit[1], ocompressvar.dtype)
+        compressmask = ma.masked_not_equal(compressvar,compressv)
+    elif crit[0] == 'range':
+        icompressv = gen.typemod(crit[1], ocompressvar.dtype)
+        ecompressv = gen.typemod(crit[2], ocompressvar.dtype)
+        compressmask = ma.masked_outside(compressvar,icompressv,ecompressv)
+    else:
+        print errormsg
+        print '  ' + fname + ": compressing criteria '" + crit[0] + "' not ready !!"
+        print '    avaialable ones:', availcrit
+        quit(-1)
+
+    Nvals = np.sum(~compressmask.mask)
+    if Nvals == 0:
+        print errormsg
+        print '  ' + fname + ": for compress variable '" + compressvarn+ "' sliced "+\
+          slicecompressvar, "and criteria '" + criteria + "' no values are obtained!!"
+        print '    fix either the slice and/or the criteria'
+        quit(-1)
+    else:
+        print infmsg
+        print '  ' + fname + ': compressed number of values:', Nvals
+
+    # Getting axis dimension-variables
+    axisdims = {}
+    for axdv in axesdimvars:
+        axN = axdv.split('|')[0]
+        dn = axdv.split('|')[1]
+        dv = axdv.split('|')[2]
+        axisdims[axN] = [dn, dv]
+
+    # Creation of the output file
+    onewnc = NetCDFFile(fname + '.nc', 'w')
+
+    # Dimensions
+    newdim = onewnc.createDimension(compressvarn, Nvals)
+    dnv = axisdims['X']
+    newdim = onewnc.createDimension(dnv[0], len(onc.dimensions[dnv[0]]))
+    compressS = dnv[0]
+    dnv = axisdims['Y']
+    newdim = onewnc.createDimension(dnv[0], len(onc.dimensions[dnv[0]]))
+    compressS = dnv[0] + ' ' + dnv[1]
+
+    # Space Dimension variables
+    dnv = axisdims['X']
+    add_vars(onc,onewnc,[dnv[1]])
+    dnv = axisdims['Y']
+    add_vars(onc,onewnc,[dnv[1]])
+
+    # Variable
+    icvar = np.arange(np.prod(compressvar.shape))
+    icc = icvar[~compressmask.mask]
+
+    newvar = onenc.createVariable(compressvar, 'i', ('compressvar'))
+    cfvarvals = gen.variablesvalues(compressvar)
+    basicvardef(newvar, cfvarvals[1], cfvarvals[4], cfvarvals[5])
+    newvar.setncattr('compress', compressS)
+    newvar[:] = icc
+    
+    return
+
+
+values = 'XLAND:Time|0:single,1:X|west_east|XLONG_M,Y|south_north|XLAT_M'
+
+compress_data(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2')
+
 #quit() 
 
