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 discrete_contribution_function(Nlay,Nb,
     &     first_band,last_band,
     &     z,
     &     band_is_NER,
     &     band_net_flux_toa,
     &     discrete_cf)
      implicit none
      include 'max.inc'
c
c     Purpose: to compute the contribution function in each narrowband
c     The contribution function is defined as the fraction of energy that leaves the
c     atmosphere as a function of its emission altitude.
c     
c     Input:
c       + Nlay: number of atmospheric layers
c       + Nb: number of spectral intervals
c       + first_band: index of the first integration narrowband
c       + last_band: index of the last integration narrowband
c       + z: altitude at the top of each layer
c       + band_is_NER: NER between each layer and space, in each narrowband
c       + band_net_flux_toa: net flux @ TOA, in each narrowband
c
c     Output:
c       + discrete_cf: contribution function, as a function of the emission layer (discrete)
c
c     I/O
      integer Nlay,Nb
      integer first_band,last_band
      double precision z(0:Nlay_mx)
      double precision band_is_NER(0:Nlay_mx,1:Nb_mx)
      double precision band_net_flux_toa(1:Nb_mx)
      double precision discrete_cf(0:Nlay_mx,1:Nb_mx)
c     temp
      integer i,band
c     parameters
      double precision epsilon
      parameter(epsilon=1.0D-10)
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine discrete_contribution_function'

      do band=first_band,last_band
         do i=0,Nlay
c            if ((band_is_NER(i,band).lt.0.0D+0).and.
c     &           (dabs(band_is_NER(i,band)).gt.epsilon))then
c               call warning(label)
c               write(*,*) 'band_is_NER(',i,',',band,')=',
c     &              band_is_NER(i,band)
c            write(*,*) 'is not supposed to be negative'
c            endif
c     The contribution function is the Net Exchange between each layer (ground included)
c     and space, normalized by the total (the net flux @ TOA in the current band).
            discrete_cf(i,band)=band_is_NER(i,band)
     &           /band_net_flux_toa(band)
         enddo ! i
      enddo ! band

      return
      end
