Index: palm/trunk/SOURCE/init_masks.f90
===================================================================
--- palm/trunk/SOURCE/init_masks.f90	(revision 1354)
+++ palm/trunk/SOURCE/init_masks.f90	(revision 1414)
@@ -20,5 +20,6 @@
 ! Current revisions:
 ! -----------------
-! 
+! Bugfix: first and last grid points as they appear in 3D volume data can now
+! be included to masked data output
 ! 
 ! Former revisions:
@@ -125,9 +126,9 @@
 !
 !-- Allocation and initialization
-    ALLOCATE( tmp_array( MAX(nx,ny,nz)+1 ) )
-
-    ALLOCATE( mask_i(max_masks,nxr-nxl+1), &
-              mask_j(max_masks,nyn-nys+1), &
-              mask_k(max_masks,nzt-nzb+1) )
+    ALLOCATE( tmp_array( MAX(nx,ny,nz)+2 ) )
+
+    ALLOCATE( mask_i(max_masks,nxr-nxl+2), &
+              mask_j(max_masks,nyn-nys+2), &
+              mask_k(max_masks,nzt-nzb+2) )
 !
 !-- internal mask arrays ("mask,dimension,selection")
@@ -183,7 +184,7 @@
 !-- Global arrays are required by define_netcdf_header.
     IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
-       ALLOCATE( mask_i_global(max_masks,nx+1), &
-                 mask_j_global(max_masks,ny+1), &
-                 mask_k_global(max_masks,nz+1) )
+       ALLOCATE( mask_i_global(max_masks,nx+2), &
+                 mask_j_global(max_masks,ny+2), &
+                 mask_k_global(max_masks,nz+2) )
        mask_i_global = -1; mask_j_global = -1; mask_k_global = -1
     ENDIF
@@ -469,9 +470,9 @@
        IF ( netcdf_data_format > 4 )  THEN
          
-          CALL MPI_BCAST( mask_i_global(mid,:), nx+1, MPI_INTEGER, 0, comm2d, &
+          CALL MPI_BCAST( mask_i_global(mid,:), nx+2, MPI_INTEGER, 0, comm2d, &
                           ierr )
-          CALL MPI_BCAST( mask_j_global(mid,:), ny+1, MPI_INTEGER, 0, comm2d, &
+          CALL MPI_BCAST( mask_j_global(mid,:), ny+2, MPI_INTEGER, 0, comm2d, &
                           ierr )
-          CALL MPI_BCAST( mask_k_global(mid,:), nz+1, MPI_INTEGER, 0, comm2d, &
+          CALL MPI_BCAST( mask_k_global(mid,:), nz+2, MPI_INTEGER, 0, comm2d, &
                           ierr ) 
 	  
@@ -537,4 +538,5 @@
              IF ( dim == 1 .OR. dim == 2 )  THEN
                 m = NINT( mask(mid,dim,count) * mask_scale(dim) * ddxyz - 0.5_wp )
+                IF ( m < 0 )  m = 0  ! avoid negative values
              ELSEIF ( dim == 3 )  THEN
                 ind_array =  &
@@ -542,11 +544,12 @@
                 m = ind_array(1) - 1 + nzb  ! MINLOC uses lower array bound 1
              ENDIF
-             IF ( m > nxyz )  THEN
+             IF ( m > (nxyz+1) )  THEN
                 WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )  &
                      m,' in mask ',mid,' along dimension ',dim,  &
-                     ' exceeds ',nxyz_string,' = ',nxyz
+                     ' exceeds (',nxyz_string,'+1) = ',nxyz+1
                 CALL message( 'init_masks', 'PA0331', 1, 2, 0, 6, 0 )
              ENDIF
-             IF ( m >= lb .AND. m <= ub )  THEN
+             IF ( ( m >= lb .AND. m <= ub ) .OR.     &
+                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
                 IF ( count_l == 0 )  mask_start_l(mid,dim) = count
                 count_l = count_l + 1
@@ -575,14 +578,14 @@
              IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
                 tmp2 = mask_loop(mid,dim,2)
-                mask_loop(mid,dim,2) = nxyz*dxyz / mask_scale(dim)   ! (default)
+                mask_loop(mid,dim,2) = (nxyz+1)*dxyz / mask_scale(dim)   ! (default)
              ENDIF
              IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
-                  > nxyz * dxyz / mask_scale(dim) )  THEN
+                  > (nxyz+1) * dxyz / mask_scale(dim) )  THEN
                 WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),5A,I1,A,F9.3)' ) &
                      'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1), &
                      ' and/or mask_loop(',mid,',',dim,',2)=', &
-                     mask_loop(mid,dim,2),'&exceed ', &
-                     nxyz_string,'*',dxyz_string,'/mask_scale(',dim,')=', &
-                     nxyz*dxyz/mask_scale(dim)
+                     mask_loop(mid,dim,2),'&exceed (', &
+                     nxyz_string,'+1)*',dxyz_string,'/mask_scale(',dim,')=', &
+                     (nxyz+1)*dxyz/mask_scale(dim)
                 CALL message( 'init_masks', 'PA0332', 1, 2, 0, 6, 0 )
              ENDIF
@@ -593,16 +596,17 @@
              loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim) &
                   * ddxyz )
+             IF ( loop_begin == -1 )  loop_begin = 0  ! avoid negative values
           ELSEIF ( dim == 3 )  THEN
              IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
                 tmp2 = mask_loop(mid,dim,2)
-                mask_loop(mid,dim,2) = zu(nz) / mask_scale(dim)   ! (default)
+                mask_loop(mid,dim,2) = zu(nz+1) / mask_scale(dim)   ! (default)
              ENDIF
              IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
-                  > zu(nz) / mask_scale(dim) )  THEN
+                  > zu(nz+1) / mask_scale(dim) )  THEN
                 WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),A,I1,A,F9.3)' ) &
                      'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1), &
                      ' and/or mask_loop(',mid,',',dim,',2)=', &
-                     mask_loop(mid,dim,2),'&exceed zu(nz)/mask_scale(',dim, &
-                     ')=',zu(nz)/mask_scale(dim)
+                     mask_loop(mid,dim,2),'&exceed zu(nz+1)/mask_scale(',dim, &
+                     ')=',zu(nz+1)/mask_scale(dim)
                 CALL message( 'init_masks', 'PA0333', 1, 2, 0, 6, 0 )
              ENDIF
@@ -648,5 +652,6 @@
           DO  m = loop_begin, loop_end, loop_stride
              count = count + 1
-             IF ( m >= lb  .AND.  m <= ub )  THEN
+             IF ( ( m >= lb  .AND.  m <= ub ) .OR.   &
+                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
                 IF ( count_l == 0 )  mask_start_l(mid,dim) = count
                 count_l = count_l + 1
@@ -662,4 +667,5 @@
           mask_size(mid,dim)   = count
           mask_size_l(mid,dim) = count_l
+
        ENDIF
 
