SUBROUTINE READVEC (cdfile,pritz,pbltop,LDJB)

!*** *READVEC*

!   Purpose.
!   --------
!      To read a GRIB-format vector into SPA3JB and SPA2JB, or into
!      SPA3 and SPA3. 

!      If LDJB==T then the input file is required to contain fields for
!      all variables in SPA3JB and SPA2JB. In addition, if "pbltop" is
!      present, the inout file should contain boundary-layer top.

!      If LDJB==F (deprecated) then thr following fields only are read:
!      vorticity, divergence, temperature, specific humidity, ozone (if
!      the model includes ozone, and this is not the RRKF), lnsp and
!      boundary-layer top (if "pbltop" is present).

!   Interface.
!   ----------

!      CALL READVEC (CDFILE)

!   Explicit Inputs:
!                     cdfile  -- name of file containing the vector
!                     ldjb    -- .TRUE. (default) => read into SPA3JB
!                                and SPA2JB (otherwise SPA3 and SPA2)
!   Explicit Output:
!                     pritz   -- optional argument. Used to return the
!                                ritz value of a singular vector
!                     pbltop  -- optional argument. Height of boundary layer

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

!   Reference.
!   ----------

!   Author.
!   -------
!      Mike Fisher *ECMWF*

!   Modifications.
!   --------------
!      Original 21/11/95
!      29/01/97 -- message passing
!        Modified : 00-09-01 by D.Dent : MPE to MPL conversion
!        Modified : 00-10-26 by M.Fisher : added optional argument PBLTOP
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!        M.Hamrud      01-May-2006 Generalized IO scheme

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NFLEVL   ,NSPEC2
USE YOMSP    , ONLY : SPVOR    ,SPDIV    ,SPT      ,SPQ      ,SPSP,     SPO3
USE YOMSPJB  , ONLY : SPJB
USE YOMMP    , ONLY : NPSP
USE YOMGRB   , ONLY : NGRBVO   ,NGRBD    ,NGRBT    ,NGRBQ    ,&
 & NGRBO3   ,NGRBLNSP ,NGRBBLH  
USE YOMSKF   , ONLY : LSKF
USE YOMGEM   , ONLY : NGPTOT
USE IOSTREAM_MIX , ONLY : SETUP_IOSTREAM, SETUP_IOREQUEST, IO_GET, IO_INQUIRE,&
 & CLOSE_IOSTREAM, TYPE_IOSTREAM , TYPE_IOREQUEST, CLOSE_IOREQUEST
USE YOMJG    , ONLY : SPJB_VARS_INFO
USE YOMVAR   , ONLY : LFCOBS

IMPLICIT NONE

CHARACTER(LEN=*)           ,INTENT(IN)    :: CDFILE
REAL(KIND=JPRB)   ,OPTIONAL,INTENT(OUT)   :: PRITZ 
REAL(KIND=JPRB)   ,OPTIONAL,INTENT(OUT)   :: PBLTOP(NGPTOT) 
LOGICAL           ,OPTIONAL,INTENT(IN)    :: LDJB

LOGICAL :: LLJB
INTEGER(KIND=JPIM) :: I2D,I3D,IRITZGBH1,IRITZGBH2,J3D,J2D,ICODE
INTEGER(KIND=JPIM) :: IGRIB2D(100),IGRIB3D(100)
REAL(KIND=JPRB),ALLOCATABLE :: ZSPA3(:,:,:),ZSPA2(:,:)
TYPE(TYPE_IOSTREAM)  :: YL_IOSTREAM
TYPE(TYPE_IOREQUEST) :: YL_IOREQUEST
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

IF (PRESENT(LDJB)) THEN
  LLJB=LDJB
ELSE
  LLJB=.TRUE.
ENDIF

IF (LLJB) THEN
  i2d=SPJB%ns2g
  i3d=SPJB%ns3d
  IGRIB2D(1:i2d)=SPJB%ngrib2
  IGRIB3D(1:i3d)=SPJB%ngrib3

  IF (PRESENT(PBLTOP) .AND. &
   &  .NOT.ANY(SPJB_VARS_INFO(:)%IGRIBCODE==NGRBBLH)) THEN
    I2D=I2D+1
    IGRIB2D(I2D) = NGRBBLH
  ENDIF
