C get_errcov.f 
C   1) Create the error covariance matrices for velocity and elevation
C   2) Create the weight functions that are used in the cost function
C      for the velocity and elevation errors
C
C The error variance of the velocity observations is defined 
C as the mean square of the vector error (m^2/s^2). 
C
C The error variance of the elevation observations is defined 
C as the mean square of the error (m^2). 
C
C Notes for Nov 2002 update. 
C Eliminate the include 'observes.h' by using the functions that 
C return the number of observations. 
C Deal with the possibility that nobs = 0 or n_eobs=0.
C Suggestion set weight functions to zero. 
C

      subroutine get_errvar

      include 'CASCO.DIM' 
      include 'covariance.h'
c      include 'observes.h' 
      include 'casco_params.h' 

c  functions
      integer nobservations
      integer nelevobservations


      real vel_err_var 
      real elev_err_var 

      integer nobs, n_eobs 
      integer i 

      nobs = nobservations()
      n_eobs = nelevobservations()

      vel_err_var = exp_rms_vel_err**2 
      elev_err_var = exp_rms_elev_err**2 

      write(*,*)'GET_ERRVAR'
      write(*,*)'nobs, n_eobs ', nobs, n_eobs 
      write(*,*)' Velocity error variance = ',vel_err_var
      write(*,*)' Elevation error variance ', elev_err_var 

      do i = 1, NOBSDIM
         ercov(i) = 0.0
         W_delta(i) = 0.0
      enddo 

      do i = 1, NELEVOBSDIM
         elevcov(i) = 0.0
         W_eps(i) = 0.0
      enddo 
      
      if(nobs.gt.0) then 
         write(*,*)'GET_ERRVAR: set ercov and W_delta' 
         do i = 1, nobs 
            ercov(i) = vel_err_var 
         enddo

         do i = 1, nobs
            W_delta(i) = 1.0/(float(nobs)*ercov(i))
         enddo
      endif 

      if(n_eobs.gt.0) then 
         write(*,*)'GET_ERRVAR: set elevcov and W_eps' 
         do i = 1, n_eobs
            elevcov(i) = elev_err_var
         enddo
         do i = 1, n_eobs
            W_eps(i) = 1.0/(float(n_eobs)*elevcov(i))
         enddo
      endif  


      return 
      end
