SUBROUTINE LAIDDIOBSAD(KPROMA,KPROMB,KSTART,KPROF,KFLEV,KFLDN,KFLDX,&
 & PCLA,PDLO,PCLO,KL0,PXSL,PXF)  

!**** *LAIDDIOBSAD-  Observation interpolator:
!                    BI-dimensional 12 points interpolations (adjoint)

!     Purpose.
!     --------
!       Performs AD of bi-dimensional 12 points interpolations.
!       The position of the interpolation point and the weights
!        are considered as constants (they are not differentiated).

!**   Interface.
!     ----------
!        *CALL* *LAIDDIOBSAD(...)

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

!        INPUT:
!          KPROMA  - horizontal dimension for grid-point quantities.
!          KPROMB  - horizontal dimension for interpolation point
!                    quantities.
!          KSTART  - first element of arrays where
!                    computations are performed.
!          KPROF   - depth of work.
!          KFLEV   - vertical dimension.
!          KFLDN   - number of the first field.
!          KFLDX   - number of the last field.
!          PCLA    - weights (distances) for horizontal cubic interpolation
!                    on a same latitude.
!          PDLO    - distances for horizontal linear interpolations 
!                    in longitude (latitude rows 0, 3)
!          PCLO    - weights for horizontal cubic interpolations in longitude
!                    (latitude rows 1, 2)
!          KL0     - indices of the four western points
!                    of the 16 points interpolation grid.
!          PXSL    - quantity to be interpolated.

!        OUTPUT:
!          PXF     - interpolated variable.

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

!     Method.
!     -------
!        See documentation

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

!        No external.
!        Called by SLINTAD.

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

!     Author.
!     -------
!        D. Vasiljevic

!     Modifications.
!     --------------
!        Original : 20/05/92
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        F. Vana    20-Aug-2008 adapted to weights driven interp. dataflow
!     ------------------------------------------------------------------

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

USE YOMPLDSW , ONLY : LOPT_SCALAR

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMB 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDX 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLA(KPROMB,3) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDLO(KPROMB,0:3) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLO(KPROMB,3,2)
INTEGER(KIND=JPIM),INTENT(IN)    :: KL0(KPROMB,0:3,0:3) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXSL(KPROMA*(KFLDX-KFLDN+1)) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXF(:,:) 

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

INTEGER(KIND=JPIM) :: IP01, IP02, IP10, IP11, IP12, IP13, IP20,&
 & IP21, IP22, IP23, IP31, IP32, JLEV, JROF  

REAL(KIND=JPRB) :: Z0, Z1, Z2, Z3

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

!*       1.    INTERPOLATIONS.
!              ---------------

IF(LOPT_SCALAR)THEN

  DO JLEV=1,KFLEV
!DIR$ IVDEP
!OCL NOVREC
    DO JROF=KSTART,KPROF

      Z0 = PXF(JLEV,JROF)*(1.0_JPRB-PCLA(JROF,1)-PCLA(JROF,2)-PCLA(JROF,3))
      Z1 = PXF(JLEV,JROF)*PCLA(JROF,1)
      Z2 = PXF(JLEV,JROF)*PCLA(JROF,2)
      Z3 = PXF(JLEV,JROF)*PCLA(JROF,3)

      IP31 = KL0(JROF,3,1)+(JLEV-1)*KPROMA
      IP32 = KL0(JROF,3,2)+(JLEV-1)*KPROMA
      IP20 = KL0(JROF,2,0)+(JLEV-1)*KPROMA
      IP21 = KL0(JROF,2,1)+(JLEV-1)*KPROMA
      IP22 = KL0(JROF,2,2)+(JLEV-1)*KPROMA
      IP23 = KL0(JROF,2,3)+(JLEV-1)*KPROMA
      IP10 = KL0(JROF,1,0)+(JLEV-1)*KPROMA
      IP11 = KL0(JROF,1,1)+(JLEV-1)*KPROMA
      IP12 = KL0(JROF,1,2)+(JLEV-1)*KPROMA
      IP13 = KL0(JROF,1,3)+(JLEV-1)*KPROMA
      IP01 = KL0(JROF,0,1)+(JLEV-1)*KPROMA
      IP02 = KL0(JROF,0,2)+(JLEV-1)*KPROMA

      PXSL(IP31) = PXSL(IP31) + Z3*(1.0_JPRB-PDLO(JROF,3))
      PXSL(IP32) = PXSL(IP32) + Z3*PDLO(JROF,3)

      PXSL(IP20) = PXSL(IP20) + Z2*(1.0_JPRB &
       & -PCLO(JROF,1,2)-PCLO(JROF,2,2)-PCLO(JROF,3,2))
      PXSL(IP21) = PXSL(IP21) + Z2*PCLO(JROF,1,2)
      PXSL(IP22) = PXSL(IP22) + Z2*PCLO(JROF,2,2)
      PXSL(IP23) = PXSL(IP23) + Z2*PCLO(JROF,3,2)
     
      PXSL(IP10) = PXSL(IP10) + Z1*(1.0_JPRB &
       & -PCLO(JROF,1,1)-PCLO(JROF,2,1)-PCLO(JROF,3,1))
      PXSL(IP11) = PXSL(IP11) + Z1*PCLO(JROF,1,1)
      PXSL(IP12) = PXSL(IP12) + Z1*PCLO(JROF,2,1)
      PXSL(IP13) = PXSL(IP13) + Z1*PCLO(JROF,3,1)

      PXSL(IP01) = PXSL(IP01) + Z0*(1.0_JPRB-PDLO(JROF,0))
      PXSL(IP02) = PXSL(IP02) + Z0*PDLO(JROF,0)
     
    ENDDO
  ENDDO
