c23456789012345678901234567890123456789012345678901234567890123456789012


      SUBROUTINE Ca_modx

c     ******************************************************************
c
c     This routine sets the error covariance matrix CA^{-1} for linear 
c     equations
c
c     ******************************************************************

c     -------------
c     Declarations
c     -------------

c     ''''''''''''''''''''''
      implicit none
      include 'commonvar.f'      
c     ''''''''''''''''''''''

c---- Local variables

      integer	n,i,j,k,kcomp




c     --------------------
c     Covariance matrix CA
c     --------------------

c---- Initialization

      kcomp = 0


c---- Mass conservation 

      do i=1,itot
      do j=1,jtot
      do k=1,ktot

         if (flag_mas(i,j,k)) then
           kcomp           = kcomp + 1
           Ca_mod_1(kcomp) = e_mas**2 
         endif

      enddo 
      enddo
      enddo


c---- Thermal wind for U

      do i=1,itot+1	
      do j=1,jtot
      do k=1,ktot-1

         if (flag_twu(i,j,k)) then 
           kcomp           = kcomp + 1
           Ca_mod_1(kcomp) = e_twu**2 
         endif

      enddo
      enddo
      enddo


c---- Thermal wind for V

      do i=1,itot
      do j=1,jtot+1
      do k=1,ktot-1

         if (flag_twv(i,j,k)) then
           kcomp           = kcomp + 1
           Ca_mod_1(kcomp) = e_twv**2 
         endif

      enddo
      enddo
      enddo


c---- Vorticity

      do i=1,itot
      do j=1,jtot
      do k=1,ktot

         if (flag_vor(i,j,k)) then
            kcomp           = kcomp + 1
            Ca_mod_1(kcomp) = e_vor**2
         endif

      enddo
      enddo
      enddo


c---- Testing the order of the matrix Ca 

      if (kcomp.ne.n_li) then
        write(*,*)
        write(*,*) 'Ca_mod_1 and A_mod are not conformable'
        write(*,*)
        stop 'OK'
      endif 




c     --------------------------
c     Covariance matrix Ca^{-1}
c     --------------------------

      if (iflag.eq.3) then 

        do n=1,n_li
           Ca_mod_1(n) = 0.d0
        enddo

      else

        do n=1,n_li
           Ca_mod_1(n) = 1.d0 / Ca_mod_1(n)
        enddo

      endif




c     -------------------
c     End of the routine
c     -------------------

      return

      end

c     ******************************************************************

