!-------------------------------------- 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 CAINGD(KDIM,PX) 9,4
#if defined (DOC)
*
***s/r CAIN (canonical injection): transfer of the control variable
*     .                            to COMGD with optional spectral filter
*
*Author  : M. Buehner
*Revision:
*
*    -------------------
*Arguments
*     i : KDIM = dimension of the control variable
*     i : PX   = control variable
#endif
      IMPLICIT NONE
*implicits
#include "comdim.cdk"
#include "comlun.cdk"
#include "comgd0.cdk"
#include "comsp.cdk"
*
      INTEGER KDIM, JDIM, JLEV, JLAT, JLON
      LOGICAL LFILTER
      REAL*8 PX(KDIM)
      EXTERNAL ABORT3D, TRANSFER
C
      LFILTER=.TRUE.
C
C*    1. Set model state to zero (in COMSP)
C
 100  CONTINUE
C
      CALL TRANSFER('ZGD0')
C
C     2. Fill in the grid point model state
C
 200  CONTINUE
C
      JDIM = 0
C
      DO JLEV = 1, NKGDIM
        DO JLAT = 1, NJ
          DO JLON = 1, NI
            JDIM = JDIM + 1
            GD(JLON,JLEV,JLAT) = PX(JDIM)
          ENDDO
        ENDDO
      ENDDO
c
      IF(LFILTER) THEN
        CALL REESPE(NKSDIM,SP,GD
     S             ,NLA,NIBEG,NIEND,NJBEG,NJEND,NKSDIM)
        CALL SPEREE(NKSDIM,SP,GD
     S             ,NLA,NIBEG,NIEND,NJBEG,NJEND,NKSDIM)
      ENDIF
C
C     3. Verify the dimension of the control variable
C
 300  CONTINUE
      IF(JDIM.NE.KDIM) THEN
         WRITE(NULOUT,FMT=9300) KDIM, JDIM
         CALL ABORT3D(NULOUT,'ERROR IN CAINGD  ')
      END IF
 9300 FORMAT(//,10X,'Error in CAINGD. Wrong dimension',
     S     ' for the control variable. KDIM = ',I8,4X,'JDIM =',I8)
C
      RETURN
      END