Index: trunk/LMDZ.GENERIC/README
===================================================================
--- trunk/LMDZ.GENERIC/README	(revision 2278)
+++ trunk/LMDZ.GENERIC/README	(revision 2283)
@@ -1525,2 +1525,11 @@
 - Get rid of "zerophys"; modern Fortran syntax is better to initialize an
   array.
+
+== 09/04/2020 (r2283) == JVO
++ Set the temperature boundaries and step for Planck function
+  integration as input in callphys.def, for more flexibility.
+  + User can now set them by tplanckmin, tplanckmax and dtplanck
+  + Default values are a wide range 30-1500K
+  + Add a sanity check in callcorrk instead of leaving out-of-bounds planckir indexes.
+
+
Index: trunk/LMDZ.GENERIC/libf/phystd/callcorrk.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/callcorrk.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/callcorrk.F90	(revision 2283)
@@ -32,5 +32,6 @@
       use comcstfi_mod, only: pi, mugaz, cpp
       use callkeys_mod, only: varactive,diurnal,tracer,water,varfixed,satval,diagdtau,    &
-                              kastprof,strictboundcorrk,specOLR,CLFvarying
+                              kastprof,strictboundcorrk,specOLR,CLFvarying,               &
+                              tplanckmin,tplanckmax
       use optcv_mod, only: optcv
       use optci_mod, only: optci
@@ -739,5 +740,9 @@
 
       ! Test for out-of-bounds temperature.
+      ! -- JVO 20 : Also add a sanity test checking that tlevrad is
+      !             within Planck function temperature boundaries,
+      !             which would cause gfluxi/sfluxi to crash.
       do k=1,L_LEVELS
+
          if(tlevrad(k).lt.tgasmin)then
             print*,'Minimum temperature is outside the radiative'
@@ -773,5 +778,23 @@
             endif
          endif
+
+         if (tlevrad(k).lt.tplanckmin) then
+            print*,'Minimum temperature is outside the boundaries for'
+            print*,'Planck function integration set in callphys.def, aborting.'
+            print*,"k=",k," tlevrad(k)=",tlevrad(k)
+            print*,"tplanckmin=",tplanckmin
+            message="Minimum temperature outside Planck function bounds - Change tplanckmin in callphys.def"
+            call abort_physic(subname,message,1)
+          else if (tlevrad(k).gt.tplanckmax) then
+            print*,'Maximum temperature is outside the boundaries for'
+            print*,'Planck function integration set in callphys.def, aborting.'
+            print*,"k=",k," tlevrad(k)=",tlevrad(k)
+            print*,"tplanckmax=",tplanckmax
+            message="Maximum temperature outside Planck function bounds - Change tplanckmax in callphys.def"
+            call abort_physic(subname,message,1)
+          endif
+
       enddo
+
       do k=1,L_NLAYRAD+1
          if(tmid(k).lt.tgasmin)then
Index: trunk/LMDZ.GENERIC/libf/phystd/callkeys_mod.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/callkeys_mod.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/callkeys_mod.F90	(revision 2283)
@@ -69,4 +69,8 @@
 !$OMP THREADPRIVATE(versH2H2cia,iddist,iaervar,iradia,startype)
 
+      real,save :: tplanckmin
+      real,save :: tplanckmax
+      real,save :: dtplanck
+!$OMP THREADPRIVATE(tplanckmin,tplanckmax,dtplanck)
       real,save :: topdustref
       real,save :: Nmix_co2
Index: trunk/LMDZ.GENERIC/libf/phystd/gfluxi.F
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/gfluxi.F	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/gfluxi.F	(revision 2283)
@@ -85,6 +85,6 @@
          LAMDA(L) = ALPHA(L)*(1.0D0-W0(L)*COSBAR(L))/UBARI
          
-         NT    = int(TLEV(2*L)*NTfac)   - NTstar+1
-         NT2   = int(TLEV(2*L+2)*NTfac) - NTstar+1
+         NT    = int(TLEV(2*L)*NTfac)   - NTstart+1
+         NT2   = int(TLEV(2*L+2)*NTfac) - NTstart+1
          
 ! AB : PLANCKIR(NW,NT) is replaced by P1, the linear interpolation result for a temperature NT
@@ -112,13 +112,13 @@
       ! -- same results for most thin atmospheres
       ! -- and stabilizes integrations
-      NT    = int(TLEV(2*L+1)*NTfac) - NTstar+1
+      NT    = int(TLEV(2*L+1)*NTfac) - NTstart+1
       !! For deep, opaque, thick first layers (e.g. Saturn)
       !! what is below works much better, not unstable, ...
       !! ... and actually fully accurate because 1st layer temp (JL) 
