SUBROUTINE SPEC2STATE(YDSPEC)

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

!    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: YQ, YO3
USE SPECTRAL_FIELDS_MOD

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

INTEGER(KIND=JPIM) :: igrd
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

IF (YDSPEC%NSMAX/=NSMAX) CALL ABOR1('SPEC2STATE: Error resolution')
igrd=0
IF (YQ%LGP)  igrd=igrd+1
IF (YO3%LGP) igrd=igrd+1
IF (YDSPEC%NS3D/=NS3D+igrd) CALL ABOR1('SPEC2STATE: Error 3D fields')
IF (YDSPEC%NS2G/=NFD2D) CALL ABOR1('SPEC2STATE: Error 2D fields')

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

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

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

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