!= Module HistoryFileIO
!
! Authors::   SUGIYAMA Ko-ichiro
! Version::   $Id: historyfileio.f90,v 1.9 2014/03/04 05:55:04 sugiyama Exp $
! Tag Name::  $Name:  $
! Copyright:: Copyright (C) GFD Dennou Club, 2006. All rights reserved.
! License::   See COPYRIGHT[link:../../COPYRIGHT]

module HistoryFileIO
  !
  !ãƒ•ã‚¡ã‚¤ãƒ«å‡ºåŠ›. é•·ã„æ™‚é–“ã‚¹ãƒ†ãƒƒãƒ—ã®å€¤ã‚’å‡ºåŠ›.
  !

  ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
  ! Kind type parameter
  !
  use dc_types, only: STRING     ! æ–‡å—åˆ—. Strings.

  !æš—é»™ã®åž‹å®£è¨€ç¦æ¢
  implicit none

  !å±žæ€§ã®æŒ‡å®š
  private

  !å…¬é–‹æ‰‹ç¶šã
  public HistoryFileIO_init
  public HistoryFileIO_finalize

  ! éžå…¬é–‹å¤‰æ•°
  ! Private variables
  !
  character(STRING), parameter, private :: module_name = 'historyfileio'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åç§°.
                              ! Module name
  character(STRING), parameter, private :: version = &
    & '$Name:  $' // &
    & '$Id: historyfileio.f90,v 1.9 2014/03/04 05:55:04 sugiyama Exp $'
                              ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®ãƒãƒ¼ã‚¸ãƒ§ãƒ³
                              ! Module version

contains

!!!------------------------------------------------------------------------
  subroutine HistoryFileIO_init
    !
    ! history_file_io ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«ã®åˆæœŸåŒ–ã‚’è¡Œã„ã¾ã™.
    !--
    ! NAMELIST#history_file_io_nml ã®èªã¿è¾¼ã¿ã¯ã“ã®æ‰‹ç¶šãã§è¡Œã‚ã‚Œã¾ã™.
    !++
    !
    ! "history_file_io" module is initialized.
    !--
    ! "NAMELIST#history_file_io_nml" is loaded in this procedure.
    !++
    !

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !

    ! gtool5 netCDF ãƒ‡ãƒ¼ã‚¿ã®å…¥å‡ºåŠ›ã‚¤ãƒ³ã‚¿ãƒ¼ãƒ•ã‚§ãƒ¼ã‚¹ (å¤§è¦æ¨¡ãƒ¢ãƒ‡ãƒ«ç”¨)
    ! Interface of Input/Output of gtool5 netCDF data (For large models)
    !
    use gtool_historyauto, only: HistoryAutoCreate,  &
      &                          HistoryAutoAddAttr, &
      &                          HistoryAutoPutAxis, &
      &                          HistoryAutoAddVariable

    ! ãƒ•ã‚¡ã‚¤ãƒ«å…¥å‡ºåŠ›è£œåŠ©
    ! File I/O support
    !
    use dc_iounit, only: FileOpen

    ! ç¨®åˆ¥åž‹ãƒ‘ãƒ©ãƒ¡ã‚¿
    ! Kind type parameter
    !
    use dc_types,      only: DP, &              ! å€ç²¾åº¦å®Ÿæ•°åž‹. Double precision.
      &                      STRING             ! æ–‡å—åˆ—.       Strings.
    use mpi_wrapper,   only: FLAG_LIB_MPI
    use namelist_util, only: namelist_filename
    use axesset,       only: x_X, y_Y, z_Z
    use gridset,       only: nx, ny, nz, ncmax
    use fileset,       only: filetitle,        &!ãƒ‡ãƒ¼ã‚¿ã®è¡¨é¡Œ
      &                      filesource,       &!ãƒ‡ãƒ¼ã‚¿ã‚’ä½œæˆã™ã‚‹æ‰‹é †
      &                      FileInstitution    !æœ€çµ‚å¤‰æ›´è€…ãƒ»çµ„ç¹”
    use timeset,       only: Restarttime, IntegPeriod
    use composition,   only: SpcWetSymbol, GasNum

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none

    !å¤‰æ•°å®šç¾©
    real(DP), parameter :: TimeDisp = 1.0e5 !å‡ºåŠ›é–“éš”ã®ãƒ‡ãƒ•ã‚©ãƒ«ãƒˆå€¤
    real(DP) :: EndTime
    integer  :: l, s

    EndTime = RestartTime + IntegPeriod

    !-----------------------------------------------------------
    ! ãƒ’ã‚¹ãƒˆãƒªãƒ¼ä½œæˆ
    !-----------------------------------------------------------
    call HistoryAutoCreate(                             &
      & title = FileTitle,                              &
      & source = FileSource,                            &
      & institution = FileInstitution,                  &
      & dims=(/'x','y','z','t'/),                       &
      & dimsizes=(/nx, ny, nz, 0/),                     &
      & longnames=(/'x-coordinate',                     &
      &             'y-coordinate',                     &
      &             'z-coordinate',                     &
      &             'time        '/),                   &
      & units=(/'m  ','m  ','m  ','sec'/),              &
      & xtypes=(/'double','double','double','double'/), &
      & origin   = Restarttime,                         &
      & terminus = EndTime,                             &
      & interval = TimeDisp,                            &
      & flag_mpi_split = FLAG_LIB_MPI,                  &
      & namelist_filename = namelist_filename)

    call HistoryAutoAddAttr( &
      & varname = 'x', attrname = 'standard_name', &   ! (in)
      & value = 'x-coordinate' )                       ! (in)
    call HistoryAutoAddAttr( &
      & varname = 'y', attrname = 'standard_name', &   ! (in)
      & value = 'y-coordinate' )                       ! (in)
    call HistoryAutoAddAttr( &
      & varname = 'z', attrname = 'standard_name', &   ! (in)
      & value = 'z-coordinate' )                       ! (in)

    call HistoryAutoPutAxis('x', x_X(1:nx))
    call HistoryAutoPutAxis('y', y_Y(1:ny))
    call HistoryAutoPutAxis('z', z_Z(1:nz))

    ! å°å— ; Print
    !