ELSEIF (LFCOBS .OR. LSKF .OR. .NOT.ANY(SPJB_VARS_INFO(:)%IGRIBCODE==NGRBO3)) THEN
  IF (PRESENT(PBLTOP)) THEN
    I3D=4
    I2D=2
    IGRIB2D(1:I2D)=(/NGRBLNSP,NGRBBLH/)
    IGRIB3D(1:I3D)=(/NGRBVO,NGRBD,NGRBT,NGRBQ/)
  ELSE
    I3D=4
    I2D=1
    IGRIB2D(1:I2D)=(/NGRBLNSP/)
    IGRIB3D(1:I3D)=(/NGRBVO,NGRBD,NGRBT,NGRBQ/)
  ENDIF
ELSE
  IF (PRESENT(PBLTOP)) THEN
    I3D=5
    I2D=2
    IGRIB2D(1:I2D)=(/NGRBLNSP,NGRBBLH/)
    IGRIB3D(1:I3D)=(/NGRBVO,NGRBD,NGRBT,NGRBQ,NGRBO3/)
  ELSE
    I3D=5
    I2D=1
    IGRIB2D(1:I2D)=(/NGRBLNSP/)
    IGRIB3D(1:I3D)=(/NGRBVO,NGRBD,NGRBT,NGRBQ,NGRBO3/)
  ENDIF
ENDIF
ALLOCATE(ZSPA3(NFLEVL,NSPEC2,I3D))
ALLOCATE(ZSPA2(NSPEC2,I2D))
!--- processor 1 does the IO

CALL SETUP_IOSTREAM(YL_IOSTREAM,'CIO',TRIM(CDFILE),CDMODE='r',KIOMASTER=1,LDNOHELP=.TRUE.)
CALL SETUP_IOREQUEST(YL_IOREQUEST,'SPECTRAL_FIELDS',LDGRIB=.TRUE.,&
 & KGRIB3D=IGRIB3D(1:I3D),KGRIB2D=IGRIB2D(1:I2D),KCHUNKSIZE=1)

CALL IO_GET(YL_IOSTREAM,YL_IOREQUEST,PR2=ZSPA2,PR3=ZSPA3)

IF(PRESENT(PRITZ)) THEN
  CALL IO_INQUIRE(YL_IOSTREAM,YL_IOREQUEST,LDREAD=.FALSE.,&
 & KRITZ1=IRITZGBH1,KRITZ2=IRITZGBH2)
  PRITZ = IRITZGBH2*EXP( LOG(10._JPRB)*IRITZGBH1 )
ENDIF

CALL CLOSE_IOREQUEST(YL_IOREQUEST)
CALL CLOSE_IOSTREAM(YL_IOSTREAM)

IF (LLJB) THEN
  SPJB%SP3D=ZSPA3
  IF (NPSP==1) SPJB%SP2D=ZSPA2(:,1:SPJB%ns2g)
ELSE
!--- store in SPA3/SPA2 (retained for compatability with sv codes)
  DO J3D=1,I3D
    ICODE=IGRIB3D(J3D)
    IF (ICODE == NGRBVO) THEN
      SPVOR(:,:) =  ZSPA3(:,:,J3D)
    ELSEIF (ICODE == NGRBD) THEN
      SPDIV(:,:) = ZSPA3(:,:,J3D)
    ELSEIF (ICODE == NGRBT) THEN
      SPT  (:,:) =  ZSPA3(:,:,J3D)
    ELSEIF (ICODE == NGRBQ) THEN
      SPQ  (:,:) =  ZSPA3(:,:,J3D)
    ELSEIF (ICODE == NGRBO3) THEN
      SPO3 (:,:) =  ZSPA3(:,:,J3D)
    ENDIF
  ENDDO
  DO J2D=1,I2D
    ICODE=IGRIB2D(J2D)
    IF (ICODE == NGRBLNSP) THEN
      SPSP(:) = ZSPA2(:,J2D)
    ENDIF
  ENDDO
ENDIF

IF(PRESENT(PBLTOP)) THEN
  DO J2D=1,I2D
    ICODE=IGRIB2D(J2D)
    IF (ICODE == NGRBBLH) THEN
      PBLTOP(:) = ZSPA2(:,J2D)
    ENDIF
  ENDDO
ENDIF

DEALLOCATE(ZSPA3)
DEALLOCATE(ZSPA2)

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