MODULE YOMCOSJO

USE PARKIND1  ,ONLY : JPIM     ,JPRB

USE PARDIMO ,ONLY : JPXVAR, JPNOTP, JPHUBLEV
USE PARERR  ,ONLY : JPMXESLV
USE PARCMA  ,ONLY : JPMXTOCH, JPMXOCT, JP_NUMEV 
USE YOMTVRAD,ONLY : MXSENSOR
USE YOM_YGFL,ONLY : JPGHG, JPGRG
USE PARERSCA,ONLY : JPQCELL,JPICELL
USE YOMHOOK   , ONLY : LHOOK,   DR_HOOK

IMPLICIT NONE

SAVE

!*
!      /YOMCOSJO/ - JO RELEVANT PARAMETERS

!     D. VASILJEVIC    90/02/21  ECMWF

!     modif. M.Jurasek 04/05/12  added radar reflectivity
!            C.Payan   07/04/27  added WSCAT5_FAC (reduction factor for
!            Quikscat)
!            K. Scipal 08/05/05 added scat soil moisture
!        Y.Tremolet    29-May-2008 Print Jo table by bins

!*

INTEGER(KIND=JPIM), PARAMETER :: NVAR_U  = 1
INTEGER(KIND=JPIM), PARAMETER :: NVAR_U10= 2
INTEGER(KIND=JPIM), PARAMETER :: NVAR_DD = 3
INTEGER(KIND=JPIM), PARAMETER :: NVAR_FF = 4
INTEGER(KIND=JPIM), PARAMETER :: NVAR_H  = 5
INTEGER(KIND=JPIM), PARAMETER :: NVAR_H2 = 6
INTEGER(KIND=JPIM), PARAMETER :: NVAR_T  = 7
INTEGER(KIND=JPIM), PARAMETER :: NVAR_Z  = 8
INTEGER(KIND=JPIM), PARAMETER :: NVAR_DZ = 9
INTEGER(KIND=JPIM), PARAMETER :: NVAR_LH =10
INTEGER(KIND=JPIM), PARAMETER :: NVAR_T2 =11
INTEGER(KIND=JPIM), PARAMETER :: NVAR_TS =12
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RAD=13
INTEGER(KIND=JPIM), PARAMETER :: NVAR_SN =14
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RR =15
INTEGER(KIND=JPIM), PARAMETER :: NVAR_PS =16
INTEGER(KIND=JPIM), PARAMETER :: NVAR_CC =17
INTEGER(KIND=JPIM), PARAMETER :: NVAR_CLW=18
INTEGER(KIND=JPIM), PARAMETER :: NVAR_Q  =19
INTEGER(KIND=JPIM), PARAMETER :: NVAR_FFS=20
INTEGER(KIND=JPIM), PARAMETER :: NVAR_S0 =21
INTEGER(KIND=JPIM), PARAMETER :: NVAR_X  =22
INTEGER(KIND=JPIM), PARAMETER :: NVAR_PWC=23
INTEGER(KIND=JPIM), PARAMETER :: NVAR_TO3=24
INTEGER(KIND=JPIM), PARAMETER :: NVAR_TCW=25
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RFL=26
INTEGER(KIND=JPIM), PARAMETER :: NVAR_APD=27
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RO =28
INTEGER(KIND=JPIM), PARAMETER :: NVAR_HLS=29
INTEGER(KIND=JPIM), PARAMETER :: NVAR_AOD=30 ! aerosol optical depth at 0.55 microns
INTEGER(KIND=JPIM), PARAMETER :: NVAR_LRA=31

INTEGER(KIND=JPIM), PARAMETER    :: &
 & NVAR_GRG(JPGRG)=(/32, 33, 34, 35, 36/)

INTEGER(KIND=JPIM), PARAMETER    :: &
 & NVAR_GHG(JPGHG)=(/37, 38, 39/)

