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 read_sw_tom_flux(infile,
     &     Nb,lambda_lo,lambda_hi,toa_sw)
      implicit none
      include 'max.inc'
c     
c     Purpose: to read the incoming SW flux @ TOA
c     
c     Input:
c       + infile: name of the file to read
c     
c     Output:
c       + Nb: number of spectral intervals
c       + lambda_lo: lower wavenumber of each interval [micrometers]
c       + lambda_hi: higher wavenumber of each interval [micrometers]
c       + toa_sw: incoming SW flux @ TOA in each interval [W/m^2]
c     
c     I/O
      character*(Nchar_mx) infile
      integer Nb
      double precision lambda_lo(1:Nb_mx)
      double precision lambda_hi(1:Nb_mx)
      double precision toa_sw(1:Nb_mx)      
c     temp
      integer ios,band
      double precision nu_lo(1:Nb_mx)
      double precision nu_hi(1:Nb_mx)
c     Debug
      integer Nq(1:Nb_mx)
      double precision w(1:Nb_mx,1:Nq_mx)
      integer quad
      double precision tauH(1:Nb_mx,1:Nq_mx)
      double precision Fdn0(1:Nb_mx),Fdn0_int
c     Debug
c     label
      character*(Nchar_mx) label
      label='subroutine read_sw_tom_flux'

      write(*,*) 'Opening file: ',trim(infile)
      open(11,file=trim(infile),
     &     status='old',iostat=ios)
      if (ios.ne.0) then        ! file not found
         call error(label)
         write(*,*) 'File could not be found:'
         write(*,*) trim(infile)
         stop
      else
         read(11,*)
         read(11,*) Nb
         read(11,*)
c     reverse order because bands are provided by order of increasing wavenumber
         do band=Nb,1,-1
            read(11,*) nu_lo(band),nu_hi(band),toa_sw(band)
            lambda_lo(band)=1.0D+4/nu_hi(band)
            lambda_hi(band)=1.0D+4/nu_lo(band)
         enddo                  ! band
      endif
      close(11)

c     Debug
c$$$      infile='./data/od_abs_sw_tot.txt'
c$$$      write(*,*) 'Opening file: ',trim(infile)
c$$$      open(11,file=trim(infile),
c$$$     &     status='old',iostat=ios)
c$$$      if (ios.ne.0) then        ! file not found
c$$$         call error(label)
c$$$         write(*,*) 'File could not be found:'
c$$$         write(*,*) trim(infile)
c$$$         stop
c$$$      endif
c$$$      do band=1,Nb
c$$$         read(11,*)             !lambda_lo(band),lambda_hi(band)
c$$$         read(11,*) Nq(band)
c$$$         do quad=1,Nq(band)
c$$$            read(11,*) w(band,quad)
c$$$            read(11,*) tauH(band,quad)
c$$$         enddo                  ! quad
c$$$      enddo                     ! band
c$$$      close(11)
c$$$      Fdn0_int=0.0D+0
c$$$      do band=1,Nb
c$$$         Fdn0(band)=0.0D+0
c$$$         do quad=1,Nq(band)
c$$$            Fdn0(band)=Fdn0(band)
c$$$     &           +toa_sw(band)*exp(-tauH(band,quad))*w(band,quad)
c$$$         enddo                  ! quad
c$$$         Fdn0_int=Fdn0_int+Fdn0(band)
c$$$      enddo                     ! band
c$$$      open(12,file='./results/Fdn0.txt')
c$$$      do band=1,Nb
c$$$         write(12,*) lambda_lo(band),lambda_hi(band),Fdn0(band)
c$$$      enddo                     ! band
c$$$      write(12,*) Fdn0_int
c$$$      close(12)
c     Debug

      return
      end
      
