SUBROUTINE SUGRIDUA(KFILE)

!**** *SUGRIDUA*  - Initialize grid-point fields (GFL) from FA file

!     Purpose.
!     --------
!          To initialize grid-point fields (GFL) from FA file ; if not required in input
!          the fields are set to zero.

!     Interface.
!     ----------
!        *CALL* *SUGRIDUA

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

!        KFILE : an indicator for which  file is to be read         

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

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

!     Externals.
!     ----------
!        ABOR1
!        RDPXFA 

!     Reference.
!     ----------
!        Revised data flow in IFS/Arpege (By Mats Hamrud)
!        Arpege/Aladin files package (J. Clochard, R. El Khatib, D. Paradis)

!     Author.
!     -------
!        Eric Bazile *Meteo-France*

!     Modifications.
!     --------------
!        Original : 95-01-12
!        Modified 98-08-10 by K. YESSAD: removal of LRPOLE option.
!          - removal of LRPOLE in YOMCT0.
!          - removal of LLPOLE and code under LRPOLE, LLPOLE.
!        R. El Khatib 98-12-15 Fully rewritten
!        P.Smolikova 02-09-30 : Interface to INI2SCAN2M for d4 in NH
!        R. El Khatib 03-08-06 gfl.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        Y. Seity 04-11-16 Initialisation of TKE for AROME
!        Y. Bouteloup 06-April-2006 : No read of GFL in coupling file when NCOUPLING/=1
!------------------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NFLEVG   ,NPROMA
USE YOMGEM   , ONLY : NGPTOT
USE YOM_YGFL , ONLY : YGFL     ,YGFLC
USE YOMGFL   , ONLY : GFL 

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFILE 

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

INTEGER(KIND=JPIM) :: JGFL, IGFL, JLEV, IOFF, JKGLO, IBL, IST, IEND, IGGP, JROF

INTEGER(KIND=JPIM) :: IGPFIELDS  ! max number of fields*levels to read from file

INTEGER(KIND=JPIM), ALLOCATABLE :: ILEV(:)           ! fields levels

CHARACTER(LEN=4),  ALLOCATABLE :: CLPREF(:) ! fields name prefix
CHARACTER(LEN=12), ALLOCATABLE :: CLSUFF(:) ! fields name suffix

REAL(KIND=JPRB), ALLOCATABLE :: ZGPFIELDS(:,:)       ! data fields to be read from file
LOGICAL         :: LLREAD
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "rdpxfa.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('SUGRIDUA',0,ZHOOK_HANDLE)

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

!        1.    SETUP DESCRIPTORS ARRAYS
!              ------------------------

IGPFIELDS=YGFL%NUMGPFLDS*NFLEVG
ALLOCATE(CLPREF(IGPFIELDS),ILEV(IGPFIELDS),CLSUFF(IGPFIELDS))

IGFL=0
IGGP=0
DO JGFL=1,YGFL%NUMFLDS
  LLREAD = (YGFLC(JGFL)%NREQIN==1).AND.((YGFLC(JGFL)%NCOUPLING==1).OR.(KFILE/=9))
  SELECT CASE (YGFLC(JGFL)%LGP)
  CASE(.TRUE.)
    IGGP=IGGP+1
    SELECT CASE (LLREAD)
    CASE(.TRUE.)
      IGFL=IGFL+1
      IF (IGFL > YGFL%NUMGPFLDS) CALL ABOR1('SUGRIDUA : ERROR IGFL')
      IOFF=(IGFL-1)*NFLEVG 
      DO JLEV=1,NFLEVG
        CLPREF(IOFF+JLEV)='S   '
        ILEV(IOFF+JLEV)=JLEV
        CLSUFF(IOFF+JLEV)=YGFLC(JGFL)%CNAME(1:12)
      ENDDO
    END SELECT
  END SELECT
ENDDO
IF (IGGP /= YGFL%NUMGPFLDS) CALL ABOR1('SUGRIDUA : ERROR IGGP')

!        2.    READ AND DISTRIBUTE GRID-POINT FIELDS
!              -------------------------------------

IGGP=IGFL*NFLEVG
ALLOCATE(ZGPFIELDS(NGPTOT,IGGP))
CALL RDPXFA(ZGPFIELDS,CLPREF,ILEV,CLSUFF,IGGP,KFILE)
DEALLOCATE(CLPREF,ILEV,CLSUFF)

!        3.    COPY GRIDPOINT FIELDS TO BUFFER
!              -------------------------------

DO JKGLO=1,NGPTOT,NPROMA
  IBL=(JKGLO-1)/NPROMA+1
  IST=1
  IEND=MIN(NPROMA,NGPTOT-JKGLO+1)
  IGFL=0
  DO JGFL=1,YGFL%NUMFLDS
    LLREAD = (YGFLC(JGFL)%NREQIN==1).AND.((YGFLC(JGFL)%NCOUPLING==1).OR.(KFILE/=9))
    SELECT CASE (YGFLC(JGFL)%LGP)
    CASE(.TRUE.)
      SELECT CASE (YGFLC(JGFL)%NREQIN)
      CASE(1)
        SELECT CASE (LLREAD)
        CASE(.TRUE.)
          IGFL=IGFL+1
          IOFF=(IGFL-1)*NFLEVG 
          DO JLEV=1,NFLEVG
            DO JROF=IST,IEND
              GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)=ZGPFIELDS(JROF+JKGLO-1,IOFF+JLEV)
            ENDDO
          ENDDO
        CASE DEFAULT
          DO JLEV=1,NFLEVG
            DO JROF=IST,IEND
              GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)=0.0_JPRB
            ENDDO
          ENDDO
        END SELECT
      CASE(-1)
        DO JLEV=1,NFLEVG
          DO JROF=IST,IEND
            GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)=YGFLC(JGFL)%REFVALI
          ENDDO
        ENDDO
      CASE DEFAULT
        DO JLEV=1,NFLEVG
          DO JROF=IST,IEND
            GFL(JROF,JLEV,YGFLC(JGFL)%MP,IBL)=0.0_JPRB
          ENDDO
        ENDDO
      END SELECT
    END SELECT
  ENDDO
ENDDO

DEALLOCATE(ZGPFIELDS)

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

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