SUBROUTINE SUNMEN(KSMAX,KHTYP,KDGL,KLOEN,PSQM2,KDGSA,KDGEN,KMEN,PNLGINC)

!**** *SUNMEN*  - Initialize maximum zonal wave number for each latitude

!     Purpose.
!     --------
!           Initialize maximum zonal wave number for each latitude

!**   Interface.
!     ----------
!        *CALL* *SUNMEN( ... )

!        Explicit arguments :
!        --------------------
!        INPUT :
!               KSMAX : truncation
!               KHTYP : regular or reduced grid
!               KDGL  : number of latitudes
!               KLOEN : number of points on each latitude row
!               PSQM2 : square root of 1-Mu**2
!               KDGSA : first bound of arrays KLOEN,PSQM2
!               KDGEN : last bound of arrays KLOEN,PSQM2
!               PNLGINC : increment to get non-linear grid 
!        OUTPUT :
!               KMEN  : maximum zonal wave number for each latitude

!        Implicit arguments :
!        --------------------
!        none.

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        none.

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     Modifications.
!     --------------
!        Original : 94-04-29 from SUGEM1
!        Modified by M. Tolstykh: 94-08-31 Set-up for CUD interpolation
!        Modified by K. YESSAD (MARCH 1995): Extra-latitudes computations
!                 according to value of NDGSUR and LDPOLE only.
!        Modified 98-08-10 by K. YESSAD: removal of LRPOLE option.
!         - removal of dummy argument LDPOLE and code under LDPOLE.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KDGSA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDGEN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSMAX 
INTEGER(KIND=JPIM),INTENT(IN)    :: KHTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDGL 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLOEN(KDGSA:KDGEN) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSQM2(KDGSA:KDGEN) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KMEN(KDGSA:KDGEN) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNLGINC 
REAL(KIND=JPRB) :: ZSQM2(KDGSA:KDGEN)

INTEGER(KIND=JPIM) :: IDGNH, IDGSUR, JGL, JGLSUR
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

!*       1.    Initialize KMEN for regular grid
!              --------------------------------

IF (LHOOK) CALL DR_HOOK('SUNMEN',0,ZHOOK_HANDLE)
IF(KHTYP == 0)THEN
  DO JGL=KDGSA,KDGEN
    KMEN (JGL)=KSMAX
  ENDDO
ENDIF

!*       2.    Initialize KMEN for reduced grid
!              --------------------------------

IF(KHTYP /= 0)THEN

  IDGNH=(KDGL+1)/2
  DO JGL=KDGSA,KDGEN
    ZSQM2(JGL)=PSQM2(JGL)*PNLGINC
  ENDDO

!       * Northern hemisphere :
  KMEN(1)=MIN(KSMAX,INT(REAL(KLOEN(1)-1,JPRB)/(2.0_JPRB+ZSQM2(1))))
  DO JGL=2,IDGNH
    KMEN(JGL)=MIN(KSMAX,MAX(KMEN(JGL-1),&
     & INT(REAL(KLOEN(JGL)-1,JPRB)/(2.0_JPRB+ ZSQM2(JGL)))))  
  ENDDO
!       * Southern hemisphere :
  KMEN(KDGL)=MIN(KSMAX,INT(REAL(KLOEN(KDGL)-1,JPRB)/(2.0_JPRB+ZSQM2(KDGL))))
  DO JGL=KDGL-1, IDGNH+1, -1
    KMEN(JGL)=MIN(KSMAX,MAX(KMEN(JGL+1),&
     & INT(REAL(KLOEN(JGL)-1,JPRB)/(2.0_JPRB+ ZSQM2(JGL)))))  
  ENDDO
!       * Extrapolar latitudes :
  IDGSUR=KDGEN-KDGL
  IF (IDGSUR > 0) THEN
!DIR$ IVDEP
!OCL NOVREC
    DO JGLSUR=1,IDGSUR
      KMEN(1-JGLSUR)=KMEN(JGLSUR)
    ENDDO
!DIR$ IVDEP
!OCL NOVREC
    DO JGLSUR=1,IDGSUR
      KMEN(KDGL+JGLSUR)=KMEN(KDGL+1-JGLSUR)
    ENDDO
  ENDIF

ENDIF
!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SUNMEN',1,ZHOOK_HANDLE)
END SUBROUTINE SUNMEN

