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 branches/2017/dev_merge_2017/NEMOGCM/NEMO/OPA_SRC/BDY – NEMO

source: branches/2017/dev_merge_2017/NEMOGCM/NEMO/OPA_SRC/BDY/bdyini.F90 @ 9105

Last change on this file since 9105 was 9105, checked in by flavoni, 6 years ago

fix

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