Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 892)
+++ trunk/tools/nc_var_tools.py	(revision 893)
@@ -149,5 +149,7 @@
 
         self.values = np.arange((totshape), dtype=np.float).reshape(dt,dz,dy,dx)        
-        self.dimensions = OrderedDict(sorted(dimdict.items(), key=lambda t: t[0]))
+# Here for a fake netCDF file
+#        self.dimensions = OrderedDict(sorted(dimdict.items(), key=lambda t: t[0]))
+        self.dimensions = (dnt, dnz, dny, dnx)
         self.shape = (dt, dz, dy, dx)
         self.standard_name = 'general_var'
@@ -8990,4 +8992,8 @@
     dimvn = values.split(',')[3].split(':')
 
+# Operations with which when the variable dimensions will be ajusted to the file
+#   the zero value along the operated dimensions will be taken
+    zerodimops = ['max', 'mean', 'mean2', 'min', 'sum']
+
     dimsoperS = gen.numVector_String(dimsoper,', ')
     objnc = NetCDFFile(ncfile, 'r')
@@ -9022,5 +9028,5 @@
     newNdim = 0
     newNvar = 0
-    for vn in varns:
+    for vn in varns + dimvn:
         print "'" + vn + "' ... .. ."
         if not objnc.variables.has_key(vn):
@@ -9034,104 +9040,6 @@
         tocompute = False
         for vd in ov.dimensions:
-            print 'Lluis vd:', vd, gen.searchInlist(dimsoper, vd)
             if gen.searchInlist(dimsoper, vd): tocompute = True
-   
-        print 'Lluis tocompute:', tocompute, ov.dimensions, dimsoper
-
-        if tocompute:
-            varslice, varsliceddims = SliceVarDict(ov,dimslice)
-            varvals0 = ov[tuple(varslice)]
-            varvals = np.squeeze(varvals0)
-
-            finalvarvals, finaldims = operdim(varvals, varsliceddims, dimsoper, operkind)
-
-            # Adding dimensions
-            idn = 0
-            for dn in finaldims:
-                print 'Lluis: dn unlimited;', dn, objnc.dimensions[dn].isunlimited(), idn
-                if newNdim == 0:
-                    if not objnc.dimensions[dn].isunlimited():
-                        objnewnc.createDimension(dn, finalvarvals.shape[idn])
-                    else:
-                        objnewnc.createDimension(dn, None)
-                    newNdim = newNdim + 1
-                else:
-                    if not gen.searchInlist(objnewnc.dimensions, dn):
-                        if not objnc.dimensions[dn].isunlimited():
-                            objnewnc.createDimension(dn, finalvarvals.shape[idn])
-                        else:
-                            objnewnc.createDimension(dn, None)
-                        newNdim = newNdim + 1
-                idn = idn + 1
-
-            # Adding variable
-            varinf = variable_inf(ov)
-            if varinf.FillValue is not None:
-                newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype),  \
-                  tuple(finaldims), fillValue=varinf.FillValue)
-            else:
-                newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype),  \
-                  tuple(finaldims))
-                newNvar = newNvar + 1
-            newvar[:] = finalvarvals[:]
-
-# new variable
-            oldvarattr = ov.ncattrs()
-    
-            varname = gen.variables_values(vn)[0]
-            if gen.searchInlist(oldvarattr, 'standard_name'):
-                stdname = ov.getncattr('standard_name')
-            else:
-                stdname = gen.variables_values(vn)[1]
-        
-            if gen.searchInlist(oldvarattr, 'long_name'):
-                lname = ov.getncattr('long_name')
-            else:
-                lname = gen.variables_values(vn)[4].replace('|',' ')
-
-            if gen.searchInlist(oldvarattr, 'units'):
-                uname = ov.getncattr('units')
-            else:
-                uname = gen.variables_values(vn)[5]
-    
-            newattr = basicvardef(newvar, stdname + operkind, lname + ' '+ operkind +\
-              ' along ' + dimsoperS, uname)
-
-            for idim in ov.dimensions:
-                for dv in dimslice.keys():
-                    if dv == idim:
-                        if type(dimslice[dv]) == type([2]):
-                            Sdv = gen.numVector_Sring(dimslice[dv],', ')
-                        elif type(dimslice[dv]) == type(int(2)):
-                            if dimslice[dv] == -1:
-                                Sdv = str(len(objnc.dimensions[idim])) + ', 1'
-                            else:
-                                Sdv = str(dimslice[dv])
-                    else:
-                        Sdv = str(len(objnc.dimensions[idim]))
- 
-                if idim == ov.dimensions[0]:
-                    origdimsS = idim + '(' + Sdv + ')'
-                else:
-                    origdimsS = origdimsS + ' ' + idim + '(' + Sdv + ')'
-
-            newattr = set_attribute(newvar, 'orig_dimensions', origdimsS)
-            objnewnc.sync()
-    
-# variable dimension
-    for vn in dimvn:
-        print "'" + vn + "' ... .. ."
-        if not objnc.variables.has_key(vn):
-            print errormsg
-            print '  ' + fname + ': netCDF file "' + ncfile +                        \
-              '" does not have variable "' + vn + '" !!'
-            quit(-1)
-
-        # Do we have to compute this variable?
-        ov = objnc.variables[vn]
-        tocompute = False
-        for vd in ov.dimensions:
-            if gen.searchInlist(dimsoper, vd): tocompute = True
-   
+
         if tocompute:
             varslice, varsliceddims = SliceVarDict(ov,dimslice)
