source: palm/tags/release-5.0/UTIL/combine_plot_fields.f90

Last change on this file was 2696, checked in by kanani, 6 years ago

Merge of branch palm4u into trunk

  • Property svn:keywords set to Id
File size: 29.9 KB
Line 
1 PROGRAM combine_plot_fields
2
3!------------------------------------------------------------------------------!
4! This file is part of the PALM model system.
5!
6! PALM is free software: you can redistribute it and/or modify it under the
7! terms of the GNU General Public License as published by the Free Software
8! Foundation, either version 3 of the License, or (at your option) any later
9! version.
10!
11! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
12! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14!
15! You should have received a copy of the GNU General Public License along with
16! PALM. If not, see <http://www.gnu.org/licenses/>.
17!
18! Copyright 1997-2017  Leibniz Universitaet Hannover
19!------------------------------------------------------------------------------!
20!
21! Current revisions:
22! -----------------
23!
24!
25! Former revisions:
26! -----------------
27! $Id: combine_plot_fields.f90 2696 2017-12-14 17:12:51Z banzhafs $
28! data of 3d-nest runs are completely processed now
29!
30! 2523 2017-10-05 14:42:47Z kanani
31! Increased LEN for CHARACTER variable var_name, equal to the value in PALM
32!
33! 2512 2017-10-04 08:26:59Z raasch
34! PALM output does not contain ghost layer data any more
35! avs- and iso2d-related parts removed, handling of compressed data removed
36!
37! 2365 2017-08-21 14:59:59Z kanani
38! Vertical grid nesting implemented (SadiqHuq)
39!
40! 1809 2016-04-05 20:13:28Z raasch
41!
42! 1808 2016-04-05 19:44:00Z raasch
43! cpu measurements are done with standard FORTRAN routine on every machine
44!
45! 1551 2015-03-03 14:18:16Z maronga
46! Adjustments for data output of soil model quantities
47!
48! 1468 2014-09-24 14:06:57Z maronga
49! Adapted for use on up to 6-digit processor cores (not tested)
50!
51! 1394 2014-05-06 10:17:31Z keck
52! KIND-attribute added to all INTEGER and REAL declaration statements
53!
54! 1046 2012-11-09 14:38:45Z maronga
55! code put under GPL (PALM 3.9)
56!
57! 691 2011-03-04 08:45:30Z maronga
58! Bugfix: check for precursor ocean runs, removed typo
59!
60! 493 2010-03-01 08:30:24Z raasch
61! Exit in case of already complete NetCDF data (due to parallel output in PALM)
62! cpu measurements included
63!
64! 210 2008-11-06 08:54:02Z raasch
65! Size of pf3d adjusted to the required output size (1 gridpoint less, along
66! all three dimensions), because output of a subset of the data
67! (pf3d(nxa:nxe...) in the NF90_PUT_VAR statement caused segmentation fault
68! with the INTEL compiler.
69! Subdomain data are read into temporary arrays pf_tmp/pf3d_tmp in order to
70! avoid INTEL compiler warnings about (automatic) creation of temporary arrays
71! Bugfix: three misplaced #endif directives
72!
73! 114 2007-10-10 00:03:15Z raasch
74! Bugfix: model_string needed a default value
75!
76! Aug 07    Loop for processing of output by coupled runs, id_string does not
77!           contain modus any longer
78!
79! 18/01/06  Output of time-averaged data
80!
81! 25/05/05  Errors removed
82!
83! 26/04/05  Output in NetCDF format, iso2d and avs output only if parameter
84!           file exists
85!
86! 31/10/01  All comments and messages translated into English
87!
88! 23/02/99  Keine Bearbeitung komprimierter 3D-Daten
89! Ursprungsversion vom 28/07/97
90!
91!
92! Description:
93! ------------
94! This routine combines data of the PALM-subdomains into one file. In PALM
95! every processor element opens its own file and writes 2D- or 3D-binary-data
96! into it (different files are opened for xy-, xz-, yz-cross-sections and
97! 3D-data). For plotting or analyzing these PE-data have to be collected and
98! to be put into single files, which is done by this routine.
99! Output format is NetCDF. Additionally, a data are output in a binary format
100! readable by ISO2D-software (cross-sections) and by AVS (3D-data).
101!------------------------------------------------------------------------------!
102
103#if defined( __netcdf )
104    USE netcdf
105#endif
106
107    IMPLICIT NONE
108
109!
110!-- Local variables
111    CHARACTER (LEN=2)    ::  modus
112    CHARACTER (LEN=4)    ::  model_string
113    CHARACTER (LEN=6)    ::  id_string
114    CHARACTER (LEN=30)   ::  dimname, var_name
115    CHARACTER (LEN=40)   ::  filename
116
117    CHARACTER (LEN=2000), DIMENSION(0:1) ::  var_list
118
119    INTEGER, PARAMETER ::  iwp = 4, spk = SELECTED_REAL_KIND( 6 ), wp = 8
120
121    INTEGER(iwp) ::  av, danz, i, id, j, k, model, models, nc_stat,            &
122                     nxa, nxag, nxe, nxeg, nya, nyag, nye, nyeg,               &
123                     nza, nze, pos, time_step, xa, xe, xxa, xxe, ya, ya_do,    &
124                     ya_tot, ye, ye_do, ye_tot, yya, yye, za, ze, zza, zze
125
126    INTEGER(8)                        ::  count, count_rate
127
128    INTEGER(iwp), DIMENSION(0:1)      ::  current_level, current_var,          &
129                                          fanz, id_set, id_var_time, num_var
130    INTEGER(iwp), DIMENSION(4)        ::  id_dims_loc
131    INTEGER(iwp), DIMENSION(0:1,4)    ::  id_dims
132    INTEGER(iwp), DIMENSION(0:1,1000) ::  id_var, levels
133
134    LOGICAL  ::  found, nest3d, netcdf_output, netcdf_parallel, netcdf_0,      &
135                 netcdf_1, vnest
136
137    REAL(wp) ::  cpu_start_time, cpu_end_time, dx, simulated_time
138    REAL(wp),  DIMENSION(:,:), ALLOCATABLE   ::  pf, pf_tmp
139    REAL(spk), DIMENSION(:,:,:), ALLOCATABLE ::  pf3d, pf3d_tmp
140
141
142
143    PRINT*, ''
144    PRINT*, ''
145    PRINT*, '*** combine_plot_fields ***'
146
147!
148!-- Find out if a coupled or nested run has been carried out
149    INQUIRE( FILE='COUPLING_PORT_OPENED', EXIST=found )
150    INQUIRE( FILE='VNESTING_PORT_OPENED', EXIST=vnest )
151    INQUIRE( FILE='3DNESTING', EXIST=nest3d )
152    IF ( found )  THEN
153       models = 2
154       PRINT*, '    coupled run'
155    ELSEIF ( vnest )  THEN
156       models = 2
157       PRINT*, '    Vertically nested grid coupling'
158    ELSEIF ( nest3d )  THEN
159       OPEN( 90, FILE='3DNESTING', FORM='FORMATTED' )
160       READ ( 90, '(I2)' )  models
161       CLOSE ( 90 )
162       PRINT*, '    3d-nest run'
163       PRINT*, '    number of nest domains = ', models
164    ELSE
165       models = 1
166       PRINT*, '    uncoupled run'
167    ENDIF
168
169!
170!-- Find out if a precursor ocean run has been carried out
171    INQUIRE( FILE='PRECURSOR_OCEAN', EXIST=found )
172    IF ( found )  THEN
173       model_string = '_O'
174       PRINT*, '    precursor ocean run'
175    ELSE
176       model_string = ''
177    ENDIF
178
179!
180!-- Do everything for each model
181    DO model = 1, models
182!
183!--    Set the model string used to identify the filenames
184       IF ( found  .OR.  vnest )  THEN
185          PRINT*, ''
186          IF ( model == 2 )  THEN
187             IF ( vnest )  THEN
188                model_string = '_N'
189                PRINT*, '    now combining FINE data'
190                PRINT*, '    ========================'
191             ELSE
192                model_string = '_O'
193                PRINT*, '    now combining ocean data'
194                PRINT*, '    ========================'
195             ENDIF
196          ELSE
197             IF ( vnest )  THEN
198                PRINT*, '    now combining COARSE data'
199                PRINT*, '    ============================='
200             ELSE
201                PRINT*, '    now combining atmosphere data'
202                PRINT*, '    ============================='
203             ENDIF
204          ENDIF
205       ELSEIF ( nest3d )  THEN
206          PRINT*, ''
207          PRINT*, '--> processing nest id = ', model
208          IF ( model == 1 )  THEN
209             model_string = ''
210          ELSE
211             WRITE( model_string, '(A2,I2.2)' )  '_N', model
212          ENDIF
213       ENDIF
214!
215!--    2D-arrays for ISO2D
216!--    Main loop for the three different cross-sections, starting with
217!--    xy-section
218       modus = 'XY'
219       PRINT*, ''
220       DO  WHILE ( modus == 'XY'  .OR.  modus == 'XZ'  .OR.  modus == 'YZ' )
221!
222!--       Take current time
223          CALL SYSTEM_CLOCK( count, count_rate )
224          cpu_start_time = REAL( count ) / REAL( count_rate )
225
226          netcdf_parallel = .FALSE.
227!
228!--       Check, if file from PE0 exists. If it does not exist, PALM did not
229!--       create any output for this cross-section.
230          danz = 0
231          WRITE (id_string,'(I6.6)')  danz
232          INQUIRE ( &
233               FILE='PLOT2D_'//modus//TRIM( model_string )//'_'//id_string, &
234               EXIST=found )
235!
236!--       Find out the number of files (equal to the number of PEs which
237!--       have been used in PALM) and open them
238          DO  WHILE ( found )
239
240             OPEN ( danz+110, &
241                  FILE='PLOT2D_'//modus//TRIM( model_string )//'_'//id_string, &
242                  FORM='UNFORMATTED' )
243             danz = danz + 1
244             WRITE (id_string,'(I6.6)')  danz
245             INQUIRE ( &
246                  FILE='PLOT2D_'//modus//TRIM( model_string )//'_'//id_string, &
247                  EXIST=found )
248
249          ENDDO
250
251!
252!--       Inquire whether a NetCDF file exists
253          INQUIRE( FILE='DATA_2D_'//modus//'_NETCDF'//TRIM( model_string ), &
254               EXIST=netcdf_0 )
255
256!
257!--       Inquire whether a NetCDF file for time-averaged data exists
258          INQUIRE( FILE='DATA_2D_'//modus//'_AV_NETCDF'//TRIM( model_string ),&
259               EXIST=netcdf_1 )
260
261          IF ( netcdf_0  .OR.  netcdf_1 )  THEN
262             netcdf_output = .TRUE.
263!
264!--          Inquire whether the NetCDF file is already complete (parallel
265!--          output)
266             INQUIRE( FILE='NO_COMBINE_PLOT_FIELDS_'//modus, &
267                      EXIST=netcdf_parallel )
268             IF ( netcdf_parallel )  THEN
269                netcdf_parallel = .TRUE.
270             ELSE
271                netcdf_parallel = .FALSE.
272             ENDIF
273          ELSE
274             netcdf_output = .FALSE.
275          ENDIF
276
277!
278!--       Info-output
279          PRINT*, ''
280#if defined( __netcdf )
281          IF ( netcdf_output )  THEN
282             IF ( netcdf_parallel )  THEN
283             PRINT*, '    NetCDF ' // modus // '-data are in one file ', &
284                          '(NetCDF4-format) - merging not neccessary'
285             ELSE
286                PRINT*, '    NetCDF output enabled'
287             ENDIF
288          ENDIF
289#else
290          IF ( netcdf_output )  THEN
291             PRINT*, '--- Sorry, no NetCDF support on this host'
292             netcdf_output = .FALSE.
293          ENDIF
294#endif
295          IF ( .NOT. netcdf_parallel )  THEN
296             IF ( danz /= 0 )  THEN
297                PRINT*, '    ',modus,'-section:  ', danz, ' file(s) found'
298             ELSE
299                PRINT*, '    no ', modus, '-section data available'
300             ENDIF
301          ENDIF
302
303          IF ( netcdf_output  .AND. .NOT. netcdf_parallel  .AND.  danz /= 0 ) &
304          THEN
305#if defined( __netcdf )
306             DO  av = 0, 1
307
308                IF ( av == 0  .AND.  .NOT.  netcdf_0 )  CYCLE
309                IF ( av == 1  .AND.  .NOT.  netcdf_1 )  CYCLE
310
311!
312!--             Open NetCDF dataset
313                IF ( av == 0 )  THEN
314                   filename = 'DATA_2D_'//modus//'_NETCDF' &
315                        //TRIM( model_string )
316                ELSE
317                   filename = 'DATA_2D_'//modus//'_AV_NETCDF' &
318                        //TRIM( model_string )
319                ENDIF
320                nc_stat = NF90_OPEN( filename, NF90_WRITE, id_set(av) )
321                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 1 )
322
323!
324!--             Get the list of variables (order of variables corresponds with
325!--             the order of data on the binary file)
326                var_list(av) = ' '    ! GET_ATT does not assign trailing blanks
327                nc_stat = NF90_GET_ATT( id_set(av), NF90_GLOBAL, 'VAR_LIST', &
328                     var_list(av) )
329                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 2 )
330
331!
332!--             Inquire id of the time coordinate variable
333                nc_stat = NF90_INQ_VARID( id_set(av), 'time', id_var_time(av) )
334                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 3 )
335
336!
337!--             Count number of variables; there is one more semicolon in the
338!--             string than variable names
339                num_var(av) = -1
340                DO  i = 1, LEN( var_list(av) )
341                   IF ( var_list(av)(i:i) == ';' )  num_var(av) = num_var(av) +1
342                ENDDO
343
344!
345!--             Extract the variable names from the list and inquire their
346!--             NetCDF IDs
347                pos = INDEX( var_list(av), ';' )
348!
349!--             Loop over all variables
350                DO  i = 1, num_var(av)
351
352!
353!--                Extract variable name from list
354                   var_list(av) = var_list(av)(pos+1:)
355                   pos = INDEX( var_list(av), ';' )
356                   var_name = var_list(av)(1:pos-1)
357
358!
359!--                Get variable ID from name
360                   nc_stat = NF90_INQ_VARID( id_set(av), TRIM( var_name ), &
361                        id_var(av,i) )
362                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 4 )
363
364!
365!--                Get number of x/y/z levels for that variable
366                   nc_stat = NF90_INQUIRE_VARIABLE( id_set(av), id_var(av,i), &
367                        dimids = id_dims_loc )
368                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 5 )
369                   id_dims(av,:) = id_dims_loc
370
371!
372!--                Inquire dimension ID
373                   DO  j = 1, 4
374                      nc_stat = NF90_INQUIRE_DIMENSION( id_set(av), &
375                           id_dims(av,j), dimname, levels(av,i) )
376                      IF ( nc_stat /= NF90_NOERR ) CALL handle_netcdf_error( 6 )
377
378                      IF ( modus == 'XY' .AND. INDEX(dimname, 'z') /= 0 )  EXIT
379                      IF ( modus == 'XZ' .AND. INDEX(dimname, 'y') /= 0 )  EXIT
380                      IF ( modus == 'YZ' .AND. INDEX(dimname, 'x') /= 0 )  EXIT
381                   ENDDO
382
383                ENDDO
384
385             ENDDO   ! av = 0, 1
386
387#endif
388          ENDIF
389
390!
391!--       Read the arrays, as long as the end of the file is reached
392          IF ( .NOT. netcdf_parallel )  THEN
393
394             fanz          =         0
395             current_level =         1
396             current_var   = 999999999
397
398             DO  WHILE ( danz /= 0 )
399
400!
401!--             Loop over all files (reading data of the subdomains)
402                DO  id = 0, danz-1
403!
404!--                File from PE0 contains special information at the beginning,
405!--                concerning the lower and upper indices of the total-domain
406!--                used in PALM (nxa, nxe, nya, nye).
407!--                Allocate necessary arrays, open the output file and write
408!--                the coordinate informations needed by ISO2D.
409                   IF ( id == 0  .AND.  fanz(0) == 0  .AND.  fanz(1) == 0 ) THEN
410
411                      READ ( id+110 )  nxa, nxe, nya, nye
412                      ALLOCATE ( pf(nxa:nxe,nya:nye) )
413!
414!--                   Set actual domain bounds to total domain
415                      ya_do = nya
416                      ye_do = nye
417
418                   ENDIF
419!
420!--                Read output time
421                   IF ( netcdf_output  .AND.  id == 0 )  THEN
422                      READ ( id+110, END=998 )  simulated_time, time_step, av
423                   ENDIF
424!
425!--                Read subdomain indices
426                   READ ( id+110, END=998 )  xa, xe, ya, ye, ya_tot, ye_tot
427
428!
429!--                IF the PE made no output (in case that no part of the
430!--                cross-section is situated on this PE), indices have the
431!--                value -1
432                   IF ( .NOT. ( xa == -1  .AND.  xe == -1  .AND. &
433                                ya == -1  .AND.  ye == -1 ) )  THEN
434
435
436!
437!--                   Read the subdomain grid-point values
438                      ALLOCATE( pf_tmp(xa:xe,ya:ye) )
439                      READ ( id+110 )  pf_tmp
440!
441!--                   Calculate indices on atmospheric grid (e.g. for soil model
442!--                   quantities)
443                      IF ( ya /= ya_tot .OR. ye /= ye_tot )  THEN
444                         ye_do = ye - ya
445                         ya_do = ya
446                         pf(xa:xe,ya_do:ye_do) = pf_tmp
447                      ELSE
448                         ye_do = nye
449                         ya_do = nya
450                         pf(xa:xe,ya:ye) = pf_tmp
451                      ENDIF
452
453                      DEALLOCATE( pf_tmp )
454                   ENDIF
455                   IF ( id == 0 )  fanz(av) = fanz(av) + 1
456
457                ENDDO
458!
459!--             Write data in NetCDF format
460                IF ( netcdf_output )  THEN
461#if defined( __netcdf )
462!
463!--                Check if a new time step has begun; if yes write data to
464!--                time axis
465                   IF ( current_var(av) > num_var(av) )  THEN
466                      current_var(av) = 1
467                      nc_stat = NF90_PUT_VAR( id_set(av), id_var_time(av), &
468                                              (/ simulated_time /),        &
469                                              start = (/ time_step /),     &
470                                              count = (/ 1 /) )
471                      IF ( nc_stat /= NF90_NOERR ) CALL handle_netcdf_error( 7 )
472                   ENDIF
473
474!
475!--                Now write the data; this is mode dependent
476                   SELECT CASE ( modus )
477
478                      CASE ( 'XY' )
479                         nc_stat = NF90_PUT_VAR( id_set(av),                   &
480                                           id_var(av,current_var(av)),         &
481                                           pf(nxa:nxe,nya:nye),                &
482                             start = (/ 1, 1, current_level(av), time_step /), &
483                                      count = (/ nxe-nxa+1, nye-nya+1, 1, 1 /) )
484                         IF ( nc_stat /= NF90_NOERR )  THEN
485                            CALL handle_netcdf_error( 8 )
486                         ENDIF
487                 
488                      CASE ( 'XZ' )
489                         nc_stat = NF90_PUT_VAR( id_set(av),                   &
490                                           id_var(av,current_var(av)),         &
491                                           pf(nxa:nxe,ya_do:ye_do),            &
492                             start = (/ 1, current_level(av), 1, time_step /), &
493                                      count = (/ nxe-nxa+1, 1, ye_do-ya_do+1, 1 /) )
494                         IF ( nc_stat /= NF90_NOERR )  THEN
495                            CALL handle_netcdf_error( 9 )
496                         ENDIF
497
498                      CASE ( 'YZ' )
499                         nc_stat = NF90_PUT_VAR( id_set(av),                   &
500                                           id_var(av,current_var(av)),         &
501                                           pf(nxa:nxe,ya_do:ye_do),            &
502                             start = (/ current_level(av), 1, 1, time_step /), &
503                                      count = (/ 1, nxe-nxa+1, ye_do-ya_do+1, 1 /) )
504                         IF ( nc_stat /= NF90_NOERR )  THEN
505                            CALL handle_netcdf_error( 10 )
506                         ENDIF
507
508                   END SELECT
509
510!
511!--                Data is written, check if max level is reached
512                   current_level(av) = current_level(av) + 1
513                   IF ( current_level(av) > levels(av,current_var(av)) )  THEN
514                      current_level(av) = 1
515                      current_var(av)   = current_var(av) + 1
516                   ENDIF
517
518#endif
519                ENDIF
520
521             ENDDO
522
523          ENDIF
524
525998       IF ( danz /= 0  .AND.  .NOT. netcdf_parallel )  THEN
526!
527!--          Print the number of the arrays processed
528             WRITE (*,'(16X,I4,A)')  fanz(0)+fanz(1), ' array(s) processed'
529             IF ( fanz(1) /= 0 )  THEN
530                WRITE (*,'(16X,I4,A)')  fanz(1), ' array(s) are time-averaged'
531             ENDIF
532
533!
534!--          Close all files and deallocate arrays
535             DO  id = 0, danz-1
536                CLOSE ( id+110 )
537             ENDDO
538             CLOSE ( 2 )
539             DEALLOCATE ( pf )
540
541!
542!--          Close the NetCDF file
543             IF ( netcdf_output )  THEN
544#if defined( __netcdf )
545                IF ( netcdf_0 )  THEN
546                   nc_stat = NF90_CLOSE( id_set(0) )
547                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 11 )
548                ENDIF
549                IF ( netcdf_1 )  THEN
550                   nc_stat = NF90_CLOSE( id_set(1) )
551                   IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 12 )
552                ENDIF
553#endif
554             ENDIF
555          ENDIF
556
557!
558!--       Output required cpu time
559          IF ( danz /= 0  .AND.  .NOT. netcdf_parallel )  THEN
560             CALL SYSTEM_CLOCK( count, count_rate )
561             cpu_end_time = REAL( count ) / REAL( count_rate )
562             WRITE (*,'(5X,A,F9.3,A)')  'Required cpu-time: ', &
563                                        cpu_end_time-cpu_start_time, ' sec'
564          ENDIF
565
566!
567!--       Choose the next cross-section
568          SELECT CASE ( modus )
569             CASE ( 'XY' )
570                modus = 'XZ'
571             CASE ( 'XZ' )
572                modus = 'YZ'
573             CASE ( 'YZ' )
574                modus = 'no'
575          END SELECT
576
577       ENDDO
578
579
580!
581!--    Combine the 3D-arrays
582       netcdf_parallel = .FALSE.
583
584!
585!--    Info-output
586       PRINT*, ' '
587
588!
589!--    Take current time
590       CALL SYSTEM_CLOCK( count, count_rate )
591       cpu_start_time = REAL( count ) / REAL( count_rate )
592
593!
594!--    Inquire whether a NetCDF file exists
595       INQUIRE( FILE='DATA_3D_NETCDF'//TRIM( model_string ), EXIST=netcdf_0 )
596
597!
598!--    Inquire whether a NetCDF file for time-averaged data exists
599       INQUIRE( FILE='DATA_3D_AV_NETCDF'//TRIM( model_string ), EXIST=netcdf_1 )
600
601       IF ( netcdf_0  .OR.  netcdf_1 )  THEN
602          netcdf_output = .TRUE.
603!
604!--       Inquire whether the NetCDF file is already complete (parallel output)
605          INQUIRE( FILE='NO_COMBINE_PLOT_FIELDS_3D', EXIST=netcdf_parallel )
606          IF ( netcdf_parallel )  THEN
607             netcdf_parallel = .TRUE.
608          ELSE
609             netcdf_parallel = .FALSE.
610          ENDIF
611       ELSE
612          netcdf_output = .FALSE.
613       ENDIF
614
615!
616!--    Check, if file from PE0 exists; not neccessary in case of parallel
617!--    PALM output
618       IF ( .NOT. netcdf_parallel )  THEN
619          danz = 0
620          WRITE (id_string,'(I6.6)')  danz
621          INQUIRE ( &
622               FILE='PLOT3D_DATA'//TRIM( model_string )//'_'//TRIM( id_string ),  &
623               EXIST=found )
624       ELSE
625          found = .FALSE.
626       ENDIF
627
628!
629!--    Find out the number of files and open them
630       DO  WHILE ( found )
631
632          OPEN ( danz+110, &
633               FILE='PLOT3D_DATA'//TRIM( model_string )//'_'//TRIM(id_string), &
634               FORM='UNFORMATTED')
635          danz = danz + 1
636          WRITE (id_string,'(I6.6)')  danz
637          INQUIRE ( &
638               FILE='PLOT3D_DATA'//TRIM( model_string )//'_'//TRIM(id_string), &
639               EXIST=found )
640
641       ENDDO
642
643#if defined( __netcdf )
644       IF ( netcdf_output )  THEN
645          IF ( netcdf_parallel )  THEN
646             PRINT*, '    NetCDF data are in one file (NetCDF4-format)', &
647                          ' - merging not neccessary'
648          ELSE
649             PRINT*, '    NetCDF output enabled'
650          ENDIF
651       ENDIF
652#else
653       IF ( netcdf_output )  THEN
654          PRINT*, '--- Sorry, no NetCDF support on this host'
655          netcdf_output = .FALSE.
656       ENDIF
657#endif
658       IF ( .NOT. netcdf_parallel )  THEN
659          IF ( danz /= 0 )  THEN
660             PRINT*, '    3D-data:     ', danz, ' file(s) found'
661          ELSE
662             PRINT*, '    no 3D-data file available'
663          ENDIF
664       ENDIF
665
666       IF ( netcdf_output  .AND. .NOT. netcdf_parallel  .AND.  danz /= 0 )  THEN
667#if defined( __netcdf )
668          DO  av = 0, 1
669
670             IF ( av == 0  .AND.  .NOT.  netcdf_0 )  CYCLE
671             IF ( av == 1  .AND.  .NOT.  netcdf_1 )  CYCLE
672
673!
674!--          Open NetCDF dataset
675             IF ( av == 0 )  THEN
676                filename = 'DATA_3D_NETCDF'//TRIM( model_string )
677             ELSE
678                filename = 'DATA_3D_AV_NETCDF'//TRIM( model_string )
679             ENDIF
680             nc_stat = NF90_OPEN( filename, NF90_WRITE, id_set(av) )
681             IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 13 )
682
683
684!
685!--          Get the list of variables (order of variables corresponds with the
686!--          order of data on the binary file)
687             var_list(av) = ' '    ! GET_ATT does not assign trailing blanks
688             nc_stat = NF90_GET_ATT( id_set(av), NF90_GLOBAL, 'VAR_LIST', &
689                  var_list(av) )
690             IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 14 )
691
692!
693!--          Inquire id of the time coordinate variable
694             nc_stat = NF90_INQ_VARID( id_set(av), 'time', id_var_time(av) )
695             IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 15 )
696
697!
698!--          Count number of variables; there is one more semicolon in the
699!--          string than variable names
700             num_var(av) = -1
701             DO  i = 1, LEN( var_list(av) )
702                IF ( var_list(av)(i:i) == ';' )  num_var(av) = num_var(av) + 1
703             ENDDO
704
705!
706!--          Extract the variable names from the list and inquire their NetCDF
707!--          IDs
708             pos = INDEX( var_list(av), ';' )
709!
710!--          Loop over all variables
711             DO  i = 1, num_var(av)
712
713!
714!--             Extract variable name from list
715                var_list(av) = var_list(av)(pos+1:)
716                pos = INDEX( var_list(av), ';' )
717                var_name = var_list(av)(1:pos-1)
718
719!
720!--             Get variable ID from name
721                nc_stat = NF90_INQ_VARID( id_set(av), TRIM( var_name ), &
722                     id_var(av,i) )
723                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 16 )
724
725             ENDDO
726
727          ENDDO    ! av=0,1
728
729#endif
730       ENDIF
731
732!
733!--    Read arrays, until the end of the file is reached
734       IF ( .NOT. netcdf_parallel )  THEN
735
736          current_var = 999999999
737          fanz = 0
738          DO  WHILE ( danz /= 0 )
739
740!
741!--          Loop over all files
742             DO  id = 0, danz-1
743!
744!--             File from PE0 contains at the beginning the index bounds
745!--             of PALM's total domain.
746!--             Allocate the array for storing the total domain data
747                IF ( id == 0  .AND.  fanz(0) == 0  .AND.  fanz(1) == 0 )  THEN
748!                   READ ( id+110 )  nxag, nxeg, nyag, nyeg
749                   READ ( id+110 )  nxa, nxe, nya, nye, nza, nze
750                   ALLOCATE ( pf3d(nxa:nxe,nya:nye,nza:nze) )
751                ENDIF
752
753!
754!--             Read output time
755                IF ( netcdf_output  .AND.  id == 0 )  THEN
756                   IF ( netcdf_1 )  THEN
757                      READ ( id+110, END=999 )  simulated_time, time_step, av
758                   ELSE
759!
760!--                   For compatibility with earlier PALM versions
761                      READ ( id+110, END=999 )  simulated_time, time_step
762                      av = 0
763                   ENDIF
764                ENDIF
765
766!
767!--             Read subdomain indices and grid point values
768                READ ( id+110, END=999 )  xa, xe, ya, ye, za, ze
769                ALLOCATE( pf3d_tmp(xa:xe,ya:ye,za:ze) )
770                READ ( id+110 )  pf3d_tmp
771
772                xxa = MAX( nxa, xa )
773                xxe = MIN( nxe, xe )
774                yya = MAX( nya, ya )
775                yye = MIN( nye, ye )
776                DO  k = za, ze
777                   DO  j = yya, yye
778                      DO  i = xxa, xxe
779                         pf3d(i,j,k) = pf3d_tmp(i,j,k)
780                      ENDDO
781                   ENDDO
782                ENDDO
783
784                DEALLOCATE( pf3d_tmp )
785                IF ( id == 0 )  fanz(av) = fanz(av) + 1
786
787             ENDDO
788
789!
790!--          Write data of the total domain in NetCDF format
791             IF ( netcdf_output )  THEN
792#if defined( __netcdf )
793!
794!--             Check if a new time step has begun; if yes write data to time
795!--             axis
796                IF ( current_var(av) > num_var(av) )  THEN
797                   current_var(av) = 1
798                   nc_stat = NF90_PUT_VAR( id_set(av), id_var_time(av), &
799                                      (/ simulated_time /),&
800                                      start = (/ time_step /), count = (/ 1 /) )
801                   IF ( nc_stat /= NF90_NOERR ) CALL handle_netcdf_error( 17 )
802                ENDIF
803
804!
805!--             Now write the data
806                nc_stat = NF90_PUT_VAR( id_set(av), id_var(av,current_var(av)),&
807                                        pf3d(nxa:nxe,nya:nye,za:ze), start = (/ 1, 1, 1, time_step /),&
808                              count = (/ nxe-nxa+1, nye-nya+1, ze-za+1, 1 /) )
809                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 18 )
810
811                current_var(av) = current_var(av) + 1
812
813#endif
814             ENDIF
815
816          ENDDO
817
818       ENDIF
819
820999    IF ( danz /= 0  .AND.  .NOT. netcdf_parallel )  THEN
821!
822!--       Print the number of arrays processed
823          WRITE (*,'(16X,I4,A)')  fanz(0)+fanz(1), ' array(s) processed'
824          IF ( fanz(1) /= 0 )  THEN
825             WRITE (*,'(16X,I4,A)')  fanz(1), ' array(s) are time-averaged'
826          ENDIF
827!
828!--       Close all files and deallocate array
829          DO  id = 0, danz-1
830             CLOSE ( id+110 )
831          ENDDO
832          CLOSE ( 2 )
833          DEALLOCATE ( pf3d )
834!
835!--       Close the NetCDF file
836          IF ( netcdf_output )  THEN
837#if defined( __netcdf )
838             IF ( netcdf_0 )  THEN
839                nc_stat = NF90_CLOSE( id_set(0) )
840                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 19 )
841             ENDIF
842             IF ( netcdf_1 )  THEN
843                nc_stat = NF90_CLOSE( id_set(1) )
844                IF ( nc_stat /= NF90_NOERR )  CALL handle_netcdf_error( 20 )
845             ENDIF
846#endif
847          ENDIF
848
849!
850!--       Output required cpu time
851          CALL SYSTEM_CLOCK( count, count_rate )
852          cpu_end_time = REAL( count ) / REAL( count_rate )
853          WRITE (*,'(5X,A,F9.3,A)')  'Required cpu-time: ', &
854                                     cpu_end_time-cpu_start_time, ' sec'
855
856       ENDIF
857
858    ENDDO  ! models
859
860
861 CONTAINS
862
863
864    SUBROUTINE handle_netcdf_error( errno )
865!
866!--    Prints out a text message corresponding to the current NetCDF status
867
868       IMPLICIT NONE
869
870       INTEGER, INTENT(IN) ::  errno
871
872#if defined( __netcdf )
873       IF ( nc_stat /= NF90_NOERR )  THEN
874          PRINT*, '+++ combine_plot_fields  netcdf: ', av, errno, &
875                  TRIM( nf90_strerror( nc_stat ) )
876       ENDIF
877#endif
878
879    END SUBROUTINE handle_netcdf_error
880
881
882 END PROGRAM combine_plot_fields
883
884
885
Note: See TracBrowser for help on using the repository browser.