MODULE lbcnfd
   !!======================================================================
   !!                       ***  MODULE  lbcnfd  ***
   !! Ocean        : north fold  boundary conditions
   !!======================================================================
   !!             9.0  !  09-03  (R. Benshila) Initial version 
   !!----------------------------------------------------------------------
   !! * Modules used
   !USE oce             ! ocean dynamics and tracers   
   USE dom_oce         ! ocean space and time domain 
   !USE in_out_manager  ! I/O manager

   IMPLICIT NONE
   PRIVATE

   PUBLIC lbc_nfd       ! north fold conditions
   !!----------------------------------------------------------------------

CONTAINS

   SUBROUTINE lbc_nfd( pt2d, cd_type, psgn, pr2dj )
      !!----------------------------------------------------------------------
      !!                  ***  routine lbc_nfd_2d  ***
      !!
      !! ** Purpose :   2D lateral boundary condition : North fold treatment
      !!       without processor exchanges. 
      !!
      !! ** Method  :   
      !!
      !! ** Action  :   pt2d with update value at its periphery
      !!
      !!----------------------------------------------------------------------
      !! * Arguments
      CHARACTER(len=1) , INTENT( in ) ::   &
         cd_type       ! define the nature of ptab array grid-points
      !             ! = T , U , V , F , W points
      !             ! = S : T-point, north fold treatment ???
      !             ! = G : F-point, north fold treatment ???
      REAL(wp), INTENT( in ) ::   &
         psgn          ! control of the sign change
      !             !   = -1. , the sign is changed if north fold boundary
      !             !   =  1. , the sign is kept  if north fold boundary
      REAL(wp), DIMENSION(:,:), INTENT( inout ) ::   &
         pt2d          ! 3D array on which the boundary condition is applied
      INTEGER, OPTIONAL, INTENT(in) :: pr2dj

      !! * Local declarations
      INTEGER  ::   ji, jl, ipr2dj
      INTEGER  ::   ijt, iju, ijpj, ijpjm1

      SELECT CASE ( jpni )
      CASE ( 1 )  ! only one proc along I
         ijpj = nlcj
      CASE DEFAULT 
         ijpj = 4
      END SELECT


      IF( PRESENT(pr2dj) ) THEN
         ipr2dj = pr2dj
         IF (jpni .GT. 1) ijpj = ijpj + ipr2dj
      ELSE
         ipr2dj = 0 
      ENDIF

      ijpjm1 = ijpj-1


      SELECT CASE ( npolj )

      CASE ( 3, 4 )                       ! *  North fold  T-point pivot

         SELECT CASE ( cd_type )

         CASE ( 'T', 'S', 'W' )
            DO jl = 0, ipr2dj
               DO ji = 2, jpiglo
                  ijt=jpiglo-ji+2
                  pt2d(ji,ijpj+jl) = psgn * pt2d(ijt,ijpj-2-jl)
               END DO
            END DO
            DO ji = jpiglo/2+1, jpiglo
               ijt=jpiglo-ji+2
               pt2d(ji,ijpj-1) = psgn * pt2d(ijt,ijpj-1)
            END DO
         CASE ( 'U' )                                     ! U-point
            DO jl =0, ipr2dj
               DO ji = 1, jpiglo-1
                  iju = jpiglo-ji+1
                  pt2d(ji,ijpj+jl) = psgn * pt2d(iju,ijpj-2-jl)
               END DO
            END DO
            DO ji = jpiglo/2, jpiglo-1
               iju = jpiglo-ji+1
               pt2d(ji,ijpjm1) = psgn * pt2d(iju,ijpjm1)
            END DO
         CASE ( 'V' )                                     ! V-point
            DO jl =-1, ipr2dj
               DO ji = 2, jpiglo
                  ijt = jpiglo-ji+2
                  pt2d(ji,ijpj+jl) = psgn * pt2d(ijt,ijpj-3-jl)
               END DO
            END DO
         CASE ( 'F' , 'G' )                               ! F-point
            DO jl =-1, ipr2dj
               DO ji = 1, jpiglo-1
                  iju = jpiglo-ji+1
                  pt2d(ji,ijpj+jl) = psgn * pt2d(iju,ijpj-3-jl)
               END DO
            END DO
         CASE ( 'I' )                                     ! ice U-V point
            DO jl =0, ipr2dj
               pt2d(2,ijpj+jl) = psgn * pt2d(3,ijpj-1+jl)
               DO ji = 3, jpiglo
                  iju = jpiglo - ji + 3
                  pt2d(ji,ijpj+jl) = psgn * pt2d(iju,ijpj-1-jl)
               END DO
            END DO
         END SELECT

      CASE ( 5, 6 )                        ! *  North fold  F-point pivot

         SELECT CASE ( cd_type )
         CASE ( 'T' , 'W' ,'S' )                          ! T-, W-point
            DO jl = 0, ipr2dj
               DO ji = 1, jpiglo
                  ijt = jpiglo-ji+1
                  pt2d(ji,ijpj+jl) = psgn * pt2d(ijt,ijpj-1-jl)
               END DO
            END DO
         CASE ( 'U' )                                     ! U-point
            DO jl = 0, ipr2dj
               DO ji = 1, jpiglo-1
                  iju = jpiglo-ji
                  pt2d(ji,ijpj+jl) = psgn * pt2d(iju,ijpj-1-jl)
               END DO
            END DO
         CASE ( 'V' )                                     ! V-point
            DO jl = 0, ipr2dj
               DO ji = 1, jpiglo
                  ijt = jpiglo-ji+1
                  pt2d(ji,ijpj+jl) = psgn * pt2d(ijt,ijpj-2-jl)
               END DO
            END DO
            DO ji = jpiglo/2+1, jpiglo
               ijt = jpiglo-ji+1
               pt2d(ji,ijpjm1) = psgn * pt2d(ijt,ijpjm1)
            END DO
         CASE ( 'F' , 'G' )                               ! F-point
            DO jl = 0, ipr2dj
               DO ji = 1, jpiglo-1
                  iju = jpiglo-ji
                  pt2d(ji,ijpj+jl) = psgn * pt2d(iju,ijpj-2-jl)
               END DO
            END DO
            DO ji = jpiglo/2+1, jpiglo-1
               iju = jpiglo-ji
               pt2d(ji,ijpjm1) = psgn * pt2d(iju,ijpjm1)
            END DO
         CASE ( 'I' )                                  ! ice U-V point
            pt2d( 2 ,ijpj:ijpj+ipr2dj) = 0.e0
            DO jl = 0, ipr2dj
               DO ji = 2 , jpiglo-1
                  ijt = jpiglo - ji + 2
                  pt2d(ji,ijpj+jl)= 0.5 * ( pt2d(ji,ijpj-1-jl) + psgn * pt2d(ijt,ijpj-1-jl) )
               END DO
            END DO
         END SELECT

      CASE DEFAULT                           ! *  closed : the code probably never go through

         !SELECT CASE ( cd_type)
         !CASE ( 'T' , 'U' , 'V' , 'W' )                 ! T-, U-, V-, W-points
         !   pt2d(:, 1:1-ipr2dj     ) = 0.e0
         !   pt2d(:,ijpj:ijpj+ipr2dj) = 0.e0
         !CASE ( 'F' )                                   ! F-point
         !   pt2d(:,ijpj:ijpj+ipr2dj) = 0.e0
         !CASE ( 'I' )                                   ! ice U-V point
         !   pt2d(:, 1:1-ipr2dj     ) = 0.e0
         !   pt2d(:,ijpj:ijpj+ipr2dj) = 0.e0
         !END SELECT

      END SELECT
      

   END SUBROUTINE lbc_nfd

   !!======================================================================
END MODULE lbcnfd
