program main

  use vtype_module

  use fi_module
  use lbl_module

  implicit none

  character(extstr) :: CtlFN  = 'conv_par2nc.conf'

  integer, parameter :: MaxNmlArySize = 64

  character(extstr) :: HITRANSrc
  character(extstr) :: HITRANRef

  character(len=extstr)              :: HITFileName   (MaxNmlArySize)
  character(len=extstr)              :: OutputFileName(MaxNmlArySize)
  integer                            :: NHIT
  character(len=extstr), allocatable :: a_HITFileName   (:)
  character(len=extstr), allocatable :: a_OutputFileName(:)

  character(extstr)                  :: OutFileName


  ! Local variables
  !

  type( LineParam )              :: LParam1
  type( LineParam )              :: LParam2

  integer                        :: NLine


  character(len=extstr)     :: FileName

  integer               :: MolNum      (MaxNmlArySize)
  real(dp)              :: LineStrenMin(MaxNmlArySize)

  integer               :: NMolNum
  integer , allocatable :: m_MolNum      (:)
  real(dp), allocatable :: m_LineStrenMin(:)

  integer            :: FileUnit
  integer            :: iostat

  integer            :: m

  namelist /file_nml/ &
    & HITRANSrc, HITRANRef, &
    & HITFileName, OutputFileName
  namelist /mol_nml/ &
    & MolNum, &
    & LineStrenMin


  HITRANSrc         = "HITRANXXXX"
  HITRANRef         = "Rothman et al. XXXX"
  HITFileName   (:) = ""
  OutputFileName(:) = ""

  MolNum      (:) = 0
  LineStrenMin(:) = 0.0d0


  call fi_open( CtlFN, "read", FileUnit )
  !
  rewind( FileUnit )
  read( FileUnit, nml = file_nml, iostat = iostat )
  if ( iostat > 0 ) then
    stop 'Namelist in file_nml is something wrong.'
  end if
  write( 6, nml = file_nml )
  !
  rewind( FileUnit )
  read( FileUnit, nml = mol_nml, iostat = iostat )
  if ( iostat > 0 ) then
    stop 'Namelist in mol_nml is something wrong.'
  end if
  write( 6, nml = mol_nml )
  !
  close( FileUnit )


  NMolNum = 0
  do m = 1, MaxNmlArySize
    if ( MolNum(m) <= 0 ) exit
  end do
  NMolNum = m - 1
  allocate( m_MolNum( NMolNum ) )
  do m = 1, NMolNum
    m_MolNum(m) = MolNum(m)
  end do

  NHIT = 0
  do m = 1, MaxNmlArySize
    if ( len_trim( HITFileName(m) ) == 0 ) exit
  end do
  NHIT = m - 1
  allocate( a_HITFileName   (NHIT) )
  allocate( a_OutputFileName(NHIT) )
  do m = 1, NHIT
    a_HITFileName   (m) = HITFileName   (m)
    a_OutputFileName(m) = OutputFileName(m)
  end do
  ! check
  if ( NHIT /= NMolNum ) then
    write( 6, * ) 'Number of HITRAN data files specified in mol_nml does not match to number of species in file_nml.'
    write( 6, * ) 'Number of HITRAN data files: ', NHIT
    write( 6, * ) 'Number of species          : ', NMolNum
    stop
  end if


  allocate( m_LineStrenMin(NHIT) )
  m_LineStrenMin = LineStrenMin(1:NHIT)


  do m = 1, NHIT
    FileName    = a_HITFileName   (m)
    OutFileName = a_OutputFileName(m)

    write( 6, * ) 'Input  File: ', trim( FileName )
    write( 6, * ) 'Output File: ', trim( OutFileName )

    NLine = fi_lc( FileName )

    write( 6, '(a,i9)' ) ' Number of lines : ', NLine

    call lbl_lineparam_init( m_MolNum( m ), NLine, 1, LParam1 )

!!$    call lbl_readfile         ( FileName, LParam1 )
    call lbl_readfile_hitran04( FileName, LParam1 )

    call lbl_show_statistics( LParam1 )

    call lbl_select_lines( m_LineStrenMin(m), LParam1, LParam2 )

    call lbl_show_statistics( LParam2 )

    call lbl_writefile_netcdf( OutFileName, HITRANSrc, HITRANRef, LParam2 )

    call lbl_lineparam_finalize( LParam1 )
    call lbl_lineparam_finalize( LParam2 )

  end do


end program main
