Index: trunk/tools/nc_var.py
===================================================================
--- trunk/tools/nc_var.py	(revision 2194)
+++ trunk/tools/nc_var.py	(revision 2195)
@@ -62,4 +62,5 @@
 ## e.g. # nc_var.py -o temporal_stats -S 'Time:WRFtime:day@1@min,LTday@-3@1@min:bottom_top@ZNU,south_north@XLAT,west_east@XLONG' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2
 ## e.g. # nc_var.py -o retrieve_stations -f wrfout_d01_1995-01-01_00:00:00 -S 'tmin_percentiles.nc:stname:None:stlon:stlat:None:nearest:west_east:XLONG:south_north:XLAT:HGT:Time:WRFtime' -v T2,QVAPOR
+## e.g. # nc_var.py -o compute_slice2Dstats -S 'XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2
 
 from optparse import OptionParser
@@ -89,4 +90,5 @@
 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps
 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps
+# compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables
 # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
 # computevar_model: Function to provide the way to compute a CF-variable providing its name
@@ -203,5 +205,6 @@
   'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum',                \
   'compute_opersvarsfiles',                                                          \
-  'compute_opervaralltime', 'compute_opervartimes', 'compute_tevolboxtraj',          \
+  'compute_opervaralltime', 'compute_opervartimes', 'compute_slice2Dstats',          \
+  'compute_tevolboxtraj',                                                            \
   'computevar_model', 'curve_section', 'DatesFiles',                                 \
   'DataSetSection', 'DataSetSection_multidims', 'DataSetSection_multivars',          \
@@ -342,4 +345,6 @@
 elif oper == 'compute_opervartimes':
     ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname)
+elif oper == 'compute_slice2Dstats':
+    ncvar.compute_slice2Dstats(opts.values, opts.ncfile, opts.varname)
 elif oper == 'compute_tevolboxtraj':
     ncvar.compute_tevolboxtraj(opts.values, opts.ncfile, opts.varname)
Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 2194)
+++ trunk/tools/nc_var_tools.py	(revision 2195)
@@ -46,4 +46,5 @@
 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps
 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps
+# compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables
 # computevar_model: Function to provide the way to compute a CF-variable providing its name
 # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory
@@ -26132,12 +26133,14 @@
 
 def compute_slice2Dstats(values, ncfile, variable):
-    """ Function to compute stats of variables of a file following slices along 2 variables
-      values=dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,maxvar2,slcevar2
-        dimn[1/2]: dimension from which produce the slices of variable 1/2 ('all' 
-          for non-specific dimension to cut along)
+    """ Function to compute stats of variables of a file following slices along 2 
+        variables
+      values=varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2
         varn[1/2]: name of the variable 1/2
         minvar[1/2]: minimum value to start the slices for variable 1/2
         maxvar[1/2]: maximum value to end the slices for variable 1/2
         slcevar[1/2]: length of slices in variable 1/2
+        dimvars: ':' separated list of [dimn]|[vardimn] dimension name [dimn] and 
+          its associated dimension-variable to get its values to provide values for 
+          the final file
       ncfile= netCDF file to use
       variable: ',' list of variables ('all' for all variables)
@@ -26150,38 +26153,19 @@
         quit()
 
-    expectargs = 'dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,' +       \
-     'maxvar2,slcevar2'
+    expectargs = 'varn1,minvar1,maxvar1,slcevar1,varn2,minvar2,maxvar2,slcevar2,' +  \
+      'dimvars'
     gen.check_arguments(fname, values, expectargs, ',')
 
-    dimn1 = values.split(',')[0]
-    varn1 = values.split(',')[1]
-    minvar1 = np.float(values.split(',')[2])
-    maxvar1 = np.float(values.split(',')[3])
-    slicevar1 = np.float(values.split(',')[4])
-    dimn2 = values.split(',')[5]
-    varn2 = values.split(',')[6]
-    minvar2 = np.float(values.split(',')[7])
-    maxvar2 = np.float(values.split(',')[8])
-    slicevar2 = np.float(values.split(',')[9])
+    varn1 = values.split(',')[0]
+    minvar1 = np.float(values.split(',')[1])
+    maxvar1 = np.float(values.split(',')[2])
+    slicevar1 = np.float(values.split(',')[3])
+    varn2 = values.split(',')[4]
+    minvar2 = np.float(values.split(',')[5])
+    maxvar2 = np.float(values.split(',')[6])
+    slicevar2 = np.float(values.split(',')[7])
+    dimvars = gen.stringS_dictvar(values.split(',')[8], Dc=':', DVs='|')
     
     onc = NetCDFFile(ncfile, 'r')
