!--
! *** Caution!! ***
! 
! This file is generated from "historyautoclose.rb2f90" by Ruby 1.8.5.
! Please do not edit this file directly.
!
! [JAPANESE]
!
! ※※※ 注意!!! ※※※
!
! このファイルは "historyautoclose.rb2f90" から Ruby 1.8.5
! によって自動生成されたファイルです.
! このファイルを直接編集しませんようお願い致します.
!
!
!++
!
!= gtool_historyauto の終了処理
!= Termination of gtool_historyauto
!
! Authors::   Yasuhiro MORIKAWA
! Version::   $Id: historyautoclose.f90,v 1.3 2009-10-11 03:55:57 morikawa Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2008-2009. All rights reserved. 
! License::   See COPYRIGHT[link:../../../COPYRIGHT]
!
  subroutine HistoryAutoClose1
    !
    ! HistoryAutoCreate で始まったデータ出力の終了処理を行います. 
    ! プログラムを終了する前に必ずこのサブルーチンを呼んでください. 
    ! 
    ! Terminates data output with "HistoryAutoCreate". 
    ! Call this subroutine certainly before a progrem is finished. 
    ! 
    use gtool_historyauto_internal, only: initialized, numdims, &
      & gthstnml, gthst_axes, data_axes, numwgts, gthst_weights, data_weights, &
      & numvars, gthst_vars, slice_vars, weight_vars, varname_vars, &
      & slice_vars, space_avr_vars, &
      & output_valid_vars, create_timing_vars, &
      & close_timing_vars, renew_timing_vars, &
      & output_timing_vars, output_timing_avr_vars, tavr_vars, &
      & interval_time_vars, prev_outtime_vars, &
      & origin_time_vars, terminus_time_vars, &
      & histaddvar_vars, newfile_inttime_vars, newfile_createtime_vars, &
      & checked_tstepnum, checked_tstep_varnum, saved_tstep, &
      & save_mpi_split, save_mpi_gather, flag_allvarfixed, all_output_save, &
      & time_unit_bycreate, time_unit_suffix
    use gtool_history_nmlinfo_generic, only: HstNmlInfoClose, HstNmlInfoGetNames, &
      & HstNmlInfoAssocGtHist, HstNmlInfoPutLine
    use gtool_history, only: GT_HISTORY, HistoryClose, HistoryInitialized, &
      & HistoryAxisClear, HistoryVarinfoClear
    use dc_trace, only: BeginSub, EndSub
    use dc_string, only: PutLine, Printf, Split, StrInclude, StoA, JoinChar
    use dc_error, only: StoreError, DC_NOERR, DC_ENOTINIT
    use dc_date, only: DCDiffTimeCreate
    use dc_date_types, only: DC_DIFFTIME, DC_DATETIME
    use dc_types, only: DP, STRING, TOKEN
    implicit none

    ! ヒストリファイルへのデータ出力設定
    ! Configure the settings for history data output
    !
    character(STRING):: name = ''
                              ! 変数名. Variable identifier
    character(TOKEN), pointer:: varnames_array(:) =>null()
                              ! 変数名リスト配列. 
                              ! List of variables (array) 
    integer:: i, vnmax
    type(GT_HISTORY), pointer:: gthist =>null()
                              ! gtool_history モジュール用構造体. 
                              ! Derived type for "gtool_history" module

    ! 作業変数
    ! Work variables
    !
    integer:: stat
    character(STRING):: cause_c
    character(*), parameter:: subname = 'HistoryAutoClose1'
  continue
    call BeginSub( subname )
    stat = DC_NOERR
    cause_c = ''

    ! 初期設定チェック
    ! Check initialization
    !
    if ( .not. initialized ) then
      stat = DC_ENOTINIT
      cause_c = 'gtool_historyauto'
      goto 999
    end if

    ! ヒストリファイルへのデータ出力の終了処理
    ! Terminate the settings for history data output
    !
    call HstNmlInfoGetNames( gthstnml, & ! (in)
      & varnames_array )                 ! (out)
    vnmax = size( varnames_array )

    do i = 1, vnmax
      name = varnames_array(i)
      if ( trim( name ) == '' ) exit
      nullify( gthist )
      call HstNmlInfoAssocGtHist( & 
        & gthstnml = gthstnml, & ! (in)
        & name = name, &         ! (in)
        & history = gthist )     ! (out)
      if ( HistoryInitialized( gthist ) ) then
        call HistoryClose( history = gthist ) ! (inout)
      end if
    end do

    ! ヒストリファイルへのデータ出力設定の割付解除
    ! Deallocate the settings for history data output
    !
    call HstNmlInfoClose( gthstnml ) ! (inout)

    ! 座標軸情報のクリア
    ! Create axes information
    !
    do i = 1, numdims
      call HistoryAxisClear( gthst_axes(i) )
      deallocate( data_axes(i) % a_axis )
    end do
    numdims = 0

    ! 座標重み情報のクリア
    ! Create axes weights information
    !
    do i = 1, numwgts
      call HistoryVarinfoClear( gthst_weights(i) )
      deallocate( data_weights(i) % a_axis )
    end do
    numwgts = 0

    ! 変数情報のクリア
    ! Create variables information
    !
    do i = 1, numvars
      call HistoryVarinfoClear( gthst_vars(i) )

      if ( associated( slice_vars(i) % st ) ) deallocate( slice_vars(i) % st )
      if ( associated( slice_vars(i) % ed ) ) deallocate( slice_vars(i) % ed )
      if ( associated( slice_vars(i) % sd ) ) deallocate( slice_vars(i) % sd )

                          if ( associated( weight_vars(i) % wgt1 ) ) deallocate( weight_vars(i) % wgt1 )
                    
      if ( associated( weight_vars(i) % wgt2 ) ) deallocate( weight_vars(i) % wgt2 )
                    
      if ( associated( weight_vars(i) % wgt3 ) ) deallocate( weight_vars(i) % wgt3 )
                    
      if ( associated( weight_vars(i) % wgt4 ) ) deallocate( weight_vars(i) % wgt4 )
                    
      if ( associated( weight_vars(i) % wgt5 ) ) deallocate( weight_vars(i) % wgt5 )
                    
      if ( associated( weight_vars(i) % wgt6 ) ) deallocate( weight_vars(i) % wgt6 )
                    
      if ( associated( weight_vars(i) % wgt7 ) ) deallocate( weight_vars(i) % wgt7 )
                    

      if ( associated( space_avr_vars(i) % avr ) ) deallocate( space_avr_vars(i) % avr )

      varname_vars(i)             = ''
      output_valid_vars(i)        = .false.
      create_timing_vars(i,:)     = .false.
      close_timing_vars(i,:)      = .false.
      renew_timing_vars(i,:)      = .false.
      output_timing_vars(i,:)     = .false.
      output_timing_avr_vars(i,:) = .false.
      tavr_vars(i)                = .false.
      interval_time_vars(i)       = 0.0_DP
      prev_outtime_vars(i)        = 0.0_DP
      origin_time_vars(i)         = 0.0_DP
      terminus_time_vars(i)       = -1.0_DP
      histaddvar_vars(i)          = .false.
      newfile_inttime_vars(i)     = -1.0_DP
      newfile_createtime_vars(i)  = 0.0_DP

