New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
bdyini.F90 in NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY – NEMO

source: NEMO/branches/2019/dev_r10984_HPC-13_IRRMANN_BDY_optimization/src/OCE/BDY/bdyini.F90 @ 11044

Last change on this file since 11044 was 11044, checked in by girrmann, 5 years ago

dev_r10984_HPC-13 : missing part of [11024], see #2285

  • Property svn:keywords set to Id
File size: 81.7 KB
Line 
1MODULE bdyini
2   !!======================================================================
3   !!                       ***  MODULE  bdyini  ***
4   !! Unstructured open boundaries : initialisation
5   !!======================================================================
6   !! History :  1.0  !  2005-01  (J. Chanut, A. Sellar)  Original code
7   !!             -   !  2007-01  (D. Storkey) Update to use IOM module
8   !!             -   !  2007-01  (D. Storkey) Tidal forcing
9   !!            3.0  !  2008-04  (NEMO team)  add in the reference version
10   !!            3.3  !  2010-09  (E.O'Dea) updates for Shelf configurations
11   !!            3.3  !  2010-09  (D.Storkey) add ice boundary conditions
12   !!            3.4  !  2011     (D. Storkey) rewrite in preparation for OBC-BDY merge
13   !!            3.4  !  2012     (J. Chanut) straight open boundary case update
14   !!            3.5  !  2012     (S. Mocavero, I. Epicoco) optimization of BDY communications
15   !!            3.7  !  2016     (T. Lovato) Remove bdy macro, call here init for dta and tides
16   !!----------------------------------------------------------------------
17   !!   bdy_init      : Initialization of unstructured open boundaries
18   !!----------------------------------------------------------------------
19   USE oce            ! ocean dynamics and tracers variables
20   USE dom_oce        ! ocean space and time domain
21   USE bdy_oce        ! unstructured open boundary conditions
22   USE bdydta         ! open boundary cond. setting   (bdy_dta_init routine)
23   USE bdytides       ! open boundary cond. setting   (bdytide_init routine)
24   USE sbctide        ! Tidal forcing or not
25   USE phycst   , ONLY: rday
26   !
27   USE in_out_manager ! I/O units
28   USE lbclnk         ! ocean lateral boundary conditions (or mpp link)
29   USE lib_mpp        ! for mpp_sum 
30   USE iom            ! I/O
31
32   IMPLICIT NONE
33   PRIVATE
34
35   PUBLIC   bdy_init   ! routine called in nemo_init
36
37   INTEGER, PARAMETER ::   jp_nseg = 100   !
38   INTEGER, PARAMETER ::   nrimmax =  20   ! maximum rimwidth in structured
39                                               ! open boundary data files
40   ! Straight open boundary segment parameters:
41   INTEGER  ::   nbdysege, nbdysegw, nbdysegn, nbdysegs 
42   INTEGER, DIMENSION(jp_nseg) ::   jpieob, jpjedt, jpjeft, npckge   !
43   INTEGER, DIMENSION(jp_nseg) ::   jpiwob, jpjwdt, jpjwft, npckgw   !
44   INTEGER, DIMENSION(jp_nseg) ::   jpjnob, jpindt, jpinft, npckgn   !
45   INTEGER, DIMENSION(jp_nseg) ::   jpjsob, jpisdt, jpisft, npckgs   !
46   !!----------------------------------------------------------------------
47   !! NEMO/OCE 4.0 , NEMO Consortium (2018)
48   !! $Id$
49   !! Software governed by the CeCILL license (see ./LICENSE)
50   !!----------------------------------------------------------------------
51CONTAINS
52
53   SUBROUTINE bdy_init
54      !!----------------------------------------------------------------------
55      !!                 ***  ROUTINE bdy_init  ***
56      !!
57      !! ** Purpose :   Initialization of the dynamics and tracer fields with
58      !!              unstructured open boundaries.
59      !!
60      !! ** Method  :   Read initialization arrays (mask, indices) to identify
61      !!              an unstructured open boundary
62      !!
63      !! ** Input   :  bdy_init.nc, input file for unstructured open boundaries
64      !!----------------------------------------------------------------------
65      NAMELIST/nambdy/ ln_bdy, nb_bdy, ln_coords_file, cn_coords_file,         &
66         &             ln_mask_file, cn_mask_file, cn_dyn2d, nn_dyn2d_dta,     &
67         &             cn_dyn3d, nn_dyn3d_dta, cn_tra, nn_tra_dta,             &
68         &             ln_tra_dmp, ln_dyn3d_dmp, rn_time_dmp, rn_time_dmp_out, &
69         &             cn_ice, nn_ice_dta,                                     &
70         &             rn_ice_tem, rn_ice_sal, rn_ice_age,                     &
71         &             ln_vol, nn_volctl, nn_rimwidth, nb_jpk_bdy
72         !
73      INTEGER  ::   ios                 ! Local integer output status for namelist read
74      !!----------------------------------------------------------------------
75
76      ! ------------------------
77      ! Read namelist parameters
78      ! ------------------------
79      REWIND( numnam_ref )              ! Namelist nambdy in reference namelist :Unstructured open boundaries
80      READ  ( numnam_ref, nambdy, IOSTAT = ios, ERR = 901)
81901   IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy in reference namelist', lwp )
82      REWIND( numnam_cfg )              ! Namelist nambdy in configuration namelist :Unstructured open boundaries
83      READ  ( numnam_cfg, nambdy, IOSTAT = ios, ERR = 902 )
84902   IF( ios >  0 )   CALL ctl_nam ( ios , 'nambdy in configuration namelist', lwp )
85      IF(lwm) WRITE ( numond, nambdy )
86
87      IF( .NOT. Agrif_Root() ) ln_bdy = .FALSE.   ! forced for Agrif children
88     
89      ! -----------------------------------------
90      ! unstructured open boundaries use control
91      ! -----------------------------------------
92      IF ( ln_bdy ) THEN
93         IF(lwp) WRITE(numout,*)
94         IF(lwp) WRITE(numout,*) 'bdy_init : initialization of open boundaries'
95         IF(lwp) WRITE(numout,*) '~~~~~~~~'
96         !
97         ! Open boundaries definition (arrays and masks)
98         CALL bdy_segs
99         !
100         ! Open boundaries initialisation of external data arrays
101         CALL bdy_dta_init
102         !
103         ! Open boundaries initialisation of tidal harmonic forcing
104         IF( ln_tide ) CALL bdytide_init
105         !
106      ELSE
107         IF(lwp) WRITE(numout,*)
108         IF(lwp) WRITE(numout,*) 'bdy_init : open boundaries not used (ln_bdy = F)'
109         IF(lwp) WRITE(numout,*) '~~~~~~~~'
110         !
111      ENDIF
112      !
113   END SUBROUTINE bdy_init
114
115
116   SUBROUTINE bdy_segs
117      !!----------------------------------------------------------------------
118      !!                 ***  ROUTINE bdy_init  ***
119      !!         
120      !! ** Purpose :   Definition of unstructured open boundaries.
121      !!
122      !! ** Method  :   Read initialization arrays (mask, indices) to identify
123      !!              an unstructured open boundary
124      !!
125      !! ** Input   :  bdy_init.nc, input file for unstructured open boundaries
126      !!----------------------------------------------------------------------     
127      INTEGER  ::   ib_bdy, ii, ij, ik, igrd, ib, ir, iseg ! dummy loop indices
128      INTEGER  ::   icount, icountr, ibr_max, ilen1, ibm1  ! local integers
129      INTEGER  ::   iwe, ies, iso, ino, inum, id_dummy     !   -       -
130      INTEGER  ::   igrd_start, igrd_end, jpbdta           !   -       -
131      INTEGER  ::   jpbdtau, jpbdtas                       !   -       -
132      INTEGER  ::   ib_bdy1, ib_bdy2, ib1, ib2             !   -       -
133      INTEGER  ::   i_offset, j_offset, inbdy            !   -       -
134      INTEGER , POINTER  ::  nbi, nbj, nbr                 ! short cuts
135      REAL(wp), POINTER, DIMENSION(:,:)       ::   pmask    ! pointer to 2D mask fields
136      REAL(wp) ::   zefl, zwfl, znfl, zsfl                 ! local scalars
137      INTEGER, DIMENSION (2)                  ::   kdimsz
138      INTEGER, DIMENSION(jpbgrd,jp_bdy)       ::   nblendta         ! Length of index arrays
139      INTEGER, ALLOCATABLE, DIMENSION(:,:,:)  ::   nbidta, nbjdta   ! Index arrays: i and j indices of bdy dta
140      INTEGER, ALLOCATABLE, DIMENSION(:,:,:)  ::   nbrdta           ! Discrete distance from rim points
141      CHARACTER(LEN=1),DIMENSION(jpbgrd)      ::   cgrid
142      INTEGER :: com_east, com_west, com_south, com_north, jpk_max  ! Flags for boundaries sending
143      INTEGER :: com_east_b, com_west_b, com_south_b, com_north_b  ! Flags for boundaries receiving
144      INTEGER :: iw_b(4), ie_b(4), is_b(4), in_b(4)                ! Arrays for neighbours coordinates
145      REAL(wp), TARGET, DIMENSION(jpi,jpj) ::   zfmask  ! temporary fmask array excluding coastal boundary condition (shlat)
146      LOGICAL  ::   llnobdy, llsobdy, lleabdy, llwebdy     ! local logicals
147      !!
148      CHARACTER(LEN=1)                     ::   ctypebdy   !     -        -
149      INTEGER                              ::   nbdyind, nbdybeg, nbdyend
150      !!
151      NAMELIST/nambdy_index/ ctypebdy, nbdyind, nbdybeg, nbdyend
152      INTEGER  ::   ios                 ! Local integer output status for namelist read
153      !!----------------------------------------------------------------------
154      !
155      cgrid = (/'t','u','v'/)
156
157      ! -----------------------------------------
158      ! Check and write out namelist parameters
159      ! -----------------------------------------
160      IF( jperio /= 0 )   CALL ctl_stop( 'bdy_segs: Cyclic or symmetric,',   &
161         &                               ' and general open boundary condition are not compatible' )
162
163      IF( nb_bdy == 0 ) THEN
164        IF(lwp) WRITE(numout,*) 'nb_bdy = 0, NO OPEN BOUNDARIES APPLIED.'
165      ELSE
166        IF(lwp) WRITE(numout,*) 'Number of open boundary sets : ', nb_bdy
167      ENDIF
168
169      DO ib_bdy = 1,nb_bdy
170        IF(lwp) WRITE(numout,*) ' ' 
171        IF(lwp) WRITE(numout,*) '------ Open boundary data set ',ib_bdy,'------' 
172
173        IF( ln_coords_file(ib_bdy) ) THEN
174           IF(lwp) WRITE(numout,*) 'Boundary definition read from file '//TRIM(cn_coords_file(ib_bdy))
175        ELSE
176           IF(lwp) WRITE(numout,*) 'Boundary defined in namelist.'
177        ENDIF
178        IF(lwp) WRITE(numout,*)
179
180        IF(lwp) WRITE(numout,*) 'Boundary conditions for barotropic solution:  '
181        SELECT CASE( cn_dyn2d(ib_bdy) )                 
182          CASE( 'none' )         
183             IF(lwp) WRITE(numout,*) '      no open boundary condition'       
184             dta_bdy(ib_bdy)%ll_ssh = .false.
185             dta_bdy(ib_bdy)%ll_u2d = .false.
186             dta_bdy(ib_bdy)%ll_v2d = .false.
187          CASE( 'frs' )         
188             IF(lwp) WRITE(numout,*) '      Flow Relaxation Scheme'
189             dta_bdy(ib_bdy)%ll_ssh = .false.
190             dta_bdy(ib_bdy)%ll_u2d = .true.
191             dta_bdy(ib_bdy)%ll_v2d = .true.
192          CASE( 'flather' )     
193             IF(lwp) WRITE(numout,*) '      Flather radiation condition'
194             dta_bdy(ib_bdy)%ll_ssh = .true.
195             dta_bdy(ib_bdy)%ll_u2d = .true.
196             dta_bdy(ib_bdy)%ll_v2d = .true.
197          CASE( 'orlanski' )     
198             IF(lwp) WRITE(numout,*) '      Orlanski (fully oblique) radiation condition with adaptive nudging'
199             dta_bdy(ib_bdy)%ll_ssh = .false.
200             dta_bdy(ib_bdy)%ll_u2d = .true.
201             dta_bdy(ib_bdy)%ll_v2d = .true.
202          CASE( 'orlanski_npo' ) 
203             IF(lwp) WRITE(numout,*) '      Orlanski (NPO) radiation condition with adaptive nudging'
204             dta_bdy(ib_bdy)%ll_ssh = .false.
205             dta_bdy(ib_bdy)%ll_u2d = .true.
206             dta_bdy(ib_bdy)%ll_v2d = .true.
207          CASE DEFAULT   ;   CALL ctl_stop( 'unrecognised value for cn_dyn2d' )
208        END SELECT
209        IF( cn_dyn2d(ib_bdy) /= 'none' ) THEN
210           SELECT CASE( nn_dyn2d_dta(ib_bdy) )                   !
211              CASE( 0 )      ;   IF(lwp) WRITE(numout,*) '      initial state used for bdy data'       
212              CASE( 1 )      ;   IF(lwp) WRITE(numout,*) '      boundary data taken from file'
213              CASE( 2 )      ;   IF(lwp) WRITE(numout,*) '      tidal harmonic forcing taken from file'
214              CASE( 3 )      ;   IF(lwp) WRITE(numout,*) '      boundary data AND tidal harmonic forcing taken from files'
215              CASE DEFAULT   ;   CALL ctl_stop( 'nn_dyn2d_dta must be between 0 and 3' )
216           END SELECT
217           IF (( nn_dyn2d_dta(ib_bdy) .ge. 2 ).AND.(.NOT.ln_tide)) THEN
218             CALL ctl_stop( 'You must activate with ln_tide to add tidal forcing at open boundaries' )
219           ENDIF
220        ENDIF
221        IF(lwp) WRITE(numout,*)
222
223        IF(lwp) WRITE(numout,*) 'Boundary conditions for baroclinic velocities:  '
224        SELECT CASE( cn_dyn3d(ib_bdy) )                 
225          CASE('none')
226             IF(lwp) WRITE(numout,*) '      no open boundary condition'       
227             dta_bdy(ib_bdy)%ll_u3d = .false.
228             dta_bdy(ib_bdy)%ll_v3d = .false.
229          CASE('frs')       
230             IF(lwp) WRITE(numout,*) '      Flow Relaxation Scheme'
231             dta_bdy(ib_bdy)%ll_u3d = .true.
232             dta_bdy(ib_bdy)%ll_v3d = .true.
233          CASE('specified')
234             IF(lwp) WRITE(numout,*) '      Specified value'
235             dta_bdy(ib_bdy)%ll_u3d = .true.
236             dta_bdy(ib_bdy)%ll_v3d = .true.
237          CASE('neumann')
238             IF(lwp) WRITE(numout,*) '      Neumann conditions'
239             dta_bdy(ib_bdy)%ll_u3d = .false.
240             dta_bdy(ib_bdy)%ll_v3d = .false.
241          CASE('zerograd')
242             IF(lwp) WRITE(numout,*) '      Zero gradient for baroclinic velocities'
243             dta_bdy(ib_bdy)%ll_u3d = .false.
244             dta_bdy(ib_bdy)%ll_v3d = .false.
245          CASE('zero')
246             IF(lwp) WRITE(numout,*) '      Zero baroclinic velocities (runoff case)'
247             dta_bdy(ib_bdy)%ll_u3d = .false.
248             dta_bdy(ib_bdy)%ll_v3d = .false.
249          CASE('orlanski')
250             IF(lwp) WRITE(numout,*) '      Orlanski (fully oblique) radiation condition with adaptive nudging'
251             dta_bdy(ib_bdy)%ll_u3d = .true.
252             dta_bdy(ib_bdy)%ll_v3d = .true.
253          CASE('orlanski_npo')
254             IF(lwp) WRITE(numout,*) '      Orlanski (NPO) radiation condition with adaptive nudging'
255             dta_bdy(ib_bdy)%ll_u3d = .true.
256             dta_bdy(ib_bdy)%ll_v3d = .true.
257          CASE DEFAULT   ;   CALL ctl_stop( 'unrecognised value for cn_dyn3d' )
258        END SELECT
259        IF( cn_dyn3d(ib_bdy) /= 'none' ) THEN
260           SELECT CASE( nn_dyn3d_dta(ib_bdy) )                   !
261              CASE( 0 )      ;   IF(lwp) WRITE(numout,*) '      initial state used for bdy data'       
262              CASE( 1 )      ;   IF(lwp) WRITE(numout,*) '      boundary data taken from file'
263              CASE DEFAULT   ;   CALL ctl_stop( 'nn_dyn3d_dta must be 0 or 1' )
264           END SELECT
265        ENDIF
266
267        IF ( ln_dyn3d_dmp(ib_bdy) ) THEN
268           IF ( cn_dyn3d(ib_bdy) == 'none' ) THEN
269              IF(lwp) WRITE(numout,*) 'No open boundary condition for baroclinic velocities: ln_dyn3d_dmp is set to .false.'
270              ln_dyn3d_dmp(ib_bdy)=.false.
271           ELSEIF ( cn_dyn3d(ib_bdy) == 'frs' ) THEN
272              CALL ctl_stop( 'Use FRS OR relaxation' )
273           ELSE
274              IF(lwp) WRITE(numout,*) '      + baroclinic velocities relaxation zone'
275              IF(lwp) WRITE(numout,*) '      Damping time scale: ',rn_time_dmp(ib_bdy),' days'
276              IF((lwp).AND.rn_time_dmp(ib_bdy)<0) CALL ctl_stop( 'Time scale must be positive' )
277              dta_bdy(ib_bdy)%ll_u3d = .true.
278              dta_bdy(ib_bdy)%ll_v3d = .true.
279           ENDIF
280        ELSE
281           IF(lwp) WRITE(numout,*) '      NO relaxation on baroclinic velocities'
282        ENDIF
283        IF(lwp) WRITE(numout,*)
284
285        IF(lwp) WRITE(numout,*) 'Boundary conditions for temperature and salinity:  '
286        SELECT CASE( cn_tra(ib_bdy) )                 
287          CASE('none')
288             IF(lwp) WRITE(numout,*) '      no open boundary condition'       
289             dta_bdy(ib_bdy)%ll_tem = .false.
290             dta_bdy(ib_bdy)%ll_sal = .false.
291          CASE('frs')
292             IF(lwp) WRITE(numout,*) '      Flow Relaxation Scheme'
293             dta_bdy(ib_bdy)%ll_tem = .true.
294             dta_bdy(ib_bdy)%ll_sal = .true.
295          CASE('specified')
296             IF(lwp) WRITE(numout,*) '      Specified value'
297             dta_bdy(ib_bdy)%ll_tem = .true.
298             dta_bdy(ib_bdy)%ll_sal = .true.
299          CASE('neumann')
300             IF(lwp) WRITE(numout,*) '      Neumann conditions'
301             dta_bdy(ib_bdy)%ll_tem = .false.
302             dta_bdy(ib_bdy)%ll_sal = .false.
303          CASE('runoff')
304             IF(lwp) WRITE(numout,*) '      Runoff conditions : Neumann for T and specified to 0.1 for salinity'
305             dta_bdy(ib_bdy)%ll_tem = .false.
306             dta_bdy(ib_bdy)%ll_sal = .false.
307          CASE('orlanski')
308             IF(lwp) WRITE(numout,*) '      Orlanski (fully oblique) radiation condition with adaptive nudging'
309             dta_bdy(ib_bdy)%ll_tem = .true.
310             dta_bdy(ib_bdy)%ll_sal = .true.
311          CASE('orlanski_npo')
312             IF(lwp) WRITE(numout,*) '      Orlanski (NPO) radiation condition with adaptive nudging'
313             dta_bdy(ib_bdy)%ll_tem = .true.
314             dta_bdy(ib_bdy)%ll_sal = .true.
315          CASE DEFAULT   ;   CALL ctl_stop( 'unrecognised value for cn_tra' )
316        END SELECT
317        IF( cn_tra(ib_bdy) /= 'none' ) THEN
318           SELECT CASE( nn_tra_dta(ib_bdy) )                   !
319              CASE( 0 )      ;   IF(lwp) WRITE(numout,*) '      initial state used for bdy data'       
320              CASE( 1 )      ;   IF(lwp) WRITE(numout,*) '      boundary data taken from file'
321              CASE DEFAULT   ;   CALL ctl_stop( 'nn_tra_dta must be 0 or 1' )
322           END SELECT
323        ENDIF
324
325        IF ( ln_tra_dmp(ib_bdy) ) THEN
326           IF ( cn_tra(ib_bdy) == 'none' ) THEN
327              IF(lwp) WRITE(numout,*) 'No open boundary condition for tracers: ln_tra_dmp is set to .false.'
328              ln_tra_dmp(ib_bdy)=.false.
329           ELSEIF ( cn_tra(ib_bdy) == 'frs' ) THEN
330              CALL ctl_stop( 'Use FRS OR relaxation' )
331           ELSE
332              IF(lwp) WRITE(numout,*) '      + T/S relaxation zone'
333              IF(lwp) WRITE(numout,*) '      Damping time scale: ',rn_time_dmp(ib_bdy),' days'
334              IF(lwp) WRITE(numout,*) '      Outflow damping time scale: ',rn_time_dmp_out(ib_bdy),' days'
335              IF((lwp).AND.rn_time_dmp(ib_bdy)<0) CALL ctl_stop( 'Time scale must be positive' )
336              dta_bdy(ib_bdy)%ll_tem = .true.
337              dta_bdy(ib_bdy)%ll_sal = .true.
338           ENDIF
339        ELSE
340           IF(lwp) WRITE(numout,*) '      NO T/S relaxation'
341        ENDIF
342        IF(lwp) WRITE(numout,*)
343
344#if defined key_si3
345         IF(lwp) WRITE(numout,*) 'Boundary conditions for sea ice:  '
346         SELECT CASE( cn_ice(ib_bdy) )                 
347         CASE('none')
348             IF(lwp) WRITE(numout,*) '      no open boundary condition'       
349             dta_bdy(ib_bdy)%ll_a_i = .false.
350             dta_bdy(ib_bdy)%ll_h_i = .false.
351             dta_bdy(ib_bdy)%ll_h_s = .false.
352         CASE('frs')
353             IF(lwp) WRITE(numout,*) '      Flow Relaxation Scheme'
354             dta_bdy(ib_bdy)%ll_a_i = .true.
355             dta_bdy(ib_bdy)%ll_h_i = .true.
356             dta_bdy(ib_bdy)%ll_h_s = .true.
357         CASE DEFAULT   ;   CALL ctl_stop( 'unrecognised value for cn_ice' )
358         END SELECT
359        IF( cn_ice(ib_bdy) /= 'none' ) THEN
360           SELECT CASE( nn_ice_dta(ib_bdy) )                   !
361              CASE( 0 )      ;   IF(lwp) WRITE(numout,*) '      initial state used for bdy data'       
362              CASE( 1 )      ;   IF(lwp) WRITE(numout,*) '      boundary data taken from file'
363              CASE DEFAULT   ;   CALL ctl_stop( 'nn_ice_dta must be 0 or 1' )
364           END SELECT
365        ENDIF
366        IF(lwp) WRITE(numout,*)
367        IF(lwp) WRITE(numout,*) '      tem of bdy sea-ice = ', rn_ice_tem(ib_bdy)         
368        IF(lwp) WRITE(numout,*) '      sal of bdy sea-ice = ', rn_ice_sal(ib_bdy)         
369        IF(lwp) WRITE(numout,*) '      age of bdy sea-ice = ', rn_ice_age(ib_bdy)         
370#endif
371
372        IF(lwp) WRITE(numout,*) '      Width of relaxation zone = ', nn_rimwidth(ib_bdy)
373        IF(lwp) WRITE(numout,*)
374         !
375      END DO
376
377     IF( nb_bdy > 0 ) THEN
378        IF( ln_vol ) THEN                     ! check volume conservation (nn_volctl value)
379          IF(lwp) WRITE(numout,*) 'Volume correction applied at open boundaries'
380          IF(lwp) WRITE(numout,*)
381          SELECT CASE ( nn_volctl )
382            CASE( 1 )      ;   IF(lwp) WRITE(numout,*) '      The total volume will be constant'
383            CASE( 0 )      ;   IF(lwp) WRITE(numout,*) '      The total volume will vary according to the surface E-P flux'
384            CASE DEFAULT   ;   CALL ctl_stop( 'nn_volctl must be 0 or 1' )
385          END SELECT
386          IF(lwp) WRITE(numout,*)
387          !
388          ! sanity check if used with tides       
389          IF( ln_tide ) THEN
390             IF(lwp) WRITE(numout,*) ' The total volume correction is not working with tides. '
391             IF(lwp) WRITE(numout,*) ' Set ln_vol to .FALSE. '
392             IF(lwp) WRITE(numout,*) ' or '
393             IF(lwp) WRITE(numout,*) ' equilibriate your bdy input files '
394             CALL ctl_stop( 'The total volume correction is not working with tides.' )
395          END IF
396        ELSE
397          IF(lwp) WRITE(numout,*) 'No volume correction applied at open boundaries'
398          IF(lwp) WRITE(numout,*)
399        ENDIF
400        IF( nb_jpk_bdy(ib_bdy) > 0 ) THEN
401           IF(lwp) WRITE(numout,*) '*** open boundary will be interpolate in the vertical onto the native grid ***'
402        ELSE
403           IF(lwp) WRITE(numout,*) '*** open boundary will be read straight onto the native grid without vertical interpolation ***'
404        ENDIF
405     ENDIF
406
407      ! -------------------------------------------------
408      ! Initialise indices arrays for open boundaries
409      ! -------------------------------------------------
410
411      ! Work out global dimensions of boundary data
412      ! ---------------------------------------------
413      REWIND( numnam_cfg )     
414
415      nblendta(:,:) = 0
416      nbdysege = 0
417      nbdysegw = 0
418      nbdysegn = 0
419      nbdysegs = 0
420      icount   = 0 ! count user defined segments
421      ! Dimensions below are used to allocate arrays to read external data
422      jpbdtas = 1 ! Maximum size of boundary data (structured case)
423      jpbdtau = 1 ! Maximum size of boundary data (unstructured case)
424
425      DO ib_bdy = 1, nb_bdy
426
427         IF( .NOT. ln_coords_file(ib_bdy) ) THEN ! Work out size of global arrays from namelist parameters
428 
429            icount = icount + 1
430            ! No REWIND here because may need to read more than one nambdy_index namelist.
431            ! Read only namelist_cfg to avoid unseccessfull overwrite
432            ! keep full control of the configuration namelist
433            READ  ( numnam_cfg, nambdy_index, IOSTAT = ios, ERR = 904 )
434904         IF( ios /= 0 )   CALL ctl_nam ( ios , 'nambdy_index in configuration namelist', lwp )
435            IF(lwm) WRITE ( numond, nambdy_index )
436
437            SELECT CASE ( TRIM(ctypebdy) )
438              CASE( 'N' )
439                 IF( nbdyind == -1 ) THEN  ! Automatic boundary definition: if nbdysegX = -1
440                    nbdyind  = jpjglo - 2  ! set boundary to whole side of model domain.
441                    nbdybeg  = 2
442                    nbdyend  = jpiglo - 1
443                 ENDIF
444                 nbdysegn = nbdysegn + 1
445                 npckgn(nbdysegn) = ib_bdy ! Save bdy package number
446                 jpjnob(nbdysegn) = nbdyind
447                 jpindt(nbdysegn) = nbdybeg
448                 jpinft(nbdysegn) = nbdyend
449                 !
450              CASE( 'S' )
451                 IF( nbdyind == -1 ) THEN  ! Automatic boundary definition: if nbdysegX = -1
452                    nbdyind  = 2           ! set boundary to whole side of model domain.
453                    nbdybeg  = 2
454                    nbdyend  = jpiglo - 1
455                 ENDIF
456                 nbdysegs = nbdysegs + 1
457                 npckgs(nbdysegs) = ib_bdy ! Save bdy package number
458                 jpjsob(nbdysegs) = nbdyind
459                 jpisdt(nbdysegs) = nbdybeg
460                 jpisft(nbdysegs) = nbdyend
461                 !
462              CASE( 'E' )
463                 IF( nbdyind == -1 ) THEN  ! Automatic boundary definition: if nbdysegX = -1
464                    nbdyind  = jpiglo - 2  ! set boundary to whole side of model domain.
465                    nbdybeg  = 2
466                    nbdyend  = jpjglo - 1
467                 ENDIF
468                 nbdysege = nbdysege + 1 
469                 npckge(nbdysege) = ib_bdy ! Save bdy package number
470                 jpieob(nbdysege) = nbdyind
471                 jpjedt(nbdysege) = nbdybeg
472                 jpjeft(nbdysege) = nbdyend
473                 !
474              CASE( 'W' )
475                 IF( nbdyind == -1 ) THEN  ! Automatic boundary definition: if nbdysegX = -1
476                    nbdyind  = 2           ! set boundary to whole side of model domain.
477                    nbdybeg  = 2
478                    nbdyend  = jpjglo - 1
479                 ENDIF
480                 nbdysegw = nbdysegw + 1
481                 npckgw(nbdysegw) = ib_bdy ! Save bdy package number
482                 jpiwob(nbdysegw) = nbdyind
483                 jpjwdt(nbdysegw) = nbdybeg
484                 jpjwft(nbdysegw) = nbdyend
485                 !
486              CASE DEFAULT   ;   CALL ctl_stop( 'ctypebdy must be N, S, E or W' )
487            END SELECT
488
489            ! For simplicity we assume that in case of straight bdy, arrays have the same length
490            ! (even if it is true that last tangential velocity points
491            ! are useless). This simplifies a little bit boundary data format (and agrees with format
492            ! used so far in obc package)
493
494            nblendta(1:jpbgrd,ib_bdy) =  (nbdyend - nbdybeg + 1) * nn_rimwidth(ib_bdy)
495            jpbdtas = MAX(jpbdtas, (nbdyend - nbdybeg + 1))
496            IF (lwp.and.(nn_rimwidth(ib_bdy)>nrimmax)) &
497            & CALL ctl_stop( 'rimwidth must be lower than nrimmax' )
498
499         ELSE            ! Read size of arrays in boundary coordinates file.
500            CALL iom_open( cn_coords_file(ib_bdy), inum )
501            DO igrd = 1, jpbgrd
502               id_dummy = iom_varid( inum, 'nbi'//cgrid(igrd), kdimsz=kdimsz ) 
503               nblendta(igrd,ib_bdy) = MAXVAL(kdimsz)
504               jpbdtau = MAX(jpbdtau, MAXVAL(kdimsz))
505            END DO
506            CALL iom_close( inum )
507            !
508         ENDIF 
509         !
510      END DO ! ib_bdy
511
512      IF (nb_bdy>0) THEN
513         jpbdta = MAXVAL(nblendta(1:jpbgrd,1:nb_bdy))
514
515         ! Allocate arrays
516         !---------------
517         ALLOCATE( nbidta(jpbdta, jpbgrd, nb_bdy), nbjdta(jpbdta, jpbgrd, nb_bdy),    &
518            &      nbrdta(jpbdta, jpbgrd, nb_bdy) )
519         
520         jpk_max = MAXVAL(nb_jpk_bdy)
521         jpk_max = MAX(jpk_max, jpk)
522
523         ALLOCATE( dta_global(jpbdtau, 1, jpk_max) )
524         ALLOCATE( dta_global_z(jpbdtau, 1, jpk_max) ) ! needed ?? TODO
525         ALLOCATE( dta_global_dz(jpbdtau, 1, jpk_max) )! needed ?? TODO
526
527         IF ( icount>0 ) THEN
528            ALLOCATE( dta_global2(jpbdtas, nrimmax, jpk_max) )
529            ALLOCATE( dta_global2_z(jpbdtas, nrimmax, jpk_max) ) ! needed ?? TODO
530            ALLOCATE( dta_global2_dz(jpbdtas, nrimmax, jpk_max) )! needed ?? TODO 
531         ENDIF
532         !
533      ENDIF
534
535      ! Now look for crossings in user (namelist) defined open boundary segments:
536      !--------------------------------------------------------------------------
537      IF( icount>0 )   CALL bdy_ctl_seg
538
539      ! Calculate global boundary index arrays or read in from file
540      !------------------------------------------------------------               
541      ! 1. Read global index arrays from boundary coordinates file.
542      DO ib_bdy = 1, nb_bdy
543         !
544         IF( ln_coords_file(ib_bdy) ) THEN
545            !
546            CALL iom_open( cn_coords_file(ib_bdy), inum )
547            DO igrd = 1, jpbgrd
548               CALL iom_get( inum, jpdom_unknown, 'nbi'//cgrid(igrd), dta_global(1:nblendta(igrd,ib_bdy),:,1) )
549               DO ii = 1,nblendta(igrd,ib_bdy)
550                  nbidta(ii,igrd,ib_bdy) = INT( dta_global(ii,1,1) )
551               END DO
552               CALL iom_get( inum, jpdom_unknown, 'nbj'//cgrid(igrd), dta_global(1:nblendta(igrd,ib_bdy),:,1) )
553               DO ii = 1,nblendta(igrd,ib_bdy)
554                  nbjdta(ii,igrd,ib_bdy) = INT( dta_global(ii,1,1) )
555               END DO
556               CALL iom_get( inum, jpdom_unknown, 'nbr'//cgrid(igrd), dta_global(1:nblendta(igrd,ib_bdy),:,1) )
557               DO ii = 1,nblendta(igrd,ib_bdy)
558                  nbrdta(ii,igrd,ib_bdy) = INT( dta_global(ii,1,1) )
559               END DO
560               !
561               ibr_max = MAXVAL( nbrdta(:,igrd,ib_bdy) )
562               IF(lwp) WRITE(numout,*)
563               IF(lwp) WRITE(numout,*) ' Maximum rimwidth in file is ', ibr_max
564               IF(lwp) WRITE(numout,*) ' nn_rimwidth from namelist is ', nn_rimwidth(ib_bdy)
565               IF (ibr_max < nn_rimwidth(ib_bdy))   &
566                     CALL ctl_stop( 'nn_rimwidth is larger than maximum rimwidth in file',cn_coords_file(ib_bdy) )
567            END DO
568            CALL iom_close( inum )
569            !
570         ENDIF 
571         !
572      END DO     
573   
574      ! 2. Now fill indices corresponding to straight open boundary arrays:
575      ! East
576      !-----
577      DO iseg = 1, nbdysege
578         ib_bdy = npckge(iseg)
579         !
580         ! ------------ T points -------------
581         igrd=1
582         icount=0
583         DO ir = 1, nn_rimwidth(ib_bdy)
584            DO ij = jpjedt(iseg), jpjeft(iseg)
585               icount = icount + 1
586               nbidta(icount, igrd, ib_bdy) = jpieob(iseg) + 2 - ir
587               nbjdta(icount, igrd, ib_bdy) = ij
588               nbrdta(icount, igrd, ib_bdy) = ir
589            ENDDO
590         ENDDO
591         !
592         ! ------------ U points -------------
593         igrd=2
594         icount=0
595         DO ir = 1, nn_rimwidth(ib_bdy)
596            DO ij = jpjedt(iseg), jpjeft(iseg)
597               icount = icount + 1
598               nbidta(icount, igrd, ib_bdy) = jpieob(iseg) + 1 - ir
599               nbjdta(icount, igrd, ib_bdy) = ij
600               nbrdta(icount, igrd, ib_bdy) = ir
601            ENDDO
602         ENDDO
603         !
604         ! ------------ V points -------------
605         igrd=3
606         icount=0
607         DO ir = 1, nn_rimwidth(ib_bdy)
608!            DO ij = jpjedt(iseg), jpjeft(iseg) - 1
609            DO ij = jpjedt(iseg), jpjeft(iseg)
610               icount = icount + 1
611               nbidta(icount, igrd, ib_bdy) = jpieob(iseg) + 2 - ir
612               nbjdta(icount, igrd, ib_bdy) = ij
613               nbrdta(icount, igrd, ib_bdy) = ir
614            ENDDO
615            nbidta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
616            nbjdta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
617         ENDDO
618      ENDDO
619      !
620      ! West
621      !-----
622      DO iseg = 1, nbdysegw
623         ib_bdy = npckgw(iseg)
624         !
625         ! ------------ T points -------------
626         igrd=1
627         icount=0
628         DO ir = 1, nn_rimwidth(ib_bdy)
629            DO ij = jpjwdt(iseg), jpjwft(iseg)
630               icount = icount + 1
631               nbidta(icount, igrd, ib_bdy) = jpiwob(iseg) + ir - 1
632               nbjdta(icount, igrd, ib_bdy) = ij
633               nbrdta(icount, igrd, ib_bdy) = ir
634            ENDDO
635         ENDDO
636         !
637         ! ------------ U points -------------
638         igrd=2
639         icount=0
640         DO ir = 1, nn_rimwidth(ib_bdy)
641            DO ij = jpjwdt(iseg), jpjwft(iseg)
642               icount = icount + 1
643               nbidta(icount, igrd, ib_bdy) = jpiwob(iseg) + ir - 1
644               nbjdta(icount, igrd, ib_bdy) = ij
645               nbrdta(icount, igrd, ib_bdy) = ir
646            ENDDO
647         ENDDO
648         !
649         ! ------------ V points -------------
650         igrd=3
651         icount=0
652         DO ir = 1, nn_rimwidth(ib_bdy)
653!            DO ij = jpjwdt(iseg), jpjwft(iseg) - 1
654            DO ij = jpjwdt(iseg), jpjwft(iseg)
655               icount = icount + 1
656               nbidta(icount, igrd, ib_bdy) = jpiwob(iseg) + ir - 1
657               nbjdta(icount, igrd, ib_bdy) = ij
658               nbrdta(icount, igrd, ib_bdy) = ir
659            ENDDO
660            nbidta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
661            nbjdta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
662         ENDDO
663      ENDDO
664      !
665      ! North
666      !-----
667      DO iseg = 1, nbdysegn
668         ib_bdy = npckgn(iseg)
669         !
670         ! ------------ T points -------------
671         igrd=1
672         icount=0
673         DO ir = 1, nn_rimwidth(ib_bdy)
674            DO ii = jpindt(iseg), jpinft(iseg)
675               icount = icount + 1
676               nbidta(icount, igrd, ib_bdy) = ii
677               nbjdta(icount, igrd, ib_bdy) = jpjnob(iseg) + 2 - ir 
678               nbrdta(icount, igrd, ib_bdy) = ir
679            ENDDO
680         ENDDO
681         !
682         ! ------------ U points -------------
683         igrd=2
684         icount=0
685         DO ir = 1, nn_rimwidth(ib_bdy)
686!            DO ii = jpindt(iseg), jpinft(iseg) - 1
687            DO ii = jpindt(iseg), jpinft(iseg)
688               icount = icount + 1
689               nbidta(icount, igrd, ib_bdy) = ii
690               nbjdta(icount, igrd, ib_bdy) = jpjnob(iseg) + 2 - ir
691               nbrdta(icount, igrd, ib_bdy) = ir
692            ENDDO
693            nbidta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
694            nbjdta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
695         ENDDO
696         !
697         ! ------------ V points -------------
698         igrd=3
699         icount=0
700         DO ir = 1, nn_rimwidth(ib_bdy)
701            DO ii = jpindt(iseg), jpinft(iseg)
702               icount = icount + 1
703               nbidta(icount, igrd, ib_bdy) = ii
704               nbjdta(icount, igrd, ib_bdy) = jpjnob(iseg) + 1 - ir
705               nbrdta(icount, igrd, ib_bdy) = ir
706            ENDDO
707         ENDDO
708      ENDDO
709      !
710      ! South
711      !-----
712      DO iseg = 1, nbdysegs
713         ib_bdy = npckgs(iseg)
714         !
715         ! ------------ T points -------------
716         igrd=1
717         icount=0
718         DO ir = 1, nn_rimwidth(ib_bdy)
719            DO ii = jpisdt(iseg), jpisft(iseg)
720               icount = icount + 1
721               nbidta(icount, igrd, ib_bdy) = ii
722               nbjdta(icount, igrd, ib_bdy) = jpjsob(iseg) + ir - 1
723               nbrdta(icount, igrd, ib_bdy) = ir
724            ENDDO
725         ENDDO
726         !
727         ! ------------ U points -------------
728         igrd=2
729         icount=0
730         DO ir = 1, nn_rimwidth(ib_bdy)
731!            DO ii = jpisdt(iseg), jpisft(iseg) - 1
732            DO ii = jpisdt(iseg), jpisft(iseg)
733               icount = icount + 1
734               nbidta(icount, igrd, ib_bdy) = ii
735               nbjdta(icount, igrd, ib_bdy) = jpjsob(iseg) + ir - 1
736               nbrdta(icount, igrd, ib_bdy) = ir
737            ENDDO
738            nbidta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
739            nbjdta(icount, igrd, ib_bdy) = -ib_bdy ! Discount this point
740         ENDDO
741         !
742         ! ------------ V points -------------
743         igrd=3
744         icount=0
745         DO ir = 1, nn_rimwidth(ib_bdy)
746            DO ii = jpisdt(iseg), jpisft(iseg)
747               icount = icount + 1
748               nbidta(icount, igrd, ib_bdy) = ii
749               nbjdta(icount, igrd, ib_bdy) = jpjsob(iseg) + ir - 1
750               nbrdta(icount, igrd, ib_bdy) = ir
751            ENDDO
752         ENDDO
753      ENDDO
754
755      !  Deal with duplicated points
756      !-----------------------------
757      ! We assign negative indices to duplicated points (to remove them from bdy points to be updated)
758      ! if their distance to the bdy is greater than the other
759      ! If their distance are the same, just keep only one to avoid updating a point twice
760      DO igrd = 1, jpbgrd
761         DO ib_bdy1 = 1, nb_bdy
762            DO ib_bdy2 = 1, nb_bdy
763               IF (ib_bdy1/=ib_bdy2) THEN
764                  DO ib1 = 1, nblendta(igrd,ib_bdy1)
765                     DO ib2 = 1, nblendta(igrd,ib_bdy2)
766                        IF ((nbidta(ib1, igrd, ib_bdy1)==nbidta(ib2, igrd, ib_bdy2)).AND. &
767                        &   (nbjdta(ib1, igrd, ib_bdy1)==nbjdta(ib2, igrd, ib_bdy2))) THEN
768!                           IF ((lwp).AND.(igrd==1)) WRITE(numout,*) ' found coincident point ji, jj:', &
769!                                                       &              nbidta(ib1, igrd, ib_bdy1),      &
770!                                                       &              nbjdta(ib2, igrd, ib_bdy2)
771                           ! keep only points with the lowest distance to boundary:
772                           IF (nbrdta(ib1, igrd, ib_bdy1)<nbrdta(ib2, igrd, ib_bdy2)) THEN
773                             nbidta(ib2, igrd, ib_bdy2) =-ib_bdy2
774                             nbjdta(ib2, igrd, ib_bdy2) =-ib_bdy2
775                           ELSEIF (nbrdta(ib1, igrd, ib_bdy1)>nbrdta(ib2, igrd, ib_bdy2)) THEN
776                             nbidta(ib1, igrd, ib_bdy1) =-ib_bdy1
777                             nbjdta(ib1, igrd, ib_bdy1) =-ib_bdy1
778                           ! Arbitrary choice if distances are the same:
779                           ELSE
780                             nbidta(ib1, igrd, ib_bdy1) =-ib_bdy1
781                             nbjdta(ib1, igrd, ib_bdy1) =-ib_bdy1
782                           ENDIF
783                        END IF
784                     END DO
785                  END DO
786               ENDIF
787            END DO
788         END DO
789      END DO
790
791      ! Work out dimensions of boundary data on each processor
792      ! ------------------------------------------------------
793
794      ! Rather assume that boundary data indices are given on global domain
795      ! TO BE DISCUSSED ?
796!      iw = mig(1) + 1            ! if monotasking and no zoom, iw=2
797!      ie = mig(1) + nlci-1 - 1   ! if monotasking and no zoom, ie=jpim1
798!      is = mjg(1) + 1            ! if monotasking and no zoom, is=2
799!      in = mjg(1) + nlcj-1 - 1   ! if monotasking and no zoom, in=jpjm1     
800      iwe = mig(1) - 1 + 2         ! if monotasking and no zoom, iw=2
801      ies = mig(1) + nlci-1 - 1  ! if monotasking and no zoom, ie=jpim1
802      iso = mjg(1) - 1 + 2         ! if monotasking and no zoom, is=2
803      ino = mjg(1) + nlcj-1 - 1  ! if monotasking and no zoom, in=jpjm1
804
805      ALLOCATE( nbondi_bdy(nb_bdy))
806      ALLOCATE( nbondj_bdy(nb_bdy))
807      nbondi_bdy(:)=2
808      nbondj_bdy(:)=2
809      ALLOCATE( nbondi_bdy_b(nb_bdy))
810      ALLOCATE( nbondj_bdy_b(nb_bdy))
811      nbondi_bdy_b(:)=2
812      nbondj_bdy_b(:)=2
813
814      ! Work out dimensions of boundary data on each neighbour process
815      IF(nbondi == 0) THEN
816         iw_b(1) = 1 + nimppt(nowe+1)
817         ie_b(1) = 1 + nimppt(nowe+1)+nlcit(nowe+1)-3
818         is_b(1) = 1 + njmppt(nowe+1)
819         in_b(1) = 1 + njmppt(nowe+1)+nlcjt(nowe+1)-3
820
821         iw_b(2) = 1 + nimppt(noea+1)
822         ie_b(2) = 1 + nimppt(noea+1)+nlcit(noea+1)-3
823         is_b(2) = 1 + njmppt(noea+1)
824         in_b(2) = 1 + njmppt(noea+1)+nlcjt(noea+1)-3
825      ELSEIF(nbondi == 1) THEN
826         iw_b(1) = 1 + nimppt(nowe+1)
827         ie_b(1) = 1 + nimppt(nowe+1)+nlcit(nowe+1)-3
828         is_b(1) = 1 + njmppt(nowe+1)
829         in_b(1) = 1 + njmppt(nowe+1)+nlcjt(nowe+1)-3
830      ELSEIF(nbondi == -1) THEN
831         iw_b(2) = 1 + nimppt(noea+1)
832         ie_b(2) = 1 + nimppt(noea+1)+nlcit(noea+1)-3
833         is_b(2) = 1 + njmppt(noea+1)
834         in_b(2) = 1 + njmppt(noea+1)+nlcjt(noea+1)-3
835      ENDIF
836
837      IF(nbondj == 0) THEN
838         iw_b(3) = 1 + nimppt(noso+1)
839         ie_b(3) = 1 + nimppt(noso+1)+nlcit(noso+1)-3
840         is_b(3) = 1 + njmppt(noso+1)
841         in_b(3) = 1 + njmppt(noso+1)+nlcjt(noso+1)-3
842
843         iw_b(4) = 1 + nimppt(nono+1)
844         ie_b(4) = 1 + nimppt(nono+1)+nlcit(nono+1)-3
845         is_b(4) = 1 + njmppt(nono+1)
846         in_b(4) = 1 + njmppt(nono+1)+nlcjt(nono+1)-3
847      ELSEIF(nbondj == 1) THEN
848         iw_b(3) = 1 + nimppt(noso+1)
849         ie_b(3) = 1 + nimppt(noso+1)+nlcit(noso+1)-3
850         is_b(3) = 1 + njmppt(noso+1)
851         in_b(3) = 1 + njmppt(noso+1)+nlcjt(noso+1)-3
852      ELSEIF(nbondj == -1) THEN
853         iw_b(4) = 1 + nimppt(nono+1)
854         ie_b(4) = 1 + nimppt(nono+1)+nlcit(nono+1)-3
855         is_b(4) = 1 + njmppt(nono+1)
856         in_b(4) = 1 + njmppt(nono+1)+nlcjt(nono+1)-3
857      ENDIF
858
859      DO ib_bdy = 1, nb_bdy
860         DO igrd = 1, jpbgrd
861            icount  = 0
862            icountr = 0
863            idx_bdy(ib_bdy)%nblen(igrd)    = 0
864            idx_bdy(ib_bdy)%nblenrim(igrd) = 0
865            DO ib = 1, nblendta(igrd,ib_bdy)
866               ! check that data is in correct order in file
867               ibm1 = MAX(1,ib-1)
868               IF(lwp) THEN         ! Since all procs read global data only need to do this check on one proc...
869                  IF( nbrdta(ib,igrd,ib_bdy) < nbrdta(ibm1,igrd,ib_bdy) ) THEN
870                     CALL ctl_stop('bdy_segs : ERROR : boundary data in file must be defined ', &
871                          &        ' in order of distance from edge nbr A utility for re-ordering ', &
872                          &        ' boundary coordinates and data files exists in the TOOLS/OBC directory')
873                  ENDIF   
874               ENDIF
875               ! check if point is in local domain
876               IF(  nbidta(ib,igrd,ib_bdy) >= iwe .AND. nbidta(ib,igrd,ib_bdy) <= ies .AND.   &
877                  & nbjdta(ib,igrd,ib_bdy) >= iso .AND. nbjdta(ib,igrd,ib_bdy) <= ino      ) THEN
878                  !
879                  icount = icount  + 1
880                  !
881                  IF( nbrdta(ib,igrd,ib_bdy) == 1 )   icountr = icountr+1
882               ENDIF
883            END DO
884            idx_bdy(ib_bdy)%nblenrim(igrd) = icountr !: length of rim boundary data on each proc
885            idx_bdy(ib_bdy)%nblen   (igrd) = icount  !: length of boundary data on each proc       
886         END DO  ! igrd
887
888         ! Allocate index arrays for this boundary set
889         !--------------------------------------------
890         ilen1 = MAXVAL( idx_bdy(ib_bdy)%nblen(:) )
891         ALLOCATE( idx_bdy(ib_bdy)%nbi   (ilen1,jpbgrd) ,   &
892            &      idx_bdy(ib_bdy)%nbj   (ilen1,jpbgrd) ,   &
893            &      idx_bdy(ib_bdy)%nbr   (ilen1,jpbgrd) ,   &
894            &      idx_bdy(ib_bdy)%nbd   (ilen1,jpbgrd) ,   &
895            &      idx_bdy(ib_bdy)%nbdout(ilen1,jpbgrd) ,   &
896            &      idx_bdy(ib_bdy)%ntreat(ilen1,jpbgrd) ,   &
897            &      idx_bdy(ib_bdy)%nbmap (ilen1,jpbgrd) ,   &
898            &      idx_bdy(ib_bdy)%nbw   (ilen1,jpbgrd) ,   &
899            &      idx_bdy(ib_bdy)%flagu (ilen1,jpbgrd) ,   &
900            &      idx_bdy(ib_bdy)%flagv (ilen1,jpbgrd) )
901
902         ! Dispatch mapping indices and discrete distances on each processor
903         ! -----------------------------------------------------------------
904
905         com_east  = 0
906         com_west  = 0
907         com_south = 0
908         com_north = 0
909
910         com_east_b  = 0
911         com_west_b  = 0
912         com_south_b = 0
913         com_north_b = 0
914
915         DO igrd = 1, jpbgrd
916            icount  = 0
917            ! Loop on rimwidth to ensure outermost points come first in the local arrays.
918            DO ir=1, nn_rimwidth(ib_bdy)
919               DO ib = 1, nblendta(igrd,ib_bdy)
920                  ! check if point is in local domain and equals ir
921                  IF(  nbidta(ib,igrd,ib_bdy) >= iwe .AND. nbidta(ib,igrd,ib_bdy) <= ies .AND.   &
922                     & nbjdta(ib,igrd,ib_bdy) >= iso .AND. nbjdta(ib,igrd,ib_bdy) <= ino .AND.   &
923                     & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
924                     !
925                     icount = icount  + 1
926
927                     ! Rather assume that boundary data indices are given on global domain
928                     ! TO BE DISCUSSED ?
929!                     idx_bdy(ib_bdy)%nbi(icount,igrd)   = nbidta(ib,igrd,ib_bdy)- mig(1)+1
930!                     idx_bdy(ib_bdy)%nbj(icount,igrd)   = nbjdta(ib,igrd,ib_bdy)- mjg(1)+1
931                     idx_bdy(ib_bdy)%nbi(icount,igrd)   = nbidta(ib,igrd,ib_bdy)- mig(1)+1
932                     idx_bdy(ib_bdy)%nbj(icount,igrd)   = nbjdta(ib,igrd,ib_bdy)- mjg(1)+1
933                     ! check if point has to be sent
934                     ii = idx_bdy(ib_bdy)%nbi(icount,igrd)
935                     ij = idx_bdy(ib_bdy)%nbj(icount,igrd)
936                     if((com_east .ne. 1) .and. (ii == (nlci-1)) .and. (nbondi .le. 0)) then
937                        com_east = 1
938                     elseif((com_west .ne. 1) .and. (ii == 2) .and. (nbondi .ge. 0) .and. (nbondi .ne. 2)) then
939                        com_west = 1
940                     endif
941                     if((com_south .ne. 1) .and. (ij == 2) .and. (nbondj .ge. 0) .and. (nbondj .ne. 2)) then
942                        com_south = 1
943                     elseif((com_north .ne. 1) .and. (ij == (nlcj-1)) .and. (nbondj .le. 0)) then
944                        com_north = 1
945                     endif
946                     idx_bdy(ib_bdy)%nbr(icount,igrd)   = nbrdta(ib,igrd,ib_bdy)
947                     idx_bdy(ib_bdy)%nbmap(icount,igrd) = ib
948                  ENDIF
949                  ! check if point has to be received from a neighbour
950                  IF(nbondi == 0) THEN
951                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(1) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(1) .AND.   &
952                       & nbjdta(ib,igrd,ib_bdy) >= is_b(1) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(1) .AND.   &
953                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
954                       ii = nbidta(ib,igrd,ib_bdy)- iw_b(1)+2
955                       if( ii == (nlcit(nowe+1)-1) ) then
956                          ij = nbjdta(ib,igrd,ib_bdy) - is_b(1)+2
957                          if((ij == 2) .and. (nbondj == 0 .or. nbondj == 1)) then
958                            com_south = 1
959                          elseif((ij == nlcjt(nowe+1)-1) .and. (nbondj == 0 .or. nbondj == -1)) then
960                            com_north = 1
961                          endif
962                          com_west_b = 1
963                       endif
964                     ENDIF
965                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(2) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(2) .AND.   &
966                       & nbjdta(ib,igrd,ib_bdy) >= is_b(2) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(2) .AND.   &
967                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
968                       ii = nbidta(ib,igrd,ib_bdy)- iw_b(2)+2
969                       if( ii == 2 ) then
970                          ij = nbjdta(ib,igrd,ib_bdy) - is_b(2)+2
971                          if((ij == 2) .and. (nbondj == 0 .or. nbondj == 1)) then
972                            com_south = 1
973                          elseif((ij == nlcjt(noea+1)-1) .and. (nbondj == 0 .or. nbondj == -1)) then
974                            com_north = 1
975                          endif
976                          com_east_b = 1
977                       endif
978                     ENDIF
979                  ELSEIF(nbondi == 1) THEN
980                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(1) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(1) .AND.   &
981                       & nbjdta(ib,igrd,ib_bdy) >= is_b(1) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(1) .AND.   &
982                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
983                       ii = nbidta(ib,igrd,ib_bdy)- iw_b(1)+2
984                       if( ii == (nlcit(nowe+1)-1) ) then
985                          ij = nbjdta(ib,igrd,ib_bdy) - is_b(1)+2
986                          if((ij == 2) .and. (nbondj == 0 .or. nbondj == 1)) then
987                            com_south = 1
988                          elseif((ij == nlcjt(nowe+1)-1) .and. (nbondj == 0 .or. nbondj == -1)) then
989                            com_north = 1
990                          endif
991                          com_west_b = 1
992                       endif
993                     ENDIF
994                  ELSEIF(nbondi == -1) THEN
995                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(2) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(2) .AND.   &
996                       & nbjdta(ib,igrd,ib_bdy) >= is_b(2) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(2) .AND.   &
997                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
998                       ii = nbidta(ib,igrd,ib_bdy)- iw_b(2)+2
999                       if( ii == 2 ) then
1000                          ij = nbjdta(ib,igrd,ib_bdy) - is_b(2)+2
1001                          if((ij == 2) .and. (nbondj == 0 .or. nbondj == 1)) then
1002                            com_south = 1
1003                          elseif((ij == nlcjt(noea+1)-1) .and. (nbondj == 0 .or. nbondj == -1)) then
1004                            com_north = 1
1005                          endif
1006                          com_east_b = 1
1007                       endif
1008                     ENDIF
1009                  ENDIF
1010                  IF(nbondj == 0) THEN
1011                     IF(com_north_b .ne. 1 .AND. (nbidta(ib,igrd,ib_bdy) == iw_b(4)-1  &
1012                       & .OR. nbidta(ib,igrd,ib_bdy) == ie_b(4)+1) .AND. &
1013                       & nbjdta(ib,igrd,ib_bdy) == is_b(4) .AND. nbrdta(ib,igrd,ib_bdy) == ir) THEN
1014                       com_north_b = 1 
1015                     ENDIF
1016                     IF(com_south_b .ne. 1 .AND. (nbidta(ib,igrd,ib_bdy) == iw_b(3)-1  &
1017                       &.OR. nbidta(ib,igrd,ib_bdy) == ie_b(3)+1) .AND. &
1018                       & nbjdta(ib,igrd,ib_bdy) == in_b(3) .AND. nbrdta(ib,igrd,ib_bdy) == ir) THEN
1019                       com_south_b = 1 
1020                     ENDIF
1021                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(3) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(3) .AND.   &
1022                       & nbjdta(ib,igrd,ib_bdy) >= is_b(3) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(3) .AND.   &
1023                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
1024                       ij = nbjdta(ib,igrd,ib_bdy)- is_b(3)+2
1025                       if((com_south_b .ne. 1) .and. (ij == (nlcjt(noso+1)-1))) then
1026                          com_south_b = 1
1027                       endif
1028                     ENDIF
1029                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(4) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(4) .AND.   &
1030                       & nbjdta(ib,igrd,ib_bdy) >= is_b(4) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(4) .AND.   &
1031                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
1032                       ij = nbjdta(ib,igrd,ib_bdy)- is_b(4)+2
1033                       if((com_north_b .ne. 1) .and. (ij == 2)) then
1034                          com_north_b = 1
1035                       endif
1036                     ENDIF
1037                  ELSEIF(nbondj == 1) THEN
1038                     IF( com_south_b .ne. 1 .AND. (nbidta(ib,igrd,ib_bdy) == iw_b(3)-1 .OR. &
1039                       & nbidta(ib,igrd,ib_bdy) == ie_b(3)+1) .AND. &
1040                       & nbjdta(ib,igrd,ib_bdy) == in_b(3) .AND. nbrdta(ib,igrd,ib_bdy) == ir) THEN
1041                       com_south_b = 1 
1042                     ENDIF
1043                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(3) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(3) .AND.   &
1044                       & nbjdta(ib,igrd,ib_bdy) >= is_b(3) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(3) .AND.   &
1045                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
1046                       ij = nbjdta(ib,igrd,ib_bdy)- is_b(3)+2
1047                       if((com_south_b .ne. 1) .and. (ij == (nlcjt(noso+1)-1))) then
1048                          com_south_b = 1
1049                       endif
1050                     ENDIF
1051                  ELSEIF(nbondj == -1) THEN
1052                     IF(com_north_b .ne. 1 .AND. (nbidta(ib,igrd,ib_bdy) == iw_b(4)-1  &
1053                       & .OR. nbidta(ib,igrd,ib_bdy) == ie_b(4)+1) .AND. &
1054                       & nbjdta(ib,igrd,ib_bdy) == is_b(4) .AND. nbrdta(ib,igrd,ib_bdy) == ir) THEN
1055                       com_north_b = 1 
1056                     ENDIF
1057                     IF( nbidta(ib,igrd,ib_bdy) >= iw_b(4) .AND. nbidta(ib,igrd,ib_bdy) <= ie_b(4) .AND.   &
1058                       & nbjdta(ib,igrd,ib_bdy) >= is_b(4) .AND. nbjdta(ib,igrd,ib_bdy) <= in_b(4) .AND.   &
1059                       & nbrdta(ib,igrd,ib_bdy) == ir  ) THEN
1060                       ij = nbjdta(ib,igrd,ib_bdy)- is_b(4)+2
1061                       if((com_north_b .ne. 1) .and. (ij == 2)) then
1062                          com_north_b = 1
1063                       endif
1064                     ENDIF
1065                  ENDIF
1066               ENDDO
1067            ENDDO
1068         ENDDO 
1069
1070         ! definition of the i- and j- direction local boundaries arrays used for sending the boundaries
1071         IF(     (com_east  == 1) .and. (com_west  == 1) ) THEN   ;   nbondi_bdy(ib_bdy) =  0
1072         ELSEIF( (com_east  == 1) .and. (com_west  == 0) ) THEN   ;   nbondi_bdy(ib_bdy) = -1
1073         ELSEIF( (com_east  == 0) .and. (com_west  == 1) ) THEN   ;   nbondi_bdy(ib_bdy) =  1
1074         ENDIF
1075         IF(     (com_north == 1) .and. (com_south == 1) ) THEN   ;   nbondj_bdy(ib_bdy) =  0
1076         ELSEIF( (com_north == 1) .and. (com_south == 0) ) THEN   ;   nbondj_bdy(ib_bdy) = -1
1077         ELSEIF( (com_north == 0) .and. (com_south == 1) ) THEN   ;   nbondj_bdy(ib_bdy) =  1
1078         ENDIF
1079
1080         ! definition of the i- and j- direction local boundaries arrays used for receiving the boundaries
1081         IF(     (com_east_b  == 1) .and. (com_west_b  == 1) ) THEN   ;   nbondi_bdy_b(ib_bdy) =  0
1082         ELSEIF( (com_east_b  == 1) .and. (com_west_b  == 0) ) THEN   ;   nbondi_bdy_b(ib_bdy) = -1
1083         ELSEIF( (com_east_b  == 0) .and. (com_west_b  == 1) ) THEN   ;   nbondi_bdy_b(ib_bdy) =  1
1084         ENDIF
1085         IF(     (com_north_b == 1) .and. (com_south_b == 1) ) THEN   ;   nbondj_bdy_b(ib_bdy) =  0
1086         ELSEIF( (com_north_b == 1) .and. (com_south_b == 0) ) THEN   ;   nbondj_bdy_b(ib_bdy) = -1
1087         ELSEIF( (com_north_b == 0) .and. (com_south_b == 1) ) THEN   ;   nbondj_bdy_b(ib_bdy) =  1
1088         ENDIF
1089
1090         ! Compute rim weights for FRS scheme
1091         ! ----------------------------------
1092         DO igrd = 1, jpbgrd
1093            DO ib = 1, idx_bdy(ib_bdy)%nblen(igrd)
1094               nbr => idx_bdy(ib_bdy)%nbr(ib,igrd)
1095               idx_bdy(ib_bdy)%nbw(ib,igrd) = 1.- TANH( REAL( nbr - 1 ) *0.5 )      ! tanh formulation
1096!               idx_bdy(ib_bdy)%nbw(ib,igrd) = (REAL(nn_rimwidth(ib_bdy)+1-nbr)/REAL(nn_rimwidth(ib_bdy)))**2.  ! quadratic
1097!               idx_bdy(ib_bdy)%nbw(ib,igrd) =  REAL(nn_rimwidth(ib_bdy)+1-nbr)/REAL(nn_rimwidth(ib_bdy))       ! linear
1098            END DO
1099         END DO 
1100
1101         ! Compute damping coefficients
1102         ! ----------------------------
1103         DO igrd = 1, jpbgrd
1104            DO ib = 1, idx_bdy(ib_bdy)%nblen(igrd)
1105               nbr => idx_bdy(ib_bdy)%nbr(ib,igrd)
1106               idx_bdy(ib_bdy)%nbd(ib,igrd) = 1. / ( rn_time_dmp(ib_bdy) * rday ) & 
1107               & *(REAL(nn_rimwidth(ib_bdy)+1-nbr)/REAL(nn_rimwidth(ib_bdy)))**2.   ! quadratic
1108               idx_bdy(ib_bdy)%nbdout(ib,igrd) = 1. / ( rn_time_dmp_out(ib_bdy) * rday ) & 
1109               & *(REAL(nn_rimwidth(ib_bdy)+1-nbr)/REAL(nn_rimwidth(ib_bdy)))**2.   ! quadratic
1110            END DO
1111         END DO
1112
1113      END DO
1114
1115      ! ------------------------------------------------------
1116      ! Initialise masks and find normal/tangential directions
1117      ! ------------------------------------------------------
1118
1119      ! Read global 2D mask at T-points: bdytmask
1120      ! -----------------------------------------
1121      ! bdytmask = 1  on the computational domain AND on open boundaries
1122      !          = 0  elsewhere   
1123 
1124      bdytmask(:,:) = ssmask(:,:)
1125
1126      ! Derive mask on U and V grid from mask on T grid
1127
1128      bdyumask(:,:) = 0._wp
1129      bdyvmask(:,:) = 0._wp
1130      DO ij = 1, jpjm1
1131         DO ii = 1, jpim1
1132            bdyumask(ii,ij) = bdytmask(ii,ij) * bdytmask(ii+1, ij )
1133            bdyvmask(ii,ij) = bdytmask(ii,ij) * bdytmask(ii  ,ij+1) 
1134         END DO
1135      END DO
1136      CALL lbc_lnk_multi( 'bdyini', bdyumask, 'U', 1. , bdyvmask, 'V', 1. )   ! Lateral boundary cond.
1137
1138      ! bdy masks are now set to zero on boundary points:
1139      !
1140      igrd = 1
1141      DO ib_bdy = 1, nb_bdy
1142        DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)     
1143          bdytmask(idx_bdy(ib_bdy)%nbi(ib,igrd), idx_bdy(ib_bdy)%nbj(ib,igrd)) = 0._wp
1144        END DO
1145      END DO
1146      !
1147      igrd = 2
1148      DO ib_bdy = 1, nb_bdy
1149        DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
1150          bdyumask(idx_bdy(ib_bdy)%nbi(ib,igrd), idx_bdy(ib_bdy)%nbj(ib,igrd)) = 0._wp
1151        END DO
1152      END DO
1153      !
1154      igrd = 3
1155      DO ib_bdy = 1, nb_bdy
1156        DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
1157          bdyvmask(idx_bdy(ib_bdy)%nbi(ib,igrd), idx_bdy(ib_bdy)%nbj(ib,igrd)) = 0._wp
1158        END DO
1159      END DO
1160
1161      ! For the flagu/flagv calculation below we require a version of fmask without
1162      ! the land boundary condition (shlat) included:
1163      zfmask(:,:) = 0
1164      DO ij = 2, jpjm1
1165         DO ii = 2, jpim1
1166            zfmask(ii,ij) = tmask(ii,ij  ,1) * tmask(ii+1,ij  ,1)   &
1167           &              * tmask(ii,ij+1,1) * tmask(ii+1,ij+1,1)
1168         END DO     
1169      END DO
1170
1171      ! Lateral boundary conditions
1172      CALL lbc_lnk( 'bdyini', zfmask, 'F', 1. ) 
1173      CALL lbc_lnk_multi( 'bdyini', bdyumask, 'U', 1. , bdyvmask, 'V', 1., bdytmask, 'T', 1. )
1174      DO ib_bdy = 1, nb_bdy       ! Indices and directions of rim velocity components
1175
1176         idx_bdy(ib_bdy)%flagu(:,:) = 0._wp
1177         idx_bdy(ib_bdy)%flagv(:,:) = 0._wp
1178         icount = 0 
1179
1180         ! Calculate relationship of U direction to the local orientation of the boundary
1181         ! flagu = -1 : u component is normal to the dynamical boundary and its direction is outward
1182         ! flagu =  0 : u is tangential
1183         ! flagu =  1 : u is normal to the boundary and is direction is inward
1184 
1185         DO igrd = 1, jpbgrd 
1186            SELECT CASE( igrd )
1187               CASE( 1 )   ;   pmask => umask   (:,:,1)   ;   i_offset = 0
1188               CASE( 2 )   ;   pmask => bdytmask(:,:)     ;   i_offset = 1
1189               CASE( 3 )   ;   pmask => zfmask  (:,:)     ;   i_offset = 0
1190            END SELECT
1191            icount = 0
1192            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd) 
1193               nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
1194               nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
1195               zefl = pmask(nbi+i_offset-1,nbj)
1196               zwfl = pmask(nbi+i_offset,nbj)
1197               ! This error check only works if you are using the bdyXmask arrays
1198               IF( i_offset == 1 .and. zefl + zwfl == 2 ) THEN
1199                  icount = icount + 1
1200                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(nbi),mjg(nbj)
1201               ELSE
1202                  idx_bdy(ib_bdy)%flagu(ib,igrd) = -zefl + zwfl
1203               ENDIF
1204            END DO
1205            IF( icount /= 0 ) THEN
1206               WRITE(ctmp1,*) ' E R R O R : Some ',cgrid(igrd),' grid points,',   &
1207                  ' are not boundary points (flagu calculation). Check nbi, nbj, indices for boundary set ',ib_bdy
1208               WRITE(ctmp2,*) ' ========== '
1209               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1210            ENDIF
1211         END DO
1212
1213         ! Calculate relationship of V direction to the local orientation of the boundary
1214         ! flagv = -1 : v component is normal to the dynamical boundary but its direction is outward
1215         ! flagv =  0 : v is tangential
1216         ! flagv =  1 : v is normal to the boundary and is direction is inward
1217
1218         DO igrd = 1, jpbgrd 
1219            SELECT CASE( igrd )
1220               CASE( 1 )   ;   pmask => vmask (:,:,1)   ;   j_offset = 0
1221               CASE( 2 )   ;   pmask => zfmask(:,:)     ;   j_offset = 0
1222               CASE( 3 )   ;   pmask => bdytmask        ;   j_offset = 1
1223            END SELECT
1224            icount = 0
1225            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd) 
1226               nbi => idx_bdy(ib_bdy)%nbi(ib,igrd)
1227               nbj => idx_bdy(ib_bdy)%nbj(ib,igrd)
1228               znfl = pmask(nbi,nbj+j_offset-1)
1229               zsfl = pmask(nbi,nbj+j_offset  )
1230               ! This error check only works if you are using the bdyXmask arrays
1231               IF( j_offset == 1 .and. znfl + zsfl == 2 ) THEN
1232                  IF(lwp) WRITE(numout,*) 'Problem with igrd = ',igrd,' at (global) nbi, nbj : ',mig(nbi),mjg(nbj)
1233                  icount = icount + 1
1234               ELSE
1235                  idx_bdy(ib_bdy)%flagv(ib,igrd) = -znfl + zsfl
1236               END IF
1237            END DO
1238            IF( icount /= 0 ) THEN
1239               WRITE(ctmp1,*) ' E R R O R : Some ',cgrid(igrd),' grid points,',   &
1240                  ' are not boundary points (flagv calculation). Check nbi, nbj, indices for boundary set ',ib_bdy
1241               WRITE(ctmp2,*) ' ========== '
1242               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1243            ENDIF
1244         END DO
1245         !
1246      END DO
1247     
1248      ! detect corner interior and its orientation index 1 to 4  depending on the orientation
1249      ! detect corner exterior and its orientation index 5 to 8  depending on the orientation
1250      ! detect geometries with 3 neighbours        index 9 to 12 depending on the orientation
1251      ! else                                       index 0
1252      DO ib_bdy = 1, nb_bdy
1253         DO igrd = 1, jpbgrd
1254            SELECT CASE( igrd )
1255               CASE( 1 )   ;   pmask => bdytmask 
1256               CASE( 2 )   ;   pmask => bdyumask 
1257               CASE( 3 )   ;   pmask => bdyvmask 
1258            END SELECT
1259            DO ib = 1, idx_bdy(ib_bdy)%nblenrim(igrd)
1260               ii        =  idx_bdy(ib_bdy)%nbi(ib,igrd)
1261               ij        =  idx_bdy(ib_bdy)%nbj(ib,igrd)
1262               llnobdy = pmask(ii  ,ij+1) == 1. 
1263               llsobdy = pmask(ii  ,ij-1) == 1. 
1264               lleabdy = pmask(ii+1,ij  ) == 1. 
1265               llwebdy = pmask(ii-1,ij  ) == 1. 
1266               inbdy  = COUNT( (/ llnobdy, llsobdy, lleabdy, llwebdy /) )
1267               IF( inbdy == 0 )   THEN   ! no neighbours -> interior of a corner
1268                  !               !              !     _____     !     _____     
1269                  !  1 |   o      !  2  o   |    !  3 | x        !  4     x |   
1270                  !    |_x_ _     !    _ _x_|    !    |   o      !      o   |   
1271                  IF( pmask(ii+1,ij+1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 1
1272                  IF( pmask(ii-1,ij+1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 2
1273                  IF( pmask(ii+1,ij-1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 3
1274                  IF( pmask(ii-1,ij-1) == 1. )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 4
1275               END IF
1276               IF( inbdy == 1 )   THEN   ! middle of linear bdy
1277                  idx_bdy(ib_bdy)%ntreat(ib,igrd) = 0   ! regular treatment with flags
1278               END IF
1279               IF( inbdy == 2 )   THEN   ! exterior of a corner
1280                  !        o      !        o      !    _____|       !       |_____ 
1281                  !  5 ____x o    !  6   o x___   ! 7      x o      !  8   o x     
1282                  !         |     !       |       !        o        !        o
1283                  IF( llnobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 5
1284                  IF( llnobdy .AND. llwebdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 6
1285                  IF( llsobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 7
1286                  IF( llsobdy .AND. llwebdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 8
1287               END IF
1288               IF( inbdy == 3 )   THEN   ! 3 neighbours __   __
1289                  !    |_  o      !        o  _|  !       |_|     !       o         
1290                  !  9  _| x o    ! 10   o x |_   ! 11   o x o    ! 12  o x o       
1291                  !    |   o      !        o   |  !        o      !    __|¨|__   
1292                  IF( llnobdy .AND. lleabdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 9
1293                  IF( llnobdy .AND. llwebdy .AND. llsobdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 10
1294                  IF( llwebdy .AND. llsobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 11
1295                  IF( llwebdy .AND. llnobdy .AND. lleabdy )   idx_bdy(ib_bdy)%ntreat(ib,igrd) = 12
1296               END IF
1297               IF( inbdy == 4 )   THEN
1298                  WRITE(ctmp1,*) ' E R R O R : Problem with  ',cgrid(igrd) ,' grid points,',   &
1299                       '  some points on boundary set ', ib_bdy, ' have 4 neighbours'
1300                  WRITE(ctmp2,*) ' ========== '
1301                  CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1302               END IF
1303            END DO
1304         END DO
1305      END DO
1306      !
1307      ! Tidy up
1308      !--------
1309      IF( nb_bdy>0 )   DEALLOCATE( nbidta, nbjdta, nbrdta )
1310      !
1311   END SUBROUTINE bdy_segs
1312
1313   SUBROUTINE bdy_ctl_seg
1314      !!----------------------------------------------------------------------
1315      !!                 ***  ROUTINE bdy_ctl_seg  ***
1316      !!
1317      !! ** Purpose :   Check straight open boundary segments location
1318      !!
1319      !! ** Method  :   - Look for open boundary corners
1320      !!                - Check that segments start or end on land
1321      !!----------------------------------------------------------------------
1322      INTEGER  ::   ib, ib1, ib2, ji ,jj, itest 
1323      INTEGER, DIMENSION(jp_nseg,2) :: icorne, icornw, icornn, icorns 
1324      REAL(wp), DIMENSION(2) ::   ztestmask
1325      !!----------------------------------------------------------------------
1326      !
1327      IF (lwp) WRITE(numout,*) ' '
1328      IF (lwp) WRITE(numout,*) 'bdy_ctl_seg: Check analytical segments'
1329      IF (lwp) WRITE(numout,*) '~~~~~~~~~~~~'
1330      !
1331      IF(lwp) WRITE(numout,*) 'Number of east  segments     : ', nbdysege
1332      IF(lwp) WRITE(numout,*) 'Number of west  segments     : ', nbdysegw
1333      IF(lwp) WRITE(numout,*) 'Number of north segments     : ', nbdysegn
1334      IF(lwp) WRITE(numout,*) 'Number of south segments     : ', nbdysegs
1335      ! 1. Check bounds
1336      !----------------
1337      DO ib = 1, nbdysegn
1338         IF (lwp) WRITE(numout,*) '**check north seg bounds pckg: ', npckgn(ib)
1339         IF ((jpjnob(ib).ge.jpjglo-1).or.& 
1340            &(jpjnob(ib).le.1))        CALL ctl_stop( 'nbdyind out of domain' )
1341         IF (jpindt(ib).ge.jpinft(ib)) CALL ctl_stop( 'Bdy start index is greater than end index' )
1342         IF (jpindt(ib).le.1     )     CALL ctl_stop( 'Start index out of domain' )
1343         IF (jpinft(ib).ge.jpiglo)     CALL ctl_stop( 'End index out of domain' )
1344      END DO
1345      !
1346      DO ib = 1, nbdysegs
1347         IF (lwp) WRITE(numout,*) '**check south seg bounds pckg: ', npckgs(ib)
1348         IF ((jpjsob(ib).ge.jpjglo-1).or.& 
1349            &(jpjsob(ib).le.1))        CALL ctl_stop( 'nbdyind out of domain' )
1350         IF (jpisdt(ib).ge.jpisft(ib)) CALL ctl_stop( 'Bdy start index is greater than end index' )
1351         IF (jpisdt(ib).le.1     )     CALL ctl_stop( 'Start index out of domain' )
1352         IF (jpisft(ib).ge.jpiglo)     CALL ctl_stop( 'End index out of domain' )
1353      END DO
1354      !
1355      DO ib = 1, nbdysege
1356         IF (lwp) WRITE(numout,*) '**check east  seg bounds pckg: ', npckge(ib)
1357         IF ((jpieob(ib).ge.jpiglo-1).or.& 
1358            &(jpieob(ib).le.1))        CALL ctl_stop( 'nbdyind out of domain' )
1359         IF (jpjedt(ib).ge.jpjeft(ib)) CALL ctl_stop( 'Bdy start index is greater than end index' )
1360         IF (jpjedt(ib).le.1     )     CALL ctl_stop( 'Start index out of domain' )
1361         IF (jpjeft(ib).ge.jpjglo)     CALL ctl_stop( 'End index out of domain' )
1362      END DO
1363      !
1364      DO ib = 1, nbdysegw
1365         IF (lwp) WRITE(numout,*) '**check west  seg bounds pckg: ', npckgw(ib)
1366         IF ((jpiwob(ib).ge.jpiglo-1).or.& 
1367            &(jpiwob(ib).le.1))        CALL ctl_stop( 'nbdyind out of domain' )
1368         IF (jpjwdt(ib).ge.jpjwft(ib)) CALL ctl_stop( 'Bdy start index is greater than end index' )
1369         IF (jpjwdt(ib).le.1     )     CALL ctl_stop( 'Start index out of domain' )
1370         IF (jpjwft(ib).ge.jpjglo)     CALL ctl_stop( 'End index out of domain' )
1371      ENDDO
1372      !
1373      !     
1374      ! 2. Look for segment crossings
1375      !------------------------------
1376      IF (lwp) WRITE(numout,*) '**Look for segments corners  :'
1377      !
1378      itest = 0 ! corner number
1379      !
1380      ! flag to detect if start or end of open boundary belongs to a corner
1381      ! if not (=0), it must be on land.
1382      ! if a corner is detected, save bdy package number for further tests
1383      icorne(:,:)=0. ; icornw(:,:)=0. ; icornn(:,:)=0. ; icorns(:,:)=0.
1384      ! South/West crossings
1385      IF ((nbdysegw > 0).AND.(nbdysegs > 0)) THEN
1386         DO ib1 = 1, nbdysegw       
1387            DO ib2 = 1, nbdysegs
1388               IF (( jpisdt(ib2)<=jpiwob(ib1)).AND. &
1389                &  ( jpisft(ib2)>=jpiwob(ib1)).AND. &
1390                &  ( jpjwdt(ib1)<=jpjsob(ib2)).AND. &
1391                &  ( jpjwft(ib1)>=jpjsob(ib2))) THEN
1392                  IF ((jpjwdt(ib1)==jpjsob(ib2)).AND.(jpisdt(ib2)==jpiwob(ib1))) THEN 
1393                     ! We have a possible South-West corner                     
1394!                     WRITE(numout,*) ' Found a South-West corner at (i,j): ', jpisdt(ib2), jpjwdt(ib1)
1395!                     WRITE(numout,*) ' between segments: ', npckgw(ib1), npckgs(ib2)
1396                     icornw(ib1,1) = npckgs(ib2)
1397                     icorns(ib2,1) = npckgw(ib1)
1398                  ELSEIF ((jpisft(ib2)==jpiwob(ib1)).AND.(jpjwft(ib1)==jpjsob(ib2))) THEN
1399                     WRITE(ctmp1,*) ' E R R O R : Found an acute open boundary corner at point (i,j)= ', &
1400                        &                                     jpisft(ib2), jpjwft(ib1)
1401                     WRITE(ctmp2,*) ' ==========  Not allowed yet'
1402                     WRITE(ctmp3,*) '             Crossing problem with West segment: ',npckgw(ib1), & 
1403                        &                                        ' and South segment: ',npckgs(ib2)
1404                     CALL ctl_stop( ' ', ctmp1, ctmp2, ctmp3, ' ' )
1405                  ELSE
1406                     WRITE(ctmp1,*) ' E R R O R : Check South and West Open boundary indices'
1407                     WRITE(ctmp2,*) ' ==========  Crossing problem with West segment: ',npckgw(ib1) , &
1408                        &                                         ' and South segment: ',npckgs(ib2)
1409                     CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1410                  END IF
1411               END IF
1412            END DO
1413         END DO
1414      END IF
1415      !
1416      ! South/East crossings
1417      IF ((nbdysege > 0).AND.(nbdysegs > 0)) THEN
1418         DO ib1 = 1, nbdysege
1419            DO ib2 = 1, nbdysegs
1420               IF (( jpisdt(ib2)<=jpieob(ib1)+1).AND. &
1421                &  ( jpisft(ib2)>=jpieob(ib1)+1).AND. &
1422                &  ( jpjedt(ib1)<=jpjsob(ib2)  ).AND. &
1423                &  ( jpjeft(ib1)>=jpjsob(ib2)  )) THEN
1424                  IF ((jpjedt(ib1)==jpjsob(ib2)).AND.(jpisft(ib2)==jpieob(ib1)+1)) THEN
1425                     ! We have a possible South-East corner
1426!                     WRITE(numout,*) ' Found a South-East corner at (i,j): ', jpisft(ib2), jpjedt(ib1)
1427!                     WRITE(numout,*) ' between segments: ', npckge(ib1), npckgs(ib2)
1428                     icorne(ib1,1) = npckgs(ib2)
1429                     icorns(ib2,2) = npckge(ib1)
1430                  ELSEIF ((jpjeft(ib1)==jpjsob(ib2)).AND.(jpisdt(ib2)==jpieob(ib1)+1)) THEN
1431                     WRITE(ctmp1,*) ' E R R O R : Found an acute open boundary corner at point (i,j)= ', &
1432                        &                                     jpisdt(ib2), jpjeft(ib1)
1433                     WRITE(ctmp2,*) ' ==========  Not allowed yet'
1434                     WRITE(ctmp3,*) '             Crossing problem with East segment: ',npckge(ib1), &
1435                        &                                        ' and South segment: ',npckgs(ib2)
1436                     CALL ctl_stop( ' ', ctmp1, ctmp2, ctmp3, ' ' )
1437                  ELSE
1438                     WRITE(ctmp1,*) ' E R R O R : Check South and East Open boundary indices'
1439                     WRITE(ctmp2,*) ' ==========  Crossing problem with East segment: ',npckge(ib1), &
1440                     &                                           ' and South segment: ',npckgs(ib2)
1441                     CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1442                  END IF
1443               END IF
1444            END DO
1445         END DO
1446      END IF
1447      !
1448      ! North/West crossings
1449      IF ((nbdysegn > 0).AND.(nbdysegw > 0)) THEN
1450         DO ib1 = 1, nbdysegw       
1451            DO ib2 = 1, nbdysegn
1452               IF (( jpindt(ib2)<=jpiwob(ib1)  ).AND. &
1453                &  ( jpinft(ib2)>=jpiwob(ib1)  ).AND. &
1454                &  ( jpjwdt(ib1)<=jpjnob(ib2)+1).AND. &
1455                &  ( jpjwft(ib1)>=jpjnob(ib2)+1)) THEN
1456                  IF ((jpjwft(ib1)==jpjnob(ib2)+1).AND.(jpindt(ib2)==jpiwob(ib1))) THEN
1457                     ! We have a possible North-West corner
1458!                     WRITE(numout,*) ' Found a North-West corner at (i,j): ', jpindt(ib2), jpjwft(ib1)
1459!                     WRITE(numout,*) ' between segments: ', npckgw(ib1), npckgn(ib2)
1460                     icornw(ib1,2) = npckgn(ib2)
1461                     icornn(ib2,1) = npckgw(ib1)
1462                  ELSEIF ((jpjwdt(ib1)==jpjnob(ib2)+1).AND.(jpinft(ib2)==jpiwob(ib1))) THEN
1463                     WRITE(ctmp1,*) ' E R R O R : Found an acute open boundary corner at point (i,j)= ', &
1464                     &                                     jpinft(ib2), jpjwdt(ib1)
1465                     WRITE(ctmp2,*) ' ==========  Not allowed yet'
1466                     WRITE(ctmp3,*) '             Crossing problem with West segment: ',npckgw(ib1), &
1467                     &                                                    ' and North segment: ',npckgn(ib2)
1468                     CALL ctl_stop( ' ', ctmp1, ctmp2, ctmp3, ' ' )
1469                  ELSE
1470                     WRITE(ctmp1,*) ' E R R O R : Check North and West Open boundary indices'
1471                     WRITE(ctmp2,*) ' ==========  Crossing problem with West segment: ',npckgw(ib1), &
1472                     &                                                    ' and North segment: ',npckgn(ib2)
1473                     CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1474                  END IF
1475               END IF
1476            END DO
1477         END DO
1478      END IF
1479      !
1480      ! North/East crossings
1481      IF ((nbdysegn > 0).AND.(nbdysege > 0)) THEN
1482         DO ib1 = 1, nbdysege       
1483            DO ib2 = 1, nbdysegn
1484               IF (( jpindt(ib2)<=jpieob(ib1)+1).AND. &
1485                &  ( jpinft(ib2)>=jpieob(ib1)+1).AND. &
1486                &  ( jpjedt(ib1)<=jpjnob(ib2)+1).AND. &
1487                &  ( jpjeft(ib1)>=jpjnob(ib2)+1)) THEN
1488                  IF ((jpjeft(ib1)==jpjnob(ib2)+1).AND.(jpinft(ib2)==jpieob(ib1)+1)) THEN
1489                     ! We have a possible North-East corner
1490!                     WRITE(numout,*) ' Found a North-East corner at (i,j): ', jpinft(ib2), jpjeft(ib1)
1491!                     WRITE(numout,*) ' between segments: ', npckge(ib1), npckgn(ib2)
1492                     icorne(ib1,2) = npckgn(ib2)
1493                     icornn(ib2,2) = npckge(ib1)
1494                  ELSEIF ((jpjedt(ib1)==jpjnob(ib2)+1).AND.(jpindt(ib2)==jpieob(ib1)+1)) THEN
1495                     WRITE(ctmp1,*) ' E R R O R : Found an acute open boundary corner at point (i,j)= ', &
1496                     &                                     jpindt(ib2), jpjedt(ib1)
1497                     WRITE(ctmp2,*) ' ==========  Not allowed yet'
1498                     WRITE(ctmp3,*) '             Crossing problem with East segment: ',npckge(ib1), &
1499                     &                                           ' and North segment: ',npckgn(ib2)
1500                     CALL ctl_stop( ' ', ctmp1, ctmp2, ctmp3, ' ' )
1501                  ELSE
1502                     WRITE(ctmp1,*) ' E R R O R : Check North and East Open boundary indices'
1503                     WRITE(ctmp2,*) ' ==========  Crossing problem with East segment: ',npckge(ib1), &
1504                     &                                           ' and North segment: ',npckgn(ib2)
1505                     CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1506                  END IF
1507               END IF
1508            END DO
1509         END DO
1510      END IF
1511      !
1512      ! 3. Check if segment extremities are on land
1513      !--------------------------------------------
1514      !
1515      ! West segments
1516      DO ib = 1, nbdysegw
1517         ! get mask at boundary extremities:
1518         ztestmask(1:2)=0.
1519         DO ji = 1, jpi
1520            DO jj = 1, jpj             
1521              IF (((ji + nimpp - 1) == jpiwob(ib)).AND. & 
1522               &  ((jj + njmpp - 1) == jpjwdt(ib))) ztestmask(1)=tmask(ji,jj,1)
1523              IF (((ji + nimpp - 1) == jpiwob(ib)).AND. & 
1524               &  ((jj + njmpp - 1) == jpjwft(ib))) ztestmask(2)=tmask(ji,jj,1) 
1525            END DO
1526         END DO
1527         CALL mpp_sum( 'bdyini', ztestmask, 2 )   ! sum over the global domain
1528
1529         IF (ztestmask(1)==1) THEN
1530            IF (icornw(ib,1)==0) THEN
1531               WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgw(ib)
1532               WRITE(ctmp2,*) ' ==========  does not start on land or on a corner'                                                 
1533               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1534            ELSE
1535               ! This is a corner
1536               IF(lwp) WRITE(numout,*) 'Found a South-West corner at (i,j): ', jpiwob(ib), jpjwdt(ib)
1537               CALL bdy_ctl_corn(npckgw(ib), icornw(ib,1))
1538               itest=itest+1
1539            ENDIF
1540         ENDIF
1541         IF (ztestmask(2)==1) THEN
1542            IF (icornw(ib,2)==0) THEN
1543               WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgw(ib)
1544               WRITE(ctmp2,*) ' ==========  does not end on land or on a corner'                                                 
1545               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1546            ELSE
1547               ! This is a corner
1548               IF(lwp) WRITE(numout,*) 'Found a North-West corner at (i,j): ', jpiwob(ib), jpjwft(ib)
1549               CALL bdy_ctl_corn(npckgw(ib), icornw(ib,2))
1550               itest=itest+1
1551            ENDIF
1552         ENDIF
1553      END DO
1554      !
1555      ! East segments
1556      DO ib = 1, nbdysege
1557         ! get mask at boundary extremities:
1558         ztestmask(1:2)=0.
1559         DO ji = 1, jpi
1560            DO jj = 1, jpj             
1561              IF (((ji + nimpp - 1) == jpieob(ib)+1).AND. & 
1562               &  ((jj + njmpp - 1) == jpjedt(ib))) ztestmask(1)=tmask(ji,jj,1)
1563              IF (((ji + nimpp - 1) == jpieob(ib)+1).AND. & 
1564               &  ((jj + njmpp - 1) == jpjeft(ib))) ztestmask(2)=tmask(ji,jj,1) 
1565            END DO
1566         END DO
1567         CALL mpp_sum( 'bdyini', ztestmask, 2 )   ! sum over the global domain
1568
1569         IF (ztestmask(1)==1) THEN
1570            IF (icorne(ib,1)==0) THEN
1571               WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckge(ib)
1572               WRITE(ctmp2,*) ' ==========  does not start on land or on a corner'                                                 
1573               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1574            ELSE
1575               ! This is a corner
1576               IF(lwp) WRITE(numout,*) 'Found a South-East corner at (i,j): ', jpieob(ib)+1, jpjedt(ib)
1577               CALL bdy_ctl_corn(npckge(ib), icorne(ib,1))
1578               itest=itest+1
1579            ENDIF
1580         ENDIF
1581         IF (ztestmask(2)==1) THEN
1582            IF (icorne(ib,2)==0) THEN
1583               WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckge(ib)
1584               WRITE(ctmp2,*) ' ==========  does not end on land or on a corner'                                                 
1585               CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1586            ELSE
1587               ! This is a corner
1588               IF(lwp) WRITE(numout,*) 'Found a North-East corner at (i,j): ', jpieob(ib)+1, jpjeft(ib)
1589               CALL bdy_ctl_corn(npckge(ib), icorne(ib,2))
1590               itest=itest+1
1591            ENDIF
1592         ENDIF
1593      END DO
1594      !
1595      ! South segments
1596      DO ib = 1, nbdysegs
1597         ! get mask at boundary extremities:
1598         ztestmask(1:2)=0.
1599         DO ji = 1, jpi
1600            DO jj = 1, jpj             
1601              IF (((jj + njmpp - 1) == jpjsob(ib)).AND. & 
1602               &  ((ji + nimpp - 1) == jpisdt(ib))) ztestmask(1)=tmask(ji,jj,1)
1603              IF (((jj + njmpp - 1) == jpjsob(ib)).AND. & 
1604               &  ((ji + nimpp - 1) == jpisft(ib))) ztestmask(2)=tmask(ji,jj,1) 
1605            END DO
1606         END DO
1607         CALL mpp_sum( 'bdyini', ztestmask, 2 )   ! sum over the global domain
1608
1609         IF ((ztestmask(1)==1).AND.(icorns(ib,1)==0)) THEN
1610            WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgs(ib)
1611            WRITE(ctmp2,*) ' ==========  does not start on land or on a corner'                                                 
1612            CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1613         ENDIF
1614         IF ((ztestmask(2)==1).AND.(icorns(ib,2)==0)) THEN
1615            WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgs(ib)
1616            WRITE(ctmp2,*) ' ==========  does not end on land or on a corner'                                                 
1617            CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1618         ENDIF
1619      END DO
1620      !
1621      ! North segments
1622      DO ib = 1, nbdysegn
1623         ! get mask at boundary extremities:
1624         ztestmask(1:2)=0.
1625         DO ji = 1, jpi
1626            DO jj = 1, jpj             
1627              IF (((jj + njmpp - 1) == jpjnob(ib)+1).AND. & 
1628               &  ((ji + nimpp - 1) == jpindt(ib))) ztestmask(1)=tmask(ji,jj,1)
1629              IF (((jj + njmpp - 1) == jpjnob(ib)+1).AND. & 
1630               &  ((ji + nimpp - 1) == jpinft(ib))) ztestmask(2)=tmask(ji,jj,1) 
1631            END DO
1632         END DO
1633         CALL mpp_sum( 'bdyini', ztestmask, 2 )   ! sum over the global domain
1634
1635         IF ((ztestmask(1)==1).AND.(icornn(ib,1)==0)) THEN
1636            WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgn(ib)
1637            WRITE(ctmp2,*) ' ==========  does not start on land'                                                 
1638            CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1639         ENDIF
1640         IF ((ztestmask(2)==1).AND.(icornn(ib,2)==0)) THEN
1641            WRITE(ctmp1,*) ' E R R O R : Open boundary segment ', npckgn(ib)
1642            WRITE(ctmp2,*) ' ==========  does not end on land'                                                 
1643            CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1644         ENDIF
1645      END DO
1646      !
1647      IF ((itest==0).AND.(lwp)) WRITE(numout,*) 'NO open boundary corner found'
1648      !
1649      ! Other tests TBD:
1650      ! segments completly on land
1651      ! optimized open boundary array length according to landmask
1652      ! Nudging layers that overlap with interior domain
1653      !
1654   END SUBROUTINE bdy_ctl_seg
1655
1656
1657   SUBROUTINE bdy_ctl_corn( ib1, ib2 )
1658      !!----------------------------------------------------------------------
1659      !!                 ***  ROUTINE bdy_ctl_corn  ***
1660      !!
1661      !! ** Purpose :   Check numerical schemes consistency between
1662      !!                segments having a common corner
1663      !!
1664      !! ** Method  :   
1665      !!----------------------------------------------------------------------
1666      INTEGER, INTENT(in)  ::   ib1, ib2
1667      INTEGER :: itest
1668      !!----------------------------------------------------------------------
1669      itest = 0
1670
1671      IF( cn_dyn2d(ib1) /= cn_dyn2d(ib2) )   itest = itest + 1
1672      IF( cn_dyn3d(ib1) /= cn_dyn3d(ib2) )   itest = itest + 1
1673      IF( cn_tra  (ib1) /= cn_tra  (ib2) )   itest = itest + 1
1674      !
1675      IF( nn_dyn2d_dta(ib1) /= nn_dyn2d_dta(ib2) )   itest = itest + 1
1676      IF( nn_dyn3d_dta(ib1) /= nn_dyn3d_dta(ib2) )   itest = itest + 1
1677      IF( nn_tra_dta  (ib1) /= nn_tra_dta  (ib2) )   itest = itest + 1
1678      !
1679      IF( nn_rimwidth(ib1) /= nn_rimwidth(ib2) )   itest = itest + 1   
1680      !
1681      IF( itest>0 ) THEN
1682         WRITE(ctmp1,*) ' E R R O R : Segments ', ib1, 'and ', ib2
1683         WRITE(ctmp2,*) ' ==========  have different open bdy schemes'                                                 
1684         CALL ctl_stop( ' ', ctmp1, ctmp2, ' ' )
1685      ENDIF
1686      !
1687   END SUBROUTINE bdy_ctl_corn
1688
1689   !!=================================================================================
1690END MODULE bdyini
Note: See TracBrowser for help on using the repository browser.