Index: palm/trunk/SOURCE/user_data_output_mask.f90
===================================================================
--- palm/trunk/SOURCE/user_data_output_mask.f90	(revision 2718)
+++ palm/trunk/SOURCE/user_data_output_mask.f90	(revision 3435)
@@ -25,4 +25,7 @@
 ! -----------------
 ! $Id$
+! Add terrain-following output
+! 
+! 2718 2018-01-02 08:49:38Z maronga
 ! Corrected "Former revisions" section
 ! 
@@ -62,16 +65,21 @@
     USE kinds
     
+    USE surface_mod,                                                           &
+        ONLY:  get_topography_top_index_ji
+    
     USE user
 
     IMPLICIT NONE
 
-    CHARACTER (LEN=*) ::  variable   !< 
+    CHARACTER (LEN=*) ::  variable  !< 
+    CHARACTER (LEN=5) ::  grid      !< flag to distinquish between staggered grids
 
-    INTEGER(iwp) ::  av   !< 
-    INTEGER(iwp) ::  i    !< 
-    INTEGER(iwp) ::  j    !< 
-    INTEGER(iwp) ::  k    !< 
+    INTEGER(iwp) ::  av             !< 
+    INTEGER(iwp) ::  i              !< 
+    INTEGER(iwp) ::  j              !< 
+    INTEGER(iwp) ::  k              !< 
+    INTEGER(iwp) ::  topo_top_ind   !< k index of highest horizontal surface
 
-    LOGICAL ::  found     !< 
+    LOGICAL ::  found               !< 
 
     REAL(wp),                                                                  &
@@ -81,4 +89,5 @@
 
     found = .TRUE.
+    grid  = 's'
 
     SELECT CASE ( TRIM( variable ) )
@@ -90,21 +99,74 @@
 !       CASE ( 'u2' )
 !          IF ( av == 0 )  THEN
-!            DO  i = 1, mask_size_l(mid,1)
-!               DO  j = 1, mask_size_l(mid,2)
-!                  DO  k = 1, mask_size_l(mid,3)
-!                      local_pf(i,j,k) = u2(mask_k(mid,k),                       &
-!                                           mask_j(mid,j),mask_i(mid,i))
+!             IF ( .NOT. mask_surface(mid) )  THEN
+!!
+!!--             Default masked output
+!                DO  i = 1, mask_size_l(mid,1)
+!                   DO  j = 1, mask_size_l(mid,2)
+!                      DO  k = 1, mask_size_l(mid,3)
+!                         local_pf(i,j,k) = u2(mask_k(mid,k),                  &
+!                                              mask_j(mid,j),                  &
+!                                              mask_i(mid,i))
+!                      ENDDO
 !                   ENDDO
 !                ENDDO
-!             ENDDO
-!          ELSE
-!            DO  i = 1, mask_size_l(mid,1)
-!               DO  j = 1, mask_size_l(mid,2)
-!                  DO  k = 1, mask_size_l(mid,3)
-!                      local_pf(i,j,k) = u2_av(mask_k(mid,k),                    &
-!                                              mask_j(mid,j),mask_i(mid,i))
+!             ELSE
+!!
+!!--             Terrain-following masked output
+!                DO  i = 1, mask_size_l(mid,1)
+!                   DO  j = 1, mask_size_l(mid,2)
+!!
+!!--                   Get k index of highest horizontal surface
+!                      topo_top_ind = get_topography_top_index_ji( &
+!                                        mask_j(mid,j), &
+!                                        mask_i(mid,i), &
+!                                        grid )
+!!
+!!--                   Save output array
+!                      DO  k = 1, mask_size_l(mid,3)
+!                         local_pf(i,j,k) = u2(MIN( topo_top_ind+mask_k(mid,k),&
+!                                                   nzt+1 ),                   &
+!                                              mask_j(mid,j),                  &
+!                                              mask_i(mid,i)                   )
+!                      ENDDO
 !                   ENDDO
 !                ENDDO
-!             ENDDO
+!             ENDIF
+!          ELSE
+!             IF ( .NOT. mask_surface(mid) )  THEN
+!!
+!!--             Default masked output
+!                DO  i = 1, mask_size_l(mid,1)
+!                   DO  j = 1, mask_size_l(mid,2)
+!                      DO  k = 1, mask_size_l(mid,3)
+!                          local_pf(i,j,k) = u2_av(mask_k(mid,k),              &
+!                                                  mask_j(mid,j),              &
+!                                                  mask_i(mid,i) )
+!                       ENDDO
+!                    ENDDO
+!                 ENDDO
+!             ELSE
+!!
+!!--             Terrain-following masked output
+!                DO  i = 1, mask_size_l(mid,1)
+!                   DO  j = 1, mask_size_l(mid,2)
+!!
+!!--                   Get k index of highest horizontal surface
+!                      topo_top_ind = get_topography_top_index_ji( &
+!                                        mask_j(mid,j), &
+!                                        mask_i(mid,i), &
+!                                        grid )
+!!
+!!--                   Save output array
+!                      DO  k = 1, mask_size_l(mid,3)
+!                         local_pf(i,j,k) = u2_av(                             &
+!                                              MIN( topo_top_ind+mask_k(mid,k),&
+!                                                   nzt+1 ),                   &
+!                                              mask_j(mid,j),                  &
+!                                              mask_i(mid,i)                   )
+!                      ENDDO
+!                   ENDDO
+!                ENDDO
+!             ENDIF
 !          ENDIF
 
