program main

  use vtype_module

  use fi_module
  use xsc_module

  implicit none

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

  integer                        :: NFile
  character(extstr), allocatable :: a_XSCFileName(:)
  character(extstr) :: OutputFileName
  character(extstr) :: Src
  character(extstr) :: Com
  character(extstr) :: Ref

  real(dp) :: WaveNumS
  real(dp) :: WaveNumE

  ! Local variables
  !

  integer , parameter   :: MaxNmlArySize = 256
  character(extstr)     :: XSCFileName(MaxNmlArySize)


  type( XSCParam ) :: XSCP

  integer            :: FileUnit
  integer            :: iostat

  integer            :: n


  namelist /file_nml/ &
    & WaveNumS, WaveNumE, &
    & XSCFileName, &
    & OutputFileName, &
    & Src, Com, Ref


  WaveNumS = -1.0d0
  WaveNumE =  1.0d100

  XSCFileName    = ""
  OutputFileName = "./out"

  Src = ""
  Com = ""
  Ref = ""

  ! Explanation

  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 )
  !
  close( FileUnit )

  NFile = 0
  do n = 1, MaxNmlArySize
    if ( len_trim( XSCFileName(n) ) > 0 ) then
      NFile = NFile + 1
    end if
  end do

  allocate( a_XSCFileName(NFile) )
  a_XSCFileName = XSCFileName(1:NFile)

  write( 6, * ) 'Input  File: '
  do n = 1, NFile
    write( 6, * ) '  ', trim( a_XSCFileName(n) )
  end do
  write( 6, * ) 'Output File: ', trim( OutputFileName )

  call XSCReadHITRANXSCFile( &
    & NFile, a_XSCFileName, &
    & WaveNumS, WaveNumE, &
    & XSCP &
    & )
  call XSCWriteFileNetcdf( OutputFileName, Src, Com, Ref, XSCP )
  call XSCParamFinalize( XSCP )

  deallocate( a_XSCFileName )

end program main
