SUBROUTINE READ_SPEC_FROMFA(KSTEP,YD_SPEC,LDBACKGR)

! ------------------------------------------------------------------
!     Purpose.
!     --------
!       Read spectral fields from ARPEGE files, one file per range.
!       If LDBACKGR=F, the ARPEGE files contains the trajectory coming from
!       a "high resolution" run, transformed at the current
!       resolution by an ad-hoc procedure (for example FULL-POS).
!       If LDBACKGR=T, the ARPEGE files contains the background.

!     * INPUT:
!       KSTEP:     step numbering for the trajectory

!     * INPUT/OUTPUT:
!       YD_SPEC:   buffer to store the trajectory

!     * INPUT OPTIONAL:
!       LDBACKGR:  T (background file); F (trajectory file).

!     Author.
!     -------
!       K. Yessad, after routine READ_SPEC_FROMFA of Yannick Tremolet *ECMWF*

!     Modifications.
!     --------------
!       Original  : April 2005
!       K.Yessad  : June 2006   Possibility to read purely gp ARPEGE files
! ------------------------------------------------------------------

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

USE YOMLUN, ONLY : NULOUT
USE YOMCT0, ONLY : LELAM    ,CNMEXP   ,NPRINTLEV, &
 & LARPEGEF_RDGP_TRAJHR,LARPEGEF_RDGP_TRAJBG
USE YOMOP , ONLY : CFNTRAJHR,CFNTRAJBG
USE YOMDIM, ONLY : NS3D     ,NS1D
USE YOMSP , ONLY : SPA3     ,SPA2     ,SPA1     ,SPSP
USE SPECTRAL_FIELDS_MOD, ONLY : SPECTRAL_FIELD

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
TYPE(SPECTRAL_FIELD),INTENT(INOUT) :: YD_SPEC 
LOGICAL           ,INTENT(IN), OPTIONAL :: LDBACKGR

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

INTEGER(KIND=JPIM) :: IFILE
CHARACTER(LEN=16)  :: CLFNTRAJHR_SAVE
CHARACTER(LEN=16)  :: CLFNTRAJBG_SAVE
CHARACTER(LEN=4)   :: CLSTEP
LOGICAL            :: LLBACKGR

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"
#include "copy_spa2spec.intfb.h"
#include "suspec.intfb.h"

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

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

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

IF (LELAM) CALL ABOR1(' READ_SPEC_FROMFA: lelam case not implemented ')

IF (NPRINTLEV >= 2) THEN
  WRITE(NULOUT,*) ' Entering READ_SPEC_FROMFA '
  CALL FLUSH(NULOUT)
ENDIF

IF (PRESENT(LDBACKGR)) THEN
  LLBACKGR=LDBACKGR
ELSE
  LLBACKGR=.FALSE.
ENDIF

IF (LLBACKGR) THEN

  ! * save CFNTRAJBG
  CLFNTRAJBG_SAVE=CFNTRAJBG

  ! * fill SPA3,SPSP,SPA1 with the background read on an ARPEGE file.
  !   This ARPEGE file is assumed to contain data at truncation NSMAX.
  !   The part of SUSPEC which is used is the same as for reading the
  !   initial files.
  IFILE=51
  WRITE(CLSTEP,'(I4.4)') KSTEP-1
  CFNTRAJBG='ICMRF'//CNMEXP(1:4)//CLSTEP//'    '

  IF (NPRINTLEV >= 2) THEN
    WRITE(NULOUT,'(A,A16)') ' READ_SPEC_FROMFA: read file ',CFNTRAJBG
    WRITE(NULOUT,'(A)') ' and copy on SPA3,SPSP,SPA1.'
  ENDIF
  CALL SUSPEC(SPA3,SPA2,SPA1,KFILE=IFILE,LDRDGRIDSP=LARPEGEF_RDGP_TRAJBG)

  ! * Restore the initial content of CFNTRAJBG
  CFNTRAJBG=CLFNTRAJBG_SAVE

ELSE

  IF (KSTEP > 1) THEN

    ! * save CFNTRAJHR
    CLFNTRAJHR_SAVE=CFNTRAJHR

    ! * fill SPA3,SPSP,SPA1 with the trajectory read on an ARPEGE file.
    !   This ARPEGE file is assumed to contain data at truncation NSMAX.
    !   The part of SUSPEC which is used is the same as for reading the
    !   initial files.
    IFILE=50
    WRITE(CLSTEP,'(I4.4)') KSTEP-1
    CFNTRAJHR='TRAJ'//CNMEXP(1:4)//CLSTEP//'    '

    IF (NPRINTLEV >= 2) THEN
      WRITE(NULOUT,'(A,A16)') ' READ_SPEC_FROMFA: read file ',CFNTRAJHR
      WRITE(NULOUT,'(A)') ' and copy on SPA3,SPSP,SPA1.'
    ENDIF

    IF (CFNTRAJHR == 'TRAJ'//CNMEXP(1:4)//'0000    ') THEN
      WRITE(NULOUT,'(A,A16,A)') ' File ',CFNTRAJHR,' should not be read!'
      CALL ABOR1(' READ_SPEC_FROMFA: ABOR1 called ')
    ENDIF

    CALL SUSPEC(SPA3,SPA2,SPA1,KFILE=IFILE,LDRDGRIDSP=LARPEGEF_RDGP_TRAJHR)

    ! * Restore the initial content of CFNTRAJHR
    CFNTRAJHR=CLFNTRAJHR_SAVE

  ENDIF

ENDIF

! * Transfer the content of SPA3,SPSP,SPA1 into YD_SPEC:
CALL COPY_SPA2SPEC(NS3D,1,NS1D,SPA3,SPSP,SPA1,YD_SPEC)

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

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