SUBROUTINE SUINIF(KFILE,LDRDGRIDSP)

!**** *SUINIF*  - Routine to initialize the fields of the model.

!     Purpose.
!     --------
!           Initialize the fields of the model.

!**   Interface.
!     ----------
!        *CALL* *SUINIF(KFILE)*

!        Explicit arguments :
!        --------------------
!        KFILE : an indicator for which  file is to be read
!               KFILE = 0 the CFNISH file is read
!               KFILE = 1 the CFNGSH file is read
!               KFILE = 2 the CFNRF  file is read
!               KFILE = 3 the CFNBS file is read (bias, for recursive dfi)
!               KFILE = 4 the CFNGG  file is read
!               KFILE = 5 the CFNPL  file is read
!               KFILE = 6 the CFNFGI file is read
!               KFILE = 7 the CFNANI file is read
!               KFILE =12 the CFNBGHRSH/GG file is read
!        LDRDGRIDSP: T (resp. F): GMV, GMVS and spectral GFL read as grid-point
!               fields (resp. spectral fields) on files.

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

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

!     Externals.     SUSPEC  - initialize spectral fields
!     ----------     SUGRIDF - initialize gridpoint fields
!                    SUGRCFU - initialize cum. fluxes from input file
!                    SUGRXFU - initialize inst. fluxes from input file
!                    SUGRIDO - initialize ocean mixed layer gridpoint fields

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS
!        Note de travail ARPEGE Nr 12 et 17

!     Author.
!     -------
!        Mats Hamrud   *ECMWF*

!     Modifications.
!     --------------
!        Original : 91-11-14
!        Modified : 94-03-03 R. El Khatib (initialize fluxes from input file)
!        Modified : 94-05-16 C. Temperton (spectral RT option)
!        Modified : 94-06-30 M. Hamrud (upper air grid-point work file)
!        M. Hamrud  :  95-10-01 - Interface block for SUSPEC
!        Modified : 94-06-30 R. El Khatib (initial fluxes read from separate files)
!        E. Andersson 960425 : Provision for AN and FG gridpoint q
!        K. YESSAD: 97-06-13 : Call of SPECFITA if DM.
!        E. Holm  : 97-11-28 : Ozone included
!        L. Dragulanescu: 97-05 ALADIN 801
!        R. El Khatib : 98-04-02 Cleanup ALADIN 801
!        P. Neytchev : 98-06-28 Removing argument from the call of ESPECRT
!        R. El Khatib : 00-12-01 Fix for the adiabatic model
!        M. Szczech : 01-03-20 Adding argument for the call of ESPECRT
!        P.Smolikova 02-09-30 : Interface to SUSPEC for d4 in NH
!        R. El Khatib : 02-02-19 SPECFITA rewritten
!        G. Hello : 03-09-19 : call to spnh_conv_nhvar (case NH)
!        M. Janiskova : 03-05 call for sugridu when LENCLD2
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!        M.Hamrud      01-Dec-2005 Generalized IO scheme
!        M.Hamrud      01-Jul-2006 Revised surface fields
!        K.Yessad      June 2006   Possibility to read purely gp ARPEGE files
!        J.Haseler     02-Mar-2007 Trajectory fields may not be available
!        N.Wedi/K.Yessad (Jan 2008): different dev for NH model and PC scheme
!        E.Holm        13-Nov-2008 Add high resolution background=12, used
!                                  by rdfpinc for adding increments
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!        R. El Khatib : 09-99-2008 : fix use of LNHDYN+LSPRT
!        R. El Khatib  26-Nov-2008  Cleaning
!        Y.Takaya      29-Jan-2009 Ocean mixed layer fields
!     -----------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0   , ONLY : LFPART2  ,NCONF    ,LARPEGEF ,LSPRT    ,&
 & LFBDAP   ,LELAM    ,LNHDYN   ,NPRINTLEV, LIFSMIN
USE YOMDYNA  , ONLY : LRWSDLR
USE YOMSP    , ONLY : SPOR     ,SPA3 ,SPA2, SPA1
USE YOMPHY   , ONLY : LREASUR  
USE YOEPHY   , ONLY : LEOCML  !KPP
USE YOMCFU   , ONLY : LCUMFU   ,LREACFU
USE YOMXFU   , ONLY : LXFU     ,LREAXFU
USE YOPHNC   , ONLY : LENCLD2
USE YOM_YGFL , ONLY : YGFL
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0B  , ONLY : LECMWF
USE TRAJECTORY_MOD, ONLY : LTRAJHR

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFILE
LOGICAL,OPTIONAL,INTENT(IN)      :: LDRDGRIDSP

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

