!-------------------------------------- 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 CAINAD2(KDIM,PX,PX2) 1
#if defined (DOC)
*
***s/r CAINAD2  Adjoint of the canonical injection
*     .         which accounts for Hessian eigenvector preconditioning
*
*Author  : M. Buehner *ARMA/AES  April 2002
*
*Revision:
*
*    -------------------
**    Purpose: identity transformation used to insure a communication
*     .        link between the model state and the minimization algo-
*     .        rithm. An adjustment is made according to the norm used.
*     .        (See SUSCAL where the norm is defined)
*
*Arguments
*     i : KDIM = dimension of the control variable
*     i : PX   = control variable
#endif
      IMPLICIT NONE
*implicits
#include "comlun.cdk"
#include "comdim.cdk"
#include "comcva.cdk"
#include "comanl.cdk"
*
      INTEGER KDIM, JDIM, JLEV, JLA
      REAL*8 PX(KDIM),PX2(KDIM)
      INTEGER RR,J,IERR
      REAL*8 TEMP2
      REAL*8 ZPX(KDIM)
      EXTERNAL ABORT3D
C
C*    1. Transfer of the model spcetral state
C
      WRITE(NULOUT,*) 'CAINAD2: NPRECON=',nprecon,kdim
c
      DO J = 1, KDIM
        ZPX(J)= PX(J)
      ENDDO
c
c CALCULATE PX= (I - G2*G2^T)*PX
c
      DO RR = 1, NPRECON
        TEMP2=0.0d0
        DO J = 1, KDIM
          TEMP2=TEMP2 + RRNK2(J,RR)*PX(J)
        ENDDO
        DO J = 1, KDIM
          ZPX(J)=ZPX(J) - RRNK2(J,RR)*TEMP2
        ENDDO
      ENDDO
c
      DO J = 1, KDIM
         PX2(J)= PX2(J) + ZPX(J)
      ENDDO
c
      RETURN
      END