module setup_config_from_lmdz

  use parkind1, only : jprb

  implicit none

  public

  type driver_config_type
     logical    :: ok_effective_size  = .true.
     logical    :: ok_separation_eta = .false.   
     logical    :: ok_separation_tanh = .false.
     real(jprb) :: high_inv_effective_size   = -1.0_jprb ! m-1
     real(jprb) :: middle_inv_effective_size = -1.0_jprb ! m-1
     real(jprb) :: low_inv_effective_size    = -1.0_jprb ! m-1
     real(jprb) :: cloud_inhom_separation_factor  = -1.0_jprb
     real(jprb) :: cloud_separation_scale_surface = -1.0_jprb
     real(jprb) :: cloud_separation_scale_toa     = -1.0_jprb
     real(jprb) :: cloud_separation_scale_power   = -1.0_jprb
     real(jprb) :: frac_std = 0.75_jprb
     real(jprb) :: overlap_decorr_length = 2000.0_jprb
     ! KDECOLAT : 0 > cste, 1 > Shonk et al. (2010) 2 > 1 lisse a l'equateur
     !            3 : 1+variation sur la verticale
     !            4 : 2+variation sur la verticale
     integer    :: kdecolat = 0
     real(jprb) :: low_decorrelation_length = 2000.0_jprb 
     real(jprb) :: mid_decorrelation_length = 2000.0_jprb
     real(jprb) :: high_decorrelation_length = 2000.0_jprb

     ! Save inputs in "inputs.nc"
     logical :: do_save_inputs
 contains
 procedure :: read => read_config_from_namelist

  end type driver_config_type

contains

  !---------------------------------------------------------------------
  subroutine read_config_from_namelist(this, file_name, is_success)

    use radiation_io, only : nulerr, radiation_abort       

    class(driver_config_type), intent(inout) :: this
    character(*), intent(in)          :: file_name
    logical, intent(out), optional    :: is_success
    logical    :: ok_effective_size, ok_separation_eta, ok_separation_tanh
    integer    :: iosopen ! Status after calling open
    real(jprb) :: high_inv_effective_size 
    real(jprb) :: middle_inv_effective_size
    real(jprb) :: low_inv_effective_size
    real(jprb) :: cloud_inhom_separation_factor 
    real(jprb) :: cloud_separation_scale_surface
    real(jprb) :: cloud_separation_scale_toa
    real(jprb) :: cloud_separation_scale_power
    real(jprb) :: frac_std
    real(jprb) :: overlap_decorr_length
    integer    :: kdecolat 
    real(jprb) :: low_decorrelation_length
    real(jprb) :: mid_decorrelation_length 
    real(jprb) :: high_decorrelation_length 
    logical :: do_save_inputs

    namelist /radiation_driver/ ok_effective_size, ok_separation_eta, ok_separation_tanh, &
         &  frac_std, overlap_decorr_length, kdecolat, &
         &  low_decorrelation_length, mid_decorrelation_length, high_decorrelation_length, &
         &  high_inv_effective_size, middle_inv_effective_size, low_inv_effective_size, &
         &  cloud_inhom_separation_factor, cloud_separation_scale_surface, &
         &  cloud_separation_scale_toa, cloud_separation_scale_power, &
         do_save_inputs

    ok_effective_size = .false.
    ok_separation_eta = .false.
    ok_separation_tanh = .false.
    high_inv_effective_size   = -1.0_jprb 
    middle_inv_effective_size = -1.0_jprb 
    low_inv_effective_size    = -1.0_jprb 
    cloud_inhom_separation_factor  = -1.0_jprb
    cloud_separation_scale_surface = -1.0_jprb
    cloud_separation_scale_toa     = -1.0_jprb
    cloud_separation_scale_power   = -1.0_jprb
    frac_std = 0.75_jprb
    overlap_decorr_length = 2000.0_jprb
    kdecolat = 0
    low_decorrelation_length = 2000.0_jprb
    mid_decorrelation_length = 2000.0_jprb
    high_decorrelation_length = 2000.0_jprb
    do_save_inputs = .false.

    ! Open the namelist file and read the radiation_driver namelist
    open(unit=10, iostat=iosopen, file=trim(file_name))
    if (iosopen /= 0) then
      ! An error occurred
      if (present(is_success)) then
        is_success = .false.
        ! We now continue the subroutine so that the default values
        ! are placed in the config structure
      else
        write(nulerr,'(a,a,a)') '*** Error: namelist file "', &
             &                trim(file_name), '" not found'
        call radiation_abort('Driver configuration error')
      end if
    else
      ! Read the radiation_driver namelist, noting that it is not an
      ! error if this namelist is not present, provided all the required
      ! variables are present in the NetCDF data file instead
      read(unit=10, nml=radiation_driver)
      close(unit=10)
    end if

    ! Copy namelist data into configuration object
    this%ok_effective_size = ok_effective_size 
    this%ok_separation_eta = ok_separation_eta
    this%ok_separation_tanh = ok_separation_tanh
    this%frac_std = frac_std
    this%overlap_decorr_length = overlap_decorr_length
    this%kdecolat = kdecolat
    this%low_decorrelation_length = low_decorrelation_length
    this%mid_decorrelation_length = mid_decorrelation_length
    this%high_decorrelation_length = high_decorrelation_length
    this%cloud_inhom_separation_factor = cloud_inhom_separation_factor
    this%cloud_separation_scale_surface = cloud_separation_scale_surface
    this%cloud_separation_scale_toa = cloud_separation_scale_toa
    this%cloud_separation_scale_power = cloud_separation_scale_power
    this%high_inv_effective_size = high_inv_effective_size
    this%middle_inv_effective_size = middle_inv_effective_size
    this%low_inv_effective_size = low_inv_effective_size
    this%do_save_inputs = do_save_inputs

  end subroutine read_config_from_namelist          

end module setup_config_from_lmdz
