Index: trunk/tools/nc_var_tools.py
===================================================================
--- trunk/tools/nc_var_tools.py	(revision 700)
+++ trunk/tools/nc_var_tools.py	(revision 701)
@@ -9854,13 +9854,31 @@
         typeinf = np.iinfo(np.int)
         if (abs(np.float64(val)) > typeinf.max):
+            print warnmsg
+            print '  ' + fname + ': value to transform ', val,' overpasses type:',   \
+               vtype,' limits!!'
+            print '  Changing value to kind largest allowed value:', typeinf.max
             newval = abs(np.float64(val))/np.float64(val) * np.int(typeinf.max)
+        else:
+            newval = np.int(val)
     elif vtype == type(np.int16(1)):
         typeinf = np.iinfo(np.int16)
         if (abs(np.float64(val)) > typeinf.max):
+            print warnmsg
+            print '  ' + fname + ': value to transform ', val,' overpasses type:',   \
+               vtype,' limits!!'
+            print '  Changing value to kind largest allowed value:', typeinf.max
             newval = abs(np.float64(val))/np.float64(val) * np.int16(typeinf.max)
+        else:
+            newval = np.int16(val)
     elif vtype == type(np.int32(1)):
         typeinf = np.iinfo(np.int32)
         if (abs(np.float64(val)) > typeinf.max):
+            print warnmsg
+            print '  ' + fname + ': value to transform ', val,' overpasses type:',   \
+               vtype,' limits!!'
+            print '  Changing value to kind largest allowed value:', typeinf.max
             newval = abs(np.float64(val))/np.float64(val) * np.int32(typeinf.max)
+        else:
+            newval = np.int32(val)
     elif vtype == type(np.int64(1)):
         newval = np.int64(val)
@@ -13612,4 +13630,75 @@
 #netcdf_concatenation('mean_pluc.nc@all|mean_dtcon.nc@all')
 
+def count_cond(var, val, con):
+    """ Function to count values of a variable which attain a condition
+      [var]= variable
+      [val]= value
+      [con]= statistics/condition
+        'eq': equal than [val]
+        'neq': not equal than [val]
+        'lt': less than [val]
+        'le': less and equal than [val]
+        'gt': greater than [val]
+        'ge': greater and equal than [val]
+        'in': inside the range [val[0]] <= [var] <= [val[1]] (where [val]=[val1, val2]
+        'out': inside the range [val[0]] > [var]; [val[1]] < [var] (where [val]=[val1, val2]
+      >>> vals = np.arange(100)
+      >>> count_cond(vals,50,'eq')
+      1
+      >>> count_cond(vals,50,'neq')
+      99
+      >>> count_cond(vals,50,'lt')
+      50
+      >>> count_cond(vals,50,'le')
+      51
+      >>> count_cond(vals,50,'gt')
+      49
+      >>> count_cond(vals,50,'ge')
+      50
+      >>> count_cond(vals,[25,75],'in')
+      51
+      >>> count_cond(vals,[25,75],'out')
+      49
+    """
+    fname = 'count_cond'
+
+    cons = ['eq', 'neq', 'lt', 'le', 'gt', 'ge', 'in', 'out']
+
+    if con == 'eq':
+        Nvals = np.sum(var == val)
+    elif con == 'neq':
+        Nvals = np.sum(var != val)
+    elif con == 'lt':
+        Nvals = np.sum(var < val)
+    elif con == 'le':
+        Nvals = np.sum(var <= val)
+    elif con == 'gt':
+        Nvals = np.sum(var > val)
+    elif con == 'ge':
+        Nvals = np.sum(var >= val)
+    elif con == 'in':
+        if len(val) != 2:
+            print errormsg
+            print '  ' + fname + ": for condition '" + con + "' we must have two " + \
+              " values!!" 
+            print '  we have:',  val
+            quit(-1)
+        Nvals = np.sum((var >= val[0])*(var <= val[1]))
+    elif con == 'out':
+        if len(val) != 2:
+            print errormsg
+            print '  ' + fname + ": for condition '" + con + "' we must have two " + \
+              " values!!" 
+            print '  we have:',  val
+            quit(-1)
+        Nvals = np.sum((var < val[0])+(var > val[1]))
+    else:
+        print errormsg
+        print '  ' + fname + ": condition '" + con + "' not ready!!"
+        print '  only ready:', cons
+        quit(-1)
+
+    return Nvals
+
 def field_stats(values, ncfile, varn):
     """ Function to retrieve statistics from a field
@@ -13619,4 +13708,5 @@
           'full': all statistics given variable
         [fillVals]: ':' list of _fillValues ('None' for any)
+        [countVals]: ':' list of Values@cond to use to count conditions ('None' for any)
       [ncfile]= name of the netCDF file to use
       [varn]= variable name to use ('all' for all variables)
@@ -13631,10 +13721,12 @@
 
 
-    arguments = '[stats],[fillVals]'
+    arguments = '[stats],[fillVals],[coutnVals]'
     check_arguments(fname,values,arguments,',')
 
     stats=values.split(',')[0]
     fillVals=values.split(',')[1]
-
+    countVals=values.split(',')[2]
+
+# Fill Values
     if fillVals == 'None':
         fillV = None
@@ -13646,4 +13738,15 @@
             fillV = [fillVals]
             NfillV = 1
+
+# Count Values
+    if countVals == 'None':
+        countV = None
+    else:
+        if countVals.find(':') != -1:
+            countV = countVals.split(':')
+            NcountV = len(countV)
+        else:
+            countV = [countVals]
+            NcountV = 1
 
     ncobj = NetCDFFile(ncfile, 'r')
@@ -13665,8 +13768,15 @@
         field = objfield[:]
         dtype = objfield.dtype
-        print 'Lluis dtype:',dtype,'fillVals:',fillV
+
+        counts = {}
+        if countVals is not None:
+            for icV in range(NcountV):
+                cV = countV[icV].split('@')[0]
+                cC = countV[icV].split('@')[1]
+                countval = retype(cV, dtype)
+                counts[countV[icV]] = [count_cond(field, countval, cC), cC]
+
         if fillVals is not None:
             for ifV in range(NfillV):
-                print 'fV:', fillV[ifV]
                 fillval = retype(fillV[ifV], dtype)
                 field = ma.masked_equal(field, fillval)
@@ -13686,4 +13796,8 @@
             print '    Fstats mean2:', mean2v
             print '    Fstats variability:', varv
+            if countVals is not None:
+                for icV in range(NcountV):
+                    print '    ' + fname + " N values '" + countV[icV][1] + "'",     \
+                      countV[icV][0], ':', counts[countV[icV]]
         else:
             print errormsg
