SUBROUTINE SUPLIS(KNSMIN,KNSMAX,KDGLX,PDDSINTA,LDPNM,PNM,PDDPNM,PDDA,PDDI)

!**** *SUPLIS* - Interface for Legendre polynomials computation.

!      Purpose.
!      --------
!         Computes Legendre polynomials for a set of latitudes.
!         Real PNM(m,n) are stored for all latitudes, for n
!          between 0 and KNSMAX, for m between 0 and KNSMIN (if LDPNM=.T.).
!         Double precision PDDPNM are stored for the last latitude only.
!         The address of the Legendre Polynomial PNM(n,m) is :
!            PNM(.,IRDEB(m)+KNSMAX-n)

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

!      Explicit arguments
!      ------------------

!       KNSMIN     - lower truncation.                              (input)
!       KNSMAX     - upper truncation.                              (input)
!       KDGLX      - number of latitudes where Legendre             (input)
!                     polynomials have to be computed.
!       PDDSINTA   - sinus of the latitudes where Legendre          (input)
!                     polynomials have to be computed.
!       LDPNM      - .T. : PNM is computed.                         (input)
!                    .F. : PNM is not computed.
!       PNM        - Legendre polynomials (real compacted data).    (output)
!       PDDPNM     - Legendre polynomials for the last latitude     (output)
!                     KDGLX (double precision not compacted data).
!       PDDA       - Double precision (SQRT(2n+1)).                 (output)
!       PDDI       - Double precision (n).                          (output)

!      Implicit arguments
!      ------------------

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

!      Reference.
!      ----------

!      Externals.
!      ---------
!         Calls SUPOLA.
!         Called by SUDIL and SUROT.

!      Author.
!      -------
!         K. YESSAD (METEO-FRANCE/CNRM/GMAP).

!      Modifications.
!      --------------
!       Original : JUNE 1993.
!       K. YESSAD (APRIL 1998): replace some integer multiplications by
!         double precision multiplications to avoid integer floating point
!         exception (> 2**31) when using high truncations ( KNSMAX > 640 ) 
!         and 32 bits integers.
!       M.Hamrud      01-Oct-2003 CY28 Cleaning
!       K.Yessad (Nov 2008): SUPOL->SUPOLA + cleanings.
!     ------------------------------------------------------------------

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

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KNSMIN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNSMAX 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDGLX 
REAL(KIND=JPRH)   ,INTENT(IN)    :: PDDSINTA(KDGLX) 
LOGICAL           ,INTENT(IN)    :: LDPNM 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNM(KDGLX,(KNSMIN+1)*(KNSMIN+2)/2+(KNSMAX-KNSMIN)*(KNSMIN+1)) 
REAL(KIND=JPRH)   ,INTENT(INOUT) :: PDDPNM(0:KNSMAX,0:KNSMAX) 
REAL(KIND=JPRH)   ,INTENT(INOUT) :: PDDA(0:KNSMAX) 
REAL(KIND=JPRH)   ,INTENT(INOUT) :: PDDI(0:KNSMAX) 

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

REAL(KIND=JPRH) :: ZDLC(0:KNSMAX,0:KNSMAX)
REAL(KIND=JPRH) :: ZDLD(0:KNSMAX,0:KNSMAX)
REAL(KIND=JPRH) :: ZDLE(0:KNSMAX,0:KNSMAX)
REAL(KIND=JPRH) :: ZDLB(0:KNSMAX)
REAL(KIND=JPRH) :: ZDLF(0:KNSMAX)
REAL(KIND=JPRH) :: ZDLG(0:KNSMAX)
REAL(KIND=JPRH) :: ZDLH(0:KNSMAX)
INTEGER(KIND=JPIM) :: IRDEB(0:KNSMIN)

INTEGER(KIND=JPIM), PARAMETER :: JPKS=KIND(PNM)
INTEGER(KIND=JPIM), PARAMETER :: JPKD=KIND(PDDPNM)

INTEGER(KIND=JPIM) :: IQUAD, JGL, JM, JN
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "supola.intfb.h"

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SUPLIS',0,ZHOOK_HANDLE)
!     ------------------------------------------------------------------

!*        1. Initialisations.
!            ----------------

IQUAD=1

IF (LDPNM) THEN
  DO JM=0,KNSMIN
    IRDEB(JM)=1+(KNSMAX+1)*(KNSMAX+2)/2-(KNSMAX+1-JM)*(KNSMAX+2-JM)/2
  ENDDO
ENDIF

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

!*        2. Computes Legendre Polynomials.
!            ------------------------------

DO JN=3,KNSMAX
  DO JM=2,JN-1
    ZDLC(JM,JN)=SQRT((REAL(2*JN+1,JPKD)*REAL(JN+JM-1,JPKD)*&
     & REAL(JN+JM-3,JPKD))&
     & /(REAL(2*JN-3,JPKD)*REAL(JN+JM,JPKD)*REAL(JN+JM-2,JPKD)))  
    ZDLD(JM,JN)=SQRT((REAL(2*JN+1,JPKD)*REAL(JN+JM-1,JPKD)*&
     & REAL(JN-JM+1,JPKD))&
     & /(REAL(2*JN-1,JPKD)*REAL(JN+JM,JPKD)*REAL(JN+JM-2,JPKD)))  
    ZDLE(JM,JN)=SQRT(REAL((2*JN+1)*(JN-JM),JPKD)/REAL((2*JN-1)*(JN+JM),JPKD))
  ENDDO
ENDDO

DO JN=1,KNSMAX
  PDDA(JN)=SQRT(REAL(2*JN+1,JPKD))
  ZDLB(JN)=SQRT(REAL(2*JN+1,JPKD)/REAL(JN*(JN+1),JPKD))
  ZDLF(JN)=REAL(2*JN-1,JPKD)/REAL(JN,JPKD)
  ZDLG(JN)=REAL(JN-1,JPKD)/REAL(JN,JPKD)
  ZDLH(JN)=SQRT(REAL(2*JN+1,JPKD)/REAL(2*JN,JPKD))
  PDDI(JN)=REAL(JN,JPKD)
ENDDO

DO JGL=1,KDGLX

  CALL SUPOLA(KNSMAX,PDDSINTA(JGL),PDDPNM,PDDA,ZDLB,ZDLC,ZDLD,ZDLE,ZDLF,ZDLG,&
   & ZDLH,PDDI)

  IF (LDPNM) THEN
    DO JM=0,KNSMIN
      DO JN=JM,KNSMAX
        PNM(JGL,IRDEB(JM)+KNSMAX-JN)=REAL(PDDPNM(JM,JN),JPKS)
      ENDDO
    ENDDO
  ENDIF

ENDDO

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SUPLIS',1,ZHOOK_HANDLE)
END SUBROUTINE SUPLIS