ELSE
  DO JROF=KSTART,KPROF
!DIR$ IVDEP
!OCL NOVREC
    DO JLEV=1,KFLEV

      Z0 = PXF(JLEV,JROF)*(1.0_JPRB-PCLA(JROF,1)-PCLA(JROF,2)-PCLA(JROF,3))
      Z1 = PXF(JLEV,JROF)*PCLA(JROF,1)
      Z2 = PXF(JLEV,JROF)*PCLA(JROF,2)
      Z3 = PXF(JLEV,JROF)*PCLA(JROF,3)

      IP31 = KL0(JROF,3,1)+(JLEV-1)*KPROMA
      IP32 = KL0(JROF,3,2)+(JLEV-1)*KPROMA
      IP20 = KL0(JROF,2,0)+(JLEV-1)*KPROMA
      IP21 = KL0(JROF,2,1)+(JLEV-1)*KPROMA
      IP22 = KL0(JROF,2,2)+(JLEV-1)*KPROMA
      IP23 = KL0(JROF,2,3)+(JLEV-1)*KPROMA
      IP10 = KL0(JROF,1,0)+(JLEV-1)*KPROMA
      IP11 = KL0(JROF,1,1)+(JLEV-1)*KPROMA
      IP12 = KL0(JROF,1,2)+(JLEV-1)*KPROMA
      IP13 = KL0(JROF,1,3)+(JLEV-1)*KPROMA
      IP01 = KL0(JROF,0,1)+(JLEV-1)*KPROMA
      IP02 = KL0(JROF,0,2)+(JLEV-1)*KPROMA

      PXSL(IP31) = PXSL(IP31) + Z3*(1.0_JPRB-PDLO(JROF,3))
      PXSL(IP32) = PXSL(IP32) + Z3*PDLO(JROF,3)

      PXSL(IP20) = PXSL(IP20) + Z2*(1.0_JPRB &
       & -PCLO(JROF,1,2)-PCLO(JROF,2,2)-PCLO(JROF,3,2))
      PXSL(IP21) = PXSL(IP21) + Z2*PCLO(JROF,1,2)
      PXSL(IP22) = PXSL(IP22) + Z2*PCLO(JROF,2,2)
      PXSL(IP23) = PXSL(IP23) + Z2*PCLO(JROF,3,2)
     
      PXSL(IP10) = PXSL(IP10) + Z1*(1.0_JPRB &
       & -PCLO(JROF,1,1)-PCLO(JROF,2,1)-PCLO(JROF,3,1))
      PXSL(IP11) = PXSL(IP11) + Z1*PCLO(JROF,1,1)
      PXSL(IP12) = PXSL(IP12) + Z1*PCLO(JROF,2,1)
      PXSL(IP13) = PXSL(IP13) + Z1*PCLO(JROF,3,1)

      PXSL(IP01) = PXSL(IP01) + Z0*(1.0_JPRB-PDLO(JROF,0))
      PXSL(IP02) = PXSL(IP02) + Z0*PDLO(JROF,0)

    ENDDO
  ENDDO
ENDIF

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

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

