!== Sample program for gtool_history/gtool5
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: histcopy.f90,v 1.1 2008-09-23 09:56:38 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006-. All rights reserved.
! License::   See COPYRIGHT[link:../COPYRIGHT]
!
program histcopy
  ! histcopy.f90 - Sample program for gtool_history/gtool5.
  !                Test Program for "HistoryCopyVariable".
  !
  use dc_trace,   only: SetDebug
  use dc_message, only: MessageNotify
  use dc_types,   only: DP
  use gtool_history
  implicit none
  integer            :: i,j,k,l                        ! 菴懈･ｭ螟画焚
  integer, parameter :: nx=3, ny=4, nz=5               ! 繧ｰ繝ｪ繝�ラ謨ｰ
  integer, parameter :: time_step=6                    ! 譎る俣繧ｹ繝�ャ繝玲焚
  real,    parameter :: x(nx)=(/(1.0*(i-1),i=1,nx)/)   ! x蠎ｧ讓吝､画焚
  real(DP),parameter :: y(ny)=(/(10.0*(i-1),i=1,ny)/)  ! y蠎ｧ讓吝､画焚
  real(DP),parameter :: z(nz)=(/(100.0*(i-1),i=1,nz)/) ! z蠎ｧ讓吝､画焚

  real               :: u           ! 蜃ｺ蜉帷畑辟｡谺｡蜈�､画焚
  real(DP)           :: v(nx)       ! 蜃ｺ蜉帷畑 1 谺｡蜈�､画焚
  real               :: w(nx,ny)    ! 蜃ｺ蜉帷畑 2 谺｡蜈�､画焚
  real(DP)           :: q(nx,ny,nz) ! 蜃ｺ蜉帷畑 3 谺｡蜈�､画焚