-      !NT    = int(TLEV(2*L)*NTfac) - NTstar+1
+      !NT    = int(TLEV(2*L)*NTfac) - NTstart+1
       !! (or this one yields same results
-      !NT    = int( (TLEV(2*L)+TLEV(2*L+1))*0.5*NTfac ) - NTstar+1
-      
-      NT2   = int(TLEV(2*L)*NTfac)   - NTstar+1
+      !NT    = int( (TLEV(2*L)+TLEV(2*L+1))*0.5*NTfac ) - NTstart+1
+      
+      NT2   = int(TLEV(2*L)*NTfac)   - NTstart+1
       
 ! AB : PLANCKIR(NW,NT) is replaced by P1, the linear interpolation result for a temperature NT
Index: trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90	(revision 2283)
@@ -233,4 +233,19 @@
      write(*,*) "strictboundcorrk = ",strictboundcorrk
 
+     write(*,*) "Minimum atmospheric temperature for Planck function integration ?"
+     tplanckmin=30.0 ! default value
+     call getin_p("tplanckmin",tplanckmin)
+     write(*,*) " tplanckmin = ",tplanckmin
+ 
+     write(*,*) "Maximum atmospheric temperature for Planck function integration ?"
+     tplanckmax=1500.0 ! default value
+     call getin_p("tplanckmax",tplanckmax)
+     write(*,*) " tplanckmax = ",tplanckmax
+ 
+     write(*,*) "Temperature step for Planck function integration ?"
+     dtplanck=0.1 ! default value
+     call getin_p("dtplanck",dtplanck)
+     write(*,*) " dtplanck = ",dtplanck
+ 
      write(*,*) "call gaseous absorption in the visible bands?", &
                     "(matters only if callrad=T)"
@@ -809,5 +824,5 @@
 #endif
   ! initialize variables in radinc_h
-  call ini_radinc_h(nlayer)
+  call ini_radinc_h(nlayer,tplanckmin,tplanckmax,dtplanck)
  
   ! allocate "comsoil_h" arrays
Index: trunk/LMDZ.GENERIC/libf/phystd/radcommon_h.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/radcommon_h.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/radcommon_h.F90	(revision 2283)
@@ -1,4 +1,4 @@
 module radcommon_h
-      use radinc_h, only: L_NSPECTI, L_NSPECTV, NTstar, NTstop, &
+      use radinc_h, only: L_NSPECTI, L_NSPECTV, NTstart, NTstop, &
                           naerkind, nsizemax
       implicit none
@@ -123,5 +123,5 @@
       REAL,SAVE :: tstellar ! Stellar brightness temperature (SW)
 
-      real*8,save :: planckir(L_NSPECTI,NTstop-NTstar+1)
+      REAL*8, DIMENSION(:,:), ALLOCATABLE, SAVE :: planckir
 
       real*8,save :: PTOP
Index: trunk/LMDZ.GENERIC/libf/phystd/radinc_h.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/radinc_h.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/radinc_h.F90	(revision 2283)
@@ -72,14 +72,13 @@
 
       ! For Planck function integration: 
-      ! equivalent temperatures are 1/NTfac of these values
-      integer, parameter :: NTstar = 500
-      integer, parameter :: NTstop = 15000 ! new default for all non hot Jupiter runs
-      real*8, parameter :: NTfac = 1.0D+1  
-      !integer, parameter :: NTstar = 1000
-      !integer, parameter :: NTstop = 25000
-      !real*8,parameter :: NTfac = 5.0D+1    
-      !integer, parameter :: NTstar = 2000
-      !integer, parameter :: NTstop = 50000
-      !real*8,parameter :: NTfac = 1.0D+2    
+      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+      ! Integration boundary temperatures are NTstart/NTfac and Ntstop/NTfac
+      ! -- JVO 20 : Now read boundary T and integration dT as inputs in callphys.def
+      !             NTstart, Nstop and NTfac then set by ini_radinc_h
+      !             Smart user can adjust values depending he's running hot or cold atm
+      !             Default is wide range : 30K-1500K, with 0.1K step
+      !             ->  NTstart=300, Nstop=15000, NTfac=10
+      integer :: NTstart, NTstop
+      real*8  :: NTfac
 
       ! Maximum number of grain size classes for aerosol convolution:
@@ -96,13 +95,20 @@
 contains
 
-  subroutine ini_radinc_h(nbp_lev)
+  subroutine ini_radinc_h(nbp_lev,tplanckmin,tplanckmax,dtplanck)
   ! Initialize module variables
   implicit none
   integer,intent(in) :: nbp_lev
+  real*8, intent(in) :: tplanckmin
+  real*8, intent(in) :: tplanckmax
+  real*8, intent(in) :: dtplanck
   
   L_NLAYRAD = nbp_lev
-  L_LEVELS = 2*(nbp_lev-1)+3
+  L_LEVELS  = 2*(nbp_lev-1)+3
   L_NLEVRAD = nbp_lev+1
-  
+
+  NTfac   = 1.D0 / dtplanck
+  NTstart = int(tplanckmin * NTfac)
+  NTstop  = int(tplanckmax * NTfac)
+ 
   end subroutine
 
Index: trunk/LMDZ.GENERIC/libf/phystd/setspi.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/setspi.F90	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/setspi.F90	(revision 2283)
@@ -22,5 +22,5 @@
 !==================================================================
 
-      use radinc_h,    only: L_NSPECTI,corrkdir,banddir,NTstar,NTstop,NTfac
+      use radinc_h,    only: L_NSPECTI,corrkdir,banddir,NTstart,NTstop,NTfac
       use radcommon_h, only: BWNI,BLAMI,WNOI,DWNI,WAVEI,planckir,sigma
       use datafile_mod, only: datadir
@@ -149,5 +149,7 @@
       print*,''
       print*,'setspi: Current Planck integration range:'
-      print*,'T = ',dble(NTstar)/NTfac, ' to ',dble(NTstop)/NTfac,' K.'
+      print*,'T = ',dble(NTstart)/NTfac, ' to ',dble(NTstop)/NTfac,' K.'
+
+      IF(.NOT.ALLOCATED(planckir)) ALLOCATE(planckir(L_NSPECTI,NTstop-NTstart+1))
 
       do NW=1,L_NSPECTI
@@ -156,5 +158,5 @@
          bpa = (b+a)/2.0D0
          bma = (b-a)/2.0D0
-         do nt=NTstar,NTstop
+         do nt=NTstart,NTstop
             T   = dble(NT)/NTfac
             ans = 0.0D0
@@ -165,5 +167,5 @@
             end do
 
-            planckir(NW,nt-NTstar+1) = ans*bma/(PI*DWNI(NW))
+            planckir(NW,nt-NTstart+1) = ans*bma/(PI*DWNI(NW))
          end do
       end do
@@ -172,5 +174,5 @@
       if(forceEC)then
          print*,'setspi: Force F=sigma*eps*T^4 for all values of T!'
-         do nt=NTstar,NTstop
+         do nt=NTstart,NTstop
             plancksum=0.0D0
             T=dble(NT)/NTfac
@@ -178,10 +180,10 @@
             do NW=1,L_NSPECTI
                plancksum=plancksum+  &
-                  planckir(NW,nt-NTstar+1)*DWNI(NW)*pi
+                  planckir(NW,nt-NTstart+1)*DWNI(NW)*pi
             end do
 
             do NW=1,L_NSPECTI
-               planckir(NW,nt-NTstar+1)=     &
-                  planckir(NW,nt-NTstar+1)*  &
+               planckir(NW,nt-NTstart+1)=     &
+                  planckir(NW,nt-NTstart+1)*  &
                           sigma*(dble(nt)/NTfac)**4/plancksum
             end do
@@ -192,7 +194,7 @@
          ! check energy conservation at lower temperature boundary
          plancksum=0.0D0
-         nt=NTstar
+         nt=NTstart
          do NW=1,L_NSPECTI
-            plancksum=plancksum+planckir(NW,nt-NTstar+1)*DWNI(NW)*pi
+            plancksum=plancksum+planckir(NW,nt-NTstart+1)*DWNI(NW)*pi
          end do
          print*,'setspi: At lower limit:'
@@ -204,5 +206,5 @@
          nt=NTstop
          do NW=1,L_NSPECTI
-            plancksum=plancksum+planckir(NW,nt-NTstar+1)*DWNI(NW)*pi
+            plancksum=plancksum+planckir(NW,nt-NTstart+1)*DWNI(NW)*pi
          end do
          print*,'setspi: At upper limit:'
Index: trunk/LMDZ.GENERIC/libf/phystd/sfluxi.F
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/sfluxi.F	(revision 2278)
+++ trunk/LMDZ.GENERIC/libf/phystd/sfluxi.F	(revision 2283)
@@ -67,6 +67,6 @@
       TSURF = TLEV(L_LEVELS)
 
-      NTS   = int(TSURF*NTfac)-NTstar+1
-      NTT   = int(TTOP *NTfac)-NTstar+1
+      NTS   = int(TSURF*NTfac)-NTstart+1
+      NTT   = int(TTOP *NTfac)-NTstart+1
 
 !JL12 corrects the surface planck function so that its integral is equal to sigma Tsurf^4
