Index: trunk/SRC/Interpolation/file_interp.pro
===================================================================
--- trunk/SRC/Interpolation/file_interp.pro	(revision 496)
+++ trunk/SRC/Interpolation/file_interp.pro	(revision 497)
@@ -4,5 +4,5 @@
 ;
 ;-
-FUNCTION boxmean_interp, data, divx, divy
+FUNCTION boxmean_interp, data, divx, divy, nankeep = nankeep
  ;
   compile_opt idl2, strictarrsubs
@@ -11,9 +11,15 @@
   jpiout = sz[0] / divx
   jpjout = sz[1] / divy
+  IF keyword_set(nankeep) THEN divi = finite(data)
 ;
   data = reform(data, divx, jpiout, divy, jpjout, /overwrite)
-  data = total(temporary(data), 1)   ; ave along divx
-  data = total(temporary(data), 2)   ; ave along divy
-  data = temporary(data) / float(divx*divy)
+  data = total(temporary(data), 1, nan = nankeep)   ; ave along divx
+  data = total(temporary(data), 2, nan = nankeep)   ; ave along divy
+  IF keyword_set(nankeep) THEN BEGIN
+    divi = reform(temporary(divi), divx, jpiout, divy, jpjout, /overwrite)
+    divi = total(temporary(divi), 1, /nan) ; ave along divx
+    divi = total(temporary(divi), 2, /nan) ; ave along divy
+    data = temporary(data) / temporary(divi)
+  ENDIF ELSE data = temporary(data) / float(divx*divy)
 
   RETURN, data
@@ -31,5 +37,5 @@
                         , DATA_SMWIN = data_smwin, DATA_NSMOOTH = data_nsmooth $
                         , EXTRAP_SMWIN = extrap_smwin, EXTRAP_NSMOOTH = extrap_nsmooth $
-                        , _EXTRA = ex
+                        , NANKEEP = nankeep, _EXTRA = ex
 ;
   compile_opt idl2, strictarrsubs
@@ -48,5 +54,5 @@
   IF totnanmask NE n_elements(nanmask) THEN BEGIN
     data[where(nanmask EQ 0b)] = 1.e20 ; put large value to be sure they are removed during the interpolation
-    IF inmask[0] NE -1 THEN mask = temporary(nanmask) * inmask ELSE mask = temporary(nanmask)
+    IF inmask[0] NE -1 THEN mask = nanmask * inmask ELSE mask = nanmask
   ENDIF ELSE mask = inmask
 ; take care of missing values
@@ -60,4 +66,5 @@
     ENDCASE
     IF total(missmask) EQ 0 THEN return, missing_value
+    IF keyword_set(nankeep) THEN nanmask = temporary(nanmask) * missmask
     IF mask[0] NE -1 THEN mask = temporary(missmask) * mask ELSE mask = temporary(missmask)
   ENDIF
@@ -87,7 +94,13 @@
     data = (temporary(data))[data_smwin:nx+data_smwin-1, data_smwin:ny+data_smwin-1]
   ENDIF
+; put back NaN if wanted...
+  IF keyword_set(nankeep) THEN BEGIN
+    nanmask = where(nanmask EQ 0b, cntnan)
+    IF cntnan NE 0 THEN data[temporary(nanmask)] = !values.f_nan
+  ENDIF
 ; interpolation
   IF method EQ 'boxmean' THEN BEGIN
-    data = boxmean_interp(temporary(data), divx, divy)
+; put back NaN if wanted...
+    data = boxmean_interp(temporary(data), divx, divy, nankeep = nankeep)
   ENDIF ELSE BEGIN
     IF NOT keyword_set(inirr) THEN BEGIN
@@ -298,4 +311,8 @@
 ; @keyword _EXTRA
 ; to use <pro>extrapolate</pro>, <pro>extrapsmooth</pro> and <pro>fromreg</pro> keywords
+;
+; @keyword NANKEEP {default=0}{type=scalar: 0 or 1}
+; switch on to keep NaN values before doing the interpolation. Note
+; that NaN values are not taken into account when doing bowmean method
 ;
 ; @uses
@@ -342,5 +359,5 @@
                  , DATA_SMWIN = data_smwin, DATA_NSMOOTH = data_nsmooth $
                  , EXTRAP_SMWIN = extrap_smwin, EXTRAP_NSMOOTH = extrap_nsmooth $
-                 , _EXTRA = ex
+                 , NANKEEP = nankeep, _EXTRA = ex
 ;
   compile_opt idl2, strictarrsubs
@@ -586,5 +603,5 @@
                                                   , DATA_SMWIN = data_smwin, DATA_NSMOOTH = data_nsmooth $
                                                   , EXTRAP_SMWIN = extrap_smwin, EXTRAP_NSMOOTH = extrap_nsmooth $
-                                                  , _extra = ex)
+                                                  , NANKEEP = nankeep, _extra = ex)
               IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
               ncdf_varput, outid, outvarid[i], temporary(data)
@@ -609,5 +626,5 @@
                                                     , DATA_SMWIN = data_smwin, DATA_NSMOOTH = data_nsmooth $
                                                     , EXTRAP_SMWIN = extrap_smwin, EXTRAP_NSMOOTH = extrap_nsmooth $
-                                                    , _extra = ex)
+                                                    , NANKEEP = nankeep, _extra = ex)
                 IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
                 ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
@@ -633,5 +650,5 @@
                                                       , DATA_SMWIN = data_smwin, DATA_NSMOOTH = data_nsmooth $
                                                       , EXTRAP_SMWIN = extrap_smwin, EXTRAP_NSMOOTH = extrap_nsmooth $
-                                                      , _extra = ex)
+                                                      , NANKEEP = nankeep, _extra = ex)
                   IF interp AND n_elements(data) EQ 1 THEN data = replicate(data, jpiout, jpjout)
                   ncdf_varput, outid, outvarid[i], temporary(data), offset = off, count = outcnt
