c23456789012345678901234567890123456789012345678901234567890123456789012


      SUBROUTINE Co_1xxx(opt)

c     ******************************************************************
c
c     This routine sets the error covariance matrix for prior state 
c
c     ******************************************************************

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

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

c---- Argument variable

      character	opt*5


c---- Local variables

      integer	i,j,k,n,kcomp,ic

      character*12 stringx




c     -----------------------------
c     Errors in dynamical variables
c     -----------------------------

c---- Errors in density

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

         e_r(i,j,k) = e_r0
         if (.not.flag_r(i,j,k)) e_r(i,j,k) = -99.

      enddo
      enddo
      enddo

c     --------------------
      call wocexxx('rerr')
c     --------------------


c---- Errors in transport component U 

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

         e_U(i,j,k) = e_U0 
         if (.not.flag_U(i,j,k)) e_U(i,j,k) = -99.

      enddo
      enddo
      enddo

c     -----------------
      call observa(opt)
c     -----------------


c---- Errors in transport component V

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

         e_V(i,j,k) = e_V0 
         if (.not.flag_V(i,j,k)) e_V(i,j,k) = -99.

      enddo
      enddo   
      enddo

c     -----------------
      call observa(opt)
c     -----------------


c---- Errors in transport component W

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

         e_W(i,j,k) = e_W0 
         if (.not.flag_W(i,j,k)) e_W(i,j,k) = -99.

      enddo
      enddo
      enddo


c---- Errors in tracer 

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

         e_p(i,j,k) = e_p0
         if (.not.flag_p(i,j,k)) e_p(i,j,k) = -99.

      enddo
      enddo
      enddo

c     --------------------
      call wocexxx('perr')
c     --------------------




c     ------------------------
c     Errors if iflag = 2 or 3
c     ------------------------

c     ---------------------------------------
      if (iflag.eq.2) call assignx('err_den')
      if (iflag.eq.3) call assignx('err_pos')
c     ---------------------------------------




c     ----------------------
c     Covariance matrix C_o 
c     ----------------------

c---- Density

      kcomp = 0

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

         if (flag_r(i,j,k)) then
           kcomp       = kcomp + 1
           Co_1(kcomp) = e_r(i,j,k)**2
         endif

      enddo
      enddo
      enddo


c---- Transport component U

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

         if (flag_U(i,j,k)) then
           kcomp       = kcomp + 1
           Co_1(kcomp) = e_U(i,j,k)**2
         endif

      enddo
      enddo
      enddo


c---- Transport component V

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

         if (flag_V(i,j,k)) then
           kcomp       = kcomp + 1
           Co_1(kcomp) = e_V(i,j,k)**2
         endif

      enddo
      enddo
      enddo


c---- Transport component W 

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

         if (flag_W(i,j,k)) then
           kcomp       = kcomp + 1
           Co_1(kcomp) = e_W(i,j,k)**2
         endif

      enddo
      enddo
      enddo


c---- Tracer 

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

         if (flag_p(i,j,k)) then
           kcomp       = kcomp + 1
           Co_1(kcomp) = e_p(i,j,k)**2
         endif

      enddo
      enddo
      enddo


c---- Testing the order of matrix

      if (kcomp.ne.n_o) then
        write(*,*)
        write(*,*) 'Co and xo are not conformable'
        write(*,*)
        stop 'STOP'
      endif 




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

c---- Covariance matrix

      do n=1,n_o
         Co_1(n) = 1.d0 / Co_1(n)
      enddo 


c---- Writing diagonal elements of Co^{-1}

      open (50,file='c_o_1')

      do n=1,n_o
         write(50,'(i5,e8.1)') n,Co_1(n)
      enddo

      close (50)

c      stop 'DIAGONAL ELEMENTS OF Co^{-1} IN c_o_1'




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

      return

      end

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