Index: /palm/trunk/SOURCE/chemistry_model_mod.f90
===================================================================
--- /palm/trunk/SOURCE/chemistry_model_mod.f90	(revision 4894)
+++ /palm/trunk/SOURCE/chemistry_model_mod.f90	(revision 4895)
@@ -26,4 +26,7 @@
 ! -----------------
 ! $Id$
+! Remove offset in terrain-following masked output
+!
+! 4887 2021-02-26 16:22:32Z banzhafs
 ! Unnecessary comments removed
 !
@@ -1696,5 +1699,5 @@
     INTEGER(iwp) ::  k               !< grid index along z-direction
     INTEGER(iwp) ::  kk              !< masked output index along z-direction
-    INTEGER(iwp) ::  ktt             !< k index of highest terrain surface
+    INTEGER(iwp) ::  ktt             !< k index of lowest non-terrain grid point
     INTEGER(iwp) ::  lsp
     INTEGER(iwp) ::  mid             !< masked output running index
@@ -1732,5 +1735,5 @@
                    DO  j = 1, mask_size_l(mid,2)
 !
-!--                   Get k index of the highest terraing surface
+!--                   Get k index of the lowest non-terrain grid point
                       im = mask_i(mid,i)
                       jm = mask_j(mid,j)
@@ -1738,7 +1741,7 @@
                                     DIM = 1 ) - 1
                       DO  k = 1, mask_size_l(mid,3)
-                         kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                      Set value if not in building
+                         kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                      Set value if not in building, else set fill value
                          IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                             local_pf(i,j,k) = fill_value
@@ -1770,5 +1773,5 @@
                    DO  j = 1, mask_size_l(mid,2)
 !
-!--                   Get k index of the highest terraing surface
+!--                   Get k index of the lowest non-terrain grid point
                       im = mask_i(mid,i)
                       jm = mask_j(mid,j)
@@ -1776,7 +1779,7 @@
                                     DIM = 1 ) - 1
                       DO  k = 1, mask_size_l(mid,3)
-                         kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                      Set value if not in building
+                         kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                      Set value if not in building, else set fill value
                          IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                             local_pf(i,j,k) = fill_value
Index: /palm/trunk/SOURCE/data_output_mask.f90
===================================================================
--- /palm/trunk/SOURCE/data_output_mask.f90	(revision 4894)
+++ /palm/trunk/SOURCE/data_output_mask.f90	(revision 4895)
@@ -20,9 +20,12 @@
 ! Current revisions:
 ! -----------------
-! 
-! 
+!
+!
 ! Former revisions:
 ! -----------------
 ! $Id$
+! Remove offset in terrain-following masked output
+! 
+! 4828 2021-01-05 11:21:41Z Giersch
 ! file re-formatted to follow the PALM coding standard
 !
@@ -154,5 +157,5 @@
     INTEGER(iwp) ::  k                       !< loop index
     INTEGER(iwp) ::  kk                      !< vertical index
-    INTEGER(iwp) ::  ktt                     !< k index of highest terrain surface
+    INTEGER(iwp) ::  ktt                     !< k index of lowest non-terrain surface
     INTEGER(iwp) ::  mid                     !< masked output running index
     INTEGER(iwp) ::  n                       !< loop index
@@ -288,5 +291,6 @@
                    DO  i = 1, mask_size_l(mid,1)
                       DO  j = 1, mask_size_l(mid,2)
-!--                      Get k index of the highest terraing surface
+!
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
@@ -294,6 +298,6 @@
                                        DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -356,5 +360,6 @@
                    DO  i = 1, mask_size_l(mid,1)
                       DO  j = 1, mask_size_l(mid,2)
-!--                      Get k index of the highest terraing surface
+!
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
@@ -362,6 +367,6 @@
                                        DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -399,5 +404,6 @@
                       DO  i = 1, mask_size_l(mid,1)
                          DO  j = 1, mask_size_l(mid,2)
-!--                         Get k index of the highest terraing surface
+!
+!--                         Get k index of the lowest non-terrain grid point
                             im = mask_i(mid,i)
                             jm = mask_j(mid,j)