-
-    if dimn1 != 'all' and not gen.searchInlist(onc.dimensions, dimn1):
-        print errormsg
-        print '  ' + fname + ": file '" + ncfile + "' does not have dimension 1: '" +\
-          dimn1 + "' !!"
-        dimns = list(onc.dimensions)
-        dimns.sort()
-        print '    available ones:', dimns
-        quit(-1)
-
-    if dimn2 != 'all' and not gen.searchInlist(onc.dimensions, dimn2):
-        print errormsg
-        print '  ' + fname + ": file '" + ncfile + "' does not have dimension 2: '" +\
-          dimn2 + "' !!"
-        dimns = list(onc.dimensions)
-        dimns.sort()
-        print '    available ones:', dimns
-        quit(-1)
 
     if not onc.variables.has_key(varn1):
@@ -26203,6 +26187,19 @@
         quit(-1)
 
+    for dn in dimvars.keys():
+        if not gen.searchInlist(onc.dimensions,dn):
+            print errormsg
+            print '  ' +fname+ ": file '" + ncfile + "' does not have dimension: '"+ \
+              dn + "' !!"
+            dimns = list(onc.dimensions)
+            dimns.sort()
+            print '    available ones:', dimns
+            quit(-1)
+
+
     ovar1 = onc.variables[varn1]
     ovar2 = onc.variables[varn2]
+    v1u = ovar1.units
+    v2u = ovar2.units
 
     dvar1 = (maxvar1 - minvar1+slicevar1)/slicevar1
@@ -26223,14 +26220,24 @@
     sliceshape1 = [Nslices1] + list(ovar1.shape)
     slcvar1 = np.zeros(tuple(sliceshape1), dtype=bool)
+    slcvalsc1 = np.zeros((Nslices1), dtype=np.float)
+    slcvals1 = np.zeros((Nslices1,2), dtype=np.float)
     var1v = ovar1[:]
     for islc in range(Nslices1-1):
         slcvar1[islc,] = ma.masked_inside(var1v, slices1[islc], slices1[islc+1]).mask
+        slcvalsc1[islc] = (slices1[islc+1]+slices1[islc+1])/2.
+        slcvals1[islc,0] = slices1[islc]
+        slcvals1[islc,1] = slices1[islc+1]
 
     # Slices var2
     sliceshape2 = [Nslices2] + list(ovar2.shape)
     slcvar2 = np.zeros(tuple(sliceshape2), dtype=bool)
+    slcvalsc2 = np.zeros((Nslices2), dtype=np.float)
+    slcvals2 = np.zeros((Nslices2,2), dtype=np.float)
     var2v = ovar2[:]
     for islc in range(Nslices2-1):
         slcvar2[islc,] = ma.masked_inside(var2v, slices2[islc], slices2[islc+1]).mask
+        slcvalsc2[islc] = (slices2[islc+1]+slices2[islc+1])/2.
+        slcvals2[islc,0] = slices2[islc]
+        slcvals2[islc,1] = slices2[islc+1]
 
     if variable == 'all':
@@ -26239,9 +26246,42 @@
         varns = gen.str_list(variable, ',')
 
+    onewnc = NetCDFFile(fname + '.nc', 'w')
+    # dimensions
+    newdim = onewnc.createDimension('slice_'+varn1, Nslices1)
+    newdim = onewnc.createDimension('slice_'+varn2, Nslices2)
+    newdim = onewnc.createDimension('slice_bnds', 2)
+
+    # variable dimensions
+    newvar = onewnc.createVariable('slice_'+varn1, 'f', ('slice_'+varn1))
+    newvar[:] = slcvalsc1[:]
+    basicvardef(newvar, 'slice_'+varn1, 'slices for variable ' + varn1, v1u)
+
+    newvar = onewnc.createVariable('slice_'+varn1+'_bnds', 'f', ('slice_'+varn1,     \
+      'slice_bnds'))
+    newvar[:] = slcvals1[:]
+    basicvardef(newvar, 'slice_'+varn1+'_bnds', 'boundaries of slices for variable '+\
+      varn1, v1u)
+
+    newvar = onewnc.createVariable('slice_'+varn2, 'f', ('slice_'+varn2))
+    newvar[:] = slcvalsc2[:]
+    basicvardef(newvar, 'slice_'+varn2, 'slices for variable ' + varn2, v2u)
+
+    newvar = onewnc.createVariable('slice_'+varn2+'_bnds', 'f', ('slice_'+varn2,     \
+      'slice_bnds'))
+    newvar[:] = slcvals2[:]
+    basicvardef(newvar, 'slice_'+varn2+'_bnds', 'boundaries of slices for variable '+\
+      varn2, v2u)
+
     for varn in varns:
+        print '    ' + varn + ' ...'
         ovar = onc.variables[varn]
         varv = ovar[:]
         vdimns = list(ovar.dimensions)
         vshape = list(ovar.shape)
+        vrank = len(vdimns)
+        vu = ovar.units
+
+        for dn in vdimns:
+            if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn])
 
         # mask1 in var slice
@@ -26272,37 +26312,85 @@
 
         newvarshape = [Nslices2, Nslices1] + vshape
-        newvar2masked = np.zeros(tuple(newvarshape), dtype=ovar.dtype)
+        newvar2masked = np.ones(tuple(newvarshape), dtype=ovar.dtype)*gen.fillValueF
 
         for islc2 in range(Nslices2):
             newmask2 = mask2varslice[islc2]
             for islc1 in range(Nslices1):
