  program main

    use vtype_module
    use ni3_module
    use netcdf

    implicit none

    character(len=extstr)            :: InFN
    integer(i4b)                     :: FU


    integer(i4b)                     :: NHeader
    integer(i4b)                     :: NData
    integer(i4b)                     :: NData1
    integer(i4b)                     :: NData2

    real(dp)  :: WavelengthS
    real(dp)  :: WavelengthE

    integer, parameter :: NPtcl = 4+1
    integer            :: a_PtclNum ( NPtcl )
    character(stdstr)  :: a_PtclName( NPtcl )

    integer              , parameter   :: NChar = stdstr
    integer                            :: a_CharNum( NChar )

    real(dp), allocatable :: w_Wavelength(:)
    real(dp), allocatable :: wa_QExt(:,:)
    real(dp), allocatable :: wa_SSA (:,:)
    real(dp), allocatable :: wa_AF  (:,:)

    real(dp)  :: F0

    character(len=extstr)            :: NcFN
    integer(i4b)                     :: NcID
    character(len=extstr*2)          :: ref
    character(len=extstr)            :: Mode, title, com, &
         name, stdname, longname, units
    integer(i4b)                       :: ndims
    character(len=extstr), allocatable :: a_dimnames(:)
!!$    real(dp)                         :: fillvalue

    integer(i4b)          :: l
    integer(i4b)          :: n


    title   = 'Optical properties of cloud'
    NcFN    = '../ncfiles/cloud_opt_prop_Crisp1986.nc'
    com     = ''
    ref   = "Crisp, D. (1986), Radiative Forcing of the Venus Mesosphere I. Solar Fluxes and Heating Rates, " &
      // "Icarus, 67, 484."

    NHeader = 2
    NData1   = 50 - NHeader
    NData2   = 16 - NHeader
    NData    = NData1 + NData2

    allocate( w_Wavelength( NData ) )
    allocate( wa_QExt( NData, NPtcl ) )
    allocate( wa_SSA ( NData, NPtcl ) )
    allocate( wa_AF  ( NData, NPtcl ) )


    do n = 1, NPtcl
      a_PtclNum(n) = n
    end do
    a_PtclName( 1 ) = "vcldmode1"
    a_PtclName( 2 ) = "vcldmode2"
    a_PtclName( 3 ) = "vcldmode2'"
    a_PtclName( 4 ) = "vcldmode3"
    a_PtclName( 5 ) = "vuvabs"

    do l = 1, NChar
      a_CharNum(l) = l
    end do

    !
    ! Preparation for input file
    !
    FU = 50
    !
    InFN    = 'data/uv-vis_Mode1+UVabs.txt'
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = 1, NData1
      read( FU, * ) WavelengthS, WavelengthE, &
        & F0, &
        & wa_QExt(l,1), wa_SSA(l,1), wa_AF(l,1), &
        & wa_QExt(l,5), wa_SSA(l,5), wa_AF(l,5)
      w_Wavelength(l) = ( WavelengthS + WavelengthE ) / 2.0d0
    end do
    close( FU )
    !
    InFN    = 'data/uv-vis_Mode2+3.txt'
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = 1, NData1
      read( FU, * ) WavelengthS, WavelengthE, &
        & wa_QExt(l,2), wa_SSA(l,2), wa_AF(l,2), &
        & wa_QExt(l,3), wa_SSA(l,3), wa_AF(l,3), &
        & wa_QExt(l,4), wa_SSA(l,4), wa_AF(l,4)
    end do
    close( FU )

    InFN    = 'data/infrared_Mode1.txt'
    n = 1
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = NData1+1, NData1+NData2
      read( FU, * ) WavelengthS, WavelengthE, &
        & wa_QExt(l,n), wa_SSA(l,n)
      wa_AF(l,n) = 0.0d0
      w_Wavelength(l) = ( WavelengthS + WavelengthE ) / 2.0d0
    end do
    close( FU )
    !
    InFN    = 'data/infrared_Mode2.txt'
    n = 2
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = NData1+1, NData1+NData2
      read( FU, * ) WavelengthS, WavelengthE, &
        & wa_QExt(l,n), wa_SSA(l,n)
      wa_AF(l,n) = 0.0d0
    end do
    close( FU )
    !
    InFN    = 'data/infrared_Mode2prime.txt'
    n = 3
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = NData1+1, NData1+NData2
      read( FU, * ) WavelengthS, WavelengthE, &
        & wa_QExt(l,n), wa_SSA(l,n)
      wa_AF(l,n) = 0.0d0
    end do
    close( FU )
    !
    InFN    = 'data/infrared_Mode3.txt'
    n = 4
    open( FU, file = InFN, status='unknown' )
    do l = 1, NHeader
      read( FU, * )
    end do
    do l = NData1+1, NData1+NData2
      read( FU, * ) WavelengthS, WavelengthE, &
        & wa_QExt(l,n), wa_SSA(l,n)
      wa_AF(l,n) = 0.0d0
    end do
    close( FU )
    !
    n = 5
    do l = NData1+1, NData1+NData2
