   !!======================================================================

  SUBROUTINE grid_mask

   !&E---------------------------------------------------------------------
   !&E                 ***  ROUTINE grid_mask  ***
   !&E
   !&E ** Purpose : estimate of the indexes of the boundaries
   !&E              for each line or column
   !&E
   !&E ** Description : esdtimate of jb,jh,ig,id
   !&E
   !&E ** Called by : main.F90
   !&E
   !&E ** External calls :
   !&E
   !&E ** Used ij-arrays :
   !&E
   !&E ** Modified variables :
   !&E
   !&E ** Reference :
   !&E
   !&E ** History :
   !&E       !  2010-11  (V. Garnier) from geom
   !&E       !  12-2016 (A. Fiandrino, B. Thouvenin. ) introducton of specificities key_lagoon 
   !&E
   !&E---------------------------------------------------------------------

#include "toolcpp.h"

   !! * Modules used
   USE parameters
   USE comvars2d, ONLY : jb,jh,ig,id,h0_g
#ifdef key_lagoon
   USE comvars2d,  ONLY: nbgrau,igrau,jgrau,type_grau,l_open_grau
#endif
#ifdef key_lagoon_spec
   USE comvars2d,  ONLY: xmask_h0,xmask_hx,xmask_hy
#endif
   IMPLICIT NONE

   !! * Arguments

   !! * Local declarations
   INTEGER        :: i,j
#ifdef key_lagoon
   INTEGER        :: igr
#ifdef key_lagoon_spec
! le 28/05/2013 Definition d un masque dans les zones conch de la lagune de Thau
   INTEGER        :: n,ij_parc
   INTEGER(KIND=rsh),DIMENSION(618)   :: iparc,jparc
#endif
#endif

   !!----------------------------------------------------------------------
   !! * Executable part

