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

Last change on this file since 3459 was 3435, checked in by gronemeier, 6 years ago

new: terrain-following masked output; bugfixes: increase vertical dimension of gamma_w_green_sat by 1, add checks for masked output for chemistry_model and radiation_model, reordered calls to xxx_define_netcdf_grid in masked output part

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