program main

  use vtype_module
  use ni3_module
  use netcdf

  implicit none

  integer, parameter :: NumCases = 21

  integer               :: a_CaseNum     (NumCases)
  character(len=extstr) :: a_CaseChar    (NumCases)
  character(len=extstr) :: a_CaseTailChar(NumCases)

  integer               :: CaseNum
  character(len=extstr) :: CaseChar
  character(len=extstr) :: CaseTailChar

  integer         , parameter :: lmax = 2978-2
  real(sp)                    :: w_WaveNum(1:lmax)
  real(sp)                    :: w_SpeFlux(1:lmax)


  character(len=extstr)            :: line

  character(len=extstr)            :: ncfn
  character(len=extstr)            :: mode
  character(len=4*extstr)          :: title, com, ref, inst, conv, src, hist
  character(len=extstr)            :: name, stdname, longname, units

  integer(i4b)                     :: ncid

  integer(i4b)                       :: NDims
  character(len=extstr), allocatable :: a_Dimname( : )

  real(sp)             , parameter :: FillValue = -99999.0

  character(len=extstr)            :: dir0
  character(len=extstr)            :: dir
  character(len=extstr)            :: fn
  character(len=extstr)            :: path
  integer                 :: fu
  integer                 :: ios
  logical                 :: ex
  logical                 :: od

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

  data a_CaseNum      / 9,     10,    19,    20,    23,    23,    23,    25,    27,    28,    29,    31,    33,    35,    37,    37,    37,    52,    53,    54,    55 /
  data a_CaseChar     / '',    '',    '',    '',   'a',   'b',   'c',    '',    '',    '',    '',    '',    '',    '',   'a',   'b',   'g',    '',    '',    '',    '' /
  data a_CaseTailChar / 'mls', 'mls', 'mls', 'mls', 'mls', 'mls', 'mls', 'trp', 'mls', 'mls', 'mlw', 'sas', 'saw', 'mls', 'mls', 'mls', 'mls', 'mls', 'mls', 'mls', 'mls' /


  do n = 1, NumCases
    write( 6, * ) n

    CaseNum      = a_CaseNum(n)
    CaseChar     = a_CaseChar(n)
    CaseTailChar = a_CaseTailChar(n)

    !
    ! Output to a NetCDF file
    !
    mode = "new"
    title = "Surface spectral flux calculated by LBLRTM for ICRCCM"
    inst  = "GFD Dennou Club, DCPAM project"
    src   = "http://www.met.fsu.edu/people/ellingson/LBLWeb/"
    conv  = "CF-1.5"
    hist  = "An electronic (ASCII) data file was downloaded from http://www.met.fsu.edu/people/ellingson/LBLWeb/, and was converted into netcdf file by GFD Dennou Club."
    ref   = "Ellingson, R.G., J. Ellis, and S. Fels, 1991: "
    ref   = trim(ref) // "The intercomparison of radiation codes used in climate models: Long wave results, J. Geophys. Res., 96, D5, 8929."
    com   = ""!Please note that all data are not included."
    !
    write( ncfn, '(a,i2.2,a)' ) "out/Case", CaseNum, trim( CaseChar ) // "srf.nc"
    write( 6, * ) 'Creating ', trim( ncfn )
    call ni3_open( ncfn, mode, ncid )
    call ni3_set_ga( ncid, title = title, conv = conv, inst = inst, src = src, hist = hist, ref = ref, com = com )
    !
    !============================================================================
    name    = "wn"
    stdname = "wn"
    units   = "m-1"
!!$  call ni3_set_dim( ncid, name, NF90_REAL, z_LevelNum, &
!!$    & stdname = stdname, units  = units )
    call ni3_def_dim( ncid, name, NF90_REAL, lmax, &
      & stdname = stdname, units  = units )
    !
    NDims = 1
    allocate( a_Dimname( NDims ) )
    a_DimName( 1 ) = "wn"
    !
    name         = "srf"
    stdname      = "surface_spectral_flux"
    units        = "W m-2 (m-1)-1"
    call ni3_def_var( ncid, name, NF90_REAL, ndims, a_DimName, &
      & stdname = stdname, units = units, fillvalue = FillValue )
    !
    deallocate( a_Dimname )
    !============================================================================


    dir0 = '../data/www.met.fsu.edu/people/ellingson/LBLWeb/results'
    write( dir, '(a,i2.2,a)' ) trim( dir0 ) // '/' // 'Case', CaseNum, trim( CaseChar ) // '-' // trim( CaseTailChar )
    write( fn, '(a,i2.2,a)' ) 'Case', CaseNum, trim( CaseChar ) // 'srf_' // trim( CaseTailChar ) // '.txt'
    path = trim( dir ) // '/' // trim( fn )

    write( 6, * ) 'Reading ', trim( path )

    inquire( file = path, opened = od, exist = ex )

    fu = 50
    open( fu, file=path, status='unknown' )

    ! header
    do l = 1, 2
      read( fu, * ) line
    end do

    do l = 1, lmax
!    cm-1          mW/(m2 cm sr) <--- mW/(m2 cm-1 sr) ?
      read( fu, *, iostat = ios ) w_WaveNum(l), w_SpeFlux(l)
      if ( ios == -1 ) stop 'Error during reading a file'
    end do

    w_WaveNum = w_WaveNum * 1.0d2
    w_SpeFlux = w_SpeFlux * 1.0d-3 * 1.0d-2 * acos( -1.0d0 )

    name = "wn"
    call ni3_put_var( ncid, name, w_WaveNum )
    name = "srf"
    call ni3_put_var( ncid, name, w_SpeFlux )

    close( fu )

    call ni3_close( ncid )

  end do


end program main
