Index: trunk/SRC/ReadWrite/ncdf_getmask.pro
===================================================================
--- trunk/SRC/ReadWrite/ncdf_getmask.pro	(revision 327)
+++ trunk/SRC/ReadWrite/ncdf_getmask.pro	(revision 341)
@@ -27,7 +27,7 @@
 ;
 ; @keyword MISSING_VALUE {type=scalar}
-; To define (or redefine if the attribute is
-; already existing) the missing values used with USEASMASK
-; keyword
+; To define (or redefine if the attribute is already existing) the
+; missing values used with USEASMASK keyword. Note that this value is
+; not used if TESTOP keyword is given and contains 2 words.  
 ;
 ; @keyword USEASMASK {type=scalar string}
@@ -35,8 +35,22 @@
 ; that will be used to build the land/sea mask. In this case the
 ; mask is based on the first record (if record dimension
-; exists). The mask is build according to :
-;    1 the keyword missing_value if existing
-;    2 the attribute 'missing_value' if existing
-;    3 NaN values if existing
+; exists). The mask is build according to operator defined by TESTOP
+; keyword (default NE) and the testing values defined as 
+;   1) the second word of TESTOP if existing
+;   2) MISSING_VALUE keyword
+;   3) attribute missing_value or _fillvalue of the variable USEASMASK
+;   4) !Values.f_nan (can be used only with NE and EQ operators)
+;
+; @keyword TESTOP {default='NE'} {type=scalar string, for example 'GT 0.5'}
+; a string describing the type of test that will be done to define the
+; mask. The test is performed on the variable specified by USEASMASK
+; keyword.
+; TESTOP can contain 1 or 2 words. The first word is the operator
+; definition: "EQ" "NE" "GE" "GT" "LE" "LT" (default is NE). The
+; second word define the testing value. If TESTOP contains only 1
+; word, then the test value is denifed by
+;   1) MISSING_VALUE keyword
+;   2) attribute missing_value or _fillvalue of the variable USEASMASK
+;   3) !Values.f_nan (can be used only with NE and EQ operators)
 ;
 ; @keyword 
@@ -52,4 +66,6 @@
 ; IDL> mask = ncdf_getmask('meshmaskORCA2.nc', maskname = 'tmask')
 ;
+; IDL> mask = ncdf_getmask('t106.nc', useasmask = 'SLM', testop = 'le 0.5')
+;
 ; @history
 ; August 2007: Sebastien Masson (smasson\@lodyc.jussieu.fr)
@@ -59,8 +75,8 @@
 ;
 ;-
-FUNCTION ncdf_getmask, fileid, ADDSCL_BEFORE=addscl_before $
-                     , MASKNAME=maskname, USEASMASK=useasmask $
-                     , MISSING_VALUE=missing_value, INVMASK=invmask $
-                     , _EXTRA=ex
+FUNCTION ncdf_getmask, fileid, ADDSCL_BEFORE = addscl_before $
+                       , MASKNAME = maskname, USEASMASK = useasmask $
+                       , MISSING_VALUE = missing_value, INVMASK = invmask $
+                       , TESTOP = testop, _EXTRA = ex
 ;
   compile_opt idl2, strictarrsubs
@@ -102,8 +118,8 @@
       ncdf_varget, cdfid, mskid, mask, count = count
     ENDIF ELSE ncdf_varget, cdfid, mskid, mask
-; check if we need to applay add_offset and scale factor
+
+; get add_offset, scale factor and missing value attributes
     ncdf_getatt, cdfid, mskid, add_offset = add, scale_factor = scl, missing_value = miss
-    IF n_elements(missing_value) NE 0 THEN miss = missing_value
-
+; do we apply add_offset and scale factor ?
     IF keyword_set(addscl_before) THEN BEGIN
       IF scl NE 1 THEN mask = mask * scl
@@ -112,24 +128,42 @@
 
     IF keyword_set(useasmask)  THEN BEGIN
-      IF n_elements(miss) NE 0 THEN BEGIN
+
+      IF n_elements(missing_value) NE 0 THEN miss = missing_value
+      IF size(miss, /type) EQ 7 THEN miss = !values.f_nan
+
+      IF NOT keyword_set(testop) THEN testop = 'NE'
+      tmp = strsplit(testop, ' ', /extract)
+      op = strupcase(tmp[0])
+      IF op EQ 'EQ' THEN BEGIN
+        op = 'NE'
+        invmask = 1b - keyword_set(invmask) 
+      ENDIF
+      IF n_elements(tmp) EQ 1 THEN testval = miss ELSE testval = float(tmp[1]) 
+      IF finite(testval) EQ 0 THEN BEGIN
+        IF op NE 'NE' THEN mask = report('NaN test value can be used only with EQ or NE operator') ELSE mask = finite(mask)
+      ENDIF ELSE BEGIN
+        CASE op OF
+          'GE':mask = mask GE testval
+          'GT':mask = mask GT testval
+          'LE':mask = mask LE testval
+          'LT':mask = mask LT testval
+          'NE':BEGIN 
 ; we have to take care of the float accuracy
-        CASE 1 OF
-          miss GE 1.e6:mask = mask LT (miss-10)
-          miss LE -1.e6:mask = mask GT (miss+10)
-          abs(miss) LE 1.e-6:mask = abs(mask) GT 1.e-6
-          ELSE:mask = mask NE miss
+            CASE 1 OF
+              testval GE  1.e6:mask = mask LT (testval - 10)
+              testval LE -1.e6:mask = mask GT (testval + 10)
+              abs(testval) LE 1.e-6:mask = abs(mask) GT 1.e-6
+              ELSE:mask = mask NE testval
+            ENDCASE
+          END
         ENDCASE
-      ENDIF ELSE BEGIN
-        mask = finite(mask)
-        IF min(mask) EQ 1 THEN BEGIN
-          ras = report( 'missing or nan values not found...')
-          mask = -1
-        ENDIF
       ENDELSE
+
+      IF mask[0] NE -1 THEN BEGIN 
+        mask = byte(round(mask))
+        if keyword_set(invmask) then mask = 1b-mask
+      ENDIF
+
     ENDIF
-
-    mask = byte(round(mask))
-    if keyword_set(invmask) then mask = 1b-mask
-
   ENDIF ELSE mask = -1
 
