source: palm/trunk/SOURCE/init_masks.f90 @ 3620

Last change on this file since 3620 was 3589, checked in by suehring, 5 years ago

Remove erroneous UTF encoding; last commit documented

  • Property svn:keywords set to Id
File size: 34.9 KB
RevLine 
[1682]1!> @file init_masks.f90
[2000]2!------------------------------------------------------------------------------!
[2696]3! This file is part of the PALM model system.
[1036]4!
[2000]5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
[1036]9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
[2718]17! Copyright 1997-2018 Leibniz Universitaet Hannover
[2000]18!------------------------------------------------------------------------------!
[1036]19!
[484]20! Current revisions:
[298]21! -----------------
[1805]22!
[3589]23!
[1321]24! Former revisions:
25! -----------------
26! $Id: init_masks.f90 3589 2018-11-30 15:09:51Z moh.hefny $
[3589]27! Move the control parameter "salsa" from salsa_mod to control_parameters
28! (M. Kurppa)
29!
30! 3582 2018-11-29 19:16:36Z suehring
[3554]31! add variable description
32!
33! 3512 2018-11-09 18:09:51Z gronemeier
[3512]34! Bugfix: do not output ghost points if mask_x/y_loop is not specified
35!
36! 3467 2018-10-30 19:05:21Z suehring
[3467]37! Implementation of a new aerosol module salsa.
38!
39! 3435 2018-10-26 18:25:44Z gronemeier
[3435]40! Add checks for chemistry and radiation model
41! Set indices for terrain-following masked output
42!
43! 3421 2018-10-24 18:39:32Z gronemeier
[3421]44! Renamed output variables
45!
46! 3419 2018-10-24 17:27:31Z gronemeier
[3294]47! ocean renamed ocean_mode
48!
49! 3274 2018-09-24 15:42:55Z knoop
[3274]50! Modularization of all bulk cloud physics code components
51!
52! 3065 2018-06-12 07:03:02Z Giersch
[3065]53! dz_stretch_level was replaced by dz_stretch_level_start
54!
55! 3049 2018-05-29 13:52:36Z Giersch
[3045]56! Error messages revised
57!
[3049]58! 3045 2018-05-28 07:55:41Z Giersch
59! Error messages revised
60!
[3045]61! 2718 2018-01-02 08:49:38Z maronga
[2716]62! Corrected "Former revisions" section
63!
64! 2696 2017-12-14 17:12:51Z kanani
65! Change in file header (GPL part)
66!
67! 2344 2017-08-09 11:00:34Z raasch
[2344]68! explicit setting of initial values for array domask required due to a bug
69! in the Cray compiler (appears only if option -eD is used)
70!
71! 2301 2017-06-29 16:41:21Z gronemeier
[2301]72! Bugfix: set variable name length to global value
73!
74!
[2292]75! Implementation of new microphysic scheme: cloud_scheme = 'morrison'
76! includes two more prognostic equations for cloud drop concentration (nc) 
77! and cloud water content (qc).
78!
79! 2271 2017-06-09 12:34:55Z sward
[2271]80! Changed error message
81!
82! 2101 2017-01-05 16:42:31Z suehring
[1321]83!
[2032]84! 2031 2016-10-21 15:11:58Z knoop
85! renamed variable rho to rho_ocean
86!
[2001]87! 2000 2016-08-20 18:09:15Z knoop
88! Forced header and separation lines into 80 columns
89!
[1823]90! 1822 2016-04-07 07:49:42Z hoffmann
91! icloud_scheme replaced by microphysics_*
92!
[1805]93! 1804 2016-04-05 16:30:18Z maronga
94! Removed code for parameter file check (__check)
95!
[1784]96! 1783 2016-03-06 18:36:17Z raasch
97! netcdf module name changed + related changes
98!
[1683]99! 1682 2015-10-07 23:56:08Z knoop
100! Code annotations made doxygen readable
101!
[1439]102! 1438 2014-07-22 14:14:06Z heinze
103! +nr, qc, qr
104!
[1415]105! 1414 2014-05-31 11:19:48Z gronemeier
106! Bugfix: first and last grid points as they appear in 3D volume data can now
107! be included to masked data output
108!
[1354]109! 1353 2014-04-08 15:21:23Z heinze
110! REAL constants provided with KIND-attribute
111!
[1325]112! 1324 2014-03-21 09:13:16Z suehring
113! Bugfix: ONLY statement for module netcdf_control removed
114!
[1321]115! 1320 2014-03-20 08:40:49Z raasch
[1320]116! ONLY-attribute added to USE-statements,
117! kind-parameters added to all INTEGER and REAL declaration statements,
118! kinds are defined in new module kinds,
119! revision history before 2012 removed,
120! comment fields (!:) to be used for variable explanations added to
121! all variable declaration statements
[298]122!
[1187]123! 1186 2013-06-18 06:22:52Z raasch
124! bugfix: 0.0 replaced by zu(nzb) as the lowest default height level for masks,
125! because a zero value does not work in case of ocean runs
126!
[1037]127! 1036 2012-10-22 13:43:42Z raasch
128! code put under GPL (PALM 3.9)
129!
[1033]130! 1032 2012-10-21 13:03:21Z letzel
131! mask locations determined based on scalar positions
132!
[1035]133! 1031 2012-10-19 14:35:30Z raasch
134! netCDF4 without parallel file support implemented
135!
[997]136! 996 2012-09-07 10:41:47Z raasch
137! little reformatting
138!
[979]139! 978 2012-08-09 08:28:32Z fricke
140! +z0h*
141!
[810]142! 809 2012-01-30 13:32:58Z maronga
143! Bugfix: replaced .AND. and .NOT. with && and ! in the preprocessor directives
144!
[808]145! 807 2012-01-25 11:53:51Z maronga
146! New cpp directive "__check" implemented which is used by check_namelist_files
147!
[449]148! 410 2009-12-04 17:05:40Z letzel
149! Initial revision
[298]150!
[449]151!
[298]152! Description:
153! ------------
[1682]154!> Initialize masked data output
[298]155!------------------------------------------------------------------------------!
[1682]156 SUBROUTINE init_masks
[298]157
[1320]158    USE arrays_3d,                                                             &
159        ONLY:  zu, zw
160
[3435]161    USE bulk_cloud_model_mod,                                                  &
162        ONLY: bulk_cloud_model, microphysics_morrison, microphysics_seifert
163
164    USE chemistry_model_mod,                                                   &
165        ONLY:  chem_check_data_output
166
[1320]167    USE control_parameters,                                                    &
[3435]168        ONLY:  air_chemistry,                                                  &
169               constant_diffusion, cloud_droplets,                             &
[1320]170               data_output_masks, data_output_masks_user,                      &
[3065]171               doav, doav_n, domask, domask_no, dz, dz_stretch_level_start,    &
172               humidity, mask, masks, mask_scale, mask_i,                      &
[1320]173               mask_i_global, mask_j, mask_j_global, mask_k, mask_k_global,    &
[3435]174               mask_k_over_surface,                                            &
175               mask_loop, mask_size, mask_size_l, mask_start_l,                &
176               mask_surface, mask_x,                                           &
[1320]177               mask_x_loop, mask_xyz_dimension, mask_y, mask_y_loop, mask_z,   &
178               mask_z_loop, max_masks,  message_string, mid,                   &
[3582]179               passive_scalar, ocean_mode, salsa, varnamelength
[1320]180
181    USE grid_variables,                                                        &
182        ONLY:  dx, dy
183
184    USE indices,                                                               &
185        ONLY:  nx, nxl, nxr, ny, nyn, nys, nz, nzb, nzt
186
187    USE kinds
188
[1783]189    USE netcdf_interface,                                                      &
190        ONLY:  domask_unit, netcdf_data_format
[1320]191
192    USE particle_attributes,                                                   &
193        ONLY:  particle_advection
194
[3467]195    USE pegrid 
[3582]196   
[3435]197    USE radiation_model_mod,                                                   &
198        ONLY:  radiation, radiation_check_data_output
[3467]199       
200    USE salsa_mod,                                                             &
[3582]201        ONLY:  salsa_check_data_output 
[3435]202
[298]203    IMPLICIT NONE
204
[3554]205    CHARACTER (LEN=varnamelength) ::  var  !< contains variable name
206    CHARACTER (LEN=7)             ::  unit !< contains unit of variable
[1320]207   
[3554]208    CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  do_mask      !< list of output variables
209    CHARACTER (LEN=varnamelength), DIMENSION(max_masks,100) ::  do_mask_user !< list of user-specified output variables
[298]210
[3435]211    INTEGER(iwp) ::  count        !< counting masking indices along a dimension
[3554]212    INTEGER(iwp) ::  i            !< loop index
213    INTEGER(iwp) ::  ilen         !< length of string saved in 'do_mask'
214    INTEGER(iwp) ::  ind(6)       !< index limits (lower/upper bounds) of output array
215    INTEGER(iwp) ::  ind_array(1) !< array index
216    INTEGER(iwp) ::  j            !< loop index
217    INTEGER(iwp) ::  k            !< loop index
[3435]218    INTEGER(iwp) ::  m            !< mask index
[3554]219    INTEGER(iwp) ::  n            !< loop index
220    INTEGER(iwp) ::  sender       !< PE id of sending PE
[1320]221   
[3554]222    INTEGER(iwp), DIMENSION(:), ALLOCATABLE ::  tmp_array !< temporary 1D array
[298]223
[3554]224    LOGICAL ::  found !< true if variable is found
[2344]225
[298]226!
[2344]227!-- Initial values are explicitly set here due to a bug in the Cray compiler
228!-- in case of assignments of initial values in declaration statements for
229!-- arrays with more than 9999 elements (appears with -eD only)
230    domask = ' '
231
232!
[298]233!-- Allocation and initialization
[1414]234    ALLOCATE( tmp_array( MAX(nx,ny,nz)+2 ) )
[298]235
[1414]236    ALLOCATE( mask_i(max_masks,nxr-nxl+2), &
237              mask_j(max_masks,nyn-nys+2), &
238              mask_k(max_masks,nzt-nzb+2) )
[298]239!
240!-- internal mask arrays ("mask,dimension,selection")
241    ALLOCATE( mask(max_masks,3,mask_xyz_dimension), &
242              mask_loop(max_masks,3,3) )
[493]243   
[298]244!
[3045]245!-- Parallel mask output not yet supported. In check_parameters data format
246!-- is restricted and is switched back to non-parallel output. Therefore the
247!-- following error can not occur at the moment.
[1031]248    IF ( netcdf_data_format > 4 )  THEN
[3045]249       message_string = 'netCDF file formats '//                               &
250                        '5 and 6 (with parallel I/O support)'//                &
251                        ' are currently not supported.'
[564]252       CALL message( 'init_masks', 'PA0328', 1, 2, 0, 6, 0 )
[298]253    ENDIF
[553]254
[298]255!
256!-- Store data output parameters for masked data output in few shared arrays
[996]257    DO  mid = 1, masks
[564]258   
[553]259       do_mask     (mid,:) = data_output_masks(mid,:)
260       do_mask_user(mid,:) = data_output_masks_user(mid,:)
261       mask      (mid,1,:) = mask_x(mid,:) 
262       mask      (mid,2,:) = mask_y(mid,:)
263       mask      (mid,3,:) = mask_z(mid,:) 
[3435]264!
265!--    Flag a mask as terrain following
266       IF ( mask_k_over_surface(mid,1) /= -1_iwp )  THEN
267          mask_surface(mid) = .TRUE.
268       ENDIF
269
[1353]270       IF ( mask_x_loop(mid,1) == -1.0_wp  .AND.  mask_x_loop(mid,2) == -1.0_wp&
271            .AND.  mask_x_loop(mid,3) == -1.0_wp )  THEN
272          mask_loop(mid,1,1:2) = -1.0_wp
273          mask_loop(mid,1,3)   =  0.0_wp
[564]274       ELSE
275          mask_loop(mid,1,:) = mask_x_loop(mid,:)
276       ENDIF
[1353]277       IF ( mask_y_loop(mid,1) == -1.0_wp  .AND.  mask_y_loop(mid,2) == -1.0_wp&
278            .AND.  mask_y_loop(mid,3) == -1.0_wp )  THEN
279          mask_loop(mid,2,1:2) = -1.0_wp
280          mask_loop(mid,2,3)   =  0.0_wp
[564]281       ELSE
282          mask_loop(mid,2,:) = mask_y_loop(mid,:)
283       ENDIF
[1353]284       IF ( mask_z_loop(mid,1) == -1.0_wp  .AND.  mask_z_loop(mid,2) == -1.0_wp&
285            .AND.  mask_z_loop(mid,3) == -1.0_wp )  THEN
286          mask_loop(mid,3,1:2) = -1.0_wp
287          mask_loop(mid,3,3)   =  0.0_wp
[564]288       ELSE
289          mask_loop(mid,3,:) = mask_z_loop(mid,:)
290       ENDIF
291       
[553]292    ENDDO
293   
[298]294    mask_i = -1; mask_j = -1; mask_k = -1
[553]295   
[298]296!
297!-- Global arrays are required by define_netcdf_header.
[1031]298    IF ( myid == 0  .OR.  netcdf_data_format > 4 )  THEN
[1414]299       ALLOCATE( mask_i_global(max_masks,nx+2), &
300                 mask_j_global(max_masks,ny+2), &
301                 mask_k_global(max_masks,nz+2) )
[298]302       mask_i_global = -1; mask_j_global = -1; mask_k_global = -1
303    ENDIF
304
305!
306!-- Determine variable names for each mask
307    DO  mid = 1, masks
308!
309!--    Append user-defined data output variables to the standard data output
310       IF ( do_mask_user(mid,1) /= ' ' )  THEN
311          i = 1
312          DO  WHILE ( do_mask(mid,i) /= ' '  .AND.  i <= 100 )
313             i = i + 1
314          ENDDO
315          j = 1
316          DO  WHILE ( do_mask_user(mid,j) /= ' '  .AND.  j <= 100 )
317             IF ( i > 100 )  THEN
[3045]318                WRITE ( message_string, * ) 'number of output quantitities ',  &
319                     'given by data_output_mask and data_output_mask_user ',   &
[298]320                     'exceeds the limit of 100'
[564]321                CALL message( 'init_masks', 'PA0329', 1, 2, 0, 6, 0 )
[298]322             ENDIF
323             do_mask(mid,i) = do_mask_user(mid,j)
324             i = i + 1
325             j = j + 1
326          ENDDO
327       ENDIF
328
329!
330!--    Check and set steering parameters for mask data output and averaging
331       i   = 1
[996]332       DO WHILE ( do_mask(mid,i) /= ' '  .AND.  i <= 100 )
[298]333!
334!--       Check for data averaging
335          ilen = LEN_TRIM( do_mask(mid,i) )
336          j = 0                                              ! no data averaging
337          IF ( ilen > 3 )  THEN
338             IF ( do_mask(mid,i)(ilen-2:ilen) == '_av' )  THEN
339                j = 1                                           ! data averaging
340                do_mask(mid,i) = do_mask(mid,i)(1:ilen-3)
341             ENDIF
342          ENDIF
343          var = TRIM( do_mask(mid,i) )
344!
345!--       Check for allowed value and set units
346          SELECT CASE ( TRIM( var ) )
347
348             CASE ( 'e' )
349                IF ( constant_diffusion )  THEN
[3045]350                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[298]351                        '" requires constant_diffusion = .FALSE.'
[564]352                   CALL message( 'init_masks', 'PA0103', 1, 2, 0, 6, 0 )
[298]353                ENDIF
354                unit = 'm2/s2'
355
[3421]356             CASE ( 'thetal' )
[3274]357                IF ( .NOT. bulk_cloud_model )  THEN
[3045]358                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3274]359                        '" requires bulk_cloud_model = .TRUE.'
[773]360                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
[771]361                ENDIF
362                unit = 'K'
363
[2292]364             CASE ( 'nc' )
[3274]365                IF ( .NOT. bulk_cloud_model )  THEN
[3045]366                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3274]367                        '" requires bulk_cloud_model = .TRUE.'
[2292]368                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
369                 ELSEIF ( .NOT. microphysics_morrison ) THEN
[3045]370                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
371                         'requires  = morrison'
[2292]372                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
373                ENDIF
374                unit = '1/m3'
375
[1438]376             CASE ( 'nr' )
[3274]377                IF ( .NOT. bulk_cloud_model )  THEN
[3045]378                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3274]379                        '" requires bulk_cloud_model = .TRUE.'
[1438]380                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
[1822]381                 ELSEIF ( .NOT. microphysics_seifert ) THEN
[3045]382                   message_string = 'output of "' // TRIM( var ) // '"' //     &
383                         'requires cloud_scheme = seifert_beheng'
[1438]384                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
385                ENDIF
386                unit = '1/m3'
387
[298]388             CASE ( 'pc', 'pr' )
389                IF ( .NOT. particle_advection )  THEN
[3045]390                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
391                        '" requires a "particles_par"-NAMELIST in the ',       &
[298]392                        'parameter file (PARIN)'
[564]393                   CALL message( 'init_masks', 'PA0104', 1, 2, 0, 6, 0 )
[298]394                ENDIF
395                IF ( TRIM( var ) == 'pc' )  unit = 'number'
396                IF ( TRIM( var ) == 'pr' )  unit = 'm'
397
[3421]398             CASE ( 'q', 'thetav' )
[298]399                IF ( .NOT. humidity )  THEN
[3045]400                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[298]401                        '" requires humidity = .TRUE.'
[564]402                   CALL message( 'init_masks', 'PA0105', 1, 2, 0, 6, 0 )
[298]403                ENDIF
404                IF ( TRIM( var ) == 'q'   )  unit = 'kg/kg'
[3421]405                IF ( TRIM( var ) == 'thetav' )  unit = 'K'
[298]406
[1438]407             CASE ( 'qc' )
[3274]408                IF ( .NOT. bulk_cloud_model )  THEN
[3045]409                   message_string = 'output of "' // TRIM( var ) // '"' //     &
[3274]410                            'requires bulk_cloud_model = .TRUE.'
[1438]411                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
412                ENDIF
413                unit = 'kg/kg'
414
[298]415             CASE ( 'ql' )
[3274]416                IF ( .NOT. ( bulk_cloud_model  .OR.  cloud_droplets ) )  THEN
[3045]417                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3274]418                        '" requires bulk_cloud_model = .TRUE. or ',            &
419                        'cloud_droplets = .TRUE.'
[3045]420                   CALL message( 'init_masks', 'PA0106', 1, 2, 0, 6, 0 )
[298]421                ENDIF
422                unit = 'kg/kg'
423
424             CASE ( 'ql_c', 'ql_v', 'ql_vp' )
425                IF ( .NOT. cloud_droplets )  THEN
[3045]426                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[298]427                        '" requires cloud_droplets = .TRUE.'
[564]428                   CALL message( 'init_masks', 'PA0107', 1, 2, 0, 6, 0 )
[298]429                ENDIF
430                IF ( TRIM( var ) == 'ql_c'  )  unit = 'kg/kg'
431                IF ( TRIM( var ) == 'ql_v'  )  unit = 'm3'
432                IF ( TRIM( var ) == 'ql_vp' )  unit = 'none'
433
434             CASE ( 'qv' )
[3274]435                IF ( .NOT. bulk_cloud_model )  THEN
[3045]436                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3274]437                        '" requires bulk_cloud_model = .TRUE.'
[564]438                   CALL message( 'init_masks', 'PA0108', 1, 2, 0, 6, 0 )
[298]439                ENDIF
440                unit = 'kg/kg'
441
[1438]442             CASE ( 'qr' )
[3274]443                IF ( .NOT. bulk_cloud_model )  THEN
[3045]444                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
[3274]445                            'requires bulk_cloud_model = .TRUE.'
[1438]446                   CALL message( 'check_parameters', 'PA0108', 1, 2, 0, 6, 0 )
[1822]447                ELSEIF ( .NOT. microphysics_seifert ) THEN
[3045]448                   message_string = 'output of "' // TRIM( var ) // '" ' //    &
449                            'requires cloud_scheme = seifert_beheng'
[1438]450                   CALL message( 'check_parameters', 'PA0359', 1, 2, 0, 6, 0 )
451                ENDIF
452                unit = 'kg/kg'
453
[3421]454             CASE ( 'rho_sea_water' )
[3294]455                IF ( .NOT. ocean_mode )  THEN
[3045]456                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3294]457                        '" requires ocean mode'
[564]458                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
[298]459                ENDIF
460                unit = 'kg/m3'
461
462             CASE ( 's' )
463                IF ( .NOT. passive_scalar )  THEN
[3045]464                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[298]465                        '" requires passive_scalar = .TRUE.'
[564]466                   CALL message( 'init_masks', 'PA0110', 1, 2, 0, 6, 0 )
[298]467                ENDIF
468                unit = 'conc'
469
470             CASE ( 'sa' )
[3294]471                IF ( .NOT. ocean_mode )  THEN
[3045]472                   WRITE ( message_string, * ) 'output of "', TRIM( var ),     &
[3294]473                        '" requires ocean mode'
[564]474                   CALL message( 'init_masks', 'PA0109', 1, 2, 0, 6, 0 )
[298]475                ENDIF
476                unit = 'psu'
477
[3421]478             CASE ( 'us*', 't*', 'lwp*', 'pra*', 'prr*', 'z0*', 'z0h*' )
[3045]479                WRITE ( message_string, * ) 'illegal value for data_',         &
480                     'output: "', TRIM( var ), '" is only allowed',            &
[298]481                     'for horizontal cross section'
[564]482                CALL message( 'init_masks', 'PA0111', 1, 2, 0, 6, 0 )
[298]483
[3421]484             CASE ( 'p', 'theta', 'u', 'v', 'w' )
[298]485                IF ( TRIM( var ) == 'p'  )  unit = 'Pa'
[3421]486                IF ( TRIM( var ) == 'theta' )  unit = 'K'
[298]487                IF ( TRIM( var ) == 'u'  )  unit = 'm/s'
488                IF ( TRIM( var ) == 'v'  )  unit = 'm/s'
489                IF ( TRIM( var ) == 'w'  )  unit = 'm/s'
490                CONTINUE
491
492             CASE DEFAULT
[3435]493             
[298]494                CALL user_check_data_output( var, unit )
[3467]495                           
496                IF ( salsa )  THEN
497                   CALL salsa_check_data_output( var, unit )
498                ENDIF               
[298]499
[3435]500                IF ( unit == 'illegal'  .AND.  air_chemistry                   &
501                     .AND.  (var(1:3) == 'kc_' .OR. var(1:3) == 'em_') )  THEN
502                   CALL chem_check_data_output( var, unit, 0, 0, 0 )
503                ENDIF
504
[298]505                IF ( unit == 'illegal' )  THEN
[3435]506                   CALL radiation_check_data_output( var, unit, 0, 0, 0 )
507                ENDIF
508
509                IF ( unit == 'illegal' )  THEN
[298]510                   IF ( do_mask_user(mid,1) /= ' ' )  THEN
[3045]511                      WRITE ( message_string, * ) 'illegal value for data_',   &
512                           'output_masks or data_output_masks_user: "',        &
[298]513                           TRIM( do_mask(mid,i) ), '"'
[3045]514                      CALL message( 'init_masks', 'PA0018', 1, 2, 0, 6, 0 )
[298]515                   ELSE
[3045]516                      WRITE ( message_string, * ) 'illegal value for data_',   &
517                           ' output_masks : "', TRIM( do_mask(mid,i) ), '"'
[564]518                      CALL message( 'init_masks', 'PA0330', 1, 2, 0, 6, 0 )
[298]519                   ENDIF
520                ENDIF
521
522          END SELECT
523!
524!--       Set the internal steering parameters appropriately
525          domask_no(mid,j)                    = domask_no(mid,j) + 1
526          domask(mid,j,domask_no(mid,j))      = do_mask(mid,i)
527          domask_unit(mid,j,domask_no(mid,j)) = unit
528
529          IF ( j == 1 )  THEN
530!
531!--          Check, if variable is already subject to averaging
532             found = .FALSE.
533             DO  k = 1, doav_n
534                IF ( TRIM( doav(k) ) == TRIM( var ) )  found = .TRUE.
535             ENDDO
536
537             IF ( .NOT. found )  THEN
538                doav_n = doav_n + 1
539                doav(doav_n) = var
540             ENDIF
541          ENDIF
542
543          i = i + 1
544
545       ENDDO   ! do_mask(mid,i)
546    ENDDO   ! mid
547
548
549!
550!-- Determine mask locations for each mask
551    DO  mid = 1, masks
552!
553!--    Set local masks for each subdomain along all three dimensions
554       CALL set_mask_locations( 1, dx, 'dx', nx, 'nx', nxl, nxr )
555       CALL set_mask_locations( 2, dy, 'dy', ny, 'ny', nys, nyn )
[3435]556       IF ( .NOT. mask_surface(mid) )  THEN
557          CALL set_mask_locations( 3, dz(1), 'dz', nz, 'nz', nzb, nzt )
558       ELSE
[298]559!
[3435]560!--       Set vertical mask locations and size in case of terrain-following
561!--       output
562          count = 0
563          DO  WHILE ( mask_k_over_surface(mid, count+1) >= 0 )
564             m = mask_k_over_surface(mid, count+1)
565             IF ( m > nz+1 )  THEN
566                WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )               &
567                     m,' in mask ',mid,' along dimension ', 3,                 &
568                     ' exceeds (nz+1) = ',nz+1
569                CALL message( 'init_masks', 'PA0331', 1, 2, 0, 6, 0 )
570             ENDIF
571             count = count + 1
572             mask_k(mid,count) = mask_k_over_surface(mid, count)
573             IF ( count == mask_xyz_dimension )  EXIT
574          ENDDO
575          mask_start_l(mid,3) = 1
576          mask_size(mid,3)    = count
577          mask_size_l(mid,3)  = count
578       ENDIF
579!
[298]580!--    Set global masks along all three dimensions (required by
581!--    define_netcdf_header).
[1804]582#if defined( __parallel )
[298]583!
584!--    PE0 receives partial arrays from all processors of the respective mask
585!--    and outputs them. Here a barrier has to be set, because otherwise
586!--    "-MPI- FATAL: Remote protocol queue full" may occur.
[807]587
[298]588       CALL MPI_BARRIER( comm2d, ierr )
589
590       IF ( myid == 0 )  THEN
591!
592!--       Local arrays can be relocated directly.
593          mask_i_global(mid,mask_start_l(mid,1): &
594                       mask_start_l(mid,1)+mask_size_l(mid,1)-1) = &
595                       mask_i(mid,:mask_size_l(mid,1))
596          mask_j_global(mid,mask_start_l(mid,2): &
597                       mask_start_l(mid,2)+mask_size_l(mid,2)-1) = &
598                       mask_j(mid,:mask_size_l(mid,2))
599          mask_k_global(mid,mask_start_l(mid,3): &
600                       mask_start_l(mid,3)+mask_size_l(mid,3)-1) = &
601                       mask_k(mid,:mask_size_l(mid,3))
602!
603!--       Receive data from all other PEs.
604          DO  n = 1, numprocs-1
605!
606!--          Receive index limits first, then arrays.
607!--          Index limits are received in arbitrary order from the PEs.
608             CALL MPI_RECV( ind(1), 6, MPI_INTEGER, MPI_ANY_SOURCE, 0,  &
609                  comm2d, status, ierr )
610!
611!--          Not all PEs have data for the mask.
612             IF ( ind(1) /= -9999 )  THEN
613                sender = status(MPI_SOURCE)
614                CALL MPI_RECV( tmp_array(ind(1)), ind(2)-ind(1)+1,  &
[363]615                               MPI_INTEGER, sender, 1, comm2d, status, ierr )
[298]616                mask_i_global(mid,ind(1):ind(2)) = tmp_array(ind(1):ind(2))
617                CALL MPI_RECV( tmp_array(ind(3)), ind(4)-ind(3)+1,  &
[363]618                               MPI_INTEGER, sender, 2, comm2d, status, ierr )
[298]619                mask_j_global(mid,ind(3):ind(4)) = tmp_array(ind(3):ind(4))
620                CALL MPI_RECV( tmp_array(ind(5)), ind(6)-ind(5)+1,  &
[363]621                               MPI_INTEGER, sender, 3, comm2d, status, ierr )
[298]622                mask_k_global(mid,ind(5):ind(6)) = tmp_array(ind(5):ind(6))
623             ENDIF
624          ENDDO
625
626       ELSE
627!
628!--       If at least part of the mask resides on the PE, send the index limits
629!--       for the target array, otherwise send -9999 to PE0.
[996]630          IF ( mask_size_l(mid,1) > 0  .AND.  mask_size_l(mid,2) > 0  .AND.  &
631               mask_size_l(mid,3) > 0  )  THEN
[298]632             ind(1) = mask_start_l(mid,1)
633             ind(2) = mask_start_l(mid,1) + mask_size_l(mid,1) - 1
634             ind(3) = mask_start_l(mid,2)
635             ind(4) = mask_start_l(mid,2) + mask_size_l(mid,2) - 1
636             ind(5) = mask_start_l(mid,3)
637             ind(6) = mask_start_l(mid,3) + mask_size_l(mid,3) - 1
638          ELSE
639             ind(1) = -9999; ind(2) = -9999
640             ind(3) = -9999; ind(4) = -9999
641             ind(5) = -9999; ind(6) = -9999
642          ENDIF
643          CALL MPI_SEND( ind(1), 6, MPI_INTEGER, 0, 0, comm2d, ierr )
644!
645!--       If applicable, send data to PE0.
646          IF ( ind(1) /= -9999 )  THEN
647             tmp_array(:mask_size_l(mid,1)) = mask_i(mid,:mask_size_l(mid,1))
648             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,1),  &
[363]649                            MPI_INTEGER, 0, 1, comm2d, ierr )
[298]650             tmp_array(:mask_size_l(mid,2)) = mask_j(mid,:mask_size_l(mid,2))
651             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,2),  &
[363]652                            MPI_INTEGER, 0, 2, comm2d, ierr )
[298]653             tmp_array(:mask_size_l(mid,3)) = mask_k(mid,:mask_size_l(mid,3))
654             CALL MPI_SEND( tmp_array(1), mask_size_l(mid,3),  &
[363]655                            MPI_INTEGER, 0, 3, comm2d, ierr )
[298]656          ENDIF
657       ENDIF
658!
659!--    A barrier has to be set, because otherwise some PEs may proceed too fast
660!--    so that PE0 may receive wrong data on tag 0.
661       CALL MPI_BARRIER( comm2d, ierr )
[409]662       
[1031]663       IF ( netcdf_data_format > 4 )  THEN
[409]664         
[1414]665          CALL MPI_BCAST( mask_i_global(mid,:), nx+2, MPI_INTEGER, 0, comm2d, &
[493]666                          ierr )
[1414]667          CALL MPI_BCAST( mask_j_global(mid,:), ny+2, MPI_INTEGER, 0, comm2d, &
[493]668                          ierr )
[1414]669          CALL MPI_BCAST( mask_k_global(mid,:), nz+2, MPI_INTEGER, 0, comm2d, &
[493]670                          ierr ) 
[1682]671     
[409]672       ENDIF
[298]673
[1804]674#else
[298]675!
676!--    Local arrays can be relocated directly.
677       mask_i_global(mid,:) = mask_i(mid,:)
678       mask_j_global(mid,:) = mask_j(mid,:)
679       mask_k_global(mid,:) = mask_k(mid,:)
680#endif
681    ENDDO   ! mid
682
683    DEALLOCATE( tmp_array )
684!
685!-- Internal mask arrays cannot be deallocated on PE 0 because they are
686!-- required for header output on PE 0.
687    IF ( myid /= 0 )  DEALLOCATE( mask, mask_loop )
688
689 CONTAINS
[493]690
[298]691!------------------------------------------------------------------------------!
692! Description:
693! ------------
[1682]694!> Set local mask for each subdomain along 'dim' direction.
[298]695!------------------------------------------------------------------------------!
[1682]696    SUBROUTINE set_mask_locations( dim, dxyz, dxyz_string, nxyz, nxyz_string, &
697                                   lb, ub )
[298]698
699       IMPLICIT NONE
700
[1682]701       CHARACTER (LEN=2) ::  dxyz_string !<
702       CHARACTER (LEN=2) ::  nxyz_string !<
[1320]703       
[1682]704       INTEGER(iwp)  ::  count       !<
705       INTEGER(iwp)  ::  count_l     !<
706       INTEGER(iwp)  ::  dim         !<
707       INTEGER(iwp)  ::  m           !<
708       INTEGER(iwp)  ::  loop_begin  !<
709       INTEGER(iwp)  ::  loop_end    !<
710       INTEGER(iwp)  ::  loop_stride !<
711       INTEGER(iwp)  ::  lb          !<
712       INTEGER(iwp)  ::  nxyz        !<
713       INTEGER(iwp)  ::  ub          !<
[1320]714       
[1682]715       REAL(wp)      ::  dxyz  !<
716       REAL(wp)      ::  ddxyz !<
717       REAL(wp)      ::  tmp1  !<
718       REAL(wp)      ::  tmp2  !<
[298]719
[1353]720       count = 0;  count_l = 0 
721       ddxyz = 1.0_wp / dxyz 
722       tmp1  = 0.0_wp
723       tmp2  = 0.0_wp
[298]724
[1353]725       IF ( mask(mid,dim,1) >= 0.0_wp )  THEN
[298]726!
727!--       use predefined mask_* array
[1353]728          DO  WHILE ( mask(mid,dim,count+1) >= 0.0_wp )
[298]729             count = count + 1
730             IF ( dim == 1 .OR. dim == 2 )  THEN
[1353]731                m = NINT( mask(mid,dim,count) * mask_scale(dim) * ddxyz - 0.5_wp )
[1414]732                IF ( m < 0 )  m = 0  ! avoid negative values
[298]733             ELSEIF ( dim == 3 )  THEN
734                ind_array =  &
[595]735                     MINLOC( ABS( mask(mid,dim,count) * mask_scale(dim) - zu ) )
[298]736                m = ind_array(1) - 1 + nzb  ! MINLOC uses lower array bound 1
737             ENDIF
[1414]738             IF ( m > (nxyz+1) )  THEN
[3045]739                WRITE ( message_string, '(I3,A,I3,A,I1,3A,I3)' )               &
740                     m,' in mask ',mid,' along dimension ',dim,                &
[1414]741                     ' exceeds (',nxyz_string,'+1) = ',nxyz+1
[564]742                CALL message( 'init_masks', 'PA0331', 1, 2, 0, 6, 0 )
[298]743             ENDIF
[1414]744             IF ( ( m >= lb .AND. m <= ub ) .OR.     &
745                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
[298]746                IF ( count_l == 0 )  mask_start_l(mid,dim) = count
747                count_l = count_l + 1
748                IF ( dim == 1 )  THEN
749                   mask_i(mid,count_l) = m
750                ELSEIF ( dim == 2 )  THEN
751                   mask_j(mid,count_l) = m
752                ELSEIF ( dim == 3 )  THEN
753                   mask_k(mid,count_l) = m
754                ENDIF
755             ENDIF
756             IF ( count == mask_xyz_dimension )  EXIT
757          ENDDO
758          mask_size(mid,dim)   = count
759          mask_size_l(mid,dim) = count_l
760
761       ELSE
762!
763!--       use predefined mask_loop_* array, or use the default (all grid points
764!--       along this direction)
[1353]765          IF ( mask_loop(mid,dim,1) < 0.0_wp )  THEN
[298]766             tmp1 = mask_loop(mid,dim,1)
[1186]767             mask_loop(mid,dim,1) = zw(nzb)  !   lowest level  (default)
[298]768          ENDIF
769          IF ( dim == 1 .OR. dim == 2 )  THEN
[1353]770             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
[298]771                tmp2 = mask_loop(mid,dim,2)
[3512]772                mask_loop(mid,dim,2) = nxyz*dxyz / mask_scale(dim)   ! (default)
[298]773             ENDIF
774             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
[1414]775                  > (nxyz+1) * dxyz / mask_scale(dim) )  THEN
[303]776                WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),5A,I1,A,F9.3)' ) &
[3045]777                     'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1),     &
[303]778                     ' and/or mask_loop(',mid,',',dim,',2)=', &
[3045]779                     mask_loop(mid,dim,2),' exceed (', &
780                     nxyz_string,'+1)*',dxyz_string,'/mask_scale(',dim,')=',   &
[1414]781                     (nxyz+1)*dxyz/mask_scale(dim)
[564]782                CALL message( 'init_masks', 'PA0332', 1, 2, 0, 6, 0 )
[298]783             ENDIF
[3045]784             loop_begin  = NINT( mask_loop(mid,dim,1) * mask_scale(dim)        &
[1353]785                  * ddxyz - 0.5_wp )
[3045]786             loop_end    = NINT( mask_loop(mid,dim,2) * mask_scale(dim)        &
[1353]787                  * ddxyz - 0.5_wp )
[3045]788             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim)        &
[1032]789                  * ddxyz )
[1414]790             IF ( loop_begin == -1 )  loop_begin = 0  ! avoid negative values
[298]791          ELSEIF ( dim == 3 )  THEN
[1353]792             IF ( mask_loop(mid,dim,2) < 0.0_wp )  THEN
[298]793                tmp2 = mask_loop(mid,dim,2)
[1414]794                mask_loop(mid,dim,2) = zu(nz+1) / mask_scale(dim)   ! (default)
[298]795             ENDIF
796             IF ( MAXVAL( mask_loop(mid,dim,1:2) )  &
[1414]797                  > zu(nz+1) / mask_scale(dim) )  THEN
[3045]798                WRITE ( message_string, '(2(A,I3,A,I1,A,F9.3),A,I1,A,F9.3)' )  &
799                     'mask_loop(',mid,',',dim,',1)=',mask_loop(mid,dim,1),     &
[303]800                     ' and/or mask_loop(',mid,',',dim,',2)=', &
[3045]801                     mask_loop(mid,dim,2),' exceed zu(nz+1)/mask_scale(',dim,  &
[1414]802                     ')=',zu(nz+1)/mask_scale(dim)
[564]803                CALL message( 'init_masks', 'PA0333', 1, 2, 0, 6, 0 )
[298]804             ENDIF
[303]805             ind_array =  &
[595]806                  MINLOC( ABS( mask_loop(mid,dim,1) * mask_scale(dim) - zu ) )
[298]807             loop_begin =  &
[303]808                  ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
809             ind_array =  &
[595]810                  MINLOC( ABS( mask_loop(mid,dim,2) * mask_scale(dim) - zu ) )
[298]811             loop_end = ind_array(1) - 1 + nzb ! MINLOC uses lower array bound 1
812!
[303]813!--          The following line assumes a constant vertical grid spacing within
814!--          the vertical mask range; it fails for vertical grid stretching.
[3065]815!--          Maybe revise later. Issue warning but continue execution. ABS(...)
816!--          within the IF statement is necessary because the default value of
817!--          dz_stretch_level_start is -9999999.9_wp.
[303]818             loop_stride = NINT( mask_loop(mid,dim,3) * mask_scale(dim) * ddxyz )
819
[3065]820             IF ( mask_loop(mid,dim,2) * mask_scale(dim) >                     &
821                  ABS( dz_stretch_level_start(1) ) )  THEN
[3045]822                WRITE ( message_string, '(A,I3,A,I1,A,F9.3,A,F8.2,3A)' )       &
823                     'mask_loop(',mid,',',dim,',2)=', mask_loop(mid,dim,2),    &
[3065]824                     ' exceeds dz_stretch_level=',dz_stretch_level_start(1),   &
[3046]825                     '.&Vertical mask locations will not ',                    &
[3045]826                     'match the desired heights within the stretching ',       &
[2271]827                     'region.'
[564]828                CALL message( 'init_masks', 'PA0334', 0, 1, 0, 6, 0 )
[303]829             ENDIF
830
[298]831          ENDIF
832!
833!--       If necessary, reset mask_loop(mid,dim,1) and mask_loop(mid,dim,2).
[1353]834          IF ( tmp1 < 0.0_wp )  mask_loop(mid,dim,1) = tmp1
835          IF ( tmp2 < 0.0_wp )  mask_loop(mid,dim,2) = tmp2
[298]836!
837!--       The default stride +/-1 (every grid point) applies if
838!--       mask_loop(mid,dim,3) is not specified (its default is zero).
839          IF ( loop_stride == 0 )  THEN
840             IF ( loop_end >= loop_begin )  THEN
841                loop_stride =  1
842             ELSE
843                loop_stride = -1
844             ENDIF
845          ENDIF
846          DO  m = loop_begin, loop_end, loop_stride
847             count = count + 1
[1414]848             IF ( ( m >= lb  .AND.  m <= ub ) .OR.   &
849                  ( m == (nxyz+1) .AND. ub == nxyz )  )  THEN
[298]850                IF ( count_l == 0 )  mask_start_l(mid,dim) = count
851                count_l = count_l + 1
852                IF ( dim == 1 )  THEN
853                   mask_i(mid,count_l) = m
854                ELSEIF ( dim == 2 )  THEN
855                   mask_j(mid,count_l) = m
856                ELSEIF ( dim == 3 )  THEN
857                   mask_k(mid,count_l) = m
858                ENDIF
859             ENDIF
860          ENDDO
861          mask_size(mid,dim)   = count
862          mask_size_l(mid,dim) = count_l
[1414]863
[298]864       ENDIF
865
866    END SUBROUTINE set_mask_locations
867
868 END SUBROUTINE init_masks
Note: See TracBrowser for help on using the repository browser.