continue

  call SetDebug

  !-----------------------------------------------------------------
  ! 縺ｾ縺壹�繧ｳ繝斐�蜈�→縺ｪ繧九ヵ繧｡繧､繝ｫ繧剃ｽ懈�
  !-----------------------------------------------------------------
  call HistoryCreate(file='xhistcopy/xhistcopy1.nc', &
    & title='gtool_history HistoryCopyVariable test 1', &
    & source='gtool5/Fortran library test kit', &
    & institution='GFD Dennou Club', &
    & dims=(/'x','y','z','t'/), &
    & dimsizes=(/nx,ny,nz,0/), &
    & longnames=(/'X-coordinate','Y-coordinate', &
    &             'Z-coordinate','time        '/), &
    & units=(/'m','m','m','s'/), &
    & origin=0.0, interval=0.2, &
    & xtypes=(/'real  ','double','double','real  '/))

  call HistoryPut('x',x)   ! 谺｡蜈�､画焚蜃ｺ蜉�
  call HistoryPut('y',y)   ! 谺｡蜈�､画焚蜃ｺ蜉�
  call HistoryPut('z',z)   ! 谺｡蜈�､画焚蜃ｺ蜉�

  !----- 螟画焚螳夂ｾｩ -----
  call HistoryAddVariable('u', dims=(/'t'/), &
    & longname='Non Dimensional any quantity', &
    & units='non-dimensional')
  call HistoryAddVariable('v', dims=(/'x','t'/), &
    & longname='1 Dimensional any quantity', &
    & units='1-dimensional any unit', xtype='double')
  call HistoryAddVariable('w', dims=(/'x','y','t'/), &
    & longname='2 Dimensional any quantity', &
    & units='2-dimensional any unit')
  call HistoryAddVariable('q', dims=(/'x','y','z','t'/), &
    & longname='3 Dimensional any quantity', &
    & units='3-dimensional any unit', xtype='double')

  !----- 螟画焚螻樊ｧ莉伜刈 -----
  ! 蜷�､画焚縺ｸ縺ｮ螻樊ｧ莉伜刈
  call HistoryAddAttr('u', 'scale_factor',  100.0)
  call HistoryAddAttr('v', 'add_offset',    1000.0)
  call HistoryAddAttr('w', 'missing_value', -2.0e20)
  call HistoryAddAttr('q', 'valid_range', (/-1.0e30, 1.0e30/))

  !----- 謨ｰ蛟､莉｣蜈･ -----
  do, l = 1, time_step
    u = real(l)*0.01
    do i = 1, nx
      v(i) = dble(l)*0.01d0+dble(i)*1.0d0
      do j = 1, ny
        w(i,j) = real(l)*0.01+real(i)*1.0+real(j)*100.0
        do k = 1, nz
          q(i,j,k) = dble(l)*0.01d0 + dble(i)*1.0d0 &
            & + dble(j)*100.0d0+dble(k)*10000.0d0
        enddo
      enddo
    enddo
    ! 譎る俣繧呈�遉ｺ逧�↓荳弱∴繧句�ｴ蜷�
    call HistoryPut('t', real(l)*0.2)
    call HistoryPut('u', u)
    call HistoryPut('v', v)
    call HistoryPut('w', w)
    call HistoryPut('q', q)
  enddo
  call HistoryClose

  !-----------------------------------------------------------------
  !  谺｡蜈��隱崎ｭ倥ユ繧ｹ繝�
  !    1) HistoryCreate 縺ｯ蝓ｺ譛ｬ逧�↓荳翫→蜷梧ｧ�
  !    2) 螟画峩轤ｹ縺ｯ莉･荳九�2縺､
  !       a) 譎る俣縺ｮ谺｡蜈�､画焚 "t" 繧� "time" 縺ｫ
  !       b) 驩帷峩蠎ｧ讓吶�谺｡蜈�､画焚 "z" 繧� "Z" 縺ｫ
  !    縺薙ｌ繧峨�蟾ｮ逡ｰ縺ｯ縲？istoryCopyVariable 縺ｧ縺ｯ辟｡隕悶＆繧後ｋ莉墓ｧ倥↓
  !    縺ｪ縺｣縺ｦ縺�ｋ縲�
  !-----------------------------------------------------------------

  call HistoryCreate(file='xhistcopy/xhistcopy2.nc', &
    & title='gtool_history HistoryCopyVariable test 1', &
    & source='gtool5/Fortran library test kit', &
    & institution='GFD Dennou Club', &
    & dims=(/'x   ','y   ','Z   ','time'/), & ! 繧上＊縺ｨ t 繧� time 縺ｫ螟画峩
    & dimsizes=(/nx,ny,nz,0/), &
    & longnames=(/'X-coordinate','Y-coordinate', &
    &             'Z-coordinate','time        '/), &
    & units=(/'m','m','m','s'/), &
    & origin=0.0, interval=0.2, &
    & xtypes=(/'real  ','double','double','real  '/))

  call HistoryPut('x',x)   ! 谺｡蜈�､画焚蜃ｺ蜉�
  call HistoryPut('y',y)   ! 谺｡蜈�､画焚蜃ｺ蜉�
  call HistoryPut('Z',z)   ! 谺｡蜈�､画焚蜃ｺ蜉�

  !----- 螟画焚繧ｳ繝斐� -----
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'u')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'v')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'w')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'q')

  !----- 蜈�ヵ繧｡繧､繝ｫ縺ｮ譎ゅ→蜷梧ｧ倥↑險育ｮ励Ν繝ｼ繝� -----
  do, l = 1, time_step
    u = real(l)*0.01
    do i = 1, nx
      v(i) = dble(l)*0.01d0+dble(i)*1.0d0
      do j = 1, ny
        w(i,j) = real(l)*0.01+real(i)*1.0+real(j)*100.0
        do k = 1, nz
          q(i,j,k) = dble(l)*0.01d0 + dble(i)*1.0d0 &
            & + dble(j)*100.0d0+dble(k)*10000.0d0
        enddo
      enddo
    enddo
    ! 譎る俣繧呈�遉ｺ逧�↓荳弱∴繧句�ｴ蜷�
    call HistoryPut('time', real(l)*0.2)
    call HistoryPut('u', u)
    call HistoryPut('v', v)
    call HistoryPut('w', w)
    call HistoryPut('q', q)
  enddo

  call HistoryClose

  !-----------------------------------------------------------------
  !  谺｡蜈��閾ｪ蜍慕函謌舌ユ繧ｹ繝�
  !    1) HistoryCreate 縺ｧ謖�ｮ壹☆繧区ｬ｡蜈��譎る俣縺ｮ縺ｿ縲�
  !       遨ｺ髢捺ｬ｡蜈��螳夂ｾｩ縺ｨ蛟､縺ｮ莉｣蜈･縺ｯ HistoryCopyVariable 縺ｧ螳夂ｾｩ
  !       縺輔ｌ繧句､画焚縺ｫ蠢懊§縺ｦ陦後↑繧上ｌ繧九�
  !    2) 遨ｺ髢捺ｬ｡蜈��縺溘ａ縺ｮ HistoryPut 縺ｯ陦後↑繧上↑縺��
  !       HistoryCopyVariable 縺ｧ閾ｪ蜍慕噪縺ｫ繧ｳ繝斐�縺輔ｌ繧九�
  !    3) 譎る俣谺｡蜈��縺溘ａ縺ｮ HistoryPut 縺ｯ陦後↑繧上↑縺��
  !       螟画焚縺ｮ HistoryPut 縺ｫ蠢懊§縺ｦ閾ｪ蜍慕噪縺ｫ菴懈�縺輔ｌ繧九�
  !-----------------------------------------------------------------

  call HistoryCreate(file='xhistcopy/xhistcopy3.nc', &
    & title='gtool_history HistoryCopyVariable test 1', &
    & source='gtool5/Fortran library test kit', &
    & institution='GFD Dennou Club', &
    & dims=(/'t'/), &
    & dimsizes=(/0/), &
    & longnames=(/'time'/), &
    & units=(/'s'/), &
    & origin=0.0, interval=0.2, &
    & xtypes=(/'real'/))

  !----- 螟画焚繧ｳ繝斐� -----
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'u')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'v')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'w')
  call HistoryCopyVariable('xhistcopy/xhistcopy1.nc', 'q')

  !----- 蜈�ヵ繧｡繧､繝ｫ縺ｮ譎ゅ→蜷梧ｧ倥↑險育ｮ励Ν繝ｼ繝� -----
  do, l = 1, time_step
    u = real(l)*0.01
    do i = 1, nx
      v(i) = dble(l)*0.01d0+dble(i)*1.0d0
      do j = 1, ny
        w(i,j) = real(l)*0.01+real(i)*1.0+real(j)*100.0
        do k = 1, nz
          q(i,j,k) = dble(l)*0.01d0 + dble(i)*1.0d0 &
            & + dble(j)*100.0d0+dble(k)*10000.0d0
        enddo
      enddo
    enddo
    ! 譎る俣繧呈�遉ｺ逧�↓縺ｯ荳弱∴縺ｪ縺�
    !   HistoryCreate 縺ｮ origin 縺ｨ interval 縺ｫ萓晏ｭ�
    call HistoryPut('u', u)
    call HistoryPut('v', v)
    call HistoryPut('w', w)
    call HistoryPut('q', q)
  enddo

  call HistoryClose

end program histcopy