!20140604
!20140611

program flux_vertical

  use gtool_history
  implicit none

  integer :: k, kk, kkk
  real(8) :: r_Press(0:32)
  real(8) :: r_Temp(0:32)
  real(8) :: WaveNum(1:300000)
  real(8) :: r_FluxUp(0:32, 1:300000)
  real(8) :: r_FluxDn(0:32, 1:300000)
  real(8) :: r_Flux  (0:32, 1:300000)
  real(8) :: r_FluxUp_1cm(0:32, 1:3000)
  real(8) :: r_FluxDn_1cm(0:32, 1:3000)
  real(8) :: r_Flux_1cm  (0:32, 1:3000)
  integer :: fom = 10
  integer :: foc = 11
  integer :: LayNum
  character( 4) :: RunCase
  character(10) :: charFlux
  integer :: SelectFlux
  character(10) :: DataBase
  character( 3) :: charData

  DataBase = 'HITRAN2008'
  RunCase = 'C55x'
  !SelectFlux = 1

  write(charData, '(A1, A2)') 'H', DataBase(9:10)

  call HistoryGet('./../output_Flux/Flux_ICRCCM_'//charData//'_'//RunCase//'.nc', 'p', r_Press)
  call HistoryGet('./../output_Flux/Flux_ICRCCM_'//charData//'_'//RunCase//'.nc', 'wn', WaveNum)
  call HistoryGet('./../output_Flux/Flux_ICRCCM_'//charData//'_'//RunCase//'.nc', 'temp', r_Temp)
  call HistoryGet('./../output_Flux/Flux_ICRCCM_'//charData//'_'//RunCase//'.nc', 'FluxUp', r_FluxUp)
  call HistoryGet('./../output_Flux/Flux_ICRCCM_'//charData//'_'//RunCase//'.nc', 'FluxDn', r_FluxDn)

do kkk = 1, 4

  SelectFlux = kkk  

  if(SelectFlux == 1) then
    LayNum = 32
    charFlux ='FluxUp_TOA'
    r_Flux = r_FluxUp
  elseif(SelectFlux == 2) then
    LayNum = 0
    charFlux ='FluxDn_SRF'
    r_Flux = r_FluxDn
  elseif(SelectFlux == 3) then
    LayNum = 13
    charFlux ='FluxUp_TPP'
    r_Flux = r_FluxUp
  elseif(SelectFlux == 4) then
    LayNum = 13
    charFlux ='FluxDn_TPP'
    r_Flux = r_FluxDn
  endif

  print *, r_Press(LayNum)

  open(fom, file='Flux_ICRCCM_'//charData//'_'//RunCase//'_'//charFlux//'')
  write(fom, *) '# Flux_ICRCCM_'//charData//'_'//RunCase//','//charFlux//', wavenumber:1-300000[m-1]'
  write(fom, *) '# flux [W m-2 m]'

  do k = 1, 300000
    write(fom, *) r_Flux(LayNum, k)
  end do

  close(fom)

  r_Flux_1cm(:,:) = 0.0_8 !20140611
  open(foc, file='Flux_ICRCCM_'//charData//'_'//RunCase//'_'//charFlux//'_1cm-1')
  write(foc, *) '# Flux_ICRCCM_'//charData//'_'//RunCase//','//charFlux//', wavenumber:1-3000[cm-1]'
  write(foc, *) '# flux [W m-2 cm]'

  do k = 1, 3000
    do kk = 1, 100
      r_Flux_1cm(LayNum, k) = r_Flux_1cm(LayNum, k) + r_Flux(LayNum, kk+(k-1)*100)
    end do
    write(foc, *) r_Flux_1cm(LayNum, k)
  end do

  close(foc)

end do

end program flux_vertical