INTEGER(KIND=JPIM), PARAMETER :: NVAR_COD=40 ! cloud optical depth
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RAO=41 ! ratio of fine-mode aerosol optical depth to total at 0.55 mic
INTEGER(KIND=JPIM), PARAMETER :: NVAR_RFA=42 ! aerosol reflectance
INTEGER(KIND=JPIM), PARAMETER :: NVAR_ODA=43 ! aerosol optical depth (multi-channel)
INTEGER(KIND=JPIM), PARAMETER :: NVAR_DOW=44
INTEGER(KIND=JPIM), PARAMETER :: NVAR_SM =45 !soil moisture
INTEGER(KIND=JPIM), PARAMETER :: NVAR_SMN=46 !normalised soil moisture
INTEGER(KIND=JPIM), PARAMETER :: NVAR_SFR=47 !L-band brightness temperature

INTEGER(KIND=JPIM), DIMENSION(JPXVAR) :: NVAR=(/&
 &  NVAR_U  ,NVAR_U10,NVAR_DD ,NVAR_FF ,NVAR_H  &
 & ,NVAR_H2 ,NVAR_T  ,NVAR_Z  ,NVAR_DZ ,NVAR_LH &
 & ,NVAR_T2 ,NVAR_TS ,NVAR_RAD,NVAR_SN ,NVAR_RR &
 & ,NVAR_PS ,NVAR_CC ,NVAR_CLW,NVAR_Q  ,NVAR_FFS&
 & ,NVAR_S0 ,NVAR_X  ,NVAR_PWC,NVAR_TO3,NVAR_TCW&
 & ,NVAR_RFL,NVAR_APD,NVAR_RO ,NVAR_HLS,NVAR_AOD&
 & ,NVAR_LRA,NVAR_GRG,NVAR_GHG,NVAR_COD&
 & ,NVAR_RAO,NVAR_RFA,NVAR_ODA&
 & ,NVAR_DOW&
 & ,NVAR_SM, NVAR_SMN, NVAR_SFR /)  

 CHARACTER(len=3), PARAMETER  :: &
 & CVAR_GRG(JPGRG)=(/'NOX',    'SO2',    'CO ',    'For',    'GO3'  /)

 CHARACTER(len=3), PARAMETER  :: &
 & CVAR_GHG(JPGHG)=(/'CO2',    'CH4',    'N2O'/)

CHARACTER(len=3), DIMENSION(JPXVAR) :: CVAR_NAME(JPXVAR)=(/&
 &      'U  ',    'U10',    'DD ',    'FF ',    'H  '&
 & ,    'H2 ',    'T  ',    'Z  ',    'DZ ',    'LH '&
 & ,    'T2 ',    'TS ',    'RAD',    'SN ',    'RR '&
 & ,    'PS ',    'CC ',    'CLW',    'Q  ',    'FFS'&
 & ,    'S0 ',    'X  ',    'PWC',    'TO3',    'TCW'&
 & ,    'RFL',    'APD',    'RO ',    'HLS',    'AOD'&
 & ,    'LRA',CVAR_GRG,CVAR_GHG ,     'COD',    'RAO'&
 & ,    'RFA',    'ODA',    'DOW',    'SM ',    'SMN', 'SFR' /)

INTEGER(KIND=JPIM) , DIMENSION(JPXVAR  ,JPNOTP  ) :: NOTVAR

!*       1.3.2 ARRAY HOLDING SUB-OBS. TYPES POINTERS

!     LQSCATT     L    .T.=>Scatterometer cost is quadratic.
!                      Only the ambiguous wind nearer the
!                      high resolution first guess is used.
!     WSCAT5_FAC  R    reduction factor for NSCAT5 (Qscat data),used in hjo 
!                      default (historical) value WSCAT5_FAC=0.25_jprb in defrun
!     WSCAT6_FAC  R    reduction factor for NSCAT6 (Qscat data),used in hjo 
!                      default (historical) value WSCAT6_FAC=0.25_jprb in defrun
LOGICAL :: LQSCATT
REAL(KIND=JPRB) :: WSCAT5_FAC,WSCAT6_FAC

