c     Copyright (C)
c     2010-2013 Université Bordeaux 1
c     2010-2013 Ecole des Mines d'Albi
c     2014-2018 - Mesostar
c     
c     This program is free software; you can redistribute it and/or modify
c     it under the terms of the GNU General Public License as published by
c     the Free Software Foundation; either version 2, or (at your option)
c     any later version.

c     This program is distributed in the hope that it will be useful,
c     but WITHOUT ANY WARRANTY; without even the implied warranty of
c     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c     GNU General Public License for more details.

c     You should have received a copy of the GNU General Public License
c     along with this program; if not, write to the Free Software
c     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
c     USA.
c
      subroutine analytic_budgets_from_ner(ai,Nlay,ner,
     &     budget,gas_ground,gas_space,gas_gas)
      implicit none
      include 'max.inc'
      include 'param.inc'
c     
c     Purpose: to compute analytic budgets from a NER matrix
c
c     Input:
c       + ai: true if angular integration is enabled
c       + Nlay: number of gas layers
c       + ner: NER matrix (W/m^2 if ai=T, W/m^2/sr if ai=F)
c     
c     Output:
c       + budget: radiative budgets (W/m²) (cooling rate)
c       + gas_ground: part of the radiative budget due to NERs with ground (W/m²)
c       + gas_space: part of the radiative budget due to NERs with space (W/m²)
c       + gas_gas: part of the radiative budget due to NERs between gas layers (W/m²)
c
c     Results have to be devided by gas layer thickness in order to get
c     volumic budgets (W/m³). This can not be done within this routine, since
c     it has to be very general (only layers optical thicknesses are known).
c
c     A positive value means the gas layer is heated by radiative exchanges.
c     The budget is therefore a heating rate.
c
c     I/O
      logical ai
      integer Nlay
      double precision ner(0:Nlay_mx+1,0:Nlay_mx+1)
      double precision budget(1:Nlay_mx)
      double precision gas_ground(1:Nlay_mx)
      double precision gas_space(1:Nlay_mx)
      double precision gas_gas(1:Nlay_mx)
c     temp
      integer i,j,p
      double precision coeff
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine analytic_budgets_from_ner'

c     tests
      if (Nlay.le.0) then
         call error(label)
         write(*,*) 'Bad input parameter:'
         write(*,*) 'Nlay=',Nlay
         stop
      endif
      if (Nlay.gt.Nlay_mx) then
         call error(label)
         write(*,*) 'Bad input parameter:'
         write(*,*) 'Nlay=',Nlay
         write(*,*) 'while Nlay_mx=',Nlay_mx
         stop
      endif
      if (ai) then
         coeff=1.0D+0
      else
         coeff=pi
      endif

      do i=1,Nlay
         budget(i)=0.0D+0
         gas_ground(i)=0.0D+0
         gas_space(i)=0.0D+0
         gas_gas(i)=0.0D+0
         do j=0,Nlay+1
            budget(i)=budget(i)-ner(i,j)*coeff
            if (j.eq.0) then
               gas_ground(i)=gas_ground(i)-ner(i,j)*coeff
            else if (j.eq.Nlay+1) then
               gas_space(i)=gas_space(i)-ner(i,j)*coeff
            else
               gas_gas(i)=gas_gas(i)-ner(i,j)*coeff
            endif
         enddo ! j
      enddo ! i

      return
      end



      subroutine analytic_budgets_from_net_fluxes(Nlay,net_flux,
     &     budget)
      implicit none
      include 'max.inc'
      include 'param.inc'
c     
c     Purpose: to compute analytic budgets from a NER matrix
c
c     Input:
c       + Nlay: number of gas layers
c       + net_flux: net flux at each interface (W/m²)
c     
c     Output:
c       + budget: radiative budgets (W/m²)
c
c     Results have to be devided by gas layer thickness in order to get
c     volumic budgets (W/m³). This can not be done within this routine, since
c     it has to be very general (only layers optical thicknesses are known).
c
c     A positive value means the gas layer is heated by radiative exchanges.
c     The budget is therefore a heating rate.
c
c     I/O
      integer Nlay
      double precision net_flux(0:Nlay_mx)
      double precision budget(1:Nlay_mx)
c     temp
      integer i,j,p
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine analytic_budgets_from_net_fluxes'

c     tests
      if (Nlay.le.0) then
         call error(label)
         write(*,*) 'Bad input parameter:'
         write(*,*) 'Nlay=',Nlay
         stop
      endif
      if (Nlay.gt.Nlay_mx) then
         call error(label)
         write(*,*) 'Bad input parameter:'
         write(*,*) 'Nlay=',Nlay
         write(*,*) 'while Nlay_mx=',Nlay_mx
         stop
      endif

      do i=1,Nlay
c     Using a minus sign because of the convention: positive means heating
         budget(i)=net_flux(i-1)-net_flux(i)
      enddo ! i

      return
      end