#if defined key_lagoon && defined key_lagoon_spec
! le 28/05/2013 Définition d'un masque dans les zones conch de la lagune de Thau
! coordonnees des parcs conchylicoles Zone A + zone B + zone C
        data (iparc(n),jparc(n),n=1,nbparc)/                     &
        134,48, 134,49, 134,51, 134,53, 134,55, 135,48, 135,49, 135,51,   &
        135,53, 135,55, 136,51, 136,53, 136,55, 137,49, 137,50, 137,51,   &
        138,49, 138,50, 138,51, 138,53, 138,55, 138,57, 138,59, 139,49,   &
        139,50, 139,51, 139,52, 139,53, 139,55, 139,57, 139,59, 139,61,   &
        140,61, 141,50, 141,51, 141,52, 141,53, 141,54, 141,55, 141,57,   &
        141,59, 142,50, 142,51, 142,52, 142,53, 142,54, 142,55, 142,57,   &
        142,59, 142,61, 143,57, 143,59, 143,61, 144,50, 144,52, 144,54,   &
        144,55, 145,50, 145,52, 145,54, 145,55, 145,56, 145,57, 145,59,   &
        145,61, 146,54, 146,55, 146,56, 146,57, 146,59, 146,61, 147,50,   &
        147,52, 148,50, 148,52, 148,54, 148,56, 148,57, 148,58, 148,59,   &
        148,61, 149,50, 149,52, 149,54, 149,56, 149,57, 149,58, 149,59,   &
        149,60, 149,61, 150,48, 150,49, 150,50, 151,47, 151,48, 151,49,   &
        151,50, 151,51, 151,52, 151,53, 151,54, 151,56, 151,58, 151,59,   &
        151,60, 152,47, 152,48, 152,49, 152,50, 152,51, 152,52, 152,53,   &
        152,54, 152,56, 152,58, 152,59, 152,60, 153,58, 153,59, 153,60,   &
        154,47, 154,49, 154,50, 154,51, 154,52, 154,54, 154,56, 154,58,   &
        154,59, 155,45, 155,47, 155,49, 155,50, 155,51, 155,52, 155,53,   &
        155,54, 155,56, 155,58, 155,60, 156,56, 156,58, 156,60, 157,44,   &
        157,45, 157,47, 157,49, 157,51, 157,52, 157,53, 157,54, 157,56,   &
        158,44, 158,45, 158,47, 158,49, 158,51, 158,52, 158,53, 158,54,   &
        158,55, 158,56, 158,58, 158,60, 159,52, 159,53, 159,54, 159,55,   &
        159,56, 159,58, 159,60, 160,42, 160,44, 160,45, 160,46, 160,47,   &
        160,49, 161,42, 161,44, 161,45, 161,46, 161,47, 161,49, 161,51,   &
        161,53, 161,54, 161,55, 161,56, 161,58, 161,60, 162,49, 162,51,   &
        162,53, 162,54, 162,55, 162,56, 162,58, 162,60, 163,42, 163,44,   &
        163,46, 163,47, 164,42, 164,44, 164,46, 164,47, 164,48, 164,49,   &
        164,51, 164,53, 164,55, 164,57, 164,58, 164,59, 165,42, 165,44,   &
        165,45, 165,46, 165,47, 165,48, 165,49, 165,50, 165,51, 165,53,   &
        165,55, 165,57, 165,58, 165,59, 166,42, 166,44, 166,57, 166,58,   &
        166,59, 167,41, 167,42, 167,44, 167,46, 167,48, 167,49, 167,50,   &
        167,51, 167,53, 167,55, 168,41, 168,42, 168,44, 168,46, 168,48,   &
        168,49, 168,50, 168,51, 168,53, 168,55, 168,57, 168,59, 169,55,   &
        169,57, 169,59, 170,41, 170,42, 170,43, 170,44, 170,45, 170,46,   &
        170,48, 170,49, 170,50, 170,51, 170,52, 170,53, 171,41, 171,42,   &
        171,43, 171,44, 171,46, 171,48, 171,50, 171,51, 171,52, 171,53,   &
        171,54, 171,55, 171,57, 172,50, 172,51, 172,52, 172,53, 172,54,   &
        172,55, 172,57, &
        74,55,  75,51,  75,53,  75,55,  76,48,  76,49,  76,53,  76,55,   &
        77,45,  77,46,  77,47,  77,49,  77,51,  77,52,  78,42,  78,43,   &
        78,44,  78,45,  78,47,  78,49,  78,54,  79,40,  79,41,  79,42,   &
        79,43,  79,45,  79,52,  79,54,  79,55,  80,40,  80,41,  80,43,   &
        80,48,  80,50,  80,52,  80,53,  80,55,  81,46,  81,48,  81,50,   &
        81,52,  82,42,  82,43,  82,44,  82,46,  82,48,  82,56,  83,41,   &
        83,42,  83,44,  83,46,  83,51,  83,53,  84,42,  84,49,  84,51,   &
        84,53,  85,45,  85,47,  85,49,  85,51,  85,52,  86,42,  86,43,   &
        86,45,  86,47,  86,48,  86,49,  86,50,  86,54,  86,55,  86,57,   &
        87,43,  87,45,  87,52,  87,54,  87,55,  87,57,  88,43,  88,48,   &
        88,50,  88,52,  88,55,  89,46,  89,48,  89,50,  89,51,  90,43,   &
        90,44,  90,46,  90,48,  90,49,  90,55,  90,56,  91,44,  91,51,   &
        91,53,  91,54,  91,55,  91,56,  92,49,  92,51,  92,52,  92,53,   &
        92,54,  92,56,  93,45,  93,47,  93,49,  93,50,  93,51,  93,52,   &
        93,57,  94,45,  94,47,  94,48,  94,49,  94,54,  94,55,  94,56,   &
        94,57,  95,45,  95,52,  95,53,  95,54,  95,55,  95,57,  96,48,   &
        96,50,  96,51,  96,53,  96,55,  97,46,  97,48,  97,50,  97,51,   &
        98,46,  98,48,  98,49,  98,55,  98,56,  99,51,  99,52,  99,53,   &
        99,54,  99,55,  99,56, 100,49, 100,51, 100,52, 100,53, 100,54,   &
        101,49, 101,50, 101,52, 102,54, 102,55, 102,56, 102,57, 103,52,   &
        103,53, 103,54, 103,55, 103,57, 104,50, 104,51, 104,53, 104,55,   &
        105,50, 105,51, 105,57, 105,58, 106,54, 106,55, 106,56, 106,58,   &
        107,51, 107,52, 107,53, 107,54, 107,56, 107,58, 108,51, 108,52,   &
        108,54, 109,52, &
        44,44, 44,45, 45,41, 45,42, 45,43, 45,44, 45,46, 46,39,   &
        46,40, 46,41, 46,42, 46,44, 46,46, 47,35, 47,37, 47,38,   &
        47,39, 47,40, 47,42, 47,47, 48,35, 48,37, 48,38, 48,39,   &
        48,40, 48,44, 48,45, 48,47, 49,35, 49,36, 49,42, 49,43,   &
        49,45, 49,47, 49,48, 50,38, 50,40, 50,41, 50,43, 51,36,   &
        51,38, 51,39, 51,40, 51,41, 52,36, 52,37, 52,38, 52,39,   &
        52,44, 53,36, 53,41, 53,42, 53,44, 54,40, 54,41, 54,42,   &
        54,44, 55,37, 55,38, 55,39, 55,40, 55,42, 56,37, 56,38,   &
        56,40, 56,44, 56,45, 57,42, 57,43, 57,45, 57,47, 58,38,   &
        58,39, 58,40, 58,41, 58,43, 58,45, 59,38, 59,39, 59,41,   &
        60,39, 60,44, 60,46, 61,42, 61,44, 61,46, 61,47, 62,37,   &
        62,38, 62,39, 62,40, 62,42, 62,44, 63,37, 63,38, 63,39,   &
        63,40, 63,42, 63,47, 63,49, 63,50, 64,38, 64,45, 64,47,   &
        64,49, 64,50, 65,40, 65,43, 65,45, 65,47, 66,38, 66,39,   &
        66,41, 66,43, 66,45, 66,50, 66,51, 67,39, 67,41, 67,48,   &
        67,50, 67,51, 68,39, 68,44, 68,46, 69,42, 69,44, 70,40,   &
        70,42, 70,44, 70,45, 71,40, 71,42, 72,45, 73,41, 73,43,   &
        73,45, 73,46, 74,41, 74,43, 74,44, 75,41/ 

     ALLOCATE(xmask_h0(limin:limax,ljmin:ljmax))
     ALLOCATE(xmask_hx(limin:limax,ljmin:ljmax))
     ALLOCATE(xmask_hy(limin:limax,ljmin:ljmax))
     xmask_h0(:,:)  =0.0_rsh ; xmask_hx(:,:)  =0.0_rsh ; xmask_hy(:,:) =0.0_rsh

     DO ij_parc=1,nbparc
        i=iparc(ij_parc)
        j=jparc(ij_parc)
        IF_MPI ( (i >= limin) .AND. (i <= limax) .AND. (j >= ljmin) .AND. (j <= ljmax) ) THEN