!* Generalized Cross Validation

INTEGER(KIND=JPIM) , DIMENSION(JPXVAR  ,JPNOTP) :: NGCVVAR

!* Variational Quality Control
!     NITERQC     I    ITERATION NO. AT WHICH VARQC KICKS IN
!     RAQC        R    VAR QC, PROB. FOR GROSS ERROR BY VAR. AND OBS-TYPE
!     RLQC        R    VAR QC, RANGE BY VARIABLE AND OBS-TYPE
!     RBGQC       R    BACKGROUND CHECK LIMITS
!     RBGQC_RAD1C R    BACKG. CHECK LIMITS FOR 1C RADIANCES
!     ROERR_RAD1C R    OBSERVATION ERRORS FOR 1C RADIANCES
!     ROERR_QSCAT R    OBSERVATION ERRORS FOR QUIKSCAT WINDS
!     ROERR_OSCAT R    OBSERVATION ERRORS FOR OCEANSAT-2 WINDS
!     LVARQCG     L    SWITCH VARIATIONAL QC ON/OFF GLOBALLY
!     LQVARQC     L    SWITCH QUADRATIC VARIATIONAL QC ON/OFF
!     LVARQC      L    SWITCH VARIATIONAL QC ON/OFF BY VAR. AND OBS-TYPE
!     NHLSFGOPT   I    OPTION FOR HLOS IN FGCHK
!     ZHLSFGOPT   R    PARAMETER FOR OPTION FOR HLOS IN FGCHK
INTEGER(KIND=JPIM) :: NITERQC
REAL(KIND=JPRB) :: RAQC(JPXVAR,JPNOTP)
REAL(KIND=JPRB) :: RLQC(JPXVAR,JPNOTP)
REAL(KIND=JPRB) :: RBGQC(JPXVAR,JPNOTP,3)
REAL(KIND=JPRB) :: ROERR_RAD1C(0:MXSENSOR,JPMXTOCH)
REAL(KIND=JPRB) :: RAQC_RAD1C(0:MXSENSOR,JPMXTOCH)
REAL(KIND=JPRB) :: RLQC_RAD1C(0:MXSENSOR,JPMXTOCH)
REAL(KIND=JPRB) :: RBGQC_RAD1C(0:MXSENSOR,JPMXTOCH,3)
REAL(KIND=JPRB) :: ROERR_QSCAT(JPQCELL)
REAL(KIND=JPRB) :: ROERR_OSCAT(JPICELL)
LOGICAL :: LVARQCG
LOGICAL :: LQVARQC
LOGICAL :: LVARQC(JPXVAR,JPNOTP)
LOGICAL :: LVARQC_RAD1C(0:MXSENSOR)
LOGICAL :: LOW_MODIS_AMV_ERR

!* Variational Quality Control with Huber
!     RHUBERLEFT            R    LEFT HUBER CUTOFF VALUES 
!     RHUBERRIGHT           R    RIGHT HUBER CUTOFF VALUES
!     RHUBERBGQC            R    BACKGROUND CHECK LIMITS FOR HUBER VARQC
!     RHUBERBGQC_SONDEGOOD  R    BACKGROUND CHECK LIMITS FOR HUBER VARQC FOR GOOD SONDES AND PILOTS
!     RHUBERBGQC_SONDEOK    R    BACKGROUND CHECK LIMITS FOR HUBER VARQC FOR OK SONDES AND PILOTS
!     RHUBEROETUNING        R    FACTOR TO REDUCE THE OBSERVATION ERROR WHEN USING HUBER VARQC
!     LHUBERMINQCG          L    SWITCH HUBER VARIATIONAL QC WEIGHTS ON/OFF GLOBALLY
!     LHUBERBGQCG           L    SWITCH CHANGE IN BGQC LIMITS FOR HUBER VARIATIONAL QC ON/OFF GLOBALLY
!     LHUBERMINQC           L    SWITCH HUBER VARIATIONAL QC WEIGHTS ON/OFF BY VAR. AND OBS-TYPE
!     LHUBERBGQC            L    SWITCH CAHNGE IN BGQC LIMITS FOR HUBER VARIATIONAL QC ON/OFF BY VAR. AND OBS-TYPE
!     NBDLAYER              I    BOUNDARY LAYER TOP (IN THE RANGE 1:JPMXESLV)
!     NTROPOP               I    TROPOPAUSE (IN THE RANGE 1:JPMXESLV)
! NBDLAYER and NTROPOP are the link between JPHUBLEV and JPMXESLV defined in sulevlay.F90
REAL(KIND=JPRB) :: RHUBERLEFT(JPXVAR,JPMXOCT,JPNOTP,JPHUBLEV)
REAL(KIND=JPRB) :: RHUBERRIGHT(JPXVAR,JPMXOCT,JPNOTP,JPHUBLEV)
REAL(KIND=JPRB) :: RHUBERBGQC(JPXVAR,JPMXOCT,JPNOTP,JPHUBLEV,3)
REAL(KIND=JPRB) :: RHUBEROETUNING(JPMXESLV,JPXVAR,JPMXOCT,JPNOTP)