!!$      wa_QExt(l,n) = 0.0d0
!!$      wa_SSA (l,n) = 0.0d0
!!$      wa_AF  (l,n) = 0.0d0
      wa_QExt(l,n) = wa_QExt(l,1)
      wa_SSA (l,n) = wa_SSA (l,1)
      wa_AF  (l,n) = wa_AF  (l,1)
    end do

    w_Wavelength = w_Wavelength * 1.0d-6
    do n = 1, NPtcl
      do l = 1, NData
        if ( wa_QExt(l,n) == 0.0d0 ) then
          wa_SSA(l,n) = 0.0d0
        else
          wa_SSA(l,n) = ( wa_QExt(l,n) - wa_SSA(l,n) ) / wa_QExt(l,n)
        end if
      end do
    end do


    !
    ! Preparation for output file
    !
    Mode = "new"
    call ni3_open( NcFN, Mode, NcID )
    call ni3_set_ga( ncid, title = title, com = com, ref = ref )

    name     = "wl"
    stdname  = name
    longname = 'wavelength'
    units    = "m"
    call ni3_set_dim( NcID, name, NI3_REAL, w_Wavelength, &
      stdname = stdname, longname = longname, units  = units )

    name     = "PtclNum"
    stdname  = name
    longname = 'particle number'
    units    = "1"
    call ni3_set_dim( NcID, name, NI3_INT, a_PtclNum, &
      stdname = stdname, longname = longname, units  = units )

    Name     = "CharNum"
    StdName  = Name
    LongName = "character number"
    Units    = "1"
    call ni3_set_dim( NcID, Name, NI3_INT, a_CharNum, &
      & StdName, LongName, Units )

    NDims = 2
    allocate( a_dimnames( NDims ) )

    Name     = "PtclName"
    StdName  = Name
    LongName = "particle name"
    Units    = "m"
    a_DimNames( 1 ) = "CharNum"
    a_DimNames( 2 ) = "PtclNum"
    call ni3_def_var( NcID, Name, NI3_CHAR, NDims, a_DimNames, &
      & StdName, LongName, Units )
    call ni3_put_var( NcID, Name, a_PtclName )

    a_dimnames( 1 ) = "wl"
    a_dimnames( 2 ) = "PtclNum"

    name         = "QExt"
    stdname      = name
    longname     = "extinction efficiency factor"
    units        = "1"
    call ni3_def_var( NcID, name, NI3_REAL, ndims, a_dimnames, &
         stdname = stdname, longname = longname, units = units )
    call ni3_put_var( NcID, name, wa_QExt )

    name         = "SSA"
    stdname      = name
    longname     = "single scattering albedo"
    units        = "1"
    call ni3_def_var( NcID, name, NI3_REAL, ndims, a_dimnames, &
         stdname = stdname, longname = longname, units = units )
    call ni3_put_var( NcID, name, wa_SSA  )

    name         = "AF"
    stdname      = name
    longname     = "asymmetry factor"
    units        = "1"
    call ni3_def_var( NcID, name, NI3_REAL, ndims, a_dimnames, &
         stdname = stdname, longname = longname, units = units )
    call ni3_put_var( NcID, name, wa_AF   )

    deallocate( a_dimnames )

    close( FU )
    call ni3_close( NcID )


  end program main
