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

  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---------------------------------------------------------------------





























































!! * Modules used
   USE parameters
   USE comvars2d, ONLY : jb,jh,ig,id,h0_g


   IMPLICIT NONE

!! * Arguments

!! * Local declarations
   INTEGER        :: i,j


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



   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



   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



   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



   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



   !PRINT*, 'exit GRID_MASK routine'
  
  END SUBROUTINE grid_mask
