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

Last change on this file since 691 was 691, checked in by maronga, 13 years ago

Bugfix for precursor atmosphere/ocean runs, re-adjustments for lcxt4

  • Property svn:keywords set to Id
File size: 63.8 KB
Line 
1 SUBROUTINE data_output_2d( mode, av )
2
3!------------------------------------------------------------------------------!
4! Current revisions:
5! -----------------
6! Replaced simulated_time by time_since_reference_point
7!
8! Former revisions:
9! -----------------
10! $Id: data_output_2d.f90 691 2011-03-04 08:45:30Z maronga $
11!
12! 673 2011-01-18 16:19:48Z suehring
13! When using Multigrid or SOR solver an additional CALL exchange_horiz is
14! is needed for pressure output.
15!
16! 667 2010-12-23 12:06:00Z suehring/gryschka
17! nxl-1, nxr+1, nys-1, nyn+1 replaced by nxlg, nxrg, nysg, nyng in loops and
18! allocation of arrays local_2d and total_2d.
19! Calls of exchange_horiz are modiefied.
20!
21! 622 2010-12-10 08:08:13Z raasch
22! optional barriers included in order to speed up collective operations
23!
24! 493 2010-03-01 08:30:24Z raasch
25! NetCDF4 support (parallel output)
26!
27! 367 2009-08-25 08:35:52Z maronga
28! simulated_time in NetCDF output replaced by time_since_reference_point.
29! Output of NetCDF messages with aid of message handling routine.
30! Bugfix: averaging along z is not allowed for 2d quantities (e.g. u* and z0)
31! Output of messages replaced by message handling routine.
32! Output of user defined 2D (XY) arrays at z=nzb+1 is now possible
33! Bugfix: to_be_resorted => s_av for time-averaged scalars
34! Calculation of shf* and qsws* added.
35!
36! 215 2008-11-18 09:54:31Z raasch
37! Bugfix: no output of particle concentration and radius unless particles
38! have been started
39!
40! 96 2007-06-04 08:07:41Z raasch
41! Output of density and salinity
42!
43! 75 2007-03-22 09:54:05Z raasch
44! Output of precipitation amount/rate and roughness length,
45! 2nd+3rd argument removed from exchange horiz
46!
47! RCS Log replace by Id keyword, revision history cleaned up
48!
49! Revision 1.5  2006/08/22 13:50:29  raasch
50! xz and yz cross sections now up to nzt+1
51!
52! Revision 1.2  2006/02/23 10:19:22  raasch
53! Output of time-averaged data, output of averages along x, y, or z,
54! output of user-defined quantities,
55! section data are copied from local_pf to local_2d before they are output,
56! output of particle concentration and mean radius,
57! Former subroutine plot_2d renamed data_output_2d, pl2d.. renamed do2d..,
58! anz renamed ngp, ebene renamed section, pl2d_.._anz renamed do2d_.._n
59!
60! Revision 1.1  1997/08/11 06:24:09  raasch
61! Initial revision
62!
63!
64! Description:
65! ------------
66! Data output of horizontal cross-sections in NetCDF format or binary format
67! compatible to old graphic software iso2d.
68! Attention: The position of the sectional planes is still not always computed
69! ---------  correctly. (zu is used always)!
70!------------------------------------------------------------------------------!
71
72    USE arrays_3d
73    USE averaging
74    USE cloud_parameters
75    USE control_parameters
76    USE cpulog
77    USE grid_variables
78    USE indices
79    USE interfaces
80    USE netcdf_control
81    USE particle_attributes
82    USE pegrid
83
84    IMPLICIT NONE
85
86    CHARACTER (LEN=2)  ::  do2d_mode, mode
87    CHARACTER (LEN=4)  ::  grid
88    CHARACTER (LEN=25) ::  section_chr
89    CHARACTER (LEN=50) ::  rtext
90    INTEGER ::  av, ngp, file_id, i, if, is, iis, j, k, l, layer_xy, n, psi, &
91                s, sender, &
92                ind(4)
93    LOGICAL ::  found, resorted, two_d
94    REAL    ::  mean_r, s_r3, s_r4
95    REAL, DIMENSION(:), ALLOCATABLE ::      level_z
96    REAL, DIMENSION(:,:), ALLOCATABLE ::    local_2d, local_2d_l
97    REAL, DIMENSION(:,:,:), ALLOCATABLE ::  local_pf
98#if defined( __parallel )
99    REAL, DIMENSION(:,:),   ALLOCATABLE ::  total_2d
100#endif
101    REAL, DIMENSION(:,:,:), POINTER ::  to_be_resorted
102
103    NAMELIST /LOCAL/  rtext
104
105    CALL cpu_log (log_point(3),'data_output_2d','start')
106
107!
108!-- Immediate return, if no output is requested (no respective sections
109!-- found in parameter data_output)
110    IF ( mode == 'xy'  .AND.  .NOT. data_output_xy(av) )  RETURN
111    IF ( mode == 'xz'  .AND.  .NOT. data_output_xz(av) )  RETURN
112    IF ( mode == 'yz'  .AND.  .NOT. data_output_yz(av) )  RETURN
113
114    two_d = .FALSE.    ! local variable to distinguish between output of pure 2D
115                       ! arrays and cross-sections of 3D arrays.
116
117!
118!-- Depending on the orientation of the cross-section, the respective output
119!-- files have to be opened.
120    SELECT CASE ( mode )
121
122       CASE ( 'xy' )
123          s = 1
124          ALLOCATE( level_z(nzb:nzt+1), local_2d(nxlg:nxrg,nysg:nyng) )
125
126!
127!--       Classic and 64bit offset NetCDF output is done only on PE0.
128!--       netCDF4/HDF5 output is done in parallel on all PEs.
129          IF ( netcdf_output .AND. ( myid == 0 .OR. netcdf_data_format > 2 ) ) &
130          THEN
131             CALL check_open( 101+av*10 )
132          ENDIF
133
134          IF ( data_output_2d_on_each_pe )  THEN
135             CALL check_open( 21 )
136          ELSE
137             IF ( myid == 0 )  THEN
138                IF ( iso2d_output )  CALL check_open( 21 )
139#if defined( __parallel )
140                ALLOCATE( total_2d(-nbgp:nx+nbgp,-nbgp:ny+nbgp) )
141#endif
142             ENDIF
143          ENDIF
144
145       CASE ( 'xz' )
146          s = 2
147          ALLOCATE( local_2d(nxlg:nxrg,nzb:nzt+1) )
148
149!
150!--       Classic and 64bit offset NetCDF output is done only on PE0.
151!--       netCDF4/HDF5 output may be done in parallel on all PEs.
152          IF ( netcdf_output .AND. ( myid == 0 .OR. netcdf_data_format > 2 ) ) &
153          THEN
154             CALL check_open( 102+av*10 )
155          ENDIF
156
157          IF ( data_output_2d_on_each_pe )  THEN
158             CALL check_open( 22 )
159          ELSE
160             IF ( myid == 0 )  THEN
161                IF ( iso2d_output )  CALL check_open( 22 )
162#if defined( __parallel )
163                ALLOCATE( total_2d(-nbgp:nx+nbgp,nzb:nzt+1) )
164#endif
165             ENDIF
166          ENDIF
167
168       CASE ( 'yz' )
169
170          s = 3
171          ALLOCATE( local_2d(nysg:nyng,nzb:nzt+1) )
172
173!
174!--       Classic and 64bit offset NetCDF output is done only on PE0.
175!--       netCDF4/HDF5 output may be done in parallel on all PEs.
176          IF ( netcdf_output .AND. ( myid == 0 .OR. netcdf_data_format > 2 ) ) &
177          THEN
178             CALL check_open( 103+av*10 )
179          ENDIF
180
181          IF ( data_output_2d_on_each_pe )  THEN
182             CALL check_open( 23 )
183          ELSE
184             IF ( myid == 0 )  THEN
185                IF ( iso2d_output )  CALL check_open( 23 )
186#if defined( __parallel )
187                ALLOCATE( total_2d(-nbgp:ny+nbgp,nzb:nzt+1) )
188#endif
189             ENDIF
190          ENDIF
191
192       CASE DEFAULT
193
194          message_string = 'unknown cross-section: ' // TRIM( mode )
195          CALL message( 'data_output_2d', 'PA0180', 1, 2, 0, 6, 0 )
196
197    END SELECT
198
199!
200!-- Allocate a temporary array for resorting (kji -> ijk).
201    ALLOCATE( local_pf(nxlg:nxrg,nysg:nyng,nzb:nzt+1) )
202
203!
204!-- Loop of all variables to be written.
205!-- Output dimensions chosen
206    if = 1
207    l = MAX( 2, LEN_TRIM( do2d(av,if) ) )
208    do2d_mode = do2d(av,if)(l-1:l)
209
210    DO  WHILE ( do2d(av,if)(1:1) /= ' ' )
211
212       IF ( do2d_mode == mode )  THEN
213!
214!--       Store the array chosen on the temporary array.
215          resorted = .FALSE.
216          SELECT CASE ( TRIM( do2d(av,if) ) )
217
218             CASE ( 'e_xy', 'e_xz', 'e_yz' )
219                IF ( av == 0 )  THEN
220                   to_be_resorted => e
221                ELSE
222                   to_be_resorted => e_av
223                ENDIF
224                IF ( mode == 'xy' )  level_z = zu
225
226             CASE ( 'lwp*_xy' )        ! 2d-array
227                IF ( av == 0 )  THEN
228                   DO  i = nxlg, nxrg
229                      DO  j = nysg, nyng
230                         local_pf(i,j,nzb+1) = SUM( ql(nzb:nzt,j,i) * &
231                                                    dzw(1:nzt+1) )
232                      ENDDO
233                   ENDDO
234                ELSE
235                   DO  i = nxlg, nxrg
236                      DO  j = nysg, nyng
237                         local_pf(i,j,nzb+1) = lwp_av(j,i)
238                      ENDDO
239                   ENDDO
240                ENDIF
241                resorted = .TRUE.
242                two_d = .TRUE.
243                level_z(nzb+1) = zu(nzb+1)
244
245             CASE ( 'p_xy', 'p_xz', 'p_yz' )
246                IF ( psolver == 'multigrid' .OR. psolver == 'sor' )           &
247                   CALL exchange_horiz( p, nbgp )
248                IF ( av == 0 )  THEN
249                   to_be_resorted => p
250                ELSE
251                   to_be_resorted => p_av
252                ENDIF
253                IF ( mode == 'xy' )  level_z = zu
254
255             CASE ( 'pc_xy', 'pc_xz', 'pc_yz' )  ! particle concentration
256                IF ( av == 0 )  THEN
257                   IF ( simulated_time >= particle_advection_start )  THEN
258                      tend = prt_count
259                      CALL exchange_horiz( tend, nbgp )
260                   ELSE
261                      tend = 0.0
262                   ENDIF
263                   DO  i = nxlg, nxrg
264                      DO  j = nysg, nyng
265                         DO  k = nzb, nzt+1
266                            local_pf(i,j,k) = tend(k,j,i)
267                         ENDDO
268                      ENDDO
269                   ENDDO
270                   resorted = .TRUE.
271                ELSE
272                   CALL exchange_horiz( pc_av, nbgp )
273                   to_be_resorted => pc_av
274                ENDIF
275
276             CASE ( 'pr_xy', 'pr_xz', 'pr_yz' )  ! mean particle radius
277                IF ( av == 0 )  THEN
278                   IF ( simulated_time >= particle_advection_start )  THEN
279                      DO  i = nxl, nxr
280                         DO  j = nys, nyn
281                            DO  k = nzb, nzt+1
282                               psi = prt_start_index(k,j,i)
283                               s_r3 = 0.0
284                               s_r4 = 0.0
285                               DO  n = psi, psi+prt_count(k,j,i)-1
286                                  s_r3 = s_r3 + particles(n)%radius**3
287                                  s_r4 = s_r4 + particles(n)%radius**4
288                               ENDDO
289                               IF ( s_r3 /= 0.0 )  THEN
290                                  mean_r = s_r4 / s_r3
291                               ELSE
292                                  mean_r = 0.0
293                               ENDIF
294                               tend(k,j,i) = mean_r
295                            ENDDO
296                         ENDDO
297                      ENDDO
298                      CALL exchange_horiz( tend, nbgp )
299                   ELSE
300                      tend = 0.0
301                   END IF
302                   DO  i = nxlg, nxrg
303                      DO  j = nysg, nyng
304                         DO  k = nzb, nzt+1
305                            local_pf(i,j,k) = tend(k,j,i)
306                         ENDDO
307                      ENDDO
308                   ENDDO
309                   resorted = .TRUE.
310                ELSE
311                   CALL exchange_horiz( pr_av, nbgp )
312                   to_be_resorted => pr_av
313                ENDIF
314
315             CASE ( 'pra*_xy' )        ! 2d-array / integral quantity => no av
316                CALL exchange_horiz_2d( precipitation_amount )
317                   DO  i = nxlg, nxrg
318                      DO  j = nysg, nyng
319                      local_pf(i,j,nzb+1) =  precipitation_amount(j,i)
320                   ENDDO
321                ENDDO
322                precipitation_amount = 0.0   ! reset for next integ. interval
323                resorted = .TRUE.
324                two_d = .TRUE.
325                level_z(nzb+1) = zu(nzb+1)
326
327             CASE ( 'prr*_xy' )        ! 2d-array
328                IF ( av == 0 )  THEN
329                   CALL exchange_horiz_2d( precipitation_rate )
330                   DO  i = nxlg, nxrg
331                      DO  j = nysg, nyng
332                         local_pf(i,j,nzb+1) =  precipitation_rate(j,i)
333                      ENDDO
334                   ENDDO
335                ELSE
336                   CALL exchange_horiz_2d( precipitation_rate_av )
337                   DO  i = nxlg, nxrg
338                      DO  j = nysg, nyng
339                         local_pf(i,j,nzb+1) =  precipitation_rate_av(j,i)
340                      ENDDO
341                   ENDDO
342                ENDIF
343                resorted = .TRUE.
344                two_d = .TRUE.
345                level_z(nzb+1) = zu(nzb+1)
346
347             CASE ( 'pt_xy', 'pt_xz', 'pt_yz' )
348                IF ( av == 0 )  THEN
349                   IF ( .NOT. cloud_physics ) THEN
350                      to_be_resorted => pt
351                   ELSE
352                   DO  i = nxlg, nxrg
353                      DO  j = nysg, nyng
354                            DO  k = nzb, nzt+1
355                               local_pf(i,j,k) = pt(k,j,i) + l_d_cp *    &
356                                                             pt_d_t(k) * &
357                                                             ql(k,j,i)
358                            ENDDO
359                         ENDDO
360                      ENDDO
361                      resorted = .TRUE.
362                   ENDIF
363                ELSE
364                   to_be_resorted => pt_av
365                ENDIF
366                IF ( mode == 'xy' )  level_z = zu
367
368             CASE ( 'q_xy', 'q_xz', 'q_yz' )
369                IF ( av == 0 )  THEN
370                   to_be_resorted => q
371                ELSE
372                   to_be_resorted => q_av
373                ENDIF
374                IF ( mode == 'xy' )  level_z = zu
375
376             CASE ( 'ql_xy', 'ql_xz', 'ql_yz' )
377                IF ( av == 0 )  THEN
378                   to_be_resorted => ql
379                ELSE
380                   to_be_resorted => ql_av
381                ENDIF
382                IF ( mode == 'xy' )  level_z = zu
383
384             CASE ( 'ql_c_xy', 'ql_c_xz', 'ql_c_yz' )
385                IF ( av == 0 )  THEN
386                   to_be_resorted => ql_c
387                ELSE
388                   to_be_resorted => ql_c_av
389                ENDIF
390                IF ( mode == 'xy' )  level_z = zu
391
392             CASE ( 'ql_v_xy', 'ql_v_xz', 'ql_v_yz' )
393                IF ( av == 0 )  THEN
394                   to_be_resorted => ql_v
395                ELSE
396                   to_be_resorted => ql_v_av
397                ENDIF
398                IF ( mode == 'xy' )  level_z = zu
399
400             CASE ( 'ql_vp_xy', 'ql_vp_xz', 'ql_vp_yz' )
401                IF ( av == 0 )  THEN
402                   to_be_resorted => ql_vp
403                ELSE
404                   to_be_resorted => ql_vp_av
405                ENDIF
406                IF ( mode == 'xy' )  level_z = zu
407
408             CASE ( 'qsws*_xy' )        ! 2d-array
409                IF ( av == 0 ) THEN
410                   DO  i = nxlg, nxrg
411                      DO  j = nysg, nyng
412                         local_pf(i,j,nzb+1) =  qsws(j,i)
413                      ENDDO
414                   ENDDO
415                ELSE
416                   DO  i = nxlg, nxrg
417                      DO  j = nysg, nyng 
418                         local_pf(i,j,nzb+1) =  qsws_av(j,i)
419                      ENDDO
420                   ENDDO
421                ENDIF
422                resorted = .TRUE.
423                two_d = .TRUE.
424                level_z(nzb+1) = zu(nzb+1)
425
426             CASE ( 'qv_xy', 'qv_xz', 'qv_yz' )
427                IF ( av == 0 )  THEN
428                   DO  i = nxlg, nxrg
429                      DO  j = nysg, nyng
430                         DO  k = nzb, nzt+1
431                            local_pf(i,j,k) = q(k,j,i) - ql(k,j,i)
432                         ENDDO
433                      ENDDO
434                   ENDDO
435                   resorted = .TRUE.
436                ELSE
437                   to_be_resorted => qv_av
438                ENDIF
439                IF ( mode == 'xy' )  level_z = zu
440
441             CASE ( 'rho_xy', 'rho_xz', 'rho_yz' )
442                IF ( av == 0 )  THEN
443                   to_be_resorted => rho
444                ELSE
445                   to_be_resorted => rho_av
446                ENDIF
447
448             CASE ( 's_xy', 's_xz', 's_yz' )
449                IF ( av == 0 )  THEN
450                   to_be_resorted => q
451                ELSE
452                   to_be_resorted => s_av
453                ENDIF
454
455             CASE ( 'sa_xy', 'sa_xz', 'sa_yz' )
456                IF ( av == 0 )  THEN
457                   to_be_resorted => sa
458                ELSE
459                   to_be_resorted => sa_av
460                ENDIF
461
462             CASE ( 'shf*_xy' )        ! 2d-array
463                IF ( av == 0 ) THEN
464                   DO  i = nxlg, nxrg
465                      DO  j = nysg, nyng
466                         local_pf(i,j,nzb+1) =  shf(j,i)
467                      ENDDO
468                   ENDDO
469                ELSE
470                   DO  i = nxlg, nxrg
471                      DO  j = nysg, nyng
472                         local_pf(i,j,nzb+1) =  shf_av(j,i)
473                      ENDDO
474                   ENDDO
475                ENDIF
476                resorted = .TRUE.
477                two_d = .TRUE.
478                level_z(nzb+1) = zu(nzb+1)
479
480             CASE ( 't*_xy' )        ! 2d-array
481                IF ( av == 0 )  THEN
482                   DO  i = nxlg, nxrg
483                      DO  j = nysg, nyng
484                         local_pf(i,j,nzb+1) = ts(j,i)
485                      ENDDO
486                   ENDDO
487                ELSE
488                   DO  i = nxlg, nxrg
489                      DO  j = nysg, nyng
490                         local_pf(i,j,nzb+1) = ts_av(j,i)
491                      ENDDO
492                   ENDDO
493                ENDIF
494                resorted = .TRUE.
495                two_d = .TRUE.
496                level_z(nzb+1) = zu(nzb+1)
497
498             CASE ( 'u_xy', 'u_xz', 'u_yz' )
499                IF ( av == 0 )  THEN
500                   to_be_resorted => u
501                ELSE
502                   to_be_resorted => u_av
503                ENDIF
504                IF ( mode == 'xy' )  level_z = zu
505!
506!--             Substitute the values generated by "mirror" boundary condition
507!--             at the bottom boundary by the real surface values.
508                IF ( do2d(av,if) == 'u_xz'  .OR.  do2d(av,if) == 'u_yz' )  THEN
509                   IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0
510                ENDIF
511
512             CASE ( 'u*_xy' )        ! 2d-array
513                IF ( av == 0 )  THEN
514                   DO  i = nxlg, nxrg
515                      DO  j = nysg, nyng
516                         local_pf(i,j,nzb+1) = us(j,i)
517                      ENDDO
518                   ENDDO
519                ELSE
520                   DO  i = nxlg, nxrg
521                      DO  j = nysg, nyng
522                         local_pf(i,j,nzb+1) = us_av(j,i)
523                      ENDDO
524                   ENDDO
525                ENDIF
526                resorted = .TRUE.
527                two_d = .TRUE.
528                level_z(nzb+1) = zu(nzb+1)
529
530             CASE ( 'v_xy', 'v_xz', 'v_yz' )
531                IF ( av == 0 )  THEN
532                   to_be_resorted => v
533                ELSE
534                   to_be_resorted => v_av
535                ENDIF
536                IF ( mode == 'xy' )  level_z = zu
537!
538!--             Substitute the values generated by "mirror" boundary condition
539!--             at the bottom boundary by the real surface values.
540                IF ( do2d(av,if) == 'v_xz'  .OR.  do2d(av,if) == 'v_yz' )  THEN
541                   IF ( ibc_uv_b == 0 )  local_pf(:,:,nzb) = 0.0
542                ENDIF
543
544             CASE ( 'vpt_xy', 'vpt_xz', 'vpt_yz' )
545                IF ( av == 0 )  THEN
546                   to_be_resorted => vpt
547                ELSE
548                   to_be_resorted => vpt_av
549                ENDIF
550                IF ( mode == 'xy' )  level_z = zu
551
552             CASE ( 'w_xy', 'w_xz', 'w_yz' )
553                IF ( av == 0 )  THEN
554                   to_be_resorted => w
555                ELSE
556                   to_be_resorted => w_av
557                ENDIF
558                IF ( mode == 'xy' )  level_z = zw
559
560             CASE ( 'z0*_xy' )        ! 2d-array
561                IF ( av == 0 ) THEN
562                   DO  i = nxlg, nxrg
563                      DO  j = nysg, nyng
564                         local_pf(i,j,nzb+1) =  z0(j,i)
565                      ENDDO
566                   ENDDO
567                ELSE
568                   DO  i = nxlg, nxrg
569                      DO  j = nysg, nyng
570                         local_pf(i,j,nzb+1) =  z0_av(j,i)
571                      ENDDO
572                   ENDDO
573                ENDIF
574                resorted = .TRUE.
575                two_d = .TRUE.
576                level_z(nzb+1) = zu(nzb+1)
577
578             CASE DEFAULT
579!
580!--             User defined quantity
581                CALL user_data_output_2d( av, do2d(av,if), found, grid, &
582                                          local_pf, two_d )
583                resorted = .TRUE.
584
585                IF ( grid == 'zu' )  THEN
586                   IF ( mode == 'xy' )  level_z = zu
587                ELSEIF ( grid == 'zw' )  THEN
588                   IF ( mode == 'xy' )  level_z = zw
589                ELSEIF ( grid == 'zu1' ) THEN
590                   IF ( mode == 'xy' )  level_z(nzb+1) = zu(nzb+1)
591                ENDIF
592
593                IF ( .NOT. found )  THEN
594                   message_string = 'no output provided for: ' //    &
595                                    TRIM( do2d(av,if) )
596                   CALL message( 'data_output_2d', 'PA0181', 0, 0, 0, 6, 0 )
597                ENDIF
598
599          END SELECT
600
601!
602!--       Resort the array to be output, if not done above
603          IF ( .NOT. resorted )  THEN
604             DO  i = nxlg, nxrg
605                DO  j = nysg, nyng
606                   DO  k = nzb, nzt+1
607                      local_pf(i,j,k) = to_be_resorted(k,j,i)
608                   ENDDO
609                ENDDO
610             ENDDO
611          ENDIF
612
613!
614!--       Output of the individual cross-sections, depending on the cross-
615!--       section mode chosen.
616          is = 1
617   loop1: DO  WHILE ( section(is,s) /= -9999  .OR.  two_d )
618
619             SELECT CASE ( mode )
620
621                CASE ( 'xy' )
622!
623!--                Determine the cross section index
624                   IF ( two_d )  THEN
625                      layer_xy = nzb+1
626                   ELSE
627                      layer_xy = section(is,s)
628                   ENDIF
629
630!
631!--                Update the NetCDF xy cross section time axis
632                   IF ( myid == 0  .OR.  netcdf_data_format > 2 )  THEN
633                      IF ( simulated_time /= do2d_xy_last_time(av) )  THEN
634                         do2d_xy_time_count(av) = do2d_xy_time_count(av) + 1
635                         do2d_xy_last_time(av)  = simulated_time
636                         IF ( ( .NOT. data_output_2d_on_each_pe  .AND. &
637                              netcdf_output )  .OR.  netcdf_data_format > 2 ) &
638                         THEN
639#if defined( __netcdf )
640                            nc_stat = NF90_PUT_VAR( id_set_xy(av),             &
641                                                    id_var_time_xy(av),        &
642                                             (/ time_since_reference_point /), &
643                                         start = (/ do2d_xy_time_count(av) /), &
644                                                    count = (/ 1 /) )
645                            CALL handle_netcdf_error( 'data_output_2d', 53 )
646#endif
647                         ENDIF
648                      ENDIF
649                   ENDIF
650!
651!--                If required, carry out averaging along z
652                   IF ( section(is,s) == -1  .AND.  .NOT. two_d )  THEN
653
654                      local_2d = 0.0
655!
656!--                   Carry out the averaging (all data are on the PE)
657                      DO  k = nzb, nzt+1
658                         DO  j = nysg, nyng
659                            DO  i = nxlg, nxrg
660                               local_2d(i,j) = local_2d(i,j) + local_pf(i,j,k)
661                            ENDDO
662                         ENDDO
663                      ENDDO
664
665                      local_2d = local_2d / ( nzt -nzb + 2.0)
666
667                   ELSE
668!
669!--                   Just store the respective section on the local array
670                      local_2d = local_pf(:,:,layer_xy)
671
672                   ENDIF
673
674#if defined( __parallel )
675                   IF ( netcdf_output  .AND.  netcdf_data_format > 2 )  THEN
676!
677!--                   Output in NetCDF4/HDF5 format.
678!--                   Do not output redundant ghost point data except for the
679!--                   boundaries of the total domain.
680                      IF ( two_d ) THEN
681                         iis = 1
682                      ELSE
683                         iis = is
684                      ENDIF
685
686#if defined( __netcdf )
687                      IF ( nxr == nx  .AND.  nyn /= ny )  THEN
688                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
689                                                 id_var_do2d(av,if),           &
690                                                 local_2d(nxl:nxr+1,nys:nyn),  &
691                                                 start = (/ nxl+1, nys+1, iis, &
692                                                    do2d_xy_time_count(av) /), &
693                                                 count = (/ nxr-nxl+2,         &
694                                                            nyn-nys+1, 1, 1 /) )
695                      ELSEIF ( nxr /= nx  .AND.  nyn == ny )  THEN
696                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
697                                                 id_var_do2d(av,if),           &
698                                                 local_2d(nxl:nxr,nys:nyn+1),  &
699                                                 start = (/ nxl+1, nys+1, iis, &
700                                                    do2d_xy_time_count(av) /), &
701                                                 count = (/ nxr-nxl+1,         &
702                                                            nyn-nys+2, 1, 1 /) )
703                      ELSEIF ( nxr == nx  .AND.  nyn == ny )  THEN
704                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
705                                                 id_var_do2d(av,if),           &
706                                                 local_2d(nxl:nxr+1,nys:nyn+1),&
707                                                 start = (/ nxl+1, nys+1, iis, &
708                                                    do2d_xy_time_count(av) /), &
709                                                 count = (/ nxr-nxl+2,          &
710                                                            nyn-nys+2, 1, 1 /) )
711                      ELSE
712                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
713                                                 id_var_do2d(av,if),           &
714                                                 local_2d(nxl:nxr,nys:nyn),    &
715                                                 start = (/ nxl+1, nys+1, iis, &
716                                                    do2d_xy_time_count(av) /), &
717                                                 count = (/ nxr-nxl+1,         &
718                                                            nyn-nys+1, 1, 1 /) )
719                      ENDIF
720
721                      CALL handle_netcdf_error( 'data_output_2d', 55 )
722#endif
723                   ELSE
724
725                      IF ( data_output_2d_on_each_pe )  THEN
726!
727!--                      Output of partial arrays on each PE
728#if defined( __netcdf )
729                         IF ( netcdf_output  .AND.  myid == 0 )  THEN
730                            WRITE ( 21 )  time_since_reference_point, &
731                                          do2d_xy_time_count(av), av
732                         ENDIF
733#endif
734                         WRITE ( 21 )  nxlg, nxrg, nysg, nyng
735                         WRITE ( 21 )  local_2d
736
737                      ELSE
738!
739!--                      PE0 receives partial arrays from all processors and
740!--                      then outputs them. Here a barrier has to be set,
741!--                      because otherwise "-MPI- FATAL: Remote protocol queue
742!--                      full" may occur.
743                         CALL MPI_BARRIER( comm2d, ierr )
744
745                         ngp = ( nxrg-nxlg+1 ) * ( nyng-nysg+1 )
746                         IF ( myid == 0 )  THEN
747!
748!--                         Local array can be relocated directly.
749                            total_2d(nxlg:nxrg,nysg:nyng) = local_2d
750!
751!--                         Receive data from all other PEs.
752                            DO  n = 1, numprocs-1
753!
754!--                            Receive index limits first, then array.
755!--                            Index limits are received in arbitrary order from
756!--                            the PEs.
757                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,    &
758                                              MPI_ANY_SOURCE, 0, comm2d, &
759                                              status, ierr )
760                               sender = status(MPI_SOURCE)
761                               DEALLOCATE( local_2d )
762                               ALLOCATE( local_2d(ind(1):ind(2),ind(3):ind(4)) )
763                               CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp,  &
764                                              MPI_REAL, sender, 1, comm2d,   &
765                                              status, ierr )
766                               total_2d(ind(1):ind(2),ind(3):ind(4)) = local_2d
767                            ENDDO
768!
769!--                         Output of the total cross-section.
770                            IF ( iso2d_output )  THEN
771                               WRITE (21)  total_2d(-nbgp:nx+nbgp,-nbgp:ny+nbgp)
772                            ENDIF
773!
774!--                         Relocate the local array for the next loop increment
775                            DEALLOCATE( local_2d )
776                            ALLOCATE( local_2d(nxlg:nxrg,nysg:nyng) )
777
778#if defined( __netcdf )
779                            IF ( netcdf_output )  THEN
780                               IF ( two_d ) THEN
781                                  nc_stat = NF90_PUT_VAR( id_set_xy(av),       &
782                                                          id_var_do2d(av,if),  &
783                                                      total_2d(0:nx+1,0:ny+1), &
784                                start = (/ 1, 1, 1, do2d_xy_time_count(av) /), &
785                                                count = (/ nx+2, ny+2, 1, 1 /) )
786                               ELSE
787                                  nc_stat = NF90_PUT_VAR( id_set_xy(av),       &
788                                                          id_var_do2d(av,if),  &
789                                                      total_2d(0:nx+1,0:ny+1), &
790                               start = (/ 1, 1, is, do2d_xy_time_count(av) /), &
791                                                count = (/ nx+2, ny+2, 1, 1 /) )
792                               ENDIF
793                               CALL handle_netcdf_error( 'data_output_2d', 54 )
794                            ENDIF
795#endif
796
797                         ELSE
798!
799!--                         First send the local index limits to PE0
800                            ind(1) = nxlg; ind(2) = nxrg
801                            ind(3) = nysg; ind(4) = nyng
802                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0, &
803                                           comm2d, ierr )
804!
805!--                         Send data to PE0
806                            CALL MPI_SEND( local_2d(nxlg,nysg), ngp, &
807                                           MPI_REAL, 0, 1, comm2d, ierr )
808                         ENDIF
809!
810!--                      A barrier has to be set, because otherwise some PEs may
811!--                      proceed too fast so that PE0 may receive wrong data on
812!--                      tag 0
813                         CALL MPI_BARRIER( comm2d, ierr )
814                      ENDIF
815
816                   ENDIF
817#else
818                   IF ( iso2d_output )  THEN
819                      WRITE (21)  local_2d(nxl:nxr+1,nys:nyn+1)
820                   ENDIF
821#if defined( __netcdf )
822                   IF ( netcdf_output )  THEN
823                      IF ( two_d ) THEN
824                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
825                                                 id_var_do2d(av,if),           &
826                                                local_2d(nxl:nxr+1,nys:nyn+1), &
827                                start = (/ 1, 1, 1, do2d_xy_time_count(av) /), &
828                                              count = (/ nx+2, ny+2, 1, 1 /) )
829                      ELSE
830                         nc_stat = NF90_PUT_VAR( id_set_xy(av),                &
831                                                 id_var_do2d(av,if),           &
832                                                local_2d(nxl:nxr+1,nys:nyn+1), &
833                               start = (/ 1, 1, is, do2d_xy_time_count(av) /), &
834                                              count = (/ nx+2, ny+2, 1, 1 /) )
835                      ENDIF
836                      CALL handle_netcdf_error( 'data_output_2d', 447 )
837                   ENDIF
838#endif
839#endif
840                   do2d_xy_n = do2d_xy_n + 1
841!
842!--                Write LOCAL parameter set for ISO2D.
843                   IF ( myid == 0  .AND.  iso2d_output )  THEN
844                      IF ( section(is,s) /= -1 )  THEN
845                         WRITE ( section_chr, '(''z = '',F7.2,'' m  (GP '',I3, &
846                                               &'')'')'                        &
847                               )  level_z(layer_xy), layer_xy
848                      ELSE
849                         section_chr = 'averaged along z'
850                      ENDIF
851                      IF ( av == 0 )  THEN
852                         rtext = TRIM( do2d(av,if) ) // '  t = ' //    &
853                                 TRIM( simulated_time_chr ) // '  ' // &
854                                 TRIM( section_chr )
855                      ELSE
856                         rtext = TRIM( do2d(av,if) ) // '  averaged t = ' // &
857                                 TRIM( simulated_time_chr ) // '  ' //       &
858                                 TRIM( section_chr )
859                      ENDIF
860                      WRITE (27,LOCAL)
861                   ENDIF
862!
863!--                For 2D-arrays (e.g. u*) only one cross-section is available.
864!--                Hence exit loop of output levels.
865                   IF ( two_d )  THEN
866                      two_d = .FALSE.
867                      EXIT loop1
868                   ENDIF
869
870                CASE ( 'xz' )
871!
872!--                Update the NetCDF xz cross section time axis
873                   IF ( myid == 0  .OR.  netcdf_data_format > 2 )  THEN
874
875                      IF ( simulated_time /= do2d_xz_last_time(av) )  THEN
876                         do2d_xz_time_count(av) = do2d_xz_time_count(av) + 1
877                         do2d_xz_last_time(av)  = simulated_time
878                         IF ( ( .NOT. data_output_2d_on_each_pe  .AND.        &
879                              netcdf_output )  .OR.  netcdf_data_format > 2 ) &
880                         THEN
881#if defined( __netcdf )
882                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
883                                                    id_var_time_xz(av),        &
884                                             (/ time_since_reference_point /), &
885                                         start = (/ do2d_xz_time_count(av) /), &
886                                                    count = (/ 1 /) )
887                            CALL handle_netcdf_error( 'data_output_2d', 56 )
888#endif
889                         ENDIF
890                      ENDIF
891
892                   ENDIF
893
894!
895!--                If required, carry out averaging along y
896                   IF ( section(is,s) == -1 )  THEN
897
898                      ALLOCATE( local_2d_l(nxlg:nxrg,nzb:nzt+1) )
899                      local_2d_l = 0.0
900                      ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
901!
902!--                   First local averaging on the PE
903                      DO  k = nzb, nzt+1
904                         DO  j = nys, nyn
905                            DO  i = nxlg, nxrg
906                               local_2d_l(i,k) = local_2d_l(i,k) + &
907                                                 local_pf(i,j,k)
908                            ENDDO
909                         ENDDO
910                      ENDDO
911#if defined( __parallel )
912!
913!--                   Now do the averaging over all PEs along y
914                      IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
915                      CALL MPI_ALLREDUCE( local_2d_l(nxlg,nzb),              &
916                                          local_2d(nxlg,nzb), ngp, MPI_REAL, &
917                                          MPI_SUM, comm1dy, ierr )
918#else
919                      local_2d = local_2d_l
920#endif
921                      local_2d = local_2d / ( ny + 1.0 )
922
923                      DEALLOCATE( local_2d_l )
924
925                   ELSE
926!
927!--                   Just store the respective section on the local array
928!--                   (but only if it is available on this PE!)
929                      IF ( section(is,s) >= nys  .AND.  section(is,s) <= nyn ) &
930                      THEN
931                         local_2d = local_pf(:,section(is,s),nzb:nzt+1)
932                      ENDIF
933
934                   ENDIF
935
936#if defined( __parallel )
937                   IF ( netcdf_output  .AND.  netcdf_data_format > 2 )  THEN
938!
939!--                   ATTENTION: The following lines are a workaround, because
940!--                              independet output does not work with the
941!--                              current NetCDF4 installation. Therefore, data
942!--                              are transferred from PEs having the cross
943!--                              sections to other PEs along y having no cross
944!--                              section data. Some of these data are the
945!--                              output.
946!--                   BEGIN WORKAROUND---------------------------------------
947                      IF ( npey /= 1  .AND.  section(is,s) /= -1)  THEN
948                         ALLOCATE( local_2d_l(nxlg:nxrg,nzb:nzt+1) )
949                         local_2d_l = 0.0
950                         IF ( section(is,s) >= nys .AND. section(is,s) <= nyn )&
951                         THEN
952                            local_2d_l = local_2d
953                         ENDIF
954#if defined( __parallel )
955!
956!--                      Distribute data over all PEs along y
957                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
958                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
959                         CALL MPI_ALLREDUCE( local_2d_l(nxlg,nzb),            &
960                                             local_2d(nxlg,nzb), ngp,         &
961                                             MPI_REAL, MPI_SUM, comm1dy, ierr )
962#else
963                         local_2d = local_2d_l
964#endif
965                         DEALLOCATE( local_2d_l )
966                      ENDIF
967!--                   END WORKAROUND-----------------------------------------
968
969!
970!--                   Output in NetCDF4/HDF5 format.
971!--                   Output only on those PEs where the respective cross
972!--                   sections reside. Cross sections averaged along y are
973!--                   output on the respective first PE along y (myidy=0).
974                      IF ( ( section(is,s) >= nys  .AND.  &
975                             section(is,s) <= nyn )  .OR.  &
976                           ( section(is,s) == -1  .AND.  myidy == 0 ) )  THEN
977!
978!--                      Do not output redundant ghost point data except for the
979!--                      boundaries of the total domain.
980#if defined( __netcdf )
981                         IF ( nxr == nx )  THEN
982                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
983                                                id_var_do2d(av,if),            &
984                                                local_2d(nxl:nxr+1,nzb:nzt+1), &
985                                                start = (/ nxl+1, is, 1,       &
986                                                    do2d_xz_time_count(av) /), &
987                                                count = (/ nxr-nxl+2, 1,       &
988                                                           nzt+2, 1 /) )
989                         ELSE
990                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
991                                                id_var_do2d(av,if),            &
992                                                local_2d(nxl:nxr,nzb:nzt+1),   &
993                                                start = (/ nxl+1, is, 1,       &
994                                                    do2d_xz_time_count(av) /), &
995                                                count = (/ nxr-nxl+1, 1,       &
996                                                           nzt+2, 1 /) )
997                         ENDIF
998
999                         CALL handle_netcdf_error( 'data_output_2d', 57 )
1000
1001                      ELSE
1002!
1003!--                      Output on other PEs. Only one point is output!!
1004!--                      ATTENTION: This is a workaround (see above)!!
1005                         IF ( npey /= 1 )  THEN
1006                            nc_stat = NF90_PUT_VAR( id_set_xz(av),             &
1007                                                    id_var_do2d(av,if),        &
1008                                                    local_2d(nxl:nxl,nzb:nzb), &
1009                                                    start = (/ nxl+1, is, 1,   &
1010                                                    do2d_xz_time_count(av) /), &
1011                                                    count = (/ 1, 1, 1, 1 /) )
1012                            CALL handle_netcdf_error( 'data_output_2d', 451 )
1013                         ENDIF
1014#endif
1015                      ENDIF
1016
1017                   ELSE
1018
1019                      IF ( data_output_2d_on_each_pe )  THEN
1020!
1021!--                      Output of partial arrays on each PE. If the cross
1022!--                      section does not reside on the PE, output special
1023!--                      index values.
1024#if defined( __netcdf )
1025                         IF ( netcdf_output  .AND.  myid == 0 )  THEN
1026                            WRITE ( 22 )  time_since_reference_point, &
1027                                          do2d_xz_time_count(av), av
1028                         ENDIF
1029#endif
1030                         IF ( ( section(is,s) >= nys  .AND.                  &
1031                                section(is,s) <= nyn )  .OR.                 &
1032                              ( section(is,s) == -1  .AND.  nys-1 == -1 ) )  &
1033                         THEN
1034                            WRITE (22)  nxlg, nxrg, nzb, nzt+1
1035                            WRITE (22)  local_2d
1036                         ELSE
1037                            WRITE (22)  -1, -1, -1, -1
1038                         ENDIF
1039
1040                      ELSE
1041!
1042!--                      PE0 receives partial arrays from all processors of the
1043!--                      respective cross section and outputs them. Here a
1044!--                      barrier has to be set, because otherwise
1045!--                      "-MPI- FATAL: Remote protocol queue full" may occur.
1046                         CALL MPI_BARRIER( comm2d, ierr )
1047
1048                         ngp = ( nxrg-nxlg+1 ) * ( nzt-nzb+2 )
1049                         IF ( myid == 0 )  THEN
1050!
1051!--                         Local array can be relocated directly.
1052                            IF ( ( section(is,s) >= nys  .AND.                 &
1053                                   section(is,s) <= nyn )  .OR.                &
1054                                 ( section(is,s) == -1  .AND.  nys-1 == -1 ) ) &
1055                            THEN
1056                               total_2d(nxlg:nxrg,nzb:nzt+1) = local_2d
1057                            ENDIF
1058!
1059!--                         Receive data from all other PEs.
1060                            DO  n = 1, numprocs-1
1061!
1062!--                            Receive index limits first, then array.
1063!--                            Index limits are received in arbitrary order from
1064!--                            the PEs.
1065                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,     &
1066                                              MPI_ANY_SOURCE, 0, comm2d,  &
1067                                              status, ierr )
1068!
1069!--                            Not all PEs have data for XZ-cross-section.
1070                               IF ( ind(1) /= -9999 )  THEN
1071                                  sender = status(MPI_SOURCE)
1072                                  DEALLOCATE( local_2d )
1073                                  ALLOCATE( local_2d(ind(1):ind(2), &
1074                                                     ind(3):ind(4)) )
1075                                  CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp, &
1076                                                 MPI_REAL, sender, 1, comm2d,  &
1077                                                 status, ierr )
1078                                  total_2d(ind(1):ind(2),ind(3):ind(4)) = &
1079                                                                        local_2d
1080                               ENDIF
1081                            ENDDO
1082!
1083!--                         Output of the total cross-section.
1084                            IF ( iso2d_output )  THEN
1085                               WRITE (22)  total_2d(-nbgp:nx+nbgp,nzb:nzt+1)
1086                            ENDIF
1087!
1088!--                         Relocate the local array for the next loop increment
1089                            DEALLOCATE( local_2d )
1090                            ALLOCATE( local_2d(nxlg:nxrg,nzb:nzt+1) )
1091
1092#if defined( __netcdf )
1093                            IF ( netcdf_output )  THEN
1094                               nc_stat = NF90_PUT_VAR( id_set_xz(av),          &
1095                                                    id_var_do2d(av,if),        &
1096                                                    total_2d(0:nx+1,nzb:nzt+1),&
1097                               start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
1098                                                count = (/ nx+2, 1, nz+2, 1 /) )
1099                               CALL handle_netcdf_error( 'data_output_2d', 58 )
1100                            ENDIF
1101#endif
1102
1103                         ELSE
1104!
1105!--                         If the cross section resides on the PE, send the
1106!--                         local index limits, otherwise send -9999 to PE0.
1107                            IF ( ( section(is,s) >= nys  .AND.                 &
1108                                   section(is,s) <= nyn )  .OR.                &
1109                                 ( section(is,s) == -1  .AND.  nys-1 == -1 ) ) &
1110                            THEN
1111                               ind(1) = nxlg; ind(2) = nxrg
1112                               ind(3) = nzb;   ind(4) = nzt+1
1113                            ELSE
1114                               ind(1) = -9999; ind(2) = -9999
1115                               ind(3) = -9999; ind(4) = -9999
1116                            ENDIF
1117                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0, &
1118                                           comm2d, ierr )
1119!
1120!--                         If applicable, send data to PE0.
1121                            IF ( ind(1) /= -9999 )  THEN
1122                               CALL MPI_SEND( local_2d(nxlg,nzb), ngp, &
1123                                              MPI_REAL, 0, 1, comm2d, ierr )
1124                            ENDIF
1125                         ENDIF
1126!
1127!--                      A barrier has to be set, because otherwise some PEs may
1128!--                      proceed too fast so that PE0 may receive wrong data on
1129!--                      tag 0
1130                         CALL MPI_BARRIER( comm2d, ierr )
1131                      ENDIF
1132
1133                   ENDIF
1134#else
1135                   IF ( iso2d_output )  THEN
1136                      WRITE (22)  local_2d(nxl:nxr+1,nzb:nzt+1)
1137                   ENDIF
1138#if defined( __netcdf )
1139                   IF ( netcdf_output )  THEN
1140                      nc_stat = NF90_PUT_VAR( id_set_xz(av),                   &
1141                                              id_var_do2d(av,if),              &
1142                                              local_2d(nxl:nxr+1,nzb:nzt+1),   &
1143                               start = (/ 1, is, 1, do2d_xz_time_count(av) /), &
1144                                              count = (/ nx+2, 1, nz+2, 1 /) )
1145                      CALL handle_netcdf_error( 'data_output_2d', 451 )
1146                   ENDIF
1147#endif
1148#endif
1149                   do2d_xz_n = do2d_xz_n + 1
1150!
1151!--                Write LOCAL-parameter set for ISO2D.
1152                   IF ( myid == 0  .AND.  iso2d_output )  THEN
1153                      IF ( section(is,s) /= -1 )  THEN
1154                         WRITE ( section_chr, '(''y = '',F8.2,'' m  (GP '',I3, &
1155                                               &'')'')'                        &
1156                               )  section(is,s)*dy, section(is,s)
1157                      ELSE
1158                         section_chr = 'averaged along y'
1159                      ENDIF
1160                      IF ( av == 0 )  THEN
1161                         rtext = TRIM( do2d(av,if) ) // '  t = ' //    &
1162                                 TRIM( simulated_time_chr ) // '  ' // &
1163                                 TRIM( section_chr )
1164                      ELSE
1165                         rtext = TRIM( do2d(av,if) ) // '  averaged t = ' // &
1166                                 TRIM( simulated_time_chr ) // '  ' //       &
1167                                 TRIM( section_chr )
1168                      ENDIF
1169                      WRITE (28,LOCAL)
1170                   ENDIF
1171
1172                CASE ( 'yz' )
1173!
1174!--                Update the NetCDF yz cross section time axis
1175                   IF ( myid == 0  .OR.  netcdf_data_format > 2 )  THEN
1176
1177                      IF ( simulated_time /= do2d_yz_last_time(av) )  THEN
1178                         do2d_yz_time_count(av) = do2d_yz_time_count(av) + 1
1179                         do2d_yz_last_time(av)  = simulated_time
1180                         IF ( ( .NOT. data_output_2d_on_each_pe  .AND.        &
1181                              netcdf_output )  .OR.  netcdf_data_format > 2 ) &
1182                         THEN
1183#if defined( __netcdf )
1184                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1185                                                    id_var_time_yz(av),        &
1186                                             (/ time_since_reference_point /), &
1187                                         start = (/ do2d_yz_time_count(av) /), &
1188                                                    count = (/ 1 /) )
1189                            CALL handle_netcdf_error( 'data_output_2d', 59 )
1190#endif
1191                         ENDIF
1192                      ENDIF
1193
1194                   ENDIF
1195!
1196!--                If required, carry out averaging along x
1197                   IF ( section(is,s) == -1 )  THEN
1198
1199                      ALLOCATE( local_2d_l(nysg:nyng,nzb:nzt+1) )
1200                      local_2d_l = 0.0
1201                      ngp = ( nyng-nysg+1 ) * ( nzt-nzb+2 )
1202!
1203!--                   First local averaging on the PE
1204                      DO  k = nzb, nzt+1
1205                         DO  j = nysg, nyng
1206                            DO  i = nxl, nxr
1207                               local_2d_l(j,k) = local_2d_l(j,k) + &
1208                                                 local_pf(i,j,k)
1209                            ENDDO
1210                         ENDDO
1211                      ENDDO
1212#if defined( __parallel )
1213!
1214!--                   Now do the averaging over all PEs along x
1215                      IF ( collective_wait )  CALL MPI_BARRIER( comm2d, ierr )
1216                      CALL MPI_ALLREDUCE( local_2d_l(nysg,nzb),              &
1217                                          local_2d(nysg,nzb), ngp, MPI_REAL, &
1218                                          MPI_SUM, comm1dx, ierr )
1219#else
1220                      local_2d = local_2d_l
1221#endif
1222                      local_2d = local_2d / ( nx + 1.0 )
1223
1224                      DEALLOCATE( local_2d_l )
1225
1226                   ELSE
1227!
1228!--                   Just store the respective section on the local array
1229!--                   (but only if it is available on this PE!)
1230                      IF ( section(is,s) >= nxl  .AND.  section(is,s) <= nxr ) &
1231                      THEN
1232                         local_2d = local_pf(section(is,s),:,nzb:nzt+1)
1233                      ENDIF
1234
1235                   ENDIF
1236
1237#if defined( __parallel )
1238                   IF ( netcdf_output  .AND.  netcdf_data_format > 2 )  THEN
1239!
1240!--                   ATTENTION: The following lines are a workaround, because
1241!--                              independet output does not work with the
1242!--                              current NetCDF4 installation. Therefore, data
1243!--                              are transferred from PEs having the cross
1244!--                              sections to other PEs along y having no cross
1245!--                              section data. Some of these data are the
1246!--                              output.
1247!--                   BEGIN WORKAROUND---------------------------------------
1248                      IF ( npex /= 1  .AND.  section(is,s) /= -1)  THEN
1249                         ALLOCATE( local_2d_l(nysg:nyng,nzb:nzt+1) )
1250                         local_2d_l = 0.0
1251                         IF ( section(is,s) >= nxl .AND. section(is,s) <= nxr )&
1252                         THEN
1253                            local_2d_l = local_2d
1254                         ENDIF
1255#if defined( __parallel )
1256!
1257!--                      Distribute data over all PEs along x
1258                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb + 2 )
1259                         IF ( collective_wait ) CALL MPI_BARRIER( comm2d, ierr )
1260                         CALL MPI_ALLREDUCE( local_2d_l(nysg,nzb),            &
1261                                             local_2d(nysg,nzb), ngp,         &
1262                                             MPI_REAL, MPI_SUM, comm1dx, ierr )
1263#else
1264                         local_2d = local_2d_l
1265#endif
1266                         DEALLOCATE( local_2d_l )
1267                      ENDIF
1268!--                   END WORKAROUND-----------------------------------------
1269
1270!
1271!--                   Output in NetCDF4/HDF5 format.
1272!--                   Output only on those PEs where the respective cross
1273!--                   sections reside. Cross sections averaged along x are
1274!--                   output on the respective first PE along x (myidx=0).
1275                      IF ( ( section(is,s) >= nxl  .AND.  &
1276                             section(is,s) <= nxr )  .OR.  &
1277                           ( section(is,s) == -1  .AND.  myidx == 0 ) )  THEN
1278!
1279!--                      Do not output redundant ghost point data except for the
1280!--                      boundaries of the total domain.
1281#if defined( __netcdf )
1282                         IF ( nyn == ny )  THEN
1283                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1284                                                id_var_do2d(av,if),            &
1285                                                local_2d(nys:nyn+1,nzb:nzt+1), &
1286                                                start = (/ is, nys+1, 1,       &
1287                                                    do2d_yz_time_count(av) /), &
1288                                                count = (/ 1, nyn-nys+2,       &
1289                                                           nzt+2, 1 /) )
1290                         ELSE
1291                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1292                                                id_var_do2d(av,if),            &
1293                                                local_2d(nys:nyn,nzb:nzt+1),   &
1294                                                start = (/ is, nys+1, 1,       &
1295                                                    do2d_yz_time_count(av) /), &
1296                                                count = (/ 1, nyn-nys+1,       &
1297                                                           nzt+2, 1 /) )
1298                         ENDIF
1299
1300                         CALL handle_netcdf_error( 'data_output_2d', 60 )
1301
1302                      ELSE
1303!
1304!--                      Output on other PEs. Only one point is output!!
1305!--                      ATTENTION: This is a workaround (see above)!!
1306                         IF ( npex /= 1 )  THEN
1307                            nc_stat = NF90_PUT_VAR( id_set_yz(av),             &
1308                                                    id_var_do2d(av,if),        &
1309                                                    local_2d(nys:nys,nzb:nzb), &
1310                                                    start = (/ is, nys+1, 1,   &
1311                                                    do2d_yz_time_count(av) /), &
1312                                                    count = (/ 1, 1, 1, 1 /) )
1313                            CALL handle_netcdf_error( 'data_output_2d', 452 )
1314                         ENDIF
1315#endif
1316                      ENDIF
1317
1318                   ELSE
1319
1320                      IF ( data_output_2d_on_each_pe )  THEN
1321!
1322!--                      Output of partial arrays on each PE. If the cross
1323!--                      section does not reside on the PE, output special
1324!--                      index values.
1325#if defined( __netcdf )
1326                         IF ( netcdf_output  .AND.  myid == 0 )  THEN
1327                            WRITE ( 23 )  time_since_reference_point, &
1328                                          do2d_yz_time_count(av), av
1329                         ENDIF
1330#endif
1331                         IF ( ( section(is,s) >= nxl  .AND.                  &
1332                                section(is,s) <= nxr )  .OR.                 &
1333                              ( section(is,s) == -1  .AND.  nxl-1 == -1 ) )  &
1334                         THEN
1335                            WRITE (23)  nysg, nyng, nzb, nzt+1
1336                            WRITE (23)  local_2d
1337                         ELSE
1338                            WRITE (23)  -1, -1, -1, -1
1339                         ENDIF
1340
1341                      ELSE
1342!
1343!--                      PE0 receives partial arrays from all processors of the
1344!--                      respective cross section and outputs them. Here a
1345!--                      barrier has to be set, because otherwise
1346!--                      "-MPI- FATAL: Remote protocol queue full" may occur.
1347                         CALL MPI_BARRIER( comm2d, ierr )
1348
1349                         ngp = ( nyng-nysg+1 ) * ( nzt-nzb+2 )
1350                         IF ( myid == 0 )  THEN
1351!
1352!--                         Local array can be relocated directly.
1353                            IF ( ( section(is,s) >= nxl  .AND.                 &
1354                                   section(is,s) <= nxr )   .OR.               &
1355                                 ( section(is,s) == -1  .AND.  nxl-1 == -1 ) ) &
1356                            THEN
1357                               total_2d(nysg:nyng,nzb:nzt+1) = local_2d
1358                            ENDIF
1359!
1360!--                         Receive data from all other PEs.
1361                            DO  n = 1, numprocs-1
1362!
1363!--                            Receive index limits first, then array.
1364!--                            Index limits are received in arbitrary order from
1365!--                            the PEs.
1366                               CALL MPI_RECV( ind(1), 4, MPI_INTEGER,     &
1367                                              MPI_ANY_SOURCE, 0, comm2d,  &
1368                                              status, ierr )
1369!
1370!--                            Not all PEs have data for YZ-cross-section.
1371                               IF ( ind(1) /= -9999 )  THEN
1372                                  sender = status(MPI_SOURCE)
1373                                  DEALLOCATE( local_2d )
1374                                  ALLOCATE( local_2d(ind(1):ind(2), &
1375                                                     ind(3):ind(4)) )
1376                                  CALL MPI_RECV( local_2d(ind(1),ind(3)), ngp, &
1377                                                 MPI_REAL, sender, 1, comm2d,  &
1378                                                 status, ierr )
1379                                  total_2d(ind(1):ind(2),ind(3):ind(4)) = &
1380                                                                        local_2d
1381                               ENDIF
1382                            ENDDO
1383!
1384!--                         Output of the total cross-section.
1385                            IF ( iso2d_output )  THEN
1386                               WRITE (23)  total_2d(0:ny+1,nzb:nzt+1)
1387                            ENDIF
1388!
1389!--                         Relocate the local array for the next loop increment
1390                            DEALLOCATE( local_2d )
1391                            ALLOCATE( local_2d(nysg:nyng,nzb:nzt+1) )
1392
1393#if defined( __netcdf )
1394                            IF ( netcdf_output )  THEN
1395                               nc_stat = NF90_PUT_VAR( id_set_yz(av),          &
1396                                                    id_var_do2d(av,if),        &
1397                                                    total_2d(0:ny+1,nzb:nzt+1),&
1398                               start = (/ is, 1, 1, do2d_yz_time_count(av) /), &
1399                                                count = (/ 1, ny+2, nz+2, 1 /) )
1400                               CALL handle_netcdf_error( 'data_output_2d', 61 )
1401                            ENDIF
1402#endif
1403
1404                         ELSE
1405!
1406!--                         If the cross section resides on the PE, send the
1407!--                         local index limits, otherwise send -9999 to PE0.
1408                            IF ( ( section(is,s) >= nxl  .AND.                 &
1409                                   section(is,s) <= nxr )  .OR.                &
1410                                 ( section(is,s) == -1  .AND.  nxl-1 == -1 ) ) &
1411                            THEN
1412                               ind(1) = nysg; ind(2) = nyng
1413                               ind(3) = nzb;   ind(4) = nzt+1
1414                            ELSE
1415                               ind(1) = -9999; ind(2) = -9999
1416                               ind(3) = -9999; ind(4) = -9999
1417                            ENDIF
1418                            CALL MPI_SEND( ind(1), 4, MPI_INTEGER, 0, 0, &
1419                                           comm2d, ierr )
1420!
1421!--                         If applicable, send data to PE0.
1422                            IF ( ind(1) /= -9999 )  THEN
1423                               CALL MPI_SEND( local_2d(nysg,nzb), ngp, &
1424                                              MPI_REAL, 0, 1, comm2d, ierr )
1425                            ENDIF
1426                         ENDIF
1427!
1428!--                      A barrier has to be set, because otherwise some PEs may
1429!--                      proceed too fast so that PE0 may receive wrong data on
1430!--                      tag 0
1431                         CALL MPI_BARRIER( comm2d, ierr )
1432                      ENDIF
1433
1434                   ENDIF
1435#else
1436                   IF ( iso2d_output )  THEN
1437                      WRITE (23)  local_2d(nys:nyn+1,nzb:nzt+1)
1438                   ENDIF
1439#if defined( __netcdf )
1440                   IF ( netcdf_output )  THEN
1441                      nc_stat = NF90_PUT_VAR( id_set_yz(av),                   &
1442                                              id_var_do2d(av,if),              &
1443                                              local_2d(nys:nyn+1,nzb:nzt+1),   &
1444                               start = (/ is, 1, 1, do2d_xz_time_count(av) /), &
1445                                              count = (/ 1, ny+2, nz+2, 1 /) )
1446                      CALL handle_netcdf_error( 'data_output_2d', 452 )
1447                   ENDIF
1448#endif
1449#endif
1450                   do2d_yz_n = do2d_yz_n + 1
1451!
1452!--                Write LOCAL-parameter set for ISO2D.
1453                   IF ( myid == 0  .AND.  iso2d_output )  THEN
1454                      IF ( section(is,s) /= -1 )  THEN
1455                         WRITE ( section_chr, '(''x = '',F8.2,'' m  (GP '',I3, &
1456                                               &'')'')'                        &
1457                               )  section(is,s)*dx, section(is,s)
1458                      ELSE
1459                         section_chr = 'averaged along x'
1460                      ENDIF
1461                      IF ( av == 0 )  THEN
1462                         rtext = TRIM( do2d(av,if) ) // '  t = ' //    &
1463                                 TRIM( simulated_time_chr ) // '  ' // &
1464                                 TRIM( section_chr )
1465                      ELSE
1466                         rtext = TRIM( do2d(av,if) ) // '  averaged t = ' // &
1467                                 TRIM( simulated_time_chr ) // '  ' //       &
1468                                 TRIM( section_chr )
1469                      ENDIF
1470                      WRITE (29,LOCAL)
1471                   ENDIF
1472
1473             END SELECT
1474
1475             is = is + 1
1476          ENDDO loop1
1477
1478       ENDIF
1479
1480       if = if + 1
1481       l = MAX( 2, LEN_TRIM( do2d(av,if) ) )
1482       do2d_mode = do2d(av,if)(l-1:l)
1483
1484    ENDDO
1485
1486!
1487!-- Deallocate temporary arrays.
1488    IF ( ALLOCATED( level_z ) )  DEALLOCATE( level_z )
1489    DEALLOCATE( local_pf, local_2d )
1490#if defined( __parallel )
1491    IF ( .NOT.  data_output_2d_on_each_pe  .AND.  myid == 0 )  THEN
1492       DEALLOCATE( total_2d )
1493    ENDIF
1494#endif
1495
1496!
1497!-- Close plot output file.
1498    file_id = 20 + s
1499
1500    IF ( data_output_2d_on_each_pe )  THEN
1501       CALL close_file( file_id )
1502    ELSE
1503       IF ( myid == 0 )  CALL close_file( file_id )
1504    ENDIF
1505
1506
1507    CALL cpu_log (log_point(3),'data_output_2d','stop','nobarrier')
1508
1509 END SUBROUTINE data_output_2d
Note: See TracBrowser for help on using the repository browser.