source: trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90 @ 2480

Last change on this file since 2480 was 2470, checked in by yjaziri, 5 years ago

Generic GCM:
global1d and szangle for 1D simulation moved from callcorrk to callkeys
to defined a consistent 1D sza in physiq_mod used also in chemistry
+ typo
YJ

  • Property svn:executable set to *
File size: 101.5 KB
Line 
1      module physiq_mod
2     
3      implicit none
4     
5      contains
6     
7      subroutine physiq(ngrid,nlayer,nq,   &
8                  nametrac,                &
9                  firstcall,lastcall,      &
10                  pday,ptime,ptimestep,    &
11                  pplev,pplay,pphi,        &
12                  pu,pv,pt,pq,             &
13                  flxw,                    &
14                  pdu,pdv,pdt,pdq,pdpsrf)
15 
16      use radinc_h, only : L_NSPECTI,L_NSPECTV,naerkind
17      use watercommon_h, only : RLVTT, Psat_water,epsi,su_watercycle, RV, T_h2o_ice_liq
18      use thermcell_mod, only: init_thermcell_mod
19      use gases_h, only: gnom, gfrac
20      use radcommon_h, only: sigma, glat, grav, BWNV
21      use radii_mod, only: h2o_reffrad, co2_reffrad
22      use aerosol_mod, only: iaero_co2, iaero_h2o
23      use surfdat_h, only: phisfi, zmea, zstd, zsig, zgam, zthe, &
24                           dryness, watercaptag
25      use comdiurn_h, only: coslat, sinlat, coslon, sinlon
26      use comsaison_h, only: mu0, fract, dist_star, declin, right_ascen
27      use comsoil_h, only: nsoilmx, layer, mlayer, inertiedat
28      use geometry_mod, only: latitude, longitude, cell_area
29      USE comgeomfi_h, only: totarea, totarea_planet
30      USE tracer_h, only: noms, mmol, radius, rho_q, qext, &
31                          alpha_lift, alpha_devil, qextrhor, &
32                          igcm_h2o_ice, igcm_h2o_vap, igcm_dustbin, &
33                          igcm_co2_ice
34      use time_phylmdz_mod, only: ecritphy, iphysiq, nday
35      use phyetat0_mod, only: phyetat0
36      use phyredem, only: physdem0, physdem1
37      use slab_ice_h, only: capcalocean, capcalseaice,capcalsno, &
38                            noceanmx
39      use ocean_slab_mod, only :ocean_slab_init, ocean_slab_ice, &
40                                ini_surf_heat_transp_mod, &
41                                ocean_slab_get_vars,ocean_slab_final
42      use surf_heat_transp_mod,only :init_masquv
43      use planetwide_mod, only: planetwide_minval,planetwide_maxval,planetwide_sumval
44      use mod_phys_lmdz_para, only : is_master
45      use planete_mod, only: apoastr, periastr, year_day, peri_day, &
46                            obliquit, nres, z0
47      use comcstfi_mod, only: pi, g, rcp, r, rad, mugaz, cpp
48      use time_phylmdz_mod, only: daysec
49      use callkeys_mod, only: albedo_spectral_mode, calladj, calldifv, &
50                              calllott_nonoro, callrad, callsoil, callstats, &
51                              calltherm, CLFvarying, co2cond, corrk, diagdtau, &
52                              diurnal, enertest, fat1au, flatten, j2, &
53                              hydrology, icetstep, intheat, iradia, kastprof, &
54                              lwrite, mass_redistrib, massplanet, meanOLR, &
55                              nearco2cond, newtonian, noseason_day, oblate, &
56                              ok_slab_ocean, photochem, rings_shadow, rmean, &
57                              season, sedimentation, sourceevol, specOLR, &
58                              startphy_file, testradtimes, tlocked, &
59                              tracer, UseTurbDiff, water, watercond, &
60                              waterrain, global1d, szangle
61      use nonoro_gwd_ran_mod, only: nonoro_gwd_ran
62      use conc_mod
63      use phys_state_var_mod
64      use callcorrk_mod, only: callcorrk
65      use vdifc_mod, only: vdifc
66      use turbdiff_mod, only: turbdiff
67      use turb_mod, only : q2,sensibFlux,turb_resolved
68      use mass_redistribution_mod, only: mass_redistribution
69#ifndef MESOSCALE
70      use vertical_layers_mod, only: presnivs, pseudoalt
71      use mod_phys_lmdz_omp_data, ONLY: is_omp_master
72#else
73      use comm_wrf, only : comm_HR_SW, comm_HR_LW, &
74                           comm_CLOUDFRAC,comm_TOTCLOUDFRAC,&
75                           comm_SURFRAIN,comm_REEVAP,comm_HR_DYN,&
76                           comm_RAIN,comm_SNOW,comm_ALBEQ,&
77                           comm_FLUXTOP_DN,comm_FLUXABS_SW,&
78                           comm_FLUXTOP_LW,comm_FLUXSURF_SW,&
79                           comm_FLUXSURF_LW,comm_FLXGRD,&
80                           comm_LSCEZ,comm_H2OICE_REFF
81#endif
82
83#ifdef CPP_XIOS     
84      use xios_output_mod, only: initialize_xios_output, &
85                                 update_xios_timestep, &
86                                 send_xios_field
87      use wxios, only: wxios_context_init, xios_context_finalize
88#endif
89
90      implicit none
91
92
93!==================================================================
94!     
95!     Purpose
96!     -------
97!     Central subroutine for all the physics parameterisations in the
98!     universal model. Originally adapted from the Mars LMDZ model.
99!
100!     The model can be run without or with tracer transport
101!     depending on the value of "tracer" in file "callphys.def".
102!
103!
104!   It includes:
105!
106!      I. Initialization :
107!         I.1 Firstcall initializations.
108!         I.2 Initialization for every call to physiq.
109!
110!      II. Compute radiative transfer tendencies (longwave and shortwave) :
111!         II.a Option 1 : Call correlated-k radiative transfer scheme.
112!         II.b Option 2 : Call Newtonian cooling scheme.
113!         II.c Option 3 : Atmosphere has no radiative effect.
114!
115!      III. Vertical diffusion (turbulent mixing) :
116!
117!      IV. Convection :
118!         IV.a Thermal plume model
119!         IV.b Dry convective adjusment
120!
121!      V. Condensation and sublimation of gases (currently just CO2).
122!
123!      VI. Tracers
124!         VI.1. Water and water ice.
125!         VI.2  Photochemistry
126!         VI.3. Aerosols and particles.
127!         VI.4. Updates (pressure variations, surface budget).
128!         VI.5. Slab Ocean.
129!         VI.6. Surface Tracer Update.
130!
131!      VII. Surface and sub-surface soil temperature.
132!
133!      VIII. Perform diagnostics and write output files.
134!
135!
136!   arguments
137!   ---------
138!
139!   INPUT
140!   -----
141!
142!    ngrid                 Size of the horizontal grid.
143!    nlayer                Number of vertical layers.
144!    nq                    Number of advected fields.
145!    nametrac              Name of corresponding advected fields.
146!
147!    firstcall             True at the first call.
148!    lastcall              True at the last call.
149!
150!    pday                  Number of days counted from the North. Spring equinoxe.
151!    ptime                 Universal time (0<ptime<1): ptime=0.5 at 12:00 UT.
152!    ptimestep             timestep (s).
153!
154!    pplay(ngrid,nlayer)   Pressure at the middle of the layers (Pa).
155!    pplev(ngrid,nlayer+1) Intermediate pressure levels (Pa).
156!    pphi(ngrid,nlayer)    Geopotential at the middle of the layers (m2.s-2).
157!
158!    pu(ngrid,nlayer)      u, zonal component of the wind (ms-1).
159!    pv(ngrid,nlayer)      v, meridional component of the wind (ms-1).
160!
161!    pt(ngrid,nlayer)      Temperature (K).
162!
163!    pq(ngrid,nlayer,nq)   Advected fields.
164!
165!    pudyn(ngrid,nlayer)    \
166!    pvdyn(ngrid,nlayer)     \ Dynamical temporal derivative for the
167!    ptdyn(ngrid,nlayer)     / corresponding variables.
168!    pqdyn(ngrid,nlayer,nq) /
169!    flxw(ngrid,nlayer)      vertical mass flux (kg/s) at layer lower boundary
170!
171!   OUTPUT
172!   ------
173!
174!    pdu(ngrid,nlayer)        \
175!    pdv(ngrid,nlayer)         \  Temporal derivative of the corresponding
176!    pdt(ngrid,nlayer)         /  variables due to physical processes.
177!    pdq(ngrid,nlayer)        /
178!    pdpsrf(ngrid)             /
179!
180!
181!     Authors
182!     -------
183!           Frederic Hourdin        15/10/93
184!           Francois Forget        1994
185!           Christophe Hourdin        02/1997
186!           Subroutine completely rewritten by F. Forget (01/2000)
187!           Water ice clouds: Franck Montmessin (update 06/2003)
188!           Radiatively active tracers: J.-B. Madeleine (10/2008-06/2009)
189!           New correlated-k radiative scheme: R. Wordsworth (2009)
190!           Many specifically Martian subroutines removed: R. Wordsworth (2009)       
191!           Improved water cycle: R. Wordsworth / B. Charnay (2010)
192!           To F90: R. Wordsworth (2010)
193!           New turbulent diffusion scheme: J. Leconte (2012)
194!           Loops converted to F90 matrix format: J. Leconte (2012)
195!           No more ngridmx/nqmx, F90 commons and adaptation to parallel: A. Spiga (2012)
196!           Purge of the code : M. Turbet (2015)
197!           Photochemical core developped by F. Lefevre: B. Charnay (2017)
198!==================================================================
199
200
201!    0.  Declarations :
202!    ------------------
203
204include "netcdf.inc"
205
206! Arguments :
207! -----------
208
209!   INPUTS:
210!   -------
211
212      integer,intent(in) :: ngrid             ! Number of atmospheric columns.
213      integer,intent(in) :: nlayer            ! Number of atmospheric layers.
214      integer,intent(in) :: nq                ! Number of tracers.
215      character*30,intent(in) :: nametrac(nq) ! Names of the tracers taken from dynamics.
216     
217      logical,intent(in) :: firstcall ! Signals first call to physics.
218      logical,intent(in) :: lastcall  ! Signals last call to physics.
219     
220      real,intent(in) :: pday                  ! Number of elapsed sols since reference Ls=0.
221      real,intent(in) :: ptime                 ! "Universal time", given as fraction of sol (e.g.: 0.5 for noon).
222      real,intent(in) :: ptimestep             ! Physics timestep (s).
223      real,intent(in) :: pplev(ngrid,nlayer+1) ! Inter-layer pressure (Pa).
224      real,intent(in) :: pplay(ngrid,nlayer)   ! Mid-layer pressure (Pa).
225      real,intent(in) :: pphi(ngrid,nlayer)    ! Geopotential at mid-layer (m2s-2).
226      real,intent(in) :: pu(ngrid,nlayer)      ! Zonal wind component (m/s).
227      real,intent(in) :: pv(ngrid,nlayer)      ! Meridional wind component (m/s).
228      real,intent(in) :: pt(ngrid,nlayer)      ! Temperature (K).
229      real,intent(in) :: pq(ngrid,nlayer,nq)   ! Tracers (kg/kg_of_air).
230      real,intent(in) :: flxw(ngrid,nlayer)    ! Vertical mass flux (ks/s) at lower boundary of layer
231
232!   OUTPUTS:
233!   --------
234
235!     Physical tendencies :
236
237      real,intent(out) :: pdu(ngrid,nlayer)    ! Zonal wind tendencies (m/s/s).
238      real,intent(out) :: pdv(ngrid,nlayer)    ! Meridional wind tendencies (m/s/s).
239      real,intent(out) :: pdt(ngrid,nlayer)    ! Temperature tendencies (K/s).
240      real,intent(out) :: pdq(ngrid,nlayer,nq) ! Tracer tendencies (kg/kg_of_air/s).
241      real,intent(out) :: pdpsrf(ngrid)        ! Surface pressure tendency (Pa/s).
242
243! Local saved variables:
244! ----------------------
245      integer,save :: day_ini                                      ! Initial date of the run (sol since Ls=0).
246      integer,save :: icount                                       ! Counter of calls to physiq during the run.
247!$OMP THREADPRIVATE(day_ini,icount)
248
249! Local variables :
250! -----------------
251
252!     Aerosol (dust or ice) extinction optical depth  at reference wavelength
253!     for the "naerkind" optically active aerosols:
254
255      real aerosol(ngrid,nlayer,naerkind) ! Aerosols.
256      real zh(ngrid,nlayer)               ! Potential temperature (K).
257      real pw(ngrid,nlayer)               ! Vertical velocity (m/s). (NOTE : >0 WHEN DOWNWARDS !!)
258      real omega(ngrid,nlayer)            ! omega velocity (Pa/s, >0 when downward)
259
260      integer l,ig,ierr,iq,nw,isoil
261     
262      real zls                       ! Solar longitude (radians).
263      real zlss                      ! Sub solar point longitude (radians).
264      real zday                      ! Date (time since Ls=0, calculated in sols).
265      real zzlay(ngrid,nlayer)       ! Altitude at the middle of the atmospheric layers.
266      real zzlev(ngrid,nlayer+1)     ! Altitude at the atmospheric layer boundaries.
267
268! VARIABLES for the thermal plume model
269     
270      real f(ngrid)                       ! Mass flux norm
271      real fm(ngrid,nlayer+1)             ! Mass flux
272      real fm_bis(ngrid,nlayer)           ! Recasted fm
273      real entr(ngrid,nlayer)             ! Entrainment
274      real detr(ngrid,nlayer)             ! Detrainment
275      real dqevap(ngrid,nlayer,nq)        ! water tracer mass mixing ratio variations due to evaporation
276      real dtevap(ngrid,nlayer)           ! temperature variation due to evaporation
277      real zqtherm(ngrid,nlayer,nq)       ! vapor mass mixing ratio after evaporation
278      real zttherm(ngrid,nlayer)          ! temperature after evaporation
279      real fraca(ngrid,nlayer+1)          ! Fraction of the surface that plumes occupies
280      real zw2(ngrid,nlayer+1)            ! Vertical speed
281      real zw2_bis(ngrid,nlayer)          ! Recasted zw2
282     
283! TENDENCIES due to various processes :
284
285      ! For Surface Temperature : (K/s)
286      real zdtsurf(ngrid)     ! Cumulated tendencies.
287      real zdtsurfmr(ngrid)   ! Mass_redistribution routine.
288      real zdtsurfc(ngrid)    ! Condense_co2 routine.
289      real zdtsdif(ngrid)     ! Turbdiff/vdifc routines.
290      real zdtsurf_hyd(ngrid) ! Hydrol routine.
291           
292      ! For Atmospheric Temperatures : (K/s)   
293      real dtlscale(ngrid,nlayer)                             ! Largescale routine.
294      real zdtc(ngrid,nlayer)                                 ! Condense_co2 routine.
295      real zdtdif(ngrid,nlayer)                               ! Turbdiff/vdifc routines.
296      real zdttherm(ngrid,nlayer)                             ! Calltherm routine.
297      real zdtmr(ngrid,nlayer)                                ! Mass_redistribution routine.
298      real zdtrain(ngrid,nlayer)                              ! Rain routine.
299      real dtmoist(ngrid,nlayer)                              ! Moistadj routine.
300      real dt_ekman(ngrid,noceanmx), dt_hdiff(ngrid,noceanmx) ! Slab_ocean routine.
301      real zdtsw1(ngrid,nlayer), zdtlw1(ngrid,nlayer)         ! Callcorrk routine.
302                             
303      ! For Surface Tracers : (kg/m2/s)
304      real dqsurf(ngrid,nq)                 ! Cumulated tendencies.
305      real zdqsurfc(ngrid)                  ! Condense_co2 routine.
306      real zdqsdif(ngrid,nq)                ! Turbdiff/vdifc routines.
307      real zdqssed(ngrid,nq)                ! Callsedim routine.
308      real zdqsurfmr(ngrid,nq)              ! Mass_redistribution routine.
309      real zdqsrain(ngrid), zdqssnow(ngrid) ! Rain routine.
310      real dqs_hyd(ngrid,nq)                ! Hydrol routine.
311      real reevap_precip(ngrid)             ! re-evaporation flux of precipitation (integrated over the atmospheric column)
312                 
313      ! For Tracers : (kg/kg_of_air/s)
314      real zdqc(ngrid,nlayer,nq)      ! Condense_co2 routine.
315      real zdqadj(ngrid,nlayer,nq)    ! Convadj routine.
316      real zdqdif(ngrid,nlayer,nq)    ! Turbdiff/vdifc routines.
317      real zdqevap(ngrid,nlayer)      ! Turbdiff routine.
318      real zdqtherm(ngrid,nlayer,nq)  ! Calltherm routine.
319      real zdqsed(ngrid,nlayer,nq)    ! Callsedim routine.
320      real zdqmr(ngrid,nlayer,nq)     ! Mass_redistribution routine.
321      real zdqrain(ngrid,nlayer,nq)   ! Rain routine.
322      real dqmoist(ngrid,nlayer,nq)   ! Moistadj routine.
323      real dqvaplscale(ngrid,nlayer)  ! Largescale routine.
324      real dqcldlscale(ngrid,nlayer)  ! Largescale routine.
325      REAL zdqchim(ngrid,nlayer,nq)   ! Calchim_asis routine
326      REAL zdqschim(ngrid,nq)         ! Calchim_asis routine
327
328      REAL array_zero1(ngrid)
329      REAL array_zero2(ngrid,nlayer)
330                       
331      ! For Winds : (m/s/s)
332      real zdvadj(ngrid,nlayer), zduadj(ngrid,nlayer)       ! Convadj routine.
333      real zdutherm(ngrid,nlayer), zdvtherm(ngrid,nlayer)   ! Calltherm routine.
334      real zdumr(ngrid,nlayer), zdvmr(ngrid,nlayer)         ! Mass_redistribution routine.
335      real zdvdif(ngrid,nlayer), zdudif(ngrid,nlayer)       ! Turbdiff/vdifc routines.
336      real zdhdif(ngrid,nlayer)                             ! Turbdiff/vdifc routines.
337      real zdhadj(ngrid,nlayer)                             ! Convadj routine.
338     
339      ! For Pressure and Mass :
340      real zdmassmr(ngrid,nlayer) ! Atmospheric Mass tendency for mass_redistribution (kg_of_air/m2/s).
341      real zdmassmr_col(ngrid)    ! Atmospheric Column Mass tendency for mass_redistribution (kg_of_air/m2/s).
342      real zdpsrfmr(ngrid)        ! Pressure tendency for mass_redistribution routine (Pa/s).
343
344     
345   
346! Local variables for LOCAL CALCULATIONS:
347! ---------------------------------------
348      real zflubid(ngrid)
349      real zplanck(ngrid),zpopsk(ngrid,nlayer)
350      real ztim1,ztim2,ztim3, z1,z2
351      real ztime_fin
352      real zdh(ngrid,nlayer)
353      real gmplanet
354      real taux(ngrid),tauy(ngrid)
355
356
357! local variables for DIAGNOSTICS : (diagfi & stat)
358! -------------------------------------------------
359      real ps(ngrid)                                     ! Surface Pressure.
360      real zt(ngrid,nlayer)                              ! Atmospheric Temperature.
361      real zu(ngrid,nlayer),zv(ngrid,nlayer)             ! Zonal and Meridional Winds.
362      real zq(ngrid,nlayer,nq)                           ! Atmospheric Tracers.
363      real zdtadj(ngrid,nlayer)                          ! Convadj Diagnostic.
364      real zdtdyn(ngrid,nlayer)                          ! Dynamical Heating (K/s).
365      real zdudyn(ngrid,nlayer)                          ! Dynamical Zonal Wind tendency (m.s-2).
366
367      real reff(ngrid,nlayer)                       ! Effective dust radius (used if doubleq=T).
368      real vmr(ngrid,nlayer)                        ! volume mixing ratio
369      real time_phys
370     
371      real ISR,ASR,OLR,GND,DYN,GSR,Ts1,Ts2,Ts3,TsS ! for Diagnostic.
372     
373      real qcol(ngrid,nq) ! Tracer Column Mass (kg/m2).
374
375!     included by RW for H2O Manabe scheme
376      real rneb_man(ngrid,nlayer) ! H2O cloud fraction (moistadj).
377      real rneb_lsc(ngrid,nlayer) ! H2O cloud fraction (large scale).
378
379
380!     to test energy conservation (RW+JL)
381      real mass(ngrid,nlayer),massarea(ngrid,nlayer)
382      real dEtot, dEtots, AtmToSurf_TurbFlux
383      real,save :: dEtotSW, dEtotsSW, dEtotLW, dEtotsLW
384!$OMP THREADPRIVATE(dEtotSW, dEtotsSW, dEtotLW, dEtotsLW)
385      real dEzRadsw(ngrid,nlayer),dEzRadlw(ngrid,nlayer),dEzdiff(ngrid,nlayer)
386      real dEdiffs(ngrid),dEdiff(ngrid)
387      real madjdE(ngrid), lscaledE(ngrid),madjdEz(ngrid,nlayer), lscaledEz(ngrid,nlayer)
388     
389!JL12 conservation test for mean flow kinetic energy has been disabled temporarily
390
391      real dtmoist_max,dtmoist_min     
392      real dItot, dItot_tmp, dVtot, dVtot_tmp
393
394
395      real h2otot                      ! Total Amount of water. For diagnostic.
396      real icesrf,liqsrf,icecol,vapcol ! Total Amounts of water (ice,liq,vap). For diagnostic.
397      real dWtot, dWtot_tmp, dWtots, dWtots_tmp
398      logical,save :: watertest
399!$OMP THREADPRIVATE(watertest)
400
401      real qsat(ngrid,nlayer) ! Water Vapor Volume Mixing Ratio at saturation (kg/kg_of_air).
402      real RH(ngrid,nlayer)   ! Relative humidity.
403      real H2Omaxcol(ngrid)   ! Maximum possible H2O column amount (at 100% saturation) (kg/m2).
404      real psat_tmp
405     
406      logical clearsky ! For double radiative transfer call. By BC
407     
408      ! For Clear Sky Case.
409      real fluxsurf_lw1(ngrid), fluxsurf_sw1(ngrid), fluxsurfabs_sw1(ngrid)  ! For SW/LW flux.
410      real fluxtop_lw1(ngrid), fluxabs_sw1(ngrid)                            ! For SW/LW flux.
411      real albedo_equivalent1(ngrid)                                         ! For Equivalent albedo calculation.
412      real tau_col1(ngrid)                                                   ! For aerosol optical depth diagnostic.
413      real OLR_nu1(ngrid,L_NSPECTI), OSR_nu1(ngrid,L_NSPECTV)                ! For Outgoing Radiation diagnostics.
414      real int_dtaui1(ngrid,nlayer,L_NSPECTI),int_dtauv1(ngrid,nlayer,L_NSPECTV) ! For optical thickness diagnostics.
415      real tf, ntf
416
417      real nconsMAX, vdifcncons(ngrid), cadjncons(ngrid) ! Vdfic water conservation test. By RW
418
419      real muvar(ngrid,nlayer+1) ! For Runaway Greenhouse 1D study. By RW
420
421      real reffcol(ngrid,naerkind)
422
423!     Sourceevol for 'accelerated ice evolution'. By RW
424      real delta_ice,ice_tot
425      integer num_run
426      logical,save :: ice_update
427!  Non-oro GW tendencies
428      REAL d_u_hin(ngrid,nlayer), d_v_hin(ngrid,nlayer)
429      REAL d_t_hin(ngrid,nlayer)
430!  Diagnostics 2D of gw_nonoro
431      REAL zustrhi(ngrid), zvstrhi(ngrid)
432
433
434      real :: tsurf2(ngrid)
435      real :: flux_o(ngrid),flux_g(ngrid),fluxgrdocean(ngrid)
436      real :: flux_sens_lat(ngrid)
437      real :: qsurfint(ngrid,nq)
438#ifdef MESOSCALE
439      REAL :: lsf_dt(nlayer)
440      REAL :: lsf_dq(nlayer)
441#endif
442
443      ! Misc
444      character*2 :: str2
445!==================================================================================================
446
447! -----------------
448! I. INITIALISATION
449! -----------------
450
451! --------------------------------
452! I.1   First Call Initialisation.
453! --------------------------------
454      if (firstcall) then
455        ! Allocate saved arrays (except for 1D model, where this has already
456        ! been done)
457#ifndef MESOSCALE
458        if (ngrid>1) call phys_state_var_init(nq)
459#endif
460
461!        Variables set to 0
462!        ~~~~~~~~~~~~~~~~~~
463         dtrad(:,:) = 0.0
464         fluxrad(:) = 0.0
465         tau_col(:) = 0.0
466         zdtsw(:,:) = 0.0
467         zdtlw(:,:) = 0.0
468
469
470!        Initialize aerosol indexes.
471!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
472         call iniaerosol()
473
474     
475!        Initialize tracer names, indexes and properties.
476!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
477         IF (.NOT.ALLOCATED(noms)) ALLOCATE(noms(nq)) ! (because noms is an argument of physdem1 whether or not tracer is on)
478         if (tracer) then
479            call initracer(ngrid,nq,nametrac)
480            if(photochem) then
481              call ini_conc_mod(ngrid,nlayer)
482            endif
483         endif
484
485#ifdef CPP_XIOS
486        ! Initialize XIOS context
487        write(*,*) "physiq: call wxios_context_init"
488        CALL wxios_context_init
489#endif
490
491!        Read 'startfi.nc' file.
492!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
493#ifndef MESOSCALE
494         call phyetat0(startphy_file,                                 &
495                       ngrid,nlayer,"startfi.nc",0,0,nsoilmx,nq,      &
496                       day_ini,time_phys,tsurf,tsoil,emis,q2,qsurf,   &
497                       cloudfrac,totcloudfrac,hice,                   &
498                       rnat,pctsrf_sic,tslab, tsea_ice,sea_ice)
499#else
500      emis(:)=0.0
501      q2(:,:)=0.0
502      qsurf(:,:)=0.0
503      day_ini = pday
504#endif
505
506#ifndef MESOSCALE
507         if (.not.startphy_file) then
508           ! additionnal "academic" initialization of physics
509           if (is_master) write(*,*) "Physiq: initializing tsurf(:) to pt(:,1) !!"
510           tsurf(:)=pt(:,1)
511           if (is_master) write(*,*) "Physiq: initializing tsoil(:) to pt(:,1) !!"
512           do isoil=1,nsoilmx
513             tsoil(1:ngrid,isoil)=tsurf(1:ngrid)
514           enddo
515           if (is_master) write(*,*) "Physiq: initializing day_ini to pdat !"
516           day_ini=pday
517         endif
518#endif
519         if (pday.ne.day_ini) then
520             write(*,*) "ERROR in physiq.F90:"
521             write(*,*) "bad synchronization between physics and dynamics"
522             write(*,*) "dynamics day: ",pday
523             write(*,*) "physics day:  ",day_ini
524             stop
525         endif
526
527         write (*,*) 'In physiq day_ini =', day_ini
528
529!        Initialize albedo calculation.
530!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
531         albedo(:,:)=0.0
532          albedo_bareground(:)=0.0
533          albedo_snow_SPECTV(:)=0.0
534          albedo_co2_ice_SPECTV(:)=0.0
535         call surfini(ngrid,nq,qsurf,albedo,albedo_bareground,albedo_snow_SPECTV,albedo_co2_ice_SPECTV)
536         
537!        Initialize orbital calculation.
538!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
539         call iniorbit(apoastr,periastr,year_day,peri_day,obliquit)
540
541
542         if(tlocked)then
543            print*,'Planet is tidally locked at resonance n=',nres
544            print*,'Make sure you have the right rotation rate!!!'
545         endif
546
547!        Initialize oceanic variables.
548!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
549
550         if (ok_slab_ocean)then
551
552           call ocean_slab_init(ngrid,ptimestep, tslab, &
553                                sea_ice, pctsrf_sic)
554
555           call ini_surf_heat_transp_mod()
556           
557           knindex(:) = 0
558
559           do ig=1,ngrid
560              zmasq(ig)=1
561              knindex(ig) = ig
562              if (nint(rnat(ig)).eq.0) then
563                 zmasq(ig)=0
564              endif
565           enddo
566
567           CALL init_masquv(ngrid,zmasq)
568
569         endif ! end of 'ok_slab_ocean'.
570
571
572!        Initialize soil.
573!        ~~~~~~~~~~~~~~~~
574         if (callsoil) then
575         
576            call soil(ngrid,nsoilmx,firstcall,lastcall,inertiedat, &
577                      ptimestep,tsurf,tsoil,capcal,fluxgrd)
578
579            if (ok_slab_ocean) then
580               do ig=1,ngrid
581                  if (nint(rnat(ig)).eq.2) then
582                     capcal(ig)=capcalocean
583                     if (pctsrf_sic(ig).gt.0.5) then
584                        capcal(ig)=capcalseaice
585                        if (qsurf(ig,igcm_h2o_ice).gt.0.) then
586                           capcal(ig)=capcalsno
587                        endif
588                     endif
589                  endif
590               enddo
591            endif ! end of 'ok_slab_ocean'.
592
593         else ! else of 'callsoil'.
594         
595            print*,'WARNING! Thermal conduction in the soil turned off'
596            capcal(:)=1.e6
597            fluxgrd(:)=intheat
598            print*,'Flux from ground = ',intheat,' W m^-2'
599           
600         endif ! end of 'callsoil'.
601         
602         icount=1
603
604!        Decide whether to update ice at end of run.
605!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
606         ice_update=.false.
607         if(sourceevol)then
608!$OMP MASTER
609            open(128,file='num_run',form='formatted', &
610                     status="old",iostat=ierr)
611            if (ierr.ne.0) then
612               write(*,*) "physiq: Error! No num_run file!"
613               write(*,*) " (which is needed for sourceevol option)"
614               stop
615            endif
616            read(128,*) num_run
617            close(128)
618!$OMP END MASTER
619!$OMP BARRIER
620       
621            if(num_run.ne.0.and.mod(num_run,2).eq.0)then
622               print*,'Updating ice at end of this year!'
623               ice_update=.true.
624               ice_min(:)=1.e4
625            endif
626           
627         endif ! end of 'sourceevol'.
628
629
630         ! Here is defined the type of the surface : Continent or Ocean.
631         ! BC2014 : This is now already done in newstart.
632         ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
633         if (.not.ok_slab_ocean) then
634         
635           rnat(:)=1.
636           do ig=1,ngrid
637              if(inertiedat(ig,1).gt.1.E4)then
638                 rnat(ig)=0
639              endif
640           enddo
641
642           print*,'WARNING! Surface type currently decided by surface inertia'
643           print*,'This should be improved e.g. in newstart.F'
644           
645         endif ! end of 'ok_slab_ocean'.
646
647
648!        Initialize surface history variable.
649!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
650         qsurf_hist(:,:)=qsurf(:,:)
651
652!        Initialize variable for dynamical heating and zonal wind tendency diagnostic
653!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
654         ztprevious(:,:)=pt(:,:)
655         zuprevious(:,:)=pu(:,:)
656
657!        Set temperature just above condensation temperature (for Early Mars)
658!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
659         if(nearco2cond) then
660            write(*,*)' WARNING! Starting at Tcond+1K'
661            do l=1, nlayer
662               do ig=1,ngrid
663                  pdt(ig,l)= ((-3167.8)/(log(.01*pplay(ig,l))-23.23)+4     &
664                      -pt(ig,l)) / ptimestep
665               enddo
666            enddo
667         endif
668
669         if(meanOLR)then         
670            call system('rm -f rad_bal.out') ! to record global radiative balance.           
671            call system('rm -f tem_bal.out') ! to record global mean/max/min temperatures.           
672            call system('rm -f h2o_bal.out') ! to record global hydrological balance.
673         endif
674
675
676         watertest=.false.       
677         if(water)then ! Initialize variables for water cycle
678           
679            if(enertest)then
680               watertest = .true.
681            endif
682
683            if(ice_update)then
684               ice_initial(:)=qsurf(:,igcm_h2o_ice)
685            endif
686
687         endif
688         
689!        Set some parameters for the thermal plume model
690!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
691         if (calltherm) then
692            CALL init_thermcell_mod(g, rcp, r, pi, T_h2o_ice_liq, RV)
693         endif
694         
695         call su_watercycle ! even if we don't have a water cycle, we might
696                            ! need epsi for the wvp definitions in callcorrk.F
697                            ! or RETV, RLvCp for the thermal plume model
698#ifndef MESOSCALE
699         if (ngrid.ne.1) then ! Note : no need to create a restart file in 1d.
700            call physdem0("restartfi.nc",longitude,latitude,nsoilmx,ngrid,nlayer,nq, &
701                         ptimestep,pday+nday,time_phys,cell_area,          &
702                         albedo_bareground,inertiedat,zmea,zstd,zsig,zgam,zthe)
703         endif
704#endif         
705         
706         ! XIOS outputs
707#ifdef CPP_XIOS
708
709         write(*,*) "physiq: call initialize_xios_output"
710         call initialize_xios_output(pday,ptime,ptimestep,daysec, &
711                                     presnivs,pseudoalt)
712#endif
713         write(*,*) "physiq: end of firstcall"
714      endif ! end of 'firstcall'
715
716! ------------------------------------------------------
717! I.2   Initializations done at every physical timestep:
718! ------------------------------------------------------
719
720#ifdef CPP_XIOS     
721      ! update XIOS time/calendar
722      call update_xios_timestep
723#endif     
724
725      ! Initialize various variables
726      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
727     
728      if ( .not.nearco2cond ) then
729         pdt(1:ngrid,1:nlayer) = 0.0
730      endif     
731      zdtsurf(1:ngrid)      = 0.0
732      pdq(1:ngrid,1:nlayer,1:nq) = 0.0
733      dqsurf(1:ngrid,1:nq)= 0.0
734      pdu(1:ngrid,1:nlayer) = 0.0
735      pdv(1:ngrid,1:nlayer) = 0.0
736      pdpsrf(1:ngrid)       = 0.0
737      zflubid(1:ngrid)      = 0.0     
738      flux_sens_lat(1:ngrid) = 0.0
739      taux(1:ngrid) = 0.0
740      tauy(1:ngrid) = 0.0
741
742      zday=pday+ptime ! Compute time, in sols (and fraction thereof).
743
744      ! Compute Stellar Longitude (Ls), and orbital parameters.
745      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
746      if (season) then
747         call stellarlong(zday,zls)
748      else
749         call stellarlong(noseason_day,zls)
750      end if
751
752      call orbite(zls,dist_star,declin,right_ascen)
753     
754      if (tlocked) then
755              zlss=Mod(-(2.*pi*(zday/year_day)*nres - right_ascen),2.*pi)
756      elseif (diurnal) then
757              zlss=-2.*pi*(zday-.5)
758      else if(diurnal .eqv. .false.) then
759              zlss=9999.
760      endif
761
762
763      ! Compute variations of g with latitude (oblate case).
764      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
765      if (oblate .eqv. .false.) then     
766         glat(:) = g         
767      else if (flatten .eq. 0.0 .or. J2 .eq. 0.0 .or. Rmean .eq. 0.0 .or. MassPlanet .eq. 0.0) then     
768         print*,'I need values for flatten, J2, Rmean and MassPlanet to compute glat (else set oblate=.false.)'
769         call abort
770      else
771         gmplanet = MassPlanet*grav*1e24
772         do ig=1,ngrid
773            glat(ig)= gmplanet/(Rmean**2) * (1.D0 + 0.75 *J2 - 2.0*flatten/3. + (2.*flatten - 15./4.* J2) * cos(2. * (pi/2. - latitude(ig))))
774         end do
775      endif
776
777      ! Compute geopotential between layers.
778      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
779      zzlay(1:ngrid,1:nlayer)=pphi(1:ngrid,1:nlayer)
780      do l=1,nlayer         
781         zzlay(1:ngrid,l)= zzlay(1:ngrid,l)/glat(1:ngrid)
782      enddo
783
784      zzlev(1:ngrid,1)=0.
785      zzlev(1:ngrid,nlayer+1)=1.e7 ! Dummy top of last layer above 10000 km...
786
787      do l=2,nlayer
788         do ig=1,ngrid
789            z1=(pplay(ig,l-1)+pplev(ig,l))/(pplay(ig,l-1)-pplev(ig,l))
790            z2=(pplev(ig,l)+pplay(ig,l))/(pplev(ig,l)-pplay(ig,l))
791            zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
792         enddo
793      enddo     
794
795      ! Compute potential temperature
796      ! Note : Potential temperature calculation may not be the same in physiq and dynamic...
797      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
798      do l=1,nlayer         
799         do ig=1,ngrid
800            zpopsk(ig,l)=(pplay(ig,l)/pplev(ig,1))**rcp
801            zh(ig,l)=pt(ig,l)/zpopsk(ig,l)
802            mass(ig,l)  = (pplev(ig,l) - pplev(ig,l+1))/glat(ig)
803            massarea(ig,l)=mass(ig,l)*cell_area(ig)
804         enddo
805      enddo
806
807     ! Compute vertical velocity (m/s) from vertical mass flux
808     ! w = F / (rho*area) and rho = P/(r*T)
809     ! But first linearly interpolate mass flux to mid-layers
810      do l=1,nlayer-1
811         pw(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
812      enddo
813      pw(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
814      do l=1,nlayer
815         pw(1:ngrid,l)=(pw(1:ngrid,l)*r*pt(1:ngrid,l)) /  &
816                       (pplay(1:ngrid,l)*cell_area(1:ngrid))
817      enddo
818      ! omega in Pa/s
819      do l=1,nlayer-1
820         omega(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
821      enddo
822      omega(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
823      do l=1,nlayer
824         omega(1:ngrid,l)=g*omega(1:ngrid,l)/cell_area(1:ngrid)
825      enddo
826
827      ! ----------------------------------------------------------------
828      ! Compute mean mass, cp, and R
829      ! --------------------------------
830#ifndef MESOSCALE
831      if(photochem) then
832         call concentrations(ngrid,nlayer,nq,pplay,pt,pdt,pq,pdq,ptimestep)
833      endif
834#endif
835
836!---------------------------------
837! II. Compute radiative tendencies
838!---------------------------------
839
840      if (callrad) then
841         if( mod(icount-1,iradia).eq.0.or.lastcall) then
842
843          ! Compute local stellar zenith angles
844          ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
845            if (tlocked) then
846            ! JL14 corrects tidally resonant (and inclined) cases. nres=omega_rot/omega_orb
847               ztim1=SIN(declin)
848               ztim2=COS(declin)*COS(zlss)
849               ztim3=COS(declin)*SIN(zlss)
850
851               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
852                            ztim1,ztim2,ztim3,mu0,fract, flatten)
853
854            elseif (diurnal) then
855               ztim1=SIN(declin)
856               ztim2=COS(declin)*COS(2.*pi*(zday-.5))
857               ztim3=-COS(declin)*SIN(2.*pi*(zday-.5))
858
859               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
860                            ztim1,ztim2,ztim3,mu0,fract, flatten)
861            else if(diurnal .eqv. .false.) then
862
863               call mucorr(ngrid,declin,latitude,mu0,fract,10000.,rad,flatten)
864               ! WARNING: this function appears not to work in 1D
865
866               if ((ngrid.eq.1).and.(global1d)) then ! Fixed zenith angle 'szangle' in 1D simulations w/ globally-averaged sunlight.
867                  mu0 = cos(pi*szangle/180.0)
868                  !print*,'acosz=',mu0,', szangle=',szangle
869               endif
870
871            endif
872           
873            ! Eclipse incoming sunlight (e.g. Saturn ring shadowing).       
874            if(rings_shadow) then
875                call call_rings(ngrid, ptime, pday, diurnal)
876            endif   
877
878
879            if (corrk) then
880           
881! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
882! II.a Call correlated-k radiative transfer scheme
883! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
884               if(kastprof)then
885                  print*,'kastprof should not = true here'
886                  call abort
887               endif
888               if(water) then
889                  muvar(1:ngrid,1:nlayer)=mugaz/(1.e0+(1.e0/epsi-1.e0)*pq(1:ngrid,1:nlayer,igcm_h2o_vap))
890                  muvar(1:ngrid,nlayer+1)=mugaz/(1.e0+(1.e0/epsi-1.e0)*pq(1:ngrid,nlayer,igcm_h2o_vap))
891                  ! take into account water effect on mean molecular weight
892               else
893                  muvar(1:ngrid,1:nlayer+1)=mugaz
894               endif
895     
896
897               if(ok_slab_ocean) then
898                  tsurf2(:)=tsurf(:)
899                  do ig=1,ngrid
900                     if (nint(rnat(ig))==0) then
901                        tsurf(ig)=((1.-pctsrf_sic(ig))*tslab(ig,1)**4+pctsrf_sic(ig)*tsea_ice(ig)**4)**0.25
902                     endif
903                  enddo
904               endif !(ok_slab_ocean)
905               
906               ! standard callcorrk
907               clearsky=.false.
908               call callcorrk(ngrid,nlayer,pq,nq,qsurf,                           &
909                              albedo,albedo_equivalent,emis,mu0,pplev,pplay,pt,   &
910                              tsurf,fract,dist_star,aerosol,muvar,                &
911                              zdtlw,zdtsw,fluxsurf_lw,fluxsurf_sw,                &
912                              fluxsurfabs_sw,fluxtop_lw,                          &
913                              fluxabs_sw,fluxtop_dn,OLR_nu,OSR_nu,                &
914                              int_dtaui,int_dtauv,                                &
915                              tau_col,cloudfrac,totcloudfrac,                     &
916                              clearsky,firstcall,lastcall)     
917
918                ! Option to call scheme once more for clear regions 
919               if(CLFvarying)then
920
921                  ! ---> PROBLEMS WITH ALLOCATED ARRAYS : temporary solution in callcorrk: do not deallocate if CLFvarying ...
922                  clearsky=.true.
923                  call callcorrk(ngrid,nlayer,pq,nq,qsurf,                           &
924                                 albedo,albedo_equivalent1,emis,mu0,pplev,pplay,pt,   &
925                                 tsurf,fract,dist_star,aerosol,muvar,                &
926                                 zdtlw1,zdtsw1,fluxsurf_lw1,fluxsurf_sw1,            &
927                                 fluxsurfabs_sw1,fluxtop_lw1,                        &
928                                 fluxabs_sw1,fluxtop_dn,OLR_nu1,OSR_nu1,             &
929                                 int_dtaui1,int_dtauv1,                              &
930                                 tau_col1,cloudfrac,totcloudfrac,                    &
931                                 clearsky,firstcall,lastcall)
932                  clearsky = .false.  ! just in case.     
933
934                  ! Sum the fluxes and heating rates from cloudy/clear cases
935                  do ig=1,ngrid
936                     tf=totcloudfrac(ig)
937                     ntf=1.-tf                   
938                     fluxsurf_lw(ig)       = ntf*fluxsurf_lw1(ig)       + tf*fluxsurf_lw(ig)
939                     fluxsurf_sw(ig)       = ntf*fluxsurf_sw1(ig)       + tf*fluxsurf_sw(ig)
940                     albedo_equivalent(ig) = ntf*albedo_equivalent1(ig) + tf*albedo_equivalent(ig)
941                     fluxsurfabs_sw(ig)    = ntf*fluxsurfabs_sw1(ig)    + tf*fluxsurfabs_sw(ig)
942                     fluxtop_lw(ig)        = ntf*fluxtop_lw1(ig)        + tf*fluxtop_lw(ig)
943                     fluxabs_sw(ig)        = ntf*fluxabs_sw1(ig)        + tf*fluxabs_sw(ig)
944                     tau_col(ig)           = ntf*tau_col1(ig)           + tf*tau_col(ig)
945                   
946                     zdtlw(ig,1:nlayer) = ntf*zdtlw1(ig,1:nlayer) + tf*zdtlw(ig,1:nlayer)
947                     zdtsw(ig,1:nlayer) = ntf*zdtsw1(ig,1:nlayer) + tf*zdtsw(ig,1:nlayer)
948
949                     OSR_nu(ig,1:L_NSPECTV) = ntf*OSR_nu1(ig,1:L_NSPECTV) + tf*OSR_nu(ig,1:L_NSPECTV)                       
950                     OLR_nu(ig,1:L_NSPECTI) = ntf*OLR_nu1(ig,1:L_NSPECTI) + tf*OLR_nu(ig,1:L_NSPECTI)                       
951                     if (diagdtau) then
952                       int_dtauv(ig,:,1:L_NSPECTV) = ntf*int_dtauv1(ig,:,1:L_NSPECTV) + tf*int_dtauv(ig,:,1:L_NSPECTV)                       
953                       int_dtaui(ig,:,1:L_NSPECTI) = ntf*int_dtaui1(ig,:,1:L_NSPECTI) + tf*int_dtaui(ig,:,1:L_NSPECTI)                       
954                     endif
955                  enddo                               
956
957               endif ! end of CLFvarying.
958
959               if(ok_slab_ocean) then
960                  tsurf(:)=tsurf2(:)
961               endif
962
963
964               ! Radiative flux from the sky absorbed by the surface (W.m-2).
965               GSR=0.0
966               fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)+fluxsurfabs_sw(1:ngrid)
967
968                            !if(noradsurf)then ! no lower surface; SW flux just disappears
969                            !   GSR = SUM(fluxsurf_sw(1:ngrid)*cell_area(1:ngrid))/totarea
970                            !   fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)
971                            !   print*,'SW lost in deep atmosphere = ',GSR,' W m^-2'
972                            !endif
973
974               ! Net atmospheric radiative heating rate (K.s-1)
975               dtrad(1:ngrid,1:nlayer)=zdtsw(1:ngrid,1:nlayer)+zdtlw(1:ngrid,1:nlayer)
976               
977               ! Late initialization of the Ice Spectral Albedo. We needed the visible bands to do that !
978               if (firstcall .and. albedo_spectral_mode) then
979                  call spectral_albedo_calc(albedo_snow_SPECTV)
980               endif
981
982            elseif(newtonian)then
983           
984! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
985! II.b Call Newtonian cooling scheme
986! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
987               call newtrelax(ngrid,nlayer,mu0,sinlat,zpopsk,pt,pplay,pplev,dtrad,firstcall)
988
989               zdtsurf(1:ngrid) = +(pt(1:ngrid,1)-tsurf(1:ngrid))/ptimestep
990               ! e.g. surface becomes proxy for 1st atmospheric layer ?
991
992            else
993           
994! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
995! II.c Atmosphere has no radiative effect
996! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
997               fluxtop_dn(1:ngrid)  = fract(1:ngrid)*mu0(1:ngrid)*Fat1AU/dist_star**2
998               if(ngrid.eq.1)then ! / by 4 globally in 1D case...
999                  fluxtop_dn(1)  = fract(1)*Fat1AU/dist_star**2/2.0
1000               endif
1001               fluxsurf_sw(1:ngrid) = fluxtop_dn(1:ngrid)
1002               print*,'------------WARNING---WARNING------------' ! by MT2015.
1003               print*,'You are in corrk=false mode, '
1004               print*,'and the surface albedo is taken equal to the first visible spectral value'               
1005               
1006               fluxsurfabs_sw(1:ngrid) = fluxtop_dn(1:ngrid)*(1.-albedo(1:ngrid,1))
1007               fluxrad_sky(1:ngrid)    = fluxsurfabs_sw(1:ngrid)
1008               fluxtop_lw(1:ngrid)  = emis(1:ngrid)*sigma*tsurf(1:ngrid)**4
1009
1010               dtrad(1:ngrid,1:nlayer)=0.0 ! no atmospheric radiative heating
1011
1012            endif ! end of corrk
1013
1014         endif ! of if(mod(icount-1,iradia).eq.0)
1015       
1016
1017         ! Transformation of the radiative tendencies
1018         ! ------------------------------------------
1019         zplanck(1:ngrid)=tsurf(1:ngrid)*tsurf(1:ngrid)
1020         zplanck(1:ngrid)=emis(1:ngrid)*sigma*zplanck(1:ngrid)*zplanck(1:ngrid)
1021         fluxrad(1:ngrid)=fluxrad_sky(1:ngrid)-zplanck(1:ngrid)
1022         pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+dtrad(1:ngrid,1:nlayer)
1023         
1024         ! Test of energy conservation
1025         !----------------------------
1026         if(enertest)then
1027            call planetwide_sumval(cpp*massarea(:,:)*zdtsw(:,:)/totarea_planet,dEtotSW)
1028            call planetwide_sumval(cpp*massarea(:,:)*zdtlw(:,:)/totarea_planet,dEtotLW)
1029            !call planetwide_sumval(fluxsurf_sw(:)*(1.-albedo_equivalent(:))*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
1030            call planetwide_sumval(fluxsurfabs_sw(:)*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
1031            call planetwide_sumval((fluxsurf_lw(:)*emis(:)-zplanck(:))*cell_area(:)/totarea_planet,dEtotsLW)
1032            dEzRadsw(:,:)=cpp*mass(:,:)*zdtsw(:,:)
1033            dEzRadlw(:,:)=cpp*mass(:,:)*zdtlw(:,:)
1034            if (is_master) then
1035               print*,'---------------------------------------------------------------'
1036               print*,'In corrk SW atmospheric heating       =',dEtotSW,' W m-2'
1037               print*,'In corrk LW atmospheric heating       =',dEtotLW,' W m-2'
1038               print*,'atmospheric net rad heating (SW+LW)   =',dEtotLW+dEtotSW,' W m-2'
1039               print*,'In corrk SW surface heating           =',dEtotsSW,' W m-2'
1040               print*,'In corrk LW surface heating           =',dEtotsLW,' W m-2'
1041               print*,'surface net rad heating (SW+LW)       =',dEtotsLW+dEtotsSW,' W m-2'
1042            endif
1043         endif ! end of 'enertest'
1044
1045      endif ! of if (callrad)
1046
1047
1048
1049!  --------------------------------------------
1050!  III. Vertical diffusion (turbulent mixing) :
1051!  --------------------------------------------
1052
1053      if (calldifv) then
1054     
1055         zflubid(1:ngrid)=fluxrad(1:ngrid)+fluxgrd(1:ngrid)
1056
1057         ! JL12 the following if test is temporarily there to allow us to compare the old vdifc with turbdiff.
1058         if (UseTurbDiff) then
1059         
1060            call turbdiff(ngrid,nlayer,nq,rnat,                  &
1061                          ptimestep,capcal,                      &
1062                          pplay,pplev,zzlay,zzlev,z0,            &
1063                          pu,pv,pt,zpopsk,pq,tsurf,emis,qsurf,   &
1064                          pdt,pdq,zflubid,                       &
1065                          zdudif,zdvdif,zdtdif,zdtsdif,          &
1066                          sensibFlux,q2,zdqdif,zdqevap,zdqsdif,  &
1067                          taux,tauy)
1068
1069         else
1070         
1071            zdh(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1072 
1073            call vdifc(ngrid,nlayer,nq,rnat,zpopsk,           &
1074                       ptimestep,capcal,lwrite,               &
1075                       pplay,pplev,zzlay,zzlev,z0,            &
1076                       pu,pv,zh,pq,tsurf,emis,qsurf,          &
1077                       zdh,pdq,zflubid,                       &
1078                       zdudif,zdvdif,zdhdif,zdtsdif,          &
1079                       sensibFlux,q2,zdqdif,zdqsdif)
1080
1081            zdtdif(1:ngrid,1:nlayer)=zdhdif(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1082            zdqevap(1:ngrid,1:nlayer)=0.
1083
1084         end if !end of 'UseTurbDiff'
1085
1086         zdtsurf(1:ngrid)=zdtsurf(1:ngrid)+zdtsdif(1:ngrid)
1087
1088         !!! this is always done, except for turbulence-resolving simulations
1089         if (.not. turb_resolved) then
1090           pdv(1:ngrid,1:nlayer)=pdv(1:ngrid,1:nlayer)+zdvdif(1:ngrid,1:nlayer)
1091           pdu(1:ngrid,1:nlayer)=pdu(1:ngrid,1:nlayer)+zdudif(1:ngrid,1:nlayer)
1092           pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+zdtdif(1:ngrid,1:nlayer)
1093         endif
1094
1095         if(ok_slab_ocean)then
1096            flux_sens_lat(1:ngrid)=(zdtsdif(1:ngrid)*capcal(1:ngrid)-fluxrad(1:ngrid))
1097         endif
1098
1099
1100         if (tracer) then
1101           pdq(1:ngrid,1:nlayer,1:nq)=pdq(1:ngrid,1:nlayer,1:nq)+ zdqdif(1:ngrid,1:nlayer,1:nq)
1102           dqsurf(1:ngrid,1:nq)=dqsurf(1:ngrid,1:nq) + zdqsdif(1:ngrid,1:nq)
1103         end if ! of if (tracer)
1104
1105
1106         ! test energy conservation
1107         !-------------------------
1108         if(enertest)then
1109         
1110            dEzdiff(:,:)=cpp*mass(:,:)*zdtdif(:,:)
1111            do ig = 1, ngrid
1112               dEdiff(ig)=SUM(dEzdiff (ig,:))+ sensibFlux(ig)! subtract flux to the ground
1113               dEzdiff(ig,1)= dEzdiff(ig,1)+ sensibFlux(ig)! subtract flux to the ground
1114            enddo
1115           
1116            call planetwide_sumval(dEdiff(:)*cell_area(:)/totarea_planet,dEtot)
1117            dEdiffs(:)=capcal(:)*zdtsdif(:)-zflubid(:)-sensibFlux(:)
1118            call planetwide_sumval(dEdiffs(:)*cell_area(:)/totarea_planet,dEtots)
1119            call planetwide_sumval(sensibFlux(:)*cell_area(:)/totarea_planet,AtmToSurf_TurbFlux)
1120           
1121            if (is_master) then
1122           
1123               if (UseTurbDiff) then
1124                         print*,'In TurbDiff sensible flux (atm=>surf) =',AtmToSurf_TurbFlux,' W m-2'
1125                         print*,'In TurbDiff non-cons atm nrj change   =',dEtot,' W m-2'
1126                  print*,'In TurbDiff (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1127               else
1128                         print*,'In vdifc sensible flux (atm=>surf)    =',AtmToSurf_TurbFlux,' W m-2'
1129                         print*,'In vdifc non-cons atm nrj change      =',dEtot,' W m-2'
1130                         print*,'In vdifc (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1131               end if
1132            endif ! end of 'is_master'
1133           
1134         ! JL12 : note that the black body radiative flux emitted by the surface has been updated by the implicit scheme but not given back elsewhere.
1135         endif ! end of 'enertest'
1136
1137
1138         ! Test water conservation.
1139         if(watertest.and.water)then
1140         
1141            call planetwide_sumval(massarea(:,:)*zdqdif(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dWtot_tmp)
1142            call planetwide_sumval(zdqsdif(:,igcm_h2o_vap)*cell_area(:)*ptimestep/totarea_planet,dWtots_tmp)
1143            do ig = 1, ngrid
1144               vdifcncons(ig)=SUM(mass(ig,:)*zdqdif(ig,:,igcm_h2o_vap))
1145            enddo
1146            call planetwide_sumval(massarea(:,:)*zdqdif(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1147            call planetwide_sumval(zdqsdif(:,igcm_h2o_ice)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1148            dWtot = dWtot + dWtot_tmp
1149            dWtots = dWtots + dWtots_tmp
1150            do ig = 1, ngrid
1151               vdifcncons(ig)=vdifcncons(ig) + SUM(mass(ig,:)*zdqdif(ig,:,igcm_h2o_ice))
1152            enddo           
1153            call planetwide_maxval(vdifcncons(:),nconsMAX)
1154
1155            if (is_master) then
1156               print*,'---------------------------------------------------------------'
1157               print*,'In difv atmospheric water change        =',dWtot,' kg m-2'
1158               print*,'In difv surface water change            =',dWtots,' kg m-2'
1159               print*,'In difv non-cons factor                 =',dWtot+dWtots,' kg m-2'
1160               print*,'In difv MAX non-cons factor             =',nconsMAX,' kg m-2 s-1'
1161            endif
1162
1163         endif ! end of 'watertest'
1164         !-------------------------
1165
1166      else ! calldifv
1167
1168         if(.not.newtonian)then
1169
1170            zdtsurf(1:ngrid) = zdtsurf(1:ngrid) + (fluxrad(1:ngrid) + fluxgrd(1:ngrid))/capcal(1:ngrid)
1171
1172         endif
1173
1174      endif ! end of 'calldifv'
1175
1176
1177!-------------------
1178!   IV. Convection :
1179!-------------------
1180     
1181! ~~~~~~~~~~~~~~~~~~~~~~~~~~
1182! IV.a Thermal plume model :
1183! ~~~~~~~~~~~~~~~~~~~~~~~~~~
1184     
1185      IF (calltherm) THEN
1186         
1187! AB: We need to evaporate ice before calling thermcell_main.
1188         IF (water) THEN
1189            CALL evap(ngrid,nlayer,nq,ptimestep,pt,pq,pdq,pdt,dqevap,dtevap,zqtherm,zttherm)
1190         ELSE
1191            zttherm(:,:)   = pt(:,:)   + pdt(:,:)   * ptimestep
1192            zqtherm(:,:,:) = pq(:,:,:) + pdq(:,:,:) * ptimestep
1193         ENDIF
1194         
1195         CALL thermcell_main(ngrid, nlayer, nq, ptimestep, firstcall,            &
1196                             pplay, pplev, pphi, zpopsk,                         &
1197                             pu, pv, zttherm, zqtherm,                           &
1198                             zdutherm, zdvtherm, zdttherm, zdqtherm,             &
1199                             fm, entr, detr, zw2, fraca)
1200         
1201         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer) + zdutherm(1:ngrid,1:nlayer)
1202         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer) + zdvtherm(1:ngrid,1:nlayer)
1203         pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer) + zdttherm(1:ngrid,1:nlayer)
1204         
1205         IF (tracer) THEN
1206            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqtherm(1:ngrid,1:nlayer,1:nq)
1207         ENDIF
1208         
1209      ENDIF ! end of 'calltherm'
1210     
1211! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1212! IV.b Dry convective adjustment :
1213! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1214
1215      if(calladj) then
1216
1217         zdh(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1218         zduadj(1:ngrid,1:nlayer)=0.0
1219         zdvadj(1:ngrid,1:nlayer)=0.0
1220         zdhadj(1:ngrid,1:nlayer)=0.0
1221
1222
1223         call convadj(ngrid,nlayer,nq,ptimestep,            &
1224                      pplay,pplev,zpopsk,                   &
1225                      pu,pv,zh,pq,                          &
1226                      pdu,pdv,zdh,pdq,                      &
1227                      zduadj,zdvadj,zdhadj,                 &
1228                      zdqadj)
1229
1230         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer) + zduadj(1:ngrid,1:nlayer)
1231         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer) + zdvadj(1:ngrid,1:nlayer)
1232         pdt(1:ngrid,1:nlayer)    = pdt(1:ngrid,1:nlayer) + zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer)
1233         zdtadj(1:ngrid,1:nlayer) = zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1234
1235         if(tracer) then
1236            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqadj(1:ngrid,1:nlayer,1:nq)
1237         end if
1238
1239         ! Test energy conservation
1240         if(enertest)then
1241            call planetwide_sumval(cpp*massarea(:,:)*zdtadj(:,:)/totarea_planet,dEtot)
1242            if (is_master) print*,'In convadj atmospheric energy change  =',dEtot,' W m-2'
1243         endif
1244
1245         ! Test water conservation
1246         if(watertest)then
1247            call planetwide_sumval(massarea(:,:)*zdqadj(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dWtot_tmp)
1248            do ig = 1, ngrid
1249               cadjncons(ig)=SUM(mass(ig,:)*zdqadj(ig,:,igcm_h2o_vap))
1250            enddo
1251            call planetwide_sumval(massarea(:,:)*zdqadj(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1252            dWtot = dWtot + dWtot_tmp
1253            do ig = 1, ngrid
1254               cadjncons(ig)=cadjncons(ig) + SUM(mass(ig,:)*zdqadj(ig,:,igcm_h2o_ice))
1255            enddo           
1256            call planetwide_maxval(cadjncons(:),nconsMAX)
1257
1258            if (is_master) then
1259               print*,'In convadj atmospheric water change     =',dWtot,' kg m-2'
1260               print*,'In convadj MAX non-cons factor          =',nconsMAX,' kg m-2 s-1'
1261            endif
1262           
1263         endif ! end of 'watertest'
1264         
1265      endif ! end of 'calladj'
1266!----------------------------------------------
1267!   Non orographic Gravity Waves:
1268!---------------------------------------------
1269      IF (calllott_nonoro) THEN
1270
1271         CALL nonoro_gwd_ran(ngrid,nlayer,ptimestep,pplay,  &
1272                    zmax_th,                                &! max altitude reached by thermals (m)
1273                    pt, pu, pv,                             &
1274                    pdt, pdu, pdv,                          &
1275                    zustrhi,zvstrhi,                        &
1276                    d_t_hin, d_u_hin, d_v_hin)
1277
1278!  Update tendencies
1279         pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)      &
1280                               + d_t_hin(1:ngrid,1:nlayer)
1281!        d_t_hin(:,:)= d_t_hin(:,:)/ptimestep ! K/s (for outputs?)
1282         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer)      &
1283                               + d_u_hin(1:ngrid,1:nlayer)
1284!        d_u_hin(:,:)= d_u_hin(:,:)/ptimestep ! (m/s)/s (for outputs?)
1285         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer)      &
1286                               + d_v_hin(1:ngrid,1:nlayer)
1287!        d_v_hin(:,:)= d_v_hin(:,:)/ptimestep ! (m/s)/s (for outputs?)
1288         print*,'du_nonoro: max ', maxval(d_u_hin), 'min ', minval(d_u_hin)
1289         print*,'dv_nonoro: max ', maxval(d_v_hin), 'min ', minval(d_v_hin)
1290
1291      ENDIF ! of IF (calllott_nonoro)
1292
1293
1294     
1295!-----------------------------------------------
1296!   V. Carbon dioxide condensation-sublimation :
1297!-----------------------------------------------
1298
1299      if (co2cond) then
1300     
1301         if (.not.tracer) then
1302            print*,'We need a CO2 ice tracer to condense CO2'
1303            call abort
1304         endif
1305         call condense_co2(ngrid,nlayer,nq,ptimestep,                    &
1306                           capcal,pplay,pplev,tsurf,pt,                  &
1307                           pdt,zdtsurf,pq,pdq,                           &
1308                           qsurf,zdqsurfc,albedo,emis,                   &
1309                           albedo_bareground,albedo_co2_ice_SPECTV,      &
1310                           zdtc,zdtsurfc,pdpsrf,zdqc)
1311
1312         pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+zdtc(1:ngrid,1:nlayer)
1313         zdtsurf(1:ngrid)      = zdtsurf(1:ngrid) + zdtsurfc(1:ngrid)
1314
1315         pdq(1:ngrid,1:nlayer,1:nq)   = pdq(1:ngrid,1:nlayer,1:nq)+ zdqc(1:ngrid,1:nlayer,1:nq)
1316         dqsurf(1:ngrid,igcm_co2_ice) = dqsurf(1:ngrid,igcm_co2_ice) + zdqsurfc(1:ngrid)
1317
1318         ! test energy conservation
1319         if(enertest)then
1320            call planetwide_sumval(cpp*massarea(:,:)*zdtc(:,:)/totarea_planet,dEtot)
1321            call planetwide_sumval(capcal(:)*zdtsurfc(:)*cell_area(:)/totarea_planet,dEtots)
1322            if (is_master) then
1323               print*,'In co2cloud atmospheric energy change   =',dEtot,' W m-2'
1324               print*,'In co2cloud surface energy change       =',dEtots,' W m-2'
1325            endif
1326         endif
1327
1328      endif  ! end of 'co2cond'
1329
1330
1331!---------------------------------------------
1332!   VI. Specific parameterizations for tracers
1333!---------------------------------------------
1334
1335      if (tracer) then
1336     
1337  ! ---------------------
1338  !   VI.1. Water and ice
1339  ! ---------------------
1340         if (water) then
1341           
1342            ! Water ice condensation in the atmosphere
1343            if(watercond.and.(RLVTT.gt.1.e-8))then
1344               
1345               if (.not.calltherm) then
1346                  dqmoist(1:ngrid,1:nlayer,1:nq)=0.
1347                  dtmoist(1:ngrid,1:nlayer)=0.
1348                 
1349                  ! Moist Convective Adjustment.
1350                  ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1351                  call moistadj(ngrid,nlayer,nq,pt,pq,pdq,pplev,pplay,dtmoist,dqmoist,ptimestep,rneb_man)
1352               
1353                  pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap)     &
1354                                                     + dqmoist(1:ngrid,1:nlayer,igcm_h2o_vap)
1355                  pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice)     &
1356                                                     + dqmoist(1:ngrid,1:nlayer,igcm_h2o_ice)
1357                  pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+dtmoist(1:ngrid,1:nlayer)
1358                   
1359                  ! Test energy conservation.
1360                  if(enertest)then
1361                     call planetwide_sumval(cpp*massarea(:,:)*dtmoist(:,:)/totarea_planet,dEtot)
1362                     call planetwide_maxval(dtmoist(:,:),dtmoist_max)
1363                     call planetwide_minval(dtmoist(:,:),dtmoist_min)
1364                     madjdEz(:,:)=cpp*mass(:,:)*dtmoist(:,:)
1365                     
1366                     do ig=1,ngrid
1367                        madjdE(ig) = cpp*SUM(mass(:,:)*dtmoist(:,:))
1368                     enddo
1369                     
1370                     if (is_master) then
1371                        print*,'In moistadj atmospheric energy change   =',dEtot,' W m-2'
1372                        print*,'In moistadj MAX atmospheric energy change   =',dtmoist_max*ptimestep,'K/step'
1373                        print*,'In moistadj MIN atmospheric energy change   =',dtmoist_min*ptimestep,'K/step'
1374                     endif
1375                     
1376                     call planetwide_sumval(massarea(:,:)*dqmoist(:,:,igcm_h2o_vap)*ptimestep/totarea_planet+        &
1377                                            massarea(:,:)*dqmoist(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1378                     if (is_master) print*,'In moistadj atmospheric water change    =',dWtot,' kg m-2'
1379                     
1380                  endif ! end of 'enertest'
1381               endif ! end of '.not.calltherm'
1382               
1383               ! Large scale condensation/evaporation.
1384               ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1385               call largescale(ngrid,nlayer,nq,ptimestep,pplev,pplay,pt,pq,pdt,pdq,dtlscale,dqvaplscale,dqcldlscale,rneb_lsc)
1386
1387               pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+dtlscale(1:ngrid,1:nlayer)
1388               pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap)+dqvaplscale(1:ngrid,1:nlayer)
1389               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice)+dqcldlscale(1:ngrid,1:nlayer)
1390
1391               ! Test energy conservation.
1392               if(enertest)then
1393                  lscaledEz(:,:) = cpp*mass(:,:)*dtlscale(:,:)
1394                  do ig=1,ngrid
1395                     lscaledE(ig) = cpp*SUM(mass(:,:)*dtlscale(:,:))
1396                  enddo
1397                  call planetwide_sumval(cpp*massarea(:,:)*dtlscale(:,:)/totarea_planet,dEtot)
1398
1399                  if (is_master) print*,'In largescale atmospheric energy change =',dEtot,' W m-2'
1400
1401                  ! Test water conservation.
1402                  call planetwide_sumval(massarea(:,:)*dqvaplscale(:,:)*ptimestep/totarea_planet+        &
1403                                           SUM(massarea(:,:)*dqcldlscale(:,:))*ptimestep/totarea_planet,dWtot)
1404                       
1405                  if (is_master) print*,'In largescale atmospheric water change  =',dWtot,' kg m-2'
1406               endif ! end of 'enertest'
1407
1408               ! Compute cloud fraction.
1409               do l = 1, nlayer
1410                  do ig=1,ngrid
1411                     cloudfrac(ig,l)=MAX(rneb_lsc(ig,l),rneb_man(ig,l))
1412                  enddo
1413               enddo
1414
1415            endif ! end of 'watercond'
1416           
1417            ! Water ice / liquid precipitation.
1418            ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1419            zdqrain(1:ngrid,1:nlayer,1:nq) = 0.0  !JL18 need to do that everytimestep if mass redis is on.
1420
1421            if(waterrain)then
1422
1423               zdqsrain(1:ngrid)    = 0.0
1424               zdqssnow(1:ngrid)    = 0.0
1425
1426               call rain(ngrid,nlayer,nq,ptimestep,pplev,pplay,pt,pdt,pq,pdq,            &
1427                         zdtrain,zdqrain,zdqsrain,zdqssnow,reevap_precip,cloudfrac)
1428
1429               pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap) &
1430                                                  + zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)
1431               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice) &
1432                                                  + zdqrain(1:ngrid,1:nlayer,igcm_h2o_ice)
1433               pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+zdtrain(1:ngrid,1:nlayer)
1434               
1435               dqsurf(1:ngrid,igcm_h2o_vap) = dqsurf(1:ngrid,igcm_h2o_vap)+zdqsrain(1:ngrid)
1436               dqsurf(1:ngrid,igcm_h2o_ice) = dqsurf(1:ngrid,igcm_h2o_ice)+zdqssnow(1:ngrid)
1437
1438               ! Test energy conservation.
1439               if(enertest)then
1440               
1441                  call planetwide_sumval(cpp*massarea(:,:)*zdtrain(:,:)/totarea_planet,dEtot)
1442                  if (is_master) print*,'In rain atmospheric T energy change       =',dEtot,' W m-2'
1443                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_ice)/totarea_planet*RLVTT/cpp,dItot_tmp)
1444                  call planetwide_sumval(cell_area(:)*zdqssnow(:)/totarea_planet*RLVTT/cpp,dItot)
1445                  dItot = dItot + dItot_tmp
1446                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dVtot_tmp)
1447                  call planetwide_sumval(cell_area(:)*zdqsrain(:)/totarea_planet*RLVTT/cpp,dVtot)
1448                  dVtot = dVtot + dVtot_tmp
1449                  dEtot = dItot + dVtot
1450                 
1451                  if (is_master) then
1452                     print*,'In rain dItot =',dItot,' W m-2'
1453                     print*,'In rain dVtot =',dVtot,' W m-2'
1454                     print*,'In rain atmospheric L energy change       =',dEtot,' W m-2'
1455                  endif
1456
1457                  ! Test water conservation
1458                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_vap)*ptimestep/totarea_planet+        &
1459                          massarea(:,:)*zdqrain(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1460                  call planetwide_sumval((zdqsrain(:)+zdqssnow(:))*cell_area(:)*ptimestep/totarea_planet,dWtots)
1461                 
1462                  if (is_master) then
1463                          print*,'In rain atmospheric water change        =',dWtot,' kg m-2'
1464                          print*,'In rain surface water change            =',dWtots,' kg m-2'
1465                          print*,'In rain non-cons factor                 =',dWtot+dWtots,' kg m-2'
1466                  endif
1467                 
1468               endif ! end of 'enertest'
1469
1470            end if ! enf of 'waterrain'
1471           
1472         end if ! end of 'water'
1473
1474 ! -------------------------
1475  !   VI.2. Photochemistry
1476  ! -------------------------
1477
1478#ifndef MESOSCALE
1479         IF (photochem) then
1480
1481             DO ig=1,ngrid
1482               array_zero1(ig)=0.0
1483               DO l=1,nlayer
1484                 array_zero2(ig,l)=0.
1485               ENDDO
1486             ENDDO
1487
1488            call calchim_asis(ngrid,nlayer,nq,                          &
1489                        ptimestep,pplay,pplev,pt,pdt,dist_star,mu0,     &
1490                        fract,zzlev,zzlay,zday,pq,pdq,zdqchim,zdqschim, &
1491                        array_zero1,array_zero1,                        &
1492                        pu,pdu,pv,pdv,array_zero2,array_zero2)
1493
1494           ! increment values of tracers:
1495           DO iq=1,nq ! loop on all tracers; tendencies for non-chemistry
1496                      ! tracers is zero anyways
1497             DO l=1,nlayer
1498               DO ig=1,ngrid
1499                 pdq(ig,l,iq)=pdq(ig,l,iq)+zdqchim(ig,l,iq)
1500               ENDDO
1501             ENDDO
1502           ENDDO ! of DO iq=1,nq
1503
1504
1505           ! increment surface values of tracers:
1506           DO iq=1,nq ! loop on all tracers; tendencies for non-chemistry
1507                      ! tracers is zero anyways
1508             DO ig=1,ngrid
1509!               dqsurf(ig,iq)=dqsurf(ig,iq)+zdqschim(ig,iq)
1510             ENDDO
1511           ENDDO ! of DO iq=1,nq
1512
1513         END IF  ! of IF (photochem)
1514#endif
1515
1516
1517  ! -------------------------
1518  !   VI.3. Aerosol particles
1519  ! -------------------------
1520
1521         ! Sedimentation.
1522         if (sedimentation) then
1523       
1524            zdqsed(1:ngrid,1:nlayer,1:nq) = 0.0
1525            zdqssed(1:ngrid,1:nq)  = 0.0
1526
1527            if(watertest)then
1528           
1529               iq=igcm_h2o_ice
1530               call planetwide_sumval(massarea(:,:)*pq(:,:,iq)*ptimestep/totarea_planet,dWtot)
1531               call planetwide_sumval(massarea(:,:)*pdq(:,:,iq)*ptimestep/totarea_planet,dWtots)
1532               if (is_master) then
1533                        print*,'Before sedim pq  =',dWtot,' kg m-2'
1534                  print*,'Before sedim pdq =',dWtots,' kg m-2'
1535               endif
1536            endif
1537           
1538            call callsedim(ngrid,nlayer,ptimestep,           &
1539                          pplev,zzlev,pt,pdt,pq,pdq,        &
1540                          zdqsed,zdqssed,nq)
1541
1542            if(watertest)then
1543               iq=igcm_h2o_ice
1544               call planetwide_sumval(massarea(:,:)*pq(:,:,iq)*ptimestep/totarea_planet,dWtot)
1545               call planetwide_sumval(massarea(:,:)*pdq(:,:,iq)*ptimestep/totarea_planet,dWtots)
1546               if (is_master) then
1547                        print*,'After sedim pq  =',dWtot,' kg m-2'
1548                        print*,'After sedim pdq =',dWtots,' kg m-2'
1549                 endif
1550            endif
1551
1552            ! Whether it falls as rain or snow depends only on the surface temperature
1553            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqsed(1:ngrid,1:nlayer,1:nq)
1554            dqsurf(1:ngrid,1:nq) = dqsurf(1:ngrid,1:nq) + zdqssed(1:ngrid,1:nq)
1555
1556            ! Test water conservation
1557            if(watertest)then
1558               call planetwide_sumval(massarea(:,:)*(zdqsed(:,:,igcm_h2o_vap)+zdqsed(:,:,igcm_h2o_ice))*ptimestep/totarea_planet,dWtot)
1559               call planetwide_sumval((zdqssed(:,igcm_h2o_vap)+zdqssed(:,igcm_h2o_ice))*cell_area(:)*ptimestep/totarea_planet,dWtots)
1560               if (is_master) then
1561                        print*,'In sedim atmospheric ice change         =',dWtot,' kg m-2'
1562                        print*,'In sedim surface ice change             =',dWtots,' kg m-2'
1563                        print*,'In sedim non-cons factor                =',dWtot+dWtots,' kg m-2'
1564               endif
1565            endif
1566
1567         end if ! end of 'sedimentation'
1568
1569
1570  ! ---------------
1571  !   VI.4. Updates
1572  ! ---------------
1573
1574         ! Updating Atmospheric Mass and Tracers budgets.
1575         if(mass_redistrib) then
1576
1577            zdmassmr(1:ngrid,1:nlayer) = mass(1:ngrid,1:nlayer) *     &
1578               (   zdqevap(1:ngrid,1:nlayer)                          &
1579                 + zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1580                 + dqmoist(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1581                 + dqvaplscale(1:ngrid,1:nlayer) )
1582
1583            do ig = 1, ngrid
1584               zdmassmr_col(ig)=SUM(zdmassmr(ig,1:nlayer))
1585            enddo
1586           
1587            call writediagfi(ngrid,"mass_evap","mass gain"," ",3,zdmassmr)
1588            call writediagfi(ngrid,"mass_evap_col","mass gain col"," ",2,zdmassmr_col)
1589            call writediagfi(ngrid,"mass","mass","kg/m2",3,mass)
1590
1591            call mass_redistribution(ngrid,nlayer,nq,ptimestep,                     &
1592                                     rnat,capcal,pplay,pplev,pt,tsurf,pq,qsurf,     &
1593                                     pu,pv,pdt,zdtsurf,pdq,pdu,pdv,zdmassmr,        &
1594                                     zdtmr,zdtsurfmr,zdpsrfmr,zdumr,zdvmr,zdqmr,zdqsurfmr)
1595         
1596            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqmr(1:ngrid,1:nlayer,1:nq)
1597            dqsurf(1:ngrid,1:nq)       = dqsurf(1:ngrid,1:nq) + zdqsurfmr(1:ngrid,1:nq)
1598            pdt(1:ngrid,1:nlayer)      = pdt(1:ngrid,1:nlayer) + zdtmr(1:ngrid,1:nlayer)
1599            pdu(1:ngrid,1:nlayer)      = pdu(1:ngrid,1:nlayer) + zdumr(1:ngrid,1:nlayer)
1600            pdv(1:ngrid,1:nlayer)      = pdv(1:ngrid,1:nlayer) + zdvmr(1:ngrid,1:nlayer)
1601            pdpsrf(1:ngrid)            = pdpsrf(1:ngrid) + zdpsrfmr(1:ngrid)
1602            zdtsurf(1:ngrid)           = zdtsurf(1:ngrid) + zdtsurfmr(1:ngrid)
1603           
1604         endif
1605
1606  ! ------------------
1607  !   VI.5. Slab Ocean
1608  ! ------------------
1609
1610         if (ok_slab_ocean)then
1611
1612            do ig=1,ngrid
1613               qsurfint(:,igcm_h2o_ice)=qsurf(:,igcm_h2o_ice)
1614            enddo
1615
1616            call ocean_slab_ice(ptimestep,                          &
1617                                ngrid, knindex, tsea_ice, fluxrad,  &
1618                                zdqssnow, qsurf(:,igcm_h2o_ice),    &
1619                              - zdqsdif(:,igcm_h2o_vap),            &
1620                                flux_sens_lat,tsea_ice, pctsrf_sic, &
1621                                taux,tauy,icount)
1622
1623
1624            call ocean_slab_get_vars(ngrid,tslab,      &
1625                                     sea_ice, flux_o,  &
1626                                     flux_g, dt_hdiff, &
1627                                     dt_ekman)
1628   
1629            do ig=1,ngrid
1630               if (nint(rnat(ig)).eq.1)then
1631                  tslab(ig,1) = 0.
1632                  tslab(ig,2) = 0.
1633                  tsea_ice(ig) = 0.
1634                  sea_ice(ig) = 0.
1635                  pctsrf_sic(ig) = 0.
1636                  qsurf(ig,igcm_h2o_ice) = qsurfint(ig,igcm_h2o_ice)
1637               endif
1638            enddo
1639
1640         endif ! end of 'ok_slab_ocean'
1641
1642  ! -----------------------------
1643  !   VI.6. Surface Tracer Update
1644  ! -----------------------------
1645
1646         if(hydrology)then
1647           
1648            call hydrol(ngrid,nq,ptimestep,rnat,tsurf,qsurf,dqsurf,dqs_hyd, &
1649                        capcal,albedo,albedo_bareground,                    &
1650                        albedo_snow_SPECTV,albedo_co2_ice_SPECTV,           &
1651                        mu0,zdtsurf,zdtsurf_hyd,hice,pctsrf_sic,            &
1652                        sea_ice)
1653
1654            zdtsurf(1:ngrid)     = zdtsurf(1:ngrid) + zdtsurf_hyd(1:ngrid)
1655            dqsurf(1:ngrid,1:nq) = dqsurf(1:ngrid,1:nq) + dqs_hyd(1:ngrid,1:nq)
1656           
1657            qsurf(1:ngrid,1:nq) = qsurf(1:ngrid,1:nq) + ptimestep*dqsurf(1:ngrid,1:nq)
1658
1659            ! Test energy conservation
1660            if(enertest)then
1661               call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf_hyd(:)/totarea_planet,dEtots)
1662               if (is_master) print*,'In hydrol surface energy change     =',dEtots,' W m-2'
1663            endif
1664
1665            ! test water conservation
1666            if(watertest)then
1667               call planetwide_sumval(dqs_hyd(:,igcm_h2o_ice)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1668               if (is_master) print*,'In hydrol surface ice change            =',dWtots,' kg m-2'
1669                  call planetwide_sumval(dqs_hyd(:,igcm_h2o_vap)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1670               if (is_master) then
1671                  print*,'In hydrol surface water change          =',dWtots,' kg m-2'
1672                  print*,'---------------------------------------------------------------'
1673               endif
1674            endif
1675
1676         else ! of if (hydrology)
1677
1678            qsurf(1:ngrid,1:nq) = qsurf(1:ngrid,1:nq) + ptimestep*dqsurf(1:ngrid,1:nq)
1679
1680         end if ! of if (hydrology)
1681
1682         ! Add qsurf to qsurf_hist, which is what we save in diagfi.nc. At the same time, we set the water
1683         ! content of ocean gridpoints back to zero, in order to avoid rounding errors in vdifc, rain.
1684         qsurf_hist(:,:) = qsurf(:,:)
1685
1686         if(ice_update)then
1687            ice_min(1:ngrid)=min(ice_min(1:ngrid),qsurf(1:ngrid,igcm_h2o_ice))
1688         endif
1689
1690      endif! end of if 'tracer'
1691
1692
1693!------------------------------------------------
1694!   VII. Surface and sub-surface soil temperature
1695!------------------------------------------------
1696
1697
1698      ! Increment surface temperature
1699      if(ok_slab_ocean)then
1700         do ig=1,ngrid
1701           if (nint(rnat(ig)).eq.0)then
1702             zdtsurf(ig)= (tslab(ig,1)              &
1703             + pctsrf_sic(ig)*(tsea_ice(ig)-tslab(ig,1))-tsurf(ig))/ptimestep
1704           endif
1705           tsurf(ig)=tsurf(ig)+ptimestep*zdtsurf(ig)
1706         enddo
1707   
1708      else
1709        tsurf(1:ngrid)=tsurf(1:ngrid)+ptimestep*zdtsurf(1:ngrid)
1710      endif ! end of 'ok_slab_ocean'
1711
1712
1713      ! Compute soil temperatures and subsurface heat flux.
1714      if (callsoil) then
1715         call soil(ngrid,nsoilmx,.false.,lastcall,inertiedat,   &
1716                   ptimestep,tsurf,tsoil,capcal,fluxgrd)     
1717      endif
1718
1719
1720      if (ok_slab_ocean) then
1721     
1722         do ig=1,ngrid
1723         
1724            fluxgrdocean(ig)=fluxgrd(ig)
1725            if (nint(rnat(ig)).eq.0) then
1726               capcal(ig)=capcalocean
1727               fluxgrd(ig)=0.
1728               fluxgrdocean(ig)=pctsrf_sic(ig)*flux_g(ig)+(1-pctsrf_sic(ig))*(dt_hdiff(ig,1)+dt_ekman(ig,1))
1729               do iq=1,nsoilmx
1730                  tsoil(ig,iq)=tsurf(ig)
1731               enddo
1732               if (pctsrf_sic(ig).gt.0.5) then
1733                  capcal(ig)=capcalseaice
1734                  if (qsurf(ig,igcm_h2o_ice).gt.0.) then
1735                     capcal(ig)=capcalsno
1736                  endif
1737               endif               
1738            endif
1739           
1740         enddo
1741         
1742      endif !end of 'ok_slab_ocean'
1743
1744
1745      ! Test energy conservation
1746      if(enertest)then
1747         call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf(:)/totarea_planet,dEtots)         
1748         if (is_master) print*,'Surface energy change                 =',dEtots,' W m-2'
1749      endif
1750
1751
1752!---------------------------------------------------
1753!   VIII. Perform diagnostics and write output files
1754!---------------------------------------------------
1755
1756      ! Note : For output only: the actual model integration is performed in the dynamics.
1757
1758
1759 
1760      ! Temperature, zonal and meridional winds.
1761      zt(1:ngrid,1:nlayer) = pt(1:ngrid,1:nlayer) + pdt(1:ngrid,1:nlayer)*ptimestep
1762      zu(1:ngrid,1:nlayer) = pu(1:ngrid,1:nlayer) + pdu(1:ngrid,1:nlayer)*ptimestep
1763      zv(1:ngrid,1:nlayer) = pv(1:ngrid,1:nlayer) + pdv(1:ngrid,1:nlayer)*ptimestep
1764     
1765      ! Recast thermal plume vertical velocity array for outputs
1766      IF (calltherm) THEN
1767         DO ig=1,ngrid
1768            DO l=1,nlayer
1769               zw2_bis(ig,l) = zw2(ig,l)
1770               fm_bis(ig,l) = fm(ig,l)
1771            ENDDO
1772         ENDDO
1773      ENDIF
1774
1775      ! Diagnostic.
1776      zdtdyn(1:ngrid,1:nlayer)     = (pt(1:ngrid,1:nlayer)-ztprevious(1:ngrid,1:nlayer)) / ptimestep
1777      ztprevious(1:ngrid,1:nlayer) = zt(1:ngrid,1:nlayer)
1778
1779      zdudyn(1:ngrid,1:nlayer)     = (pu(1:ngrid,1:nlayer)-zuprevious(1:ngrid,1:nlayer)) / ptimestep
1780      zuprevious(1:ngrid,1:nlayer) = zu(1:ngrid,1:nlayer)
1781
1782      if(firstcall)then
1783         zdtdyn(1:ngrid,1:nlayer)=0.0
1784         zdudyn(1:ngrid,1:nlayer)=0.0
1785      endif
1786
1787      ! Dynamical heating diagnostic.
1788      do ig=1,ngrid
1789         fluxdyn(ig)= SUM(zdtdyn(ig,:) *mass(ig,:))*cpp
1790      enddo
1791
1792      ! Tracers.
1793      zq(1:ngrid,1:nlayer,1:nq) = pq(1:ngrid,1:nlayer,1:nq) + pdq(1:ngrid,1:nlayer,1:nq)*ptimestep
1794
1795      ! Surface pressure.
1796      ps(1:ngrid) = pplev(1:ngrid,1) + pdpsrf(1:ngrid)*ptimestep
1797
1798
1799
1800      ! Surface and soil temperature information
1801      call planetwide_sumval(cell_area(:)*tsurf(:)/totarea_planet,Ts1)
1802      call planetwide_minval(tsurf(:),Ts2)
1803      call planetwide_maxval(tsurf(:),Ts3)
1804      if(callsoil)then
1805         TsS = SUM(cell_area(:)*tsoil(:,nsoilmx))/totarea        ! mean temperature at bottom soil layer
1806         if (is_master) then
1807            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]             ave[Tdeep]'
1808            print*,Ts1,Ts2,Ts3,TsS
1809         end if
1810      else
1811         if (is_master) then
1812            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]'
1813            print*,Ts1,Ts2,Ts3
1814         endif
1815      end if
1816
1817
1818      ! Check the energy balance of the simulation during the run
1819      if(corrk)then
1820
1821         call planetwide_sumval(cell_area(:)*fluxtop_dn(:)/totarea_planet,ISR)
1822         call planetwide_sumval(cell_area(:)*fluxabs_sw(:)/totarea_planet,ASR)
1823         call planetwide_sumval(cell_area(:)*fluxtop_lw(:)/totarea_planet,OLR)
1824         call planetwide_sumval(cell_area(:)*fluxgrd(:)/totarea_planet,GND)
1825         call planetwide_sumval(cell_area(:)*fluxdyn(:)/totarea_planet,DYN)
1826         do ig=1,ngrid
1827            if(fluxtop_dn(ig).lt.0.0)then
1828               print*,'fluxtop_dn has gone crazy'
1829               print*,'fluxtop_dn=',fluxtop_dn(ig)
1830               print*,'tau_col=',tau_col(ig)
1831               print*,'aerosol=',aerosol(ig,:,:)
1832               print*,'temp=   ',pt(ig,:)
1833               print*,'pplay=  ',pplay(ig,:)
1834               call abort
1835            endif
1836         end do
1837                     
1838         if(ngrid.eq.1)then
1839            DYN=0.0
1840         endif
1841         
1842         if (is_master) then
1843            print*,'  ISR            ASR            OLR            GND            DYN [W m^-2]'
1844            print*, ISR,ASR,OLR,GND,DYN
1845         endif
1846
1847         if(enertest .and. is_master)then
1848            print*,'SW flux/heating difference SW++ - ASR = ',dEtotSW+dEtotsSW-ASR,' W m-2'
1849            print*,'LW flux/heating difference LW++ - OLR = ',dEtotLW+dEtotsLW+OLR,' W m-2'
1850            print*,'LW energy balance LW++ + ASR = ',dEtotLW+dEtotsLW+ASR,' W m-2'
1851         endif
1852
1853         if(meanOLR .and. is_master)then
1854            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1855               ! to record global radiative balance
1856               open(92,file="rad_bal.out",form='formatted',position='append')
1857               write(92,*) zday,ISR,ASR,OLR
1858               close(92)
1859               open(93,file="tem_bal.out",form='formatted',position='append')
1860               if(callsoil)then
1861                         write(93,*) zday,Ts1,Ts2,Ts3,TsS
1862               else
1863                  write(93,*) zday,Ts1,Ts2,Ts3
1864               endif
1865               close(93)
1866            endif
1867         endif
1868
1869      endif ! end of 'corrk'
1870
1871
1872      ! Diagnostic to test radiative-convective timescales in code.
1873      if(testradtimes)then
1874         open(38,file="tau_phys.out",form='formatted',position='append')
1875         ig=1
1876         do l=1,nlayer
1877            write(38,*) -1./pdt(ig,l),pt(ig,l),pplay(ig,l)
1878         enddo
1879         close(38)
1880         print*,'As testradtimes enabled,'
1881         print*,'exiting physics on first call'
1882         call abort
1883      endif
1884
1885
1886      ! Compute column amounts (kg m-2) if tracers are enabled.
1887      if(tracer)then
1888         qcol(1:ngrid,1:nq)=0.0
1889         do iq=1,nq
1890            do ig=1,ngrid
1891               qcol(ig,iq) = SUM( zq(ig,1:nlayer,iq) * mass(ig,1:nlayer))
1892            enddo
1893         enddo
1894
1895         ! Generalised for arbitrary aerosols now. By LK
1896         reffcol(1:ngrid,1:naerkind)=0.0
1897         if(co2cond.and.(iaero_co2.ne.0))then
1898            call co2_reffrad(ngrid,nlayer,nq,zq,reffrad(1,1,iaero_co2))
1899            do ig=1,ngrid
1900               reffcol(ig,iaero_co2) = SUM(zq(ig,1:nlayer,igcm_co2_ice)*reffrad(ig,1:nlayer,iaero_co2)*mass(ig,1:nlayer))
1901            enddo
1902         endif
1903         if(water.and.(iaero_h2o.ne.0))then
1904            call h2o_reffrad(ngrid,nlayer,zq(1,1,igcm_h2o_ice),zt, &
1905                             reffrad(1,1,iaero_h2o),nueffrad(1,1,iaero_h2o))
1906            do ig=1,ngrid
1907               reffcol(ig,iaero_h2o) = SUM(zq(ig,1:nlayer,igcm_h2o_ice)*reffrad(ig,1:nlayer,iaero_h2o)*mass(ig,1:nlayer))
1908            enddo
1909         endif
1910
1911      endif ! end of 'tracer'
1912
1913
1914      ! Test for water conservation.
1915      if(water)then
1916
1917         call planetwide_sumval(cell_area(:)*qsurf_hist(:,igcm_h2o_ice)/totarea_planet,icesrf)
1918         call planetwide_sumval(cell_area(:)*qsurf_hist(:,igcm_h2o_vap)/totarea_planet,liqsrf)
1919         call planetwide_sumval(cell_area(:)*qcol(:,igcm_h2o_ice)/totarea_planet,icecol)
1920         call planetwide_sumval(cell_area(:)*qcol(:,igcm_h2o_vap)/totarea_planet,vapcol)
1921
1922         h2otot = icesrf + liqsrf + icecol + vapcol
1923         
1924         if (is_master) then
1925            print*,' Total water amount [kg m^-2]: ',h2otot
1926            print*,' Surface ice    Surface liq.   Atmos. con.     Atmos. vap. [kg m^-2] '
1927            print*, icesrf,liqsrf,icecol,vapcol
1928         endif
1929
1930         if(meanOLR .and. is_master)then
1931            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1932               ! to record global water balance
1933               open(98,file="h2o_bal.out",form='formatted',position='append')
1934               write(98,*) zday,icesrf,liqsrf,icecol,vapcol
1935               close(98)
1936            endif
1937         endif
1938
1939      endif
1940
1941
1942      ! Calculate RH (Relative Humidity) for diagnostic.
1943      if(water)then
1944     
1945         do l = 1, nlayer
1946            do ig=1,ngrid
1947               call Psat_water(zt(ig,l),pplay(ig,l),psat_tmp,qsat(ig,l))
1948               RH(ig,l) = zq(ig,l,igcm_h2o_vap) / qsat(ig,l)
1949            enddo
1950         enddo
1951
1952         ! Compute maximum possible H2O column amount (100% saturation).
1953         do ig=1,ngrid
1954            H2Omaxcol(ig) = SUM( qsat(ig,:) * mass(ig,:))
1955         enddo
1956
1957      endif ! end of 'water'
1958
1959
1960      if (is_master) print*,'--> Ls =',zls*180./pi
1961     
1962     
1963!----------------------------------------------------------------------
1964!        Writing NetCDF file  "RESTARTFI" at the end of the run
1965!----------------------------------------------------------------------
1966
1967!        Note: 'restartfi' is stored just before dynamics are stored
1968!              in 'restart'. Between now and the writting of 'restart',
1969!              there will have been the itau=itau+1 instruction and
1970!              a reset of 'time' (lastacll = .true. when itau+1= itaufin)
1971!              thus we store for time=time+dtvr
1972
1973
1974
1975      if(lastcall) then
1976         ztime_fin = ptime + ptimestep/(float(iphysiq)*daysec)
1977
1978         ! Update surface ice distribution to iterate to steady state if requested
1979         if(ice_update)then
1980
1981            do ig=1,ngrid
1982
1983               delta_ice = (qsurf(ig,igcm_h2o_ice)-ice_initial(ig))
1984               
1985               ! add multiple years of evolution
1986               qsurf_hist(ig,igcm_h2o_ice) = qsurf_hist(ig,igcm_h2o_ice) + delta_ice*icetstep
1987
1988               ! if ice has gone -ve, set to zero
1989               if(qsurf_hist(ig,igcm_h2o_ice).lt.0.0)then
1990                  qsurf_hist(ig,igcm_h2o_ice) = 0.0
1991               endif
1992
1993               ! if ice is seasonal, set to zero (NEW)
1994               if(ice_min(ig).lt.0.01)then
1995                  qsurf_hist(ig,igcm_h2o_ice) = 0.0
1996               endif
1997
1998            enddo
1999
2000            ! enforce ice conservation
2001            ice_tot= SUM(qsurf_hist(:,igcm_h2o_ice)*cell_area(:) )/SUM(cell_area(:))
2002            qsurf_hist(:,igcm_h2o_ice) = qsurf_hist(:,igcm_h2o_ice)*(icesrf/ice_tot)
2003
2004         endif
2005#ifndef MESOSCALE
2006         
2007         if (ngrid.ne.1) then
2008            write(*,*)'PHYSIQ: for physdem ztime_fin =',ztime_fin
2009           
2010            call physdem1("restartfi.nc",nsoilmx,ngrid,nlayer,nq, &
2011                          ptimestep,ztime_fin,                    &
2012                          tsurf,tsoil,emis,q2,qsurf_hist,         &
2013                          cloudfrac,totcloudfrac,hice,            &
2014                          rnat,pctsrf_sic,tslab,tsea_ice,sea_ice)
2015         endif
2016#endif
2017         if(ok_slab_ocean) then
2018            call ocean_slab_final!(tslab, seaice)
2019         end if
2020
2021    endif ! end of 'lastcall'
2022
2023
2024!-----------------------------------
2025!        Saving statistics :
2026!-----------------------------------
2027
2028!    Note :("stats" stores and accumulates 8 key variables in file "stats.nc"
2029!           which can later be used to make the statistic files of the run:
2030!           "stats")          only possible in 3D runs !!!
2031
2032         
2033      if (callstats) then
2034
2035         call wstats(ngrid,"ps","Surface pressure","Pa",2,ps)
2036         call wstats(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2037         call wstats(ngrid,"fluxsurf_lw",                               &
2038                     "Thermal IR radiative flux to surface","W.m-2",2,  &
2039                     fluxsurf_lw)
2040         call wstats(ngrid,"fluxtop_lw",                                &
2041                     "Thermal IR radiative flux to space","W.m-2",2,    &
2042                     fluxtop_lw)
2043                     
2044!            call wstats(ngrid,"fluxsurf_sw",                               &
2045!                        "Solar radiative flux to surface","W.m-2",2,       &
2046!                         fluxsurf_sw_tot)                     
2047!            call wstats(ngrid,"fluxtop_sw",                                &
2048!                        "Solar radiative flux to space","W.m-2",2,         &
2049!                        fluxtop_sw_tot)
2050
2051
2052         call wstats(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
2053         call wstats(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
2054         call wstats(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
2055         !call wstats(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
2056         !call wstats(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
2057         call wstats(ngrid,"p","Pressure","Pa",3,pplay)
2058         call wstats(ngrid,"temp","Atmospheric temperature","K",3,zt)
2059         call wstats(ngrid,"u","Zonal (East-West) wind","m.s-1",3,zu)
2060         call wstats(ngrid,"v","Meridional (North-South) wind","m.s-1",3,zv)
2061         call wstats(ngrid,"w","Vertical (down-up) wind","m.s-1",3,pw)
2062         call wstats(ngrid,"q2","Boundary layer eddy kinetic energy","m2.s-2",3,q2)
2063
2064         if (tracer) then
2065            do iq=1,nq
2066               call wstats(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
2067               call wstats(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2068                           'kg m^-2',2,qsurf(1,iq) )
2069               call wstats(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
2070                          'kg m^-2',2,qcol(1,iq) )
2071                         
2072!              call wstats(ngrid,trim(noms(iq))//'_reff',                          &
2073!                          trim(noms(iq))//'_reff',                                   &
2074!                          'm',3,reffrad(1,1,iq))
2075
2076            end do
2077           
2078            if (water) then
2079               vmr=zq(1:ngrid,1:nlayer,igcm_h2o_vap)*mugaz/mmol(igcm_h2o_vap)
2080               call wstats(ngrid,"vmr_h2ovapor",                             &
2081                           "H2O vapour volume mixing ratio","mol/mol",       &
2082                           3,vmr)
2083            endif
2084
2085         endif ! end of 'tracer'
2086
2087         if(watercond.and.CLFvarying)then
2088            call wstats(ngrid,"rneb_man","H2O cloud fraction (conv)"," ",3,rneb_man)
2089            call wstats(ngrid,"rneb_lsc","H2O cloud fraction (large scale)"," ",3,rneb_lsc)
2090            call wstats(ngrid,"CLF","H2O cloud fraction"," ",3,cloudfrac)
2091            call wstats(ngrid,"CLFt","H2O column cloud fraction"," ",2,totcloudfrac)
2092            call wstats(ngrid,"RH","relative humidity"," ",3,RH)
2093         endif
2094
2095         if (ok_slab_ocean) then
2096            call wstats(ngrid,"dt_hdiff1","dt_hdiff1","K/s",2,dt_hdiff(:,1))
2097            call wstats(ngrid,"dt_hdiff2","dt_hdiff2","K/s",2,dt_hdiff(:,2))
2098            call wstats(ngrid,"dt_ekman1","dt_ekman1","K/s",2,dt_ekman(:,1))
2099            call wstats(ngrid,"dt_ekman2","dt_ekman2","K/s",2,dt_ekman(:,2))
2100            call wstats(ngrid,"tslab1","tslab1","K",2,tslab(:,1))
2101            call wstats(ngrid,"tslab2","tslab2","K",2,tslab(:,2))
2102            call wstats(ngrid,"pctsrf_sic","pct ice/sea","",2,pctsrf_sic)
2103            call wstats(ngrid,"tsea_ice","tsea_ice","K",2,tsea_ice)
2104            call wstats(ngrid,"sea_ice","sea ice","kg/m2",2,sea_ice)
2105            call wstats(ngrid,"rnat","nature of the surface","",2,rnat)
2106         endif
2107
2108         if(lastcall) then
2109            write (*,*) "Writing stats..."
2110            call mkstats(ierr)
2111         endif
2112         
2113      endif ! end of 'callstats'
2114
2115#ifndef MESOSCALE
2116       
2117!-----------------------------------------------------------------------------------------------------
2118!           OUTPUT in netcdf file "DIAGFI.NC", containing any variable for diagnostic
2119!
2120!             Note 1 : output with  period "ecritphy", set in "run.def"
2121!
2122!             Note 2 : writediagfi can also be called from any other subroutine for any variable,
2123!                      but its preferable to keep all the calls in one place ...
2124!-----------------------------------------------------------------------------------------------------
2125
2126      call writediagfi(ngrid,"Ls","solar longitude","deg",0,zls*180./pi)
2127      call writediagfi(ngrid,"Lss","sub solar longitude","deg",0,zlss*180./pi)
2128      call writediagfi(ngrid,"RA","right ascension","deg",0,right_ascen*180./pi)
2129      call writediagfi(ngrid,"Declin","solar declination","deg",0,declin*180./pi)
2130      call writediagfi(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2131      call writediagfi(ngrid,"ps","Surface pressure","Pa",2,ps)
2132      call writediagfi(ngrid,"temp","temperature","K",3,zt)
2133      call writediagfi(ngrid,"teta","potential temperature","K",3,zh)
2134      call writediagfi(ngrid,"u","Zonal wind","m.s-1",3,zu)
2135      call writediagfi(ngrid,"v","Meridional wind","m.s-1",3,zv)
2136      call writediagfi(ngrid,"w","Vertical wind","m.s-1",3,pw)
2137      call writediagfi(ngrid,"p","Pressure","Pa",3,pplay)
2138
2139!     Subsurface temperatures
2140!        call writediagsoil(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2141!        call writediagsoil(ngrid,"temp","temperature","K",3,tsoil)
2142
2143      ! Oceanic layers
2144      if(ok_slab_ocean) then
2145         call writediagfi(ngrid,"pctsrf_sic","pct ice/sea","",2,pctsrf_sic)
2146         call writediagfi(ngrid,"tsea_ice","tsea_ice","K",2,tsea_ice)
2147         call writediagfi(ngrid,"sea_ice","sea ice","kg/m2",2,sea_ice)
2148         call writediagfi(ngrid,"tslab1","tslab1","K",2,tslab(:,1))
2149         call writediagfi(ngrid,"tslab2","tslab2","K",2,tslab(:,2))
2150         call writediagfi(ngrid,"dt_hdiff1","dt_hdiff1","K/s",2,dt_hdiff(:,1))
2151         call writediagfi(ngrid,"dt_hdiff2","dt_hdiff2","K/s",2,dt_hdiff(:,2))
2152         call writediagfi(ngrid,"dt_ekman1","dt_ekman1","K/s",2,dt_ekman(:,1))
2153         call writediagfi(ngrid,"dt_ekman2","dt_ekman2","K/s",2,dt_ekman(:,2))
2154         call writediagfi(ngrid,"rnat","nature of the surface","",2,rnat)
2155         call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
2156         call writediagfi(ngrid,"latentFlux","latent heat flux","w.m^-2",2,zdqsdif(:,igcm_h2o_vap)*RLVTT)
2157      endif
2158     
2159      ! Thermal plume model
2160      if (calltherm) then
2161         call writediagfi(ngrid,'entr','Entrainment','kg m$^{-2}$ s$^{-1}$', 3, entr)
2162         call writediagfi(ngrid,'detr','Detrainment','kg m$^{-2}$ s$^{-1}$', 3, detr)
2163         call writediagfi(ngrid,'fm','Mass flux','kg m$^{-2}$ s$^{-1}$', 3, fm_bis)
2164         call writediagfi(ngrid,'w_plm','Squared vertical velocity','m s$^{-1}$', 3, zw2_bis)
2165         call writediagfi(ngrid,'fraca','Updraft fraction','', 3, fraca)
2166      endif
2167
2168!        GW non-oro outputs
2169      if (calllott_nonoro) then
2170         call WRITEDIAGFI(ngrid,"dugwno","GW non-oro dU","m/s2", 3,d_u_hin/ptimestep)
2171         call WRITEDIAGFI(ngrid,"dvgwno","GW non-oro dV","m/s2", 3,d_v_hin/ptimestep)
2172      endif
2173     
2174      ! Total energy balance diagnostics
2175      if(callrad.and.(.not.newtonian))then
2176     
2177         !call writediagfi(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
2178         !call writediagfi(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
2179         call writediagfi(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
2180         call writediagfi(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
2181         call writediagfi(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
2182         call writediagfi(ngrid,"shad","rings"," ", 2, fract)
2183         
2184!           call writediagfi(ngrid,"ASRcs","absorbed stellar rad (cs).","W m-2",2,fluxabs_sw1)
2185!           call writediagfi(ngrid,"OLRcs","outgoing longwave rad (cs).","W m-2",2,fluxtop_lw1)
2186!           call writediagfi(ngrid,"fluxsurfsw","sw surface flux.","W m-2",2,fluxsurf_sw)
2187!           call writediagfi(ngrid,"fluxsurflw","lw back radiation.","W m-2",2,fluxsurf_lw)
2188!           call writediagfi(ngrid,"fluxsurfswcs","sw surface flux (cs).","W m-2",2,fluxsurf_sw1)
2189!           call writediagfi(ngrid,"fluxsurflwcs","lw back radiation (cs).","W m-2",2,fluxsurf_lw1)
2190
2191         if(ok_slab_ocean) then
2192            call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrdocean)
2193         else
2194            call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrd)
2195         endif
2196         
2197         call writediagfi(ngrid,"DYN","dynamical heat input","W m-2",2,fluxdyn)
2198         
2199      endif ! end of 'callrad'
2200       
2201      if(enertest) then
2202     
2203         if (calldifv) then
2204         
2205            call writediagfi(ngrid,"q2","turbulent kinetic energy","J.kg^-1",3,q2)
2206            call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
2207           
2208!             call writediagfi(ngrid,"dEzdiff","turbulent diffusion heating (-sensible flux)","w.m^-2",3,dEzdiff)
2209!             call writediagfi(ngrid,"dEdiff","integrated turbulent diffusion heating (-sensible flux)","w.m^-2",2,dEdiff)
2210!             call writediagfi(ngrid,"dEdiffs","In TurbDiff (correc rad+latent heat) surf nrj change","w.m^-2",2,dEdiffs)
2211             
2212         endif
2213         
2214         if (corrk) then
2215            call writediagfi(ngrid,"dEzradsw","radiative heating","w.m^-2",3,dEzradsw)
2216            call writediagfi(ngrid,"dEzradlw","radiative heating","w.m^-2",3,dEzradlw)
2217         endif
2218         
2219         if(watercond) then
2220
2221            call writediagfi(ngrid,"lscaledE","heat from largescale","W m-2",2,lscaledE)
2222            call writediagfi(ngrid,"madjdE","heat from moistadj","W m-2",2,madjdE)
2223            call writediagfi(ngrid,"qsatatm","atm qsat"," ",3,qsat)
2224             
2225!             call writediagfi(ngrid,"lscaledEz","heat from largescale","W m-2",3,lscaledEz)
2226!             call writediagfi(ngrid,"madjdEz","heat from moistadj","W m-2",3,madjdEz)             
2227!             call writediagfi(ngrid,"h2o_max_col","maximum H2O column amount","kg.m^-2",2,H2Omaxcol)
2228
2229         endif
2230         
2231      endif ! end of 'enertest'
2232
2233        ! Diagnostics of optical thickness
2234        ! Warning this is exp(-tau), I let you postproc with -log to have tau itself - JVO 19
2235        if (diagdtau) then               
2236          do nw=1,L_NSPECTV
2237            write(str2,'(i2.2)') nw
2238            call writediagfi(ngrid,'dtauv'//str2,'Layer optical thickness attenuation in VI band '//str2,'',1,int_dtauv(:,nlayer:1:-1,nw))
2239          enddo
2240          do nw=1,L_NSPECTI
2241            write(str2,'(i2.2)') nw
2242            call writediagfi(ngrid,'dtaui'//str2,'Layer optical thickness attenuation in IR band '//str2,'',1,int_dtaui(:,nlayer:1:-1,nw))
2243          enddo
2244        endif
2245
2246
2247        ! Temporary inclusions for heating diagnostics.
2248        !call writediagfi(ngrid,"zdtsw","SW heating","T s-1",3,zdtsw)
2249        !call writediagfi(ngrid,"zdtlw","LW heating","T s-1",3,zdtlw)
2250        call writediagfi(ngrid,"dtrad","radiative heating","K s-1",3,dtrad)
2251        ! call writediagfi(ngrid,"zdtdyn","Dyn. heating","T s-1",3,zdtdyn)
2252       
2253        ! For Debugging.
2254        !call writediagfi(ngrid,'rnat','Terrain type',' ',2,real(rnat))
2255        !call writediagfi(ngrid,'pphi','Geopotential',' ',3,pphi)
2256       
2257
2258      ! Output aerosols.
2259      if (igcm_co2_ice.ne.0.and.iaero_co2.ne.0) &
2260         call writediagfi(ngrid,'CO2ice_reff','CO2ice_reff','m',3,reffrad(1,1,iaero_co2))
2261      if (igcm_h2o_ice.ne.0.and.iaero_h2o.ne.0) &
2262         call writediagfi(ngrid,'H2Oice_reff','H2Oice_reff','m',3,reffrad(:,:,iaero_h2o))
2263      if (igcm_co2_ice.ne.0.and.iaero_co2.ne.0) &
2264         call writediagfi(ngrid,'CO2ice_reffcol','CO2ice_reffcol','um kg m^-2',2,reffcol(1,iaero_co2))
2265      if (igcm_h2o_ice.ne.0.and.iaero_h2o.ne.0) &
2266         call writediagfi(ngrid,'H2Oice_reffcol','H2Oice_reffcol','um kg m^-2',2,reffcol(1,iaero_h2o))
2267
2268      ! Output tracers.
2269      if (tracer) then
2270     
2271         do iq=1,nq
2272            call writediagfi(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
2273            call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2274                             'kg m^-2',2,qsurf_hist(1,iq) )
2275            call writediagfi(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
2276                            'kg m^-2',2,qcol(1,iq) )
2277                         
2278!          call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2279!                          'kg m^-2',2,qsurf(1,iq) )                         
2280
2281            if(watercond.or.CLFvarying)then
2282               call writediagfi(ngrid,"rneb_man","H2O cloud fraction (conv)"," ",3,rneb_man)
2283               call writediagfi(ngrid,"rneb_lsc","H2O cloud fraction (large scale)"," ",3,rneb_lsc)
2284               call writediagfi(ngrid,"CLF","H2O cloud fraction"," ",3,cloudfrac)
2285               call writediagfi(ngrid,"CLFt","H2O column cloud fraction"," ",2,totcloudfrac)
2286               call writediagfi(ngrid,"RH","relative humidity"," ",3,RH)
2287            endif
2288
2289            if(waterrain)then
2290               call writediagfi(ngrid,"rain","rainfall","kg m-2 s-1",2,zdqsrain)
2291               call writediagfi(ngrid,"snow","snowfall","kg m-2 s-1",2,zdqssnow)
2292               call writediagfi(ngrid,"reevap","reevaporation of precipitation","kg m-2 s-1",2,reevap_precip)
2293            endif
2294
2295            if((hydrology).and.(.not.ok_slab_ocean))then
2296               call writediagfi(ngrid,"hice","oceanic ice height","m",2,hice)
2297            endif
2298
2299            if(ice_update)then
2300               call writediagfi(ngrid,"ice_min","min annual ice","m",2,ice_min)
2301               call writediagfi(ngrid,"ice_ini","initial annual ice","m",2,ice_initial)
2302            endif
2303
2304            do ig=1,ngrid
2305               if(tau_col(ig).gt.1.e3)then
2306                  print*,'WARNING: tau_col=',tau_col(ig)
2307                  print*,'at ig=',ig,'in PHYSIQ'
2308               endif         
2309            end do
2310
2311            call writediagfi(ngrid,"tau_col","Total aerosol optical depth","[]",2,tau_col)
2312
2313         enddo ! end of 'nq' loop
2314         
2315       endif ! end of 'tracer'
2316
2317
2318      ! Output spectrum.
2319      if(specOLR.and.corrk)then     
2320         call writediagspecIR(ngrid,"OLR3D","OLR(lon,lat,band)","W/m^2/cm^-1",3,OLR_nu)
2321         call writediagspecVI(ngrid,"OSR3D","OSR(lon,lat,band)","W/m^2/cm^-1",3,OSR_nu)
2322      endif
2323
2324#else
2325      comm_HR_SW(1:ngrid,1:nlayer) = zdtsw(1:ngrid,1:nlayer)
2326      comm_HR_LW(1:ngrid,1:nlayer) = zdtlw(1:ngrid,1:nlayer)
2327      if ((tracer).and.(water)) then
2328        comm_CLOUDFRAC(1:ngrid,1:nlayer)=cloudfrac(1:ngrid,1:nlayer)
2329        comm_TOTCLOUDFRAC(1:ngrid)=totcloudfrac(1:ngrid)
2330        comm_RAIN(1:ngrid,1:nlayer)=zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)
2331        comm_SURFRAIN(1:ngrid)=zdqsrain(1:ngrid)
2332        comm_SNOW(1:ngrid,1:nlayer)=zdqrain(1:ngrid,1:nlayer,igcm_h2o_ice)
2333        comm_ALBEQ(1:ngrid)=albedo_equivalent(1:ngrid)
2334        comm_H2OICE_REFF(1:ngrid,1:nlayer)=reffrad(1:ngrid,1:nlayer,iaero_h2o)
2335        comm_REEVAP(1:ngrid)=reevap_precip(1:ngrid)
2336      endif
2337      comm_FLUXTOP_DN(1:ngrid)=fluxtop_dn(1:ngrid)
2338      comm_FLUXABS_SW(1:ngrid)=fluxabs_sw(1:ngrid)
2339      comm_FLUXTOP_LW(1:ngrid)=fluxtop_lw(1:ngrid)
2340      comm_FLUXSURF_SW(1:ngrid)=fluxsurf_sw(1:ngrid)
2341      comm_FLUXSURF_LW(1:ngrid)=fluxsurf_lw(1:ngrid)
2342      comm_FLXGRD(1:ngrid)=fluxgrd(1:ngrid)
2343      comm_LSCEZ(1:ngrid,1:nlayer)=lscaledEz(1:ngrid,1:nlayer)
2344      sensibFlux(1:ngrid) = zflubid(1:ngrid) - capcal(1:ngrid)*zdtsdif(1:ngrid) !!! ????
2345
2346      if (turb_resolved) then
2347        open(17,file='lsf.txt',form='formatted',status='old')
2348        rewind(17)
2349        DO l=1,nlayer
2350          read(17,*) lsf_dt(l),lsf_dq(l)
2351        ENDDO
2352        close(17)
2353        do ig=1,ngrid
2354          if ((tracer).and.(water)) then
2355           pdq(ig,:,igcm_h2o_vap) = pdq(ig,:,igcm_h2o_vap) + lsf_dq(:)
2356          endif
2357          pdt(ig,:) = pdt(ig,:) + lsf_dt(:)
2358          comm_HR_DYN(ig,:) = lsf_dt(:)
2359        enddo
2360      endif
2361#endif
2362
2363! XIOS outputs
2364#ifdef CPP_XIOS     
2365      ! Send fields to XIOS: (NB these fields must also be defined as
2366      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
2367      CALL send_xios_field("ls",zls)
2368     
2369      CALL send_xios_field("ps",ps)
2370      CALL send_xios_field("area",cell_area)
2371     
2372      CALL send_xios_field("temperature",zt)
2373      CALL send_xios_field("u",zu)
2374      CALL send_xios_field("v",zv)
2375      CALL send_xios_field("omega",omega)
2376     
2377      IF (calltherm) THEN
2378         CALL send_xios_field('w_plm',zw2_bis)
2379         CALL send_xios_field('entr',entr)
2380         CALL send_xios_field('detr',detr)
2381!         CALL send_xios_field('fm',fm_bis)
2382!         CALL send_xios_field('fraca',fraca)
2383      ENDIF
2384     
2385      IF (water) THEN
2386         CALL send_xios_field('h2o_vap',zq(:,:,igcm_h2o_vap))
2387         CALL send_xios_field('h2o_ice',zq(:,:,igcm_h2o_ice))
2388      ENDIF
2389     
2390      CALL send_xios_field("ISR",fluxtop_dn)
2391      CALL send_xios_field("OLR",fluxtop_lw)
2392     
2393      if (lastcall.and.is_omp_master) then
2394        write(*,*) "physiq: call xios_context_finalize"
2395        call xios_context_finalize
2396      endif
2397#endif
2398     
2399      icount=icount+1
2400     
2401      end subroutine physiq
2402     
2403   end module physiq_mod
Note: See TracBrowser for help on using the repository browser.