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 :: kmax = 35-2
  real(sp)                    :: z_Press   (1:kmax)
  real(sp)                    :: z_UwFlux  (1:kmax)
  real(sp)                    :: z_DwFlux  (1:kmax)


  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 = "Radiative 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 ) // "flux.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    = "level"
    stdname = "level"
    units   = "Pa"
!!$  call ni3_set_dim( ncid, name, NF90_REAL, z_LevelNum, &
!!$    & stdname = stdname, units  = units )
    call ni3_def_dim( ncid, name, NF90_REAL, kmax, &
      & stdname = stdname, units  = units )
    call ni3_put_att( ncid, name, 'positive', 'down' )
    !
    NDims = 1
    allocate( a_Dimname( NDims ) )
    a_DimName( 1 ) = "level"
    !
    name         = "upflux"
    stdname      = "upward_flux"
    units        = "W m-2"
    call ni3_def_var( ncid, name, NF90_REAL, ndims, a_DimName, &
      & stdname = stdname, units = units, fillvalue = FillValue )
    !
    name         = "downflux"
    stdname      = "downward_flux"
    units        = "W m-2"
    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 ) // 'flux_' // 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 k = 1, kmax
!    mb        Wm-2       Wm-2      Wm-2
      read( fu, *, iostat = ios ) z_Press(k), z_DwFlux(k), z_UwFlux(k)
      if ( ios == -1 ) stop 'Error during reading a file'
    end do

    z_Press = z_Press * 1.0d2

    name = "level"
    call ni3_put_var( ncid, name, z_Press )
    name = "upflux"
    call ni3_put_var( ncid, name, z_UwFlux )
    name = "downflux"
    call ni3_put_var( ncid, name, z_DwFlux )

    close( fu )

    call ni3_close( ncid )

  end do


end program main
