SUBROUTINE STATE2SPEC(YDSPEC)

!    Purpose.
!    --------
!      Copy hard coded model state into spectral field.

!    Author.
!    -------
!      Y.Tremolet

!    Modifications.
!    --------------
!      Original: 03-Aug-2005
!-----------------------------------------------------------------------

USE PARKIND1, ONLY: JPRB, JPIM
USE YOMHOOK , ONLY: LHOOK, DR_HOOK
USE YOMDIM  , ONLY: NSMAX, NS3D, NFD2D, NPROMA
USE YOMMP   , ONLY: NPSP, NBSETLEV
USE YOMSP   , ONLY: SPA3, SPA2
USE YOMGFL  , ONLY: GFL
USE YOM_YGFL, ONLY: YGFLC, YGFL ! YQ, YO3 
USE SPECTRAL_FIELDS_MOD

IMPLICIT NONE
TYPE(SPECTRAL_FIELD), INTENT(INOUT) :: YDSPEC

! INTEGER(KIND=JPIM) :: igrd
INTEGER(KIND=JPIM) :: JF, JGFL
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "dir_trans.h"
END INTERFACE
#include "abor1.intfb.h"

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

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

IF (YDSPEC%NSMAX/=NSMAX) CALL ABOR1('STATE2SPEC: Error resolution')
!igrd=0
!IF (YQ%LGP)  igrd=igrd+1
!IF (YO3%LGP) igrd=igrd+1

!IF (YDSPEC%NS3D/=NS3D+igrd) CALL ABOR1('STATE2SPEC: Error 3D fields')
!IF (YDSPEC%NS2G/=NFD2D) CALL ABOR1('STATE2SPEC: Error 2D fields')

YDSPEC%SP3D(:,:,1:NS3D)=SPA3(:,:,:)
IF (NPSP==1) YDSPEC%SP2D(:,:)=SPA2(:,1:NFD2D)

!IF (YQ%LGP) THEN
!  CALL DIR_TRANS(PGP=GFL(:,:,YQ%MP,:),PSPSCALAR=YDSPEC%HUM, &
!               & KVSETSC=NBSETLEV,KPROMA=NPROMA)
!ENDIF

!IF (YO3%LGP) THEN
!  CALL DIR_TRANS(PGP=GFL(:,:,YO3%MP,:),PSPSCALAR=YDSPEC%OZN, &
!               & KVSETSC=NBSETLEV,KPROMA=NPROMA)
!ENDIF

DO JF=1,YDSPEC%NS3D
  DO JGFL=1,YGFL%NUMFLDS
    IF(YDSPEC%NGRIB3(JF)==YGFLC(JGFL)%IGRBCODE .AND. YGFLC(JGFL)%LGP) THEN
      CALL DIR_TRANS(PGP=GFL(:,:,YGFLC(JGFL)%MP,:),PSPSCALAR=YDSPEC%SP3D(:,:,JF), &
               & KVSETSC=NBSETLEV,KPROMA=NPROMA)
    ENDIF
  ENDDO
ENDDO

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