Index: trunk/SRC/Grid/smallmeshmask.pro
===================================================================
--- trunk/SRC/Grid/smallmeshmask.pro	(revision 371)
+++ trunk/SRC/Grid/smallmeshmask.pro	(revision 451)
@@ -108,5 +108,24 @@
   zgr_varlist = ncdf_listvars(cdfid)
   zgr_varlist = strtrim(strlowcase(zgr_varlist), 2)
-;------------------------------------------------------
+; is e3t a real 3D array?
+  IF (where(zgr_varlist EQ 'e3t'))[0] NE -1 THEN BEGIN 
+    varinq = ncdf_varinq(cdfid, 'e3t')
+    if varinq.ndims GE 3 THEN BEGIN
+      ncdf_diminq, cdfid, varinq.dim[0], name, iii
+      ncdf_diminq, cdfid, varinq.dim[1], name, jjj
+      ncdf_diminq, cdfid, varinq.dim[2], name, kkk
+      IF iii EQ jpi AND jjj EQ jpj AND kkk EQ jpk THEN key_e3_3d = 1
+    ENDIF   
+  ENDIF
+; is gdept a real 3D array?
+  IF (where(zgr_varlist EQ 'gdept'))[0] NE -1 THEN BEGIN 
+    varinq = ncdf_varinq(cdfid, 'gdept')
+    if varinq.ndims GE 3 THEN BEGIN
+      ncdf_diminq, cdfid, varinq.dim[0], name, iii
+      ncdf_diminq, cdfid, varinq.dim[1], name, jjj
+      ncdf_diminq, cdfid, varinq.dim[2], name, kkk
+      IF iii EQ jpi AND jjj EQ jpj AND kkk EQ jpk THEN key_gdep_3d = 1
+    ENDIF   
+  ENDIF
 ;------------------------------------------------------
 ;
@@ -139,23 +158,14 @@
     varid[16+z] = ncdf_vardef(cdfidout, zgrlist[z], [dimidz], /float)
 ; variables related to the partial steps
