!-------------------------------------- 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 --------------------------------------
!
C
C  X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X
C

      subroutine abalgl(ppsi,pchi)
*
***s/r abalgl  - Adjoint of lbalgl. Used by INMI approach.
*            - Out: Sensitivitiy of (Psi,Chiu,Tu,q,psu)
*            - In:  Sensitivitiy of (Psi,Chi,T,q,ps)
*
*Author  : Luc Fillion - -ARMA/EC  13 Jul 2009. (Based on original lam code built in Feb. 2007)
*
*     -------------------
**    Purpose: Used for LA-XD-Var analysis
*
*Arguments
*
      implicit none
      logical ldhelm
      real*8 ppsi(ni,nflev,nj)
      real*8 pchi(ni,nflev,nj)
*
#include "taglam4d.cdk"
#include "comdim.cdk"
#include "comcst.cdk"
#include "comct0.cdk"
#include "comcva.cdk"
#include "comsim.cdk"
#include "comgd0.cdk"
#include "comcorr.cdk"
#include "compdg.cdk"
!
!
      logical llmass,llchib,lldiab,llconv,llcond
      logical llfplane
      integer ji,jj,jk,ipt,jpt,jla,ik,jband,jm,ila
      integer jk1,jk2
      integer jrow,jcol,nim,njm,nijkm
      integer idim
      real*8 zcoriolis
      real*8 zgdpsi(ni,nflev,nj)
      real*8 zgdchib(ni,nflev,nj)
      real*8 zpsiomeg(ni,nflev,nj)
      real*8 zchib(ni,nflev,nj)
      real*8 zp(ni,nflev,nj)
      real*8 zpnl(ni,nflev,nj)
!
!!
      zchib(:,:,:) = 0.0
!
!*3   Adjoint of: Add Balanced and Unbalanced components
!     --------------------------------------------------
!
!cluc   zchib(:,:,:)=pchi(:,:,:)
!
!*2.  Adjoint of: Build Balanced part
!     -------------------------------
!
!*2.2 Adjoint of: Compute Balanced Chi: Result in ppsi
!
      if(linmi) then
        lldiab = ldiabatic
!cluc        call ainmi_Chi(ppsi,zchib,lldiab)
      endif
!
!*2.1 Adjoint of: 1st Order Baer-Tribbia
!
      if(linmi) then
         zpnl(:,:,:) = zp(:,:,:)
         lldiab=.false.
!cluc         call ainmi_P(ppsi,zpnl,lldiab)
      endif
!
!*1.  Adjoint of: Save Unbalanced part
!     --------------------------------
!
!
      return
      end