module rad_output

  use vtype_module

  implicit none

  private

  public :: RadOutputFluxTendency

contains

  !----------------------------------------------------------------------------

  subroutine RadOutputFluxTendency( &
    & OutFluxNcFN, OutTendencyNcFN, &
    & kmax, r_Press, z_Press, &
    & r_RadUwFlux, r_RadDwFlux, z_DTempDt &
    & )

    use ni3_module
    use netcdf

    character(*), intent(in ) :: OutFluxNcFN
    character(*), intent(in ) :: OutTendencyNcFN
    integer     , intent(in ) :: kmax
    real(DP)    , intent(in ) :: r_Press    (0:kmax)
    real(DP)    , intent(in ) :: z_Press    (1:kmax)
    real(DP)    , intent(in ) :: r_RadUwFlux(0:kmax)
    real(DP)    , intent(in ) :: r_RadDwFlux(0:kmax)
    real(DP)    , intent(in ) :: z_DTempDt  (1:kmax)


    ! Local variables
    !
    integer :: NcID
    character(extstr) :: Name
    character(extstr) :: StdName
    character(extstr) :: Units
    integer, parameter :: NDims1 = 1
    character(extstr)  :: a_DimName1(NDims1)



    call ni3_open( OutFluxNcFN, "new", NcID )

    Name    = "Press"
    StdName = "plev"
    Units   = "Pa"
    call ni3_set_dim( NcID, name, NF90_DOUBLE, r_Press, &
      & stdname = stdname, units  = units )


    a_DimName1( 1 ) = "Press"

    Name    = "RadUwFlux"
    StdName = "upward radiation flux"
    Units   = "W m-2"
    call ni3_def_var( NcID, name, NF90_DOUBLE, NDims1, a_DimName1, &
      stdname = stdname, units = units )
    call ni3_put_var( NcID, name, r_RadUwFlux )

    Name    = "RadDwFlux"
    StdName = "upward radiation flux"
    Units   = "W m-2"
    call ni3_def_var( NcID, name, NF90_DOUBLE, NDims1, a_DimName1, &
      stdname = stdname, units = units )
    call ni3_put_var( NcID, name, r_RadDwFlux )

    call ni3_close( NcID )



    call ni3_open( OutTendencyNcFN, "new", NcID )

    Name    = "Press"
    StdName = "plev"
    Units   = "Pa"
    call ni3_set_dim( NcID, name, NF90_DOUBLE, z_Press, &
      & stdname = stdname, units  = units )


    a_DimName1( 1 ) = "Press"

    Name    = "DTempDt"
    StdName = "radiative heating rate"
    Units   = "K s-1"
    call ni3_def_var( NcID, name, NF90_DOUBLE, NDims1, a_DimName1, &
      stdname = stdname, units = units )
    call ni3_put_var( NcID, name, z_DTempDt )

    call ni3_close( NcID )


  end subroutine RadOutputFluxTendency

  !----------------------------------------------------------------------------

end module rad_output
