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

Last change on this file since 1990 was 1989, checked in by jleconte, 7 years ago

28/08/2018 == JL

correct bug on rain initialization at each timestep in physiqu_mod so that mass_redist can work without rain (if precipitations are taken care with sedimentation for example)

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