!= Module FileSet_3d
!
! Authors::   SUGIYAMA Ko-ichiro, ODAKA Masatsugu
! Version::   $Id: fileset_3d.f90,v 1.1 2007/08/03 06:54:22 odakker Exp $ 
! Tag Name::  $Name: arare4-20071012 $
! Copyright:: Copyright (C) GFD Dennou Club, 2007. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]
!
!== Overview 
!
!引数に与えられた NAMELIST ファイルから, ファイル名や表題等の情報を取得し
!保管するための変数型モジュール
!
!== Error Handling
!
!== Known Bugs
!
!== Note
!
!== Future Plans
!

module fileset_3d
  !
  !引数に与えられた NAMELIST ファイルから, I/O ファイル名を取得し, 
  !保管するための変数型モジュール
  !

  !モジュール読み込み
  use gt4_history, only: gt_history
  use gridset_3d,     only: SpcNum       !凝縮成分の数
  use basicset_3d,    only: SpcWetSymbol !湿潤成分の化学種名

  !暗黙の型宣言禁止
  implicit none

  !save 属性
  save

  !公開変数
  character(80) :: InitFile          !初期値ファイル
  character(80) :: HistoryFilePrefix !ヒストリーファイル接頭詞
  character(80) :: ReStartFile       !リスタートファイル
  character(80) :: RandomFile        !乱数ファイル
  character(80) :: ExpTitle          !データの表題
  character(80) :: ExpSrc            !データを作成する手順
  character(80) :: ExpInst           !最終変更者・組織

  character(len=100), allocatable :: HistoryFile(:) !出力ファイル名
  integer                         :: FileNum        !出力ファイル数
  type(gt_history),allocatable    :: gt_hist(:)     !gt_history 型構造体

contains

  subroutine fileset_init(cfgfile)
    !
    !NAMELIST からファイル名に付けるタグを得て, 出力ファイル名を作成する. 
    !

    !モジュール読み込み
    use dc_message,    only: MessageNotify

    !暗黙の型宣言禁止
    implicit none

    !入力変数
    character(*), intent(in) :: cfgfile
    integer                  :: s        !ループ添字

    !NAMELIST から情報を取得
    NAMELIST /fileset/                                  &
      & InitFile, HistoryFilePrefix, ReStartFile, RandomFile, &
      & ExpTitle, ExpSrc           , ExpInst

    open (10, FILE=cfgfile)
    read(10, NML=fileset)
    close(10)

    !確認
    call MessageNotify( "M", &
      & "fileset_init", "InitFile=%c",    c1=trim(InitFile))
    call MessageNotify( "M", &
      & "fileset_init", "HistoryFilePrefix=%c", c1=trim(HistoryFilePrefix) )
    call MessageNotify( "M", &
      & "fileset_init", "ReStartFile=%c", c1=trim(ReStartFile) )
    call MessageNotify( "M", &
      & "fileset_init", "RandomFile=%c",  c1=trim(RandomFile) )
    call MessageNotify( "M", &
      & "fileset_init", "ExpTitle=%c",    c1=trim(ExpTitle) )
    call MessageNotify( "M", &
      & "fileset_init", "ExpSrc=%c",      c1=trim(ExpSrc) )
    call MessageNotify( "M", &
      & "fileset_init", "ExpInst=%c",     c1=trim(ExpInst) )


    !ファイル名を格納する配列の割り付け
    !  次元数は
    !    PotTemp, Exner, VelX, VelY, VelZ, MixRt(SpcNum), Km, Kh
    !    *BasicZ, *Zprof
    !  の合計 9 + SpcNum
    
    FileNum = 9 + SpcNum
    allocate(HistoryFile(FileNum))
    allocate(gt_hist(FileNum))

    HistoryFile(1) = trim(HistoryFilePrefix)//"_Exner.nc" 
    HistoryFile(2) = trim(HistoryFilePrefix)//"_PotTemp.nc" 
    HistoryFile(3) = trim(HistoryFilePrefix)//"_VelX.nc" 
    HistoryFile(4) = trim(HistoryFilePrefix)//"_VelY.nc" 
    HistoryFile(5) = trim(HistoryFilePrefix)//"_VelZ.nc" 
    HistoryFile(6) = trim(HistoryFilePrefix)//"_Km.nc" 
    HistoryFile(7) = trim(HistoryFilePrefix)//"_Kh.nc" 
    HistoryFile(8) = trim(HistoryFilePrefix)//"_BasicZ.nc" 
    HistoryFile(9) = trim(HistoryFilePrefix)//"_Zprof.nc" 

    do s = 1, SpcNum
      HistoryFile(9+s) = trim(HistoryFilePrefix)//"_"//trim(SpcWetSymbol(s))//".nc"
    end do


    !確認
    do s = 1, Filenum
      call MessageNotify( "M", &
        & "fileset_init", "HistoryFile=%c", c1=trim(HistoryFile(s)) )
    end do
    
  end subroutine fileset_init
end module fileset_3d
