# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module tomo_objects_mod
  use error_mod
  use traceback_mod
  use model_covar_mod
  use eps_reg_mod
  use tomo_oper_mod
  use data_covar_mod
  use eps_extra_mod
  use extra_op_mod
  use model_des_mod
  use multiple_realizations_mod
  use tomo_inv_mod
  use check_tomo_ops_mod
  implicit none
  logical, private,save :: verb
  contains
  logical function read_in_tomo_basics()
    read_in_tomo_basics=.false.
    call from_param("verb",verb,.false.)
!INITIALIZE MY PARAMETER CHECKING"
    call init_param_check()
!READ IN VELOCITY
    if (verb) then
      write(0,*) "  Before reading velocity"
    end if
    if (.not. read_in_velocity()) then
      write(0,*) "  ERROR: reading in velocity"
      return
    end if
!CREATE TOMO SPACE CONVERTER
!
! Basically we do some padding in converting to tau if needed
!
    if (verb) then
      write(0,*) "  Before converting velocity to tomography space"
    end if
    if (.not. init_space_conv()) then
      write(0,*) "  ERROR: initializing space converter"
      return
    end if
!READ IN REFLECTORS
    if (verb) then
      write(0,*) "  Before reading reflectors"
    end if
    if (.not. init_reflector()) then
      write(0,*) "  ERROR: reading in reflectors"
      return
    end if
    read_in_tomo_basics=.true.
  end function
!ROUTINE TO CHECK MAKE SURE PARAMETERS MAKE SOME LOGICAL SENSE
  logical function check_tomo_params()
    check_tomo_params=.false.
!##################
!BUILD DATA ERRORS
! The data errors function is responsible for
! -creating an error file (err)
! -sep3d file with x,y,z,dipxz,dipyz, and coher
! -unique specifier on type err_method=
! -having an error calc function with the interface
! -real function(move)
! -should have the ability to sense whether its parameters have changed
! -add to by checking marked places in errors_mod
!
    if (verb) then
      write(0,*) "  Before checking data error parameters"
    end if
    if (.not. check_data_err_params()) then
      write(0,*) "  ERROR: checking data error params"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking traceback parameters"
    end if
    if (.not. check_traceback_params()) then
      write(0,*) "  ERROR: checking traceback parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking model covariance parameters"
    end if
    if (.not. check_model_covar_params()) then
      write(0,*) "  ERROR: checking model covariance parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking model styling epsilon parameters"
    end if
    if (.not. check_eps_reg_params()) then
      write(0,*) "  ERROR: initializing eps regularization parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking tomography operators parameters"
    end if
    if (.not. check_tomo_oper_params()) then
      write(0,*) "  ERROR: checking tomography operator parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking extra operator parameters"
    end if
    if (.not. check_extra_op_params()) then
      write(0,*) "  ERROR: checking extra operator parameters"
      return
    end if
    if (.not. check_data_covar_params()) then
      write(0,*) "  ERROR: checking data covaraince parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking epsilon extra parameters"
    end if
    if (.not. check_eps_extra_params()) then
      write(0,*) "  ERROR:  checking extra epsilon parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking model parameterization"
    end if
    if (.not. check_model_des_params()) then
      write(0,*) "  ERROR: checking model description parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking multiple realizations parameters"
    end if
    if (.not. check_mult_params()) then
      write(0,*) "  ERROR: checking multiple realizations parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking tomography inversion parameters"
    end if
    if (.not. check_tomo_inv_params()) then
      write(0,*) "  ERROR: checking tomographic inversion parameters"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking punish_change_parame"
    end if
    if (.not. check_punish_change_params()) then
      write(0,*) "  ERROR: checking punish_change_params"
      return
    end if
    if (verb) then
      write(0,*) "  Before checking check_opers"
    end if
    if (.not. check_tomo_ops_params()) then
      write(0,*) "  ERROR: checking tomographic operators parameters"
      return
    end if
    check_tomo_params=.true.
  end function
  logical function init_tomo_objects()
    init_tomo_objects=.false.
    if (verb) then
      write(0,*) "  Before  initializing data errors"
    end if
    if (.not. init_data_errs()) then
      write(0,*) "  ERROR: initializing data errors"
      return
    end if
    if (verb) then
      write(0,*) "  Before initialize trace back"
    end if
    if (.not. init_traceback()) then
      write(0,*) "  ERROR: initializing trace back"
      return
    end if
!BUILD REG/PREC FILTER
! Should build regulization filter(s)
! Must have a unique specifier in first filter file (filt_xz)
! -filt_method
! -add to in model_covar
!
    if (verb) then
      write(0,*) "  Before creating filter"
    end if
    if (.not. init_model_covar()) then
      write(0,*) "  ERROR: initializing model covariance"
      return
    end if
    if (verb) then
      write(0,*) "  Before initializing eps for model styling goal"
    end if
    if (.not. init_eps_reg()) then
      write(0,*) "  ERROR: initializing eps_reg"
      return
    end if
    if (verb) then
      write(0,*) "  Before initializing tomography operator"
    end if
    if (.not. init_tomo_oper()) then
      write(0,*) "  ERROR: initializing tomography operator"
      return
    end if
!BUILD EXTRA REG
! Is responsible for having an
! extra_reg_init, extra_reg_op, extra_reg_dat(pointer dat),extra_reg_mod0(pointer mod)
! By default should be the null_etra_reg_init,...
    if (verb) then
      write(0,*) "  Before extra  regularization operator"
    end if
    if (.not. init_extra_op()) then
      write(0,*) "  ERROR: initializing extra operator"
      return
    end if
    if (verb) then
      write(0,*) "  Before initializing extra eps"
    end if
    if (.not. init_eps_extra()) then
      write(0,*) "  ERROR: initializing extra operator epsilon"
      return
    end if
    if (verb) then
      write(0,*) "  Before initializing data covariance operator"
    end if
    if (.not. init_data_covar()) then
      write(0,*) "  ERROR: initializing data covaraince operator"
      return
    end if
    if (verb) then
      write(0,*) "  Before finish tracebacks"
    end if
    if (.not. finish_trace_backs()) then
      write(0,*) " Trouble finishing tracebacks"
      return
    end if
    if (verb) then
      write(0,*) "  Before initialzing model descritpiton"
    end if
    if (.not. init_model_des()) then
      write(0,*) "  ERROR: initializing model description"
      return
    end if
    if (verb) then
      write(0,*) "  Before initialziing multiple realizations"
    end if
    if (.not. init_multiple_realizations()) then
      write(0,*) "  ERROR: initializing multiple realizations"
      return
    end if
    if (verb) then
      write(0,*) "  Before initializing tomographic inversion"
    end if
    if (.not. init_tomo_inversion()) then
      write(0,*) "  ERROR: initializing  tomographic inversion"
      return
    end if
    if (.not. init_check_ops()) then
      write(0,*) "  ERROR: trouble initializing operator testing"
      return
    end if
    init_tomo_objects=.true.
  end function
end module
