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 exp_sigma2ka(sigma1,sigma2,T,P1,P2,k)
      implicit none
      include 'max.inc'
      include 'constants.inc'
c
c     Purpose: to compute the average absorption coefficient between two altitudes
c     assuming that both the absorption cross section and the pressure
c     have exponential profiles, and that temperature is constant in the layer.
c
c     Input:
c       + sigma1: absorption cross section (cm^2/molecule) @ altitude z1
c       + sigma2: absorption cross section (cm^2/molecule) @ altitude z2
c       + T: average gas temperature (K) between z1 and z2
c       + P1: pressure (Pa) @ altitude z1
c       + P2: pressure (Pa) @ altitude z2
c
c     Output:
c       + k: average extinction coefficient (m^-1) between z1 and z2
c
c     I/O
      double precision sigma1,sigma2
      double precision T
      double precision P1,P2
      double precision k
c     temp
      double precision alpha
      double precision coeff
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine exp_sigma2ka'

      if ((sigma1.eq.0.0D+0).or.(sigma2.eq.0.0D+0)) then
         k=0.0D+0
      else
         if (T.eq.0.0D+0) then
            call error(label)
            write(*,*) 'T=',T
            stop
         endif
         alpha=dlog(P1*sigma1/(P2*sigma2))
         if (alpha.eq.0.0D+0) then
            call error(label)
            write(*,*) 'P1=',P1
            write(*,*) 'P2=',P2
            write(*,*) 'sigma1=',sigma1
            write(*,*) 'sigma2=',sigma2
            write(*,*) 'dlog(P1*sigma1/P2*sigma2)=',alpha
            stop
         endif
         k=1.0D-4*Na/(T*Rgp)*(P1*sigma1-P2*sigma2)/alpha ! m^-1
      endif

      return
      end



      subroutine sigma2ka(sigma,T,P1,P2,k)
      implicit none
      include 'max.inc'
      include 'constants.inc'
c
c     Purpose: to convert a cross section to an extinction coefficient
c     in a given volume between two pressure levels
c
c     Input:
c       + sigma: cross section (cm^2/molecule)
c       + T: gas temperature (K)
c       + P1: pressure (Pa) of the bottom level
c       + P2: pressure (Pa) of the top level
c
c     Output:
c       + k: extinction coefficient (m^-1)
c
c     I/O
      double precision sigma
      double precision T
      double precision P1,P2
      double precision k
c     temp
      double precision coeff
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine sigma2ka'

      if (sigma.eq.0.0D+0) then
         k=0.0D+0
      else
         if (T.eq.0.0D+0) then
            call error(label)
            write(*,*) 'T=',T
            stop
         endif
         if (dlog(P1/P2).eq.0.0D+0) then
            call error(label)
            write(*,*) 'P1=',P1
            write(*,*) 'P2=',P2
            write(*,*) 'dlog(P1/P2)=',dlog(P1/P2)
            stop
         endif
         coeff=1.0D-4*Na/(T*Rgp)*(P1-P2)/dlog(P1/P2) ! molec.cm^-2.m^-1
         k=sigma*coeff          ! [cm^2/molec]*[molec.cm^-2.m^-1]=m^-1
      endif

      return
      end



      subroutine sigma2ka_local(sigma,T,P,k)
      implicit none
      include 'max.inc'
      include 'constants.inc'
c
c     Purpose: to convert a cross section to an extinction coefficient
c     at a given pressure level
c
c     Input:
c       + sigma: cross section (cm^2/molecule)
c       + T: gas temperature (K)
c       + P: pressure (Pa)
c
c     Output:
c       + k: extinction coefficient (m^-1)
c
c     I/O
      double precision sigma
      double precision T
      double precision P
      double precision k
c     temp
      double precision coeff
c     label
      integer strlen
      character*(Nchar_mx) label
      label='subroutine sigma2ka_local'

      if (sigma.eq.0.0D+0) then
         k=0.0D+0
      else
         if (T.eq.0.0D+0) then
            call error(label)
            write(*,*) 'T=',T
            stop
         endif
         coeff=1.0D-4*Na*P/(T*Rgp) ! molec.cm^-2.m^-1
         k=sigma*coeff          ! [cm^2/molec]*[molec.cm^-2.m^-1]=m^-1
      endif

      return
      end
