  program main

    use vtype_module
    use ni3_module
    use fi_module
    use netcdf

    implicit none

    integer         :: NWaveNum
    integer         :: NMolNum
    integer         :: NLev

    real(8), allocatable :: w_WaveNum   (:)
    integer, allocatable :: m_MolNum    (:)
    integer, allocatable :: z_LevNum    (:)
    real(8), allocatable :: m_VMR       (:)
    real(8), allocatable :: m_AbsCoef   (:)
    real(8), allocatable :: m_AbsCoefLen(:)

    real(8)              :: WaveNum
    real(8)              :: Press
    real(8)              :: Temp
    real(8)              :: AbsCoef
    real(8)              :: AbsCoefLen

    character(256)            :: DirIn
    character(256)            :: FileIn
    integer                   :: FUIn
    integer                   :: NLine

    character(256)            :: NcFNOut
    character(256)            :: VarName

    character(256)            :: MolName

    integer                   :: NcIDOut
    character(256)            :: Title
    character(256)            :: Com
    character(256)            :: Name
    character(256)            :: StdName
    character(256)            :: LongName
    character(256)            :: Units

    integer                     :: VarIDAbsCoef
    integer                     :: St3(3)
    integer                     :: Co3(3)

    integer                     :: NDims
    character(256), allocatable :: a_DimNames(:)
    real(8)                     :: MissingValue

    integer                     :: iStatus

    character(256)              :: Line
    integer                     :: ios

    integer                     :: k
    integer                     :: m
    integer                     :: iWaveNum


    NLev    = 4
    DirIn   = "../kspectrum1.3/results"
    NcFNOut = "out/abscoef.nc"
    DirIn   = "./out.dwn1e2"
    NcFNOut = "out/abscoef.dwn1e2.nc"
    DirIn   = "./out.dwn1e1"
    NcFNOut = "out/abscoef.dwn1e1.nc"
    DirIn   = "../kspectrum1.3/results"
    NcFNOut = "out/abscoef.dwn1e0.nc"


    allocate( z_LevNum(NLev) )
    do k = 1, NLev
      z_LevNum(k) = k
    end do

    do k = 1, NLev

       if ( k == 1 ) then
          !
          ! Prepare for output to a NetCDF file
          !
          write( 6, * ) 'Output file : ', trim( NcFNOut )
          call ni3_open( NcFNOut, "new", NcIDOut )
          !
          Title = "kspectrum result"
          Com   = "Results of kspectrum is converted to netcdf."
          call ni3_set_ga( NcIDOut, title = Title, com = Com )
       end if


       !
      ! Prepare for intput from a kXXX file
      !
       write( FileIn, '(a,i3.3)' ) trim( DirIn ) // "/k", k
      NLine = fi_lc( FileIn )
      write( 6, * ) 'Input  file : ', trim( FileIn )
      call fi_open( FileIn, "read", FUIn )
      !
      read( FUIn, * ) Line
      read( FUIn, * ) Press
      Press = Press * 101325.0d0
      !
      read( FUIn, * ) Line
      read( FUIn, * ) Temp
      !
      read( FUIn, * ) Line
      read( FUIn, * ) NMolNum
      if ( k == 1 ) then
        allocate( m_MolNum    ( NMolNum ) )
        allocate( m_VMR       ( NMolNum ) )
        allocate( m_AbsCoef   ( NMolNum ) )
        allocate( m_AbsCoefLen( NMolNum ) )
      end if
      !
      read( FUIn, * )
      !
      do m = 1, NMolNum
        read( FUIn, * ) Line
        read( FUIn, * ) Line
        read( FUIn, * ) MolName
        select case ( MolName )
        case ( "H2O" )
          m_MolNum(m) = 1
        case ( "CO2" )
          m_MolNum(m) = 2
        case default
          write( 6, * ) 'ERROR: ', trim( MolName ), ' is not supported.'
          stop
        end select
        read( FUIn, * ) Line
        read( FUIn, * ) m_VMR(m)
      end do
      !
      read( FUIn, * )
      !
      read( FUIn, * ) Line
      !
      NWaveNum = NLine - ( 7 + NMolNum * 5 + 2 )

      if ( k == 1 ) then
        Name     = "MolNum"
        StdName  = "molecular_number"
        LongName = StdName
        Units    = "1"
        call ni3_set_dim( NcIDOut, Name, NI3_INT, m_MolNum , &
          stdname = StdName, longname = LongName, units  = Units )
        Name     = "WaveNum"
        StdName  = "wavenumber"
        LongName = StdName
        Units    = "m-1"
        call ni3_def_dim( NcIDOut, Name, NI3_DOUBLE, NWaveNum, &
          stdname = StdName, longname = LongName, units = Units )
        !
        Name     = "LevNum"
        StdName  = "level_number"
        LongName = StdName
        Units    = "1"
        call ni3_set_dim( NcIDOut, Name, NI3_INT, z_LevNum , &
          stdname = StdName, longname = LongName, units  = Units )


        NDims = 2
        allocate( a_DimNames( NDims ) )
        a_DimNames( 1 ) = "MolNum"
        a_DimNames( 2 ) = "LevNum"
        !
        Name         = "VMR"
        StdName      = "volume_mixing_ratio"
        LongName     = StdName
        Units        = "1"
        MissingValue = -1.0e10
        call ni3_def_var( NcIDOut, Name, NI3_DOUBLE, NDims, a_DimNames, &
          stdname = StdName, longname = LongName, units = Units, fillvalue = MissingValue )
        !
        deallocate( a_DimNames )

        NDims = 1
        allocate( a_DimNames( NDims ) )
        a_DimNames( 1 ) = "LevNum"
        !
        Name         = "Press"
        StdName      = "pressure"
        LongName     = StdName
        Units        = "Pa"
        MissingValue = -1.0e10
        call ni3_def_var( NcIDOut, Name, NI3_DOUBLE, NDims, a_DimNames, &
          stdname = StdName, longname = LongName, units = Units, fillvalue = MissingValue )
        !
        Name         = "Temp"
        StdName      = "temperature"
        LongName     = StdName
        Units        = "K"
        MissingValue = -1.0e10
        call ni3_def_var( NcIDOut, Name, NI3_DOUBLE, NDims, a_DimNames, &
          stdname = StdName, longname = LongName, units = Units, fillvalue = MissingValue )
        !
        deallocate( a_DimNames )

        NDims = 3
        allocate( a_DimNames( NDims ) )
        a_DimNames( 1 ) = "WaveNum"
        a_DimNames( 2 ) = "MolNum"
        a_DimNames( 3 ) = "LevNum"
        !
        Name         = "AbsCoef"
        StdName      = "absorption_coefficient"
        LongName     = StdName
        Units        = "m2 molecule-1"
        MissingValue = -1.0e10
        call ni3_def_var( NcIDOut, Name, NI3_DOUBLE, NDims, a_DimNames, &
          stdname = StdName, longname = LongName, units = Units, fillvalue = MissingValue )
        !
        deallocate( a_DimNames )


        call ni3_inq_var( NcIDOut, "AbsCoef", VarIDAbsCoef )

      end if

      call ni3_put_varss( NcIDOut, "VMR"  , k, m_VMR )

      call ni3_put_varss( NcIDOut, "Press", k, Press )
      call ni3_put_varss( NcIDOut, "Temp" , k, Temp  )

      iWaveNum = 1
      do
        read( FUIn, *, iostat = ios ) WaveNum, AbsCoef, AbsCoefLen, &
          ( m_AbsCoef(m), m_AbsCoefLen(m), m = 1, NMolNum )
        if ( ios == -1 ) exit
        !
        WaveNum   = WaveNum * 1.0d2
        m_AbsCoef = m_AbsCoef * 1.0d-4
        !
        call ni3_put_varss( NcIDOut, "WaveNum", iWaveNum, WaveNum )

!!$      call ni3_put_varss( NcIDOut, "AbsCoef", iWaveNum, m_AbsCoef )

        St3( 1 ) = iWaveNum
        St3( 2 ) = 1
        St3( 3 ) = k
        Co3( 1 ) = 1
        Co3( 2 ) = NMolNum
        Co3( 3 ) = 1
        call ni3_enddef( NcIDOut )
        iStatus = nf90_put_var( NcIDOut, VarIDAbsCoef, m_AbsCoef, start = St3, count = Co3 )
        call ni3_handle_err( iStatus )
        !
        iWaveNum = iWaveNum + 1
      end do

      close( FUIn )

      ! check
      if ( (iWaveNum-1) /= NWaveNum ) then
        write( 6, * ) 'Unexpected number of lines: ', iWaveNum-1, NWaveNum
        stop
      end if

    end do

    call ni3_close( NcIDOut )

    write( 6, * ) 'Finish'

  end program main
