Index: /trunk/tools/nc_var_tools.py
===================================================================
--- /trunk/tools/nc_var_tools.py	(revision 696)
+++ /trunk/tools/nc_var_tools.py	(revision 697)
@@ -17333,4 +17333,18 @@
         quit(-1)
 
+# Variable attributes
+    filemiss = False
+    ivarattrs = iovar.ncattrs()
+    if searchInlist(ivarattrs, 'missing_value'):
+        filemissv = iovar.getncattr('missing_value')
+        filemiss = True
+        print '  ' + fname + ' input data with missing value:',filemissv,'!!'
+
+    filefill = False
+    if searchInlist(ivarattrs, '_FillValue'): 
+        filefillv = iovar.getncattr('_FillValue')
+        filefill = True
+        print '  ' + fname + ' input data with fill value:',filefillv,'!!'
+
 # Getting shape of output variables for future average loop
 ##
@@ -17495,20 +17509,31 @@
         lonv, latv = lonlat2D(ilonv, ilatv)
 
-        ivarwgtv = np.abs(np.cos(latv*np.pi/180.))/(lonv.shape[0]*lonv.shape[1])
+        ivarwgtv = np.abs(np.cos(latv*np.pi/180.))
         TOTsumwgt = np.sum(ivarwgtv)
         if len(loopshape) == 1:
             newvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF
+            newsumvals = np.ones((loopshape[0]), dtype=np.float)*fillValueF
             for id1 in range(loopshape[0]):
                 slicevalues = SliceVar(iovar,dimsloop,[id1])
-                newvals[id1] = np.sum(ivarv[tuple(slicevalues)]*ivarwgtv)/TOTsumwgt
+                vals = ivarv[tuple(slicevalues)]
+                if filemiss: vals = np.where(vals == filemissv, 0., vals)
+                if filefill: vals = np.where(vals == filefillv, 0., vals)
+                newsumvals[id1] = np.sum(vals*ivarwgtv)
+                newvals[id1] = newsumvals[id1]/TOTsumwgt
         elif len(loopshape) == 2:
             newvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)*fillValueF
+            newsumvals = np.ones((loopshape[0],loopshape[1]), dtype=np.float)*fillValueF
             for id1 in range(loopshape[0]):
                 for id2 in range(loopshape[1]):
                     slicevalues = SliceVar(iovar,dimsloop,[id1,id2])
-                    newvals[id1,id2] = np.sum(ivarv[tuple(slicevalues)]*ivarwgtv) /  \
-                      TOTsumwgt
+                    vals = ivarv[tuple(slicevalues)]
+                    if filemiss: vals = np.where(vals == filemissv, 0., vals)
+                    if filefill: vals = np.where(vals == filefillv, 0., vals)
+                    newsumvals[id1,id2] = np.sum(vals*ivarwgtv)
+                    newvals[id1,id2] = newsumvals[id1,id2] / TOTsumwgt
         elif len(loopshape) == 3:
             newvals = np.ones((loopshape[0],loopshape[1],loopshape[2]),              \
+              dtype=np.float)*fillValueF
+            newsumvals = np.ones((loopshape[0],loopshape[1],loopshape[2]),           \
               dtype=np.float)*fillValueF
             for id1 in range(loopshape[0]):
@@ -17516,6 +17541,9 @@
                     for id3 in range(loopshape[2]):
                         slicevalues = SliceVar(iovar,dimsloop,[id1,id2,id3])
-                        newvals[id1,id2,id3] = np.sum(ivarv[tuple(slicevalues)] *    \
-                          ivarwgtv) / TOTsumwgt
+                        vals = ivarv[tuple(slicevalues)]
+                        if filemiss: vals = np.where(vals == filemissv, 0., vals)
+                        if filefill: vals = np.where(vals == filefillv, 0., vals)
+                        newsumvals[id1,id2,id3] = np.sum(vals*ivarwgtv)
+                        newvals[id1,id2,id3] = newsumvals[id1,id2,id3] / TOTsumwgt
         outweightvals = ivarwgtv
 
@@ -17534,4 +17562,12 @@
 # Output variable
 ##
+    newvar = onewnc.createVariable(varn + 'spaceweightsum', 'f4', tuple(dimsloop),   \
+      fill_value=fillValueF)
+    basicvardef(newvar, varn + 'spaceweightsum', 'space summed ' + varn + ' ' +      \
+      longvarname, iovar.getncattr('units'))
+    newvar[:] = newsumvals
+
+    onewnc.sync()
+
     newvar = onewnc.createVariable(varn + 'spaceweightmean', 'f4', tuple(dimsloop),  \
       fill_value=fillValueF)
@@ -17541,4 +17577,6 @@
 
     onewnc.sync()
+
+    print 'newsumvals:',newsumvals,'newvals:',newvals,'TOTsumwgt:',TOTsumwgt
 
 # Spatial weight
