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
      double precision function dBdT_int(T,lambda_lo,lambda_hi)
      implicit none
      include 'max.inc'
      include 'param.inc'
c
c     Purpose: to compute the sensitivity of the Planck function (at a given wavelength)
c     to temperature, integrated over a given wavelength interval.
c
c     Input:
c       + T: temperature (K)
c       + lambda_lo: lower wavelength (µm)
c       + lambda_hi: higher wavelength (µm)
c
c     Output:
c       + dBdT_int: sensitivity of B(lambda,T) to temperature, integrated over [lambda_lo,lambda_hi] (W/m²/sr/µm/K)
c
c     I/O
      double precision T
      double precision lambda_lo,lambda_hi
c     temp
      integer i,n
      double precision lambda
      double precision dBdT1,dBdT2
      double precision integral
c     functions
      double precision dBdT
c     label
      integer strlen
      character*(Nchar_mx) label
      label='function dBdT_int'
      
      n=100
      integral=0.0D+0
      do i=0,n
         dBdT1=dBdT2
         lambda=lambda_lo+(lambda_hi-lambda_lo)/dble(n)*dble(i)
         dBdT2=dBdT(T,lambda)
         if (i.gt.0) then
            integral=integral+(dBdT2+dBdT1)
         endif
      enddo ! i
      dBdT_int=integral/(2.0D+0*dble(n))

      return
      end



      double precision function dBdT(T,lambda)
      implicit none
      include 'max.inc'
      include 'param.inc'
c
c     Purpose: to compute the sensitivity of the Planck function (at a given wavelength)
c     to temperature.
c
c     Input:
c       + T: temperature (K)
c       + lambda: wavelength (µm)
c
c     Output:
c       + dBdT: sensitivity of B(lambda,T) to temperature, at (lambda,T) (W/m²/sr/µm/K)
c
c     I/O
      double precision T
      double precision lambda
c     temp
      double precision in,c1,c2
c     label
      integer strlen
      character*(Nchar_mx) label
      label='function dBdT'

      c1=2*pi*hPl*c0**2 ! W.m²
      c2=hPl*c0/kBz*1.D+6 ! µm.K
c     ATTENTION ! lambda doit être entrée en micromètres !

c      write(*,*) 'c1=',c1,'W.m2'
c      write(*,*) 'c2=',c2,'µm.K'

      if (T.lt.6.0D+0) then
         dBdT=0.0D+0
      else
         dBdT=1.0D+24/pi*c1*c2*dexp(c2/(T*lambda))/
     &        (lambda**6*T**2*(dexp(c2/
     &        (T*lambda))-1.0D+0)**2)
      endif

      return
      end