REAL(KIND=JPRB) :: RHUBERBGQC_SONDEGOOD(JPXVAR,JPNOTP,JPHUBLEV,3)
REAL(KIND=JPRB) :: RHUBERBGQC_SONDEOK(JPXVAR,JPNOTP,JPHUBLEV,3)
LOGICAL :: LHUBERMINQCG
LOGICAL :: LHUBERBGQCG
LOGICAL :: LHUBERMINQC(JPXVAR,JPNOTP)
LOGICAL :: LHUBERBGQC(JPXVAR,JPNOTP)

INTEGER(KIND=JPIM) :: NBDLAYER
INTEGER(KIND=JPIM) :: NTROPOP


INTEGER(KIND=JPIM) :: NHLSFGOPT
REAL(KIND=JPRB) :: ZHLSFGOPT 

!     obs error correlation (general)
!     LOBSCOR                                         L  MAIN SWITCH TO ACCOUNT FOR OBS ERROR CORRELATIONS
!     LOBSCORDIAG                                     L  TURN ON SOME DIAGNOSTICS WHEN R MATRIX IS "SMALL"
!     LOBSCOR_PS                                      L  OBS CORRELATIONS FOR PS (VALIDATION)
!     LOBSCOR_SATOB                                   L  OBS CORRELATIONS FOR SATOB
!     LOBSCOR_AMSUA                                   L  OBS CORRELATIONS FOR AMSUA
!     LRANDOM_OBSCOR                                  L  OBS CORRELATIONS BY RANDOMIZATION (FOR VALIDATION)
!     LOBSCOR_SPLIT                                   L  SPLIT OBSCOR PB VERTICALLY AND TEMPORALLY
!     LPRINT_OBSCOR_MATR                              L  PRINT OUT OBSCOR MATRIX, EIGENSYSTEM, AND INVERSES
!     NBCORBLK                                        I  NUMBER OF CORRELATED BLOCKS IN OBS CORR MATRIX
!     OBSCOR_SCALPR(JP_NUMEV,NBCORBLK)                R  INNER PRODUCTS
!     OBSCOR_AUX(NSETOT,NBCORBLK)          DERIVED TYPE  PRODUCTS TO BE SUMMED UP OVER PE-s,
!                                          obscor_aux_t  SETS AND REPORTS REPRODUCIBLY TO CONSTITUTE OBSCOR_SCALPR
!     OBSCOR_EIGVAL(JP_NUMEV,NBCORBLK)                R  EIGENVALUES OF OBS CORR MATRIX BLOCK
!     OBSCOR_TRUN(NBCORBLK)                           R  TRUNCATION PARAMETER OF OBS CORR MATRIX BLOCK DUE TO JP_NUMEV < Rank
!     NOVERTRUN                                       I  PARAM TO OVERTRUN No OF RETAINED EIGVECS: Neigvecs=JP_NUMEV/NOVERTRUN
!     EIGMIN                                          R  LOWER LIMITER OF R EIGENVALUES R=(1-EIGMIN) * R + EIGMIN * I
!     OBSCOR_HOR_LSCALE(NBCORBLK)                     R  HORIZONTAL CORRELATION LENGTH SCALE OF OBS ERROR
!     OBSHSCALE                                       R  HORIZONTAL CORRELATION LENGTH SCALE OF OBS ERROR (global)
!     OBSCOR_P_UPPER      "                           R  UPPERMOST LEVEL FOR VERTICAL INTERPOLATION OF OBS ERR CORR
!     OBSCOR_P_LOWER      "                           R  LOWERMOST                            "
!     NOBSCOR_INLEVS      "                           I  NUMBER OF PRESSURE LEVELS FOR VERTICAL INTERPOLATION OF OBS ERR CORR
!     OBSCOR_AUTO_COR     "                           R  DECORRELATION TIME PARAMETER
!     LUV_OBSCOR          "                           L  TYPE OF OBS, WIND OR SCALAR
!     L3D_OBSCOR          "                           L  OBS IS 3D FIELD OR 2D
!     OBSCOR_GRID_DEF                      DERIVED TYPE  REDUCED RESOLUTION GRID DEFINITION FOR OBS ERR CORR
!     OBS_ARR_GL2(:,:)                                R  AUX ARRAY TO HOLD OBSERVATION INFO NEEDED BY THE CORRELATION MODEL
!     NACTIM_START, NACTIM_END   (:)                  I  WHEN SPLITTED TEMPORALLY, TIMESLOT LIMITS OF GIVEN BLOCK
!     NMASK                                           I  AUX VALUE FOR ACTUAL CORRELATED BLOCK NUMBER
!     NSMAX_OBSCOR                                    I  RESOLUTION FOR OBSCOR MODEL
LOGICAL :: LOBSCOR, LOBSCORDIAG, LPRINT_OBSCOR_MATR  , LRANDOM_OBSCOR
LOGICAL :: LOBSCOR_AMSUA, LOBSCOR_SATOB, LOBSCOR_SPLIT
LOGICAL, ALLOCATABLE:: L3D_OBSCOR(:),LUV_OBSCOR(:)
TYPE obscor_aux_t
  INTEGER(KIND=JPIM)       :: NOBSCOR_COUNT
  REAL(KIND=JPRB), POINTER :: OBSCOR_SCALPR_L(:,:) => NULL()