@@ -9162,8 +9070,8 @@
             varinf = variable_inf(ov)
             if varinf.FillValue is not None:
-                newvar = objnewnc.createVariable(vn, nctype(varinf.dtype),           \
+                newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype),  \
                   tuple(finaldims), fillValue=varinf.FillValue)
             else:
-                newvar = objnewnc.createVariable(vn, nctype(varinf.dtype),           \
+                newvar = objnewnc.createVariable(vn+operkind, nctype(varinf.dtype),  \
                   tuple(finaldims))
                 newNvar = newNvar + 1
@@ -9192,4 +9100,71 @@
               ' along ' + dimsoperS, uname)
 
+            for idim in ov.dimensions:
+                for dv in dimslice.keys():
+                    if dv == idim:
+                        if type(dimslice[dv]) == type([2]):
+                            Sdv = gen.numVector_Sring(dimslice[dv],', ') + ', 1'
+                        elif type(dimslice[dv]) == type(int(2)):
+                            if dimslice[dv] == -1:
+                                Sdv = str(len(objnc.dimensions[idim]))
+                            else:
+                                Sdv = str(dimslice[dv])
+                    else:
+                        Sdv = str(len(objnc.dimensions[idim]))
+ 
+                if idim == ov.dimensions[0]:
+                    origdimsS = idim + '(' + Sdv + ')'
+                else:
+                    origdimsS = origdimsS + ' ' + idim + '(' + Sdv + ')'
+
+            newattr = set_attribute(newvar, 'orig_dimensions', origdimsS)
+            objnewnc.sync()
+    
+# Including operation dimensions on the new slice for the variables dimension
+#   but, which value along the operated dimension should be taken?
+    sliceaddvars = dimslice
+    if gen.searchInlist(zerodimops, operkind):
+        dimsliceval = 0
+
+    for cdn in dimsoper:
+        sliceaddvars[cdn] = dimsliceval
+
+# variable dimension
+    for vn in dimvn:
+        print "'" + vn + "' ... .. ."
+        if not objnc.variables.has_key(vn):
+            print errormsg
+            print '  ' + fname + ': netCDF file "' + ncfile +                        \
+              '" does not have variable "' + vn + '" !!'
+            quit(-1)
+
+        # Do we have to add this dimension-variable? (only that ones with same 
+        #   dimensions as desired variable)
+        ov = objnc.variables[vn]
+        toadd = False
+        for vd in ov.dimensions:
+            if gen.searchInlist(dimsoper, vd): toadd = True
+   
+        if toadd:
+            varinf = variable_inf(ov)
+            varslice, varsliceddims = SliceVarDict(ov,sliceaddvars)
+            varvals0 = ov[tuple(varslice)]
+            varvals = np.squeeze(varvals0)
+
+            print 'Lluis: variables to add:', varsliceddims
+
+            for idv in range(len(varvals.shape)):
+                if not gen.searchInlist(objnewnc.dimensions, varsliceddims[idv]):
+                    newdim = objnewnc.createDimension(varsliceddims[idv],            \
+                      varvals.shape[idv])
+
+            newvar = objnewnc.createVariable(vn, nctype(varinf.dtype),               \
+              tuple(varsliceddims))
+            newvar[:] = varvals
+
+            for attrn in varinf.attributes:
+                attrv = ov.getncattr(attrn)
+                newattr = set_attribute(newvar, attrn, attrv)
+#            LLUIS
             objnewnc.sync()
 
@@ -15665,5 +15640,5 @@
             self.vardxn = 'XLONG'
             self.vardyn = 'XLAT'
-            self.vardzn = 'DN'
+            self.vardzn = 'ZNU'
             self.vardtn = 'Times'
             if ncfile is not None and ncfile[0:2] == 'geo':