!         print*,'i j',i,j,mask_h0(i,j)
          xmask_h0(i,j)=1.0_rsh
          xmask_hx(i,j)=1.0_rsh
          xmask_hy(i,j)=1.0_rsh
!         print*,'i j',i,j,xmask_h0(i,j),xmask_hx(i,j),xmask_hy(i,j)
        ENDIF_MPI
        IF_MPI ( (i == limin+1) .AND. (j >= ljmin) .AND. (j <= ljmax) ) THEN
!         print*,'i j',i,j,mask_h0(i,j)
          xmask_hx(i-1,j)=1.0_rsh
!         print*,'i j',i,j,xmask_h0(i,j),xmask_hx(i,j),xmask_hy(i,j)
        ENDIF_MPI
        IF_MPI ( (i >= limin) .AND. (i <= limax) .AND. (j == ljmax+1) ) THEN
!         print*,'i j',i,j,mask_h0(i,j)
          xmask_hy(i,j-1)=1.0_rsh
!         print*,'i j',i,j,xmask_h0(i,j),xmask_hx(i,j),xmask_hy(i,j)
        ENDIF_MPI
     END DO
#endif

   DO i=imin,imax
     jb(i)=jmin+1
     DO j = jmin+1,jmax-2
       IF (h0_g(i,j) > -valmanq .OR. h0_g(i,j+1) > -valmanq .OR. h0_g(i,j+2) > -valmanq) THEN
         jb(i) = j
         GOTO 1050
       ELSE
         jb(i) = j
       END IF
     END DO
 1050 CONTINUE
   END DO

#ifdef key_lagoon
   DO igr=1,nbgrau
     IF(type_grau(igr) == 3 .AND. l_open_grau(igr))THEN
        ! Ouverture du grau
        i=igrau(igr)
        jb(i)=jgrau(igr)
     ENDIF
   ENDDO
#endif

   DO i=imin,imax
     jh(i)=jmax
     DO j = jmax-2,jmin+1,-1
       IF (h0_g(i,j) > -valmanq .OR. h0_g(i,j+1) > -valmanq .OR. h0_g(i,j+2) > -valmanq) THEN
         jh(i) = j + 2
         GOTO 1100
       ELSE
         jh(i) = j
       END IF
     END DO
 1100 CONTINUE
   END DO

#ifdef key_lagoon
   DO igr=1,nbgrau
     IF(type_grau(igr) == 1 .AND. l_open_grau(igr))THEN
        ! Ouverture du grau
        i=igrau(igr)
        jh(i)=jgrau(igr)
     ENDIF
   ENDDO
#endif

   DO j=jmin,jmax
     ig(j)=imin+1
     DO i = imin+1,imax-2
       IF (h0_g(i,j) > -valmanq .OR. h0_g(i+1,j) > -valmanq .OR. h0_g(i+2,j) > -valmanq) THEN
         ig(j) = i
         GOTO 1200
       ELSE
         ig(j) = i
       END IF
     END DO
 1200 CONTINUE
   END DO

#ifdef key_lagoon
   DO igr=1,nbgrau
     IF(type_grau(igr) == 4 .AND. l_open_grau(igr))THEN
        ! Ouverture du grau
        j=jgrau(igr)
        ig(j)=igrau(igr)
     ENDIF
   ENDDO
#endif

   DO j=jmin,jmax
     id(j)=imax
     DO i = imax-2,imin+1,-1
       IF (h0_g(i,j) > -valmanq .OR. h0_g(i+1,j) > -valmanq .OR. h0_g(i+2,j) > -valmanq) THEN
         id(j) = i + 2
         GOTO 1300
       ELSE
         id(j) = i
       END IF
     END DO
 1300  CONTINUE
   END DO

ig(7:160)=31                                        !iman19aout10    !iman3sep10 !28 9sep10
jb(31)=161                  !161                                        !iman14sep10 !28 avalesh 29 boud
#ifdef key_lagoon
   DO igr=1,nbgrau
     IF(type_grau(igr) == 2 .AND. l_open_grau(igr))THEN
        ! Ouverture du grau
        j=jgrau(igr)
        id(j)=igrau(igr)
     ENDIF
   ENDDO
#endif


   PRINT_DBG*, 'exit GRID_MASK routine'
  
  END SUBROUTINE grid_mask
