Index: palm/trunk/SOURCE/data_output_mask.f90
===================================================================
--- palm/trunk/SOURCE/data_output_mask.f90	(revision 4828)
+++ 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
