!== 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" ã«
  !    ã“ã‚Œã‚‰ã®å·®ç•°ã¯ã€HistoryCopyVariable ã§ã¯ç„¡è¦–ã•ã‚Œã‚‹ä»•æ§˜ã«
  !    ãªã£ã¦ã„ã‚‹ã€‚
  !-----------------------------------------------------------------

  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