# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
!<;
!err_mod
!
!Author: Robert Clapp, ESMB 463, 7230253
!
! Sun Jan 12 14:02:42 PST 2003
!
!
!Purpose:
!
! Handle the data residual
!
!To add a new error type:
!
! -add to check_pars
! -add to initializer
! -add to op
!
!
! Currently
!
! -none
!
module data_covar_mod
  use adj_mod
  use param_check_mod
  use smooth_diff_mod
  implicit none
  character(len=128) :: resid_method
  logical, private, save :: verb
  contains
  logical function init_data_covar()
    logical :: file_ok,is_2d
    init_data_covar=.false.
!check to see if parameters are different
!#########################################
! IMPORTANT #
! #
! THIS WHERE WE DESCRIBE HOW TO #
! BUILD DATA_RESID #
! #
! MUST HAVE A CORARLY IN #
! #
! CHECK PARS/OP/INITIAL_OP #
! #
!#########################################
    if (resid_method(1:4).eq."NONE") then
    else if (resid_method(1:4).eq."SMOO") then
      if (.not. smooth_diff_init()) then
        write(0,*) "trouble initializing smooth difference mod"
        return
      end if
    else
      write(0,*) "Unknown resid method=",trim(resid_method)
      return
    end if
    init_data_covar=.true.
  end function
! The number elements in the range given the number elements in the model
  integer function data_covar_nrange(nmod)
    integer :: nmod
    data_covar_nrange=0
!BY DEFAULT DIAGONAL 1
    if (resid_method(1:4).eq."NONE") then
      data_covar_nrange=nmod
    else if (resid_method(1:4).eq."SMOO") then
      data_covar_nrange=ndomain_smooth()
    else
      write(0,*) "data_resid_method",trim(resid_method)
      call seperr("Unknown data_resid_method")
    end if
  end function
  integer function data_covar_op(adj,add,model,data)
    logical,intent(in) :: adj,add
    real :: model(:),data(:)
!BY DEFAULT DIAGONAL 1
    if (resid_method(1:4).eq."NONE") then
      call adjnull(adj,add,model,data)
      if (.not. adj) then
        data= data+model
      else
        model= model+data
      end if
    else if (resid_method(1:4).eq."SMOO") then
      data_covar_op=close_op(adj,add,model,data)
    else
      write(0,*) "data_resid_method",trim(resid_method)
      call seperr("Unknown data_resid_method")
    end if
    data_covar_op=0
  end function
  logical function check_data_covar_params()
    character(len=128) :: tag
    logical :: stat(100)
    stat=.true.
    check_data_covar_params=.false.
    call from_param("verb",verb,.false.)
!#########################################
! IMPORTANT #
! #
! THIS WHERE WE DESCRIBE HOW TO #
! INITIALIZE/CHECK OBJECT PARAMS #
! #
!#########################################
    call from_param("data_resid_method",resid_method,"NONE")
    if (resid_method(1:4).eq."NONE") then
! if(verb) write(0,*) "No data covariance operation"
    else if (resid_method(1:4).eq."SMOO") then
    else
      write(0,*) "Unknown data_resid_method=",trim(resid_method)
      return
    end if
    check_data_covar_params=.true.
  end function
end module