!    call MessageNotify( 'M', module_name, '----- Initialization Messages -----' )
!    call MessageNotify( 'M', module_name, '-- version = %c', c1 = trim(version) )

    !ç„¡æ¬¡å…ƒåœ§åŠ›ã®æ“¾ä¹±
    call HistoryAutoAddVariable(                           &
      & varname='Exner',                                   &
      & dims=(/'x','y','z','t'/),                          &
      & longname='disturbunce of nondimensional pressure', &
      & units=' ',                                         &
      & xtype='double' )

    !ç„¡æ¬¡å…ƒåœ§åŠ›
    call HistoryAutoAddVariable(                           &
      & varname='ExnerAll',                                &
      & dims=(/'x','y','z','t'/),                          &
      & longname='nondimensional pressure',                &
      & units=' ',                                         &
      & xtype='double' )

    call HistoryAutoAddVariable(                           &
      & varname='PTemp',                                   &
      & dims=(/'x','y','z','t'/),                          &
      & longname='disturbunce of potential temperature',   &
      & units='K',                                         &
      & xtype='double' )

    !æ¸©ä½ã®æ“¾ä¹±
    call HistoryAutoAddVariable(                           &
      & varname='PTempAll',                                &
      & dims=(/'x','y','z','t'/),                          &
      & longname='potential temperature',                  &
      & units='K',                                         &
      & xtype='double' )

    !æ°´å¹³é€Ÿåº¦
    call HistoryAutoAddVariable(                           &
      & varname='VelX',                                    &
      & dims=(/'x','y','z','t'/),                          &
      & longname='zonal velocity',                         &
      & units='m.s-1',                                     &
      & xtype='double' )

    !æ°´å¹³é€Ÿåº¦
    call HistoryAutoAddVariable(                           &
      & varname='VelY',                                    &
      & dims=(/'x','y','z','t'/),                          &
      & longname='meridional velocity',                    &
      & units='m.s-1',                                     &
      & xtype='double' )

    !é‰›ç›´é€Ÿåº¦
    call HistoryAutoAddVariable(                           &
      & varname='VelZ',                                    &
      & dims=(/'x','y','z','t'/),                          &
      & longname='vertical velocity',                      &
      & units='m.s-1',                                     &
      & xtype='double' )

    !æ¸¦ç²˜æ€§ä¿‚æ•°(é‹å‹•é‡)
    call HistoryAutoAddVariable(                           &
      & varname='Km',                                      &
      & dims=(/'x','y','z','t'/),                          &
      & longname='turbulet diffusion coefficient',         &
      & units='m2.s-1',                                    &
      & xtype='double' )

    !æ¸¦ç²˜æ€§ä¿‚æ•°(ç†±)
    call HistoryAutoAddVariable(                           &
      & varname='Kh',                                      &
      & dims=(/'x','y','z','t'/),                          &
      & longname='turbulet diffusion coefficient for heat',&
      & units='m2.s-1',                                    &
      & xtype='double')

    !é›²å¯†åº¦
    call HistoryAutoAddVariable(                           &
      & varname='CDens',                                   &
      & dims=(/'x','y','z','t'/),                          &
      & longname='Cloud density',                          &
      & units='kg.m-3',                                    &
      & xtype='double')

    ! æ··åˆæ¯”
    do l = 1, ncmax
      call HistoryAutoAddVariable(                         &
        & varname='D'//trim(SpcWetSymbol(l)),                   &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do

    do l = 1, GasNum
      call HistoryAutoAddVariable(                         &
        & varname='D'//trim(SpcWetSymbol(l))//'All',            &
        & dims=(/'x','y','z','t'/),                        &
        & longname=trim(SpcWetSymbol(l))//' Mixing Ratio', &
        & units='kg.kg-1',                                 &
        & xtype='double')
    end do

    !----------------------------------------------------------------
    ! Mixing Ratio time change
    !----------------------------------------------------------------
    do s = 1, ncmax

      call HistoryAutoAddVariable(                             &
        & varname='D'//trim(SpcWetSymbol(s))//'DtFill1',             &
        & dims=(/'x','y','z','t'/),                            &
        & longname='Filling Negative term 1 of '               &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',                                 &
        & xtype='double' )

      call HistoryAutoAddVariable(                             &
        & varname='D'//trim(SpcWetSymbol(s))//'DtFill2',             &
        & dims=(/'x','y','z','t'/),                            &
        & longname='Filling Negative term 2 of '               &
        &           //trim(SpcWetSymbol(s))//' mixing ratio',  &
        & units='kg.kg-1.s-1',                                 &
        & xtype='double' )

    end do

  end subroutine HistoryFileIO_init

  subroutine HistoryFileIO_finalize
    !
    ! ãƒ’ã‚¹ãƒˆãƒªãƒ‡ãƒ¼ã‚¿ãƒ•ã‚¡ã‚¤ãƒ«å‡ºåŠ›ã®çµ‚äº†å‡¦ç†ã‚’è¡Œã„ã¾ã™.
    !
    ! Terminate history data files output.

    ! ãƒ¢ã‚¸ãƒ¥ãƒ¼ãƒ«å¼•ç”¨ ; USE statements
    !
    use gtool_historyauto, only: HistoryAutoClose

    ! å®£è¨€æ–‡ ; Declaration statements
    !
    implicit none

    ! ä½œæ¥å¤‰æ•°
    ! Work variables
    !

    ! å®Ÿè¡Œæ–‡ ; Executable statement
    !

    call HistoryAutoClose

  end subroutine HistoryFileIO_finalize

end module HistoryFileIO