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 initialize_rt_results(fullRT,
     &     Nlay,Nb,
     &     si_NER,si_budget_from_NERs,si_budget_from_net_fluxes,
     &     si_gas_ground,si_gas_space,si_gas_gas,
     &     si_upward_flux,si_downward_flux,si_net_flux,
     &     band_net_flux_toa,band_NER,band_is_NER,
     &     band_total_budget,band_gas_ground_budget,
     &     band_gas_space_budget,band_gas_gas_budget,
     &     band_upward_flux,band_downward_flux,band_net_flux)
      implicit none
      include 'max.inc'
c
c     Purpose: to initialize arrays that are used to store
c     radiative results.
c
c     Input:
c       + fullRT: if true, full NER are computed; if false, only gas-space NERs are computed
c       + Nlay: number of atmospheric layers
c       + Nb: number of spectral bands
c     
c     Output:
c       + si_NER: spectrally integrated NER matrix (W/m^2)
c       + si_budget_from_NERs: spectrally integrated radiative budget (W/m^3) for each gas layer (i=1,Nlay), computed from NERs
c       + si_budget_from_net_fluxes: spectrally integrated radiative budget (W/m^3) for each gas layer (i=1,Nlay), computed from net fluxes
c       + si_gas_ground: spectrally integrated part of the radiative budget (W/m^3) due to gas-ground NERs, for each gas layer (i=1,Nlay)
c       + si_gas_space: spectrally integrated part of the radiative budget (W/m^3) due to gas-space NERs, for each gas layer (i=1,Nlay)
c       + si_gas_gas: spectrally integrated part of the radiative budget (W/m^3) due to gas-gas NERs, for each gas layer (i=1,Nlay)
c       + si_upward_flux: spectrally integrated upward flux (W/m^2) at each interface (i=0,Nlay)
c       + si_downward_flux: spectrally integrated downward flux (W/m^2) at each interface (i=0,Nlay)
c       + si_net_flux: spectrally integrated net flux (W/m^2) at each interface (i=0,Nlay)
c       + band_net_flux_toa: net flux @ TOA, per band (W/m^2 in each band)
c       + band_NER: NER matrix, per band (W/m^2 in each band)
c       + band_is_NER: gas-space NERs, per band (W/m^2 in each band)
c       + band_total_budget: total radiative budget, per band (W/m^3 in each band)
c       + band_gas_ground_budget: par of the total radiative budget due to gas-ground NERs, per band (W/m^3 in each band)
c       + band_gas_space_budget: par of the total radiative budget due to gas-space NERs, per band (W/m^3 in each band)
c       + band_gas_gas_budget: par of the total radiative budget due to gas-gas NERs, per band (W/m^3 in each band)
c       + band_upward_flux: upward flux, per band (W/m^2 in each band)
c       + band_downward_flux: downward flux, per band (W/m^2 in each band)
c       + band_net_flux: net flux, per band (W/m^2 in each band)
c
c     I/O
      logical fullRT
      integer Nlay,Nb
      double precision si_NER(0:Nlay_mx+1,0:Nlay_mx+1)
      double precision si_budget_from_NERs(1:Nlay_mx)
      double precision si_budget_from_net_fluxes(1:Nlay_mx)
      double precision si_gas_ground(1:Nlay_mx)
      double precision si_gas_space(1:Nlay_mx)
      double precision si_gas_gas(1:Nlay_mx)
      double precision si_upward_flux(0:Nlay_mx)
      double precision si_downward_flux(0:Nlay_mx)
      double precision si_net_flux(0:Nlay_mx)
      double precision band_net_flux_toa(1:Nb_mx)
      double precision band_NER(0:Nlay_mx+1,0:Nlay_mx+1,1:Nb_mx)
      double precision band_is_NER(0:Nlay_mx,1:Nb_mx)
      double precision band_total_budget(1:Nlay_mx,1:Nb_mx)
      double precision band_gas_ground_budget(1:Nlay_mx,1:Nb_mx)
      double precision band_gas_space_budget(1:Nlay_mx,1:Nb_mx)
      double precision band_gas_gas_budget(1:Nlay_mx,1:Nb_mx)
      double precision band_upward_flux(0:Nlay_mx,1:Nb_mx)
      double precision band_downward_flux(0:Nlay_mx,1:Nb_mx)
      double precision band_net_flux(0:Nlay_mx,1:Nb_mx)
c     temp
      integer i,j,band
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine initialize_rt_results'

      do i=0,Nlay+1
         do j=0,Nlay+1
            si_NER(i,j)=0.0D+0
         enddo ! j
      enddo ! i
      if (fullRT) then
         do i=1,Nlay
            si_budget_from_NERs(i)=0.0D+0
            si_budget_from_net_fluxes(i)=0.0D+0
            si_gas_ground(i)=0.0D+0
            si_gas_space(i)=0.0D+0
            si_gas_gas(i)=0.0D+0
         enddo                  ! i
         do i=0,Nlay
            si_upward_flux(i)=0.0D+0
            si_downward_flux(i)=0.0D+0
            si_net_flux(i)=0.0D+0
         enddo                  ! i
      else ! initialize only the spectrally integrated net flux @ TOA
         si_net_flux(Nlay)=0.0D+0
      endif ! fullRT

      do band=1,Nb
c     Initialization of arrays that record results per narrowband
         band_net_flux_toa(band)=0.0D+0
         do i=1,Nlay
            band_is_NER(i,band)=0.0D+0
         enddo ! i
         do i=0,Nlay+1
            do j=0,Nlay+1
               band_NER(i,j,band)=0.0D+0
            enddo ! j
         enddo ! i
         if (fullRT) then
            do i=1,Nlay
               band_total_budget(i,band)=0.0D+0
               band_gas_ground_budget(i,band)=0.0D+0
               band_gas_space_budget(i,band)=0.0D+0
               band_gas_gas_budget(i,band)=0.0D+0
            enddo               ! i
            do i=0,Nlay
               band_upward_flux(i,band)=0.0D+0
               band_downward_flux(i,band)=0.0D+0
               band_net_flux(i,band)=0.0D+0
            enddo               ! i
         endif ! fullRT
      enddo                     ! band

      return
      end
      