+                slcnewvar = []
+                slcnewvar.append(islc2)
+                slcnewvar.append(islc1)
+                for iid in vshape:
+                    slcnewvar.append(slice(0,iid))
                 newmask1 = mask1varslice[islc1]
-                newmask = newmask2+newmask1
-
-                print '   Lluis shapes: newvar2masked',  newvar2masked[islc2,islc1,].shape, 'varv:', varv[newmask].shape
-                newvar2masked[islc2,islc1,] = ma.masked_array(varv, mask=~newmask)
-
-        onewnc = NetCDFFile(fname + '.nc', 'w')
-        # dimensions
-        newdim = onewnc.createDimension('lon', varv.shape[2])
-        newdim = onewnc.createDimension('lat', varv.shape[1])
-        newdim = onewnc.createDimension('time', None)
-        newdim = onewnc.createDimension('slice_'+varn1, Nslices1)
-        newdim = onewnc.createDimension('slice_'+varn2, Nslices2)
-
+                newmask = newmask2*newmask1
+                mavals = ma.array(varv, mask=~newmask)
+                newvar2masked[tuple(slcnewvar)] = mavals.filled(gen.fillValueF)
+                 
         # variables
-        newvar = onewnc.createVariable(varn, 'f', ('slice_'+varn2, 'slice_'+varn1, 'time', 'lat', 'lon'))
+        newvard = ['slice_'+varn2,'slice_'+varn1] + vdimns
+        newvar = onewnc.createVariable(varn+'sliced', 'f', tuple(newvard),           \
+          fill_value=gen.fillValueF)
         newvar[:] = newvar2masked[:]
+        basicvardef(newvar, varn+'sliced', varn + 'sliced by '+varn1+' & '+varn2, vu)
+        add_varattrs(onc, onewnc, [varn], [varn+'sliced'])
+
+        manewvar = ma.masked_equal(newvar2masked, gen.fillValueF)
+
+        newvard = ['slice_'+varn2,'slice_'+varn1]
+        newvarn = onewnc.createVariable(varn+'minsliced', 'f', tuple(newvard),        \
+          fill_value=gen.fillValueF)
+        basicvardef(newvarn, varn+'minsliced', 'minimum value of '+varn+'sliced by '+ \
+          varn1+' & '+varn2, vu)
+        add_varattrs(onc, onewnc, [varn], [varn+'minsliced'])
+
+        newvarx = onewnc.createVariable(varn+'maxsliced', 'f', tuple(newvard),        \
+          fill_value=gen.fillValueF)
+        basicvardef(newvarx, varn+'maxsliced', 'maximum value of '+varn+'sliced by '+ \
+          varn1+' & '+varn2, vu)
+        add_varattrs(onc, onewnc, [varn], [varn+'maxsliced'])
+
+        newvarm = onewnc.createVariable(varn+'meansliced', 'f', tuple(newvard),       \
+          fill_value=gen.fillValueF)
+        basicvardef(newvarm, varn+'meansliced', 'mean value of '+varn+'sliced by ' +  \
+          varn1+' & '+varn2, vu)
+        add_varattrs(onc, onewnc, [varn], [varn+'meansliced'])
+
+        newvars = onewnc.createVariable(varn+'stdsliced', 'f', tuple(newvard),        \
+          fill_value=gen.fillValueF)
+        basicvardef(newvars, varn+'stdsliced', 'standard deviation of '+varn+         \
+          'sliced by '+varn1+' & '+varn2, vu)
+        add_varattrs(onc, onewnc, [varn], [varn+'stdsliced'])
+
+        newvarn[:] = np.min(manewvar, axis=tuple(np.arange(vrank) + 2))
+        newvarx[:] = np.max(manewvar, axis=tuple(np.arange(vrank) + 2))
+        newvarm[:] = np.mean(manewvar, axis=tuple(np.arange(vrank) + 2))
+        newvars[:] = np.std(manewvar, axis=tuple(np.arange(vrank) + 2))
 
         onewnc.sync()
-        onewnc.close()
-
-        quit()
+        # Adding dimension-variables
+        for dn in vdimns:
+            if not dimvars.has_key(dn):
+                print errormsg
+                print '  ' + fname + ": no dimension-variable provided for " +       \
+                  "dimension '" + dn + "' !!"
+                print '    provided ones _______'
+                gen.printing_dictionary(dimvars)
+                quit(-1)
+            if not onewnc.variables.has_key(dimvars[dn]): 
+                add_vars(onc, onewnc, [dimvars[dn]])
+                onewnc.sync()
+
+    # Add global attributes
+    add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0')
+    add_globattrs(onc,onewnc,'all')
+    onewnc.sync()
+    onc.close()
+    onewnc.close()
+    print fname + ": successfull written of file '" + fname + ".nc' !!"
 
     return
 
-values='south_north,XLAT,-63.,19.,2.,all,HGT,250.,7000.,500.'
-
-compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2')
+#values='XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT'
+#compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,Q2')
 
 #quit() 
