! --------------------------------------------------------
! Instituto Nacional de Pesquisas Espaciais - INPE
! Coordenacao-Geral de Ciencias da Terra - CGCT
! Divisao de Impactos, Adaptacao e Vulnerabilidade - DIIAV
!
! Projeto: SisMOM
! Sistema de Monitoramento e Modelagem Oceanica e Costeira
! --------------------------------------------------------
! Leitura do namelist de configuracao (grupo &config do config.nml).
! --------------------------------------------------------
module mod_config
  use mod_kinds, only: dp
  implicit none
  private
  public :: config_t, read_config

  integer, parameter :: MAXG = 32, MAXZ = 100, SLEN = 256

  type :: config_t
    character(len=SLEN) :: input_dir, grid_dir, tmp_dir
    character(len=SLEN) :: atm_file, atm_uas, atm_vas, atm_tas
    character(len=SLEN) :: uo_file, vo_file, thetao_file, so_file
    character(len=10)   :: start_date
    integer             :: n_timesteps
    real(dp)            :: fill_value
    integer             :: ngrids, nz
    character(len=8)    :: grids(MAXG)
    real(dp)            :: z_levels(MAXZ)
  end type config_t

contains

  subroutine read_config(path, cfg)
    character(len=*), intent(in)  :: path
    type(config_t),   intent(out) :: cfg
    integer :: u, ios, i

    character(len=SLEN) :: input_dir, grid_dir, tmp_dir
    character(len=SLEN) :: atm_file, atm_uas, atm_vas, atm_tas
    character(len=SLEN) :: uo_file, vo_file, thetao_file, so_file
    character(len=8)    :: grids(MAXG)
    character(len=10)   :: start_date
    integer             :: n_timesteps
    real(dp)            :: fill_value
    real(dp)            :: z_levels(MAXZ)

    namelist /config/ input_dir, grid_dir, tmp_dir, grids, atm_file, atm_uas, &
         atm_vas, atm_tas, uo_file, vo_file, thetao_file, so_file, start_date, &
         n_timesteps, fill_value, z_levels

    input_dir = ''; grid_dir = ''; tmp_dir = ''
    atm_file = ''; atm_uas = ''; atm_vas = ''; atm_tas = ''
    uo_file = ''; vo_file = ''; thetao_file = ''; so_file = ''
    start_date = ''; n_timesteps = 0; fill_value = -999.0_dp
    grids = ''; z_levels = -huge(1.0_dp)

    open(newunit=u, file=path, status='old', action='read', iostat=ios)
    if (ios /= 0) then
      write(*,*) 'ERRO: nao foi possivel abrir o namelist: ', trim(path)
      stop 1
    end if
    read(u, nml=config, iostat=ios)
    if (ios /= 0) then
      write(*,*) 'ERRO: falha ao ler o grupo &config de ', trim(path)
      stop 1
    end if
    close(u)

    cfg%input_dir = input_dir; cfg%grid_dir = grid_dir; cfg%tmp_dir = tmp_dir
    cfg%atm_file = atm_file; cfg%atm_uas = atm_uas; cfg%atm_vas = atm_vas
    cfg%atm_tas = atm_tas
    cfg%uo_file = uo_file; cfg%vo_file = vo_file
    cfg%thetao_file = thetao_file; cfg%so_file = so_file
    cfg%start_date = start_date; cfg%n_timesteps = n_timesteps
    cfg%fill_value = fill_value
    cfg%grids = grids; cfg%z_levels = z_levels

    cfg%ngrids = 0
    do i = 1, MAXG
      if (len_trim(grids(i)) > 0) cfg%ngrids = cfg%ngrids + 1
    end do
    cfg%nz = 0
    do i = 1, MAXZ
      if (z_levels(i) > -huge(1.0_dp)/2) cfg%nz = cfg%nz + 1
    end do

    if (cfg%ngrids == 0) stop 'ERRO: nenhuma grade definida em grids'
    if (cfg%nz == 0) stop 'ERRO: nenhum nivel definido em z_levels'
  end subroutine read_config

end module mod_config
