! Copyright (C) GFD Dennou Club, 2000.  All rights reserved.

! 縺ゅｋ繝輔ぃ繧､繝ｫ蜷� urlBase 縺ｫ萓晏ｭ倥☆繧句､画焚縺吶∋縺ｦ繧貞叙蠕励☆繧九↓縺ｯ縺ｾ縺�
! var_search(iter, urlbase) 繧貞他縺ｳ蜃ｺ縺励※縺九ｉ辟｡髯舌Ν繝ｼ繝励�荳ｭ縺ｧ
! attr_next(iter, url, end) 繧貞他縺ｳ蜃ｺ縺吶Ｖrl 縺後�縺ｨ縺､
! 縺ｲ縺ｨ縺､縺ｮ螻樊ｧ蜷阪ｒ荳弱∴繧九Ｆnd 縺檎悄縺ｫ縺ｫ縺ｪ縺｣縺溘→縺阪�
! 縺吶∋縺ｦ縺ｮ螻樊ｧ繧呈爾邏｢縺礼ｵゅ∴縺溘％縺ｨ縺ｫ縺ｪ繧九�

subroutine GDNcVarSearchInit(iter, urlBase)
  use dc_types, only: string
  use gtdata_netcdf_types, only: GD_NC_VARIABLE_SEARCH
  use dc_url, only: UrlSplit
  use gtdata_netcdf_file_generic, only: GDNcFileOpen
  use dc_trace, only: beginsub, endsub, DbgMessage
  implicit none
  type(GD_NC_VARIABLE_SEARCH), intent(out):: iter
  character(len = *), intent(in):: urlBase
  character(len = string):: file
  logical:: err
  character(len = *), parameter:: subname = 'GDNcVarSearchInit'

  call beginsub(subname, 'urlbase=<%c>', c1=trim(urlbase))
  call UrlSplit(trim(urlbase), file=file)
  call GDNcFileOpen(iter%fileid, filename=file, writable=.FALSE., err=err)
  if (err) iter%fileid = -1
  iter%varid = 0
  iter%dimid = 0
  call endsub(subname, 'file=%d', i=(/iter%fileid/))
end subroutine GDNcVarSearchInit

subroutine GDNcVarSearchNext(iter, url, end)
  use gtdata_netcdf_types, only: GD_NC_VARIABLE_SEARCH
  use dc_types, only: string
  use netcdf, only: NF90_MAX_NAME, NF90_NOERR, &
    & NF90_INQUIRE_VARIABLE, NF90_INQUIRE_DIMENSION, NF90_INQ_VARID
  use dc_url, only: UrlMerge
  use gtdata_netcdf_file_generic, only: GDNcFileInquire, GDNcFileClose
  use dc_trace, only: beginsub, endsub, DbgMessage
  implicit none
  type(GD_NC_VARIABLE_SEARCH), intent(inout):: iter
  character(len = *), intent(out):: url
  logical, intent(out):: end
  character(len = string):: filename
  character(len = NF90_MAX_NAME):: varname
  integer:: stat, varid_tmp
  character(len = *), parameter:: subname = 'GDNcVarSearchNext'
continue
  call beginsub(subname)
  if (iter%fileid <= 0) then
    end = .TRUE.
    url = ''
    call endsub(subname, "bad file %d", i=(/iter%fileid/))
    return
  endif
  if (iter%varid >= 0) then
    iter%varid = iter%varid + 1
    stat = NF90_INQUIRE_VARIABLE(iter%fileid, iter%varid, name = varname)
    if (stat == NF90_NOERR) goto 900
    iter%varid = -1
  endif
  do while (iter%dimid >= 0)
    iter%dimid = iter%dimid + 1
    ! --- 謖�ｮ夂分蜿ｷ縺ｮ谺｡蜈�′縺ｪ縺代ｌ縺ｰ繧ｨ繝ｩ繝ｼ ---
    stat = NF90_INQUIRE_DIMENSION(iter%fileid, iter%dimid, name = varname)
    if (stat /= NF90_NOERR) exit
    ! --- 謖�ｮ夂分蜿ｷ縺ｮ谺｡蜈�→蜷悟錐縺ｮ螟画焚縺後≠繧後�蜊ｴ荳九∵ｬ｡逡ｪ蜿ｷ縺ｸ ---
    stat = NF90_INQ_VARID(iter%fileid, varname, varid_tmp)
    if (stat /= NF90_NOERR) goto 900
  enddo
  end = .TRUE.
  url = ""
  call GDNcFileClose(iter%fileid)
  iter%dimid = -1
  call endsub(subname, "end file %d", i=(/iter%fileid/))
  return

900 continue
  call GDNcFileInquire(iter%fileid, name=filename)
  url = UrlMerge(file=trim(filename), var=trim(varname))
  end = .FALSE.
  call endsub(subname, "file=%d url=<%c>", &
    & i=(/iter%fileid/), c1=trim(url))
  return
end subroutine GDNcVarSearchNext