-  IF (where(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $
+  with_partial = keyword_set(key_e3_3d) OR (where(zgr_varlist EQ 'hdept'))[0] NE -1 $
+                 OR (where(zgr_varlist EQ 'e3tp'))[0] NE -1 $
+                 OR (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1
+  IF with_partial THEN BEGIN
     varid = [varid, ncdf_vardef(cdfidout, 'hdept', [dimidx, dimidy], /float)]
-  IF (where(zgr_varlist EQ 'hdepw'))[0] NE -1 THEN $
     varid = [varid, ncdf_vardef(cdfidout, 'hdepw', [dimidx, dimidy], /float)]
-; old variable name. keep for compatibility with old run. Change e3tp to e3t_ps
-  IF (where(zgr_varlist EQ 'e3tp'))[0] NE -1 THEN $
     varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)]
-; old variable name. keep for compatibility with old run. Change e3wp to e3w_ps
-  IF (where(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $
     varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)]
-;
-  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $
-    varid = [varid, ncdf_vardef(cdfidout, 'e3t_ps', [dimidx, dimidy], /float)]
-  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $
-    varid = [varid, ncdf_vardef(cdfidout, 'e3w_ps', [dimidx, dimidy], /float)]
-;   IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $
-;     varid = [varid, ncdf_vardef(cdfidout, 'e3u_ps', [dimidx, dimidy], /float)]
-;   IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $
-;     varid = [varid, ncdf_vardef(cdfidout, 'e3v_ps', [dimidx, dimidy], /float)]
+  ENDIF
+;
   IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $
     varid = [varid, ncdf_vardef(cdfidout, 'mbathy', [dimidx, dimidy], /short)]
@@ -163,6 +173,5 @@
   msklist = ['tmask', 'umask', 'vmask', 'fmask']
   FOR m = 0, n_elements(msklist)-1 DO $
-    varid = [varid, ncdf_vardef(cdfidout, msklist[m] $
-                                , [dimidx, dimidy, dimidz], /byte)]
+    varid = [varid, ncdf_vardef(cdfidout, msklist[m], [dimidx, dimidy, dimidz], /byte)]
 ;------------------------------------------------------
 ;------------------------------------------------------
@@ -189,25 +198,60 @@
   inzgrlist =  zgrlist
   IF (where(zgr_varlist EQ 'gdept_0'))[0] NE -1 THEN inzgrlist =  inzgrlist+'_0'
-  FOR z = 0, n_elements(zgrlist)-1 DO $
-    ncdf_transfer, cdfid, cdfidout, inzgrlist[z], zgrlist[z]
+  FOR z = 0, n_elements(zgrlist)-1 DO ncdf_transfer, cdfid, cdfidout, inzgrlist[z], zgrlist[z]
 ; partial step variables
-  IF (where(zgr_varlist EQ 'hdept'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'hdept'
-  IF (where(zgr_varlist EQ 'hdepw'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'hdepw'
-  IF (where(zgr_varlist EQ 'e3tp'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'e3tp', 'e3t_ps'
-  IF (where(zgr_varlist EQ 'e3wp'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'e3wp', 'e3w_ps'
-  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'e3t_ps'
-  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'e3w_ps'
-;   IF (where(zgr_varlist EQ 'e3u_ps'))[0] NE -1 THEN $
-;     ncdf_transfer, cdfid, cdfidout, 'e3u_ps'
-;   IF (where(zgr_varlist EQ 'e3v_ps'))[0] NE -1 THEN $
-;     ncdf_transfer, cdfid, cdfidout, 'e3v_ps'
-  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN $
-    ncdf_transfer, cdfid, cdfidout, 'mbathy'
+  IF (where(zgr_varlist EQ 'hdept' ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'hdept'
+  IF (where(zgr_varlist EQ 'hdepw' ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'hdepw'
+  IF (where(zgr_varlist EQ 'e3tp'  ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3tp', 'e3t_ps'
+  IF (where(zgr_varlist EQ 'e3wp'  ))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3wp', 'e3w_ps'
+  IF (where(zgr_varlist EQ 'e3t_ps'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3t_ps'
+  IF (where(zgr_varlist EQ 'e3w_ps'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'e3w_ps'
+  IF (where(zgr_varlist EQ 'mbathy'))[0] NE -1 THEN ncdf_transfer, cdfid, cdfidout, 'mbathy'
+;
+  IF keyword_set(key_e3_3d)  THEN BEGIN
+; get the bottom
+    CASE (ncdf_varinq(cdfid, 'mbathy')).ndims OF
+      2:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj]
+      3:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1]
+      4:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1, 1]
+    ENDCASE
+    bottom = 0L > ( long(bat) - 1L )
+    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
+;
+    CASE (ncdf_varinq(cdfid, 'e3t')).ndims OF
+      3:ncdf_varget, cdfid, 'e3t', res, count = [jpi, jpj, jpk]
+      4:ncdf_varget, cdfid, 'e3t', res, count = [jpi, jpj, jpk, 1]
+    ENDCASE
+    ncdf_varput, cdfidout, 'e3t_ps', (temporary(res))[bottom] * (bat NE 0L)
+;
+    CASE (ncdf_varinq(cdfid, 'e3w')).ndims OF
+      3:ncdf_varget, cdfid, 'e3w', res, count = [jpi, jpj, jpk]
+      4:ncdf_varget, cdfid, 'e3w', res, count = [jpi, jpj, jpk, 1]
+    ENDCASE
+    ncdf_varput, cdfidout, 'e3w_ps', (temporary(res))[bottom] * (bat NE 0L)
+  ENDIF
+;
+  IF keyword_set(key_gdep_3d)  THEN BEGIN
+; get the bottom
+    CASE (ncdf_varinq(cdfid, 'mbathy')).ndims OF
+      2:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj]
+      3:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1]
+      4:ncdf_varget, cdfid, 'mbathy', bat, count = [jpi, jpj, 1, 1]
+    ENDCASE
+    bottom = 0L > ( long(bat) - 1L )
+    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
+;
+    CASE (ncdf_varinq(cdfid, 'gdept')).ndims OF
+      3:ncdf_varget, cdfid, 'gdept', res, count = [jpi, jpj, jpk]
+      4:ncdf_varget, cdfid, 'gdept', res, count = [jpi, jpj, jpk, 1]
+    ENDCASE
+    ncdf_varput, cdfidout, 'hdept', (temporary(res))[bottom] * (bat NE 0L)
+;
+    bottom = jpi*jpj + temporary(bottom)
+    CASE (ncdf_varinq(cdfid, 'gdepw')).ndims OF
+      3:ncdf_varget, cdfid, 'gdepw', res, count = [jpi, jpj, jpk]
+      4:ncdf_varget, cdfid, 'gdepw', res, count = [jpi, jpj, jpk, 1]
+    ENDCASE
+    ncdf_varput, cdfidout, 'hdepw', (temporary(res))[bottom] * (bat NE 0L)
+  ENDIF
 ;
 ; mask
@@ -221,11 +265,8 @@
     FOR m = 0, 3 DO BEGIN
       CASE (ncdf_varinq(cdfid, msklist[m])).ndims OF
-        3:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k] $
-        , count = [jpi, jpj, 1]
-        4:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k, 0] $
-        , count = [jpi, jpj, 1, 1]
+        3:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k   ], count = [jpi, jpj, 1]
+        4:ncdf_varget, cdfid, msklist[m], zzz, offset = [0, 0, k, 0], count = [jpi, jpj, 1, 1]
       ENDCASE
-      ncdf_varput, cdfidout, msklist[m], byte(temporary(zzz)) $
-        , offset = [0, 0, k], count = [jpi, jpj, 1]
+      ncdf_varput, cdfidout, msklist[m], byte(temporary(zzz)), offset = [0, 0, k], count = [jpi, jpj, 1]
     ENDFOR
   ENDFOR
