Index: /trunk/SRC/Commons/cm_4mesh.pro
===================================================================
--- /trunk/SRC/Commons/cm_4mesh.pro	(revision 450)
+++ /trunk/SRC/Commons/cm_4mesh.pro	(revision 451)
@@ -26,5 +26,5 @@
 ; not used (as far as I know...) COMMON coriolis, ff
 COMMON vertical, gdept, gdepw, e3t, e3w
-COMMON partial_steps, hdept, hdepw, e3t_ps, e3w_ps, e3u_ps, e3v_ps
+COMMON partial_steps, hdept, hdepw, e3t_ps, e3w_ps
 COMMON keys_grid, key_shift, key_periodic, key_stride, key_gridtype, key_yreverse, key_zreverse, key_partialstep, key_onearth
 ;
Index: /trunk/SRC/Grid/micromeshmask.pro
===================================================================
--- /trunk/SRC/Grid/micromeshmask.pro	(revision 450)
+++ /trunk/SRC/Grid/micromeshmask.pro	(revision 451)
@@ -103,4 +103,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
 ;------------------------------------------------------
 ;------------------------------------------------------
@@ -134,23 +154,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)]
@@ -191,22 +202,58 @@
     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
Index: /trunk/SRC/Grid/ncdf_meshread.pro
===================================================================
--- /trunk/SRC/Grid/ncdf_meshread.pro	(revision 450)
+++ /trunk/SRC/Grid/ncdf_meshread.pro	(revision 451)
@@ -281,28 +281,40 @@
 ; for the variables related to the partial steps
   allvarname =  ncdf_listvars(cdfid)
+;
+  key_partialstep = 0
+  hdept = -1
+  hdepw = -1
   IF (where(allvarname EQ 'hdept'))[0] NE -1 THEN BEGIN
     key_partialstep = 1
     namevar = [namevar, 'hdept', 'hdepw']
-  ENDIF ELSE BEGIN
-    key_partialstep = 0
-    hdept = -1
-    hdepw = -1
-  ENDELSE
+  ENDIF
+; is gdept a real 3D array?
+  IF (where(allvarname 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 jpiglo AND jjj EQ jpjglo AND kkk EQ jpkglo THEN key_gdep_3d = 1
+    ENDIF   
+  ENDIF   
 ; for compatibility with old versions of meshmask/partial steps
-  IF (where(allvarname EQ 'e3tp'))[0] NE -1 THEN $
-    namevar = [namevar, 'e3tp', 'e3wp'] ELSE BEGIN
-    e3t_ps = -1
-    e3w_ps = -1
-  ENDELSE
-  IF (where(allvarname EQ 'e3t_ps'))[0] NE -1 $
-  THEN namevar = [namevar, 'e3t_ps', 'e3w_ps' ]ELSE BEGIN
-    e3t_ps = -1
-    e3w_ps = -1
-  ENDELSE
-  IF (where(allvarname EQ 'e3u_ps'))[0] NE -1 $
-  THEN namevar = [namevar, 'e3u_ps', 'e3v_ps'] ELSE BEGIN
-    e3u_ps = -1
-    e3v_ps = -1
-  ENDELSE
+  e3t_ps = -1
+  e3w_ps = -1
+  IF (where(allvarname EQ 'e3tp'  ))[0] NE -1 THEN namevar = [namevar, 'e3tp', 'e3wp']
+  IF (where(allvarname EQ 'e3t_ps'))[0] NE -1 THEN namevar = [namevar, 'e3t_ps', 'e3w_ps' ]
+; is e3t a real 3D array?
+  IF (where(allvarname 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 jpiglo AND jjj EQ jpjglo AND kkk EQ jpkglo THEN BEGIN
+        key_e3_3d = 1
+        key_partialstep = 1
+      ENDIF
+    ENDIF   
+  ENDIF
 ;
 ; read all the 2d variables
@@ -312,5 +324,6 @@
     name = varinq.name
 @read_ncdf_varget
-    command = namevar[i]+'=float(temporary(res))'
+    IF strmid(name, 0, 2) EQ 'e3' THEN command = namevar[i]+' = temporary(res)' $
+    ELSE command = namevar[i]+' = float(temporary(res))'
     nothing = execute(command)
   ENDFOR
@@ -403,4 +416,35 @@
     tmask = byte(res)
   ENDELSE
+; if e3t is true 3D array...
+  IF keyword_set(key_e3_3d)  THEN BEGIN
+    bottom = 0 > ( total(tmask, 3)-1 )
+    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
+;
+    varinq = ncdf_varinq(cdfid, 'e3t')
+    name = varinq.name
+@read_ncdf_varget
+    e3t_ps = (temporary(res))[bottom] * tmask[*, *, 0]
+;
+    varinq = ncdf_varinq(cdfid, 'e3w')
+    name = varinq.name
+@read_ncdf_varget
+    e3w_ps = (temporary(res))[bottom] * tmask[*, *, 0]
+  ENDIF
+; if gdep is true 3D array...
+  IF keyword_set(key_gdep_3d) THEN BEGIN
+    bottom = 0 > ( total(tmask, 3)-1 )
+    bottom = lindgen(jpi, jpj) + jpi*jpj*temporary(bottom)
+;
+    varinq = ncdf_varinq(cdfid, 'gdept')
+    name = varinq.name
+@read_ncdf_varget
+    hdept = (temporary(res))[bottom] * tmask[*, *, 0]
+;
+    bottom = jpi*jpj + temporary(bottom)
+    varinq = ncdf_varinq(cdfid, 'gdepw')
+    name = varinq.name
+@read_ncdf_varget
+    hdepw = (temporary(res))[bottom] * tmask[*, *, 0]
+  ENDIF
 ; boundary conditions used to compute umask.
   IF ncdf_varid(cdfid, 'umask') NE -1 THEN BEGIN 
Index: /trunk/SRC/Grid/smallmeshmask.pro
===================================================================
--- /trunk/SRC/Grid/smallmeshmask.pro	(revision 450)
+++ /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