!  OBSCOR_SCALPR_L(JP_NUMEV,NOBSCOR_COUNT)
END TYPE obscor_aux_t
TYPE obsarrtype
  REAL(KIND=JPRB), POINTER :: OBS_ARR_GL2(:,:) => NULL()
END TYPE obsarrtype
TYPE(obscor_aux_t), ALLOCATABLE:: OBSCOR_AUX(:,:)

TYPE OBSCOR_GRID_STRUCT
   INTEGER(KIND=JPIM)  :: NSMAX, NSPEC2, NUMP, NDGLG, NGPTOT, NGPTOTG, NRESOL_ID
   INTEGER(KIND=JPIM), POINTER :: NPROCM(:)
   INTEGER(KIND=JPIM), POINTER :: NASM0G(:)
   INTEGER(KIND=JPIM), POINTER :: NSPZERO(:)
   REAL(KIND=JPRB), POINTER ::    RLAPDI(:)
   INTEGER(KIND=JPIM), POINTER, DIMENSION(:)     :: NLOENG, NPTRFRSTLAT, NFRSTLAT, &
   & NLSTLAT, MYMS, NASM0
  INTEGER(KIND=JPIM), POINTER, DIMENSION(:,:)   :: NSTA, NONL, NGPTOTL
END TYPE OBSCOR_GRID_STRUCT
TYPE(obscor_grid_struct) :: OBSCOR_GRID_DEF

