!== Sample program for gt4_history/gt4f90io.
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: histinquire.f90,v 1.2 2006/01/17 05:54:06 morikawa Exp $
! Tag Name::  $Name: gt4f90io-20060123 $
! Copyright:: Copyright (C) GFD Dennou Club, 2000-2005. All rights reserved.
! License::   See COPYRIGHT[link:../COPYRIGHT]
!
! Test Program for "Inquire", in gt4_history

program histinquire
  use gt4f90io
  use dc_trace, only: SetDebug
  use gt4_history
  implicit none
  integer:: i, length
  character(STRING) :: xtype, units, longname, name
  character(STRING), pointer :: dims(:)
  logical :: err
  type(GT_HISTORY) :: gthist
  type(GT_HISTORY_AXIS), pointer :: axes(:)
  type(GT_HISTORY_VARINFO), pointer :: varinfo(:)
continue

  !----- デバッグモードへ -----
!  call SetDebug

  !
  ! とりあえずファイル出力
  !
  call HistoryCreate(file = 'xhistinquire1.nc', &
    & title = 'Inquire in gt4_history test 1',      &
    & source = 'gt4f90io/Fortran library test kit', &
    & institution = 'GFD Dennou Club',              &
    & dims = StoA('x', 'time'),                  &
    & dimsizes = (/3, 0/),                          &
    & longnames = StoA('eastward length', 'time'), &
    & units = StoA('m', 'sec.'),                   &
    & origin = 0.0, interval = 2.5,                   &
    & xtypes = StoA('double', 'real'), &
    & history = gthist)

  call HistoryAddVariable('u', dims=StoA('x', 'time'), &
    & longname='any quantity', units='m/s', history=gthist)
  call HistoryAddVariable('u_ave', dims=StoA('time'), &
    & longname='any average', units='m/s', history=gthist)

  call Inquire(gthist, axes = axes)
  call Inquire(gthist, err = err, varinfo = varinfo)

  call Printf(fmt='Dimension Information')
  do i = 1, size(axes)
    call Inquire(axes(i), name, length, longname, units, xtype)
    call Printf(fmt='   name=%a, size=%d, longname=%a, units=%a, xtype=%a', &
      & i=(/length/), ca=StoA(name, longname, units, xtype))
  end do

  call Printf(fmt='Variable Information')
  if (err) then
    call Printf(fmt='   Variable is not found')
  else
    do i = 1, size(varinfo)
      call Inquire(varinfo(i), name, dims, longname, units, xtype)
      call Printf(fmt='   name=%a, dims=(%a), longname=%a, units=%a, xtype=%a', &
        & ca=StoA(name, JoinChar(dims), longname, units, xtype))
    end do
  end if

  call HistoryClose(gthist)

end program histinquire