!!$      ! dc_date 使用版
!!$
!!$      call DCDiffTimeCreate( interval_time_vars(i), &   ! (out)
!!$        & sec = 0.0_DP )                                ! (in)
!!$      call DCDiffTimeCreate( prev_outtime_vars(i), &    ! (out)
!!$        & sec = 0.0_DP )                                ! (in)
!!$      call DCDiffTimeCreate( origin_time_vars(i), &   ! (out)
!!$        & sec = 0.0_DP )                              ! (in)
!!$      call DCDiffTimeCreate( terminus_time_vars(i), & ! (out)
!!$        & sec = -1.0_DP )                             ! (in)
!!$      histaddvar_vars(i)      = .false.
!!$      call DCDiffTimeCreate( newfile_inttime_vars(i), &    ! (out)
!!$        & sec = -1.0_DP )                                  ! (in)
!!$      call DCDiffTimeCreate( newfile_createtime_vars(i), & ! (out)
!!$        & sec = 0.0_DP )                                   ! (in)

    end do
    numvars = 0

    ! 時間ステップに関する情報のクリア
    ! Clear information about time steps
    !
    checked_tstepnum = 0
    checked_tstep_varnum = 0
    saved_tstep = 1

    ! MPI に関する情報のクリア
    ! Clear information about MPI
    !
    save_mpi_split  = .false.
    save_mpi_gather = .false.

    ! 終了処理, 例外処理 
    ! Termination and Exception handling
    !
    initialized = .false.
    flag_allvarfixed = .false.
    all_output_save = .false.
    time_unit_bycreate = ''
    time_unit_suffix = ''

999 continue
    call StoreError( stat, subname, cause_c = cause_c )
    call EndSub( subname )
  end subroutine HistoryAutoClose1

!--
! vi:set readonly sw=4 ts=8:
!
!Local Variables:
!mode: f90
!buffer-read-only: t
!End:
!
!++