INTEGER(KIND=JPIM) :: NBCORBLK, NMASK
INTEGER(KIND=JPIM) :: NOVERTRUN, NSMAX_OBSCOR
REAL(KIND=JPRB)    :: EIGMIN
REAL(KIND=JPRB),ALLOCATABLE:: OBSCOR_SCALPR(:,:)
REAL(KIND=JPRB),ALLOCATABLE:: OBSCOR_EIGVAL(:,:)
REAL(KIND=JPRB),ALLOCATABLE:: OBSCOR_TRUN(:)
REAL(KIND=JPRB),ALLOCATABLE :: OBSCOR_HOR_LSCALE(:)
REAL(KIND=JPRB),ALLOCATABLE :: OBSCOR_P_UPPER(:)
REAL(KIND=JPRB),ALLOCATABLE :: OBSCOR_P_LOWER(:)
REAL(KIND=JPRB),ALLOCATABLE :: OBSCOR_AUTO_COR(:)
REAL(KIND=JPRB),ALLOCATABLE :: OBS_ARR_GL2(:,:)
INTEGER(KIND=JPIM),ALLOCATABLE :: NOBSCOR_INLEVS(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NRITZ_OBSCOR(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NACTIM_START(:), NACTIM_END(:)
REAL(KIND=JPRB)    :: OBSLATDIAG
REAL(KIND=JPRB)    :: OBSLONDIAG
REAL(KIND=JPRB)    :: OBSHSCALE

!* SYNOP/DRIBU time correlation
!     LTC         L    SWITCH FOR TIME CORRELATION
!     NTCOBS      I    NUMBER OF TIME CORRELATED OBSERVATIONS
!     NTCOBS_L    I    NUMBER OF TIME CORRELATED OBSERVATIONS, THIS PE
!     NTCLOC      I    NUMBER OF TIME CORRELATED OBS-LOCATIONS
!     RTCEFT      R    E-FOLDING TIME OF CORRELATION FUNCTION
!     RTCPART     R    TIME CORRELATED PART OF OBS ERROR.
LOGICAL :: LTC
INTEGER(KIND=JPIM) :: NTCOBS
INTEGER(KIND=JPIM) :: NTCOBS_L
INTEGER(KIND=JPIM) :: NTCLOC
REAL(KIND=JPRB) :: RTCEFT
REAL(KIND=JPRB) :: RTCPART
!     NTCOFF      I    OFFSETS TO STATION LOCATIONS IN THE OTHER ARRAYS
!     NTCOWN      I    OWNER PROCESSOR
!     NTCTIM      I    REFERENCE TIME (In minutes as given by REFTIM)
!     NTCVOI      I    VARIOUS OBSERVATION INFORMATIONS, obtype, cdtype etc
!     NTCIXG_L    I    INDEX TO GLOBAL NTC-ARRAYS
!     NTCONM_L    I    UNIQUE OBSERVATION SEQUENCE NUMBERS, OWN PE ONLY
!     NTCABN_L    I    ABSOLUTE OBSERVATION NUMBER, OWN PE ONLY
!     NTCOPT_L    I    OBSERVATION POINTER, OWN PE ONLY
!     NTCDPT_L    I    DATUM POSITION POINTER, OWN PE ONLY
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCOFF(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCOWN(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCTIM(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCVOI(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCIXG_L(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCONM_L(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCABN_L(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCOPT_L(:)
INTEGER(KIND=JPIM),ALLOCATABLE:: NTCDPT_L(:)
!     RTCNDP      R    NORMALIZED DEPARTURES (PRESSURE OR GEOPOTENTIAL)
!     RTCNDPHR    R    NORMALIZED HIGH RES DEPARTURES
!     RTCAQC      R    PROBABILITY OF GROSS ERROR
!     RTCLQC      R    RANGE OF POSSIBLE VALUES
REAL(KIND=JPRB),ALLOCATABLE:: RTCNDP(:)
REAL(KIND=JPRB),ALLOCATABLE:: RTCNDPHR(:)
REAL(KIND=JPRB),ALLOCATABLE:: RTCAQC(:)
REAL(KIND=JPRB),ALLOCATABLE:: RTCLQC(:)

!     ------------------------------------------------------------------
!     JO TABLES
!     FJOS is the actual one (reproducible)
!     JOT is for diagnostics only.

REAL(KIND=JPRB), ALLOCATABLE :: FJOS(:)
REAL(KIND=JPRB), ALLOCATABLE :: FJOADJ(:)
REAL(KIND=JPRB), ALLOCATABLE :: FJOADJB(:)

LOGICAL :: LADJOBS = .false.                    ! Save obs dep for adjoint test
INTEGER(KIND=JPIM) :: MOBSENS=0

!       JOT JO table, derived type
TYPE jotsub_t
  CHARACTER(len=32) :: sname                    ! name of sub-table
  REAL(KIND=JPRB)   , POINTER :: cost(:,:)      ! cost function value
  INTEGER(KIND=JPIM), POINTER :: count(:,:)     ! data count
  REAL(KIND=JPRB)   , POINTER :: obimpact(:,:)  ! obs impact
  REAL(KIND=JPRB)             :: obserr(JPXVAR) ! Sum of (obs error)**2
  REAL(KIND=JPRB)             :: bgerr(JPXVAR)  ! Sum of (bg error)**2
  INTEGER(KIND=JPIM)          :: codetype       ! obs codetype from ODB
END TYPE jotsub_t

TYPE jot_t
  CHARACTER(len=32)       :: name               ! name of table entry
  INTEGER(KIND=JPIM)      :: size               ! size of sub-table
  TYPE(jotsub_t), POINTER :: jost(:)            ! Jo sub-table
END TYPE jot_t

TYPE(jot_t) :: jot(JPNOTP)                      ! Jo-table

INTEGER(KIND=JPIM) :: NJOTTSZ                   ! Total size of cost and count
INTEGER(KIND=JPIM) :: NJOTISZ                   ! Size of table per item
INTEGER(KIND=JPIM) :: NPRTBINS=1                ! Number of bins for Jo prints

!     ------------------------------------------------------------------
PUBLIC ALLOCATE_OBSCOR_AUX, DEALLOCATE_OBSCOR_AUX
CONTAINS

SUBROUTINE ALLOCATE_OBSCOR_AUX(YDOBSCOR_AUX,KCOUNT)
TYPE (obscor_aux_t), INTENT(OUT)  :: YDOBSCOR_AUX
INTEGER(KIND=JPIM), INTENT(IN) :: KCOUNT
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('YOMCOSJO:ALLOCATE_OBSCOR_AUX',0,ZHOOK_HANDLE)
ALLOCATE(YDOBSCOR_AUX%OBSCOR_SCALPR_L(JP_NUMEV,KCOUNT) )
YDOBSCOR_AUX%OBSCOR_SCALPR_L(1:JP_NUMEV,1:KCOUNT)=0._JPRB
YDOBSCOR_AUX%NOBSCOR_COUNT=KCOUNT
IF (LHOOK) CALL DR_HOOK('YOMCOSJO:ALLOCATE_OBSCOR_AUX',1,ZHOOK_HANDLE)
END SUBROUTINE ALLOCATE_OBSCOR_AUX

SUBROUTINE DEALLOCATE_OBSCOR_AUX(YDOBSCOR_AUX)
TYPE (obscor_aux_t), INTENT(INOUT)  :: YDOBSCOR_AUX
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('YOMCOSJO:DEALLOCATE_OBSCOR_AUX',0,ZHOOK_HANDLE)
IF (ASSOCIATED(YDOBSCOR_AUX%OBSCOR_SCALPR_L   )) &
 & DEALLOCATE(YDOBSCOR_AUX%OBSCOR_SCALPR_L)
IF (LHOOK) CALL DR_HOOK('YOMCOSJO:DEALLOCATE_OBSCOR_AUX',1,ZHOOK_HANDLE)
END SUBROUTINE DEALLOCATE_OBSCOR_AUX

END MODULE YOMCOSJO
