!-----------------------------------------------------------------
!  データ形式変換プログラム
!
!  履歴: 2012/04/10 小高正嗣
!-----------------------------------------------------------------
program DataToNetCDF

  use gtool_history 

  integer, parameter :: nz = 205
  integer, parameter :: nzb = 207  ! nz  + 2
  integer, parameter :: nzg = 10
  real(8), parameter :: hour = 3600.0d0

  real(8), dimension(-2:nzb) ::      &
    &  tpot0, temp0, cdturb, zgrid,  &
    &  pres0, dens0, ppai0,  qvap0,  &
    &  rvap0, qrad, qdif, srad, dirh, dsrh

  real(8), dimension(nz) ::  &
    &  tpot, temp, kturb, z,      &
    &  dptdif, dptirad, dptsrad, &
    &  dptqirad, dptqsrad, dptrad

  real(8), dimension(nzg) :: gz, tgrd
 
  real(8) :: tsfc, fheat

  integer :: itw, irun, nloop1, nloop2, it, iz
  real(8) :: time0, dtime, times, ylat, als, time
  real(8) :: tinit, dt
  character(LEN=100) :: model
  
  type(GT_HISTORY) :: hst_vprof, hst_qprof, hst_tsfc, hst_tgrd

!----------------------------------------------------------------------------
  open(10,file='exparam')
  read(10,*) irun
  read(10,*) time0, nloop1, nloop2, dtime, times
  read(10,*) ylat, als
  read(10,*) model
  close(10)    

  dt = dtime * nloop2 / hour
  tinit = times/hour + dt

  write(*,*) time0, nloop1, nloop2, dtime, times
  write(*,*) dt

  call FOPEN ( irun+1 )

  do iz = -2, nzb
    READ(116,*) itw, zgrid(iz), pres0(iz), dens0(iz), temp0(iz), &
      &             tpot0(iz), ppai0(iz), qvap0(iz), rvap0(iz)  
  end do

  z = zgrid(0:nz-1)

  gz = (/ -4.0D0 , -2.7D0 , -1.8D0, -1.2D0, -0.79D0, &
    & -0.53D0, -0.35D0, -0.2D0, -0.1D0, 0.0D0 /)


  call output_gtool5_init

  do it = 1, NLOOP1
    read(31) time, ( tpot0(iz), iz=-2,nzb )
    read(32) time, ( temp0(iz), iz=-2,nzb )
    read(33) time, ( cdturb(iz),iz=-2,nzb )
    read(34) time, ( qrad(iz),  iz=-2,nzb )
    read(35) time, ( qdif(iz),  iz=-2,nzb )
    read(36) time, ( tgrd(iz),  iz= 1,nzg )
    read(37) time, ( srad(iz),  iz=-2,nzb )
    read(38) time, ( dirh(iz),  iz=-2,nzb )
    read(39) time, ( dsrh(iz),  iz=-2,nzb )

    temp  = temp0(0:nz-1)
    tpot  = tpot0(0:nz-1)
    kturb = cdturb(0:nz-1)

    call HistoryPut( 'tgrd',  tgrd , hst_tgrd  )
    call HistoryPut( 'temp',  temp , hst_vprof )
    call HistoryPut( 'tpot',  tpot , hst_vprof )
    call HistoryPut( 'kturb', kturb, hst_vprof )

    dptdif   = qdif(0:nz-1)
    dptirad  = qrad(0:nz-1)
    dptsrad  = srad(0:nz-1)
    dptqirad = dirh(0:nz-1)
    dptqsrad = dsrh(0:nz-1)
    dptrad   = dptirad + dptsrad + dptqirad + dptqsrad

    call HistoryPut( 'dptdif'  , dptdif  , hst_qprof )
    call HistoryPut( 'dptirad' , dptirad , hst_qprof )
    call HistoryPut( 'dptsrad' , dptsrad , hst_qprof )
    call HistoryPut( 'dptqirad', dptqirad, hst_qprof )
    call HistoryPut( 'dptqsrad', dptqsrad, hst_qprof )
    call HistoryPut( 'dptrad'  , dptrad  , hst_qprof )

  end do

  do it = 1, NLOOP1
    read(72) time, tsfc, fheat
    call HistoryPut( 'tsfc',  tsfc , hst_tsfc)
    call HistoryPut( 'fheat', fheat, hst_tsfc)
  end do


  call HistoryClose(hst_vprof)
  call HistoryClose(hst_qprof)
  call HistoryClose(hst_tsfc)
  call HistoryClose(hst_tgrd)

  call FCLOSE

  contains
!-----------------------------------------------------------------
    SUBROUTINE FOPEN ( IRUN )

      CHARACTER(len=20) :: FNAME(100)
      CHARACTER(len=36) :: CNUM
      CHARACTER(len=4)  :: FNIN
      CHARACTER(len=2)  :: SNAME

      CNUM = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'

      SNAME = 'T1'
      FNIN  = '@' // SNAME // CNUM(IRUN  :IRUN  )

      FNAME(31) = FNIN // '.TPOT.DATA'
      FNAME(32) = FNIN // '.TEMP.DATA'
      FNAME(33) = FNIN // '.TURB.DATA'
      FNAME(34) = FNIN // '.QRAD.DATA'
      FNAME(35) = FNIN // '.QDIF.DATA'
      FNAME(36) = FNIN // '.TGRD.DATA'
      FNAME(37) = FNIN // '.SRAD.DATA'
      FNAME(38) = FNIN // '.DIRH.DATA'
      FNAME(39) = FNIN // '.DSRH.DATA'

      DO IR = 31, 39
         OPEN ( IR, FILE = FNAME(IR), STATUS = 'OLD' ,&
     &           FORM='UNFORMATTED' )
      END DO

      FNAME(16) = FNIN // '.VB.DATA'
      OPEN ( 116, FILE = FNAME(16), STATUS='OLD'  )

      FNAME(72) = FNIN // '.TSFC.DATA'
      OPEN ( 72, FILE = FNAME(72), STATUS='OLD', FORM='UNFORMATTED' )

      RETURN

    END SUBROUTINE FOPEN
