SUBROUTINE JBTOMODEL(YDSPJB,PGP3,PGP2)

USE PARKIND1  ,ONLY : JPRB, JPIM
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMLUN   , ONLY : NULOUT
USE YOMDIM   , ONLY : NFLEVG, NPROMA
USE YOMSP    , ONLY : SPA3,   SPA2,   SPA1, SPGFL
USE YOMGFL   , ONLY : GFL,GFL5
USE YOMMP    , ONLY : NPSP, NBSETLEV
USE YOMJG    , ONLY : N_SPJB_VARS, SPJB_VARS_INFO, LSPFCE, &
              &  LJB_NONLINEAR_CVHUM, LJB_NONLINEAR_CVO3
USE YOMCT0   , ONLY : LELAM
USE YOMGRB   , ONLY : NGRBQ, NGRBO3, NGRBNOXLOG
USE SPECTRAL_FIELDS_MOD

#ifdef DOC

!**** *JBTOMODEL * - Routine to transfer control variable spectral fields
!                    to model state fields

!     Purpose.
!     --------
!           Transfer for SPA3JB, SPA2JB and SPA1JB
!           to SPA3, SPA2, SPA1 and possibly GFL

!**   Interface.
!     ----------
!        *CALL* *JBTOMODEL*

!     Explicit arguments :  None
!     --------------------

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

!     Method.
!     -------
!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Lars Isaksen *ECMWF*

!     Modifications.
!     --------------
!        Original : 2004-10-26
!        Mike Fisher : 2005-06-08 Generalize Jb for GEMS
!        C. Fischer 21-May-2005  Aladin spectral transforms added
!        M. Fisher  31-01-2008   PGP3, PGP2
!        A. Dethof  11-11-2008   LOGVAR JB for NOx
!        E. Holm    15-07-2009   When L_IN_SPGFL and LJB_NONLINEAR_CVHUM/O3
!                                then Jb q/o3 are in gridpoint space and need to 
!                                be transformed to spectral space.
!                               
#endif

IMPLICIT NONE

TYPE(SPECTRAL_FIELD), INTENT(IN) :: YDSPJB
REAL(KIND=JPRB), INTENT(IN) :: PGP3(:,:,:),PGP2(:,:,:)

INTEGER(KIND=JPIM) :: JFIELD, IPT, IPTJB, J
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "dir_trans.h"
END INTERFACE

#include "abor1.intfb.h"
#include "etransinv_jbtomodel.intfb.h"
#include "transinv_jbtomodel.intfb.h"

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

!--- Loop over Jb variables

!CALL GSTATS(1454,0)
DO JFIELD=1,N_SPJB_VARS
  IPTJB = SPJB_VARS_INFO(JFIELD)%IPTJB
  IPT   = SPJB_VARS_INFO(JFIELD)%IPT

  IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA2) THEN

    IF (NPSP>0) THEN
      SPA2(:,IPT)= YDSPJB%SP2D(:,IPTJB)
    ENDIF

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA3) THEN

!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(J)
    DO J=1,SIZE(YDSPJB%SP3D,2)
      SPA3(:,J,IPT)= YDSPJB%SP3D(:,J,IPTJB)
    ENDDO
!$OMP END PARALLEL DO

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_SPGFL) THEN

    IF (      (LJB_NONLINEAR_CVHUM .AND. SPJB_VARS_INFO(JFIELD)%IGRIBCODE == NGRBQ) &
       & .OR. (LJB_NONLINEAR_CVO3 .AND. SPJB_VARS_INFO(JFIELD)%IGRIBCODE == NGRBO3) ) THEN
      CALL DIR_TRANS (PGP=PGP3(:,1+(IPTJB-1)*NFLEVG:IPTJB*NFLEVG,:), &
                     & KVSETSC=NBSETLEV, KPROMA=NPROMA, PSPSCALAR=SPGFL(:,:,IPT))  
    ELSE
!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(J)
      DO J=1,SIZE(YDSPJB%SP3D,2)
        SPGFL(:,J,IPT)= YDSPJB%SP3D(:,J,IPTJB)
      ENDDO
!$OMP END PARALLEL DO
    ENDIF

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_GPGFL.AND. &
   &      SPJB_VARS_INFO(JFIELD)%IGRIBCODE /= NGRBNOXLOG ) THEN

    IF (LSPFCE) THEN
      IF (LELAM) THEN
        CALL ETRANSINV_JBTOMODEL (YDSPJB%SP3D(:,:,IPTJB),GFL(:,:,IPT,:))
      ELSE
        CALL TRANSINV_JBTOMODEL (YDSPJB%SP3D(:,:,IPTJB),GFL(:,:,IPT,:))
      ENDIF
    ELSE

!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(J)
      DO J=1,SIZE(PGP3,3)
        GFL(:,:,IPT,J) = PGP3(:,1+(IPTJB-1)*NFLEVG:IPTJB*NFLEVG,J)
      ENDDO
!$OMP END PARALLEL DO

    ENDIF

  ELSEIF (SPJB_VARS_INFO(JFIELD)%L_IN_GPGFL.AND. &
   &      SPJB_VARS_INFO(JFIELD)%IGRIBCODE == NGRBNOXLOG ) THEN  

    GFL(:,:,IPT,:) = PGP3(:,1+(IPTJB-1)*NFLEVG:IPTJB*NFLEVG,:)*GFL5(:,:,IPT,:)
  ELSE

    WRITE (NULOUT,*) 'DON''T KNOW WHERE TO PUT JB MODEL FIELD : ',JFIELD
    CALL ABOR1 ('DON''T KNOW WHERE TO PUT JB MODEL FIELD')

  ENDIF
ENDDO
!CALL GSTATS(1454,1)

!-- SPA1 is a special case!

IF (LELAM) SPA1(:,:)=YDSPJB%SP1D(:,:)

IF (LHOOK) CALL DR_HOOK('JBTOMODEL',1,ZHOOK_HANDLE)

END SUBROUTINE JBTOMODEL
