SUBROUTINE SUSPEC(PSPA3,PSPA2,PSPA1,CDPATH,KFILE,LDRDGRIDSP,LDINOR,LDSPOR)  

!**** *SUSPEC*  - Routine to initialize the spectral fields of the model.

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

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

!        Explicit arguments :
!        --------------------
!        KFILE : an indicator for which spectral 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 = 5 the CFNPL  file is read
!               KFILE =12 the CFNBGHRSH file is read
!               PSPVOR etc. - spectral fields
!               LDRDGRIDSP: T (resp. F): GMV, GMVS and spectral GFL read as
!                g.p. fields (resp. spectral fields) on files.
!               LDINOR - switch for initializing the orography
!               LDSPOR - switch indicating whether spectral orography
!                        field has been read

!        Implicit arguments :
!        --------------------
!        The spectral fields of the model.
!        The boundary condition fields of the model.

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

!     Externals.  SUSPECB - initialize 2D model
!     ----------  SUSPECA/SUGRIDSPA - initialize from ARPEGE file
!                 SUSPECG/SUSPECG1 - initilaize from GRIB file
!                 SUOROG  - initialize gridpoint orography
!                 SUSPECTCFOU- initialize Fou.transmis.coef.for simpl.rad.scheme

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

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

!     Modifications.
!     --------------
!        Original : 87-12-14
!        Modification : 90-08  (Yves Bouteloup *EERM/CRMD*)
!                       use of *FA* file
!      Modified 91-02-28 by Michel Deque (Relaxation of deep soil values)
!        Christain Blondin : 90-12-25 : PHYSICS field initialization
!        Bill Heckley      : 90-09-06 : option to read from any of the
!                                       spectral files, depending on the
!                                       argument KFILE
!        Modified 91-09-25 by A.Lasserre-Bigorry and M. Deque (scalar variables)
!        modified by M. Deque 91-10-01 (polar values for VCLIP;
!                            default values for VCLIN, VARSF(6) and VARSF(7)
!        Modified 91-11-25 by M.Hamrud (split into SUSPEC and SUGRIDF)
!        Modified 92-02-12 by R. El Khatib (Check file *FA* with namelist)
!        Modified 92-09-15 by F. Taillefer (Start with climatologic file).
!        Modified 93-04-13 by L. Labbe (Initialize grid-point orography for
!                                       2D models)
!        Modified 93-05-15 by M. Hamrud (Initialize from GRIB file)
!        Modified 93-08-24 by D. Giard (KFILE=3)
!        Modified 93-08-24 by R. El Khatib (KFILE=5)
!        Modified 95-06-06 by L. Isaksen (Reordering of spectral arrays)
!        Modified 95-10-01 by M.Hamrud (F90 style passing of args.)
!        Modified 95-12-12 by M.Hamrud (Initialize NFLEVL+1 -NFLSUR)
!        Modified 96-03-01 by P.Courtier (Bugfix i s.w.)
!        Modified 97-09-18 by G.Mozdzynski Support artificial data (N3DINI=1)
!        Modified 96-11-25 by M.Janiskova (Initialize transmission coef.
!                                          for simplified radiation scheme
!        Modified 98-02-11 by R. El Khatib (Move SUEOROG inside SUOROG)
!        Modified 98-04-01 by R. El Khatib (remove LELAM)
!        Modified 97-11-28 by E. Holm (Ozone included)
!        Modified 99-02-17 by K. YESSAD: options LRETCFOU, LWRTCFOU.
!        Modified 99-04-16 by R. El Khatib suspecasm+dm => suspeca + suspecabld
!        Modified 00-04-18 by M. SZCZECH: spectral thermodynamic ozon
!        Modified 01-04-20 by R. El Khatib : LARPEGEF + N3DINI for tests
!        Modified 02-09-30 by P.Smolikova : d4 in NH (interface to SUSPECA)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!        M.Hamrud      01-Dec-2005 Generalized IO scheme
!        K.Yessad      June 2006   Possibility to read purely gp ARPEGE files
!        E.Holm        13-Nov-2008 (KFILE=12)
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0   , ONLY : NCONF    ,LARPEGEF ,&
 &  N3DINI   ,LRETCFOU ,NPRINTLEV 
USE YOMCT0B  , ONLY : LECMWF
USE YOMDIM   , ONLY : NFLEVL   ,NSPEC2
USE YOMSP_PTRS , ONLY : MSPTR_VOR, MSPTR_DIV, MSPTR_T, MSPTR_SPD,&
 & MSPTR_SVD, MSPTR_GFL, MSPTR_SP, MSPTR_OR, MSPTR_UB, MSPTR_VB
USE YOM_YGFL , ONLY : YGFL

IMPLICIT NONE

REAL(KIND=JPRB),TARGET     ,INTENT(OUT)   :: PSPA3(:,:,:) 
REAL(KIND=JPRB),TARGET     ,INTENT(OUT)   :: PSPA2(:,:)
REAL(KIND=JPRB),TARGET     ,INTENT(OUT)   :: PSPA1(:,:)
CHARACTER(LEN=*)  ,OPTIONAL,INTENT(IN)    :: CDPATH
INTEGER(KIND=JPIM),OPTIONAL,INTENT(IN)    :: KFILE 
LOGICAL           ,OPTIONAL,INTENT(IN)    :: LDRDGRIDSP
LOGICAL           ,OPTIONAL,INTENT(IN)    :: LDINOR 
LOGICAL           ,OPTIONAL,INTENT(INOUT) :: LDSPOR 

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

LOGICAL :: LLDATA,LLINOR,LLSPOR,LLRDGRIDSP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "sugridspa.intfb.h"
#include "suorog.intfb.h"
#include "suspectcfou.intfb.h"
#include "suspeca.intfb.h"
#include "suspecb.intfb.h"
#include "suspecg.intfb.h"
#include "suspecg1.intfb.h"
#include "suspecg2.intfb.h"

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

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

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

CALL GSTATS(19,0)

IF (NPRINTLEV >=2) THEN
  WRITE(NULOUT,'(A,I3)') ' ENTERING SUSPEC WITH KFILE= ',KFILE
  CALL FLUSH(NULOUT)
ENDIF

LLINOR=.FALSE.
IF(PRESENT(LDINOR)) LLINOR = LDINOR

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

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

!*       1.    INITIALIZE SPECTRAL FIELDS FOR BAROTROPIC MODELS.
!              -------------------------------------------------

IF(NCONF == 201.OR.NCONF == 421.OR.NCONF == 521.OR.&
   & NCONF == 202.OR.NCONF == 422.OR.NCONF == 522) THEN  

  CALL SUSPECB(KFILE,PSPA3(:,:,MSPTR_VOR),PSPA3(:,:,MSPTR_DIV),&
   & PSPA2(:,MSPTR_SP),PSPA2(:,MSPTR_OR))
  LDSPOR=.TRUE.

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

!*       2.    INITIALIZE SPECTRAL FIELDS (3D)
!              -------------------------------

!ELSEIF(NCONF == 1 .OR.NCONF == 302 &
!   & .OR.NCONF == 401.OR.NCONF == 501.OR.NCONF == 601 &
!   & .OR.NCONF == 701.OR.NCONF == 131.OR.NCONF == 801) THEN  
ELSE

  IF (LARPEGEF.AND.(N3DINI == 0)) THEN

!*       2.1   INITIALIZE SPECTRAL FIELDS FROM *FA* FILE.

    IF(LLRDGRIDSP) THEN
      ! fields are stored in grid-point on files.
      CALL SUGRIDSPA(LLINOR,KFILE,LLDATA,&
       & PSPA3(:,:,MSPTR_VOR),PSPA3(:,:,MSPTR_DIV),&
       & PSPA3(:,:,MSPTR_T),PSPA3(:,:,MSPTR_GFL:MSPTR_GFL+YGFL%NUMSPFLDS-1),&
       & PSPA2(:,MSPTR_SP),PSPA2(:,MSPTR_OR),&
       & PSPA3(:,:,MSPTR_SPD),PSPA3(:,:,MSPTR_SVD),&
       & PSPA1(:,MSPTR_UB),PSPA1(:,MSPTR_VB))  
      IF (NPRINTLEV >=2) THEN
        WRITE(NULOUT,*) ' SUSPEC: SUGRIDSPA called '
        CALL FLUSH(NULOUT)
      ENDIF
    ELSE
      ! fields are stored in spectral on files.
      CALL SUSPECA(KFILE,LLDATA,&
       & PSPA3(:,:,MSPTR_VOR),PSPA3(:,:,MSPTR_DIV),&
       & PSPA3(:,:,MSPTR_T),PSPA3(:,:,MSPTR_GFL:MSPTR_GFL+YGFL%NUMSPFLDS-1),&
       & PSPA2(:,MSPTR_SP),PSPA2(:,MSPTR_OR),&
       & PSPA3(:,:,MSPTR_SPD),PSPA3(:,:,MSPTR_SVD),&
       & PSPA1(:,MSPTR_UB),PSPA1(:,MSPTR_VB),LLINOR)  
      IF (NPRINTLEV >=2) THEN
        WRITE(NULOUT,*) ' SUSPEC: SUSPECA called '
        CALL FLUSH(NULOUT)
      ENDIF
    ENDIF
    IF(PRESENT(LDSPOR))LDSPOR=.TRUE.

  ELSEIF ( N3DINI == 0 ) THEN

!*       2.2   INITIALIZE SPECTRAL FIELDS FROM GRIB FILES

    IF(LLRDGRIDSP) THEN
      CALL ABOR1('SUSPEC: option LLRDGRIDSP not yet implemented for GRIB')
      ! future CALL SUGRIDSPG.
    ELSE
      CALL SUSPECG(PSPA3,PSPA2,CDPATH,KFILE,LLINOR,LLSPOR)  
    ENDIF

  ELSEIF( N3DINI == 1 ) THEN

!*       2.2   INITIALIZE SPECTRAL FIELDS FROM ARTIFICIAL DATA
    IF(LLRDGRIDSP) THEN
      CALL ABOR1('SUSPEC: option LLRDGRIDSP not yet implemented for GRIB')
      ! future CALL SUGRIDSPG1.
    ELSE
      CALL SUSPECG1(PSPA3(:,:,MSPTR_VOR),PSPA3(:,:,MSPTR_DIV),&
       & PSPA3(:,:,MSPTR_T),PSPA3(:,:,MSPTR_GFL:MSPTR_GFL+YGFL%NUMSPFLDS-1),&
       & PSPA2(:,MSPTR_SP),PSPA2(:,MSPTR_OR),&
       & LLINOR,LDSPOR)  
    ENDIF

  ELSE

    !*       2.3   INITIALIZE SPECTRAL FIELDS FOR LDYNCORE = T
    IF(LLRDGRIDSP) THEN
      CALL ABOR1('SUSPEC: option LLRDGRIDSP not yet implemented for GRIB')
      ! future CALL SUGRIDSPG2 ???
    ELSE  
      CALL SUSPECG2(N3DINI,PSPA3,PSPA2,CDPATH,KFILE,LLINOR,LLSPOR)
    ENDIF

  ENDIF

!*       2.3   INITIALIZE OVERDIMENSIONED SPECTRAL FIELDS

  PSPA3(NFLEVL+1:,:,:) = 0.0_JPRB
   
ENDIF

!*       2.3  INITIALIZE TRANSMISSION COEFFICIENTS FOR SIMPLIFIED
!             RADIATION SCHEME

! * storage in Fourier coefficients.
IF (LRETCFOU) THEN
  CALL SUSPECTCFOU
ENDIF

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

!*       3.    INITIALIZE GRIDPOINT OROGRAPHY.
!              -------------------------------

!*       3.1   INITIALIZE GRIDPOINT OROGRAPHY

IF(LECMWF) THEN
  IF(LLINOR.AND.LLSPOR) THEN
    WRITE(NULOUT,'(" OROGRAPHY INITIALISED FROM SPECTRAL INPUT")')
    CALL SUOROG(PSPA2(:,MSPTR_OR))
  ENDIF
  IF(PRESENT(LDSPOR)) LDSPOR=LLSPOR
ELSE
  IF(LLINOR) THEN
    WRITE(NULOUT,'(" OROGRAPHY INITIALISED FROM SPECTRAL INPUT")')
    CALL SUOROG(PSPA2(:,MSPTR_OR))
  ENDIF
ENDIF

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

CALL GSTATS(19,1)

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