source: palm/trunk/SOURCE/data_output_2d.f90 @ 4180

Last change on this file since 4180 was 4180, checked in by scharf, 5 years ago

removed comments in 'Former revisions' section that are older than 01.01.2019

  • Property svn:keywords set to Id
File size: 89.4 KB
Line 
1!> @file data_output_2d.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2019 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! ------------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: data_output_2d.f90 4180 2019-08-21 14:37:54Z scharf $
27! Removed turbulence_closure_mod dependency
28!
29! 4039 2019-06-18 10:32:41Z suehring
30! modularize diagnostic output
31!
32! 3994 2019-05-22 18:08:09Z suehring
33! output of turbulence intensity added
34!
35! 3987 2019-05-22 09:52:13Z kanani
36! Introduce alternative switch for debug output during timestepping
37!
38! 3943 2019-05-02 09:50:41Z maronga
39! Added output of qsws for green roofs.
40!
41! 3885 2019-04-11 11:29:34Z kanani
42! Changes related to global restructuring of location messages and introduction
43! of additional debug messages
44!
45! 3766 2019-02-26 16:23:41Z raasch
46! unused variables removed
47!
48! 3655 2019-01-07 16:51:22Z knoop
49! Bugfix: use time_since_reference_point instead of simulated_time (relevant
50! when using wall/soil spinup)
51!
52!
53! Description:
54! ------------
55!> Data output of cross-sections in netCDF format or binary format
56!> to be later converted to NetCDF by helper routine combine_plot_fields.
57!> Attention: The position of the sectional planes is still not always computed
58!> ---------  correctly. (zu is used always)!
59!------------------------------------------------------------------------------!
60 SUBROUTINE data_output_2d( mode, av )
61 
62
63    USE arrays_3d,                                                                                 &
64        ONLY:  dzw, d_exner, e, heatflux_output_conversion, p, pt, q, ql, ql_c, ql_v, s, tend, u,  &
65               v, vpt, w, waterflux_output_conversion, zu, zw
66
67    USE averaging
68
69    USE basic_constants_and_equations_mod,                                     &
70        ONLY:  c_p, lv_d_cp, l_v
71
72    USE bulk_cloud_model_mod,                                                  &
73        ONLY:  bulk_cloud_model
74
75    USE control_parameters,                                                    &
76        ONLY:  data_output_2d_on_each_pe,                                      &
77               data_output_xy, data_output_xz, data_output_yz,                 &
78               debug_output_timestep,                                          &
79               do2d,                                                           &
80               do2d_xy_last_time, do2d_xy_time_count,                          &
81               do2d_xz_last_time, do2d_xz_time_count,                          &
82               do2d_yz_last_time, do2d_yz_time_count,                          &
83               ibc_uv_b, io_blocks, io_group, message_string,                  &
84               ntdim_2d_xy, ntdim_2d_xz, ntdim_2d_yz,                          &
85               psolver, section,                                               &
86               time_since_reference_point
87
88    USE cpulog,                                                                &
89        ONLY:  cpu_log, log_point
90
91    USE indices,                                                               &
92        ONLY:  nbgp, nx, nxl, nxlg, nxr, nxrg, ny, nyn, nyng, nys, nysg,       &
93               nzb, nzt, wall_flags_0
94
95    USE kinds
96
97    USE land_surface_model_mod,                                                &
98        ONLY:  zs
99
100    USE module_interface,                                                      &
101        ONLY:  module_interface_data_output_2d
102
103#if defined( __netcdf )
104    USE NETCDF
105#endif
106
107    USE netcdf_interface,                                                      &
108        ONLY:  fill_value, id_set_xy, id_set_xz, id_set_yz, id_var_do2d,       &
109               id_var_time_xy, id_var_time_xz, id_var_time_yz, nc_stat,        &
110               netcdf_data_format, netcdf_handle_error
111
112    USE particle_attributes,                                                   &
113        ONLY:  grid_particles, number_of_particles, particle_advection_start,  &
114               particles, prt_count
115   
116    USE pegrid
117
118    USE surface_mod,                                                           &
119        ONLY:  ind_pav_green, ind_veg_wall, ind_wat_win, surf_def_h,           &
120               surf_lsm_h, surf_usm_h
121
122
123    IMPLICIT NONE
124
125    CHARACTER (LEN=2)  ::  do2d_mode    !< output mode of variable ('xy', 'xz', 'yz')
126    CHARACTER (LEN=2)  ::  mode         !< mode with which the routine is called ('xy', 'xz', 'yz')
127    CHARACTER (LEN=4)  ::  grid         !< string defining the vertical grid
128   
129    INTEGER(iwp) ::  av        !< flag for (non-)average output
130    INTEGER(iwp) ::  ngp       !< number of grid points of an output slice
131    INTEGER(iwp) ::  file_id   !< id of output files
132    INTEGER(iwp) ::  flag_nr   !< number of masking flag
133    INTEGER(iwp) ::  i         !< loop index
134    INTEGER(iwp) ::  iis       !< vertical index of a xy slice in array 'local_2d_sections'
135    INTEGER(iwp) ::  is        !< slice index
136    INTEGER(iwp) ::  ivar      !< variable index
137    INTEGER(iwp) ::  j         !< loop index
138    INTEGER(iwp) ::  k         !< loop index
139    INTEGER(iwp) ::  l         !< loop index
140    INTEGER(iwp) ::  layer_xy  !< vertical index of a xy slice in array 'local_pf'
141    INTEGER(iwp) ::  m         !< loop index
142    INTEGER(iwp) ::  n         !< loop index
143    INTEGER(iwp) ::  nis       !< number of vertical slices to be written via parallel NetCDF output
144    INTEGER(iwp) ::  ns        !< number of output slices
145    INTEGER(iwp) ::  nzb_do    !< lower limit of the data field (usually nzb)
146    INTEGER(iwp) ::  nzt_do    !< upper limit of the data field (usually nzt+1)
147    INTEGER(iwp) ::  s_ind     !< index of slice types (xy=1, xz=2, yz=3)
148    INTEGER(iwp) ::  sender    !< PE id of sending PE
149    INTEGER(iwp) ::  ind(4)    !< index limits (lower/upper bounds) of array 'local_2d'
150
151    LOGICAL ::  found          !< true if output variable was found
152    LOGICAL ::  resorted       !< true if variable is resorted
153    LOGICAL ::  two_d          !< true if variable is only two dimensional
154
155    REAL(wp) ::  mean_r        !< mean particle radius
156    REAL(wp) ::  s_r2          !< sum( particle-radius**2 )
157    REAL(wp) ::  s_r3          !< sum( particle-radius**3 )
158   
159    REAL(wp), DIMENSION(:), ALLOCATABLE     ::  level_z             !< z levels for output array
160    REAL(wp), DIMENSION(:,:), ALLOCATABLE   ::  local_2d            !< local 2-dimensional array containing output values
161    REAL(wp), DIMENSION(:,:), ALLOCATABLE   ::  local_2d_l          !< local 2-dimensional array containing output values
162
163    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  local_pf            !< output array
164    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  local_2d_sections   !< local array containing values at all slices
165    REAL(wp), DIMENSION(:,:,:), ALLOCATABLE ::  local_2d_sections_l !< local array containing values at all slices
166
167#if defined( __parallel )
168    REAL(wp), DIMENSION(:,:),   ALLOCATABLE ::  total_2d    !< same as local_2d
169#endif
170    REAL(wp), DIMENSION(:,:,:), POINTER ::  to_be_resorted  !< points to array which shall be output
171
172
173    IF ( debug_output_timestep )  CALL debug_message( 'data_output_2d', 'start' )
174!
175!-- Immediate return, if no output is requested (no respective sections
176!-- found in parameter data_output)
177    IF ( mode == 'xy'  .AND.  .NOT. data_output_xy(av) )  RETURN
178    IF ( mode == 'xz'  .AND.  .NOT. data_output_xz(av) )  RETURN
179    IF ( mode == 'yz'  .AND.  .NOT. data_output_yz(av) )  RETURN
180
181    CALL cpu_log (log_point(3),'data_output_2d','start')
182
183    two_d = .FALSE.    ! local variable to distinguish between output of pure 2D
184                       ! arrays and cross-sections of 3D arrays.
185
186!
187!-- Depending on the orientation of the cross-section, the respective output
188!-- files have to be opened.
189    SELECT CASE ( mode )
190
191       CASE ( 'xy' )
192          s_ind = 1
193          ALLOCATE( level_z(nzb:nzt+1), local_2d(nxl:nxr,nys:nyn) )
194
195          IF ( netcdf_data_format > 4 )  THEN
196             ns = 1
197             DO WHILE ( section(ns,s_ind) /= -9999  .AND.  ns <= 100 )
198                ns = ns + 1
199             ENDDO
200             ns = ns - 1
201             ALLOCATE( local_2d_sections(nxl:nxr,nys:nyn,1:ns) )
202             local_2d_sections = 0.0_wp
203          ENDIF
204
205!
206!--       Parallel netCDF4/HDF5 output is done on all PEs, all other on PE0 only
207          IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
208             CALL check_open( 101+av*10 )
209          ENDIF
210          IF ( data_output_2d_on_each_pe  .AND.  netcdf_data_format < 5 )  THEN
211             CALL check_open( 21 )
212          ELSE
213             IF ( myid == 0 )  THEN
214#if defined( __parallel )
215                ALLOCATE( total_2d(0:nx,0:ny) )
216#endif
217             ENDIF
218          ENDIF
219
220       CASE ( 'xz' )
221          s_ind = 2
222          ALLOCATE( local_2d(nxl:nxr,nzb:nzt+1) )
223
224          IF ( netcdf_data_format > 4 )  THEN
225             ns = 1
226             DO WHILE ( section(ns,s_ind) /= -9999  .AND.  ns <= 100 )
227                ns = ns + 1
228             ENDDO
229             ns = ns - 1
230             ALLOCATE( local_2d_sections(nxl:nxr,1:ns,nzb:nzt+1) )
231             ALLOCATE( local_2d_sections_l(nxl:nxr,1:ns,nzb:nzt+1) )
232             local_2d_sections = 0.0_wp; local_2d_sections_l = 0.0_wp
233          ENDIF
234
235!
236!--       Parallel netCDF4/HDF5 output is done on all PEs, all other on PE0 only
237          IF ( myid == 0 .OR. netcdf_data_format > 4 )  THEN
238             CALL check_open( 102+av*10 )
239          ENDIF
240
241          IF ( data_output_2d_on_each_pe  .AND.  netcdf_data_format < 5 )  THEN
242             CALL check_open( 22 )
243          ELSE
244             IF ( myid == 0 )  THEN
245#if defined( __parallel )
246                ALLOCATE( total_2d(0:nx,nzb:nzt+1) )
247#endif
248             ENDIF
249          ENDIF
250
251       CASE ( 'yz' )
252          s_ind = 3
253          ALLOCATE( local_2d(nys:nyn,nzb:nzt+1) )
254
255          IF ( netcdf_data_format > 4 )  THEN
256             ns = 1
257             DO WHILE ( section(ns,s_ind) /= -9999  .AND.  ns <= 100 )
258                ns = ns + 1
259             ENDDO
260             ns = ns - 1
261             ALLOCATE( local_2d_sections(1:ns,nys:nyn,nzb:nzt+1) )
262             ALLOCATE( local_2d_sections_l(1:ns,nys:nyn,nzb:nzt+1) )
263             local_2d_sections = 0.0_wp; local_2d_sections_l = 0.0_wp
264          ENDIF
265
266!
267!--       Parallel netCDF4/HDF5 output is done on all PEs, all other on PE0 only
268          IF ( myid == 0 .OR. netcdf_data_format > 4 )  THEN
269             CALL check_open( 103+av*10 )
270          ENDIF
271
272          IF ( data_output_2d_on_each_pe  .AND.  netcdf_data_format < 5 )  THEN
273             CALL check_open( 23 )
274          ELSE
275             IF ( myid == 0 )  THEN
276#if defined( __parallel )
277                ALLOCATE( total_2d(0:ny,nzb:nzt+1) )
278#endif
279             ENDIF
280          ENDIF
281
282       CASE DEFAULT
283          message_string = 'unknown cross-section: ' // TRIM( mode )
284          CALL message( 'data_output_2d', 'PA0180', 1, 2, 0, 6, 0 )
285
286    END SELECT
287
288!
289!-- For parallel netcdf output the time axis must be limited. Return, if this
290!-- limit is exceeded. This could be the case, if the simulated time exceeds
291!-- the given end time by the length of the given output interval.
292    IF ( netcdf_data_format > 4 )  THEN
293       IF ( mode == 'xy'  .AND.  do2d_xy_time_count(av) + 1 >                  &
294            ntdim_2d_xy(av) )  THEN
295          WRITE ( message_string, * ) 'Output of xy cross-sections is not ',   &
296                          'given at t=', time_since_reference_point, 's because the',       & 
297                          ' maximum number of output time levels is exceeded.'
298          CALL message( 'data_output_2d', 'PA0384', 0, 1, 0, 6, 0 )
299          CALL cpu_log( log_point(3), 'data_output_2d', 'stop' )
300          RETURN
301       ENDIF
302       IF ( mode == 'xz'  .AND.  do2d_xz_time_count(av) + 1 >                  &
303            ntdim_2d_xz(av) )  THEN
304          WRITE ( message_string, * ) 'Output of xz cross-sections is not ',   &
305                          'given at t=', time_since_reference_point, 's because the',       & 
306                          ' maximum number of output time levels is exceeded.'
307          CALL message( 'data_output_2d', 'PA0385', 0, 1, 0, 6, 0 )
308          CALL cpu_log( log_point(3), 'data_output_2d', 'stop' )
309          RETURN
310       ENDIF
311       IF ( mode == 'yz'  .AND.  do2d_yz_time_count(av) + 1 >                  &
312            ntdim_2d_yz(av) )  THEN
313          WRITE ( message_string, * ) 'Output of yz cross-sections is not ',   &
314                          'given at t=', time_since_reference_point, 's because the',       & 
315                          ' maximum number of output time levels is exceeded.'
316          CALL message( 'data_output_2d', 'PA0386', 0, 1, 0, 6, 0 )
317          CALL cpu_log( log_point(3), 'data_output_2d', 'stop' )
318          RETURN
319       ENDIF
320    ENDIF
321
322!
323!-- Allocate a temporary array for resorting (kji -> ijk).
324    ALLOCATE( local_pf(nxl:nxr,nys:nyn,nzb:nzt+1) )
325    local_pf = 0.0
326
327!
328!-- Loop of all variables to be written.
329!-- Output dimensions chosen
330    ivar = 1
331    l = MAX( 2, LEN_TRIM( do2d(av,ivar) ) )
332    do2d_mode = do2d(av,ivar)(l-1:l)
333
334    DO  WHILE ( do2d(av,ivar)(1:1) /= ' ' )
335
336       IF ( do2d_mode == mode )  THEN
337!
338!--       Set flag to steer output of radiation, land-surface, or user-defined
339!--       quantities
340          found = .FALSE.
341
342          nzb_do = nzb
343          nzt_do = nzt+1
344!
345!--       Before each output, set array local_pf to fill value
346          local_pf = fill_value
347!
348!--       Set masking flag for topography for not resorted arrays
349          flag_nr = 0
350         
351!
352!--       Store the array chosen on the temporary array.
353          resorted = .FALSE.
354          SELECT CASE ( TRIM( do2d(av,ivar) ) )
355             CASE ( 'e_xy', 'e_xz', 'e_yz' )
356                IF ( av == 0 )  THEN
357                   to_be_resorted => e
358                ELSE
359                   IF ( .NOT. ALLOCATED( e_av ) ) THEN
360                      ALLOCATE( e_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
361                      e_av = REAL( fill_value, KIND = wp )
362                   ENDIF
363                   to_be_resorted => e_av
364                ENDIF
365                IF ( mode == 'xy' )  level_z = zu
366
367             CASE ( 'thetal_xy', 'thetal_xz', 'thetal_yz' )
368                IF ( av == 0 )  THEN
369                   to_be_resorted => pt
370                ELSE
371                   IF ( .NOT. ALLOCATED( lpt_av ) ) THEN
372                      ALLOCATE( lpt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
373                      lpt_av = REAL( fill_value, KIND = wp )
374                   ENDIF
375                   to_be_resorted => lpt_av
376                ENDIF
377                IF ( mode == 'xy' )  level_z = zu
378
379             CASE ( 'lwp*_xy' )        ! 2d-array
380                IF ( av == 0 )  THEN
381                   DO  i = nxl, nxr
382                      DO  j = nys, nyn
383                         local_pf(i,j,nzb+1) = SUM( ql(nzb:nzt,j,i) *          &
384                                                    dzw(1:nzt+1) )
385                      ENDDO
386                   ENDDO
387                ELSE
388                   IF ( .NOT. ALLOCATED( lwp_av ) ) THEN
389                      ALLOCATE( lwp_av(nysg:nyng,nxlg:nxrg) )
390                      lwp_av = REAL( fill_value, KIND = wp )
391                   ENDIF
392                   DO  i = nxl, nxr
393                      DO  j = nys, nyn
394                         local_pf(i,j,nzb+1) = lwp_av(j,i)
395                      ENDDO
396                   ENDDO
397                ENDIF
398                resorted = .TRUE.
399                two_d = .TRUE.
400                level_z(nzb+1) = zu(nzb+1)
401
402             CASE ( 'ghf*_xy' )        ! 2d-array
403                IF ( av == 0 )  THEN
404                   DO  m = 1, surf_lsm_h%ns
405                      i                   = surf_lsm_h%i(m)           
406                      j                   = surf_lsm_h%j(m)
407                      local_pf(i,j,nzb+1) = surf_lsm_h%ghf(m)
408                   ENDDO
409                   DO  m = 1, surf_usm_h%ns
410                      i                   = surf_usm_h%i(m)           
411                      j                   = surf_usm_h%j(m)
412                      local_pf(i,j,nzb+1) = surf_usm_h%frac(ind_veg_wall,m)  *  &
413                                            surf_usm_h%wghf_eb(m)        +      &
414                                            surf_usm_h%frac(ind_pav_green,m) *  &
415                                            surf_usm_h%wghf_eb_green(m)  +      &
416                                            surf_usm_h%frac(ind_wat_win,m)   *  &
417                                            surf_usm_h%wghf_eb_window(m)
418                   ENDDO
419                ELSE
420                   IF ( .NOT. ALLOCATED( ghf_av ) ) THEN
421                      ALLOCATE( ghf_av(nysg:nyng,nxlg:nxrg) )
422                      ghf_av = REAL( fill_value, KIND = wp )
423                   ENDIF
424                   DO  i = nxl, nxr
425                      DO  j = nys, nyn
426                         local_pf(i,j,nzb+1) = ghf_av(j,i)
427                      ENDDO
428                   ENDDO
429                ENDIF
430
431                resorted = .TRUE.
432                two_d = .TRUE.
433                level_z(nzb+1) = zu(nzb+1)
434
435             CASE ( 'ol*_xy' )        ! 2d-array
436                IF ( av == 0 ) THEN
437                   DO  m = 1, surf_def_h(0)%ns
438                      i = surf_def_h(0)%i(m)
439                      j = surf_def_h(0)%j(m)
440                      local_pf(i,j,nzb+1) = surf_def_h(0)%ol(m)
441                   ENDDO
442                   DO  m = 1, surf_lsm_h%ns
443                      i = surf_lsm_h%i(m)
444                      j = surf_lsm_h%j(m)
445                      local_pf(i,j,nzb+1) = surf_lsm_h%ol(m)
446                   ENDDO
447                   DO  m = 1, surf_usm_h%ns
448                      i = surf_usm_h%i(m)
449                      j = surf_usm_h%j(m)
450                      local_pf(i,j,nzb+1) = surf_usm_h%ol(m)
451                   ENDDO
452                ELSE
453                   IF ( .NOT. ALLOCATED( ol_av ) ) THEN
454                      ALLOCATE( ol_av(nysg:nyng,nxlg:nxrg) )
455                      ol_av = REAL( fill_value, KIND = wp )
456                   ENDIF
457                   DO  i = nxl, nxr
458                      DO  j = nys, nyn
459                         local_pf(i,j,nzb+1) = ol_av(j,i)
460                      ENDDO
461                   ENDDO
462                ENDIF
463                resorted = .TRUE.
464                two_d = .TRUE.
465                level_z(nzb+1) = zu(nzb+1)
466
467             CASE ( 'p_xy', 'p_xz', 'p_yz' )
468                IF ( av == 0 )  THEN
469                   IF ( psolver /= 'sor' )  CALL exchange_horiz( p, nbgp )
470                   to_be_resorted => p
471                ELSE
472                   IF ( .NOT. ALLOCATED( p_av ) ) THEN
473                      ALLOCATE( p_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
474                      p_av = REAL( fill_value, KIND = wp )
475                   ENDIF
476                   IF ( psolver /= 'sor' )  CALL exchange_horiz( p_av, nbgp )
477                   to_be_resorted => p_av
478                ENDIF
479                IF ( mode == 'xy' )  level_z = zu
480
481             CASE ( 'pc_xy', 'pc_xz', 'pc_yz' )  ! particle concentration
482                IF ( av == 0 )  THEN
483                   IF ( time_since_reference_point >= particle_advection_start )  THEN
484                      tend = prt_count
485!                      CALL exchange_horiz( tend, nbgp )
486                   ELSE
487                      tend = 0.0_wp
488                   ENDIF
489                   DO  i = nxl, nxr
490                      DO  j = nys, nyn
491                         DO  k = nzb, nzt+1
492                            local_pf(i,j,k) = tend(k,j,i)
493                         ENDDO
494                      ENDDO
495                   ENDDO
496                   resorted = .TRUE.
497                ELSE
498                   IF ( .NOT. ALLOCATED( pc_av ) ) THEN
499                      ALLOCATE( pc_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
500                      pc_av = REAL( fill_value, KIND = wp )
501                   ENDIF
502!                   CALL exchange_horiz( pc_av, nbgp )
503                   to_be_resorted => pc_av
504                ENDIF
505
506             CASE ( 'pr_xy', 'pr_xz', 'pr_yz' )  ! mean particle radius (effective radius)
507                IF ( av == 0 )  THEN
508                   IF ( time_since_reference_point >= particle_advection_start )  THEN
509                      DO  i = nxl, nxr
510                         DO  j = nys, nyn
511                            DO  k = nzb, nzt+1
512                               number_of_particles = prt_count(k,j,i)
513                               IF (number_of_particles <= 0)  CYCLE
514                               particles => grid_particles(k,j,i)%particles(1:number_of_particles)
515                               s_r2 = 0.0_wp
516                               s_r3 = 0.0_wp
517                               DO  n = 1, number_of_particles
518                                  IF ( particles(n)%particle_mask )  THEN
519                                     s_r2 = s_r2 + particles(n)%radius**2 * &
520                                            particles(n)%weight_factor
521                                     s_r3 = s_r3 + particles(n)%radius**3 * &
522                                            particles(n)%weight_factor
523                                  ENDIF
524                               ENDDO
525                               IF ( s_r2 > 0.0_wp )  THEN
526                                  mean_r = s_r3 / s_r2
527                               ELSE
528                                  mean_r = 0.0_wp
529                               ENDIF
530                               tend(k,j,i) = mean_r
531                            ENDDO
532                         ENDDO
533                      ENDDO
534!                      CALL exchange_horiz( tend, nbgp )
535                   ELSE
536                      tend = 0.0_wp
537                   ENDIF
538                   DO  i = nxl, nxr
539                      DO  j = nys, nyn
540                         DO  k = nzb, nzt+1
541                            local_pf(i,j,k) = tend(k,j,i)
542                         ENDDO
543                      ENDDO
544                   ENDDO
545                   resorted = .TRUE.
546                ELSE
547                   IF ( .NOT. ALLOCATED( pr_av ) ) THEN
548                      ALLOCATE( pr_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
549                      pr_av = REAL( fill_value, KIND = wp )
550                   ENDIF
551!                   CALL exchange_horiz( pr_av, nbgp )
552                   to_be_resorted => pr_av
553                ENDIF
554
555             CASE ( 'theta_xy', 'theta_xz', 'theta_yz' )
556                IF ( av == 0 )  THEN
557                   IF ( .NOT. bulk_cloud_model ) THEN
558                      to_be_resorted => pt
559                   ELSE
560                   DO  i = nxl, nxr
561                      DO  j = nys, nyn
562                            DO  k = nzb, nzt+1
563                               local_pf(i,j,k) = pt(k,j,i) + lv_d_cp *         &
564                                                             d_exner(k) *      &
565                                                             ql(k,j,i)
566                            ENDDO
567                         ENDDO
568                      ENDDO
569                      resorted = .TRUE.
570                   ENDIF
571                ELSE
572                   IF ( .NOT. ALLOCATED( pt_av ) ) THEN
573                      ALLOCATE( pt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
574                      pt_av = REAL( fill_value, KIND = wp )
575                   ENDIF
576                   to_be_resorted => pt_av
577                ENDIF
578                IF ( mode == 'xy' )  level_z = zu
579
580             CASE ( 'q_xy', 'q_xz', 'q_yz' )
581                IF ( av == 0 )  THEN
582                   to_be_resorted => q
583                ELSE
584                   IF ( .NOT. ALLOCATED( q_av ) ) THEN
585                      ALLOCATE( q_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
586                      q_av = REAL( fill_value, KIND = wp )
587                   ENDIF
588                   to_be_resorted => q_av
589                ENDIF
590                IF ( mode == 'xy' )  level_z = zu
591
592             CASE ( 'ql_xy', 'ql_xz', 'ql_yz' )
593                IF ( av == 0 )  THEN
594                   to_be_resorted => ql
595                ELSE
596                   IF ( .NOT. ALLOCATED( ql_av ) ) THEN
597                      ALLOCATE( ql_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
598                      ql_av = REAL( fill_value, KIND = wp )
599                   ENDIF
600                   to_be_resorted => ql_av
601                ENDIF
602                IF ( mode == 'xy' )  level_z = zu
603
604             CASE ( 'ql_c_xy', 'ql_c_xz', 'ql_c_yz' )
605                IF ( av == 0 )  THEN
606                   to_be_resorted => ql_c
607                ELSE
608                   IF ( .NOT. ALLOCATED( ql_c_av ) ) THEN
609                      ALLOCATE( ql_c_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
610                      ql_c_av = REAL( fill_value, KIND = wp )
611                   ENDIF
612                   to_be_resorted => ql_c_av
613                ENDIF
614                IF ( mode == 'xy' )  level_z = zu
615
616             CASE ( 'ql_v_xy', 'ql_v_xz', 'ql_v_yz' )
617                IF ( av == 0 )  THEN
618                   to_be_resorted => ql_v
619                ELSE
620                   IF ( .NOT. ALLOCATED( ql_v_av ) ) THEN
621                      ALLOCATE( ql_v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
622                      ql_v_av = REAL( fill_value, KIND = wp )
623                   ENDIF
624                   to_be_resorted => ql_v_av
625                ENDIF
626                IF ( mode == 'xy' )  level_z = zu
627
628             CASE ( 'ql_vp_xy', 'ql_vp_xz', 'ql_vp_yz' )
629                IF ( av == 0 )  THEN
630                   IF ( time_since_reference_point >= particle_advection_start )  THEN
631                      DO  i = nxl, nxr
632                         DO  j = nys, nyn
633                            DO  k = nzb, nzt+1
634                               number_of_particles = prt_count(k,j,i)
635                               IF (number_of_particles <= 0)  CYCLE
636                               particles => grid_particles(k,j,i)%particles(1:number_of_particles)
637                               DO  n = 1, number_of_particles
638                                  IF ( particles(n)%particle_mask )  THEN
639                                     tend(k,j,i) =  tend(k,j,i) +                 &
640                                                    particles(n)%weight_factor /  &
641                                                    prt_count(k,j,i)
642                                  ENDIF
643                               ENDDO
644                            ENDDO
645                         ENDDO
646                      ENDDO
647!                      CALL exchange_horiz( tend, nbgp )
648                   ELSE
649                      tend = 0.0_wp
650                   ENDIF
651                   DO  i = nxl, nxr
652                      DO  j = nys, nyn
653                         DO  k = nzb, nzt+1
654                            local_pf(i,j,k) = tend(k,j,i)
655                         ENDDO
656                      ENDDO
657                   ENDDO
658                   resorted = .TRUE.
659                ELSE
660                   IF ( .NOT. ALLOCATED( ql_vp_av ) ) THEN
661                      ALLOCATE( ql_vp_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
662                      ql_vp_av = REAL( fill_value, KIND = wp )
663                   ENDIF
664!                   CALL exchange_horiz( ql_vp_av, nbgp )
665                   to_be_resorted => ql_vp_av
666                ENDIF
667                IF ( mode == 'xy' )  level_z = zu
668
669             CASE ( 'qsws*_xy' )        ! 2d-array
670                IF ( av == 0 ) THEN
671                   local_pf(:,:,nzb+1) = REAL( fill_value, KIND = wp )
672!
673!--                In case of default surfaces, clean-up flux by density.
674!--                In case of land-surfaces, convert fluxes into
675!--                dynamic units
676                   DO  m = 1, surf_def_h(0)%ns
677                      i = surf_def_h(0)%i(m)
678                      j = surf_def_h(0)%j(m)
679                      k = surf_def_h(0)%k(m)
680                      local_pf(i,j,nzb+1) = surf_def_h(0)%qsws(m) *            &
681                                            waterflux_output_conversion(k)
682                   ENDDO
683                   DO  m = 1, surf_lsm_h%ns
684                      i = surf_lsm_h%i(m)
685                      j = surf_lsm_h%j(m)
686                      k = surf_lsm_h%k(m)
687                      local_pf(i,j,nzb+1) = surf_lsm_h%qsws(m) * l_v
688                   ENDDO
689                   DO  m = 1, surf_usm_h%ns
690                      i = surf_usm_h%i(m)
691                      j = surf_usm_h%j(m)
692                      k = surf_usm_h%k(m)
693                      local_pf(i,j,nzb+1) = surf_usm_h%qsws(m) * l_v
694                   ENDDO
695                ELSE
696                   IF ( .NOT. ALLOCATED( qsws_av ) ) THEN
697                      ALLOCATE( qsws_av(nysg:nyng,nxlg:nxrg) )
698                      qsws_av = REAL( fill_value, KIND = wp )
699                   ENDIF
700                   DO  i = nxl, nxr
701                      DO  j = nys, nyn 
702                         local_pf(i,j,nzb+1) =  qsws_av(j,i)
703                      ENDDO
704                   ENDDO
705                ENDIF
706                resorted = .TRUE.
707                two_d = .TRUE.
708                level_z(nzb+1) = zu(nzb+1)
709
710             CASE ( 'qv_xy', 'qv_xz', 'qv_yz' )
711                IF ( av == 0 )  THEN
712                   DO  i = nxl, nxr
713                      DO  j = nys, nyn
714                         DO  k = nzb, nzt+1
715                            local_pf(i,j,k) = q(k,j,i) - ql(k,j,i)
716                         ENDDO
717                      ENDDO
718                   ENDDO
719                   resorted = .TRUE.
720                ELSE
721                   IF ( .NOT. ALLOCATED( qv_av ) ) THEN
722                      ALLOCATE( qv_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
723                      qv_av = REAL( fill_value, KIND = wp )
724                   ENDIF
725                   to_be_resorted => qv_av
726                ENDIF
727                IF ( mode == 'xy' )  level_z = zu
728
729             CASE ( 'r_a*_xy' )        ! 2d-array
730                IF ( av == 0 )  THEN
731                   DO  m = 1, surf_lsm_h%ns
732                      i                   = surf_lsm_h%i(m)           
733                      j                   = surf_lsm_h%j(m)
734                      local_pf(i,j,nzb+1) = surf_lsm_h%r_a(m)
735                   ENDDO
736
737                   DO  m = 1, surf_usm_h%ns
738                      i   = surf_usm_h%i(m)           
739                      j   = surf_usm_h%j(m)
740                      local_pf(i,j,nzb+1) =                                          &
741                                 ( surf_usm_h%frac(ind_veg_wall,m)  *                &
742                                   surf_usm_h%r_a(m)       +                         & 
743                                   surf_usm_h%frac(ind_pav_green,m) *                &
744                                   surf_usm_h%r_a_green(m) +                         & 
745                                   surf_usm_h%frac(ind_wat_win,m)   *                &
746                                   surf_usm_h%r_a_window(m) )
747                   ENDDO
748                ELSE
749                   IF ( .NOT. ALLOCATED( r_a_av ) ) THEN
750                      ALLOCATE( r_a_av(nysg:nyng,nxlg:nxrg) )
751                      r_a_av = REAL( fill_value, KIND = wp )
752                   ENDIF
753                   DO  i = nxl, nxr
754                      DO  j = nys, nyn
755                         local_pf(i,j,nzb+1) = r_a_av(j,i)
756                      ENDDO
757                   ENDDO
758                ENDIF
759                resorted       = .TRUE.
760                two_d          = .TRUE.
761                level_z(nzb+1) = zu(nzb+1)
762
763             CASE ( 's_xy', 's_xz', 's_yz' )
764                IF ( av == 0 )  THEN
765                   to_be_resorted => s
766                ELSE
767                   IF ( .NOT. ALLOCATED( s_av ) ) THEN
768                      ALLOCATE( s_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
769                      s_av = REAL( fill_value, KIND = wp )
770                   ENDIF
771                   to_be_resorted => s_av
772                ENDIF
773
774             CASE ( 'shf*_xy' )        ! 2d-array
775                IF ( av == 0 ) THEN
776!
777!--                In case of default surfaces, clean-up flux by density.
778!--                In case of land- and urban-surfaces, convert fluxes into
779!--                dynamic units.
780                   DO  m = 1, surf_def_h(0)%ns
781                      i = surf_def_h(0)%i(m)
782                      j = surf_def_h(0)%j(m)
783                      k = surf_def_h(0)%k(m)
784                      local_pf(i,j,nzb+1) = surf_def_h(0)%shf(m) *             &
785                                            heatflux_output_conversion(k)
786                   ENDDO
787                   DO  m = 1, surf_lsm_h%ns
788                      i = surf_lsm_h%i(m)
789                      j = surf_lsm_h%j(m)
790                      k = surf_lsm_h%k(m)
791                      local_pf(i,j,nzb+1) = surf_lsm_h%shf(m) * c_p
792                   ENDDO
793                   DO  m = 1, surf_usm_h%ns
794                      i = surf_usm_h%i(m)
795                      j = surf_usm_h%j(m)
796                      k = surf_usm_h%k(m)
797                      local_pf(i,j,nzb+1) = surf_usm_h%shf(m) * c_p
798                   ENDDO
799                ELSE
800                   IF ( .NOT. ALLOCATED( shf_av ) ) THEN
801                      ALLOCATE( shf_av(nysg:nyng,nxlg:nxrg) )
802                      shf_av = REAL( fill_value, KIND = wp )
803                   ENDIF
804                   DO  i = nxl, nxr
805                      DO  j = nys, nyn
806                         local_pf(i,j,nzb+1) =  shf_av(j,i)
807                      ENDDO
808                   ENDDO
809                ENDIF
810                resorted = .TRUE.
811                two_d = .TRUE.
812                level_z(nzb+1) = zu(nzb+1)
813               
814             CASE ( 'ssws*_xy' )        ! 2d-array
815                IF ( av == 0 ) THEN
816                   DO  m = 1, surf_def_h(0)%ns
817                      i = surf_def_h(0)%i(m)
818                      j = surf_def_h(0)%j(m)
819                      local_pf(i,j,nzb+1) = surf_def_h(0)%ssws(m)
820                   ENDDO
821                   DO  m = 1, surf_lsm_h%ns
822                      i = surf_lsm_h%i(m)
823                      j = surf_lsm_h%j(m)
824                      local_pf(i,j,nzb+1) = surf_lsm_h%ssws(m)
825                   ENDDO
826                   DO  m = 1, surf_usm_h%ns
827                      i = surf_usm_h%i(m)
828                      j = surf_usm_h%j(m)
829                      local_pf(i,j,nzb+1) = surf_usm_h%ssws(m)
830                   ENDDO
831                ELSE
832                   IF ( .NOT. ALLOCATED( ssws_av ) ) THEN
833                      ALLOCATE( ssws_av(nysg:nyng,nxlg:nxrg) )
834                      ssws_av = REAL( fill_value, KIND = wp )
835                   ENDIF
836                   DO  i = nxl, nxr
837                      DO  j = nys, nyn 
838                         local_pf(i,j,nzb+1) =  ssws_av(j,i)
839                      ENDDO
840                   ENDDO
841                ENDIF
842                resorted = .TRUE.
843                two_d = .TRUE.
844                level_z(nzb+1) = zu(nzb+1)               
845
846             CASE ( 't*_xy' )        ! 2d-array
847                IF ( av == 0 )  THEN
848                   DO  m = 1, surf_def_h(0)%ns
849                      i = surf_def_h(0)%i(m)
850                      j = surf_def_h(0)%j(m)
851                      local_pf(i,j,nzb+1) = surf_def_h(0)%ts(m)
852                   ENDDO
853                   DO  m = 1, surf_lsm_h%ns
854                      i = surf_lsm_h%i(m)
855                      j = surf_lsm_h%j(m)
856                      local_pf(i,j,nzb+1) = surf_lsm_h%ts(m)
857                   ENDDO
858                   DO  m = 1, surf_usm_h%ns
859                      i = surf_usm_h%i(m)
860                      j = surf_usm_h%j(m)
861                      local_pf(i,j,nzb+1) = surf_usm_h%ts(m)
862                   ENDDO
863                ELSE
864                   IF ( .NOT. ALLOCATED( ts_av ) ) THEN
865                      ALLOCATE( ts_av(nysg:nyng,nxlg:nxrg) )
866                      ts_av = REAL( fill_value, KIND = wp )
867                   ENDIF
868                   DO  i = nxl, nxr
869                      DO  j = nys, nyn
870                         local_pf(i,j,nzb+1) = ts_av(j,i)
871                      ENDDO
872                   ENDDO
873                ENDIF
874                resorted = .TRUE.
875                two_d = .TRUE.
876                level_z(nzb+1) = zu(nzb+1)
877
878             CASE ( 'tsurf*_xy' )        ! 2d-array
879                IF ( av == 0 )  THEN
880                   DO  m = 1, surf_def_h(0)%ns
881                      i                   = surf_def_h(0)%i(m)           
882                      j                   = surf_def_h(0)%j(m)
883                      local_pf(i,j,nzb+1) = surf_def_h(0)%pt_surface(m)
884                   ENDDO
885
886                   DO  m = 1, surf_lsm_h%ns
887                      i                   = surf_lsm_h%i(m)           
888                      j                   = surf_lsm_h%j(m)
889                      local_pf(i,j,nzb+1) = surf_lsm_h%pt_surface(m)
890                   ENDDO
891
892                   DO  m = 1, surf_usm_h%ns
893                      i   = surf_usm_h%i(m)           
894                      j   = surf_usm_h%j(m)
895                      local_pf(i,j,nzb+1) = surf_usm_h%pt_surface(m)
896                   ENDDO
897
898                ELSE
899                   IF ( .NOT. ALLOCATED( tsurf_av ) ) THEN
900                      ALLOCATE( tsurf_av(nysg:nyng,nxlg:nxrg) )
901                      tsurf_av = REAL( fill_value, KIND = wp )
902                   ENDIF
903                   DO  i = nxl, nxr
904                      DO  j = nys, nyn
905                         local_pf(i,j,nzb+1) = tsurf_av(j,i)
906                      ENDDO
907                   ENDDO
908                ENDIF
909                resorted       = .TRUE.
910                two_d          = .TRUE.
911                level_z(nzb+1) = zu(nzb+1)
912
913             CASE ( 'u_xy', 'u_xz', 'u_yz' )
914                flag_nr = 1
915                IF ( av == 0 )  THEN
916                   to_be_resorted => u
917                ELSE
918                   IF ( .NOT. ALLOCATED( u_av ) ) THEN
919                      ALLOCATE( u_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
920                      u_av = REAL( fill_value, KIND = wp )
921                   ENDIF
922                   to_be_resorted => u_av
923                ENDIF
924                IF ( mode == 'xy' )  level_z = zu
925!
926!--             Substitute the values generated by "mirror" boundary condition
927!--             at the bottom boundary by the real surface values.
928                IF ( do2d(av,ivar) == 'u_xz'  .OR.  do2d(av,ivar) == 'u_yz' )  THEN
929                   IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0_wp
930                ENDIF
931               
932             CASE ( 'us*_xy' )        ! 2d-array
933                IF ( av == 0 )  THEN
934                   DO  m = 1, surf_def_h(0)%ns
935                      i = surf_def_h(0)%i(m)
936                      j = surf_def_h(0)%j(m)
937                      local_pf(i,j,nzb+1) = surf_def_h(0)%us(m)
938                   ENDDO
939                   DO  m = 1, surf_lsm_h%ns
940                      i = surf_lsm_h%i(m)
941                      j = surf_lsm_h%j(m)
942                      local_pf(i,j,nzb+1) = surf_lsm_h%us(m)
943                   ENDDO
944                   DO  m = 1, surf_usm_h%ns
945                      i = surf_usm_h%i(m)
946                      j = surf_usm_h%j(m)
947                      local_pf(i,j,nzb+1) = surf_usm_h%us(m)
948                   ENDDO
949                ELSE
950                   IF ( .NOT. ALLOCATED( us_av ) ) THEN
951                      ALLOCATE( us_av(nysg:nyng,nxlg:nxrg) )
952                      us_av = REAL( fill_value, KIND = wp )
953                   ENDIF
954                   DO  i = nxl, nxr
955                      DO  j = nys, nyn
956                         local_pf(i,j,nzb+1) = us_av(j,i)
957                      ENDDO
958                   ENDDO
959                ENDIF
960                resorted = .TRUE.
961                two_d = .TRUE.
962                level_z(nzb+1) = zu(nzb+1)
963
964             CASE ( 'v_xy', 'v_xz', 'v_yz' )
965                flag_nr = 2
966                IF ( av == 0 )  THEN
967                   to_be_resorted => v
968                ELSE
969                   IF ( .NOT. ALLOCATED( v_av ) ) THEN
970                      ALLOCATE( v_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
971                      v_av = REAL( fill_value, KIND = wp )
972                   ENDIF
973                   to_be_resorted => v_av
974                ENDIF
975                IF ( mode == 'xy' )  level_z = zu
976!
977!--             Substitute the values generated by "mirror" boundary condition
978!--             at the bottom boundary by the real surface values.
979                IF ( do2d(av,ivar) == 'v_xz'  .OR.  do2d(av,ivar) == 'v_yz' )  THEN
980                   IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0_wp
981                ENDIF
982
983             CASE ( 'thetav_xy', 'thetav_xz', 'thetav_yz' )
984                IF ( av == 0 )  THEN
985                   to_be_resorted => vpt
986                ELSE
987                   IF ( .NOT. ALLOCATED( vpt_av ) ) THEN
988                      ALLOCATE( vpt_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
989                      vpt_av = REAL( fill_value, KIND = wp )
990                   ENDIF
991                   to_be_resorted => vpt_av
992                ENDIF
993                IF ( mode == 'xy' )  level_z = zu
994
995             CASE ( 'theta_2m*_xy' )        ! 2d-array
996                IF ( av == 0 )  THEN
997                   DO  m = 1, surf_def_h(0)%ns
998                      i = surf_def_h(0)%i(m)
999                      j = surf_def_h(0)%j(m)
1000                      local_pf(i,j,nzb+1) = surf_def_h(0)%pt_2m(m)
1001                   ENDDO
1002                   DO  m = 1, surf_lsm_h%ns
1003                      i = surf_lsm_h%i(m)
1004                      j = surf_lsm_h%j(m)
1005                      local_pf(i,j,nzb+1) = surf_lsm_h%pt_2m(m)
1006                   ENDDO
1007                   DO  m = 1, surf_usm_h%ns
1008                      i = surf_usm_h%i(m)
1009                      j = surf_usm_h%j(m)
1010                      local_pf(i,j,nzb+1) = surf_usm_h%pt_2m(m)
1011                   ENDDO
1012                ELSE
1013                   IF ( .NOT. ALLOCATED( pt_2m_av ) ) THEN
1014                      ALLOCATE( pt_2m_av(nysg:nyng,nxlg:nxrg) )
1015                      pt_2m_av = REAL( fill_value, KIND = wp )
1016                   ENDIF
1017                   DO  i = nxl, nxr
1018                      DO  j = nys, nyn
1019                         local_pf(i,j,nzb+1) = pt_2m_av(j,i)
1020                      ENDDO
1021                   ENDDO
1022                ENDIF
1023                resorted = .TRUE.
1024                two_d = .TRUE.
1025                level_z(nzb+1) = zu(nzb+1)
1026
1027             CASE ( 'w_xy', 'w_xz', 'w_yz' )
1028                flag_nr = 3
1029                IF ( av == 0 )  THEN
1030                   to_be_resorted => w
1031                ELSE
1032                   IF ( .NOT. ALLOCATED( w_av ) ) THEN
1033                      ALLOCATE( w_av(nzb:nzt+1,nysg:nyng,nxlg:nxrg) )
1034                      w_av = REAL( fill_value, KIND = wp )
1035                   ENDIF
1036                   to_be_resorted => w_av
1037                ENDIF
1038                IF ( mode == 'xy' )  level_z = zw
1039
1040             CASE ( 'z0*_xy' )        ! 2d-array
1041                IF ( av == 0 ) THEN
1042                   DO  m = 1, surf_def_h(0)%ns
1043                      i = surf_def_h(0)%i(m)
1044                      j = surf_def_h(0)%j(m)
1045                      local_pf(i,j,nzb+1) = surf_def_h(0)%z0(m)
1046                   ENDDO
1047                   DO  m = 1, surf_lsm_h%ns
1048                      i = surf_lsm_h%i(m)
1049                      j = surf_lsm_h%j(m)
1050                      local_pf(i,j,nzb+1) = surf_lsm_h%z0(m)
1051                   ENDDO
1052                   DO  m = 1, surf_usm_h%ns
1053                      i = surf_usm_h%i(m)
1054                      j = surf_usm_h%j(m)
1055                      local_pf(i,j,nzb+1) = surf_usm_h%z0(m)
1056                   ENDDO
1057                ELSE
1058                   IF ( .NOT. ALLOCATED( z0_av ) ) THEN
1059                      ALLOCATE( z0_av(nysg:nyng,nxlg:nxrg) )
1060                      z0_av = REAL( fill_value, KIND = wp )
1061                   ENDIF
1062                   DO  i = nxl, nxr
1063                      DO  j = nys, nyn
1064                         local_pf(i,j,nzb+1) =  z0_av(j,i)
1065                      ENDDO
1066                   ENDDO
1067                ENDIF
1068                resorted = .TRUE.
1069                two_d = .TRUE.
1070                level_z(nzb+1) = zu(nzb+1)
1071
1072             CASE ( 'z0h*_xy' )        ! 2d-array
1073                IF ( av == 0 ) THEN
1074                   DO  m = 1, surf_def_h(0)%ns
1075                      i = surf_def_h(0)%i(m)
1076                      j = surf_def_h(0)%j(m)
1077                      local_pf(i,j,nzb+1) = surf_def_h(0)%z0h(m)
1078                   ENDDO
1079                   DO  m = 1, surf_lsm_h%ns
1080                      i = surf_lsm_h%i(m)
1081                      j = surf_lsm_h%j(m)
1082                      local_pf(i,j,nzb+1) = surf_lsm_h%z0h(m)
1083                   ENDDO
1084                   DO  m = 1, surf_usm_h%ns
1085                      i = surf_usm_h%i(m)
1086                      j = surf_usm_h%j(m)
1087                      local_pf(i,j,nzb+1) = surf_usm_h%z0h(m)
1088                   ENDDO
1089                ELSE
1090                   IF ( .NOT. ALLOCATED( z0h_av ) ) THEN
1091                      ALLOCATE( z0h_av(nysg:nyng,nxlg:nxrg) )
1092                      z0h_av = REAL( fill_value, KIND = wp )
1093                   ENDIF
1094                   DO  i = nxl, nxr
1095                      DO  j = nys, nyn
1096                         local_pf(i,j,nzb+1) =  z0h_av(j,i)
1097                      ENDDO
1098                   ENDDO
1099                ENDIF
1100                resorted = .TRUE.
1101                two_d = .TRUE.
1102                level_z(nzb+1) = zu(nzb+1)
1103
1104             CASE ( 'z0q*_xy' )        ! 2d-array
1105                IF ( av == 0 ) THEN
1106                   DO  m = 1, surf_def_h(0)%ns
1107                      i = surf_def_h(0)%i(m)
1108                      j = surf_def_h(0)%j(m)
1109                      local_pf(i,j,nzb+1) = surf_def_h(0)%z0q(m)
1110                   ENDDO
1111                   DO  m = 1, surf_lsm_h%ns
1112                      i = surf_lsm_h%i(m)
1113                      j = surf_lsm_h%j(m)
1114                      local_pf(i,j,nzb+1) = surf_lsm_h%z0q(m)
1115                   ENDDO
1116                   DO  m = 1, surf_usm_h%ns
1117                      i = surf_usm_h%i(m)
1118                      j = surf_usm_h%j(m)
1119                      local_pf(i,j,nzb+1) = surf_usm_h%z0q(m)
1120                   ENDDO
1121                ELSE
1122                   IF ( .NOT. ALLOCATED( z0q_av ) ) THEN
1123                      ALLOCATE( z0q_av(nysg:nyng,nxlg:nxrg) )
1124                      z0q_av = REAL( fill_value, KIND = wp )
1125                   ENDIF
1126                   DO  i = nxl, nxr
1127                      DO  j = nys, nyn
1128                         local_pf(i,j,nzb+1) =  z0q_av(j,i)
1129                      ENDDO
1130                   ENDDO
1131                ENDIF
1132                resorted = .TRUE.
1133                two_d = .TRUE.
1134                level_z(nzb+1) = zu(nzb+1)
1135
1136             CASE DEFAULT
1137
1138!
1139!--             Quantities of other modules
1140                IF ( .NOT. found )  THEN
1141                   CALL module_interface_data_output_2d(                       &
1142                           av, do2d(av,ivar), found, grid, mode,               &
1143                           local_pf, two_d, nzb_do, nzt_do,                    &
1144                           fill_value                                          &
1145                        )
1146                ENDIF
1147
1148                resorted = .TRUE.
1149
1150                IF ( grid == 'zu' )  THEN
1151                   IF ( mode == 'xy' )  level_z = zu
1152                ELSEIF ( grid == 'zw' )  THEN
1153                   IF ( mode == 'xy' )  level_z = zw
1154                ELSEIF ( grid == 'zu1' ) THEN
1155                   IF ( mode == 'xy' )  level_z(nzb+1) = zu(nzb+1)
1156                ELSEIF ( grid == 'zs' ) THEN
1157                   IF ( mode == 'xy' )  level_z = zs
1158                ENDIF
1159
1160                IF ( .NOT. found )  THEN
1161                   message_string = 'no output provided for: ' //              &
1162                                    TRIM( do2d(av,ivar) )
1163                   CALL message( 'data_output_2d', 'PA0181', 0, 0, 0, 6, 0 )
1164                ENDIF
1165
1166          END SELECT
1167
1168!
1169!--       Resort the array to be output, if not done above. Flag topography
1170!--       grid points with fill values, using the corresponding maksing flag.
1171          IF ( .NOT. resorted )  THEN
1172             DO  i = nxl, nxr
1173                DO  j = nys, nyn
1174                   DO  k = nzb_do, nzt_do
1175                      local_pf(i,j,k) = MERGE( to_be_resorted(k,j,i),          &
1176                                               REAL( fill_value, KIND = wp ),  &
1177                                               BTEST( wall_flags_0(k,j,i),     &
1178                                                      flag_nr ) ) 
1179                   ENDDO
1180                ENDDO
1181             ENDDO
1182          ENDIF
1183
1184!
1185!--       Output of the individual cross-sections, depending on the cross-
1186!--       section mode chosen.
1187          is = 1
1188   loop1: DO WHILE ( section(is,s_ind) /= -9999  .OR.  two_d )
1189
1190             SELECT CASE ( mode )
1191
1192                CASE ( 'xy' )
1193!
1194!--                Determine the cross section index
1195                   IF ( two_d )  THEN
1196                      layer_xy = nzb+1
1197                   ELSE
1198                      layer_xy = section(is,s_ind)
1199                   ENDIF
1200
1201!
1202!--                Exit the loop for layers beyond the data output domain
1203!--                (used for soil model)
1204                   IF ( layer_xy > nzt_do )  THEN
1205                      EXIT loop1
1206                   ENDIF
1207
1208!
1209!--                Update the netCDF xy cross section time axis.
1210!--                In case of parallel output, this is only done by PE0
1211!--                to increase the performance.
1212                   IF ( time_since_reference_point /= do2d_xy_last_time(av) )  THEN
1213                      do2d_xy_time_count(av) = do2d_xy_time_count(av) + 1
1214                      do2d_xy_last_time(av)  = time_since_reference_point
1215                      IF ( myid == 0 )  THEN
1216                         IF ( .NOT. data_output_2d_on_each_pe  &
1217                              .OR.  netcdf_data_format > 4 )   &
1218                         THEN
1219#if defined( __netcdf )
1220                            nc_stat = NF90_PUT_VAR( id_set_xy(av),             &
1221                                                    id_var_time_xy(av),        &
1222                                             (/ time_since_reference_point /), &
1223                                         start = (/ do2d_xy_time_count(av) /), &
1224                                                    count = (/ 1 /) )
1225                            CALL netcdf_handle_error( 'data_output_2d', 53 )
1226#endif
1227                         ENDIF
1228                      ENDIF
1229                   ENDIF
1230!
1231!--                If required, carry out averaging along z
1232                   IF ( section(is,s_ind) == -1  .AND.  .NOT. two_d )  THEN
1233
1234                      local_2d = 0.0_wp
1235!
1236!--                   Carry out the averaging (all data are on the PE)
1237                      DO  k = nzb_do, nzt_do
1238                         DO  j = nys, nyn
1239                            DO  i = nxl, nxr
1240                               local_2d(i,j) = local_2d(i,j) + local_pf(i,j,k)
1241                            ENDDO
1242                         ENDDO
1243                      ENDDO
1244
1245                      local_2d = local_2d / ( nzt_do - nzb_do + 1.0_wp)
1246
1247                   ELSE
1248!
1249!--                   Just store the respective section on the local array
1250                      local_2d = local_pf(:,:,layer_xy)
1251
1252                   ENDIF
1253
1254#if defined( __parallel )
1255                   IF ( netcdf_data_format > 4 )  THEN
1256!
1257!--                   Parallel output in netCDF4/HDF5 format.
1258                      IF ( two_d ) THEN
1259                         iis = 1
1260                      ELSE
1261                         iis = is
1262                      ENDIF
1263
1264#if defined( __netcdf )
1265!
1266!--                   For parallel output, all cross sections are first stored
1267!--                   here on a local array and will be written to the output
1268!--                   file afterwards to increase the performance.
1269                      DO  i = nxl, nxr
1270                         DO  j = nys, nyn
1271                            local_2d_sections(i,j,iis) = local_2d(i,j)
1272                         ENDDO
1273                      ENDDO
1274#endif
1275                   ELSE
1276
1277                      IF ( data_output_2d_on_each_pe )  THEN
1278!
1279!--                      Output of partial arrays on each PE
1280#if defined( __netcdf )
1281                         IF ( myid == 0 )  THEN
1282                            WRITE ( 21 )  time_since_reference_point,          &
1283                                          do2d_xy_time_count(av), av
1284                         ENDIF
1285#endif
1286                         DO  i = 0, io_blocks-1
1287                            IF ( i == io_group )  THEN
1288                               WRITE ( 21 )  nxl, nxr, nys, nyn, nys, nyn
1289                               WRITE ( 21 )  local_2d
1290                            ENDIF
1291#if defined( __parallel )
1292                            CALL MPI_BARRIER( comm2d, ierr )
1293#endif
1294                         ENDDO
1295
1296                      ELSE
1297!
1298!--                      PE0 receives partial arrays from all processors and
1299!--                      then outputs them. Here a barrier has to be set,
1300!--                      because otherwise "-MPI- FATAL: Remote protocol queue
1301!--                      full" may occur.
1302                         CALL MPI_BARRIER( comm2d, ierr )
1303
1304                         ngp = ( nxr-nxl+1 ) * ( nyn-nys+1 )
1305                         IF ( myid == 0 )  THEN
1306!
1307!--                         Local array can be relocated directly.
1308                            total_2d(nxl:nxr,nys:nyn) = local_2d
1309!
1310!--                         Receive data from all other PEs.
1311                            DO  n = 1, numprocs-1
1312!
1313!--                            Receive index limits first, then array.
1314!--                            Index limits are received in arbitrary order from
1315!--                            the PEs.
1316                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,          &
1317                                              MPI_ANY_SOURCE, 0, comm2d,       &
1318                                              status, ierr )
1319                               sender = status(MPI_SOURCE)
1320                               DEALLOCATE( local_2d )
1321                               ALLOCATE( local_2d(ind(1):ind(2),ind(3):ind(4)) )
1322                               CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp,    &
1323                                              MPI_REAL, sender, 1, comm2d,     &
1324                                              status, ierr )
1325                               total_2d(ind(1):ind(2),ind(3):ind(4)) = local_2d
1326                            ENDDO
1327!
1328!--                         Relocate the local array for the next loop increment
1329                            DEALLOCATE( local_2d )
1330                            ALLOCATE( local_2d(nxl:nxr,nys:nyn) )
1331
1332#if defined( __netcdf )
1333                            IF ( two_d ) THEN
1334                               nc_stat = NF90_PUT_VAR( id_set_xy(av),       &
1335                                                       id_var_do2d(av,ivar),  &
1336                                                       total_2d(0:nx,0:ny), &
1337                             start = (/ 1, 1, 1, do2d_xy_time_count(av) /), &
1338                                             count = (/ nx+1, ny+1, 1, 1 /) )
1339                            ELSE
1340                               nc_stat = NF90_PUT_VAR( id_set_xy(av),       &
1341                                                       id_var_do2d(av,ivar),  &
1342                                                       total_2d(0:nx,0:ny), &
1343                            start = (/ 1, 1, is, do2d_xy_time_count(av) /), &
1344                                             count = (/ nx+1, ny+1, 1, 1 /) )
1345                            ENDIF
1346                            CALL netcdf_handle_error( 'data_output_2d', 54 )
1347#endif
1348
1349                         ELSE
1350!
1351!--                         First send the local index limits to PE0
1352                            ind(1) = nxl; ind(2) = nxr
1353                            ind(3) = nys; ind(4) = nyn
1354                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0,       &
1355                                           comm2d, ierr )
1356!
1357!--                         Send data to PE0
1358                            CALL MPI_SEND( local_2d(nxl,nys), ngp,             &
1359                                           MPI_REAL, 0, 1, comm2d, ierr )
1360                         ENDIF
1361!
1362!--                      A barrier has to be set, because otherwise some PEs may
1363!--                      proceed too fast so that PE0 may receive wrong data on
1364!--                      tag 0
1365                         CALL MPI_BARRIER( comm2d, ierr )
1366                      ENDIF
1367
1368                   ENDIF
1369#else
1370#if defined( __netcdf )
1371                   IF ( two_d ) THEN
1372                      nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1373                                              id_var_do2d(av,ivar),           &
1374                                              local_2d(nxl:nxr,nys:nyn),    &
1375                             start = (/ 1, 1, 1, do2d_xy_time_count(av) /), &
1376                                           count = (/ nx+1, ny+1, 1, 1 /) )
1377                   ELSE
1378                      nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1379                                              id_var_do2d(av,ivar),           &
1380                                              local_2d(nxl:nxr,nys:nyn),    &
1381                            start = (/ 1, 1, is, do2d_xy_time_count(av) /), &
1382                                           count = (/ nx+1, ny+1, 1, 1 /) )
1383                   ENDIF
1384                   CALL netcdf_handle_error( 'data_output_2d', 447 )
1385#endif
1386#endif
1387
1388!
1389!--                For 2D-arrays (e.g. u*) only one cross-section is available.
1390!--                Hence exit loop of output levels.
1391                   IF ( two_d )  THEN
1392                      IF ( netcdf_data_format < 5 )  two_d = .FALSE.
1393                      EXIT loop1
1394                   ENDIF
1395
1396                CASE ( 'xz' )
1397!
1398!--                Update the netCDF xz cross section time axis.
1399!--                In case of parallel output, this is only done by PE0
1400!--                to increase the performance.
1401                   IF ( time_since_reference_point /= do2d_xz_last_time(av) )  THEN
1402                      do2d_xz_time_count(av) = do2d_xz_time_count(av) + 1
1403                      do2d_xz_last_time(av)  = time_since_reference_point
1404                      IF ( myid == 0 )  THEN
1405                         IF ( .NOT. data_output_2d_on_each_pe  &
1406                              .OR.  netcdf_data_format > 4 )   &
1407                         THEN
1408#if defined( __netcdf )
1409                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
1410                                                    id_var_time_xz(av),        &
1411                                             (/ time_since_reference_point /), &
1412                                         start = (/ do2d_xz_time_count(av) /), &
1413                                                    count = (/ 1 /) )
1414                            CALL netcdf_handle_error( 'data_output_2d', 56 )
1415#endif
1416                         ENDIF
1417                      ENDIF
1418                   ENDIF
1419
1420!
1421!--                If required, carry out averaging along y
1422                   IF ( section(is,s_ind) == -1 )  THEN
1423
1424                      ALLOCATE( local_2d_l(nxl:nxr,nzb_do:nzt_do) )
1425                      local_2d_l = 0.0_wp
1426                      ngp = ( nxr-nxl + 1 ) * ( nzt_do-nzb_do + 1 )
1427!
1428!--                   First local averaging on the PE
1429                      DO  k = nzb_do, nzt_do
1430                         DO  j = nys, nyn
1431                            DO  i = nxl, nxr
1432                               local_2d_l(i,k) = local_2d_l(i,k) +             &
1433                                                 local_pf(i,j,k)
1434                            ENDDO
1435                         ENDDO
1436                      ENDDO
1437#if defined( __parallel )
1438!
1439!--                   Now do the averaging over all PEs along y
1440                      IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1441                      CALL MPI_ALLREDUCE( local_2d_l(nxl,nzb_do),                &
1442                                          local_2d(nxl,nzb_do), ngp, MPI_REAL,   &
1443                                          MPI_SUM, comm1dy, ierr )
1444#else
1445                      local_2d = local_2d_l
1446#endif
1447                      local_2d = local_2d / ( ny + 1.0_wp )
1448
1449                      DEALLOCATE( local_2d_l )
1450
1451                   ELSE
1452!
1453!--                   Just store the respective section on the local array
1454!--                   (but only if it is available on this PE!)
1455                      IF ( section(is,s_ind) >= nys  .AND.  section(is,s_ind) <= nyn ) &
1456                      THEN
1457                         local_2d = local_pf(:,section(is,s_ind),nzb_do:nzt_do)
1458                      ENDIF
1459
1460                   ENDIF
1461
1462#if defined( __parallel )
1463                   IF ( netcdf_data_format > 4 )  THEN
1464!
1465!--                   Output in netCDF4/HDF5 format.
1466!--                   Output only on those PEs where the respective cross
1467!--                   sections reside. Cross sections averaged along y are
1468!--                   output on the respective first PE along y (myidy=0).
1469                      IF ( ( section(is,s_ind) >= nys  .AND.                   &
1470                             section(is,s_ind) <= nyn )  .OR.                  &
1471                           ( section(is,s_ind) == -1  .AND.  myidy == 0 ) )  THEN
1472#if defined( __netcdf )
1473!
1474!--                      For parallel output, all cross sections are first
1475!--                      stored here on a local array and will be written to the
1476!--                      output file afterwards to increase the performance.
1477                         DO  i = nxl, nxr
1478                            DO  k = nzb_do, nzt_do
1479                               local_2d_sections_l(i,is,k) = local_2d(i,k)
1480                            ENDDO
1481                         ENDDO
1482#endif
1483                      ENDIF
1484
1485                   ELSE
1486
1487                      IF ( data_output_2d_on_each_pe )  THEN
1488!
1489!--                      Output of partial arrays on each PE. If the cross
1490!--                      section does not reside on the PE, output special
1491!--                      index values.
1492#if defined( __netcdf )
1493                         IF ( myid == 0 )  THEN
1494                            WRITE ( 22 )  time_since_reference_point,          &
1495                                          do2d_xz_time_count(av), av
1496                         ENDIF
1497#endif
1498                         DO  i = 0, io_blocks-1
1499                            IF ( i == io_group )  THEN
1500                               IF ( ( section(is,s_ind) >= nys  .AND.          &
1501                                      section(is,s_ind) <= nyn )  .OR.         &
1502                                    ( section(is,s_ind) == -1  .AND.           &
1503                                      nys-1 == -1 ) )                          &
1504                               THEN
1505                                  WRITE (22)  nxl, nxr, nzb_do, nzt_do, nzb, nzt+1
1506                                  WRITE (22)  local_2d
1507                               ELSE
1508                                  WRITE (22)  -1, -1, -1, -1, -1, -1
1509                               ENDIF
1510                            ENDIF
1511#if defined( __parallel )
1512                            CALL MPI_BARRIER( comm2d, ierr )
1513#endif
1514                         ENDDO
1515
1516                      ELSE
1517!
1518!--                      PE0 receives partial arrays from all processors of the
1519!--                      respective cross section and outputs them. Here a
1520!--                      barrier has to be set, because otherwise
1521!--                      "-MPI- FATAL: Remote protocol queue full" may occur.
1522                         CALL MPI_BARRIER( comm2d, ierr )
1523
1524                         ngp = ( nxr-nxl + 1 ) * ( nzt_do-nzb_do + 1 )
1525                         IF ( myid == 0 )  THEN
1526!
1527!--                         Local array can be relocated directly.
1528                            IF ( ( section(is,s_ind) >= nys  .AND.              &
1529                                   section(is,s_ind) <= nyn )  .OR.             &
1530                                 ( section(is,s_ind) == -1  .AND.               &
1531                                   nys-1 == -1 ) )  THEN
1532                               total_2d(nxl:nxr,nzb_do:nzt_do) = local_2d
1533                            ENDIF
1534!
1535!--                         Receive data from all other PEs.
1536                            DO  n = 1, numprocs-1
1537!
1538!--                            Receive index limits first, then array.
1539!--                            Index limits are received in arbitrary order from
1540!--                            the PEs.
1541                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,          &
1542                                              MPI_ANY_SOURCE, 0, comm2d,       &
1543                                              status, ierr )
1544!
1545!--                            Not all PEs have data for XZ-cross-section.
1546                               IF ( ind(1) /= -9999 )  THEN
1547                                  sender = status(MPI_SOURCE)
1548                                  DEALLOCATE( local_2d )
1549                                  ALLOCATE( local_2d(ind(1):ind(2),            &
1550                                                     ind(3):ind(4)) )
1551                                  CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp, &
1552                                                 MPI_REAL, sender, 1, comm2d,  &
1553                                                 status, ierr )
1554                                  total_2d(ind(1):ind(2),ind(3):ind(4)) =      &
1555                                                                        local_2d
1556                               ENDIF
1557                            ENDDO
1558!
1559!--                         Relocate the local array for the next loop increment
1560                            DEALLOCATE( local_2d )
1561                            ALLOCATE( local_2d(nxl:nxr,nzb_do:nzt_do) )
1562
1563#if defined( __netcdf )
1564                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
1565                                                 id_var_do2d(av,ivar),           &
1566                                                 total_2d(0:nx,nzb_do:nzt_do), &
1567                               start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
1568                                          count = (/ nx+1, 1, nzt_do-nzb_do+1, 1 /) )
1569                            CALL netcdf_handle_error( 'data_output_2d', 58 )
1570#endif
1571
1572                         ELSE
1573!
1574!--                         If the cross section resides on the PE, send the
1575!--                         local index limits, otherwise send -9999 to PE0.
1576                            IF ( ( section(is,s_ind) >= nys  .AND.              &
1577                                   section(is,s_ind) <= nyn )  .OR.             &
1578                                 ( section(is,s_ind) == -1  .AND.  nys-1 == -1 ) ) &
1579                            THEN
1580                               ind(1) = nxl; ind(2) = nxr
1581                               ind(3) = nzb_do;   ind(4) = nzt_do
1582                            ELSE
1583                               ind(1) = -9999; ind(2) = -9999
1584                               ind(3) = -9999; ind(4) = -9999
1585                            ENDIF
1586                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0,       &
1587                                           comm2d, ierr )
1588!
1589!--                         If applicable, send data to PE0.
1590                            IF ( ind(1) /= -9999 )  THEN
1591                               CALL MPI_SEND( local_2d(nxl,nzb_do), ngp,         &
1592                                              MPI_REAL, 0, 1, comm2d, ierr )
1593                            ENDIF
1594                         ENDIF
1595!
1596!--                      A barrier has to be set, because otherwise some PEs may
1597!--                      proceed too fast so that PE0 may receive wrong data on
1598!--                      tag 0
1599                         CALL MPI_BARRIER( comm2d, ierr )
1600                      ENDIF
1601
1602                   ENDIF
1603#else
1604#if defined( __netcdf )
1605                   nc_stat = NF90_PUT_VAR( id_set_xz(av),                   &
1606                                           id_var_do2d(av,ivar),              &
1607                                           local_2d(nxl:nxr,nzb_do:nzt_do), &
1608                            start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
1609                                       count = (/ nx+1, 1, nzt_do-nzb_do+1, 1 /) )
1610                   CALL netcdf_handle_error( 'data_output_2d', 451 )
1611#endif
1612#endif
1613
1614                CASE ( 'yz' )
1615!
1616!--                Update the netCDF yz cross section time axis.
1617!--                In case of parallel output, this is only done by PE0
1618!--                to increase the performance.
1619                   IF ( time_since_reference_point /= do2d_yz_last_time(av) )  THEN
1620                      do2d_yz_time_count(av) = do2d_yz_time_count(av) + 1
1621                      do2d_yz_last_time(av)  = time_since_reference_point
1622                      IF ( myid == 0 )  THEN
1623                         IF ( .NOT. data_output_2d_on_each_pe  &
1624                              .OR.  netcdf_data_format > 4 )   &
1625                         THEN
1626#if defined( __netcdf )
1627                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1628                                                    id_var_time_yz(av),        &
1629                                             (/ time_since_reference_point /), &
1630                                         start = (/ do2d_yz_time_count(av) /), &
1631                                                    count = (/ 1 /) )
1632                            CALL netcdf_handle_error( 'data_output_2d', 59 )
1633#endif
1634                         ENDIF
1635                      ENDIF
1636                   ENDIF
1637
1638!
1639!--                If required, carry out averaging along x
1640                   IF ( section(is,s_ind) == -1 )  THEN
1641
1642                      ALLOCATE( local_2d_l(nys:nyn,nzb_do:nzt_do) )
1643                      local_2d_l = 0.0_wp
1644                      ngp = ( nyn-nys+1 ) * ( nzt_do-nzb_do+1 )
1645!
1646!--                   First local averaging on the PE
1647                      DO  k = nzb_do, nzt_do
1648                         DO  j = nys, nyn
1649                            DO  i = nxl, nxr
1650                               local_2d_l(j,k) = local_2d_l(j,k) +             &
1651                                                 local_pf(i,j,k)
1652                            ENDDO
1653                         ENDDO
1654                      ENDDO
1655#if defined( __parallel )
1656!
1657!--                   Now do the averaging over all PEs along x
1658                      IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1659                      CALL MPI_ALLREDUCE( local_2d_l(nys,nzb_do),                &
1660                                          local_2d(nys,nzb_do), ngp, MPI_REAL,   &
1661                                          MPI_SUM, comm1dx, ierr )
1662#else
1663                      local_2d = local_2d_l
1664#endif
1665                      local_2d = local_2d / ( nx + 1.0_wp )
1666
1667                      DEALLOCATE( local_2d_l )
1668
1669                   ELSE
1670!
1671!--                   Just store the respective section on the local array
1672!--                   (but only if it is available on this PE!)
1673                      IF ( section(is,s_ind) >= nxl  .AND.  section(is,s_ind) <= nxr ) &
1674                      THEN
1675                         local_2d = local_pf(section(is,s_ind),:,nzb_do:nzt_do)
1676                      ENDIF
1677
1678                   ENDIF
1679
1680#if defined( __parallel )
1681                   IF ( netcdf_data_format > 4 )  THEN
1682!
1683!--                   Output in netCDF4/HDF5 format.
1684!--                   Output only on those PEs where the respective cross
1685!--                   sections reside. Cross sections averaged along x are
1686!--                   output on the respective first PE along x (myidx=0).
1687                      IF ( ( section(is,s_ind) >= nxl  .AND.                       &
1688                             section(is,s_ind) <= nxr )  .OR.                      &
1689                           ( section(is,s_ind) == -1  .AND.  myidx == 0 ) )  THEN
1690#if defined( __netcdf )
1691!
1692!--                      For parallel output, all cross sections are first
1693!--                      stored here on a local array and will be written to the
1694!--                      output file afterwards to increase the performance.
1695                         DO  j = nys, nyn
1696                            DO  k = nzb_do, nzt_do
1697                               local_2d_sections_l(is,j,k) = local_2d(j,k)
1698                            ENDDO
1699                         ENDDO
1700#endif
1701                      ENDIF
1702
1703                   ELSE
1704
1705                      IF ( data_output_2d_on_each_pe )  THEN
1706!
1707!--                      Output of partial arrays on each PE. If the cross
1708!--                      section does not reside on the PE, output special
1709!--                      index values.
1710#if defined( __netcdf )
1711                         IF ( myid == 0 )  THEN
1712                            WRITE ( 23 )  time_since_reference_point,          &
1713                                          do2d_yz_time_count(av), av
1714                         ENDIF
1715#endif
1716                         DO  i = 0, io_blocks-1
1717                            IF ( i == io_group )  THEN
1718                               IF ( ( section(is,s_ind) >= nxl  .AND.          &
1719                                      section(is,s_ind) <= nxr )  .OR.         &
1720                                    ( section(is,s_ind) == -1  .AND.           &
1721                                      nxl-1 == -1 ) )                          &
1722                               THEN
1723                                  WRITE (23)  nys, nyn, nzb_do, nzt_do, nzb, nzt+1
1724                                  WRITE (23)  local_2d
1725                               ELSE
1726                                  WRITE (23)  -1, -1, -1, -1, -1, -1
1727                               ENDIF
1728                            ENDIF
1729#if defined( __parallel )
1730                            CALL MPI_BARRIER( comm2d, ierr )
1731#endif
1732                         ENDDO
1733
1734                      ELSE
1735!
1736!--                      PE0 receives partial arrays from all processors of the
1737!--                      respective cross section and outputs them. Here a
1738!--                      barrier has to be set, because otherwise
1739!--                      "-MPI- FATAL: Remote protocol queue full" may occur.
1740                         CALL MPI_BARRIER( comm2d, ierr )
1741
1742                         ngp = ( nyn-nys+1 ) * ( nzt_do-nzb_do+1 )
1743                         IF ( myid == 0 )  THEN
1744!
1745!--                         Local array can be relocated directly.
1746                            IF ( ( section(is,s_ind) >= nxl  .AND.             &
1747                                   section(is,s_ind) <= nxr )   .OR.           &
1748                                 ( section(is,s_ind) == -1  .AND.  nxl-1 == -1 ) ) &
1749                            THEN
1750                               total_2d(nys:nyn,nzb_do:nzt_do) = local_2d
1751                            ENDIF
1752!
1753!--                         Receive data from all other PEs.
1754                            DO  n = 1, numprocs-1
1755!
1756!--                            Receive index limits first, then array.
1757!--                            Index limits are received in arbitrary order from
1758!--                            the PEs.
1759                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,          &
1760                                              MPI_ANY_SOURCE, 0, comm2d,       &
1761                                              status, ierr )
1762!
1763!--                            Not all PEs have data for YZ-cross-section.
1764                               IF ( ind(1) /= -9999 )  THEN
1765                                  sender = status(MPI_SOURCE)
1766                                  DEALLOCATE( local_2d )
1767                                  ALLOCATE( local_2d(ind(1):ind(2),            &
1768                                                     ind(3):ind(4)) )
1769                                  CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp, &
1770                                                 MPI_REAL, sender, 1, comm2d,  &
1771                                                 status, ierr )
1772                                  total_2d(ind(1):ind(2),ind(3):ind(4)) =      &
1773                                                                        local_2d
1774                               ENDIF
1775                            ENDDO
1776!
1777!--                         Relocate the local array for the next loop increment
1778                            DEALLOCATE( local_2d )
1779                            ALLOCATE( local_2d(nys:nyn,nzb_do:nzt_do) )
1780
1781#if defined( __netcdf )
1782                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1783                                                 id_var_do2d(av,ivar),           &
1784                                                 total_2d(0:ny,nzb_do:nzt_do), &
1785                            start = (/ is, 1, 1, do2d_yz_time_count(av) /),    &
1786                                       count = (/ 1, ny+1, nzt_do-nzb_do+1, 1 /) )
1787                            CALL netcdf_handle_error( 'data_output_2d', 61 )
1788#endif
1789
1790                         ELSE
1791!
1792!--                         If the cross section resides on the PE, send the
1793!--                         local index limits, otherwise send -9999 to PE0.
1794                            IF ( ( section(is,s_ind) >= nxl  .AND.              &
1795                                   section(is,s_ind) <= nxr )  .OR.             &
1796                                 ( section(is,s_ind) == -1  .AND.  nxl-1 == -1 ) ) &
1797                            THEN
1798                               ind(1) = nys; ind(2) = nyn
1799                               ind(3) = nzb_do;   ind(4) = nzt_do
1800                            ELSE
1801                               ind(1) = -9999; ind(2) = -9999
1802                               ind(3) = -9999; ind(4) = -9999
1803                            ENDIF
1804                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0,       &
1805                                           comm2d, ierr )
1806!
1807!--                         If applicable, send data to PE0.
1808                            IF ( ind(1) /= -9999 )  THEN
1809                               CALL MPI_SEND( local_2d(nys,nzb_do), ngp,         &
1810                                              MPI_REAL, 0, 1, comm2d, ierr )
1811                            ENDIF
1812                         ENDIF
1813!
1814!--                      A barrier has to be set, because otherwise some PEs may
1815!--                      proceed too fast so that PE0 may receive wrong data on
1816!--                      tag 0
1817                         CALL MPI_BARRIER( comm2d, ierr )
1818                      ENDIF
1819
1820                   ENDIF
1821#else
1822#if defined( __netcdf )
1823                   nc_stat = NF90_PUT_VAR( id_set_yz(av),                   &
1824                                           id_var_do2d(av,ivar),              &
1825                                           local_2d(nys:nyn,nzb_do:nzt_do), &
1826                            start = (/ is, 1, 1, do2d_xz_time_count(av) /), &
1827                                           count = (/ 1, ny+1, nzt_do-nzb_do+1, 1 /) )
1828                   CALL netcdf_handle_error( 'data_output_2d', 452 )
1829#endif
1830#endif
1831
1832             END SELECT
1833
1834             is = is + 1
1835          ENDDO loop1
1836
1837!
1838!--       For parallel output, all data were collected before on a local array
1839!--       and are written now to the netcdf file. This must be done to increase
1840!--       the performance of the parallel output.
1841#if defined( __netcdf )
1842          IF ( netcdf_data_format > 4 )  THEN
1843
1844                SELECT CASE ( mode )
1845
1846                   CASE ( 'xy' )
1847                      IF ( two_d ) THEN
1848                         nis = 1
1849                         two_d = .FALSE.
1850                      ELSE
1851                         nis = ns
1852                      ENDIF
1853!
1854!--                   Do not output redundant ghost point data except for the
1855!--                   boundaries of the total domain.
1856!                      IF ( nxr == nx  .AND.  nyn /= ny )  THEN
1857!                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1858!                                                 id_var_do2d(av,ivar),           &
1859!                                                 local_2d_sections(nxl:nxr+1,  &
1860!                                                    nys:nyn,1:nis),            &
1861!                                                 start = (/ nxl+1, nys+1, 1,   &
1862!                                                    do2d_xy_time_count(av) /), &
1863!                                                 count = (/ nxr-nxl+2,         &
1864!                                                            nyn-nys+1, nis, 1  &
1865!                                                          /) )
1866!                      ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
1867!                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1868!                                                 id_var_do2d(av,ivar),           &
1869!                                                 local_2d_sections(nxl:nxr,    &
1870!                                                    nys:nyn+1,1:nis),          &
1871!                                                 start = (/ nxl+1, nys+1, 1,   &
1872!                                                    do2d_xy_time_count(av) /), &
1873!                                                 count = (/ nxr-nxl+1,         &
1874!                                                            nyn-nys+2, nis, 1  &
1875!                                                          /) )
1876!                      ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
1877!                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1878!                                                 id_var_do2d(av,ivar),           &
1879!                                                 local_2d_sections(nxl:nxr+1,  &
1880!                                                    nys:nyn+1,1:nis),          &
1881!                                                 start = (/ nxl+1, nys+1, 1,   &
1882!                                                    do2d_xy_time_count(av) /), &
1883!                                                 count = (/ nxr-nxl+2,         &
1884!                                                            nyn-nys+2, nis, 1  &
1885!                                                          /) )
1886!                      ELSE
1887                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
1888                                                 id_var_do2d(av,ivar),           &
1889                                                 local_2d_sections(nxl:nxr,    &
1890                                                    nys:nyn,1:nis),            &
1891                                                 start = (/ nxl+1, nys+1, 1,   &
1892                                                    do2d_xy_time_count(av) /), &
1893                                                 count = (/ nxr-nxl+1,         &
1894                                                            nyn-nys+1, nis, 1  &
1895                                                          /) )
1896!                      ENDIF   
1897
1898                      CALL netcdf_handle_error( 'data_output_2d', 55 )
1899
1900                   CASE ( 'xz' )
1901!
1902!--                   First, all PEs get the information of all cross-sections.
1903!--                   Then the data are written to the output file by all PEs
1904!--                   while NF90_COLLECTIVE is set in subroutine
1905!--                   define_netcdf_header. Although redundant information are
1906!--                   written to the output file in that case, the performance
1907!--                   is significantly better compared to the case where only
1908!--                   the first row of PEs in x-direction (myidx = 0) is given
1909!--                   the output while NF90_INDEPENDENT is set.
1910                      IF ( npey /= 1 )  THEN
1911                         
1912#if defined( __parallel )
1913!
1914!--                      Distribute data over all PEs along y
1915                         ngp = ( nxr-nxl+1 ) * ( nzt_do-nzb_do+1 ) * ns
1916                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
1917                         CALL MPI_ALLREDUCE( local_2d_sections_l(nxl,1,nzb_do),  &
1918                                             local_2d_sections(nxl,1,nzb_do),    &
1919                                             ngp, MPI_REAL, MPI_SUM, comm1dy,  &
1920                                             ierr )
1921#else
1922                         local_2d_sections = local_2d_sections_l
1923#endif
1924                      ENDIF
1925!
1926!--                   Do not output redundant ghost point data except for the
1927!--                   boundaries of the total domain.
1928!                      IF ( nxr == nx )  THEN
1929!                         nc_stat = NF90_PUT_VAR( id_set_xz(av),                &
1930!                                             id_var_do2d(av,ivar),               &
1931!                                             local_2d_sections(nxl:nxr+1,1:ns, &
1932!                                                nzb_do:nzt_do),                &
1933!                                             start = (/ nxl+1, 1, 1,           &
1934!                                                do2d_xz_time_count(av) /),     &
1935!                                             count = (/ nxr-nxl+2, ns, nzt_do-nzb_do+1,  &
1936!                                                        1 /) )
1937!                      ELSE
1938                         nc_stat = NF90_PUT_VAR( id_set_xz(av),                &
1939                                             id_var_do2d(av,ivar),               &
1940                                             local_2d_sections(nxl:nxr,1:ns,   &
1941                                                nzb_do:nzt_do),                &
1942                                             start = (/ nxl+1, 1, 1,           &
1943                                                do2d_xz_time_count(av) /),     &
1944                                             count = (/ nxr-nxl+1, ns, nzt_do-nzb_do+1,  &
1945                                                1 /) )
1946!                      ENDIF
1947
1948                      CALL netcdf_handle_error( 'data_output_2d', 57 )
1949
1950                   CASE ( 'yz' )
1951!
1952!--                   First, all PEs get the information of all cross-sections.
1953!--                   Then the data are written to the output file by all PEs
1954!--                   while NF90_COLLECTIVE is set in subroutine
1955!--                   define_netcdf_header. Although redundant information are
1956!--                   written to the output file in that case, the performance
1957!--                   is significantly better compared to the case where only
1958!--                   the first row of PEs in y-direction (myidy = 0) is given
1959!--                   the output while NF90_INDEPENDENT is set.
1960                      IF ( npex /= 1 )  THEN
1961
1962#if defined( __parallel )
1963!
1964!--                      Distribute data over all PEs along x
1965                         ngp = ( nyn-nys+1 ) * ( nzt-nzb + 2 ) * ns
1966                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
1967                         CALL MPI_ALLREDUCE( local_2d_sections_l(1,nys,nzb_do),  &
1968                                             local_2d_sections(1,nys,nzb_do),    &
1969                                             ngp, MPI_REAL, MPI_SUM, comm1dx,  &
1970                                             ierr )
1971#else
1972                         local_2d_sections = local_2d_sections_l
1973#endif
1974                      ENDIF
1975!
1976!--                   Do not output redundant ghost point data except for the
1977!--                   boundaries of the total domain.
1978!                      IF ( nyn == ny )  THEN
1979!                         nc_stat = NF90_PUT_VAR( id_set_yz(av),                &
1980!                                             id_var_do2d(av,ivar),               &
1981!                                             local_2d_sections(1:ns,           &
1982!                                                nys:nyn+1,nzb_do:nzt_do),      &
1983!                                             start = (/ 1, nys+1, 1,           &
1984!                                                do2d_yz_time_count(av) /),     &
1985!                                             count = (/ ns, nyn-nys+2,         &
1986!                                                        nzt_do-nzb_do+1, 1 /) )
1987!                      ELSE
1988                         nc_stat = NF90_PUT_VAR( id_set_yz(av),                &
1989                                             id_var_do2d(av,ivar),               &
1990                                             local_2d_sections(1:ns,nys:nyn,   &
1991                                                nzb_do:nzt_do),                &
1992                                             start = (/ 1, nys+1, 1,           &
1993                                                do2d_yz_time_count(av) /),     &
1994                                             count = (/ ns, nyn-nys+1,         &
1995                                                        nzt_do-nzb_do+1, 1 /) )
1996!                      ENDIF
1997
1998                      CALL netcdf_handle_error( 'data_output_2d', 60 )
1999
2000                   CASE DEFAULT
2001                      message_string = 'unknown cross-section: ' // TRIM( mode )
2002                      CALL message( 'data_output_2d', 'PA0180', 1, 2, 0, 6, 0 )
2003
2004                END SELECT                     
2005
2006          ENDIF
2007#endif
2008       ENDIF
2009
2010       ivar = ivar + 1
2011       l = MAX( 2, LEN_TRIM( do2d(av,ivar) ) )
2012       do2d_mode = do2d(av,ivar)(l-1:l)
2013
2014    ENDDO
2015
2016!
2017!-- Deallocate temporary arrays.
2018    IF ( ALLOCATED( level_z ) )  DEALLOCATE( level_z )
2019    IF ( netcdf_data_format > 4 )  THEN
2020       DEALLOCATE( local_pf, local_2d, local_2d_sections )
2021       IF( mode == 'xz' .OR. mode == 'yz' ) DEALLOCATE( local_2d_sections_l )
2022    ENDIF
2023#if defined( __parallel )
2024    IF ( .NOT.  data_output_2d_on_each_pe  .AND.  myid == 0 )  THEN
2025       DEALLOCATE( total_2d )
2026    ENDIF
2027#endif
2028
2029!
2030!-- Close plot output file.
2031    file_id = 20 + s_ind
2032
2033    IF ( data_output_2d_on_each_pe )  THEN
2034       DO  i = 0, io_blocks-1
2035          IF ( i == io_group )  THEN
2036             CALL close_file( file_id )
2037          ENDIF
2038#if defined( __parallel )
2039          CALL MPI_BARRIER( comm2d, ierr )
2040#endif
2041       ENDDO
2042    ELSE
2043       IF ( myid == 0 )  CALL close_file( file_id )
2044    ENDIF
2045
2046    CALL cpu_log( log_point(3), 'data_output_2d', 'stop' )
2047
2048    IF ( debug_output_timestep )  CALL debug_message( 'data_output_2d', 'end' )
2049
2050
2051 END SUBROUTINE data_output_2d
Note: See TracBrowser for help on using the repository browser.