!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!

      SUBROUTINE oda_Hzp 1,1
      use modmask, only : lmaskzp
#if defined (DOC)
*
* Purpose: Compute simulated profiler observations from profiled model
*          increments.
*          It returns Hdx in ROBDATA8(NCMOMA,*)
*          Interpolate vertically the contents of commvo to heights
*          (in meters) of the observations.
*          A linear interpolation in z is performed.
*
*Author  :  J. St-James, CMDA/SMC July 2003
**Revision:
*           S. Pellerin ARMA, January 2009
*            - Rename the subroutine acording to ODA naming convention
*            - Use of mask to process only assimilated data
*            - Computation of Hdx instead of Jo=sum([Hdx-d]/sigma)
*            - Withdraw of the QCVAR to be applied outside of the
*              operator
*
#endif
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comdimo.cdk"
#include "comcst.cdk"
#include "comoabdy.cdk"
#include "comoahdr.cdk"
#include "comoba.cdk"
#include "commvo.cdk"
#include "commvog.cdk"
*
      INTEGER IPB,IPT,IDBURP
      INTEGER IOBS,IPOS,IK,IBEGIN,ILAST,ILASTOB,IBEGINOB,IDATEND,NQCVAR
      INTEGER J,JDATA,IDATA,ITYP,ISTYP,JJ,JO
      REAL*8 ZVAR,ZOER,ZDA1,ZDA2,ZCON,ZINC,ZPHI,ZJON,ZGAMI,ZSLEV,ZQCARG
      REAL*8 ZWB,ZWT, ZLTV,ZTVG,ZPPOST
      REAL*8 ZLEV,ZPT,ZPB,ZLAT,ZLON,ZTORAD,ZDENO,ZCON1,ZCON2,ZCON3
      REAL*8 DLSUM
      LOGICAL LLOK, LLPRINT, LLUV
C
      DO JDATA=1,ndata
        IF (lmaskzp(jdata)) THEN
          IOBS = MOBDATA(NCMOBS,JDATA)
          IPOS = MOBDATA(NCMPOS,JDATA)
          ZLEV = ROBDATA8(NCMPPP,JDATA)
          IK   = ROBDATA(NCMLYR,JDATA)
          IPT  = IK + IPOS*NFLEV
          IPB  = IPT+1
          ZPT  = GOMGZG(IK,IOBS)/RG
          ZPB  = GOMGZG(IK+1,IOBS)/RG
          ZDENO= ZPT-ZPB
          ZWB  = (ZPT-ZLEV)/ZDENO
          ZWT  = 1.0D0 - ZWB

          ZDA1= (ZLEV-ZPB)/(ZDENO**2)
          ZDA2= (ZPT-ZLEV)/(ZDENO**2)

          ROBDATA8(NCMOMA,JDATA) =
     &         ZWB*GOMOBS(IPB,IOBS) + ZWT*GOMOBS(IPT,IOBS) +
     &         (GOMOBSG(IPB,IOBS) - GOMOBSG(IPT,IOBS))*
     &         (ZDA1*GOMGZ(IK,IOBS)/RG + ZDA2*GOMGZ(IK+1,IOBS)/RG)
        ENDIF
      END DO
      RETURN
      END