Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 671)
+++ /trunk/tools/nc_var_tools.py	(revision 672)
@@ -17172,5 +17172,5 @@
     fname = 'varDimension'
 
-    if not oncf.dimensions._has_key(dname):
+    if not oncf.dimensions.has_key(dname):
         print errormsg
         print '  ' + fname + ": NetCDF object does not have dimension '" + dname + "' !!"
@@ -17188,5 +17188,5 @@
     else:
         print warnmsg
-        print '  ' + fname ": Any  1D variables with dimension '" + dname + "' !!"
+        print '  ' + fname + ": Any  1D variables with dimension '" + dname + "' !!"
 
 # Dpouble precision
@@ -17196,11 +17196,18 @@
     if len(d1var) > 1:
         d1vardouble = []
-        for vn in d1var
+        for vn in d1var:
             ovn = oncf.variables[vn]
-            if ovn.dtype == np.float64(1.): d1vardouble.append(vn)
-        if len(d1vardouble) != 1:
+            if ovn.dtype == type(np.float64(1.)): d1vardouble.append(vn)
+        if len(d1vardouble) > 1:
             print errormsg
-            print '  ' + fname + ": More than 1D double variable with dimension '" + dname + "' !!"
+            print '  ' + fname + ": Found more than 1D double variable with " +      \
+              "dimension '" + dname + "' !!"
+            print '    found:', d1vardouble
             quit(-1)
+        elif len(d1vardouble) == 0:
+            print warnmsg
+            print '  ' + fname + ": Found any 1D double variable with dimension '" + \
+              dname + "' !!"
+            dimvarname = None
         else:
             dimvarname = d1vardouble[0]
@@ -17210,6 +17217,7 @@
     return dimvarname
 
-def ovar_onc(ovar, oncf):
+def ovar_onc(incf, ovar, oncf):
     """ Function to copy an object variable to a nother netcdf object
+      incf: input netCDF object
       ovar= variable object
       oncf= netCDF object
@@ -17217,14 +17225,29 @@
     fname = 'ovar_onc'
 
-    varname = ovar.name
-    varadims = ovar.dimensions
+    varname = ovar._name
+
+    if oncf.variables.has_key(varname):
+        print warnmsg
+        print '  ' + fname + ": netCDF object already has variable '" + ovar.name +  \
+          "' !!"
+        print '    doing noting'
+        return
+
+    vardims = ovar.dimensions
     vartype = ovar.dtype
+
+    for dn in vardims:
+        if not searchInlist(oncf.dimensions, dn):
+            if incf.dimensions[dn].isunlimited():
+                newdim = oncf.createDimension(dn, None)
+            else:
+                newdim = oncf.createDimension(dn, len(incf.dimensions[dn]))
  
     newvar = oncf.createVariable(varname, vartype, vardims)
-    newvar = ovar[:]
-
-    for ncatr in ovar.ncattrs():
-        ncatrv = ovar.get_ncattribute(ncattr)
-        set_ncatrribute(newvar, ncatr, ncatrv)
+    newvar[:] = ovar[:]
+
+    for attrn in ovar.ncattrs():
+        attrv = ovar.getncattr(attrn)
+        set_attribute(newvar, attrn, attrv)
 
     oncf.sync()
@@ -17234,5 +17257,5 @@
 def SpatialWeightedMean(values, filen, varn):
     """ Function to compute the spatial mean using weights from a netCDF file
-      values= [weightskind],[xdimname],[ydimname]
+      values= [weightskind],[xdimname],[ydimname],[addvars]
         [weightskind]: type of weights:
           * 'varnames',[varname],[oper]: using a variable [varname] with an operation [oper] as area size
@@ -17245,4 +17268,5 @@
         [xdimname]: name of the dimension for the x-axis
         [ydimname]: name of the dimension for the y-axis
+        [addvars]: ':', separetd list of name of variables to add to the output file 
       filen= name of the netCDF file
       varn= name of the variable
@@ -17260,9 +17284,10 @@
     xdimname = values.split(',')[3]
     ydimname = values.split(',')[4]
+    addvars = values.split(',')[5]
 
     ofile = 'SpatialWeightedMean_' + weightk + '.nc'
 
     if weightk == 'varnames':
-        arguments = '[weightk],[varname],[oper],[xdimname],[ydimname]'
+        arguments = '[weightk],[varname],[oper],[xdimname],[ydimname],[addvars]'
         check_arguments(fname, len(values.split(',')), arguments, ',',               \
           len(arguments.split(',')))
@@ -17498,7 +17523,6 @@
         vardim = varDimension(onc, dim)
         if vardim is not None:
-            ovar_nc(onc.variables[vardim], onewnc)
+            ovar_onc(onc, onc.variables[vardim], onewnc)
             
-
 # Output variable
 ##
@@ -17508,4 +17532,6 @@
       longvarname, iovar.getncattr('units'))
     newvar[:] = newvals
+
+    onewnc.sync()
 
 # Spatial weight
@@ -17517,4 +17543,17 @@
     basicvardef(newvar, 'spatialweight', 'space weight ' + longvarname, '-')
     newvar[:] = outweightvals
+
+    onewnc.sync()
+
+# Additional variables
+##
+    for vn in addvars.split(':'):
+        if not onc.variables.has_key(vn):
+            print errormsg
+            print '  ' + fname + ": file '" + filen + "' does not have variable '" + \
+              vn + "' !!"
+            quit(-1)
+        ovar_onc(onc, onc.variables[vn], onewnc)
+    onewnc.sync()
 
 # Global attributes
@@ -17531,4 +17570,7 @@
 
     onc.close()
+    onewnc.sync()
+    onewnc.close()
+
 
     print fname + ": Successfull written of file: '" + ofile + "' !!"
