SUBROUTINE PRE_OBSHORAD

!     *PRE_OBSHORAD* - Horizontal interpolation of the model variables
!                  from grid points to observation points (adjoint part)

!     Purpose.
!     -------
!        To prepare use of semi-lagrangian interpolation routines.

!     Interface.
!     ---------
!        CALL PRE_OBSHORAD(...)

!     Explicit arguments :
!     --------------------
!            KIBUFP1  = S/L BUFFERS TABLE
!            POBB1    = S/L BUFFER

!     Externals
!     ---------
!        SLINTAD - S/L INTERPOLATOR

!     Author
!     ------
!        Drasko Vasiljevic *ECMWF*

!     Modifications
!     -------------
!     90-05-19 Original by D. Vasiljevic
!     92-12-01 JN.Thepaut  : change of name conventions.
!     92-11-03 F.Bouttier  : Process just one obs for the E.K.F.
!     94-09-18 M.Hamrud    : Multi-tasking change
!     95-09-20 H.Jarvinen  : New CMA- file format
!     95-12-07 F.Bouttier  : Add LDMVO5 option, clean Canari
!     96-12-01 M.Hamrud    : Message passing including 4D-var
!     97-05-27 F.Taillefer : Add DL and DM for the wind orientation
!     97-11-17 E.Andersson : Introduce CIW and O3
!     99-07-02 A. Horanyi  : Bug corrections for ALADIN 3DVAR in order to 
!              avoid inconsistency between direct and adjoint code
!     02-06-19 S.Saarinen  : CALL GETDB to get ABNOB, MAPOMM & OBSTYPE
!     02-03-27 C. Fischer  : Modified correction of compas (adapted from P Moll)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!    -------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMECTAB , ONLY : NTSLOTNO,NTSLTOB
USE YOMDB
USE YOMLUN   , ONLY : NULERR
USE OBSHOR_CACHE_MIX

IMPLICIT NONE

INTEGER(KIND=JPIM) :: ITS, JOBS, IRET, INFO(0),JTSLOT, IPROMBMP

REAL(KIND=JPRB) :: ZINFO(0)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "getdb.h"
#include "putdb.h"
#include "abortdb.h"
END INTERFACE

#include "mpobseqad.intfb.h"

#include "openmp_obs.h"

!    --------------------------------------------------------------------
!*       1. PRESET
!           ------

IF (LHOOK) CALL DR_HOOK('PRE_OBSHORAD',0,ZHOOK_HANDLE)
CALL GSTATS(94,0)


DO JTSLOT=1,NACTIM
  NTSLOTNO=JTSLOT
  ITS = NTSLOTNO

  CALL GSTATS(1861,0)
  IPROMBMP = NTSLTOB(ITS)

  CALL OBSHOR_CACHE_ALLOC('PRE_OBSHORAD', ITS, IPROMBMP)
  CALL GSTATS(1861,1)

  IF (.NOT.OBSHOR_CACHE(ITS)%PROCESSED) THEN
    IRET = 0
    CALL GETDB('OBSHOR', 0, IRET, INFO, 0, ZINFO, 0,   &! Get ROBSU
     & -1, ITS, -1, -1, -1, -1)  
    
    IF (IRET /= IPROMBMP) THEN
      WRITE(NULERR,*) '*** Error in PRE_OBSHORAD: INCONSISTENT NO. OF OBSERVATIONS FROM GETDB'
      WRITE(NULERR,*) 'For timeslot=',ITS,' expecting =',IPROMBMP,', but got =',IRET
      CALL ABORTDB('PRE_OBSHORAD','INCONSISTENCIES FROM GETDB',IRET)
    ENDIF

    IF (IPROMBMP > 0) THEN
      DO JOBS=1,IPROMBMP
        OBSHOR_CACHE(ITS)%OBSTYPE(JOBS) = ROBSU(JOBS, MDBOTP)
        OBSHOR_CACHE(ITS)%MAPOMM(JOBS)  = ROBSU(JOBS, MDB_MAPOMM_AT_INDEX)
        OBSHOR_CACHE(ITS)%ABNOB(JOBS)   = ROBSU(JOBS, MDB_ABNOB_AT_INDEX)
      ENDDO
      
    ENDIF

    CALL PUTDB('OBSHOR', 0, IRET, INFO, 0, ZINFO, 0)

    OBSHOR_CACHE(ITS)%PROCESSED = .TRUE.
  ENDIF

ENDDO

!    -------------------------------------------------------------------
!*       2. TAKE INTERPOLATED VARIABLES FROM GOMU ETC.
!           ------------------------------------------

! No worries : We quarantee at least one element allocation for IOB*() in OBSHOR_CACHE_MIX !

!*      2.2  EXCHANGE OBS.EQ.


CALL MPOBSEQAD
  
CALL GSTATS(94,1)

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

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