@@ -405,6 +411,7 @@
                                           DIM = 1 ) - 1
                             DO  k = 1, mask_size_l(mid,3)
-                               kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                            Set value if not in building
+                               kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                            Set value if not in building, else set fill value
                                IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                   local_pf(i,j,k) = fill_value
@@ -493,5 +500,6 @@
                    DO  i = 1, mask_size_l(mid,1)
                       DO  j = 1, mask_size_l(mid,2)
-!--                      Get k index of the highest terraing surface
+!
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
@@ -499,6 +507,7 @@
                                        DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -532,5 +541,6 @@
                    DO  i = 1, mask_size_l(mid,1)
                       DO  j = 1, mask_size_l(mid,2)
-!--                      Get k index of the highest terraing surface
+!
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
@@ -538,6 +548,6 @@
                                        DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -679,12 +689,14 @@
              DO  i = 1, mask_size_l(mid,1)
                 DO  j = 1, mask_size_l(mid,2)
-!--                Get k index of the highest terraing surface
+!
+!--                Get k index of the lowest non-terrain grid point
                    im = mask_i(mid,i)
                    jm = mask_j(mid,j)
-                   ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 ) ),           &
+                   ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 ) ),     &
                                  DIM = 1 ) - 1
                    DO  k = 1, mask_size_l(mid,3)
-                      kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!--                   Set value if not in building
+                      kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                   Set value if not in building, else set fill value
                       IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                          local_pf(i,j,k) = fill_value
Index: /palm/trunk/SOURCE/diagnostic_output_quantities_mod.f90
===================================================================
--- /palm/trunk/SOURCE/diagnostic_output_quantities_mod.f90	(revision 4894)
+++ /palm/trunk/SOURCE/diagnostic_output_quantities_mod.f90	(revision 4895)
@@ -24,4 +24,7 @@
 ! -----------------
 ! $Id$
+! Remove offset in terrain-following masked output
+!
+! 4866 2021-02-05 17:50:14Z suehring
 ! Implemented vertical passive scalar flux
 ! Implemented air temperature as diagnostic output quantity (previous commit)
@@ -1691,10 +1694,10 @@
              DO  j = 1, mask_size_l(mid,2)
 !
-!--             Get k index of the highest terraing surface
+!--             Get k index of the lowest non-terrain grid point, else set fill value
                 im = mask_i(mid,i)
                 jm = mask_j(mid,j)
                 ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 ) ), DIM=1 ) - 1
                 DO  k = 1, mask_size_l(mid,3)
-                   kk = MIN( ktt+mask_k(mid,k), nzt+1 )
+                   kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
 !
 !--                Set value if not in building
Index: /palm/trunk/SOURCE/init_masks.f90
===================================================================
--- /palm/trunk/SOURCE/init_masks.f90	(revision 4894)
+++ /palm/trunk/SOURCE/init_masks.f90	(revision 4895)
@@ -24,4 +24,7 @@
 ! -----------------
 ! $Id$
+! Allow only mask_k_over_surface >= 1 for terrain-following mask output
+!
+! 4828 2021-01-05 11:21:41Z Giersch
 ! Implement snow and graupel (bulk microphysics)
 !
@@ -521,4 +524,8 @@
           DO  WHILE ( mask_k_over_surface(mid, count+1) >= 0 )
              m = mask_k_over_surface(mid, count+1)
+             IF ( m < 1 )  THEN
+                WRITE ( message_string, '(I3,A,I3,A)' )  m, ' in mask ', mid, ' must be > 0.'
+                CALL message( 'init_masks', 'PA0743', 1, 2, 0, 6, 0 )
+             ENDIF
              IF ( m > nz+1 )  THEN
                 WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )  m, ' in mask ', mid,             &
Index: /palm/trunk/SOURCE/salsa_mod.f90
===================================================================
--- /palm/trunk/SOURCE/salsa_mod.f90	(revision 4894)
+++ /palm/trunk/SOURCE/salsa_mod.f90	(revision 4895)
@@ -26,4 +26,7 @@
 ! -----------------
 ! $Id$
+! Remove offset in terrain-following masked output
+!
+! 4873 2021-02-12 17:12:17Z monakurppa
 ! local namelist parameter added to switch off the module although the respective module namelist
 ! appears in the namelist file