INTEGER(KIND=JPIM) :: IFILE
LOGICAL :: LLINOR, LLSPOR
LOGICAL :: LLRDGRIDSP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "especrt.intfb.h"
#include "specfita.intfb.h"
#include "specfitg.intfb.h"
#include "specrt.intfb.h"
#include "spnh_conv_nhvar.intfb.h"
#include "spnh_conv_prhs.intfb.h"
#include "sugrcfu.intfb.h"
#include "sugridf.intfb.h"
#include "sugridu.intfb.h"
#include "sugrido.intfb.h"
#include "sugrxfu.intfb.h"
#include "suspec.intfb.h"
#include "spnorm.intfb.h"

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

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

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

!*       0.    PRELIMINARY SPECTRAL FIT OF PRE-PROCESSED DATA
!              ----------------------------------------------

CALL GSTATS(14,0)

IF (PRESENT(LDRDGRIDSP)) THEN
  LLRDGRIDSP=LDRDGRIDSP
ELSE
  LLRDGRIDSP=.FALSE.
ENDIF

IF (LFPART2) THEN
  IF(LARPEGEF) THEN
    CALL SPECFITA
    IF (LHOOK) CALL DR_HOOK('SUINIF',1,ZHOOK_HANDLE)
    RETURN
  ELSE
    CALL SPECFITG
  ENDIF
ENDIF

!*       1.    INITIALIZE SPECTRAL FIELDS
!              ---------------------------

LLINOR=(NCONF /= 131.OR. LFPART2 .OR. .NOT.LECMWF) .AND. &
 & (KFILE /= 6.AND. KFILE /= 7)
CALL SUSPEC(SPA3,SPA2,SPA1,KFILE=KFILE,&
 & LDRDGRIDSP=LLRDGRIDSP,LDINOR=LLINOR,LDSPOR=LLSPOR)  
IF (NPRINTLEV >=2) THEN
  WRITE(NULOUT,'(A,I3,A)') ' SUINIF: SUSPEC(KFILE=',KFILE,') called '
  CALL FLUSH(NULOUT)
ENDIF

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

!*       2.    INITIALIZE GRID POINT FIELDS
!              ----------------------------

!*       2.1   SURFACE
IF(LREASUR) THEN
  CALL SUGRIDF(KFILE,SPOR,LLSPOR)
ENDIF

!*       2.2   OCEAN MIXED LAYER (KPP)
IF(LEOCML) THEN
  CALL SUGRIDO(KFILE)
ENDIF

!*       2.3   UPPER AIR
IF( YGFL%NUMGPFLDS>0 .OR. LENCLD2 ) THEN
  CALL SUGRIDU(KFILE)
ENDIF

!*       2.4   COMPUTE RT FROM T AND Q
!*       2.5   COMPUTE PD VD for NH case
IF (LNHDYN) THEN
  IF (.NOT.LFPART2) THEN
    ! Inside the post-processing the variables are not scaled
    ! in order to remain independent from the model SI scheme.
    IF (LRWSDLR) CALL SPNH_CONV_PRHS(.FALSE.)
    CALL SPNH_CONV_NHVAR(.FALSE.)
  ENDIF
ENDIF

!*       2.6   COMPUTE RT FROM T AND Q

IF (LSPRT) THEN
  IF (LELAM) THEN
    CALL ESPECRT(0)
  ELSE
    IF (.NOT.(LIFSMIN .AND. LTRAJHR)) THEN
      CALL SPECRT(0)
    ENDIF
  ENDIF
ENDIF

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

!*       3.    INITIALIZE CUMULATED FLUXES FROM INPUT FILE
!              -------------------------------------------

IF(LCUMFU.AND.LREACFU) THEN
  IF (LFBDAP) THEN
    CALL SUGRCFU(KFILE)
  ELSE
    IFILE=6
    CALL SUGRCFU(IFILE)
  ENDIF
ENDIF

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

!*       4.    INITIALIZE INSTANTANEOUS FLUXES FROM INPUT FILE
!              -----------------------------------------------

IF(LXFU.AND.LREAXFU) THEN
  IF (LFBDAP) THEN
    CALL SUGRXFU(KFILE)
  ELSE
    IFILE=7
    CALL SUGRXFU(IFILE)
  ENDIF
ENDIF
CALL GSTATS(14,1)

IF (.NOT.(LIFSMIN .AND. LTRAJHR)) THEN
  CALL SPNORM(0)
ENDIF
CALL FLUSH(NULOUT)

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

