!---------------------------------------------------------------------
!     Copyright (C) GFD Dennou Club, 2004. All rights reserved.
!---------------------------------------------------------------------
! histaxis.f90 - Sample program for gt4_history/gt4f90io.
!                Test Program for "HistoryCreate2",
!                "HistoryAddVariable2", "HistoryPut",
!                "HistorySetTime", "HistoryClose".
!
! History
!   2004/11/27 Morikawa Yasuhiro     Create

program histaxis
    use gt4f90io
    use dc_trace,  only: SetDebug, DbgMessage
    use dc_string, only: token
    use gt4_history
implicit none
    integer:: i
    type(GT_HISTORY_AXIS)           :: axes1(1), axes2(2), axes4(3)
    type(GT_HISTORY_VARINFO)        :: var1, var4
    character(token), pointer       :: dims1(:) =>null()
    type(GT_HISTORY)                :: gt4hist_axis_3
    type(GT_HISTORY_ATTR), allocatable :: attrs4_lon(:), attrs4_zeta(:)
    real(8)          :: Zeta(4,8)

continue

    !----- デバッグモードへ -----
    call SetDebug

    !-----------------------------------------------------------------
    !  HistoryCreate2 と HistoryAddVariable2 を用いた基本出力テスト
    !-----------------------------------------------------------------
    !----- 軸の指定 -----
    axes1(1)%name     = 'time'
    axes1(1)%length   = 0
    axes1(1)%longname = 'time'
    axes1(1)%units    = 's @ 2001-12-16T10:00'
    axes1(1)%xtype    = 'float'

    !----- 変数の指定 -----
    var1%name     = 'u'
    allocate( var1%dims(1) )
    var1%dims     = (/'time'/)
    var1%longname = 'any quantity'
    var1%units    = 'non-dimensional'
    var1%xtype    = 'float'


    call HistoryCreate(file='xhistaxis/xhistaxis1.nc', &
        & title='gt4_history HistoryCreate2 test 1',   &
        & source='gt4f90io/Fortran library test kit',  &
        & institution='GFD Dennou Club',               &
        & axes=axes1(:),                               &
        & origin=0.0, interval=0.0,                    &
        & conventions="http://www.gfd-dennou.org/arch/gtool4/conventions/", &
        & gt_version="4.1"  )

    call HistoryAddVariable(var1)
    do, i = 1, 24
        call HistoryPut( axes1(1)%name , real(i) )
        call HistoryPut( var1%name, real(i) * 10.0)
    enddo
    call HistoryClose


    !-----------------------------------------------------------------
    !  HistoryCreate2 と HistoryAddVariable2 を用いた出力テスト
    !    構造体の直接指定
    !-----------------------------------------------------------------
    allocate(dims1(2))
    dims1(:) = (/'x   ', 'time'/)

    call HistoryCreate(file='xhistaxis/xhistaxis2.nc', & 
        & title='gt4_history HistoryCreate2 test 2', &
        & source='gt4f90io/Fortran library test kit', &
        & institution='GFD DennouClub', &
        & conventions="http://www.gfd-dennou.org/arch/gtool4/conventions/", &
        & gt_version="4.1", &
        & axes=(/  &
        &        GT_HISTORY_AXIS('x', 3, 'eastward length', 'm', 'float'), &
        &        GT_HISTORY_AXIS('time', 0, 'time', 's', 'float') &
        &       /),  &
        & origin=0.0, interval=0.0) 
    call HistoryAddVariable( &
        & varinfo=GT_HISTORY_VARINFO(  &
        &   'u', dims1(:), 'foo quantity', 'm/s', 'float'  ) )
    call HistoryAddVariable( &
        & varinfo=GT_HISTORY_VARINFO(  &
        &   'v', dims1(:), 'foo quantity', 'm/s', 'float'  ) )
    call HistoryPut('x', (/0.0, 1.0, 2.0/))
    call HistoryPut('time', 0.0)
    call HistoryPut('u', (/0.0, 0.1, 0.2/))
    call HistoryPut('v', (/1.0, 1.1, 1.2/))
    call HistoryPut('time', 1.0)
    call HistoryPut('u', (/2.0, 2.1, 2.2/))
    call HistoryPut('time', 2.0)
    call HistoryPut('u', (/3.0, 3.1, 3.2/))
    call HistoryPut('v', (/4.0, 4.1, 4.2/))
    call HistoryClose

    !-----------------------------------------------------------------
    !  HistoryCreate2 を用いた出力テスト
    !    明示的に引数キーワードを与えずに出力
    !-----------------------------------------------------------------
    !----- 軸の指定 -----
    axes2(1)%name     = 'x'
    axes2(1)%length   = 3
    axes2(1)%longname = 'eastward length'
    axes2(1)%units    = 'm'
    axes2(1)%xtype    = 'float'
    axes2(2)%name     = 'time'
    axes2(2)%length   = 0
    axes2(2)%longname = 'time'
    axes2(2)%units    = 's'
    axes2(2)%xtype    = 'float'


    call HistoryCreate('xhistaxis/xhistaxis3.nc',   &
        & 'gt4_history HistoryCreate2 test 3',      &
        & 'gt4f90io/Fortran library test kit',      &
        & 'GFD Dennou Club',                        &
        & axes2,                                    &
        & 100.0, 2.5,                               &
        & gt4hist_axis_3,                           &
        & 'http://www.gfd-dennou.org/arch/gtool4/conventions/', '4.1')

    call HistoryPut('x', (/100.0, 200.0, 300.0/), gt4hist_axis_3)
    call HistoryAddVariable('u', dims=(/'x   ', 'time'/), &
         & longname='any quantity', units='m/s', history=gt4hist_axis_3)
    do, i = 1, 3
       call HistoryPut('u', (/1.0*i, 2.0*i, 3.0*i/), gt4hist_axis_3)
    enddo
    call HistoryClose(gt4hist_axis_3)


    !-----------------------------------------------------------------
    !  HistoryAddAttr2 を用いた出力テスト
    !-----------------------------------------------------------------
    !----- 軸の指定 -----
    axes4(1)%name     = 'lon'
    axes4(1)%length   = 4
    axes4(1)%longname = 'longitude'
    axes4(1)%units    = 'deg.'
    axes4(1)%xtype    = 'float'

    axes4(2)%name     = 'lat'
    axes4(2)%length   = 8
    axes4(2)%longname = 'latitude'
    axes4(2)%units    = 'deg.'
    axes4(2)%xtype    = 'float'

    axes4(3)%name     = 'time'
    axes4(3)%length   = 0
    axes4(3)%longname = 'time'
    axes4(3)%units    = 'sec.'
    axes4(3)%xtype    = 'float'

    !----- 変数の指定 -----
    var4%name     = 'zeta'
    allocate( var4%dims(3) )
    var4%dims     = (/'lon ', 'lat ', 'time'/)
    var4%longname = 'vorticity'
    var4%units    = '1/s'
    var4%xtype    = 'double'


    !----- 属性の指定 -----
    allocate( attrs4_lon(2) )
    attrs4_lon(1)%attrname = 'topology'
    attrs4_lon(1)%attrtype = 'char'
    attrs4_lon(1)%cvalue   = 'circular'

    attrs4_lon(2)%attrname = 'modulo'
    attrs4_lon(2)%attrtype = 'REAL'
    attrs4_lon(2)%rvalue   = 360.0

    allocate( attrs4_zeta(4) )
    attrs4_zeta(1)%attrname = 'gt_graph_contours_levels'
    attrs4_zeta(1)%attrtype = 'float'
    allocate( attrs4_zeta(1)%rarray(5) )
    attrs4_zeta(1)%array    = .true.
    attrs4_zeta(1)%rarray   = (/996.0, 1000.0, 1004.0, 1008.0, 1012.0/)

    attrs4_zeta(2)%attrname = 'missing_value'
    attrs4_zeta(2)%attrtype = 'DB'
    attrs4_zeta(2)%dvalue   = -2.0d20

    attrs4_zeta(3)%attrname = 'ignore_missing'
    attrs4_zeta(3)%attrtype = 'logical'
    attrs4_zeta(3)%lvalue   = .true.

    attrs4_zeta(4)%attrname = 'zero_point'
    attrs4_zeta(4)%attrtype = 'int'
    attrs4_zeta(4)%ivalue   = 1000

    !----- gt4_history モジュールの各サブルーチンを呼ぶ
    call HistoryCreate(file='xhistaxis/xhistaxis4.nc',       &
        & title='gt4_history HistoryCreate2 test 4',         &
        & source='gt4f90io/Fortran library test kit',        &
        & institution='GFD Dennou Club',                     &
        & axes=axes4(:),                                     &
        & origin=111.0, interval=12.3                       )

    call HistoryAddVariable(var4)
    call HistoryAddAttr('lon',  attrs4_lon)
    call HistoryAddAttr('zeta', attrs4_zeta)

    call HistoryPut( 'lon', (/0., 90., 180., 270./) )
    call HistoryPut( 'lat', &
         & (/-70.0, -50.0, -30.0, -10.0, 10.0, 30.0, 50.0, 70.0/) )

      Zeta(:,:) = 3.0
      call HistoryPut('zeta', Zeta)
      Zeta(:,:) = 4.0
      call HistoryPut('zeta', Zeta)
    call HistoryClose

end program histaxis
