SUBROUTINE SUGAWA35(KN,PL,PDL,PW)

!**** *SUGAWA35 * - Routine to initialize the Gaussian
!                   abcissa and the associated weights
!                   ARP version of CY35 version of tfl/module/sugaw_mod.F90
!                   (both versions must remain consistent)

!     Purpose.
!     --------
!           Initialize arrays PL,PDL and PW (quadrature abscissas and weights)
!**   Interface.
!     ----------
!        *CALL* *SUGAWA35(KN,PL,PDL,PW) *

!        Explicit arguments :
!        --------------------
!           INPUT:
!              KN       :  Number of Gauss  abscissas
!           OUTPUT:
!              PL (KN)  :  abscissas of Gauss
!              PDL(KN)  :  idem in double precision
!              PW (KN)  :  Weights of the Gaussian integration

!     PL (i) is the abscissa i starting from the northern pole, it is
! the cosine of the colatitude of the corresponding row of the collocation
! grid.

!        Implicit arguments :
!        --------------------
!       None

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

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

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

!     ARPEGE Documentation vol.2, ch3.

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

!     Modifications.
!     --------------
!        Original          : 87-10-15
!        Michel Rochas     : 90-08-30
!        Philippe Courtier : 92-12-19 Multitasking
!        Ryad El Khatib    : 94-04-20 Remove unused comdecks pardim and yomdim
!        Mats Hamrud       : 94-08-12 Printing level
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K. YESSAD (NOV 2008): make consistent arp/SUGAWA35 and tfl/SUGAW.
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RA       ,RPI
USE YOMLUN   , ONLY : NULOUT

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN) :: KN
REAL(KIND=JPRB),INTENT(OUT) :: PL(KN)
REAL(KIND=JPRH),INTENT(OUT) :: PDL(KN)
REAL(KIND=JPRB),INTENT(OUT) :: PW(KN)

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

REAL(KIND=JPRB) :: ZLI(KN),ZT(KN)
REAL(KIND=JPRB) :: ZREG(KN),ZMOD(KN),ZM(KN),ZR(KN)
INTEGER(KIND=JPIM) :: ITER(KN)
INTEGER(KIND=JPIM) :: IALLOW, INS2, ISYM, JGL

REAL(KIND=JPRB) :: Z, ZEPS, ZPI
LOGICAL :: LLP2
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "gawla35.intfb.h"

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

!*       1. Initialization.
!           ---------------

LLP2 = .FALSE.
ZPI=RPI
INS2 = KN/2+MOD(KN,2)

!*       1.1 Find first approximation of the roots of the
!           Legendre polynomial of degree KN.
DO JGL=1,INS2
  Z = REAL(4*JGL-1,JPRB)*ZPI/REAL(4*KN+2,JPRB)
  PL(JGL) = COS(Z+1.0_JPRB/(TAN(Z)*REAL(8*KN**2,JPRB)))
  ZREG(JGL) = COS(Z)
  ZLI(JGL) = PL(JGL)
ENDDO

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

!*      2. Computes roots and weights.
!          ---------------------------

ZEPS = EPSILON(Z)
!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(JGL)
DO JGL=1,INS2
  CALL GAWLA35(PL(JGL),PDL(JGL),PW(JGL),ZEPS,KN,ITER(JGL),ZMOD(JGL))
ENDDO
!$OMP END PARALLEL DO

!DIR$ IVDEP
!OCL NOVREC
DO JGL=1,KN/2
  ISYM = KN-JGL+1
  PL(ISYM) = -PL(JGL)
  PDL(ISYM) = -PDL(JGL)
  PW(ISYM) = PW(JGL)
ENDDO

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

!*      3. Diagnostics.
!          ------------

IF(LLP2)THEN
  DO JGL=1,INS2
    ZM(JGL) = (ACOS(PL(JGL))-ACOS(ZLI(JGL)))*RA
    ZR(JGL) = (ACOS(PL(JGL))-ACOS(ZREG(JGL)))*RA
    ZT(JGL) = ACOS(PL(JGL))*180._JPRB/ZPI
  ENDDO
ENDIF

IALLOW = 10
DO JGL=1,INS2
  IF(ITER(JGL) > IALLOW)THEN
    WRITE(UNIT=NULOUT,FMT='('' CONVERGENCE FAILED IN SUGAWA35 '')')
    WRITE(UNIT=NULOUT,FMT='('' ALLOWED : '',I4,''&
     &NECESSARY : '',&
     &I4)')IALLOW,ITER(JGL)
    CALL ABOR1(' FAILURE IN SUGAWA35 ')
  ENDIF

  IF(LLP2)THEN
    WRITE(UNIT=NULOUT,FMT=&
     &'('' ROW ='',I4,'' ITERATIONS='',I4,'' ROOT='',F30.20,&
     &'' WEIGHT='',F30.20,'' MODIF :'',E8.2)')JGL,ITER(JGL),PL(JGL)&
     &,PW(JGL),PL(JGL)-ZLI(JGL)
    WRITE(UNIT=NULOUT,FMT=&
     &'(10X,'' LAST INC. : '',E8.2,'' MODIF IN M : '',F10.3,&
     &'' FROM THE REGULAR GRID : '',F10.3,'' COLAT '',F10.3)')&
     &ZMOD(JGL),ZM(JGL),ZR(JGL),ZT(JGL)
  ENDIF
ENDDO

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

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