@@ -12513,5 +12516,5 @@
     INTEGER(iwp) ::  kk             !< loop index for masked output in z-direction
     INTEGER(iwp) ::  mid            !< masked output running index
-    INTEGER(iwp) ::  ktt            !< k index of highest terrain surface
+    INTEGER(iwp) ::  ktt            !< k index of lowest non-terrain grid point
 
     LOGICAL ::  found      !<
@@ -12554,13 +12557,13 @@
                    DO  j = 1, mask_size_l(mid,2)
 !
-!--                   Get k index of the highest terraing surface
+!--                   Get k index of the lowest non-terrain grid point
                       im = mask_i(mid,i)
                       jm = mask_j(mid,j)
-                      ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                    DIM = 1 ) - 1
+                      ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 ) ),        &
+                                    DIM = 1 ) - 1
                       DO  k = 1, mask_size_l(mid,3)
-                         kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                      Set value if not in building
+                         kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                      Set value if not in building, else set fill value
                          IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                             local_pf(i,j,k) = fill_value
@@ -12609,13 +12612,13 @@
                    DO  j = 1, mask_size_l(mid,2)
 !
-!--                   Get k index of the highest terraing surface
+!--                   Get k index of the lowest non-terrain grid point
                       im = mask_i(mid,i)
                       jm = mask_j(mid,j)
-                      ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                    DIM = 1 ) - 1
+                      ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),         &
+                                           DIM = 1 ) - 1
                       DO  k = 1, mask_size_l(mid,3)
-                         kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                      Set value if not in building
+                         kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                      Set value if not in building, else set fill value
                          IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                             local_pf(i,j,k) = fill_value
@@ -12690,13 +12693,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12740,13 +12743,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12788,13 +12791,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12840,13 +12843,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12892,13 +12895,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 ) ),     &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12944,13 +12947,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                       DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -12994,13 +12997,13 @@
                          DO  j = 1, mask_size_l(mid,2)
    !
-   !--                      Get k index of the highest terraing surface
+   !--                      Get k index of the lowest non-terrain grid point
                             im = mask_i(mid,i)
                             jm = mask_j(mid,j)
-                            ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                                          DIM = 1 ) - 1
+                            ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),   &
+                                          DIM = 1 ) - 1
                             DO  k = 1, mask_size_l(mid,3)
-                               kk = MIN( ktt+mask_k(mid,k), nzt+1 )
+                               kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
    !
-   !--                         Set value if not in building
+   !--                         Set value if not in building, else set fill value
                                IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                   local_pf(i,j,k) = fill_value
@@ -13047,13 +13050,13 @@
                       DO  j = 1, mask_size_l(mid,2)
 !
-!--                      Get k index of the highest terraing surface
+!--                      Get k index of the lowest non-terrain grid point
                          im = mask_i(mid,i)
                          jm = mask_j(mid,j)
-                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                          DIM = 1 ) - 1
+                         ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )),      &
+                                       DIM = 1 ) - 1
                          DO  k = 1, mask_size_l(mid,3)
-                            kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                         Set value if not in building
+                            kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                         Set value if not in building, else set fill value
                             IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                                local_pf(i,j,k) = fill_value
@@ -13093,13 +13096,12 @@
              DO  j = 1, mask_size_l(mid,2)
 !
-!--             Get k index of the highest terraing surface
+!--             Get k index of the lowest non-terrain grid point
                 im = mask_i(mid,i)
                 jm = mask_j(mid,j)
-                ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), &
-                                 DIM = 1 ) - 1
+                ktt = MINLOC( MERGE( 1, 0, BTEST( wall_flags_total_0(:,jm,im), 5 )), DIM = 1 ) - 1
                 DO  k = 1, mask_size_l(mid,3)
-                   kk = MIN( ktt+mask_k(mid,k), nzt+1 )
-!
-!--                Set value if not in building
+                   kk = MIN( ktt + mask_k(mid,k) - 1, nzt+1 )
+!
+!--                Set value if not in building, else set fill value
                    IF ( BTEST( wall_flags_total_0(kk,jm,im), 6 ) )  THEN
                       local_pf(i,j,k) = fill_value
