source: trunk/LMDZ.TITAN/libf/phytitan/callcorrk.F90 @ 3094

Last change on this file since 3094 was 3090, checked in by slebonnois, 15 months ago

BdeBatz? : Cleans microphysics and makes few corrections for physics

  • Property svn:executable set to *
File size: 41.0 KB
Line 
1      subroutine callcorrk(ngrid,nlayer,pq,nq,qsurf,zday,      &
2          albedo,albedo_equivalent,emis,mu0,pplev,pplay,zzlev, &
3          pt,tsurf,fract,dist_star,                            &
4          dtlw,dtsw,fluxsurf_lw,                               &
5          fluxsurf_sw,fluxsurfabs_sw,fluxtop_lw,               &
6          fluxabs_sw,fluxtop_dn,                               &
7          OLR_nu,OSR_nu,                                       &
8          int_dtaui,int_dtauv,popthi,popthv,poptci,poptcv,     &
9          lastcall)
10
11      use mod_phys_lmdz_para, only : is_master
12      use radinc_h
13      use radcommon_h
14      use gases_h
15      USE tracer_h
16      use callkeys_mod, only: global1d, szangle
17      use comcstfi_mod, only: pi, mugaz, cpp
18      use callkeys_mod, only: diurnal,tracer,seashaze,corrk_recombin,   &
19                              strictboundcorrk,specOLR,diagdtau,        &
20                              tplanckmin,tplanckmax,callclouds,Fcloudy
21      use geometry_mod, only: latitude
22
23      implicit none
24
25!==================================================================
26!
27!     Purpose
28!     -------
29!     Solve the radiative transfer using the correlated-k method for
30!     the gaseous absorption and the Toon et al. (1989) method for
31!     scatttering due to aerosols.
32!
33!     Authors
34!     -------
35!     Emmanuel 01/2001, Forget 09/2001
36!     Robin Wordsworth (2009)
37!     
38!     Modified
39!     --------
40!     Jan Vatant d'Ollone (2018)
41!        --> corrk recombining case
42!     B. de Batz de Trenquelléon (2023)
43!        --> Titan's haze and could optics
44!        --> clear/dark columns method
45!        --> optical diagnostics
46!
47!==================================================================
48
49!-----------------------------------------------------------------------
50!     Declaration of the arguments (INPUT - OUTPUT) on the LMD GCM grid
51!     Layer #1 is the layer near the ground.
52!     Layer #nlayer is the layer at the top.
53!-----------------------------------------------------------------------
54
55
56      ! INPUT
57      INTEGER,INTENT(IN) :: ngrid                  ! Number of atmospheric columns.
58      INTEGER,INTENT(IN) :: nlayer                 ! Number of atmospheric layers.
59      REAL,INTENT(IN) :: pq(ngrid,nlayer,nq)       ! Tracers (X/kg).
60      INTEGER,INTENT(IN) :: nq                     ! Number of tracers.
61      REAL,INTENT(IN) :: qsurf(ngrid,nq)           ! Tracers on surface (kg.m-2).
62      REAL,INTENT(IN) :: zday                      ! Time elapsed since Ls=0 (sols).
63      REAL,INTENT(IN) :: albedo(ngrid,L_NSPECTV)   ! Spectral Short Wavelengths Albedo. By MT2015
64      REAL,INTENT(IN) :: emis(ngrid)               ! Long Wave emissivity.
65      REAL,INTENT(IN) :: mu0(ngrid)                ! Cosine of sun incident angle.
66      REAL,INTENT(IN) :: pplev(ngrid,nlayer+1)     ! Inter-layer pressure (Pa).
67      REAL,INTENT(IN) :: pplay(ngrid,nlayer)       ! Mid-layer pressure (Pa).
68      REAL,INTENT(IN) :: zzlev(ngrid,nlayer+1)     ! Altitude at the atmospheric layer boundaries (ref : local surf).
69      REAL,INTENT(IN) :: pt(ngrid,nlayer)          ! Air temperature (K).
70      REAL,INTENT(IN) :: tsurf(ngrid)              ! Surface temperature (K).
71      REAL,INTENT(IN) :: fract(ngrid)              ! Fraction of day.
72      REAL,INTENT(IN) :: dist_star                 ! Distance star-planet (AU).
73      logical,intent(in) :: lastcall               ! Signals last call to physics.
74     
75      ! OUTPUT
76      REAL,INTENT(OUT) :: dtlw(ngrid,nlayer)             ! Heating rate (K/s) due to LW radiation.
77      REAL,INTENT(OUT) :: dtsw(ngrid,nlayer)             ! Heating rate (K/s) due to SW radiation.
78      REAL,INTENT(OUT) :: fluxsurf_lw(ngrid)             ! Incident LW flux to surf (W/m2).
79      REAL,INTENT(OUT) :: fluxsurf_sw(ngrid)             ! Incident SW flux to surf (W/m2)
80      REAL,INTENT(OUT) :: fluxsurfabs_sw(ngrid)          ! Absorbed SW flux by the surface (W/m2). By MT2015.
81      REAL,INTENT(OUT) :: fluxtop_lw(ngrid)              ! Outgoing LW flux to space (W/m2).
82      REAL,INTENT(OUT) :: fluxabs_sw(ngrid)              ! SW flux absorbed by the planet (W/m2).
83      REAL,INTENT(OUT) :: fluxtop_dn(ngrid)              ! Incident top of atmosphere SW flux (W/m2).
84      REAL,INTENT(OUT) :: OLR_nu(ngrid,L_NSPECTI)        ! Outgoing LW radition in each band (Normalized to the band width (W/m2/cm-1).
85      REAL,INTENT(OUT) :: OSR_nu(ngrid,L_NSPECTV)        ! Outgoing SW radition in each band (Normalized to the band width (W/m2/cm-1).
86      REAL,INTENT(OUT) :: albedo_equivalent(ngrid)       ! Spectrally Integrated Albedo. For Diagnostic. By MT2015
87      REAL,INTENT(OUT) :: int_dtaui(ngrid,nlayer,L_NSPECTI) ! IR optical thickness of layers within narrowbands for diags ().
88      REAL,INTENT(OUT) :: int_dtauv(ngrid,nlayer,L_NSPECTV) ! VI optical thickness of layers within narrowbands for diags ().
89      ! Diagnostics
90      REAL,INTENT(OUT) :: popthi(ngrid,nlayer,L_NSPECTI,5)  ! IR optical properties of haze within narrowbands (dtau,tau,k,w,g).
91      REAL,INTENT(OUT) :: popthv(ngrid,nlayer,L_NSPECTV,5)  ! VI optical properties of haze within narrowbands (dtau,tau,k,w,g).
92      REAL,INTENT(OUT) :: poptci(ngrid,nlayer,L_NSPECTI,5)  ! IR optical properties of haze and clouds within narrowbands (dtau,tau,k,w,g).
93      REAL,INTENT(OUT) :: poptcv(ngrid,nlayer,L_NSPECTV,5)  ! VI optical properties of haze and clouds within narrowbands (dtau,tau,k,w,g).
94     
95     
96     
97!-----------------------------------------------------------------------
98!     Declaration of the variables required by correlated-k subroutines
99!     Numbered from top to bottom (unlike in the GCM)
100!-----------------------------------------------------------------------
101
102      REAL*8 tmid(L_LEVELS),pmid(L_LEVELS)
103      REAL*8 tlevrad(L_LEVELS),plevrad(L_LEVELS)
104
105      ! Optical values for the optci/cv subroutines
106      REAL*8 stel(L_NSPECTV),stel_fract(L_NSPECTV)
107      ! IR
108      REAL*8 dtaui(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
109      REAL*8 taucumi(L_LEVELS,L_NSPECTI,L_NGAUSS)
110      REAL*8 wbari(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
111      REAL*8 cosbi(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
112      ! VI
113      REAL*8 dtauv(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
114      REAL*8 tauv(L_NLEVRAD,L_NSPECTV,L_NGAUSS)
115      REAL*8 taucumv(L_LEVELS,L_NSPECTV,L_NGAUSS)
116      REAL*8 wbarv(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
117      REAL*8 cosbv(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
118     
119      ! Temporary optical values for the optci/cv subroutines (clear column)
120      REAL*8 dtaui_cc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
121      REAL*8 dtauv_cc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
122      REAL*8 tauv_cc(L_NLEVRAD,L_NSPECTV,L_NGAUSS)
123      REAL*8 taucumi_cc(L_LEVELS,L_NSPECTI,L_NGAUSS)
124      REAL*8 taucumv_cc(L_LEVELS,L_NSPECTV,L_NGAUSS)
125      REAL*8 wbari_cc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
126      REAL*8 wbarv_cc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
127      REAL*8 cosbi_cc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
128      REAL*8 cosbv_cc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
129      ! Temporary optical values for the optci/cv subroutines (dark column)
130      REAL*8 dtaui_dc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
131      REAL*8 dtauv_dc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
132      REAL*8 tauv_dc(L_NLEVRAD,L_NSPECTV,L_NGAUSS)
133      REAL*8 taucumi_dc(L_LEVELS,L_NSPECTI,L_NGAUSS)
134      REAL*8 taucumv_dc(L_LEVELS,L_NSPECTV,L_NGAUSS)
135      REAL*8 wbari_dc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
136      REAL*8 wbarv_dc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
137      REAL*8 cosbi_dc(L_NLAYRAD,L_NSPECTI,L_NGAUSS)
138      REAL*8 cosbv_dc(L_NLAYRAD,L_NSPECTV,L_NGAUSS)
139 
140      ! Optical diagnostics
141      REAL*8 popt_hazei(L_LEVELS,L_NSPECTI,3)
142      REAL*8 popt_hazev(L_LEVELS,L_NSPECTV,3)
143      REAL*8 popt_cloudsi(L_LEVELS,L_NSPECTI,3)
144      REAL*8 popt_cloudsv(L_LEVELS,L_NSPECTV,3)
145      ! Temporary optical diagnostics (clear column)
146      REAL*8 popt_hazei_cc(L_LEVELS,L_NSPECTI,3)
147      REAL*8 popt_hazev_cc(L_LEVELS,L_NSPECTV,3)
148      REAL*8 popt_cloudsi_cc(L_LEVELS,L_NSPECTI,3)
149      REAL*8 popt_cloudsv_cc(L_LEVELS,L_NSPECTV,3)
150      ! Temporary optical diagnostics (dark column)
151      REAL*8 popt_hazei_dc(L_LEVELS,L_NSPECTI,3)
152      REAL*8 popt_hazev_dc(L_LEVELS,L_NSPECTV,3)
153      REAL*8 popt_cloudsi_dc(L_LEVELS,L_NSPECTI,3)
154      REAL*8 popt_cloudsv_dc(L_LEVELS,L_NSPECTV,3)
155
156
157      REAL*8 nfluxtopv,nfluxtopi,nfluxtop,fluxtopvdn
158      REAL*8 nfluxoutv_nu(L_NSPECTV)                 ! Outgoing band-resolved VI flux at TOA (W/m2).
159      REAL*8 nfluxtopi_nu(L_NSPECTI)                 ! Net band-resolved IR flux at TOA (W/m2).
160      REAL*8 fluxupi_nu(L_NLAYRAD,L_NSPECTI)         ! For 1D diagnostic.
161      REAL*8 fmneti(L_NLAYRAD),fmnetv(L_NLAYRAD)
162      REAL*8 fluxupv(L_NLAYRAD),fluxupi(L_NLAYRAD)
163      REAL*8 fluxdnv(L_NLAYRAD),fluxdni(L_NLAYRAD)
164      REAL*8 albi,acosz
165      REAL*8 albv(L_NSPECTV)                         ! Spectral Visible Albedo.
166
167      INTEGER ig,l,k,nw,iq,ip,ilay,it,lev2lay,cdcolumn,ng
168     
169      LOGICAL found
170
171      real*8 taugsurf(L_NSPECTV,L_NGAUSS-1)
172      real*8 taugsurfi(L_NSPECTI,L_NGAUSS-1)
173
174      ! Miscellaneous :
175      character(len=100) :: message
176      character(len=10),parameter :: subname="callcorrk"
177
178      logical OLRz
179      real*8 NFLUXGNDV_nu(L_NSPECTV)
180   
181      ! Included by MT for albedo calculations.     
182      REAL*8 albedo_temp(L_NSPECTV) ! For equivalent albedo calculation.
183      REAL*8 surface_stellar_flux   ! Stellar flux reaching the surface. Useful for equivalent albedo calculation.
184     
185      ! For variable haze
186      REAL*8 seashazefact(L_LEVELS)
187
188      ! For muphys optics
189      REAL*8 pqmo(ngrid,nlayer,nmicro)  ! Tracers for microphysics optics (X/m2).
190      REAL*8 i2e(ngrid,nlayer)          ! int 2 ext factor ( X.kg-1 -> X.m-2 for optics )
191     
192      ! For corr-k recombining
193      REAL*8 pqr(ngrid,L_PINT,L_REFVAR)  ! Tracers for corr-k recombining (mol/mol).
194      REAL*8 fact, tmin, tmax
195     
196      LOGICAL usept(L_PINT,L_NTREF)  ! mask if pfref grid point will be used
197      INTEGER inflay(L_PINT)         ! nearest inferior GCM layer for pfgasref grid points
198     
199!=======================================================================
200!             I.  Initialization on every call   
201!=======================================================================
202 
203
204      ! How much light do we get ?
205      do nw=1,L_NSPECTV
206         stel(nw)=stellarf(nw)/(dist_star**2)
207      end do
208
209      ! Convert (microphysical) tracers for optics: X.kg-1 --> X.m-2
210      ! NOTE: it should be moved somewhere else: calmufi performs the same kind of
211      ! computations... waste of time...
212      i2e(:,1:nlayer) = ( pplev(:,1:nlayer)-pplev(:,2:nlayer+1) ) / gzlat(:,1:nlayer)
213      pqmo(:,:,:) = 0.0
214      DO iq=1,nmicro
215        pqmo(:,:,iq) = pq(:,:,iq)*i2e(:,:)
216      ENDDO
217
218      ! Default value for fixed species for whom vmr has been taken
219      ! into account while computing high-resolution spectra
220      if (corrk_recombin) pqr(:,:,:) = 1.0
221
222!-----------------------------------------------------------------------   
223      do ig=1,ngrid ! Starting Big Loop over every GCM column
224!-----------------------------------------------------------------------
225         
226         ! Recombine reference corr-k if needed
227         if (corrk_recombin) then
228         
229         ! NB : To have decent CPU time recombining is not done on all gridpoints and wavelenghts but we
230         ! calculate a gasi/v_recomb variable on the reference corrk-k T,P grid (only for T,P values
231         ! who match the atmospheric conditions ) which is then processed as a standard pre-mix in
232         ! optci/v routines, but updated every time tracers on the ref P grid have varied > 1%.
233
234            ! Extract tracers for variable radiative species
235            ! Also find the nearest GCM layer under each ref pressure
236            do ip=1,L_PINT
237               
238               ilay=0
239               found = .false.
240               do l=1,nlayer
241                  if ( pplay(ig,l) .gt. 10.0**(pfgasref(ip)+2.0) ) then ! pfgasref=log(p[mbar])
242                     found=.true.
243                     ilay=l
244                  endif
245               enddo       
246
247               if (.not. found ) then ! set to min
248                  do iq=1,L_REFVAR
249                     if ( radvar_mask(iq) ) then
250                        pqr(ig,ip,iq) = pq(ig,1,radvar_indx(iq)) / rat_mmol(radvar_indx(iq)-nmicro) ! mol/mol
251                     endif
252                  enddo
253               else
254                  if (ilay==nlayer) then ! set to max
255                     do iq=1,L_REFVAR
256                        if ( radvar_mask(iq) ) then
257                           pqr(ig,ip,iq) = pq(ig,nlayer,radvar_indx(iq)) / rat_mmol(radvar_indx(iq)-nmicro) ! mol/mol
258                        endif
259                     enddo
260                  else ! standard
261                     fact = ( 10.0**(pfgasref(ip)+2.0) - pplay(ig,ilay+1) ) / ( pplay(ig,ilay) - pplay(ig,ilay+1) ) ! pfgasref=log(p[mbar])
262                     do iq=1,L_REFVAR
263                        if ( radvar_mask(iq) ) then
264                           pqr(ig,ip,iq) = pq(ig,ilay,radvar_indx(iq))**fact * pq(ig,ilay+1,radvar_indx(iq))**(1.0-fact)
265                           pqr(ig,ip,iq) = pqr(ig,ip,iq) / rat_mmol(radvar_indx(iq)-nmicro) ! mol/mol
266                        endif
267                     enddo
268                  endif ! if ilay==nlayer
269               endif ! if not found
270
271               inflay(ip) = ilay
272
273            enddo ! ip=1,L_PINT
274
275            ! NB : The following usept is a trick to call recombine only for the reference T-P
276            ! grid points that are useful given the temperature range at this altitude
277            ! It saves a looot of time - JVO 18
278            usept(:,:) = .true.
279            do ip=1,L_PINT-1
280              if ( inflay(ip+1)==nlayer ) then
281                usept(ip,:) = .false.
282              endif
283              if ( inflay(ip) == 0 ) then
284                usept(ip+1:,:) = .false.
285              endif
286              if ( usept(ip,1) ) then ! if not all false
287                tmin = minval(pt(ig,max(inflay(ip+1)+1,1):min(inflay(ip)+1,nlayer)))
288                tmax = maxval(pt(ig,max(inflay(ip+1)+1,1):min(inflay(ip)+1,nlayer)))
289                do it=1,L_NTREF-1
290                  if ( tgasref(it+1) .lt. tmin ) then
291                    usept(ip,it) = .false.
292                  endif
293                enddo
294                do it=2,L_NTREF
295                  if ( tgasref(it-1) .gt. tmax ) then
296                    usept(ip,it) = .false.
297                  endif
298                enddo
299                ! in case of out-of-bounds
300                if ( tgasref(1)         .lt. tmin ) usept(ip,1) = .true.
301                if ( tgasref(L_NTREF)   .gt. tmax ) usept(ip,L_NTREF) = .true.
302              endif
303            enddo ! ip=1,L_PINT-1
304            ! deal with last bound
305            if ( inflay(L_PINT-1).ne.0 ) usept(L_PINT,:) = usept(L_PINT-1,:)
306
307
308            do ip=1,L_PINT
309
310               ! Recombine k at (useful only!) reference T-P values if tracers or T have enough varied
311               do it=1,L_NTREF
312
313                 if ( usept(ip,it) .eqv. .false. ) cycle
314
315                 do l=1,L_REFVAR
316                    if ( abs( (pqr(ig,ip,l) - pqrold(ip,l)) / max(1.0e-30,pqrold(ip,l))) .GT. 0.01  & ! +- 1%
317                         .or. ( useptold(ip,it) .eqv. .false.  ) ) then ! in case T change but not the tracers
318                       call recombin_corrk( pqr(ig,ip,:),ip,it )
319                       exit ! one is enough to trigger the update
320                    endif
321                 enddo
322                 
323               enddo
324
325            enddo ! ip=1,L_PINT
326
327            useptold(:,:)=usept(:,:)
328
329       endif ! if corrk_recombin
330
331!=======================================================================
332!              II.  Transformation of the GCM variables
333!=======================================================================
334
335
336         ! Albedo and Emissivity.
337         albi=1-emis(ig)   ! Long Wave.
338         DO nw=1,L_NSPECTV ! Short Wave loop.
339            albv(nw)=albedo(ig,nw)
340         ENDDO
341
342      if ((ngrid.eq.1).and.(global1d)) then ! Fixed zenith angle 'szangle' in 1D simulations w/ globally-averaged sunlight.
343         acosz = cos(pi*szangle/180.0)
344         print*,'acosz=',acosz,', szangle=',szangle
345      else
346         acosz=mu0(ig) ! Cosine of sun incident angle : 3D simulations or local 1D simulations using latitude.
347      endif
348
349!-----------------------------------------------------------------------
350!     Pressure and temperature
351!-----------------------------------------------------------------------
352
353      DO l=1,nlayer
354         plevrad(2*l)   = pplay(ig,nlayer+1-l)/scalep
355         plevrad(2*l+1) = pplev(ig,nlayer+1-l)/scalep
356         tlevrad(2*l)   = pt(ig,nlayer+1-l)
357         tlevrad(2*l+1) = (pt(ig,nlayer+1-l)+pt(ig,max(nlayer-l,1)))/2
358      END DO
359     
360      plevrad(1) = 0.
361      plevrad(2) = 0.   !! Trick to have correct calculations of fluxes in gflux(i/v).F, but the pmid levels are not impacted by this change.
362
363      tlevrad(1) = tlevrad(2)
364      tlevrad(2*nlayer+1)=tsurf(ig)
365     
366      pmid(1) = max(pgasmin,0.0001*plevrad(3))
367      pmid(2) =  pmid(1)
368
369      tmid(1) = tlevrad(2)
370      tmid(2) = tmid(1)
371   
372      DO l=1,L_NLAYRAD-1
373         tmid(2*l+1) = tlevrad(2*l+1)
374         tmid(2*l+2) = tlevrad(2*l+1)
375         pmid(2*l+1) = plevrad(2*l+1)
376         pmid(2*l+2) = plevrad(2*l+1)
377      END DO
378      pmid(L_LEVELS) = plevrad(L_LEVELS)
379      tmid(L_LEVELS) = tlevrad(L_LEVELS)
380
381!!Alternative interpolation:
382!         pmid(3) = pmid(1)
383!         pmid(4) = pmid(1)
384!         tmid(3) = tmid(1)
385!         tmid(4) = tmid(1)
386!      DO l=2,L_NLAYRAD-1
387!         tmid(2*l+1) = tlevrad(2*l)
388!         tmid(2*l+2) = tlevrad(2*l)
389!         pmid(2*l+1) = plevrad(2*l)
390!         pmid(2*l+2) = plevrad(2*l)
391!      END DO
392!      pmid(L_LEVELS) = plevrad(L_LEVELS-1)
393!      tmid(L_LEVELS) = tlevrad(L_LEVELS-1)
394
395      ! Test for out-of-bounds pressure.
396      if(plevrad(3).lt.pgasmin)then
397         print*,'Minimum pressure is outside the radiative'
398         print*,'transfer kmatrix bounds, exiting.'
399         call abort
400      elseif(plevrad(L_LEVELS).gt.pgasmax)then
401         print*,'Maximum pressure is outside the radiative'
402         print*,'transfer kmatrix bounds, exiting.'
403         call abort
404      endif
405
406      ! Test for out-of-bounds temperature.
407      do k=1,L_LEVELS
408         if(tlevrad(k).lt.tgasmin)then
409            print*,'Minimum temperature is outside the radiative'
410            print*,'transfer kmatrix bounds'
411            print*,"k=",k," tlevrad(k)=",tlevrad(k)
412            print*,"tgasmin=",tgasmin
413            if (strictboundcorrk) then
414              call abort
415            else
416              print*,'***********************************************'
417              print*,'we allow model to continue with tlevrad=tgasmin'
418              print*,'  ... we assume we know what you are doing ... '
419              print*,'  ... but do not let this happen too often ... '
420              print*,'***********************************************'
421              !tlevrad(k)=tgasmin
422            endif
423         elseif(tlevrad(k).gt.tgasmax)then
424!            print*,'Maximum temperature is outside the radiative'
425!            print*,'transfer kmatrix bounds, exiting.'
426!            print*,"k=",k," tlevrad(k)=",tlevrad(k)
427!            print*,"tgasmax=",tgasmax
428            if (strictboundcorrk) then
429              call abort
430            else
431!              print*,'***********************************************'
432!              print*,'we allow model to continue with tlevrad=tgasmax' 
433!              print*,'  ... we assume we know what you are doing ... '
434!              print*,'  ... but do not let this happen too often ... '
435!              print*,'***********************************************'
436              !tlevrad(k)=tgasmax
437            endif
438         endif
439
440         if (tlevrad(k).lt.tplanckmin) then
441            print*,'Minimum temperature is outside the boundaries for'
442            print*,'Planck function integration set in callphys.def, aborting.'
443            print*,"k=",k," tlevrad(k)=",tlevrad(k)
444            print*,"tplanckmin=",tplanckmin
445            message="Minimum temperature outside Planck function bounds - Change tplanckmin in callphys.def"
446            call abort_physic(subname,message,1)
447          else if (tlevrad(k).gt.tplanckmax) then
448            print*,'Maximum temperature is outside the boundaries for'
449            print*,'Planck function integration set in callphys.def, aborting.'
450            print*,"k=",k," tlevrad(k)=",tlevrad(k)
451            print*,"tplanckmax=",tplanckmax
452            message="Maximum temperature outside Planck function bounds - Change tplanckmax in callphys.def"
453            call abort_physic(subname,message,1)
454          endif
455
456      enddo
457
458      do k=1,L_NLAYRAD+1
459         if(tmid(k).lt.tgasmin)then
460            print*,'Minimum temperature is outside the radiative'
461            print*,'transfer kmatrix bounds, exiting.'
462            print*,"k=",k," tmid(k)=",tmid(k)
463            print*,"tgasmin=",tgasmin
464            if (strictboundcorrk) then
465              call abort
466            else
467              print*,'***********************************************'
468              print*,'we allow model to continue with tmid=tgasmin'
469              print*,'  ... we assume we know what you are doing ... '
470              print*,'  ... but do not let this happen too often ... '
471              print*,'***********************************************'
472              tmid(k)=tgasmin
473            endif
474         elseif(tmid(k).gt.tgasmax)then
475!            print*,'Maximum temperature is outside the radiative'
476!            print*,'transfer kmatrix bounds, exiting.'
477!            print*,"k=",k," tmid(k)=",tmid(k)
478!            print*,"tgasmax=",tgasmax
479            if (strictboundcorrk) then
480              call abort
481            else
482!              print*,'***********************************************'
483!              print*,'we allow model to continue with tmid=tgasmin'
484!              print*,'  ... we assume we know what you are doing ... '
485!              print*,'  ... but do not let this happen too often ... '
486!              print*,'***********************************************'
487              tmid(k)=tgasmax
488            endif
489         endif
490      enddo
491
492!=======================================================================
493!          III. Calling the main radiative transfer subroutines
494!=======================================================================
495
496         Cmk(:)      = 0.01 * 1.0 / (gzlat(ig,:) * mugaz * 1.672621e-27) ! q_main=1.0 assumed.
497         gzlat_ig(:) = gzlat(ig,:)
498         
499         ! Compute the haze seasonal modulation factor
500         if (seashaze) call season_haze(zday,latitude(ig),plevrad,seashazefact)
501
502!-----------------------------------------------------------------------
503!        Short Wave Part
504!-----------------------------------------------------------------------
505
506         ! Clear column :
507         cdcolumn = 0
508         call optcv(pqmo(ig,:,:),nlayer,zzlev(ig,:),plevrad,tmid,pmid,    &
509              dtauv_cc,tauv_cc,taucumv_cc,wbarv_cc,cosbv_cc,tauray,taugsurf,seashazefact,&
510              popt_hazev_cc,popt_cloudsv_cc,cdcolumn)
511         ! Dark column :
512         cdcolumn = 1
513         call optcv(pqmo(ig,:,:),nlayer,zzlev(ig,:),plevrad,tmid,pmid,    &
514              dtauv_dc,tauv_dc,taucumv_dc,wbarv_dc,cosbv_dc,tauray,taugsurf,seashazefact,&
515              popt_hazev_dc,popt_cloudsv_dc,cdcolumn)
516         
517         ! Mean opacity, ssa and asf :
518         where ((exp(-dtauv_cc(:,:,:)).ge.1.d-40) .and. (exp(-dtauv_dc(:,:,:)).ge.1.d-40))
519            dtauv(:,:,:) = -log((1-Fcloudy)*exp(-dtauv_cc(:,:,:)) + Fcloudy*exp(-dtauv_dc(:,:,:)))
520         elsewhere
521            dtauv(:,:,:) = dtauv_dc(:,:,:) ! No need to average...
522         endwhere
523         do ng = 1, L_NGAUSS
524            do nw = 1, L_NSPECTV
525               taucumv(1,nw,ng) = 0.0d0
526               do k = 2, L_LEVELS
527                  if ((exp(-taucumv_cc(k,nw,ng)).ge.1.d-40) .and. (exp(-taucumv_dc(k,nw,ng)).ge.1.d-40)) then
528                     taucumv(k,nw,ng) = taucumv(k-1,nw,ng) - log((1-Fcloudy)*exp(-taucumv_cc(k,nw,ng)) + Fcloudy*exp(-taucumv_dc(k,nw,ng)))
529                  else
530                     taucumv(k,nw,ng) = taucumv(k-1,nw,ng) + taucumv_dc(k,nw,ng) ! No need to average...
531                  end if
532               end do
533               do l = 1, L_NLAYRAD
534                  tauv(l,nw,ng) = taucumv(2*l,nw,ng)
535               end do
536               tauv(l,nw,ng) = taucumv(2*L_NLAYRAD+1,nw,ng)
537            end do
538         end do
539         wbarv = (dtauv_cc*wbarv_cc + dtauv_dc*wbarv_dc) / (dtauv_cc + dtauv_dc)
540         cosbv = (dtauv_cc*wbarv_cc*cosbv_cc + dtauv_dc*wbarv_dc*cosbv_dc) / (dtauv_cc*wbarv_cc + dtauv_dc*wbarv_dc)
541         
542         ! Diagnostics for haze :
543         where (popt_hazev_cc(:,:,1) .lt. 1.d-30)
544            popt_hazev_cc(:,:,1) = 1.d-30
545         endwhere
546         where (popt_hazev_dc(:,:,1) .lt. 1.d-30)
547            popt_hazev_dc(:,:,1) = 1.d-30
548         endwhere
549         popt_hazev(:,:,1) = -log( (1-Fcloudy)*exp(-popt_hazev_cc(:,:,1)) + Fcloudy*exp(-popt_hazev_dc(:,:,1)) )
550         popt_hazev(:,:,2) = ((popt_hazev_cc(:,:,1)*popt_hazev_cc(:,:,2) + popt_hazev_dc(:,:,1)*popt_hazev_dc(:,:,2)) &
551                             / (popt_hazev_cc(:,:,1) + popt_hazev_dc(:,:,1)))
552         where ((popt_hazev_cc(:,:,2).gt.0.0D0) .or. (popt_hazev_dc(:,:,2).gt.0.0D0))
553            popt_hazev(:,:,3) = (popt_hazev_cc(:,:,1)*popt_hazev_cc(:,:,2)*popt_hazev_cc(:,:,3) + popt_hazev_dc(:,:,1)*popt_hazev_dc(:,:,2)*popt_hazev_dc(:,:,3)) &
554                             / (popt_hazev_cc(:,:,1)*popt_hazev_cc(:,:,2) + popt_hazev_dc(:,:,1)*popt_hazev_dc(:,:,2))
555         elsewhere
556            popt_hazev(:,:,3) = 0.0D0
557         endwhere
558         ! Diagnostics for clouds :
559         if (callclouds) then
560            where (popt_cloudsv_cc(:,:,1) .lt. 1.d-30)
561               popt_cloudsv_cc(:,:,1) = 1.d-30
562            endwhere
563            where (popt_cloudsv_dc(:,:,1) .lt. 1.d-30)
564               popt_cloudsv_dc(:,:,1) = 1.d-30
565            endwhere
566            popt_cloudsv(:,:,1) = -log( (1-Fcloudy)*exp(-popt_cloudsv_cc(:,:,1)) + Fcloudy*exp(-popt_cloudsv_dc(:,:,1)) )
567            popt_cloudsv(:,:,2) = ((popt_cloudsv_cc(:,:,1)*popt_cloudsv_cc(:,:,2) + popt_cloudsv_dc(:,:,1)*popt_cloudsv_dc(:,:,2)) &
568                              / (popt_cloudsv_cc(:,:,1) + popt_cloudsv_dc(:,:,1)))
569            where ((popt_cloudsv_cc(:,:,2).gt.0.0D0) .or. (popt_cloudsv_dc(:,:,2).gt.0.0D0))
570               popt_cloudsv(:,:,3) = (popt_cloudsv_cc(:,:,1)*popt_cloudsv_cc(:,:,2)*popt_cloudsv_cc(:,:,3) + popt_cloudsv_dc(:,:,1)*popt_cloudsv_dc(:,:,2)*popt_cloudsv_dc(:,:,3)) &
571                              / (popt_cloudsv_cc(:,:,1)*popt_cloudsv_cc(:,:,2) + popt_cloudsv_dc(:,:,1)*popt_cloudsv_dc(:,:,2))
572            elsewhere
573               popt_cloudsv(:,:,3) = 0.0D0
574            endwhere
575         endif
576         
577         if(fract(ig) .ge. 1.0e-4) then ! Only during daylight.
578            if((ngrid.eq.1).and.(global1d))then
579               do nw=1,L_NSPECTV
580                  stel_fract(nw)= stel(nw)* 0.25 / acosz ! globally averaged = divide by 4, and we correct for solar zenith angle
581               end do
582            else
583               do nw=1,L_NSPECTV
584                  stel_fract(nw)= stel(nw) * fract(ig)
585               end do
586            endif
587            call sfluxv(dtauv,tauv,taucumv,albv,dwnv,wbarv,cosbv,  &
588                 acosz,stel_fract,                                 &
589                 nfluxtopv,fluxtopvdn,nfluxoutv_nu,nfluxgndv_nu,   &
590                 fmnetv,fluxupv,fluxdnv,fzerov,taugsurf)
591
592         else ! During the night, fluxes = 0.
593            nfluxtopv        = 0.0d0
594            fluxtopvdn       = 0.0d0
595            nfluxoutv_nu(:)  = 0.0d0
596            nfluxgndv_nu(:)  = 0.0d0
597            do l=1,L_NLAYRAD
598               fmnetv(l)=0.0d0
599               fluxupv(l)=0.0d0
600               fluxdnv(l)=0.0d0
601            end do
602         end if
603
604
605         ! Equivalent Albedo Calculation (for OUTPUT). MT2015
606         if(fract(ig) .ge. 1.0e-4) then ! equivalent albedo makes sense only during daylight.       
607            surface_stellar_flux=sum(nfluxgndv_nu(1:L_NSPECTV))     
608            if(surface_stellar_flux .gt. 1.0e-3) then ! equivalent albedo makes sense only if the stellar flux received by the surface is positive.
609               DO nw=1,L_NSPECTV                 
610                  albedo_temp(nw)=albedo(ig,nw)*nfluxgndv_nu(nw)
611               ENDDO
612               albedo_temp(1:L_NSPECTV)=albedo_temp(1:L_NSPECTV)/surface_stellar_flux
613               albedo_equivalent(ig)=sum(albedo_temp(1:L_NSPECTV))
614            else
615               albedo_equivalent(ig)=0.0 ! Spectrally Integrated Albedo not defined for non-irradiated grid points. So we arbitrary set the equivalent albedo to 0.
616            endif
617         else
618            albedo_equivalent(ig)=0.0 ! Spectrally Integrated Albedo not defined for non-irradiated grid points. So we arbitrary set the equivalent albedo to 0.
619         endif
620
621
622!-----------------------------------------------------------------------
623!        Long Wave Part
624!-----------------------------------------------------------------------
625
626
627         ! Clear column :
628         cdcolumn = 0
629         call optci(pqmo(ig,:,:),nlayer,zzlev(ig,:),plevrad,tlevrad,tmid,pmid,&
630              dtaui_cc,taucumi_cc,cosbi_cc,wbari_cc,taugsurfi,seashazefact,   &
631              popt_hazei_cc,popt_cloudsi_cc,cdcolumn)
632         ! Dark column :
633         cdcolumn = 1
634         call optci(pqmo(ig,:,:),nlayer,zzlev(ig,:),plevrad,tlevrad,tmid,pmid,&
635              dtaui_dc,taucumi_dc,cosbi_dc,wbari_dc,taugsurfi,seashazefact,   &
636              popt_hazei_dc,popt_cloudsi_dc,cdcolumn)
637
638         ! Mean opacity, ssa and asf :
639         where ((exp(-dtaui_cc(:,:,:)).ge.1.d-40) .and. (exp(-dtaui_dc(:,:,:)).ge.1.d-40))
640            dtaui(:,:,:) = -log((1-Fcloudy)*exp(-dtaui_cc(:,:,:)) + Fcloudy*exp(-dtaui_dc(:,:,:)))
641         elsewhere
642            dtaui(:,:,:) = dtaui_dc(:,:,:) ! No need to average...
643         endwhere
644         do ng = 1, L_NGAUSS
645            do nw = 1, L_NSPECTI
646               taucumi(1,nw,ng) = 0.0d0
647               do k = 2, L_LEVELS
648                  if ((exp(-taucumi_cc(k,nw,ng)).ge.1.d-40) .and. (exp(-taucumi_dc(k,nw,ng)).ge.1.d-40)) then
649                     taucumi(k,nw,ng) = taucumi(k-1,nw,ng) - log((1-Fcloudy)*exp(-taucumi_cc(k,nw,ng)) + Fcloudy*exp(-taucumi_dc(k,nw,ng)))
650                  else
651                     taucumi(k,nw,ng) = taucumi(k-1,nw,ng) + taucumi_dc(k,nw,ng) ! No need to average...
652                  end if
653               end do
654            end do
655         end do
656         where ((wbari_cc.gt.0.0D0) .or. (wbari_dc.gt.0.0D0))
657            wbari = (dtaui_cc*wbari_cc + dtaui_dc*wbari_dc) / (dtaui_cc + dtaui_dc)
658            cosbi = (dtaui_cc*wbari_cc*cosbi_cc + dtaui_dc*wbari_dc*cosbi_dc) / (dtaui_cc*wbari_cc + dtaui_dc*wbari_dc)
659         elsewhere
660            wbari = 0.0D0
661            cosbi = 0.0D0
662         endwhere
663
664         ! Diagnostics for haze :
665         where (popt_hazei_cc(:,:,1) .lt. 1.d-30)
666            popt_hazei_cc(:,:,1) = 1.d-30
667         endwhere
668         where (popt_hazei_dc(:,:,1) .lt. 1.d-30)
669            popt_hazei_dc(:,:,1) = 1.d-30
670         endwhere
671         popt_hazei(:,:,1) = -log( (1-Fcloudy)*exp(-popt_hazei_cc(:,:,1)) + Fcloudy*exp(-popt_hazei_dc(:,:,1)) )
672         popt_hazei(:,:,2) = (popt_hazei_cc(:,:,1)*popt_hazei_cc(:,:,2) + popt_hazei_dc(:,:,1)*popt_hazei_dc(:,:,2)) &
673                             / (popt_hazei_cc(:,:,1) + popt_hazei_dc(:,:,1))
674         where ((popt_hazei_cc(:,:,2).gt.0.0D0) .or. (popt_hazei_dc(:,:,2).gt.0.0D0))
675            popt_hazei(:,:,3) = (popt_hazei_cc(:,:,1)*popt_hazei_cc(:,:,2)*popt_hazei_cc(:,:,3) + popt_hazei_dc(:,:,1)*popt_hazei_dc(:,:,2)*popt_hazei_dc(:,:,3)) &
676                             / (popt_hazei_cc(:,:,1)*popt_hazei_cc(:,:,2) + popt_hazei_dc(:,:,1)*popt_hazei_dc(:,:,2))
677         elsewhere
678            popt_hazei(:,:,3) = 0.0D0
679         endwhere
680         ! Diagnostics for clouds :
681         if (callclouds) then
682            where (popt_cloudsi_cc(:,:,1) .lt. 1.d-30)
683               popt_cloudsi_cc(:,:,1) = 1.d-30
684            endwhere
685            where (popt_cloudsi_dc(:,:,1) .lt. 1.d-30)
686               popt_cloudsi_dc(:,:,1) = 1.d-30
687            endwhere
688            popt_cloudsi(:,:,1) = -log( (1-Fcloudy)*exp(-popt_cloudsi_cc(:,:,1)) + Fcloudy*exp(-popt_cloudsi_dc(:,:,1)) )
689            popt_cloudsi(:,:,2) = ((popt_cloudsi_cc(:,:,1)*popt_cloudsi_cc(:,:,2) + popt_cloudsi_dc(:,:,1)*popt_cloudsi_dc(:,:,2)) &
690                              / (popt_cloudsi_cc(:,:,1) + popt_cloudsi_dc(:,:,1)))
691            where ((popt_cloudsi_cc(:,:,2).gt.0.0D0) .or. (popt_cloudsi_dc(:,:,2).gt.0.0D0))
692               popt_cloudsi(:,:,3) = (popt_cloudsi_cc(:,:,1)*popt_cloudsi_cc(:,:,2)*popt_cloudsi_cc(:,:,3) + popt_cloudsi_dc(:,:,1)*popt_cloudsi_dc(:,:,2)*popt_cloudsi_dc(:,:,3)) &
693                              / (popt_cloudsi_cc(:,:,1)*popt_cloudsi_cc(:,:,2) + popt_cloudsi_dc(:,:,1)*popt_cloudsi_dc(:,:,2))
694            elsewhere
695               popt_cloudsi(:,:,3) = 0.0D0
696            endwhere
697         endif
698
699         call sfluxi(plevrad,tlevrad,dtaui,taucumi,ubari,albi,      &
700              wnoi,dwni,cosbi,wbari,nfluxtopi,nfluxtopi_nu,         &
701              fmneti,fluxupi,fluxdni,fluxupi_nu,fzeroi,taugsurfi)
702
703!-----------------------------------------------------------------------
704!     Transformation of the correlated-k code outputs
705!     (into dtlw, dtsw, fluxsurf_lw, fluxsurf_sw, fluxtop_lw, fluxtop_sw)
706
707!     Flux incident at the top of the atmosphere
708         fluxtop_dn(ig)=fluxtopvdn
709
710         fluxtop_lw(ig)  = real(nfluxtopi)
711         fluxabs_sw(ig)  = real(-nfluxtopv)
712         fluxsurf_lw(ig) = real(fluxdni(L_NLAYRAD))
713         fluxsurf_sw(ig) = real(fluxdnv(L_NLAYRAD))
714         
715!        Flux absorbed by the surface. By MT2015.         
716         fluxsurfabs_sw(ig) = fluxsurf_sw(ig)*(1.-albedo_equivalent(ig))
717
718         if(fluxtop_dn(ig).lt.0.0)then
719            print*,'Achtung! fluxtop_dn has lost the plot!'
720            print*,'fluxtop_dn=',fluxtop_dn(ig)
721            print*,'acosz=',acosz
722            print*,'temp=   ',pt(ig,:)
723            print*,'pplay=  ',pplay(ig,:)
724            call abort
725         endif
726
727!     Spectral output, for exoplanet observational comparison
728         if(specOLR)then
729            do nw=1,L_NSPECTI
730               OLR_nu(ig,nw)=nfluxtopi_nu(nw)/DWNI(nw) !JL Normalize to the bandwidth
731            end do
732            do nw=1,L_NSPECTV
733               !GSR_nu(ig,nw)=nfluxgndv_nu(nw)
734               OSR_nu(ig,nw)=nfluxoutv_nu(nw)/DWNV(nw) !JL Normalize to the bandwidth
735            end do
736         endif
737
738!     Finally, the heating rates
739
740         DO l=2,L_NLAYRAD
741            dtsw(ig,L_NLAYRAD+1-l)=(fmnetv(l)-fmnetv(l-1))  &
742                *gzlat(ig,L_NLAYRAD+1-l)/(cpp*scalep*(plevrad(2*l+1)-plevrad(2*l-1)))
743            dtlw(ig,L_NLAYRAD+1-l)=(fmneti(l)-fmneti(l-1))  &
744                *gzlat(ig,L_NLAYRAD+1-l)/(cpp*scalep*(plevrad(2*l+1)-plevrad(2*l-1)))
745         END DO     
746
747!     These are values at top of atmosphere
748         dtsw(ig,L_NLAYRAD)=(fmnetv(1)-nfluxtopv)           &
749             *gzlat(ig,L_NLAYRAD)/(cpp*scalep*(plevrad(3)-plevrad(1)))
750         dtlw(ig,L_NLAYRAD)=(fmneti(1)-nfluxtopi)           &
751             *gzlat(ig,L_NLAYRAD)/(cpp*scalep*(plevrad(3)-plevrad(1)))
752
753
754      !  Optical thickness diagnostics (added by JVO)
755      if (diagdtau) then
756         do l=1,L_NLAYRAD
757            do nw=1,L_NSPECTV
758               int_dtauv(ig,l,nw) = 0.0d0
759               DO k=1,L_NGAUSS
760               ! Output exp(-tau) because gweight ponderates exp and not tau itself
761               int_dtauv(ig,l,nw)= int_dtauv(ig,l,nw) + exp(-dtauv(l,nw,k))*gweight(k)
762               ENDDO
763            enddo
764            do nw=1,L_NSPECTI
765            int_dtaui(ig,l,nw) = 0.0d0
766               DO k=1,L_NGAUSS
767               ! Output exp(-tau) because gweight ponderates exp and not tau itself
768               int_dtaui(ig,l,nw)= int_dtaui(ig,l,nw) + exp(-dtaui(l,nw,k))*gweight(k)
769               ENDDO
770            enddo
771         enddo
772      endif
773     
774     
775      ! Diagnostics : optical properties of haze and clouds (dtau, tau, k, w, g) :
776      do l = 2, L_LEVELS, 2
777         lev2lay = L_NLAYRAD+1 - l/2
778         ! Visible :
779         do nw = 1, L_NSPECTV
780            popthv(ig,lev2lay,nw,:) = 0.0d0
781            poptcv(ig,lev2lay,nw,:) = 0.0d0
782            ! Optical thickness (dtau) :
783            popthv(ig,lev2lay,nw,1) = (popt_hazev(l,nw,1) + popt_hazev(l+1,nw,1)) / 2.0
784            if (callclouds) then
785               poptcv(ig,lev2lay,nw,1) = (popt_cloudsv(l,nw,1) + popt_cloudsv(l+1,nw,1)) / 2.0
786            endif
787            ! Opacity (tau) :
788            do k = L_NLAYRAD, lev2lay, -1
789               popthv(ig,lev2lay,nw,2) = popthv(ig,lev2lay,nw,2) + popthv(ig,k,nw,1)
790               if (callclouds) then
791                  poptcv(ig,lev2lay,nw,2) = poptcv(ig,lev2lay,nw,2) + poptcv(ig,k,nw,1)
792               endif
793            enddo
794            ! Extinction (k) :
795            popthv(ig,lev2lay,nw,3) = popthv(ig,lev2lay,nw,1) / (zzlev(ig,lev2lay+1) - zzlev(ig,lev2lay))
796            if (callclouds) then
797               poptcv(ig,lev2lay,nw,3) = poptcv(ig,lev2lay,nw,1) / (zzlev(ig,lev2lay+1) - zzlev(ig,lev2lay))
798            endif
799            ! Simple Scattering Albedo (w) and Asymmetry Parameter (g) :
800            popthv(ig,lev2lay,nw,4) = (popt_hazev(l,nw,2) + popt_hazev(l+1,nw,2)) / 2.0
801            popthv(ig,lev2lay,nw,5) = (popt_hazev(l,nw,3) + popt_hazev(l+1,nw,3)) / 2.0
802            if (callclouds) then
803               poptcv(ig,lev2lay,nw,4) = (popt_cloudsv(l,nw,2) + popt_cloudsv(l+1,nw,2)) / 2.0
804               poptcv(ig,lev2lay,nw,5) = (popt_cloudsv(l,nw,3) + popt_cloudsv(l+1,nw,3)) / 2.0
805            endif
806         enddo
807         ! Infra-Red
808         do nw=1,L_NSPECTI
809            popthi(ig,lev2lay,nw,:) = 0.0d0
810            poptci(ig,lev2lay,nw,:) = 0.0d0
811            ! Optical thickness (dtau) :
812            popthi(ig,lev2lay,nw,1) = (popt_hazei(l,nw,1) + popt_hazei(l+1,nw,1)) / 2.0
813            if (callclouds) then
814               poptci(ig,lev2lay,nw,1) = (popt_cloudsi(l,nw,1) + popt_cloudsi(l+1,nw,1)) / 2.0
815            endif
816            ! Opacity (tau) :
817            do k = L_NLAYRAD, lev2lay, -1
818               popthi(ig,lev2lay,nw,2) = popthi(ig,lev2lay,nw,2) + popthi(ig,k,nw,1)
819               if (callclouds) then
820                  poptci(ig,lev2lay,nw,2) = poptci(ig,lev2lay,nw,2) + poptci(ig,k,nw,1)
821               endif
822            enddo
823            ! Extinction (k) :
824            popthi(ig,lev2lay,nw,3) = popthi(ig,lev2lay,nw,1) / (zzlev(ig,lev2lay+1) - zzlev(ig,lev2lay))
825            if (callclouds) then
826               poptci(ig,lev2lay,nw,3) = poptci(ig,lev2lay,nw,1) / (zzlev(ig,lev2lay+1) - zzlev(ig,lev2lay))
827            endif
828            ! Simple Scattering Albedo (w) and Asymmetry Parameter (g) :
829            popthi(ig,lev2lay,nw,4) = (popt_hazei(l,nw,2) + popt_hazei(l+1,nw,2)) / 2.0
830            popthi(ig,lev2lay,nw,5) = (popt_hazei(l,nw,3) + popt_hazei(l+1,nw,3)) / 2.0
831            if (callclouds) then
832               poptci(ig,lev2lay,nw,4) = (popt_cloudsi(l,nw,2) + popt_cloudsi(l+1,nw,2)) / 2.0
833               poptci(ig,lev2lay,nw,5) = (popt_cloudsi(l,nw,3) + popt_cloudsi(l+1,nw,3)) / 2.0
834            endif
835         enddo
836      enddo
837
838
839!-----------------------------------------------------------------------   
840      end do ! End of big loop over every GCM column.
841!-----------------------------------------------------------------------
842
843
844!-----------------------------------------------------------------------
845!     Additional diagnostics
846!-----------------------------------------------------------------------
847
848      ! IR spectral output, for exoplanet observational comparison
849      if(lastcall.and.(ngrid.eq.1))then  ! could disable the 1D output, they are in the diagfi and diagspec... JL12
850
851         print*,'Saving scalar quantities in surf_vals.out...'
852         print*,'psurf = ', pplev(1,1),' Pa'
853         open(116,file='surf_vals.out')
854         write(116,*) tsurf(1),pplev(1,1),fluxtop_dn(1),         &
855                      real(-nfluxtopv),real(nfluxtopi)
856         close(116)
857
858!          USEFUL COMMENT - Do Not Remove.
859!
860!           if(specOLR)then
861!               open(117,file='OLRnu.out')
862!               do nw=1,L_NSPECTI
863!                  write(117,*) OLR_nu(1,nw)
864!               enddo
865!               close(117)
866!
867!               open(127,file='OSRnu.out')
868!               do nw=1,L_NSPECTV
869!                  write(127,*) OSR_nu(1,nw)
870!               enddo
871!               close(127)
872!           endif
873
874           ! OLR vs altitude: do it as a .txt file.
875         OLRz=.false.
876         if(OLRz)then
877            print*,'saving IR vertical flux for OLRz...'
878            open(118,file='OLRz_plevs.out')
879            open(119,file='OLRz.out')
880            do l=1,L_NLAYRAD
881               write(118,*) plevrad(2*l)
882               do nw=1,L_NSPECTI
883                  write(119,*) fluxupi_nu(l,nw)
884               enddo
885            enddo
886            close(118)
887            close(119)
888         endif
889
890      endif
891
892      if (lastcall) then
893        IF( ALLOCATED( gasi ) ) DEALLOCATE( gasi )
894        IF( ALLOCATED( gasv ) ) DEALLOCATE( gasv )
895        IF( ALLOCATED( gasi_recomb ) ) DEALLOCATE( gasi_recomb )
896        IF( ALLOCATED( gasv_recomb ) ) DEALLOCATE( gasv_recomb )
897        IF( ALLOCATED( pqrold ) ) DEALLOCATE( pqrold )
898        IF( ALLOCATED( useptold ) ) DEALLOCATE( useptold )
899!$OMP BARRIER
900!$OMP MASTER
901        IF( ALLOCATED( pgasref ) ) DEALLOCATE( pgasref )
902        IF( ALLOCATED( tgasref ) ) DEALLOCATE( tgasref )
903        IF( ALLOCATED( pfgasref ) ) DEALLOCATE( pfgasref )
904        IF( ALLOCATED( gweight ) ) DEALLOCATE( gweight )
905!$OMP END MASTER
906!$OMP BARRIER
907      endif
908
909
910    end subroutine callcorrk
Note: See TracBrowser for help on using the repository browser.