!-----------------------------------------------------------------
    SUBROUTINE FCLOSE

      CLOSE (31)
      CLOSE (32)
      CLOSE (33)
      CLOSE (34)
      CLOSE (35)
      CLOSE (36)
      CLOSE (37)
      CLOSE (38)
      CLOSE (39)

      CLOSE(72)

      CLOSE(16)

      RETURN

    END SUBROUTINE FCLOSE
!-----------------------------------------------------------------
    subroutine output_gtool5_init

      call HistoryCreate( & 
        & file='tsfc.nc',  title=trim(model),             &
        & source='1D radiateve-convective model of Mars', &
        & institution='GFD_Dennou Club dcrtm project',    &
        & dims=(/'t'/), dimsizes=(/0/),                   &
        & longnames=(/'time'/), units=(/'hour'/),         &
        & origin=real(tinit), interval=real(dt),          &
        & history=hst_tsfc )
      
      call HistoryCreate( & 
        & file='tgrd.nc',  title=trim(model),             &
        & source='1D radiateve-convective model of Mars', &
        & institution='GFD_Dennou Club dcrtm project',    &
        & dims=(/'z','t'/), dimsizes=(/nzg,0/),           &
        & longnames=(/'Z-coordinate','time        '/),    &
        & units=(/'m   ','hour'/),                        &
        & origin=real(tinit), interval=real(dt),          &
        & history=hst_tgrd )

      call HistoryCreate( & 
        & file='vprof.nc',  title=trim(model),            &
        & source='1D radiateve-convective model of Mars', &
        & institution='GFD_Dennou Club dcrtm project',    &
        & dims=(/'z','t'/), dimsizes=(/nz,0/),            &
        & longnames=(/'Z-coordinate','time        '/),    &
        & units=(/'m   ','hour'/),                        &
        & origin=real(tinit), interval=real(dt),          &
        & history=hst_vprof )

      call HistoryCreate( & 
        & file='qprof.nc',  title=trim(model),            &
        & source='1D radiateve-convective model of Mars', &
        & institution='GFD_Dennou Club dcrtm project',    &
        & dims=(/'z','t'/), dimsizes=(/nz,0/),            &
        & longnames=(/'Z-coordinate','time        '/),    &
        & units=(/'m   ','hour'/),                        &
        & origin=real(tinit), interval=real(dt),          &
        & history=hst_qprof )
        
      call HistoryPut('z', z, hst_vprof)
      call HistoryPut('z', z, hst_qprof)
      call HistoryPut('z', gz, hst_tgrd)

      call HistoryAddVariable( varname='tsfc', dims=(/'t'/), &
        &  longname='surface temperature', units='K', xtype='double', &
        &  history=hst_tsfc )
        
      call HistoryAddVariable( varname='fheat', dims=(/'t'/), &
        &  longname='surface heat flux', units='W/m^2', xtype='double', &
        &  history=hst_tsfc )
        
      call HistoryAddVariable( varname='tgrd', dims=(/'z','t'/), &
        &  longname='groud temperature', units='K', xtype='double', &
        &  history=hst_tgrd )

      call HistoryAddVariable( varname='temp', dims=(/'z','t'/), &
        &  longname='temperature', units='K', xtype='double', &
        &  history=hst_vprof )
        
      call HistoryAddVariable( varname='tpot', dims=(/'z','t'/), &
        &  longname='potential temperature', units='K', xtype='double', &
        &  history=hst_vprof )
        
      call HistoryAddVariable( varname='kturb', dims=(/'z','t'/), &
        &  longname='turbulent diffusion', units='m^2/s', xtype='double', &
        &  history=hst_vprof )
        
      call HistoryAddVariable( varname='dptirad', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by IR radiarion', &
        &  units='K/s', xtype='double', history=hst_qprof )
        
      call HistoryAddVariable( varname='dptsrad', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by solar radiarion',  &
        &  units='K/s', xtype='double', history=hst_qprof )

      call HistoryAddVariable( varname='dptqirad', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by dust IR radiarion',  &
        &  units='K/s', xtype='double', history=hst_qprof )

      call HistoryAddVariable( varname='dptqsrad', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by dust solar radiarion',  &
        &  units='K/s', xtype='double', history=hst_qprof )

      call HistoryAddVariable( varname='dptrad', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by radiarion',  &
        &  units='K/s', xtype='double', history=hst_qprof )

      call HistoryAddVariable( varname='dptdif', dims=(/'z','t'/), &
        &  longname='Potemtnal temp. tendency by diffusion',  &
        &  units='K/s', xtype='double', history=hst_qprof )

    end subroutine output_gtool5_init
!-----------------------------------------------------------------
end program DataToNetCDF
