source: trunk/LMDZ.MARS/libf/phymars/physiq_mod.F @ 2917

Last change on this file since 2917 was 2916, checked in by emillour, 2 years ago

Mars PCM
Add a "diagsoil" flag to trigger outputing a diagsoil.nc file
(default is diagsoil=.false.)
EM

File size: 176.5 KB
Line 
1      MODULE physiq_mod
2
3      IMPLICIT NONE
4
5      CONTAINS
6
7      SUBROUTINE physiq(
8     $            ngrid,nlayer,nq
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 watercloud_mod, only: watercloud, zdqcloud, zdqscloud
17      use calchim_mod, only: calchim, ichemistry, zdqchim, zdqschim
18      use watersat_mod, only: watersat
19      use co2condens_mod, only: co2condens
20      use co2cloud_mod, only: co2cloud
21      use callradite_mod, only: callradite
22      use callsedim_mod, only: callsedim
23      use rocketduststorm_mod, only: rocketduststorm, dustliftday
24      use calcstormfract_mod, only: calcstormfract
25      use topmons_mod, only: topmons,topmons_setup
26      use tracer_mod, only: noms, mmol, igcm_co2, igcm_n2, igcm_co2_ice,
27     &                      igcm_co, igcm_o, igcm_h2o_vap, igcm_h2o_ice,
28     &                      igcm_hdo_vap, igcm_hdo_ice,
29     &                      igcm_ccn_mass, igcm_ccn_number,
30     &                      igcm_ccnco2_mass, igcm_ccnco2_number,
31     &                      igcm_ccnco2_h2o_mass_ice,
32     &                      igcm_ccnco2_h2o_mass_ccn,
33     &                      igcm_ccnco2_h2o_number,
34     &                      igcm_ccnco2_meteor_mass,
35     &                      igcm_ccnco2_meteor_number,
36     &                      rho_ice_co2,nuiceco2_sed,nuiceco2_ref,
37     &                      igcm_dust_mass, igcm_dust_number, igcm_h2o2,
38     &                      nuice_ref, rho_ice, rho_dust, ref_r0,
39     &                      igcm_he, igcm_stormdust_mass,
40     &                      igcm_stormdust_number, igcm_topdust_mass,
41     &                      igcm_topdust_number,
42     &                      qperemin
43      use comsoil_h, only: inertiedat, ! soil thermal inertia
44     &                     tsoil, nsoilmx,!number of subsurface layers
45     &                     mlayer,layer ! soil mid layer depths
46      use geometry_mod, only: longitude, latitude, cell_area,
47     &                        longitude_deg
48      use comgeomfi_h, only: sinlon, coslon, sinlat, coslat
49      use surfdat_h, only: phisfi, albedodat, zmea, zstd, zsig, zgam,
50     &                     zthe, z0, albedo_h2o_cap,albedo_h2o_frost,
51     &                     frost_albedo_threshold,frost_metam_threshold,
52     &                     tsurf, emis,
53     &                     capcal, fluxgrd, qsurf,
54     &                     hmons,summit,base,watercap,watercaptag
55      use comsaison_h, only: dist_sol, declin, zls,
56     &                       mu0, fract, local_time
57      use slope_mod, only: theta_sl, psi_sl
58      use conc_mod, only: rnew, cpnew, mmean
59      use time_phylmdz_mod, only: iphysiq, day_step, ecritstart, daysec
60      use dimradmars_mod, only: aerosol, totcloudfrac,
61     &                          dtrad, fluxrad_sky, fluxrad, albedo,
62     &                          naerkind, iaer_dust_doubleq,
63     &                          iaer_stormdust_doubleq, iaer_h2o_ice,
64     &                          flux_1AU
65      use dust_param_mod, only: doubleq, lifting, callddevil,
66     &                          tauscaling, odpref, dustbin,
67     &                          dustscaling_mode, dust_rad_adjust,
68     &                          freedust, reff_driven_IRtoVIS_scenario
69      use turb_mod, only: q2, wstar, ustar, sensibFlux,
70     &                    zmax_th, hfmax_th, turb_resolved
71      use planete_h, only: aphelie, periheli, year_day, peri_day,
72     &                     obliquit
73      USE comcstfi_h, only: r, cpp, mugaz, g, rcp, pi, rad
74      USE calldrag_noro_mod, ONLY: calldrag_noro
75      USE vdifc_mod, ONLY: vdifc
76      use param_v4_h, only: nreact,n_avog,
77     &                      fill_data_thermos, allocate_param_thermos
78      use iono_h, only: allocate_param_iono
79      use compute_dtau_mod, only: compute_dtau
80      use nonoro_gwd_ran_mod, only: nonoro_gwd_ran
81      use check_fields_mod, only: check_physics_fields
82#ifdef MESOSCALE
83      use comsoil_h, only: mlayer,layer
84      use surfdat_h, only: z0_default
85      use comm_wrf
86#else
87      USE planetwide_mod, ONLY: planetwide_maxval, planetwide_minval,
88     &                          planetwide_sumval
89      use phyredem, only: physdem0, physdem1
90      use phyetat0_mod, only: phyetat0, tab_cntrl_mod
91      use wstats_mod, only: callstats, wstats, mkstats
92      use writediagsoil_mod, only: writediagsoil
93      use eofdump_mod, only: eofdump
94      USE vertical_layers_mod, ONLY: ap,bp,aps,bps,presnivs,pseudoalt
95      USE mod_phys_lmdz_omp_data, ONLY: is_omp_master
96      USE time_phylmdz_mod, ONLY: day_end
97#endif
98
99#ifdef CPP_XIOS     
100      use xios_output_mod, only: initialize_xios_output,
101     &                           update_xios_timestep,
102     &                           send_xios_field
103      use wxios, only: wxios_context_init, xios_context_finalize
104#endif
105      USE mod_grid_phy_lmdz, ONLY: grid_type, unstructured
106      use ioipsl_getin_p_mod, only: getin_p
107      use comslope_mod, ONLY: nslope,def_slope,def_slope_mean,
108     &                        subslope_dist,iflat,sky_slope,
109     &                        major_slope,compute_meshgridavg,
110     &                        ini_comslope_h
111
112      IMPLICIT NONE
113c=======================================================================
114c
115c   subject:
116c   --------
117c
118c   Organisation of the physical parametrisations of the LMD
119c   martian atmospheric general circulation model.
120c
121c   The GCM can be run without or with tracer transport
122c   depending on the value of Logical "tracer" in file  "callphys.def"
123c   Tracers may be water vapor, ice OR chemical species OR dust particles
124c
125c   SEE comments in initracer.F about numbering of tracer species...
126c
127c   It includes:
128c
129c      1. Initialization:
130c      1.1 First call initializations
131c      1.2 Initialization for every call to physiq
132c      1.2.5 Compute mean mass and cp, R and thermal conduction coeff.
133c      2. Compute radiative transfer tendencies
134c         (longwave and shortwave) for CO2 and aerosols.
135c      3. Gravity wave and subgrid scale topography drag :
136c      4. Vertical diffusion (turbulent mixing):
137c      5. Convective adjustment
138c      6. Condensation and sublimation of carbon dioxide.
139c      7.  TRACERS :
140c       7a. water, water ice, co2 ice (clouds)
141c       7b. call for photochemistry when tracers are chemical species
142c       7c. other scheme for tracer (dust) transport (lifting, sedimentation)
143c       7d. updates (CO2 pressure variations, surface budget)
144c      8. Contribution to tendencies due to thermosphere
145c      9. Surface and sub-surface temperature calculations
146c     10. Write outputs :
147c           - "startfi", "histfi" (if it's time)
148c           - Saving statistics (if "callstats = .true.")
149c           - Dumping eof (if "calleofdump = .true.")
150c           - Output any needed variables in "diagfi"
151c     11. Diagnostic: mass conservation of tracers
152c
153c   author:
154c   -------
155c           Frederic Hourdin    15/10/93
156c           Francois Forget             1994
157c           Christophe Hourdin  02/1997
158c           Subroutine completly rewritten by F.Forget (01/2000)
159c           Introduction of the photochemical module: S. Lebonnois (11/2002)
160c           Introduction of the thermosphere module: M. Angelats i Coll (2002)
161c           Water ice clouds: Franck Montmessin (update 06/2003)
162c           Radiatively active tracers: J.-B. Madeleine (10/2008-06/2009)
163c             Nb: See callradite.F for more information.
164c           Mesoscale lines: Aymeric Spiga (2007 - 2011) -- check MESOSCALE flags
165c           jul 2011 malv+fgg: Modified calls to NIR heating routine and 15 um cooling parameterization
166c
167c           10/16 J. Audouard: modifications for CO2 clouds scheme
168
169c   arguments:
170c   ----------
171c
172c   input:
173c   ------
174c    ecri                  period (in dynamical timestep) to write output
175c    ngrid                 Size of the horizontal grid.
176c                          All internal loops are performed on that grid.
177c    nlayer                Number of vertical layers.
178c    nq                    Number of advected fields
179c    firstcall             True at the first call
180c    lastcall              True at the last call
181c    pday                  Number of days counted from the North. Spring
182c                          equinoxe.
183c    ptime                 Universal time (0<ptime<1): ptime=0.5 at 12:00 UT
184c    ptimestep             timestep (s)
185c    pplay(ngrid,nlayer)   Pressure at the middle of the layers (Pa)
186c    pplev(ngrid,nlayer+1) intermediate pressure levels (pa)
187c    pphi(ngrid,nlayer)    Geopotential at the middle of the layers (m2s-2)
188c    pu(ngrid,nlayer)      u component of the wind (ms-1)
189c    pv(ngrid,nlayer)      v component of the wind (ms-1)
190c    pt(ngrid,nlayer)      Temperature (K)
191c    pq(ngrid,nlayer,nq)   Advected fields
192c    pudyn(ngrid,nlayer)    |
193c    pvdyn(ngrid,nlayer)    | Dynamical temporal derivative for the
194c    ptdyn(ngrid,nlayer)    | corresponding variables
195c    pqdyn(ngrid,nlayer,nq) |
196c    flxw(ngrid,nlayer)      vertical mass flux (kg/s) at layer lower boundary
197c
198c   output:
199c   -------
200c
201c    pdu(ngrid,nlayer)       |
202c    pdv(ngrid,nlayer)       |  Temporal derivative of the corresponding
203c    pdt(ngrid,nlayer)       |  variables due to physical processes.
204c    pdq(ngrid,nlayer,nq)    |
205c    pdpsrf(ngrid)           |
206
207c
208c=======================================================================
209c
210c    0.  Declarations :
211c    ------------------
212
213      include "callkeys.h"
214      include "comg1d.h"
215      include "nlteparams.h"
216      include "netcdf.inc"
217
218c Arguments :
219c -----------
220
221c   inputs:
222c   -------
223      INTEGER,INTENT(in) :: ngrid ! number of atmospheric columns
224      INTEGER,INTENT(in) :: nlayer ! number of atmospheric layers
225      INTEGER,INTENT(in) :: nq ! number of tracers
226      LOGICAL,INTENT(in) :: firstcall ! signals first call to physics
227      LOGICAL,INTENT(in) :: lastcall ! signals last call to physics
228      REAL,INTENT(in) :: pday ! number of elapsed sols since reference Ls=0
229      REAL,INTENT(in) :: ptime ! "universal time", given as fraction of sol (e.g.: 0.5 for noon)
230      REAL,INTENT(in) :: ptimestep ! physics timestep (s)
231      REAL,INTENT(in) :: pplev(ngrid,nlayer+1) ! inter-layer pressure (Pa)
232      REAL,INTENT(IN) :: pplay(ngrid,nlayer) ! mid-layer pressure (Pa)
233      REAL,INTENT(IN) :: pphi(ngrid,nlayer) ! geopotential at mid-layer (m2s-2)
234      REAL,INTENT(in) :: pu(ngrid,nlayer) ! zonal wind component (m/s)
235      REAL,INTENT(in) :: pv(ngrid,nlayer) ! meridional wind component (m/s)
236      REAL,INTENT(in) :: pt(ngrid,nlayer) ! temperature (K)
237      REAL,INTENT(in) :: pq(ngrid,nlayer,nq) ! tracers (.../kg_of_air)
238      REAL,INTENT(in) :: flxw(ngrid,nlayer) ! vertical mass flux (ks/s)
239                                            ! at lower boundary of layer
240
241c   outputs:
242c   --------
243c     physical tendencies
244      REAL,INTENT(out) :: pdu(ngrid,nlayer) ! zonal wind tendency (m/s/s)
245      REAL,INTENT(out) :: pdv(ngrid,nlayer) ! meridional wind tendency (m/s/s)
246      REAL,INTENT(out) :: pdt(ngrid,nlayer) ! temperature tendency (K/s)
247      REAL,INTENT(out) :: pdq(ngrid,nlayer,nq) ! tracer tendencies (../kg/s)
248      REAL,INTENT(out) :: pdpsrf(ngrid) ! surface pressure tendency (Pa/s)
249
250     
251
252c Local saved variables:
253c ----------------------
254      INTEGER,SAVE :: day_ini ! Initial date of the run (sol since Ls=0)
255      INTEGER,SAVE :: icount     ! counter of calls to physiq during the run.
256      REAL,SAVE :: time_phys
257
258!$OMP THREADPRIVATE(day_ini,icount,time_phys)
259
260#ifdef DUSTSTORM
261      REAL pq_tmp(ngrid, nlayer, 2) ! To compute tendencies due the dust bomb
262#endif
263           
264c     Variables used by the water ice microphysical scheme:
265      REAL rice(ngrid,nlayer)    ! Water ice geometric mean radius (m)
266      REAL nuice(ngrid,nlayer)   ! Estimated effective variance
267                                     !   of the size distribution
268      real rsedcloud(ngrid,nlayer) ! Cloud sedimentation radius
269      real rhocloud(ngrid,nlayer)  ! Cloud density (kg.m-3)
270      real rsedcloudco2(ngrid,nlayer) ! CO2 Cloud sedimentation radius
271      real rhocloudco2(ngrid,nlayer)  ! CO2 Cloud density (kg.m-3)
272      real nuiceco2(ngrid,nlayer) ! Estimated effective variance of the
273                                  !   size distribution
274      REAL inertiesoil(ngrid,nsoilmx,nslope) ! Time varying subsurface
275                                      ! thermal inertia (J.s-1/2.m-2.K-1)
276                                      ! (used only when tifeedback=.true.)
277c     Variables used by the CO2 clouds microphysical scheme:
278      DOUBLE PRECISION riceco2(ngrid,nlayer)   ! co2 ice geometric mean radius (m)
279      real zdqssed_co2(ngrid)  ! CO2 flux at the surface  (kg.m-2.s-1)
280      real zdqssed_ccn(ngrid,nq)  ! CCN flux at the surface  (kg.m-2.s-1)
281      real zcondicea_co2microp(ngrid,nlayer)
282c     Variables used by the photochemistry
283      REAL surfdust(ngrid,nlayer) ! dust surface area (m2/m3, if photochemistry)
284      REAL surfice(ngrid,nlayer)  !  ice surface area (m2/m3, if photochemistry)
285c     Variables used by the slope model
286      REAL sl_ls, sl_lct, sl_lat
287      REAL sl_tau, sl_alb, sl_the, sl_psi
288      REAL sl_fl0, sl_flu
289      REAL sl_ra, sl_di0
290      REAL sky
291      REAL fluxsurf_dir_dn_sw(ngrid) ! Incident direct solar flux on Mars at surface (W.m-2)
292
293      REAL,PARAMETER :: stephan = 5.67e-08 ! Stephan Boltzman constant
294
295c Local variables :
296c -----------------
297
298      REAL CBRT
299      EXTERNAL CBRT
300
301!      CHARACTER*80 fichier
302      INTEGER l,ig,ierr,igout,iq,tapphys,isoil
303
304      REAL fluxsurf_lw(ngrid,nslope)      !incident LW (IR) surface flux (W.m-2)
305      REAL fluxsurf_dn_sw(ngrid,2,nslope) ! Incident SW (solar) surface flux (W.m-2)
306      REAL fluxsurf_up_sw(ngrid,2) ! Reflected SW (solar) surface flux (W.m-2)
307      REAL fluxtop_lw(ngrid)       !Outgoing LW (IR) flux to space (W.m-2)
308      REAL fluxtop_dn_sw(ngrid,2) ! Incoming SW (solar) flux from space (W.m-2)
309      REAL fluxtop_up_sw(ngrid,2) ! Outgoing SW (solar) flux to space (W.m-2)
310      REAL tau_pref_scenario(ngrid) ! prescribed dust column visible opacity
311                                    ! at odpref
312      REAL IRtoVIScoef(ngrid) ! conversion coefficient to apply on
313                              ! scenario absorption IR (9.3um) CDOD
314                              ! = tau_pref_gcm_VIS / tau_pref_gcm_IR
315      REAL tau_pref_gcm(ngrid) ! dust column visible opacity at odpref in the GCM
316c     rocket dust storm
317      REAL totstormfract(ngrid)     ! fraction of the mesh where the dust storm is contained
318      logical clearatm              ! clearatm used to calculate twice the radiative
319                                    ! transfer when rdstorm is active :
320                                    !            - in a mesh with stormdust and background dust (false)
321                                    !            - in a mesh with background dust only (true)
322c     entrainment by mountain top dust flows
323      logical nohmons               ! nohmons used to calculate twice the radiative
324                                    ! transfer when topflows is active :
325                                    !            - in a mesh with topdust and background dust (false)
326                                    !            - in a mesh with background dust only (true)
327     
328      REAL tau(ngrid,naerkind)     ! Column dust optical depth at each point
329                                   ! AS: TBD: this one should be in a module !
330      REAL zday                      ! date (time since Ls=0, in martian days)
331      REAL zzlay(ngrid,nlayer)     ! altitude at the middle of the layers
332      REAL zzlev(ngrid,nlayer+1)   ! altitude at layer boundaries
333!      REAL latvl1,lonvl1             ! Viking Lander 1 point (for diagnostic)
334
335c     Tendancies due to various processes:
336      REAL dqsurf(ngrid,nq,nslope)  ! tendency for tracers on surface (Kg/m2/s)
337      REAL zdtlw(ngrid,nlayer)     ! (K/s)
338      REAL zdtsw(ngrid,nlayer)     ! (K/s)
339      REAL pdqrds(ngrid,nlayer,nq) ! tendency for dust after rocketduststorm
340
341      REAL zdtnirco2(ngrid,nlayer) ! (K/s)
342      REAL zdtnlte(ngrid,nlayer)   ! (K/s)
343      REAL zdtsurf(ngrid,nslope)            ! (K/s)
344      REAL zdtcloud(ngrid,nlayer),zdtcloudco2(ngrid,nlayer)
345      REAL zdvdif(ngrid,nlayer),zdudif(ngrid,nlayer)  ! (m.s-2)
346      REAL zdhdif(ngrid,nlayer), zdtsdif(ngrid,nslope)         ! (K/s)
347      REAL zdvadj(ngrid,nlayer),zduadj(ngrid,nlayer)  ! (m.s-2)
348      REAL zdhadj(ngrid,nlayer)                           ! (K/s)
349      REAL zdtgw(ngrid,nlayer)                            ! (K/s)
350      REAL zdugw(ngrid,nlayer),zdvgw(ngrid,nlayer)    ! (m.s-2)
351      REAL zdtc(ngrid,nlayer),zdtsurfc(ngrid,nslope)
352      REAL zdvc(ngrid,nlayer),zduc(ngrid,nlayer)
353
354      REAL zdqdif(ngrid,nlayer,nq), zdqsdif(ngrid,nq,nslope)
355      REAL zdqsed(ngrid,nlayer,nq), zdqssed(ngrid,nq)
356      REAL zdqdev(ngrid,nlayer,nq), zdqsdev(ngrid,nq)
357      REAL zdqadj(ngrid,nlayer,nq)
358      REAL zdqc(ngrid,nlayer,nq)
359      REAL zdqcloudco2(ngrid,nlayer,nq)
360      REAL zdqsc(ngrid,nq,nslope)
361
362      REAL zdteuv(ngrid,nlayer)    ! (K/s)
363      REAL zdtconduc(ngrid,nlayer) ! (K/s)
364      REAL zdumolvis(ngrid,nlayer)
365      REAL zdvmolvis(ngrid,nlayer)
366      real zdqmoldiff(ngrid,nlayer,nq)
367      real*8 PhiEscH,PhiEscH2,PhiEscD
368
369      REAL dwatercap(ngrid,nslope), dwatercap_dif(ngrid,nslope)     ! (kg/m-2)
370
371c     Local variable for local intermediate calcul:
372      REAL zflubid(ngrid,nslope)
373      REAL zplanck(ngrid),zpopsk(ngrid,nlayer)
374      REAL zdum1(ngrid,nlayer)
375      REAL zdum2(ngrid,nlayer)
376      REAL ztim1,ztim2,ztim3, z1,z2
377      REAL ztime_fin
378      REAL zdh(ngrid,nlayer)
379      REAL zh(ngrid,nlayer)      ! potential temperature (K)
380      REAL pw(ngrid,nlayer) ! vertical velocity (m/s) (>0 when downwards)
381      INTEGER length
382      PARAMETER (length=100)
383
384c     Variables for the total dust for diagnostics
385      REAL qdusttotal(ngrid,nlayer) !it equals to dust + stormdust
386
387      INTEGER iaer
388
389c local variables only used for diagnostic (output in file "diagfi" or "stats")
390c -----------------------------------------------------------------------------
391      REAL ps(ngrid), zt(ngrid,nlayer)
392      REAL zu(ngrid,nlayer),zv(ngrid,nlayer)
393      REAL zq(ngrid,nlayer,nq)
394
395      REAL fluxtop_dn_sw_tot(ngrid), fluxtop_up_sw_tot(ngrid)
396      REAL fluxsurf_dn_sw_tot(ngrid,nslope), fluxsurf_up_sw_tot(ngrid)
397      character*2 str2
398!      character*5 str5
399      real zdtdif(ngrid,nlayer), zdtadj(ngrid,nlayer)
400      real rdust(ngrid,nlayer) ! dust geometric mean radius (m)
401      real rstormdust(ngrid,nlayer) ! stormdust geometric mean radius (m)
402      real rtopdust(ngrid,nlayer)   ! topdust geometric mean radius (m)
403      integer igmin, lmin
404      logical tdiag
405
406      real co2col(ngrid)        ! CO2 column
407      ! pplev and pplay are dynamical inputs and must not be modified in the physics.
408      ! instead, use zplay and zplev :
409      REAL zplev(ngrid,nlayer+1),zplay(ngrid,nlayer)
410!      REAL zstress(ngrid),cd
411      real tmean, zlocal(nlayer)
412      real rho(ngrid,nlayer)  ! density
413      real vmr(ngrid,nlayer)  ! volume mixing ratio
414      real rhopart(ngrid,nlayer) ! number density of a given species
415      real colden(ngrid,nq)     ! vertical column of tracers
416      real mass(nq)             ! global mass of tracers (g)
417      REAL mtot(ngrid)          ! Total mass of water vapor (kg/m2)
418      REAL mstormdtot(ngrid)    ! Total mass of stormdust tracer (kg/m2)
419      REAL mdusttot(ngrid)      ! Total mass of dust tracer (kg/m2)
420      REAL icetot(ngrid)        ! Total mass of water ice (kg/m2)
421      REAL mtotco2(ngrid)      ! Total mass of co2, including ice at the surface (kg/m2)
422      REAL vaptotco2(ngrid)     ! Total mass of co2 vapor (kg/m2)
423      REAL icetotco2(ngrid)     ! Total mass of co2 ice (kg/m2)
424      REAL Nccntot(ngrid)       ! Total number of ccn (nbr/m2)
425      REAL NccnCO2tot(ngrid)    ! Total number of ccnCO2 (nbr/m2)
426      REAL Mccntot(ngrid)       ! Total mass of ccn (kg/m2)
427      REAL rave(ngrid)          ! Mean water ice effective radius (m)
428      REAL opTES(ngrid,nlayer)  ! abs optical depth at 825 cm-1
429      REAL tauTES(ngrid)        ! column optical depth at 825 cm-1
430      REAL Qabsice                ! Water ice absorption coefficient
431      REAL taucloudtes(ngrid)  ! Cloud opacity at infrared
432                               !   reference wavelength using
433                               !   Qabs instead of Qext
434                               !   (direct comparison with TES)
435      REAL mtotD(ngrid)          ! Total mass of HDO vapor (kg/m2)
436      REAL icetotD(ngrid)        ! Total mass of HDO ice (kg/m2)
437      REAL DoH_vap(ngrid,nlayer) !D/H ratio
438      REAL DoH_ice(ngrid,nlayer) !D/H ratio
439      REAL DoH_surf(ngrid) !D/H ratio surface
440
441      REAL dqdustsurf(ngrid) ! surface q dust flux (kg/m2/s)
442      REAL dndustsurf(ngrid) ! surface n dust flux (number/m2/s)
443      REAL ndust(ngrid,nlayer) ! true n dust (kg/kg)
444      REAL qdust(ngrid,nlayer) ! true q dust (kg/kg)
445      REAL nccn(ngrid,nlayer)  ! true n ccn (kg/kg)
446      REAL qccn(ngrid,nlayer)  ! true q ccn (kg/kg)
447c     definition tendancies of stormdust tracers
448      REAL rdsdqdustsurf(ngrid) ! surface q stormdust flux (kg/m2/s)
449      REAL rdsdndustsurf(ngrid) ! surface n stormdust flux (number/m2/s)
450      REAL rdsndust(ngrid,nlayer) ! true n stormdust (kg/kg)
451      REAL rdsqdust(ngrid,nlayer) ! true q stormdust (kg/kg)
452      REAL wspeed(ngrid,nlayer+1) ! vertical velocity stormdust tracer
453      REAL wtop(ngrid,nlayer+1) ! vertical velocity topdust tracer
454       
455      REAL dsodust(ngrid,nlayer) ! density scaled opacity for background dust
456      REAL dsords(ngrid,nlayer) ! density scaled opacity for stormdust
457      REAL dsotop(ngrid,nlayer) ! density scaled opacity for topdust
458
459c Test 1d/3d scavenging
460      real h2otot(ngrid)
461      real hdotot(ngrid)
462      REAL satu(ngrid,nlayer)  ! satu ratio for output
463      REAL zqsat(ngrid,nlayer) ! saturation
464      REAL satuco2(ngrid,nlayer)  ! co2 satu ratio for output
465      REAL zqsatco2(ngrid,nlayer) ! saturation co2
466
467
468! Added for new NLTE scheme
469
470      real co2vmr_gcm(ngrid,nlayer)
471      real n2vmr_gcm(ngrid,nlayer)
472      real ovmr_gcm(ngrid,nlayer)
473      real covmr_gcm(ngrid,nlayer)
474      integer ierr_nlte
475      real*8  varerr
476
477C  Non-oro GW drag & Calcul of Brunt-Vaisala freq. (BV2)
478      REAL ztetalev(ngrid,nlayer)
479      real zdtetalev(ngrid,nlayer), zdzlev(ngrid,nlayer)
480      REAL bv2(ngrid,nlayer)    ! BV2 at zlev   
481c  Non-oro GW tendencies
482      REAL d_u_hin(ngrid,nlayer), d_v_hin(ngrid,nlayer)
483      REAL d_t_hin(ngrid,nlayer)
484c  Diagnostics 2D of gw_nonoro
485      REAL zustrhi(ngrid), zvstrhi(ngrid)
486c Variables for PBL
487      REAL zz1(ngrid)
488      REAL lmax_th_out(ngrid)
489      REAL pdu_th(ngrid,nlayer),pdv_th(ngrid,nlayer)
490      REAL pdt_th(ngrid,nlayer),pdq_th(ngrid,nlayer,nq)
491      INTEGER lmax_th(ngrid),dimout,n_out,n
492      CHARACTER(50) zstring
493      REAL dtke_th(ngrid,nlayer+1)
494      REAL zcdv(ngrid), zcdh(ngrid)
495      REAL, ALLOCATABLE, DIMENSION(:,:) :: T_out
496      REAL, ALLOCATABLE, DIMENSION(:,:) :: u_out ! Interpolated teta and u at z_out
497      REAL u_out1(ngrid)
498      REAL T_out1(ngrid)
499      REAL, ALLOCATABLE, DIMENSION(:) :: z_out     ! height of interpolation between z0 and z1 [meters]
500      REAL tstar(ngrid)  ! friction velocity and friction potential temp
501      REAL L_mo(ngrid),vhf(ngrid),vvv(ngrid)
502      real qdustrds0(ngrid,nlayer),qdustrds1(ngrid,nlayer)
503      real qstormrds0(ngrid,nlayer),qstormrds1(ngrid,nlayer)   
504      real qdusttotal0(ngrid),qdusttotal1(ngrid)
505
506c sub-grid scale water ice clouds (A. Pottier 2013)
507      logical clearsky
508      ! flux for the part without clouds
509      real zdtswclf(ngrid,nlayer)
510      real zdtlwclf(ngrid,nlayer)
511      real fluxsurf_lwclf(ngrid)     
512      real fluxsurf_dn_swclf(ngrid,2),fluxsurf_up_swclf(ngrid,2)
513      real fluxtop_lwclf(ngrid)
514      real fluxtop_dn_swclf(ngrid,2),fluxtop_up_swclf(ngrid,2)
515      real taucloudtesclf(ngrid)
516      real tf_clf, ntf_clf ! tf: fraction of clouds, ntf: fraction without clouds
517      real rave2(ngrid), totrave2(ngrid) ! Mean water ice mean radius (m)
518C test de conservation de la masse de CO2
519      REAL co2totA
520      REAL co2totB
521      REAL co2conservation
522
523c entrainment by mountain top dust flows above sub-grid scale topography
524      REAL pdqtop(ngrid,nlayer,nq) ! tendency for dust after topmons
525
526c when no startfi file is asked for init
527      real alpha,lay1 ! coefficients for building layers
528      integer iloop
529
530      ! flags to trigger extra sanity checks
531      logical,save :: check_physics_inputs=.false.
532      logical,save :: check_physics_outputs=.false.
533     
534!$OMP THREADPRIVATE(check_physics_inputs,check_physics_outputs)     
535
536c Sub-grid scale slopes
537      real :: tsurf_meshavg(ngrid) ! Surface temperature grid box averaged [K]
538      real :: albedo_meshavg(ngrid,2) ! albedo temperature grid box averaged [1]
539      real :: emis_meshavg(ngrid,2) ! emis temperature grid box averaged [1]
540      real :: qsurf_meshavg(ngrid,nq) ! surface tracer mesh averaged [kg/m^2]
541      real :: qsurf_tmp(ngrid,nq) ! temporary qsurf for chimie
542      real :: inertiedat_tmp(ngrid,nsoilmx,nslope) ! temporary inertiedat for soil.F
543      integer :: islope
544      real :: zdqsdif_meshavg_tmp(ngrid,nq) ! temporary for dust lifting
545
546      logical :: write_restart
547
548c=======================================================================
549      pdq(:,:,:) = 0.
550
551c 1. Initialisation:
552c -----------------
553c  1.1   Initialisation only at first call
554c  ---------------------------------------
555
556      IF (firstcall) THEN
557
558         call getin_p("check_physics_inputs",check_physics_inputs)
559         call getin_p("check_physics_outputs",check_physics_outputs)
560
561c        variables set to 0
562c        ~~~~~~~~~~~~~~~~~~
563         aerosol(:,:,:)=0
564         dtrad(:,:)=0
565
566#ifndef MESOSCALE
567         fluxrad(:,:)=0
568         wstar(:)=0.
569#endif
570
571#ifdef CPP_XIOS
572         ! Initialize XIOS context
573         write(*,*) "physiq: call wxios_context_init"
574         CALL wxios_context_init
575#endif
576
577c        read startfi
578c        ~~~~~~~~~~~~
579#ifndef MESOSCALE
580
581! GCM. Read netcdf initial physical parameters.
582         CALL phyetat0 ("startfi.nc",0,0,
583     &         nsoilmx,ngrid,nlayer,nq,
584     &         day_ini,time_phys,
585     &         tsurf,tsoil,albedo,emis,
586     &         q2,qsurf,tauscaling,totcloudfrac,wstar,
587     &         watercap,def_slope,def_slope_mean,subslope_dist)
588
589       DO islope=1,nslope
590       sky_slope(islope) = (1.+cos(pi*def_slope_mean(islope)/180.))/2.
591       END DO
592
593!     Sky view:
594       DO islope=1,nslope
595       sky_slope(islope) = (1.+cos(pi*def_slope_mean(islope)/180.))/2.
596       END DO
597!     Determine the 'flatest' slopes
598       iflat = 1
599       DO islope=2,nslope
600         IF(abs(def_slope_mean(islope)).lt.
601     &      abs(def_slope_mean(iflat)))THEN
602           iflat = islope
603         ENDIF
604       ENDDO
605       PRINT*,'Flat slope for islope = ',iflat
606       PRINT*,'corresponding criterium = ',def_slope_mean(iflat)
607
608#else
609! MESOSCALE. Supposedly everything is already set in modules.
610! So we just check. And we fill day_ini
611      print*,"check: --- in physiq.F"
612      print*,"check: rad,cpp,g,r,rcp,daysec"
613      print*,rad,cpp,g,r,rcp,daysec
614      PRINT*,'check: tsurf ',tsurf(1,:),tsurf(ngrid,:)
615      PRINT*,'check: tsoil ',tsoil(1,1,:),tsoil(ngrid,nsoilmx,:)
616      PRINT*,'check: inert ',inertiedat(1,1),inertiedat(ngrid,nsoilmx)
617      PRINT*,'check: midlayer,layer ', mlayer(:),layer(:)
618      PRINT*,'check: tracernames ', noms
619      PRINT*,'check: emis ',emis(1,:),emis(ngrid,:)
620      PRINT*,'check: q2 ',q2(1,1),q2(ngrid,nlayer+1)
621      PRINT*,'check: qsurf ',qsurf(1,1,:),qsurf(ngrid,nq,:)
622      PRINT*,'check: co2ice ',qsurf(1,igcm_co2,:),qsurf(ngrid,igcm_co2,:)
623      !!!
624      day_ini = pday
625      !!! a couple initializations (dummy for mesoscale) done in phyetat0
626      !!! --- maybe this should be done in update_inputs_physiq_mod
627
628      tauscaling(:)=1.0 !! probably important
629      totcloudfrac(:)=1.0
630      albedo(:,1)=albedodat(:)
631      albedo(:,2)=albedo(:,1)
632      watercap(:)=0.0
633#endif
634#ifndef MESOSCALE
635         if (.not.startphy_file) then
636           ! starting without startfi.nc and with callsoil
637           ! is not yet possible as soildepth default is not defined
638           if (callsoil) then
639              ! default mlayer distribution, following a power law:
640              !  mlayer(k)=lay1*alpha**(k-1/2)
641              lay1=2.e-4
642              alpha=2
643              do iloop=0,nsoilmx-1
644                 mlayer(iloop)=lay1*(alpha**(iloop-0.5))
645              enddo
646              lay1=sqrt(mlayer(0)*mlayer(1))
647              alpha=mlayer(1)/mlayer(0)
648              do iloop=1,nsoilmx
649                 layer(iloop)=lay1*(alpha**(iloop-1))
650              enddo
651           endif
652           ! additionnal "academic" initialization of physics
653           do islope = 1,nslope
654             tsurf(:,islope)=pt(:,1)
655           enddo
656           write(*,*) "Physiq: initializing tsoil(:) to pt(:,1) !!"
657           do isoil=1,nsoilmx
658             tsoil(1:ngrid,isoil,:)=tsurf(1:ngrid,:)
659           enddo
660           write(*,*) "Physiq: initializing inertiedat !!"
661           inertiedat(:,:)=400.
662           write(*,*) "Physiq: initializing day_ini to pdat !"
663           day_ini=pday
664        endif
665       DO islope = 1,nslope
666         inertiedat_tmp(:,:,islope) = inertiedat(:,:)
667       ENDDO
668#endif
669         if (pday.ne.day_ini) then
670           write(*,*) "PHYSIQ: ERROR: bad synchronization between ",
671     &                "physics and dynamics"
672           write(*,*) "dynamics day [pday]: ",pday
673           write(*,*) "physics day [day_ini]: ",day_ini
674           call abort_physic("physiq","dynamics day /= physics day",1)
675         endif
676
677         write (*,*) 'In physiq day_ini =', day_ini
678
679c        initialize tracers
680c        ~~~~~~~~~~~~~~~~~~
681
682         CALL initracer(ngrid,nq,qsurf)
683
684c        Initialize albedo and orbital calculation
685c        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686         CALL surfini(ngrid,qsurf)
687
688         CALL iniorbit(aphelie,periheli,year_day,peri_day,obliquit)
689
690c        initialize soil
691c        ~~~~~~~~~~~~~~~
692         IF (callsoil) THEN
693c           Thermal inertia feedback:
694            IF (tifeedback) THEN
695             DO islope = 1,nslope
696                CALL soil_tifeedback(ngrid,nsoilmx,
697     s               qsurf(:,:,islope),inertiesoil(:,:,islope))
698             ENDDO
699                CALL soil(ngrid,nsoilmx,firstcall,inertiesoil,
700     s             ptimestep,tsurf,tsoil,capcal,fluxgrd)
701            ELSE
702                CALL soil(ngrid,nsoilmx,firstcall,inertiedat_tmp,
703     s             ptimestep,tsurf,tsoil,capcal,fluxgrd)
704            ENDIF ! of IF (tifeedback)
705         ELSE
706            PRINT*,
707     &     'PHYSIQ WARNING! Thermal conduction in the soil turned off'
708            DO ig=1,ngrid
709               capcal(ig,:)=1.e5
710               fluxgrd(ig,:)=0.
711            ENDDO
712         ENDIF
713         icount=1
714
715#ifndef MESOSCALE
716c        Initialize thermospheric parameters
717c        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
718
719         if (callthermos) then
720            call fill_data_thermos
721            call allocate_param_thermos(nlayer)
722            call allocate_param_iono(nlayer,nreact)
723            call param_read_e107
724         endif
725#endif
726c        Initialize R and Cp as constant
727
728         if (.not.callthermos .and. .not.photochem) then
729                 do l=1,nlayer
730                  do ig=1,ngrid
731                   rnew(ig,l)=r
732                   cpnew(ig,l)=cpp
733                   mmean(ig,l)=mugaz
734                   enddo
735                  enddo 
736         endif         
737
738         if(callnlte.and.nltemodel.eq.2) call nlte_setup
739         if(callnirco2.and.nircorr.eq.1) call NIR_leedat
740
741
742        IF (water.AND.(ngrid.NE.1)) THEN
743          write(*,*)"physiq: water_param Surface water frost albedo:",
744     .                  albedo_h2o_frost
745          write(*,*)"physiq: water_param Surface watercap albedo:",
746     .                  albedo_h2o_cap
747        ENDIF
748
749#ifndef MESOSCALE
750
751         if (ngrid.ne.1) then
752           ! no need to compute slopes when in 1D; it is an input
753           if (callslope) call getslopes(ngrid,phisfi)
754           ! no need to create a restart file in 1d
755         if (ecritstart.GT.0) then
756             call physdem0("restartfi.nc",longitude,latitude,
757     &                   nsoilmx,ngrid,nlayer,nq,
758     &                   ptimestep,pday,0.,cell_area,
759     &                   albedodat,inertiedat,def_slope,
760     &                   subslope_dist)
761          else
762             call physdem0("restartfi.nc",longitude,latitude,
763     &                   nsoilmx,ngrid,nlayer,nq,
764     &                   ptimestep,float(day_end),0.,cell_area,
765     &                   albedodat,inertiedat,def_slope,
766     &                   subslope_dist)
767          endif
768         endif
769
770c        Initialize mountain mesh fraction for the entrainment by top flows param.
771c        ~~~~~~~~~~~~~~~
772        if (topflows) call topmons_setup(ngrid)
773
774#endif
775                 
776#ifdef CPP_XIOS
777        ! XIOS outputs
778        write(*,*) "physiq firstcall: call initialize_xios_output"
779        call initialize_xios_output(pday,ptime,ptimestep,daysec,
780     &                              presnivs,pseudoalt,mlayer)
781#endif
782      ENDIF        !  (end of "if firstcall")
783
784      if (check_physics_inputs) then
785        ! Check the validity of input fields coming from the dynamics
786        call check_physics_fields("begin physiq:",pt,pu,pv,pplev,pq)
787      endif
788
789c ---------------------------------------------------
790c 1.2   Initializations done at every physical timestep:
791c ---------------------------------------------------
792c
793
794#ifdef CPP_XIOS     
795      ! update XIOS time/calendar
796      call update_xios_timestep     
797#endif
798
799
800
801c     Initialize various variables
802c     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
803      pdv(:,:)=0
804      pdu(:,:)=0
805      pdt(:,:)=0
806      pdq(:,:,:)=0
807      pdpsrf(:)=0
808      zflubid(:,:)=0
809      zdtsurf(:,:)=0
810      dqsurf(:,:,:)=0
811      dsodust(:,:)=0.
812      dsords(:,:)=0.
813      dsotop(:,:)=0.
814      dwatercap(:,:)=0
815
816      call compute_meshgridavg(ngrid,nq,albedo,emis,tsurf,qsurf,
817     &       albedo_meshavg,emis_meshavg,tsurf_meshavg,qsurf_meshavg)
818     
819      ! Dust scenario conversion coefficient from IRabs to VISext
820      IRtoVIScoef(1:ngrid)=2.6 ! initialized with former value from Montabone et al 2015
821                               ! recomputed in aeropacity if reff_driven_IRtoVIS_scenario=.true.
822
823#ifdef DUSTSTORM
824      pq_tmp(:,:,:)=0
825#endif
826      igout=ngrid/2+1
827
828
829      zday=pday+ptime ! compute time, in sols (and fraction thereof)
830      ! Compute local time at each grid point
831      DO ig=1,ngrid
832       local_time(ig)=modulo(1.+(zday-INT(zday))
833     &                +(longitude_deg(ig)/15)/24,1.)
834      ENDDO
835c     Compute Solar Longitude (Ls) :
836c     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
837      if (season) then
838         call solarlong(zday,zls)
839      else
840         call solarlong(float(day_ini),zls)
841      end if
842
843c     Initialize pressure levels
844c     ~~~~~~~~~~~~~~~~~~
845      zplev(:,:) = pplev(:,:)
846      zplay(:,:) = pplay(:,:)
847      ps(:) = pplev(:,1)
848
849c     Compute geopotential at interlayers
850c     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
851c     ponderation des altitudes au niveau des couches en dp/p
852
853      DO l=1,nlayer
854         DO ig=1,ngrid
855            zzlay(ig,l)=pphi(ig,l)/g
856         ENDDO
857      ENDDO
858      DO ig=1,ngrid
859         zzlev(ig,1)=0.
860         zzlev(ig,nlayer+1)=1.e7    ! dummy top of last layer above 10000 km...
861      ENDDO
862      DO l=2,nlayer
863         DO ig=1,ngrid
864            z1=(zplay(ig,l-1)+zplev(ig,l))/(zplay(ig,l-1)-zplev(ig,l))
865            z2=(zplev(ig,l)+zplay(ig,l))/(zplev(ig,l)-zplay(ig,l))
866            zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
867         ENDDO
868      ENDDO
869
870
871!     Potential temperature calculation not the same in physiq and dynamic
872
873c     Compute potential temperature
874c     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
875      DO l=1,nlayer
876         DO ig=1,ngrid
877            zpopsk(ig,l)=(zplay(ig,l)/zplev(ig,1))**rcp
878            zh(ig,l)=pt(ig,l)/zpopsk(ig,l)
879         ENDDO
880      ENDDO
881
882#ifndef MESOSCALE
883c-----------------------------------------------------------------------
884c    1.2.5 Compute mean mass, cp, and R
885c    --------------------------------
886
887      if(photochem.or.callthermos) then
888         call concentrations(ngrid,nlayer,nq,
889     &                       zplay,pt,pdt,pq,pdq,ptimestep)
890      endif
891#endif
892
893      ! Compute vertical velocity (m/s) from vertical mass flux
894      ! w = F / (rho*area) and rho = P/(r*T)
895      ! but first linearly interpolate mass flux to mid-layers
896      do l=1,nlayer-1
897       pw(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
898      enddo
899      pw(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
900      do l=1,nlayer
901       pw(1:ngrid,l)=(pw(1:ngrid,l)*r*pt(1:ngrid,l)) /
902     &               (pplay(1:ngrid,l)*cell_area(1:ngrid))
903       ! NB: here we use r and not rnew since this diagnostic comes
904       ! from the dynamics
905      enddo
906
907      ! test for co2 conservation with co2 microphysics
908      if (igcm_co2_ice.ne.0) then
909        ! calculates the amount of co2 at the beginning of physics
910        co2totA = 0.
911        do ig=1,ngrid
912          do l=1,nlayer
913             co2totA = co2totA + (zplev(ig,l)-zplev(ig,l+1))/g*
914     &             (pq(ig,l,igcm_co2)+pq(ig,l,igcm_co2_ice)
915     &        +(pdq(ig,l,igcm_co2)+pdq(ig,l,igcm_co2_ice))*ptimestep)
916          end do
917          do islope = 1,nslope
918          co2totA = co2totA + qsurf(ig,igcm_co2,islope)*
919     &    subslope_dist(ig,islope)/cos(pi*def_slope_mean(islope)/180.)
920          enddo
921        end do
922      else
923        co2totA = 0.
924        do ig=1,ngrid
925          do l=1,nlayer
926             co2totA = co2totA + (zplev(ig,l)-zplev(ig,l+1))/g*
927     &             (pq(ig,l,igcm_co2)
928     &        +pdq(ig,l,igcm_co2)*ptimestep)
929          end do
930          do islope = 1,nslope
931          co2totA = co2totA + qsurf(ig,igcm_co2,islope)*
932     &    subslope_dist(ig,islope)/cos(pi*def_slope_mean(islope)/180.)
933          enddo
934        end do
935      endif ! of if (igcm_co2_ice.ne.0)
936c-----------------------------------------------------------------------
937c    2. Compute radiative tendencies :
938c------------------------------------
939
940      IF (callrad) THEN
941
942c       Local Solar zenith angle
943c       ~~~~~~~~~~~~~~~~~~~~~~~~
944
945        CALL orbite(zls,dist_sol,declin)
946
947        IF (diurnal) THEN
948            ztim1=SIN(declin)
949            ztim2=COS(declin)*COS(2.*pi*(zday-.5))
950            ztim3=-COS(declin)*SIN(2.*pi*(zday-.5))
951
952            CALL solang(ngrid,sinlon,coslon,sinlat,coslat,
953     &                  ztim1,ztim2,ztim3, mu0,fract)
954
955        ELSE
956            CALL mucorr(ngrid,declin,latitude,mu0,fract,10000.,rad)
957        ENDIF ! of IF (diurnal)
958
959         IF( MOD(icount-1,iradia).EQ.0) THEN
960
961c          NLTE cooling from CO2 emission
962c          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
963           IF(callnlte) then
964              if(nltemodel.eq.0.or.nltemodel.eq.1) then
965                CALL nltecool(ngrid,nlayer,nq,zplay,pt,pq,zdtnlte)
966              else if(nltemodel.eq.2) then
967                co2vmr_gcm(1:ngrid,1:nlayer)=
968     &                      pq(1:ngrid,1:nlayer,igcm_co2)*
969     &                      mmean(1:ngrid,1:nlayer)/mmol(igcm_co2)
970                n2vmr_gcm(1:ngrid,1:nlayer)=
971     &                      pq(1:ngrid,1:nlayer,igcm_n2)*
972     &                      mmean(1:ngrid,1:nlayer)/mmol(igcm_n2)
973                covmr_gcm(1:ngrid,1:nlayer)=
974     &                      pq(1:ngrid,1:nlayer,igcm_co)*
975     &                      mmean(1:ngrid,1:nlayer)/mmol(igcm_co)
976                ovmr_gcm(1:ngrid,1:nlayer)=
977     &                      pq(1:ngrid,1:nlayer,igcm_o)*
978     &                      mmean(1:ngrid,1:nlayer)/mmol(igcm_o)
979
980                 CALL nlte_tcool(ngrid,nlayer,zplay*9.869e-6,
981     $                pt,zzlay,co2vmr_gcm, n2vmr_gcm, covmr_gcm,
982     $                ovmr_gcm,  zdtnlte,ierr_nlte,varerr )
983                 if(ierr_nlte.gt.0) then
984                    write(*,*)
985     $                'WARNING: nlte_tcool output with error message',
986     $                'ierr_nlte=',ierr_nlte,'varerr=',varerr
987                    write(*,*)'I will continue anyway'
988                 endif
989
990                 zdtnlte(1:ngrid,1:nlayer)=
991     &                             zdtnlte(1:ngrid,1:nlayer)/86400.
992              endif
993           ELSE
994              zdtnlte(:,:)=0.
995           ENDIF !end callnlte
996
997c          Find number of layers for LTE radiation calculations
998           IF(MOD(iphysiq*(icount-1),day_step).EQ.0)
999     &          CALL nlthermeq(ngrid,nlayer,zplev,zplay)
1000
1001c          rocketstorm : compute dust storm mesh fraction
1002           IF (rdstorm) THEN
1003              CALL calcstormfract(ngrid,nlayer,nq,pq,
1004     &                 totstormfract)
1005           ENDIF
1006
1007c          Note: Dustopacity.F has been transferred to callradite.F
1008
1009#ifdef DUSTSTORM
1010!! specific case: save the quantity of dust before adding perturbation
1011
1012       if (firstcall) then
1013        pq_tmp(1:ngrid,1:nlayer,1)=pq(1:ngrid,1:nlayer,igcm_dust_mass)
1014        pq_tmp(1:ngrid,1:nlayer,2)=pq(1:ngrid,1:nlayer,igcm_dust_number)
1015       endif
1016#endif
1017         
1018c          Call main radiative transfer scheme
1019c          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1020c          Transfer through CO2 (except NIR CO2 absorption)
1021c            and aerosols (dust and water ice)
1022           ! callradite for background dust (out of the rdstorm fraction)
1023           clearatm=.true.
1024           !! callradite for background dust (out of the topflows fraction)
1025           nohmons=.true.
1026
1027c          Radiative transfer
1028c          ------------------
1029           ! callradite for the part with clouds
1030           clearsky=.false. ! part with clouds for both cases CLFvarying true/false
1031           CALL callradite(icount,ngrid,nlayer,nq,zday,zls,pq,
1032     &     albedo_meshavg,emis_meshavg,
1033     &     mu0,zplev,zplay,pt,tsurf_meshavg,fract,dist_sol,igout,
1034     &     zdtlw,zdtsw,fluxsurf_lw(:,iflat),fluxsurf_dn_sw(:,:,iflat),
1035     &     fluxsurf_up_sw,
1036     &     fluxtop_lw,fluxtop_dn_sw,fluxtop_up_sw,
1037     &     tau_pref_scenario,tau_pref_gcm,
1038     &     tau,aerosol,dsodust,tauscaling,dust_rad_adjust,IRtoVIScoef,
1039     &     taucloudtes,rdust,rice,nuice,riceco2,nuiceco2,
1040     &     qsurf_meshavg(:,igcm_co2),rstormdust,rtopdust,totstormfract,
1041     &     clearatm,dsords,dsotop,nohmons,clearsky,totcloudfrac)
1042
1043           DO islope=1,nslope
1044             fluxsurf_lw(:,islope) =fluxsurf_lw(:,iflat)
1045             fluxsurf_dn_sw(:,:,islope) =fluxsurf_dn_sw(:,:,iflat)
1046           ENDDO
1047
1048           ! case of sub-grid water ice clouds: callradite for the clear case
1049            IF (CLFvarying) THEN
1050               ! ---> PROBLEMS WITH ALLOCATED ARRAYS
1051               ! (temporary solution in callcorrk: do not deallocate
1052               ! if
1053               ! CLFvarying ...) ?? AP ??
1054               clearsky=.true. 
1055               CALL callradite(icount,ngrid,nlayer,nq,zday,zls,pq,
1056     &              albedo_meshavg,emis_meshavg,mu0,zplev,zplay,pt,
1057     &              tsurf_meshavg,fract,
1058     &              dist_sol,igout,zdtlwclf,zdtswclf,
1059     &              fluxsurf_lwclf,fluxsurf_dn_swclf,fluxsurf_up_swclf,
1060     &              fluxtop_lwclf,fluxtop_dn_swclf,fluxtop_up_swclf,
1061     &              tau_pref_scenario,tau_pref_gcm,tau,aerosol,
1062     &              dsodust,tauscaling,dust_rad_adjust,IRtoVIScoef,
1063     &              taucloudtesclf,rdust,
1064     &              rice,nuice,riceco2, nuiceco2,
1065     &              qsurf_meshavg(:,igcm_co2),
1066     &              rstormdust,rtopdust,totstormfract,
1067     &              clearatm,dsords,dsotop,
1068     &              nohmons,clearsky,totcloudfrac)
1069               clearsky = .false.  ! just in case.
1070               ! Sum the fluxes and heating rates from cloudy/clear
1071               ! cases
1072               DO ig=1,ngrid
1073                  tf_clf=totcloudfrac(ig)
1074                  ntf_clf=1.-tf_clf
1075                  DO islope=1,nslope
1076                  fluxsurf_lw(ig,islope) = ntf_clf*fluxsurf_lwclf(ig)
1077     &                          + tf_clf*fluxsurf_lw(ig,islope)
1078                  fluxsurf_dn_sw(ig,1:2,islope) =
1079     &                           ntf_clf*fluxsurf_dn_swclf(ig,1:2)
1080     &                          + tf_clf*fluxsurf_dn_sw(ig,1:2,islope)
1081                  ENDDO
1082                  fluxsurf_up_sw(ig,1:2) =
1083     &                           ntf_clf*fluxsurf_up_swclf(ig,1:2)
1084     &                          + tf_clf*fluxsurf_up_sw(ig,1:2)
1085                  fluxtop_lw(ig)  = ntf_clf*fluxtop_lwclf(ig)
1086     &                                      + tf_clf*fluxtop_lw(ig)
1087                  fluxtop_dn_sw(ig,1:2)=ntf_clf*fluxtop_dn_swclf(ig,1:2)
1088     &                                 + tf_clf*fluxtop_dn_sw(ig,1:2)
1089                  fluxtop_up_sw(ig,1:2)=ntf_clf*fluxtop_up_swclf(ig,1:2)
1090     &                                 + tf_clf*fluxtop_up_sw(ig,1:2)
1091                  taucloudtes(ig) = ntf_clf*taucloudtesclf(ig)
1092     &                                      + tf_clf*taucloudtes(ig)
1093                  zdtlw(ig,1:nlayer) = ntf_clf*zdtlwclf(ig,1:nlayer)
1094     &                                      + tf_clf*zdtlw(ig,1:nlayer)
1095                  zdtsw(ig,1:nlayer) = ntf_clf*zdtswclf(ig,1:nlayer)
1096     &                                      + tf_clf*zdtsw(ig,1:nlayer)
1097               ENDDO
1098
1099            ENDIF ! (CLFvarying)
1100           
1101!============================================================================
1102           
1103#ifdef DUSTSTORM
1104!! specific case: compute the added quantity of dust for perturbation
1105       if (firstcall) then
1106         pdq(1:ngrid,1:nlayer,igcm_dust_mass)=
1107     &      pdq(1:ngrid,1:nlayer,igcm_dust_mass)     
1108     &      - pq_tmp(1:ngrid,1:nlayer,1)
1109     &      + pq(1:ngrid,1:nlayer,igcm_dust_mass)
1110         pdq(1:ngrid,1:nlayer,igcm_dust_number)=
1111     &      pdq(1:ngrid,1:nlayer,igcm_dust_number)
1112     &      - pq_tmp(1:ngrid,1:nlayer,2)
1113     &      + pq(1:ngrid,1:nlayer,igcm_dust_number)
1114       endif
1115#endif
1116
1117c          Outputs for basic check (middle of domain)
1118c          ------------------------------------------
1119           write(*,'("Ls =",f11.6," check lat =",f10.6,
1120     &               " lon =",f11.6)')
1121     &           zls*180./pi,latitude(igout)*180/pi,
1122     &                       longitude(igout)*180/pi
1123
1124           write(*,'(" tau_pref_gcm(",f4.0," Pa) =",f9.6,
1125     &             " tau(",f4.0," Pa) =",f9.6)')
1126     &            odpref,tau_pref_gcm(igout),
1127     &            odpref,tau(igout,1)*odpref/zplev(igout,1)
1128
1129
1130c          ---------------------------------------------------------
1131c          Call slope parameterization for direct and scattered flux
1132c          ---------------------------------------------------------
1133           IF(callslope) THEN
1134           ! assume that in this case, nslope = 1
1135            if(nslope.ne.1) then
1136              call abort_physic(
1137     &      "physiq","callslope=true but nslope.ne.1",1)
1138            endif
1139            print *, 'Slope scheme is on and computing...'
1140            DO ig=1,ngrid 
1141              sl_the = theta_sl(ig)
1142              IF (sl_the .ne. 0.) THEN
1143                ztim1=fluxsurf_dn_sw(ig,1,1)+fluxsurf_dn_sw(ig,2,1)
1144                DO l=1,2
1145                 sl_lct = ptime*24. + 180.*longitude(ig)/pi/15.
1146                 sl_ra  = pi*(1.0-sl_lct/12.)
1147                 sl_lat = 180.*latitude(ig)/pi
1148                 sl_tau = tau(ig,1) !il faudrait iaerdust(iaer)
1149                 sl_alb = albedo(ig,l,1)
1150                 sl_psi = psi_sl(ig)
1151                 sl_fl0 = fluxsurf_dn_sw(ig,l,1)
1152                 sl_di0 = 0.
1153                 if ((mu0(ig) .gt. 0.).and.(ztim1.gt.0.)) then
1154                  sl_di0 = mu0(ig)*(exp(-sl_tau/mu0(ig)))
1155                  sl_di0 = sl_di0*flux_1AU/dist_sol/dist_sol
1156                  sl_di0 = sl_di0/ztim1
1157                  sl_di0 = fluxsurf_dn_sw(ig,l,1)*sl_di0
1158                 endif
1159                 ! you never know (roundup concern...)
1160                 if (sl_fl0 .lt. sl_di0) sl_di0=sl_fl0
1161                 !!!!!!!!!!!!!!!!!!!!!!!!!!
1162                 CALL param_slope( mu0(ig), declin, sl_ra, sl_lat,
1163     &                             sl_tau, sl_alb, sl_the, sl_psi,
1164     &                             sl_di0, sl_fl0, sl_flu )
1165                 !!!!!!!!!!!!!!!!!!!!!!!!!!
1166                 fluxsurf_dn_sw(ig,l,1) = sl_flu
1167                ENDDO
1168              !!! compute correction on IR flux as well
1169              sky= (1.+cos(pi*theta_sl(ig)/180.))/2.
1170              fluxsurf_lw(ig,:)= fluxsurf_lw(ig,:)*sky
1171              ENDIF
1172            ENDDO
1173c          ELSE        ! not calling subslope, nslope might be > 1
1174c          DO islope = 1,nslope
1175c            IF(def_slope_mean(islope).ge.0.) THEN
1176c              sl_psi = 0. !Northward slope
1177c            ELSE
1178c             sl_psi = 180. !Southward slope
1179c            ENDIF
1180c            sl_the=abs(def_slope_mean(islope))         
1181c            IF (sl_the .gt. 1e-6) THEN
1182c              DO ig=1,ngrid 
1183c                ztim1=fluxsurf_dn_sw(ig,1,islope)
1184c     s                +fluxsurf_dn_sw(ig,2,islope)
1185c                DO l=1,2
1186c                 sl_lct = ptime*24. + 180.*longitude(ig)/pi/15.
1187c                 sl_ra  = pi*(1.0-sl_lct/12.)
1188c                 sl_lat = 180.*latitude(ig)/pi
1189c                 sl_tau = tau(ig,1) !il faudrait iaerdust(iaer)
1190c                 sl_alb = albedo(ig,l,islope)
1191c                 sl_psi = psi_sl(ig)
1192c                 sl_fl0 = fluxsurf_dn_sw(ig,l,islope)
1193c                 sl_di0 = 0.
1194c                 if (mu0(ig) .gt. 0.) then
1195c                         sl_di0 = mu0(ig)*(exp(-sl_tau/mu0(ig)))
1196c                  sl_di0 = sl_di0*flux_1AU/dist_sol/dist_sol
1197c                  sl_di0 = sl_di0/ztim1
1198c                  sl_di0 = fluxsurf_dn_sw(ig,l,islope)*sl_di0
1199c                 endif
1200c                 ! you never know (roundup concern...)
1201c                 if (sl_fl0 .lt. sl_di0) sl_di0=sl_fl0
1202c                 !!!!!!!!!!!!!!!!!!!!!!!!!!
1203c                 CALL param_slope( mu0(ig), declin, sl_ra, sl_lat,
1204c     &                             sl_tau, sl_alb, sl_the, sl_psi,
1205c     &                             sl_di0, sl_fl0, sl_flu )
1206c                 !!!!!!!!!!!!!!!!!!!!!!!!!!
1207c                 fluxsurf_dn_sw(ig,l,islope) = sl_flu
1208c                ENDDO
1209c              !!! compute correction on IR flux as well
1210c
1211c              fluxsurf_lw(ig,islope)= fluxsurf_lw(ig,islope)
1212c     &                                *sky_slope(islope)
1213c              ENDDO
1214c            ENDIF
1215c          ENDDO ! islope = 1,nslope
1216          ENDIF ! callslope
1217
1218c          CO2 near infrared absorption
1219c          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1220           zdtnirco2(:,:)=0
1221           if (callnirco2) then
1222              call nirco2abs (ngrid,nlayer,zplay,dist_sol,nq,pq,
1223     .                       mu0,fract,declin, zdtnirco2)
1224           endif
1225
1226c          Radiative flux from the sky absorbed by the surface (W.m-2)
1227c          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1228           DO ig=1,ngrid
1229             DO islope = 1,nslope
1230               fluxrad_sky(ig,islope) =
1231     $           emis(ig,islope)*fluxsurf_lw(ig,islope)
1232     $         +fluxsurf_dn_sw(ig,1,islope)*(1.-albedo(ig,1,islope))         
1233     $         +fluxsurf_dn_sw(ig,2,islope)*(1.-albedo(ig,2,islope))
1234             ENDDO
1235           ENDDO
1236
1237
1238c          Net atmospheric radiative heating rate (K.s-1)
1239c          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1240           IF(callnlte) THEN
1241              CALL blendrad(ngrid, nlayer, zplay,
1242     &             zdtsw, zdtlw, zdtnirco2, zdtnlte, dtrad)
1243           ELSE
1244              DO l=1,nlayer
1245                 DO ig=1,ngrid
1246                    dtrad(ig,l)=zdtsw(ig,l)+zdtlw(ig,l)
1247     &                          +zdtnirco2(ig,l)
1248                  ENDDO
1249              ENDDO
1250           ENDIF
1251
1252        ENDIF ! of if(mod(icount-1,iradia).eq.0)
1253
1254c    Transformation of the radiative tendencies:
1255c    -------------------------------------------
1256
1257c          Net radiative surface flux (W.m-2)
1258c          ~~~~~~~~~~~~~~~~~~~~~~~~~~
1259c
1260           DO ig=1,ngrid
1261             DO islope = 1,nslope
1262               zplanck(ig)=tsurf(ig,islope)*tsurf(ig,islope)
1263               zplanck(ig)=emis(ig,islope)*
1264     $         stephan*zplanck(ig)*zplanck(ig)
1265               fluxrad(ig,islope)=fluxrad_sky(ig,islope)-zplanck(ig)
1266               IF(callslope) THEN
1267                 sky= (1.+cos(pi*theta_sl(ig)/180.))/2.
1268                 fluxrad(ig,nslope)=fluxrad(ig,nslope)+
1269     $                             (1.-sky)*zplanck(ig)
1270               ELSE
1271                 fluxrad(ig,islope)=fluxrad(ig,islope) +
1272     $              (1.-sky_slope(iflat))*emis(ig,iflat)*
1273     $              stephan*tsurf(ig,iflat)**4 
1274               ENDIF
1275           ENDDO
1276         ENDDO
1277
1278         DO l=1,nlayer
1279            DO ig=1,ngrid
1280               pdt(ig,l)=pdt(ig,l)+dtrad(ig,l)
1281            ENDDO
1282         ENDDO
1283
1284      ENDIF ! of IF (callrad)
1285
1286c     3.1 Rocket dust storm
1287c    -------------------------------------------
1288      IF (rdstorm) THEN
1289         clearatm=.false.
1290         pdqrds(:,:,:)=0.
1291         qdusttotal0(:)=0.
1292         qdusttotal1(:)=0.
1293         do ig=1,ngrid
1294           do l=1,nlayer
1295            zdh(ig,l)=pdt(ig,l)/zpopsk(ig,l) ! updated potential
1296                                             ! temperature tendency
1297            ! for diagnostics
1298            qdustrds0(ig,l)=pq(ig,l,igcm_dust_mass)+
1299     &           pdq(ig,l,igcm_dust_mass)*ptimestep
1300            qstormrds0(ig,l)=pq(ig,l,igcm_stormdust_mass)+
1301     &           pdq(ig,l,igcm_stormdust_mass)*ptimestep
1302            qdusttotal0(ig)=qdusttotal0(ig)+(qdustrds0(ig,l)+
1303     &          qstormrds0(ig,l))*(zplev(ig,l)-
1304     &           zplev(ig,l+1))/g
1305           enddo
1306         enddo
1307         call writediagfi(ngrid,'qdustrds0','qdust before rds',
1308     &           'kg/kg ',3,qdustrds0)
1309         call writediagfi(ngrid,'qstormrds0','qstorm before rds',
1310     &           'kg/kg ',3,qstormrds0)
1311
1312         CALL rocketduststorm(ngrid,nlayer,nq,ptime,ptimestep,
1313     &                      pq,pdq,pt,pdt,zplev,zplay,zzlev,
1314     &                      zzlay,zdtsw,zdtlw,
1315c               for radiative transfer
1316     &                      clearatm,icount,zday,zls,
1317     &                      tsurf,qsurf_meshavg(:,igcm_co2),igout,
1318     &                      totstormfract,tauscaling,
1319     &                      dust_rad_adjust,IRtoVIScoef,
1320c               input sub-grid scale cloud
1321     &                      clearsky,totcloudfrac,
1322c               input sub-grid scale topography
1323     &                      nohmons,
1324c               output
1325     &                      pdqrds,wspeed,dsodust,dsords,dsotop,
1326     &                      tau_pref_scenario,tau_pref_gcm)
1327
1328c      update the tendencies of both dust after vertical transport
1329         DO l=1,nlayer
1330          DO ig=1,ngrid
1331           pdq(ig,l,igcm_stormdust_mass)=
1332     &              pdq(ig,l,igcm_stormdust_mass)+
1333     &                      pdqrds(ig,l,igcm_stormdust_mass)
1334           pdq(ig,l,igcm_stormdust_number)=
1335     &              pdq(ig,l,igcm_stormdust_number)+
1336     &                      pdqrds(ig,l,igcm_stormdust_number)
1337
1338           pdq(ig,l,igcm_dust_mass)=
1339     &       pdq(ig,l,igcm_dust_mass)+ pdqrds(ig,l,igcm_dust_mass)
1340           pdq(ig,l,igcm_dust_number)=
1341     &           pdq(ig,l,igcm_dust_number)+
1342     &                  pdqrds(ig,l,igcm_dust_number)
1343
1344          ENDDO
1345         ENDDO
1346         do l=1,nlayer
1347           do ig=1,ngrid
1348             qdustrds1(ig,l)=pq(ig,l,igcm_dust_mass)+
1349     &           pdq(ig,l,igcm_dust_mass)*ptimestep
1350             qstormrds1(ig,l)=pq(ig,l,igcm_stormdust_mass)+
1351     &           pdq(ig,l,igcm_stormdust_mass)*ptimestep
1352             qdusttotal1(ig)=qdusttotal1(ig)+(qdustrds1(ig,l)+
1353     &          qstormrds1(ig,l))*(zplev(ig,l)-
1354     &           zplev(ig,l+1))/g
1355           enddo
1356         enddo
1357
1358c        for diagnostics
1359         call writediagfi(ngrid,'qdustrds1','qdust after rds',
1360     &           'kg/kg ',3,qdustrds1)
1361         call writediagfi(ngrid,'qstormrds1','qstorm after rds',
1362     &           'kg/kg ',3,qstormrds1)
1363         
1364         call writediagfi(ngrid,'qdusttotal0','q sum before rds',
1365     &           'kg/m2 ',2,qdusttotal0)
1366         call writediagfi(ngrid,'qdusttotal1','q sum after rds',
1367     &           'kg/m2 ',2,qdusttotal1)
1368
1369      ENDIF ! end of if(rdstorm)
1370
1371c     3.2 Dust entrained from the PBL up to the top of sub-grid scale topography
1372c    -------------------------------------------
1373      IF (topflows) THEN
1374         clearatm=.true. ! stormdust is not accounted in the extra heating on top of the mountains
1375         nohmons=.false.
1376         pdqtop(:,:,:)=0.
1377         CALL topmons(ngrid,nlayer,nq,ptime,ptimestep,
1378     &                pq,pdq,pt,pdt,zplev,zplay,zzlev,
1379     &                zzlay,zdtsw,zdtlw,
1380     &                icount,zday,zls,tsurf(:,iflat),
1381     &                qsurf_meshavg(:,igcm_co2),
1382     &                igout,aerosol,tauscaling,dust_rad_adjust,
1383     &                IRtoVIScoef,totstormfract,clearatm,
1384     &                clearsky,totcloudfrac,
1385     &                nohmons,
1386     &                pdqtop,wtop,dsodust,dsords,dsotop,
1387     &                tau_pref_scenario,tau_pref_gcm)
1388                   
1389c      update the tendencies of both dust after vertical transport
1390         DO l=1,nlayer
1391          DO ig=1,ngrid
1392           pdq(ig,l,igcm_topdust_mass)=
1393     & pdq(ig,l,igcm_topdust_mass)+
1394     &                      pdqtop(ig,l,igcm_topdust_mass)
1395           pdq(ig,l,igcm_topdust_number)=
1396     & pdq(ig,l,igcm_topdust_number)+
1397     &                      pdqtop(ig,l,igcm_topdust_number)
1398           pdq(ig,l,igcm_dust_mass)=
1399     & pdq(ig,l,igcm_dust_mass)+ pdqtop(ig,l,igcm_dust_mass)
1400           pdq(ig,l,igcm_dust_number)=
1401     & pdq(ig,l,igcm_dust_number)+pdqtop(ig,l,igcm_dust_number)
1402
1403          ENDDO
1404         ENDDO
1405
1406      ENDIF ! end of if (topflows)
1407
1408c     3.3 Dust injection from the surface
1409c    -------------------------------------------
1410           if (dustinjection.gt.0) then
1411
1412             CALL compute_dtau(ngrid,nlayer,
1413     &                          zday,pplev,tau_pref_gcm,
1414     &                          ptimestep,local_time,IRtoVIScoef,
1415     &                          dustliftday)
1416           endif ! end of if (dustinjection.gt.0)
1417
1418
1419c-----------------------------------------------------------------------
1420c    4. Gravity wave and subgrid scale topography drag :
1421c    -------------------------------------------------
1422
1423
1424      IF(calllott)THEN
1425        CALL calldrag_noro(ngrid,nlayer,ptimestep,
1426     &                 zplay,zplev,pt,pu,pv,zdtgw,zdugw,zdvgw)
1427
1428        DO l=1,nlayer
1429          DO ig=1,ngrid
1430            pdv(ig,l)=pdv(ig,l)+zdvgw(ig,l)
1431            pdu(ig,l)=pdu(ig,l)+zdugw(ig,l)
1432            pdt(ig,l)=pdt(ig,l)+zdtgw(ig,l)
1433          ENDDO
1434        ENDDO
1435      ENDIF
1436
1437c-----------------------------------------------------------------------
1438c    5. Vertical diffusion (turbulent mixing):
1439c    -----------------------------------------
1440
1441      IF (calldifv) THEN
1442         DO ig=1,ngrid
1443           DO islope = 1,nslope
1444            zflubid(ig,islope)=fluxrad(ig,islope)
1445     &                        +fluxgrd(ig,islope)
1446           ENDDO
1447         ENDDO
1448         zdum1(:,:)=0
1449         zdum2(:,:)=0
1450         do l=1,nlayer
1451            do ig=1,ngrid
1452               zdh(ig,l)=pdt(ig,l)/zpopsk(ig,l)
1453            enddo
1454         enddo
1455
1456c ----------------------
1457c Treatment of a special case : using new surface layer (Richardson based)
1458c without using the thermals in gcm and mesoscale can yield problems in
1459c weakly unstable situations when winds are near to 0. For those cases, we add
1460c a unit subgrid gustiness. Remember that thermals should be used we using the
1461c Richardson based surface layer model.
1462        IF ( .not.calltherm
1463     .       .and. callrichsl
1464     .       .and. .not.turb_resolved) THEN
1465
1466          DO ig=1, ngrid
1467             IF (zh(ig,1) .lt. tsurf_meshavg(ig)) THEN
1468               wstar(ig)=1.
1469               hfmax_th(ig)=0.2
1470             ELSE
1471               wstar(ig)=0.
1472               hfmax_th(ig)=0.
1473             ENDIF     
1474          ENDDO
1475        ENDIF
1476c ----------------------
1477
1478         IF (tke_heat_flux .ne. 0.) THEN
1479
1480             zz1(:)=(pt(:,1)+pdt(:,1)*ptimestep)*(r/g)*
1481     &                 (-alog(zplay(:,1)/zplev(:,1)))
1482             pdt(:,1)=pdt(:,1) + (tke_heat_flux/zz1(:))*zpopsk(:,1)
1483         ENDIF
1484
1485c        Calling vdif (Martian version WITH CO2 condensation)
1486         dwatercap_dif(:,:) = 0.
1487         zcdh(:) = 0.
1488         zcdv(:) = 0.
1489
1490         CALL vdifc(ngrid,nlayer,nq,zpopsk,
1491     $        ptimestep,capcal,lwrite,
1492     $        zplay,zplev,zzlay,zzlev,z0,
1493     $        pu,pv,zh,pq,tsurf,emis,qsurf,
1494     $        zdum1,zdum2,zdh,pdq,zflubid,
1495     $        zdudif,zdvdif,zdhdif,zdtsdif,q2,
1496     &        zdqdif,zdqsdif,wstar,zcdv,zcdh,hfmax_th,
1497     &        zcondicea_co2microp,sensibFlux,
1498     &        dustliftday,local_time,watercap,dwatercap_dif)
1499
1500          DO ig=1,ngrid
1501             zdtsurf(ig,:)=zdtsurf(ig,:)+zdtsdif(ig,:)
1502             dwatercap(ig,:)=dwatercap(ig,:)+dwatercap_dif(ig,:)
1503          ENDDO
1504          call compute_meshgridavg(ngrid,nq,albedo,emis,tsurf,zdqsdif,
1505     &   albedo_meshavg,emis_meshavg,tsurf_meshavg,zdqsdif_meshavg_tmp)
1506         IF (.not.turb_resolved) THEN
1507          DO l=1,nlayer
1508            DO ig=1,ngrid
1509               pdv(ig,l)=pdv(ig,l)+zdvdif(ig,l)
1510               pdu(ig,l)=pdu(ig,l)+zdudif(ig,l)
1511               pdt(ig,l)=pdt(ig,l)+zdhdif(ig,l)*zpopsk(ig,l)
1512
1513               zdtdif(ig,l)=zdhdif(ig,l)*zpopsk(ig,l) ! for diagnostic only
1514            ENDDO
1515          ENDDO
1516
1517           DO iq=1, nq
1518            DO l=1,nlayer
1519              DO ig=1,ngrid
1520                 pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqdif(ig,l,iq)
1521              ENDDO
1522            ENDDO
1523           ENDDO
1524           DO iq=1, nq
1525              DO ig=1,ngrid
1526                 dqsurf(ig,iq,:)=dqsurf(ig,iq,:) + zdqsdif(ig,iq,:)
1527              ENDDO
1528           ENDDO
1529
1530         ELSE
1531           write (*,*) '******************************************'
1532           write (*,*) '** LES mode: the difv part is only used to'
1533           write (*,*) '**  - provide HFX and UST to the dynamics'
1534           write (*,*) '**  - update TSURF'
1535           write (*,*) '******************************************'
1536           !! Specific treatment for lifting in turbulent-resolving mode (AC)
1537           IF (lifting .and. doubleq) THEN
1538             !! lifted dust is injected in the first layer.
1539             !! Sedimentation must be called after turbulent mixing, i.e. on next step, after WRF.
1540             !! => lifted dust is not incremented before the sedimentation step.
1541             zdqdif(1:ngrid,1,1:nq)=0.
1542             zdqdif(1:ngrid,1,igcm_dust_number) =
1543     .        -zdqsdif_meshavg_tmp(1:ngrid,igcm_dust_number)
1544             zdqdif(1:ngrid,1,igcm_dust_mass) =
1545     .        -zdqsdif_meshavg_tmp(1:ngrid,igcm_dust_mass)
1546             zdqdif(1:ngrid,2:nlayer,1:nq) = 0.
1547             DO iq=1, nq
1548               IF ((iq .ne. igcm_dust_mass)
1549     &          .and. (iq .ne. igcm_dust_number)) THEN
1550                 zdqsdif(:,iq,:)=0.
1551               ENDIF
1552             ENDDO
1553           ELSE
1554             zdqdif(1:ngrid,1:nlayer,1:nq) = 0.
1555             zdqsdif(1:ngrid,1:nq,1:nslope) = 0.
1556           ENDIF
1557         ENDIF
1558      ELSE   
1559         DO ig=1,ngrid
1560           DO islope=1,nslope
1561             zdtsurf(ig,islope)=zdtsurf(ig,islope)+
1562     s   (fluxrad(ig,islope)+fluxgrd(ig,islope))/capcal(ig,islope)
1563           ENDDO
1564         ENDDO
1565         IF (turb_resolved) THEN
1566            write(*,*) 'Turbulent-resolving mode !'
1567            write(*,*) 'Please set calldifv to T in callphys.def'
1568            call abort_physic("physiq","turbulent-resolving mode",1)
1569         ENDIF
1570      ENDIF ! of IF (calldifv)
1571
1572c-----------------------------------------------------------------------
1573c   6. Thermals :
1574c   -----------------------------
1575
1576      if(calltherm .and. .not.turb_resolved) then
1577
1578        call calltherm_interface(ngrid,nlayer,nq,igcm_co2,
1579     $ zzlev,zzlay,
1580     $ ptimestep,pu,pv,pt,pq,pdu,pdv,pdt,pdq,q2,
1581     $ zplay,zplev,pphi,zpopsk,
1582     $ pdu_th,pdv_th,pdt_th,pdq_th,lmax_th,zmax_th,
1583     $ dtke_th,zdhdif,hfmax_th,wstar,sensibFlux)
1584
1585         DO l=1,nlayer
1586           DO ig=1,ngrid
1587              pdu(ig,l)=pdu(ig,l)+pdu_th(ig,l)
1588              pdv(ig,l)=pdv(ig,l)+pdv_th(ig,l)
1589              pdt(ig,l)=pdt(ig,l)+pdt_th(ig,l)
1590              q2(ig,l)=q2(ig,l)+dtke_th(ig,l)*ptimestep
1591           ENDDO
1592        ENDDO
1593
1594        DO ig=1,ngrid
1595          q2(ig,nlayer+1)=q2(ig,nlayer+1)+dtke_th(ig,nlayer+1)*ptimestep
1596        ENDDO
1597
1598        DO iq=1,nq
1599         DO l=1,nlayer
1600           DO ig=1,ngrid
1601             pdq(ig,l,iq)=pdq(ig,l,iq)+pdq_th(ig,l,iq)
1602           ENDDO
1603         ENDDO
1604        ENDDO
1605
1606        lmax_th_out(:)=real(lmax_th(:))
1607
1608      else   !of if calltherm
1609        lmax_th(:)=0
1610        wstar(:)=0.
1611        hfmax_th(:)=0.
1612        lmax_th_out(:)=0.
1613      end if
1614
1615c-----------------------------------------------------------------------
1616c   7. Dry convective adjustment:
1617c   -----------------------------
1618
1619      IF(calladj) THEN
1620
1621         DO l=1,nlayer
1622            DO ig=1,ngrid
1623               zdh(ig,l)=pdt(ig,l)/zpopsk(ig,l)
1624            ENDDO
1625         ENDDO
1626         zduadj(:,:)=0
1627         zdvadj(:,:)=0
1628         zdhadj(:,:)=0
1629
1630         CALL convadj(ngrid,nlayer,nq,ptimestep,
1631     $                zplay,zplev,zpopsk,lmax_th,
1632     $                pu,pv,zh,pq,
1633     $                pdu,pdv,zdh,pdq,
1634     $                zduadj,zdvadj,zdhadj,
1635     $                zdqadj)
1636
1637         DO l=1,nlayer
1638            DO ig=1,ngrid
1639               pdu(ig,l)=pdu(ig,l)+zduadj(ig,l)
1640               pdv(ig,l)=pdv(ig,l)+zdvadj(ig,l)
1641               pdt(ig,l)=pdt(ig,l)+zdhadj(ig,l)*zpopsk(ig,l)
1642
1643               zdtadj(ig,l)=zdhadj(ig,l)*zpopsk(ig,l) ! for diagnostic only
1644            ENDDO
1645         ENDDO
1646
1647           DO iq=1, nq
1648            DO l=1,nlayer
1649              DO ig=1,ngrid
1650                 pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqadj(ig,l,iq)
1651              ENDDO
1652            ENDDO
1653           ENDDO
1654      ENDIF ! of IF(calladj)
1655
1656c-----------------------------------------------------
1657c    8. Non orographic Gravity waves :
1658c -------------------------------------------------
1659
1660      IF (calllott_nonoro) THEN
1661
1662         CALL nonoro_gwd_ran(ngrid,nlayer,ptimestep,
1663     &               cpnew,rnew,
1664     &               zplay,
1665     &               zmax_th,                      ! max altitude reached by thermals (m)
1666     &               pt, pu, pv,
1667     &               pdt, pdu, pdv,
1668     &               zustrhi,zvstrhi,
1669     &               d_t_hin, d_u_hin, d_v_hin)
1670
1671!  Update tendencies
1672         pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)
1673     &                         +d_t_hin(1:ngrid,1:nlayer)
1674         pdu(1:ngrid,1:nlayer)=pdu(1:ngrid,1:nlayer)
1675     &                         +d_u_hin(1:ngrid,1:nlayer)
1676         pdv(1:ngrid,1:nlayer)=pdv(1:ngrid,1:nlayer)
1677     &                         +d_v_hin(1:ngrid,1:nlayer)
1678
1679      ENDIF ! of IF (calllott_nonoro)
1680
1681c-----------------------------------------------------------------------
1682c   9. Specific parameterizations for tracers
1683c:   -----------------------------------------
1684
1685
1686c   9a. Water and ice
1687c     ---------------
1688
1689c        ---------------------------------------
1690c        Water ice condensation in the atmosphere
1691c        ----------------------------------------
1692         IF (water) THEN
1693
1694           call watercloud(ngrid,nlayer,ptimestep,
1695     &                zplev,zplay,pdpsrf,zzlay, pt,pdt,
1696     &                pq,pdq,zdqcloud,zdtcloud,
1697     &                nq,tau,tauscaling,rdust,rice,nuice,
1698     &                rsedcloud,rhocloud,totcloudfrac)
1699c Temperature variation due to latent heat release
1700           if (activice) then
1701               pdt(1:ngrid,1:nlayer) =
1702     &          pdt(1:ngrid,1:nlayer) +
1703     &          zdtcloud(1:ngrid,1:nlayer)
1704           endif
1705
1706! increment water vapour and ice atmospheric tracers tendencies
1707           pdq(1:ngrid,1:nlayer,igcm_h2o_vap) =
1708     &       pdq(1:ngrid,1:nlayer,igcm_h2o_vap) +
1709     &       zdqcloud(1:ngrid,1:nlayer,igcm_h2o_vap)
1710           pdq(1:ngrid,1:nlayer,igcm_h2o_ice) =
1711     &       pdq(1:ngrid,1:nlayer,igcm_h2o_ice) +
1712     &       zdqcloud(1:ngrid,1:nlayer,igcm_h2o_ice)
1713
1714           if (hdo) then
1715! increment HDO vapour and ice atmospheric tracers tendencies
1716           pdq(1:ngrid,1:nlayer,igcm_hdo_vap) =
1717     &       pdq(1:ngrid,1:nlayer,igcm_hdo_vap) +
1718     &       zdqcloud(1:ngrid,1:nlayer,igcm_hdo_vap)
1719           pdq(1:ngrid,1:nlayer,igcm_hdo_ice) =
1720     &       pdq(1:ngrid,1:nlayer,igcm_hdo_ice) +
1721     &       zdqcloud(1:ngrid,1:nlayer,igcm_hdo_ice)
1722           endif !hdo
1723
1724! increment dust and ccn masses and numbers
1725! We need to check that we have Nccn & Ndust > 0
1726! This is due to single precision rounding problems
1727           if (microphys) then
1728             pdq(1:ngrid,1:nlayer,igcm_ccn_mass) =
1729     &         pdq(1:ngrid,1:nlayer,igcm_ccn_mass) +
1730     &         zdqcloud(1:ngrid,1:nlayer,igcm_ccn_mass)
1731             pdq(1:ngrid,1:nlayer,igcm_ccn_number) =
1732     &         pdq(1:ngrid,1:nlayer,igcm_ccn_number) +
1733     &         zdqcloud(1:ngrid,1:nlayer,igcm_ccn_number)
1734             where (pq(:,:,igcm_ccn_mass) +
1735     &       ptimestep*pdq(:,:,igcm_ccn_mass) < 0.)
1736               pdq(:,:,igcm_ccn_mass) =
1737     &         - pq(:,:,igcm_ccn_mass)/ptimestep + 1.e-30
1738               pdq(:,:,igcm_ccn_number) =
1739     &         - pq(:,:,igcm_ccn_number)/ptimestep + 1.e-30
1740             end where
1741             where (pq(:,:,igcm_ccn_number) +
1742     &       ptimestep*pdq(:,:,igcm_ccn_number) < 0.)
1743               pdq(:,:,igcm_ccn_mass) =
1744     &         - pq(:,:,igcm_ccn_mass)/ptimestep + 1.e-30
1745               pdq(:,:,igcm_ccn_number) =
1746     &         - pq(:,:,igcm_ccn_number)/ptimestep + 1.e-30
1747             end where
1748           endif
1749
1750           if (scavenging) then
1751             pdq(1:ngrid,1:nlayer,igcm_dust_mass) =
1752     &         pdq(1:ngrid,1:nlayer,igcm_dust_mass) +
1753     &         zdqcloud(1:ngrid,1:nlayer,igcm_dust_mass)
1754             pdq(1:ngrid,1:nlayer,igcm_dust_number) =
1755     &         pdq(1:ngrid,1:nlayer,igcm_dust_number) +
1756     &         zdqcloud(1:ngrid,1:nlayer,igcm_dust_number)
1757             where (pq(:,:,igcm_dust_mass) +
1758     &       ptimestep*pdq(:,:,igcm_dust_mass) < 0.)
1759               pdq(:,:,igcm_dust_mass) =
1760     &         - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
1761               pdq(:,:,igcm_dust_number) =
1762     &         - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
1763             end where
1764             where (pq(:,:,igcm_dust_number) +
1765     &       ptimestep*pdq(:,:,igcm_dust_number) < 0.)
1766               pdq(:,:,igcm_dust_mass) =
1767     &         - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
1768               pdq(:,:,igcm_dust_number) =
1769     &         - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
1770             end where
1771           endif ! of if scavenging
1772                     
1773         END IF  ! of IF (water)
1774
1775c   9a bis. CO2 clouds (CL & JA)
1776c        ---------------------------------------
1777c        CO2 ice cloud condensation in the atmosphere
1778c        ----------------------------------------
1779c flag needed in callphys.def:
1780c               co2clouds=.true. is mandatory (default is .false.)
1781c               co2useh2o=.true. if you want to allow co2 condensation
1782c                                on water ice particles
1783c               meteo_flux=.true. if you want to add a meteoritic
1784c                                 supply of CCN
1785c               CLFvaryingCO2=.true. if you want to have a sub-grid
1786c                                    temperature distribution
1787c               spantCO2=integer (i.e. 3) amplitude of the sub-grid T disti
1788c               nuiceco2_sed=0.2 variance of the size distribution for the
1789c                                sedimentation
1790c               nuiceco2_ref=0.2 variance of the size distribution for the
1791c                                nucleation
1792c               imicroco2=50     micro-timestep is 1/50 of physical timestep
1793        zdqssed_co2(:) = 0.
1794        zdqssed_ccn(:,:) = 0.
1795
1796         IF (co2clouds) THEN
1797           call co2cloud(ngrid,nlayer,ptimestep,
1798     &           zplev,zplay,pdpsrf,zzlay,pt,pdt,
1799     &           pq,pdq,zdqcloudco2,zdtcloudco2,
1800     &           nq,tau,tauscaling,rdust,rice,riceco2,nuice,
1801     &           rhocloud, rsedcloudco2,rhocloudco2,zzlev,zdqssed_co2,
1802     &           zdqssed_ccn,pdu,pu,zcondicea_co2microp)
1803
1804          DO iq=1, nq
1805            DO ig=1,ngrid
1806             DO islope = 1,nslope
1807              dqsurf(ig,iq,islope)=dqsurf(ig,iq,islope)+
1808     &         zdqssed_ccn(ig,iq)*cos(pi*def_slope_mean(islope)/180.)
1809             ENDDO !(islope)
1810            ENDDO  ! (ig)
1811           ENDDO    ! (iq)q)
1812c Temperature variation due to latent heat release
1813               pdt(1:ngrid,1:nlayer) =
1814     &              pdt(1:ngrid,1:nlayer) +
1815     &              zdtcloudco2(1:ngrid,1:nlayer)
1816
1817! increment dust and ccn masses and numbers
1818! We need to check that we have Nccn & Ndust > 0
1819! This is due to single precision rounding problems
1820! increment dust tracers tendancies
1821               pdq(:,:,igcm_dust_mass) = pdq(:,:,igcm_dust_mass)
1822     &                                 + zdqcloudco2(:,:,igcm_dust_mass)
1823
1824               pdq(:,:,igcm_dust_number) = pdq(:,:,igcm_dust_number)
1825     &                               + zdqcloudco2(:,:,igcm_dust_number)
1826
1827               pdq(:,:,igcm_co2) = pdq(:,:,igcm_co2)
1828     &                             + zdqcloudco2(:,:,igcm_co2)
1829
1830               pdq(:,:,igcm_co2_ice) = pdq(:,:,igcm_co2_ice)
1831     &                                 + zdqcloudco2(:,:,igcm_co2_ice)
1832
1833               pdq(:,:,igcm_ccnco2_mass) = pdq(:,:,igcm_ccnco2_mass)
1834     &                               + zdqcloudco2(:,:,igcm_ccnco2_mass)
1835
1836               pdq(:,:,igcm_ccnco2_number) = pdq(:,:,igcm_ccnco2_number)
1837     &                             + zdqcloudco2(:,:,igcm_ccnco2_number)
1838
1839             if (meteo_flux) then
1840               pdq(:,:,igcm_ccnco2_meteor_mass) =
1841     &                 pdq(:,:,igcm_ccnco2_meteor_mass) +
1842     &                 zdqcloudco2(:,:,igcm_ccnco2_meteor_mass)
1843
1844               pdq(:,:,igcm_ccnco2_meteor_number) =
1845     &                 pdq(:,:,igcm_ccnco2_meteor_number)
1846     &                 + zdqcloudco2(:,:,igcm_ccnco2_meteor_number)
1847             end if
1848!Update water ice clouds values as well
1849             if (co2useh2o) then
1850                pdq(1:ngrid,1:nlayer,igcm_h2o_ice) =
1851     &               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) +
1852     &               zdqcloudco2(1:ngrid,1:nlayer,igcm_h2o_ice)
1853                pdq(1:ngrid,1:nlayer,igcm_ccn_mass) =
1854     &               pdq(1:ngrid,1:nlayer,igcm_ccn_mass) +
1855     &               zdqcloudco2(1:ngrid,1:nlayer,igcm_ccn_mass)
1856                pdq(1:ngrid,1:nlayer,igcm_ccn_number) =
1857     &               pdq(1:ngrid,1:nlayer,igcm_ccn_number) +
1858     &               zdqcloudco2(1:ngrid,1:nlayer,igcm_ccn_number)
1859
1860               pdq(:,:,igcm_ccnco2_h2o_mass_ice) =
1861     &                pdq(:,:,igcm_ccnco2_h2o_mass_ice) +
1862     &                zdqcloudco2(:,:,igcm_ccnco2_h2o_mass_ice)
1863
1864               pdq(:,:,igcm_ccnco2_h2o_mass_ccn) =
1865     &                pdq(:,:,igcm_ccnco2_h2o_mass_ccn) +
1866     &                zdqcloudco2(:,:,igcm_ccnco2_h2o_mass_ccn)
1867
1868               pdq(:,:,igcm_ccnco2_h2o_number) =
1869     &                   pdq(:,:,igcm_ccnco2_h2o_number) +
1870     &                   zdqcloudco2(:,:,igcm_ccnco2_h2o_number)
1871
1872c     Negative values?
1873                where (pq(:,:,igcm_ccn_mass) +
1874     &                 ptimestep*pdq(:,:,igcm_ccn_mass) < 0.)
1875                  pdq(:,:,igcm_ccn_mass) =
1876     &               - pq(:,:,igcm_ccn_mass)/ptimestep + 1.e-30
1877                  pdq(:,:,igcm_ccn_number) =
1878     &               - pq(:,:,igcm_ccn_number)/ptimestep + 1.e-30
1879                end where
1880c     Negative values?
1881                where (pq(:,:,igcm_ccn_number) +
1882     &                 ptimestep*pdq(:,:,igcm_ccn_number) < 0.)
1883                  pdq(:,:,igcm_ccn_mass) =
1884     &              - pq(:,:,igcm_ccn_mass)/ptimestep + 1.e-30
1885                  pdq(:,:,igcm_ccn_number) =
1886     &              - pq(:,:,igcm_ccn_number)/ptimestep + 1.e-30
1887                end where
1888             where (pq(:,:,igcm_ccnco2_h2o_mass_ice) +
1889     &              pq(:,:,igcm_ccnco2_h2o_mass_ccn) +
1890     &              (pdq(:,:,igcm_ccnco2_h2o_mass_ice) +
1891     &               pdq(:,:,igcm_ccnco2_h2o_mass_ccn)
1892     &               )*ptimestep < 0.)
1893               pdq(:,:,igcm_ccnco2_h2o_mass_ice) =
1894     &            - pq(:,:,igcm_ccnco2_h2o_mass_ice)
1895     &               /ptimestep + 1.e-30
1896               pdq(:,:,igcm_ccnco2_h2o_mass_ccn) =
1897     &            - pq(:,:,igcm_ccnco2_h2o_mass_ccn)
1898     &               /ptimestep + 1.e-30
1899               pdq(:,:,igcm_ccnco2_h2o_number) =
1900     &            - pq(:,:,igcm_ccnco2_h2o_number)
1901     &                /ptimestep + 1.e-30
1902             end where
1903
1904             where (pq(:,:,igcm_ccnco2_h2o_number) +
1905     &              (pdq(:,:,igcm_ccnco2_h2o_number)
1906     &               )*ptimestep < 0.)
1907               pdq(:,:,igcm_ccnco2_h2o_mass_ice) =
1908     &            - pq(:,:,igcm_ccnco2_h2o_mass_ice)
1909     &               /ptimestep + 1.e-30
1910               pdq(:,:,igcm_ccnco2_h2o_mass_ccn) =
1911     &            - pq(:,:,igcm_ccnco2_h2o_mass_ccn)
1912     &               /ptimestep + 1.e-30
1913               pdq(:,:,igcm_ccnco2_h2o_number) =
1914     &            - pq(:,:,igcm_ccnco2_h2o_number)
1915     &                /ptimestep + 1.e-30
1916             end where
1917             endif ! of if (co2useh2o)
1918c     Negative values?
1919             where (pq(:,:,igcm_ccnco2_mass) +
1920     &              ptimestep*pdq(:,:,igcm_ccnco2_mass) < 0.)
1921               pdq(:,:,igcm_ccnco2_mass) =
1922     &            - pq(:,:,igcm_ccnco2_mass)/ptimestep + 1.e-30
1923               pdq(:,:,igcm_ccnco2_number) =
1924     &            - pq(:,:,igcm_ccnco2_number)/ptimestep + 1.e-30
1925             end where
1926             where (pq(:,:,igcm_ccnco2_number) +
1927     &              ptimestep*pdq(:,:,igcm_ccnco2_number) < 0.)
1928               pdq(:,:,igcm_ccnco2_mass) =
1929     &            - pq(:,:,igcm_ccnco2_mass)/ptimestep + 1.e-30
1930               pdq(:,:,igcm_ccnco2_number) =
1931     &            - pq(:,:,igcm_ccnco2_number)/ptimestep + 1.e-30
1932             end where
1933       
1934c     Negative values?
1935             where (pq(:,:,igcm_dust_mass) +
1936     &              ptimestep*pdq(:,:,igcm_dust_mass) < 0.)
1937               pdq(:,:,igcm_dust_mass) =
1938     &           - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
1939               pdq(:,:,igcm_dust_number) =
1940     &           - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
1941             end where
1942             where (pq(:,:,igcm_dust_number) +
1943     &              ptimestep*pdq(:,:,igcm_dust_number) < 0.)
1944               pdq(:,:,igcm_dust_mass) =
1945     &           - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
1946               pdq(:,:,igcm_dust_number) =
1947     &           - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
1948             end where
1949             if (meteo_flux) then
1950             where (pq(:,:,igcm_ccnco2_meteor_mass) +
1951     &              ptimestep*pdq(:,:,igcm_ccnco2_meteor_mass) < 0.)
1952               pdq(:,:,igcm_ccnco2_meteor_mass) =
1953     &            - pq(:,:,igcm_ccnco2_meteor_mass)/ptimestep + 1.e-30
1954               pdq(:,:,igcm_ccnco2_meteor_number) =
1955     &            - pq(:,:,igcm_ccnco2_meteor_number)/ptimestep + 1.e-30
1956             end where
1957             where (pq(:,:,igcm_ccnco2_meteor_number) +
1958     &              ptimestep*pdq(:,:,igcm_ccnco2_meteor_number) < 0.)
1959               pdq(:,:,igcm_ccnco2_meteor_mass) =
1960     &            - pq(:,:,igcm_ccnco2_meteor_mass)/ptimestep + 1.e-30
1961               pdq(:,:,igcm_ccnco2_meteor_number) =
1962     &            - pq(:,:,igcm_ccnco2_meteor_number)/ptimestep + 1.e-30
1963             end where
1964             end if
1965      END IF ! of IF (co2clouds)
1966
1967c   9b. Aerosol particles
1968c     -------------------
1969c        ----------
1970c        Dust devil :
1971c        ----------
1972         IF(callddevil) then
1973           call dustdevil(ngrid,nlayer,nq, zplev,pu,pv,pt, tsurf,q2,
1974     &                zdqdev,zdqsdev)
1975
1976           if (dustbin.ge.1) then
1977              do iq=1,nq
1978                 DO l=1,nlayer
1979                    DO ig=1,ngrid
1980                       pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqdev(ig,l,iq)
1981                    ENDDO
1982                 ENDDO
1983              enddo
1984              do iq=1,nq
1985                 DO ig=1,ngrid
1986                   DO islope = 1,nslope
1987                    dqsurf(ig,iq,islope)= dqsurf(ig,iq,islope) +
1988     &          zdqsdev(ig,iq)*cos(pi*def_slope_mean(islope)/180.)
1989                   ENDDO
1990                 ENDDO
1991              enddo
1992           endif  ! of if (dustbin.ge.1)
1993
1994         END IF ! of IF (callddevil)
1995
1996c        -------------
1997c        Sedimentation :   acts also on water ice
1998c        -------------
1999         IF (sedimentation) THEN
2000           zdqsed(1:ngrid,1:nlayer,1:nq)=0
2001           zdqssed(1:ngrid,1:nq)=0
2002
2003c Sedimentation for co2 clouds tracers are inside co2cloud microtimestep
2004c Zdqssed isn't
2005           call callsedim(ngrid,nlayer,ptimestep,
2006     &                zplev,zzlev,zzlay,pt,pdt,
2007     &                rdust,rstormdust,rtopdust,
2008     &                rice,rsedcloud,rhocloud,
2009     &                pq,pdq,zdqsed,zdqssed,nq,
2010     &                tau,tauscaling)
2011
2012c Flux at the surface of co2 ice computed in co2cloud microtimestep
2013           IF (rdstorm) THEN
2014c             Storm dust cannot sediment to the surface
2015              DO ig=1,ngrid 
2016                 zdqsed(ig,1,igcm_stormdust_mass)=
2017     &             zdqsed(ig,1,igcm_stormdust_mass)+
2018     &             zdqssed(ig,igcm_stormdust_mass) /
2019     &             ((pplev(ig,1)-pplev(ig,2))/g)
2020                 zdqsed(ig,1,igcm_stormdust_number)=
2021     &             zdqsed(ig,1,igcm_stormdust_number)+
2022     &             zdqssed(ig,igcm_stormdust_number) /
2023     &               ((pplev(ig,1)-pplev(ig,2))/g)
2024                 zdqssed(ig,igcm_stormdust_mass)=0.
2025                 zdqssed(ig,igcm_stormdust_number)=0.
2026              ENDDO
2027           ENDIF !rdstorm
2028
2029           DO iq=1, nq
2030             DO l=1,nlayer
2031               DO ig=1,ngrid
2032                    pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqsed(ig,l,iq)
2033               ENDDO
2034             ENDDO
2035           ENDDO
2036           DO iq=1, nq
2037             DO ig=1,ngrid
2038               DO islope = 1,nslope
2039                    dqsurf(ig,iq,islope)= dqsurf(ig,iq,islope) +
2040     &          zdqssed(ig,iq)*cos(pi*def_slope_mean(islope)/180.)
2041               ENDDO
2042             ENDDO
2043           ENDDO
2044
2045         END IF   ! of IF (sedimentation)
2046
2047c Add lifted dust to tendancies after sedimentation in the LES (AC)
2048      IF (turb_resolved) THEN
2049           DO iq=1, nq
2050            DO l=1,nlayer
2051              DO ig=1,ngrid
2052                 pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqdif(ig,l,iq)
2053              ENDDO
2054            ENDDO
2055           ENDDO
2056           DO iq=1, nq
2057              DO ig=1,ngrid
2058                 dqsurf(ig,iq,:)=dqsurf(ig,iq,:) + zdqsdif(ig,iq,:)
2059              ENDDO
2060           ENDDO
2061      ENDIF
2062c
2063c   9c. Chemical species
2064c     ------------------
2065
2066#ifndef MESOSCALE
2067c        --------------
2068c        photochemistry :
2069c        --------------
2070         IF (photochem) then
2071
2072           if (modulo(icount-1,ichemistry).eq.0) then
2073           ! compute chemistry every ichemistry physics step
2074
2075!           dust and ice surface area
2076            call surfacearea(ngrid, nlayer, naerkind,
2077     $                       ptimestep, zplay, zzlay,
2078     $                       pt, pq, pdq, nq,
2079     $                       rdust, rice, tau, tauscaling,
2080     $                       surfdust, surfice)
2081!           call photochemistry
2082            DO ig = 1,ngrid
2083              qsurf_tmp(ig,:) = qsurf(ig,:,major_slope(ig))
2084            ENDDO
2085            call calchim(ngrid,nlayer,nq,
2086     &                   ptimestep,zplay,zplev,pt,pdt,dist_sol,mu0,
2087     $                   zzlev,zzlay,zday,pq,pdq,zdqchim,zdqschim,
2088     $                   zdqcloud,zdqscloud,tau(:,1),
2089     $                   qsurf_tmp(:,igcm_co2),
2090     $                   pu,pdu,pv,pdv,surfdust,surfice)
2091
2092            endif ! of if (modulo(icount-1,ichemistry).eq.0)
2093
2094           ! increment values of tracers:
2095           DO iq=1,nq ! loop on all tracers; tendencies for non-chemistry
2096                      ! tracers is zero anyways
2097             DO l=1,nlayer
2098               DO ig=1,ngrid
2099                 pdq(ig,l,iq)=pdq(ig,l,iq)+zdqchim(ig,l,iq)
2100               ENDDO
2101             ENDDO
2102           ENDDO ! of DO iq=1,nq
2103           
2104           ! add condensation tendency for H2O2
2105           if (igcm_h2o2.ne.0) then
2106             DO l=1,nlayer
2107               DO ig=1,ngrid
2108                 pdq(ig,l,igcm_h2o2)=pdq(ig,l,igcm_h2o2)
2109     &                                +zdqcloud(ig,l,igcm_h2o2)
2110               ENDDO
2111             ENDDO
2112           endif
2113
2114           ! increment surface values of tracers:
2115           DO iq=1,nq ! loop on all tracers; tendencies for non-chemistry
2116                      ! tracers is zero anyways
2117             DO ig=1,ngrid
2118              DO islope = 1,nslope
2119               dqsurf(ig,iq,islope)=dqsurf(ig,iq,islope) +
2120     &           zdqschim(ig,iq)*cos(pi*def_slope_mean(islope)/180.)
2121              ENDDO
2122             ENDDO
2123           ENDDO ! of DO iq=1,nq
2124
2125           ! add condensation tendency for H2O2
2126           if (igcm_h2o2.ne.0) then
2127             DO ig=1,ngrid
2128              DO islope = 1,nslope
2129              dqsurf(ig,igcm_h2o2,islope)=dqsurf(ig,igcm_h2o2,islope)+
2130     &      zdqscloud(ig,igcm_h2o2)*cos(pi*def_slope_mean(islope)/180.)
2131              ENDDO
2132             ENDDO
2133           endif
2134
2135         END IF  ! of IF (photochem)
2136#endif
2137
2138
2139#ifndef MESOSCALE
2140c-----------------------------------------------------------------------
2141c   10. THERMOSPHERE CALCULATION
2142c-----------------------------------------------------------------------
2143
2144      if (callthermos) then
2145        call thermosphere(ngrid,nlayer,nq,zplev,zplay,dist_sol,
2146     $     mu0,ptimestep,ptime,zday,tsurf_meshavg,zzlev,zzlay,
2147     &     pt,pq,pu,pv,pdt,pdq,
2148     $     zdteuv,zdtconduc,zdumolvis,zdvmolvis,zdqmoldiff,
2149     $     PhiEscH,PhiEscH2,PhiEscD)
2150
2151        DO l=1,nlayer
2152          DO ig=1,ngrid
2153            dtrad(ig,l)=dtrad(ig,l)+zdteuv(ig,l)
2154            pdt(ig,l)=pdt(ig,l)+zdtconduc(ig,l)+zdteuv(ig,l)
2155            pdv(ig,l)=pdv(ig,l)+zdvmolvis(ig,l)
2156            pdu(ig,l)=pdu(ig,l)+zdumolvis(ig,l)
2157            DO iq=1, nq
2158              pdq(ig,l,iq)=pdq(ig,l,iq)+zdqmoldiff(ig,l,iq)
2159            ENDDO
2160          ENDDO
2161        ENDDO
2162
2163      endif ! of if (callthermos)
2164#endif
2165c-----------------------------------------------------------------------
2166c   11. Carbon dioxide condensation-sublimation:
2167c     (should be the last atmospherical physical process to be computed)
2168c   -------------------------------------------
2169      IF (tituscap) THEN
2170         !!! get the actual co2 seasonal cap from Titus observations
2171         CALL geticecover(ngrid, 180.*zls/pi,
2172     .                  180.*longitude/pi, 180.*latitude/pi,
2173     .                  qsurf_tmp(:,igcm_co2) )
2174         qsurf_tmp(:,igcm_co2) = qsurf_tmp(:,igcm_co2) * 10000.
2175      ENDIF
2176     
2177
2178      IF (callcond) THEN
2179         zdtc(:,:) = 0.
2180         zdtsurfc(:,:) = 0.
2181         zduc(:,:) = 0.
2182         zdvc(:,:) = 0.
2183         zdqc(:,:,:) = 0.
2184         zdqsc(:,:,:) = 0.
2185         CALL co2condens(ngrid,nlayer,nq,ptimestep,
2186     $              capcal,zplay,zplev,tsurf,pt,
2187     $              pphi,pdt,pdu,pdv,zdtsurf,pu,pv,pq,pdq,
2188     $              qsurf(:,igcm_co2,:),albedo,emis,rdust,
2189     $              zdtc,zdtsurfc,pdpsrf,zduc,zdvc,zdqc,
2190     $              fluxsurf_dn_sw,zls,
2191     $              zdqssed_co2,zcondicea_co2microp,
2192     &              zdqsc)
2193
2194         DO iq=1, nq
2195           DO ig=1,ngrid
2196              dqsurf(ig,iq,:)=dqsurf(ig,iq,:)+zdqsc(ig,iq,:)
2197           ENDDO  ! (ig)
2198         ENDDO    ! (iq)
2199         DO l=1,nlayer
2200           DO ig=1,ngrid
2201             pdt(ig,l)=pdt(ig,l)+zdtc(ig,l)
2202             pdv(ig,l)=pdv(ig,l)+zdvc(ig,l)
2203             pdu(ig,l)=pdu(ig,l)+zduc(ig,l)
2204           ENDDO
2205         ENDDO
2206         DO ig=1,ngrid
2207           zdtsurf(ig,:) = zdtsurf(ig,:) + zdtsurfc(ig,:)
2208         ENDDO
2209
2210           DO iq=1, nq
2211            DO l=1,nlayer
2212              DO ig=1,ngrid
2213                pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqc(ig,l,iq)
2214              ENDDO
2215            ENDDO
2216           ENDDO
2217
2218#ifndef MESOSCALE
2219        ! update surface pressure
2220        DO ig=1,ngrid
2221          ps(ig) = zplev(ig,1) + pdpsrf(ig)*ptimestep
2222        ENDDO
2223        ! update pressure levels
2224        DO l=1,nlayer
2225         DO ig=1,ngrid
2226          zplay(ig,l) = aps(l) + bps(l)*ps(ig)
2227          zplev(ig,l) = ap(l)  + bp(l)*ps(ig)
2228         ENDDO
2229        ENDDO
2230        zplev(:,nlayer+1) = 0.
2231        ! update layers altitude
2232        DO l=2,nlayer
2233          DO ig=1,ngrid
2234           z1=(zplay(ig,l-1)+zplev(ig,l))/(zplay(ig,l-1)-zplev(ig,l))
2235           z2=(zplev(ig,l)+zplay(ig,l))/(zplev(ig,l)-zplay(ig,l))
2236           zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
2237          ENDDO
2238        ENDDO
2239#endif
2240      ENDIF  ! of IF (callcond)
2241
2242c-----------------------------------------------------------------------
2243c  Updating tracer budget on surface
2244c-----------------------------------------------------------------------         
2245        DO iq=1, nq
2246          DO ig=1,ngrid
2247           DO islope = 1,nslope
2248            qsurf(ig,iq,islope)=qsurf(ig,iq,islope)+
2249     &                ptimestep*dqsurf(ig,iq,islope)
2250           ENDDO
2251          ENDDO  ! (ig)
2252        ENDDO    ! (iq)
2253c-----------------------------------------------------------------------
2254c   12. Surface  and sub-surface soil temperature
2255c-----------------------------------------------------------------------
2256c
2257c
2258c   12.1 Increment Surface temperature:
2259c   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2260
2261      DO ig=1,ngrid
2262        DO islope = 1,nslope
2263            tsurf(ig,islope)=tsurf(ig,islope)+
2264     &            ptimestep*zdtsurf(ig,islope)
2265       ENDDO
2266      ENDDO
2267
2268c  Prescribe a cold trap at south pole (except at high obliquity !!)
2269c  Temperature at the surface is set there to be the temperature
2270c  corresponding to equilibrium temperature between phases of CO2
2271
2272
2273      IF (water) THEN
2274!#ifndef MESOSCALE
2275!         if (caps.and.(obliquit.lt.27.)) then => now done in co2condens
2276           ! NB: Updated surface pressure, at grid point 'ngrid', is
2277           !     ps(ngrid)=zplev(ngrid,1)+pdpsrf(ngrid)*ptimestep
2278!           tsurf(ngrid)=1./(1./136.27-r/5.9e+5*alog(0.0095*
2279!     &                     (zplev(ngrid,1)+pdpsrf(ngrid)*ptimestep)))
2280!           tsurf(ngrid)=1./(1./136.27-r/5.9e+5*alog(0.0095*ps(ngrid)))
2281!         endif
2282!#endif
2283c       -------------------------------------------------------------
2284c       Change of surface albedo in case of ground frost
2285c       everywhere except on the north permanent cap and in regions
2286c       covered by dry ice.
2287c              ALWAYS PLACE these lines after co2condens !!!
2288c       -------------------------------------------------------------
2289         do ig=1,ngrid
2290          do islope = 1,nslope
2291           if ((qsurf(ig,igcm_co2,islope).eq.0).and.
2292     &        (qsurf(ig,igcm_h2o_ice,islope)
2293     &       .gt.frost_albedo_threshold)) then
2294             if ((watercaptag(ig)).and.(cst_cap_albedo)) then
2295               albedo(ig,1,islope) = albedo_h2o_cap
2296               albedo(ig,2,islope) = albedo_h2o_cap
2297             else
2298               albedo(ig,1,islope) = albedo_h2o_frost
2299               albedo(ig,2,islope) = albedo_h2o_frost
2300             endif !((watercaptag(ig)).and.(cst_cap_albedo)) then
2301c              write(*,*) "frost thickness", qsurf(ig,igcm_h2o_ice)
2302c              write(*,*) "physiq.F frost :"
2303c     &        ,latitude(ig)*180./pi, longitude(ig)*180./pi
2304           endif
2305          enddo ! islope
2306         enddo  ! of do ig=1,ngrid
2307      ENDIF  ! of IF (water)
2308
2309c
2310c   12.2 Compute soil temperatures and subsurface heat flux:
2311c   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2312      IF (callsoil) THEN
2313c       Thermal inertia feedback
2314        IF (tifeedback) THEN
2315         DO islope = 1,nslope
2316           CALL soil_tifeedback(ngrid,nsoilmx,
2317     s               qsurf(:,:,islope),inertiesoil(:,:,islope))
2318         ENDDO
2319         CALL soil(ngrid,nsoilmx,.false.,inertiesoil,
2320     s          ptimestep,tsurf,tsoil,capcal,fluxgrd)
2321        ELSE
2322         CALL soil(ngrid,nsoilmx,.false.,inertiedat_tmp,
2323     s          ptimestep,tsurf,tsoil,capcal,fluxgrd)
2324        ENDIF
2325      ENDIF
2326
2327c     To avoid negative values
2328      IF (rdstorm) THEN
2329           where (pq(:,:,igcm_stormdust_mass) +
2330     &      ptimestep*pdq(:,:,igcm_stormdust_mass) < 0.)
2331             pdq(:,:,igcm_stormdust_mass) =
2332     &       - pq(:,:,igcm_stormdust_mass)/ptimestep + 1.e-30
2333             pdq(:,:,igcm_stormdust_number) =
2334     &       - pq(:,:,igcm_stormdust_number)/ptimestep + 1.e-30
2335           end where
2336           where (pq(:,:,igcm_stormdust_number) +
2337     &      ptimestep*pdq(:,:,igcm_stormdust_number) < 0.)
2338             pdq(:,:,igcm_stormdust_mass) =
2339     &       - pq(:,:,igcm_stormdust_mass)/ptimestep + 1.e-30
2340             pdq(:,:,igcm_stormdust_number) =
2341     &       - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
2342           end where
2343
2344            where (pq(:,:,igcm_dust_mass) +
2345     &      ptimestep*pdq(:,:,igcm_dust_mass) < 0.)
2346             pdq(:,:,igcm_dust_mass) =
2347     &       - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
2348             pdq(:,:,igcm_dust_number) =
2349     &       - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
2350           end where
2351           where (pq(:,:,igcm_dust_number) +
2352     &      ptimestep*pdq(:,:,igcm_dust_number) < 0.)
2353             pdq(:,:,igcm_dust_mass) =
2354     &       - pq(:,:,igcm_dust_mass)/ptimestep + 1.e-30
2355             pdq(:,:,igcm_dust_number) =
2356     &       - pq(:,:,igcm_dust_number)/ptimestep + 1.e-30
2357           end where
2358      ENDIF !(rdstorm)   
2359
2360c-----------------------------------------------------------------------
2361c   J. Naar : Surface and sub-surface water ice
2362c-----------------------------------------------------------------------
2363c
2364c
2365c   Increment Watercap (surface h2o reservoirs):
2366c   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2367
2368      DO ig=1,ngrid
2369        DO islope = 1,nslope
2370         watercap(ig,islope)=watercap(ig,islope)+
2371     s   ptimestep*dwatercap(ig,islope)
2372        ENDDO
2373      ENDDO
2374
2375      IF (refill_watercap) THEN
2376
2377        DO ig=1,ngrid
2378         DO islope = 1,nslope
2379           if (watercaptag(ig).and. (qsurf(ig,igcm_h2o_ice,islope)
2380     &        .gt.frost_metam_threshold)) then
2381
2382                watercap(ig,islope)=watercap(ig,islope)
2383     &          +qsurf(ig,igcm_h2o_ice,islope)
2384     &         - frost_metam_threshold
2385                qsurf(ig,igcm_h2o_ice,islope) = frost_metam_threshold
2386           endif ! (watercaptag(ig).and.
2387          ENDDO
2388        ENDDO
2389
2390      ENDIF ! (refill_watercap) THEN
2391
2392
2393c-----------------------------------------------------------------------
2394c  13. Write output files
2395c  ----------------------
2396
2397      call compute_meshgridavg(ngrid,nq,albedo,emis,tsurf,qsurf,
2398     &       albedo_meshavg,emis_meshavg,tsurf_meshavg,qsurf_meshavg)
2399
2400c    -------------------------------
2401c    Dynamical fields incrementation
2402c    -------------------------------
2403c (FOR OUTPUT ONLY : the actual model integration is performed in the dynamics)
2404      ! temperature, zonal and meridional wind
2405      DO l=1,nlayer
2406        DO ig=1,ngrid
2407          zt(ig,l)=pt(ig,l)  + pdt(ig,l)*ptimestep
2408          zu(ig,l)=pu(ig,l)  + pdu(ig,l)*ptimestep
2409          zv(ig,l)=pv(ig,l)  + pdv(ig,l)*ptimestep
2410        ENDDO
2411      ENDDO
2412
2413      ! tracers
2414      DO iq=1, nq
2415        DO l=1,nlayer
2416          DO ig=1,ngrid
2417            zq(ig,l,iq)=pq(ig,l,iq) +pdq(ig,l,iq)*ptimestep
2418          ENDDO
2419        ENDDO
2420      ENDDO
2421
2422      ! Density
2423      DO l=1,nlayer
2424         DO ig=1,ngrid
2425            rho(ig,l) = zplay(ig,l)/(rnew(ig,l)*zt(ig,l))
2426         ENDDO
2427      ENDDO
2428
2429      ! Potential Temperature
2430
2431       DO ig=1,ngrid
2432          DO l=1,nlayer
2433              zh(ig,l) = zt(ig,l)*(zplev(ig,1)/zplay(ig,l))**rcp
2434          ENDDO
2435       ENDDO
2436
2437c    Compute surface stress : (NB: z0 is a common in surfdat.h)
2438c     DO ig=1,ngrid
2439c        cd = (0.4/log(zzlay(ig,1)/z0(ig)))**2
2440c        zstress(ig) = rho(ig,1)*cd*(zu(ig,1)**2 + zv(ig,1)**2)
2441c     ENDDO
2442
2443c     Sum of fluxes in solar spectral bands (for output only)
2444      fluxtop_dn_sw_tot(1:ngrid)=fluxtop_dn_sw(1:ngrid,1) +
2445     &                           fluxtop_dn_sw(1:ngrid,2)
2446      fluxtop_up_sw_tot(1:ngrid)=fluxtop_up_sw(1:ngrid,1) +
2447     &                           fluxtop_up_sw(1:ngrid,2)
2448      fluxsurf_dn_sw_tot(1:ngrid,1:nslope)=
2449     &                           fluxsurf_dn_sw(1:ngrid,1,1:nslope) +
2450     &                           fluxsurf_dn_sw(1:ngrid,2,1:nslope)
2451      fluxsurf_up_sw_tot(1:ngrid)=fluxsurf_up_sw(1:ngrid,1) +
2452     &                            fluxsurf_up_sw(1:ngrid,2)
2453
2454c ******* TEST ******************************************************
2455      ztim1 = 999
2456      DO l=1,nlayer
2457        DO ig=1,ngrid
2458           if (pt(ig,l).lt.ztim1) then
2459               ztim1 = pt(ig,l)
2460               igmin = ig
2461               lmin = l
2462           end if
2463        ENDDO
2464      ENDDO
2465      if(min(pt(igmin,lmin),zt(igmin,lmin)).lt.70.) then
2466        write(*,*) 'PHYSIQ: stability WARNING :'
2467        write(*,*) 'pt, zt Tmin = ', pt(igmin,lmin), zt(igmin,lmin),
2468     &              'ig l =', igmin, lmin
2469      end if
2470c *******************************************************************
2471
2472c     ---------------------
2473c     Outputs to the screen
2474c     ---------------------
2475
2476      IF (lwrite) THEN
2477         PRINT*,'Global diagnostics for the physics'
2478         PRINT*,'Variables and their increments x and dx/dt * dt'
2479         WRITE(*,'(a6,a10,2a15)') 'Ts','dTs','ps','dps'
2480         WRITE(*,'(2f10.5,2f15.5)')
2481     s   tsurf(igout,:),zdtsurf(igout,:)*ptimestep,
2482     s   zplev(igout,1),pdpsrf(igout)*ptimestep
2483         WRITE(*,'(a4,a6,5a10)') 'l','u','du','v','dv','T','dT'
2484         WRITE(*,'(i4,6f10.5)') (l,
2485     s   pu(igout,l),pdu(igout,l)*ptimestep,
2486     s   pv(igout,l),pdv(igout,l)*ptimestep,
2487     s   pt(igout,l),pdt(igout,l)*ptimestep,
2488     s   l=1,nlayer)
2489      ENDIF ! of IF (lwrite)
2490
2491c        ----------------------------------------------------------
2492c        ----------------------------------------------------------
2493c        INTERPOLATIONS IN THE SURFACE-LAYER
2494c        ----------------------------------------------------------
2495c        ----------------------------------------------------------
2496
2497           n_out=0 ! number of elements in the z_out array.
2498                   ! for z_out=[3.,2.,1.,0.5,0.1], n_out must be set
2499                   ! to 5
2500           IF (n_out .ne. 0) THEN
2501
2502           IF(.NOT. ALLOCATED(z_out)) ALLOCATE(z_out(n_out))
2503           IF(.NOT. ALLOCATED(T_out)) ALLOCATE(T_out(ngrid,n_out))
2504           IF(.NOT. ALLOCATED(u_out)) ALLOCATE(u_out(ngrid,n_out))
2505
2506           z_out(:)=[3.,2.,1.,0.5,0.1]
2507           u_out(:,:)=0.
2508           T_out(:,:)=0.
2509
2510           call pbl_parameters(ngrid,nlayer,ps,zplay,z0,
2511     & g,zzlay,zzlev,zu,zv,wstar,hfmax_th,zmax_th,tsurf(:,iflat),
2512     & zh,z_out,n_out,T_out,u_out,ustar,tstar,L_mo,vhf,vvv)
2513                   ! pourquoi ustar recalcule ici? fait dans vdifc.
2514
2515#ifndef MESOSCALE
2516            IF (ngrid .eq. 1) THEN
2517               dimout=0
2518            ELSE
2519               dimout=2
2520            ENDIF
2521            DO n=1,n_out
2522               write(zstring, '(F8.6)') z_out(n)
2523               call WRITEDIAGFI(ngrid,'T_out_'//trim(zstring),
2524     &   'potential temperature at z_out','K',dimout,T_out(:,n))
2525               call WRITEDIAGFI(ngrid,'u_out_'//trim(zstring),
2526     &   'horizontal velocity norm at z_out','m/s',dimout,u_out(:,n))
2527            ENDDO
2528            call WRITEDIAGFI(ngrid,'u_star',
2529     &   'friction velocity','m/s',dimout,ustar)
2530            call WRITEDIAGFI(ngrid,'teta_star',
2531     &   'friction potential temperature','K',dimout,tstar)
2532!            call WRITEDIAGFI(ngrid,'L',
2533!     &   'Monin Obukhov length','m',dimout,L_mo)
2534            call WRITEDIAGFI(ngrid,'vvv',
2535     &   'Vertical velocity variance at zout','m',dimout,vvv)
2536            call WRITEDIAGFI(ngrid,'vhf',
2537     &   'Vertical heat flux at zout','m',dimout,vhf)
2538#else
2539         T_out1(:)=T_out(:,1)
2540         u_out1(:)=u_out(:,1)
2541#endif
2542
2543         ENDIF
2544
2545c        ----------------------------------------------------------
2546c        ----------------------------------------------------------
2547c        END OF SURFACE LAYER INTERPOLATIONS
2548c        ----------------------------------------------------------
2549c        ----------------------------------------------------------
2550
2551      IF (ngrid.NE.1) THEN
2552
2553#ifndef MESOSCALE
2554c        -------------------------------------------------------------------
2555c        Writing NetCDF file  "RESTARTFI" at the end of the run
2556c        -------------------------------------------------------------------
2557c        Note: 'restartfi' is stored just before dynamics are stored
2558c              in 'restart'. Between now and the writting of 'restart',
2559c              there will have been the itau=itau+1 instruction and
2560c              a reset of 'time' (lastacll = .true. when itau+1= itaufin)
2561c              thus we store for time=time+dtvr
2562
2563         ! default: not writing a restart file at this time step
2564         write_restart=.false.
2565         IF (ecritstart.GT.0) THEN
2566           ! For when we store multiple time steps in the restart file
2567           IF (MODULO(icount*iphysiq,ecritstart).EQ.0) THEN
2568             write_restart=.true.
2569           ENDIF
2570         ENDIF
2571         IF (lastcall) THEN
2572           ! Always write a restart at the end of the simulation
2573           write_restart=.true.
2574         ENDIF
2575         
2576          IF (write_restart) THEN
2577           IF (grid_type==unstructured) THEN !IF DYNAMICO
2578
2579             ! When running Dynamico, no need to add a dynamics time step to ztime_fin
2580             IF (ptime.LE. 1.E-10) THEN
2581             ! Residual ptime occurs with Dynamico
2582             ztime_fin = pday !+ ptime + ptimestep/(float(iphysiq)*daysec)
2583     .               - day_ini - time_phys
2584             ELSE
2585             ztime_fin = pday + ptime  !+ ptimestep/(float(iphysiq)*daysec)
2586     .                  - day_ini - time_phys
2587             ENDIF
2588             if (ecritstart==0) then
2589                ztime_fin = ztime_fin-(day_end-day_ini)
2590             endif
2591
2592           ELSE ! IF LMDZ
2593
2594          if (ecritstart.GT.0) then !IF MULTIPLE RESTARTS nothing change
2595          ztime_fin = pday - day_ini + ptime 
2596     .               + ptimestep/(float(iphysiq)*daysec)
2597          else !IF ONE RESTART final time in top of day_end
2598          ztime_fin = pday - day_ini-(day_end-day_ini)
2599     .               + ptime  + ptimestep/(float(iphysiq)*daysec)
2600          endif
2601
2602           ENDIF ! of IF (grid_type==unstructured)
2603          write(*,'(A,I7,A,F12.5)')
2604     .         'PHYSIQ: writing in restartfi ; icount=',
2605     .          icount,' date=',ztime_fin
2606           
2607          call physdem1("restartfi.nc",nsoilmx,ngrid,nlayer,nq,
2608     .                ptimestep,ztime_fin,
2609     .                tsurf,tsoil,albedo,
2610     .                emis,q2,qsurf,tauscaling,totcloudfrac,wstar,
2611     .                watercap)
2612         
2613         ENDIF ! of IF (write_restart)
2614#endif
2615
2616c        -------------------------------------------------------------------
2617c        Calculation of diagnostic variables written in both stats and
2618c          diagfi files
2619c        -------------------------------------------------------------------
2620
2621
2622         do ig=1,ngrid
2623           if(mu0(ig).le.0.01) then
2624            fluxsurf_dir_dn_sw(ig) = 0.
2625           else
2626            fluxsurf_dir_dn_sw(ig) = flux_1AU/dist_sol/dist_sol*mu0(ig)*
2627     &                    exp(-(tau(ig,iaer_dust_doubleq)+
2628     &                          tau(ig,iaer_h2o_ice))/mu0(ig))
2629           endif
2630         enddo
2631
2632           ! Density-scaled opacities
2633              do ig=1,ngrid
2634                dsodust(ig,:) =
2635     &                dsodust(ig,:)*tauscaling(ig)
2636                dsords(ig,:) =
2637     &                dsords(ig,:)*tauscaling(ig)
2638                dsotop(ig,:) =
2639     &                dsotop(ig,:)*tauscaling(ig)
2640              enddo
2641
2642           if(doubleq) then
2643              do ig=1,ngrid
2644                dqdustsurf(ig) =
2645     &                zdqssed(ig,igcm_dust_mass)*tauscaling(ig)
2646                dndustsurf(ig) =
2647     &                zdqssed(ig,igcm_dust_number)*tauscaling(ig)
2648                ndust(ig,:) =
2649     &                zq(ig,:,igcm_dust_number)*tauscaling(ig)
2650                qdust(ig,:) =
2651     &                zq(ig,:,igcm_dust_mass)*tauscaling(ig)
2652              enddo
2653              if (scavenging) then
2654                do ig=1,ngrid
2655                  dqdustsurf(ig) = dqdustsurf(ig) +
2656     &                     zdqssed(ig,igcm_ccn_mass)*tauscaling(ig)
2657                  dndustsurf(ig) = dndustsurf(ig) +
2658     &                     zdqssed(ig,igcm_ccn_number)*tauscaling(ig)
2659                  nccn(ig,:) =
2660     &                     zq(ig,:,igcm_ccn_number)*tauscaling(ig)
2661                  qccn(ig,:) =
2662     &                     zq(ig,:,igcm_ccn_mass)*tauscaling(ig)
2663                enddo
2664              endif
2665           endif ! of (doubleq)
2666
2667           if (rdstorm) then   ! diagnostics of stormdust tendancies for 1D and 3D
2668              mstormdtot(:)=0
2669              mdusttot(:)=0
2670              qdusttotal(:,:)=0
2671              do ig=1,ngrid
2672                rdsdqdustsurf(ig) =
2673     &                zdqssed(ig,igcm_stormdust_mass)*tauscaling(ig)
2674                rdsdndustsurf(ig) =
2675     &                zdqssed(ig,igcm_stormdust_number)*tauscaling(ig)
2676                rdsndust(ig,:) =
2677     &                pq(ig,:,igcm_stormdust_number)*tauscaling(ig)
2678                rdsqdust(ig,:) =
2679     &                pq(ig,:,igcm_stormdust_mass)*tauscaling(ig)
2680                do l=1,nlayer
2681                    mstormdtot(ig) = mstormdtot(ig) +
2682     &                      zq(ig,l,igcm_stormdust_mass) *
2683     &                      (zplev(ig,l) - zplev(ig,l+1)) / g
2684                    mdusttot(ig) = mdusttot(ig) +
2685     &                      zq(ig,l,igcm_dust_mass) *
2686     &                      (zplev(ig,l) - zplev(ig,l+1)) / g
2687                    qdusttotal(ig,l) = qdust(ig,l)+rdsqdust(ig,l) !calculate total dust
2688                enddo
2689              enddo
2690           endif !(rdstorm)
2691                             
2692           if (water) then
2693             mtot(:)=0
2694             icetot(:)=0
2695             rave(:)=0
2696             tauTES(:)=0
2697
2698             IF (hdo) then
2699                 mtotD(:)=0
2700                 icetotD(:)=0
2701             ENDIF !hdo
2702
2703             do ig=1,ngrid
2704               do l=1,nlayer
2705                 mtot(ig) = mtot(ig) +
2706     &                      zq(ig,l,igcm_h2o_vap) *
2707     &                      (zplev(ig,l) - zplev(ig,l+1)) / g
2708                 icetot(ig) = icetot(ig) +
2709     &                        zq(ig,l,igcm_h2o_ice) *
2710     &                        (zplev(ig,l) - zplev(ig,l+1)) / g
2711                 IF (hdo) then
2712                 mtotD(ig) = mtotD(ig) +
2713     &                      zq(ig,l,igcm_hdo_vap) *
2714     &                      (zplev(ig,l) - zplev(ig,l+1)) / g
2715                 icetotD(ig) = icetotD(ig) +
2716     &                        zq(ig,l,igcm_hdo_ice) *
2717     &                        (zplev(ig,l) - zplev(ig,l+1)) / g
2718                 ENDIF !hdo
2719
2720c                Computing abs optical depth at 825 cm-1 in each
2721c                  layer to simulate NEW TES retrieval
2722                 Qabsice = min(
2723     &             max(0.4e6*rice(ig,l)*(1.+nuice_ref)-0.05 ,0.),1.2
2724     &                        )
2725                 opTES(ig,l)= 0.75 * Qabsice *
2726     &             zq(ig,l,igcm_h2o_ice) *
2727     &             (zplev(ig,l) - zplev(ig,l+1)) / g
2728     &             / (rho_ice * rice(ig,l) * (1.+nuice_ref))
2729                 tauTES(ig)=tauTES(ig)+ opTES(ig,l)
2730               enddo
2731c              rave(ig)=rave(ig)/max(icetot(ig),1.e-30)       ! mass weight
2732c               if (icetot(ig)*1e3.lt.0.01) rave(ig)=0.
2733             enddo
2734             call watersat(ngrid*nlayer,zt,zplay,zqsat)
2735             satu(:,:) = zq(:,:,igcm_h2o_vap)/zqsat(:,:)
2736
2737             if (scavenging) then
2738               Nccntot(:)= 0
2739               Mccntot(:)= 0
2740               rave(:)=0
2741               do ig=1,ngrid
2742                 do l=1,nlayer
2743                    Nccntot(ig) = Nccntot(ig) +
2744     &              zq(ig,l,igcm_ccn_number)*tauscaling(ig)
2745     &              *(zplev(ig,l) - zplev(ig,l+1)) / g
2746                    Mccntot(ig) = Mccntot(ig) +
2747     &              zq(ig,l,igcm_ccn_mass)*tauscaling(ig)
2748     &              *(zplev(ig,l) - zplev(ig,l+1)) / g
2749cccc Column integrated effective ice radius
2750cccc is weighted by total ice surface area (BETTER than total ice mass)
2751                    rave(ig) = rave(ig) +
2752     &                      tauscaling(ig) *
2753     &                      zq(ig,l,igcm_ccn_number) *
2754     &                      (zplev(ig,l) - zplev(ig,l+1)) / g *
2755     &                      rice(ig,l) * rice(ig,l)*  (1.+nuice_ref)
2756                 enddo
2757               rave(ig)=(icetot(ig)/rho_ice+Mccntot(ig)/rho_dust)*0.75
2758     &                  /max(pi*rave(ig),1.e-30) ! surface weight
2759               if (icetot(ig)*1e3.lt.0.01) rave(ig)=0.
2760               enddo
2761             else ! of if (scavenging)
2762               rave(:)=0
2763               do ig=1,ngrid
2764                 do l=1,nlayer
2765                 rave(ig) = rave(ig) +
2766     &                      zq(ig,l,igcm_h2o_ice) *
2767     &                      (zplev(ig,l) - zplev(ig,l+1)) / g *
2768     &                      rice(ig,l) * (1.+nuice_ref)
2769                 enddo
2770                 rave(ig) = max(rave(ig) /
2771     &             max(icetot(ig),1.e-30),1.e-30) ! mass weight
2772               enddo
2773             endif ! of if (scavenging)
2774
2775           !Alternative A. Pottier weighting
2776           rave2(:) = 0.
2777           totrave2(:) = 0.
2778           do ig=1,ngrid
2779              do l=1,nlayer
2780              rave2(ig) =rave2(ig)+ zq(ig,l,igcm_h2o_ice)*rice(ig,l)
2781              totrave2(ig) = totrave2(ig) + zq(ig,l,igcm_h2o_ice)
2782              end do
2783              rave2(ig)=max(rave2(ig)/max(totrave2(ig),1.e-30),1.e-30)
2784           end do
2785
2786           endif ! of if (water)
2787
2788          if (co2clouds) then
2789            mtotco2(1:ngrid) = 0.
2790            icetotco2(1:ngrid) = 0.
2791            vaptotco2(1:ngrid) = 0.
2792            do ig=1,ngrid
2793              do l=1,nlayer
2794                vaptotco2(ig) = vaptotco2(ig) +
2795     &                          zq(ig,l,igcm_co2) *
2796     &                          (zplev(ig,l) - zplev(ig,l+1)) / g
2797                icetotco2(ig) = icetot(ig) +
2798     &                          zq(ig,l,igcm_co2_ice) *
2799     &                          (zplev(ig,l) - zplev(ig,l+1)) / g
2800              end do
2801              mtotco2(ig) = icetotco2(ig) + vaptotco2(ig)
2802            end do
2803          end if
2804
2805#ifndef MESOSCALE
2806c        -----------------------------------------------------------------
2807c        WSTATS: Saving statistics
2808c        -----------------------------------------------------------------
2809c        ("stats" stores and accumulates key variables in file "stats.nc"
2810c        which can later be used to make the statistic files of the run:
2811c        if flag "callstats" from callphys.def is .true.)
2812         
2813        call wstats(ngrid,"ps","Surface pressure","Pa",2,ps)
2814        call wstats(ngrid,"tsurf","Surface temperature","K",2
2815     &      ,tsurf(:,iflat))
2816        call wstats(ngrid,"co2ice","CO2 ice cover",
2817     &                "kg.m-2",2,qsurf(:,igcm_co2,iflat))
2818        call wstats(ngrid,"watercap","H2O ice cover",
2819     &                "kg.m-2",2,watercap(:,iflat))
2820        call wstats(ngrid,"tau_pref_scenario",
2821     &              "prescribed visible dod at 610 Pa","NU",
2822     &                2,tau_pref_scenario)
2823        call wstats(ngrid,"tau_pref_gcm",
2824     &              "visible dod at 610 Pa in the GCM","NU",
2825     &                2,tau_pref_gcm)
2826        call wstats(ngrid,"fluxsurf_lw",
2827     &                "Thermal IR radiative flux to surface","W.m-2",2,
2828     &                fluxsurf_lw(:,iflat))
2829        call wstats(ngrid,"fluxsurf_dn_sw",
2830     &        "Incoming Solar radiative flux to surface","W.m-2",2,
2831     &                fluxsurf_dn_sw_tot(:,iflat))
2832        call wstats(ngrid,"fluxsurf_up_sw",
2833     &        "Reflected Solar radiative flux from surface","W.m-2",2,
2834     &                fluxsurf_up_sw_tot)
2835        call wstats(ngrid,"fluxtop_lw",
2836     &                "Thermal IR radiative flux to space","W.m-2",2,
2837     &                fluxtop_lw)
2838        call wstats(ngrid,"fluxtop_dn_sw",
2839     &        "Incoming Solar radiative flux from space","W.m-2",2,
2840     &                fluxtop_dn_sw_tot)
2841        call wstats(ngrid,"fluxtop_up_sw",
2842     &        "Outgoing Solar radiative flux to space","W.m-2",2,
2843     &                fluxtop_up_sw_tot)
2844        call wstats(ngrid,"temp","Atmospheric temperature","K",3,zt)
2845        call wstats(ngrid,"u","Zonal (East-West) wind","m.s-1",3,zu)
2846        call wstats(ngrid,"v","Meridional (North-South) wind",
2847     &                "m.s-1",3,zv)
2848        call wstats(ngrid,"w","Vertical (down-up) wind",
2849     &                "m.s-1",3,pw)
2850        call wstats(ngrid,"rho","Atmospheric density","kg/m3",3,rho)
2851        call wstats(ngrid,"pressure","Pressure","Pa",3,zplay)
2852          call wstats(ngrid,"q2",
2853     &                "Boundary layer eddy kinetic energy",
2854     &                "m2.s-2",3,q2)
2855          call wstats(ngrid,"emis","Surface emissivity","w.m-1",2,
2856     &                emis(:,iflat))
2857c          call wstats(ngrid,"ssurf","Surface stress","N.m-2",
2858c    &                2,zstress)
2859c          call wstats(ngrid,"sw_htrt","sw heat.rate",
2860c    &                 "W.m-2",3,zdtsw)
2861c          call wstats(ngrid,"lw_htrt","lw heat.rate",
2862c    &                 "W.m-2",3,zdtlw)
2863          call wstats(ngrid,"fluxsurf_dir_dn_sw",
2864     &                "Direct incoming SW flux at surface",
2865     &                "W.m-2",2,fluxsurf_dir_dn_sw)     
2866
2867          if (calltherm) then
2868            call wstats(ngrid,"zmax_th","Height of thermals",
2869     &                "m",2,zmax_th)
2870            call wstats(ngrid,"hfmax_th","Max thermals heat flux",
2871     &                "K.m/s",2,hfmax_th)
2872            call wstats(ngrid,"wstar",
2873     &                "Max vertical velocity in thermals",
2874     &                "m/s",2,wstar)
2875          endif
2876
2877             if (water) then
2878               vmr=zq(1:ngrid,1:nlayer,igcm_h2o_vap)
2879     &      *mmean(1:ngrid,1:nlayer)/mmol(igcm_h2o_vap)
2880               call wstats(ngrid,"vmr_h2ovap",
2881     &                    "H2O vapor volume mixing ratio","mol/mol",
2882     &                    3,vmr)
2883               vmr=zq(1:ngrid,1:nlayer,igcm_h2o_ice)
2884     &      *mmean(1:ngrid,1:nlayer)/mmol(igcm_h2o_ice)
2885               call wstats(ngrid,"vmr_h2oice",
2886     &                    "H2O ice volume mixing ratio","mol/mol",
2887     &                    3,vmr)
2888              ! also store vmr_ice*rice for better diagnostics of rice
2889               vmr(1:ngrid,1:nlayer)=vmr(1:ngrid,1:nlayer)*
2890     &                               rice(1:ngrid,1:nlayer)
2891               call wstats(ngrid,"vmr_h2oice_rice",
2892     &                "H2O ice mixing ratio times ice particule size",
2893     &                    "(mol/mol)*m",
2894     &                    3,vmr)
2895               vmr=zqsat(1:ngrid,1:nlayer)
2896     &      *mmean(1:ngrid,1:nlayer)/mmol(igcm_h2o_vap)
2897               call wstats(ngrid,"vmr_h2osat",
2898     &                    "saturation volume mixing ratio","mol/mol",
2899     &                    3,vmr)
2900               call wstats(ngrid,"h2o_ice_s",
2901     &                    "surface h2o_ice","kg/m2",
2902     &                    2,qsurf(1,igcm_h2o_ice,iflat))
2903               call wstats(ngrid,'albedo',
2904     &                         'albedo',
2905     &                         '',2,albedo(1,1,iflat))
2906               call wstats(ngrid,"mtot",
2907     &                    "total mass of water vapor","kg/m2",
2908     &                    2,mtot)
2909               call wstats(ngrid,"icetot",
2910     &                    "total mass of water ice","kg/m2",
2911     &                    2,icetot)
2912               call wstats(ngrid,"reffice",
2913     &                    "Mean reff","m",
2914     &                    2,rave)
2915              call wstats(ngrid,"Nccntot",
2916     &                    "condensation nuclei","Nbr/m2",
2917     &                    2,Nccntot)
2918              call wstats(ngrid,"Mccntot",
2919     &                    "condensation nuclei mass","kg/m2",
2920     &                    2,Mccntot)
2921              call wstats(ngrid,"rice",
2922     &                    "Ice particle size","m",
2923     &                    3,rice)
2924               if (.not.activice) then
2925                 call wstats(ngrid,"tauTESap",
2926     &                      "tau abs 825 cm-1","",
2927     &                      2,tauTES)
2928               else
2929                 call wstats(ngrid,'tauTES',
2930     &                   'tau abs 825 cm-1',
2931     &                  '',2,taucloudtes)
2932               endif
2933
2934             endif ! of if (water)
2935
2936             if (co2clouds) then
2937               call wstats(ngrid,"mtotco2",
2938     &                    "total mass atm of co2","kg/m2",
2939     &                    2,mtotco2)
2940               call wstats(ngrid,"icetotco2",
2941     &                    "total mass atm of co2 ice","kg/m2",
2942     &                    2,icetotco2)
2943               call wstats(ngrid,"vaptotco2",
2944     &                    "total mass atm of co2 vapor","kg/m2",
2945     &                    2,icetotco2)
2946             end if
2947             
2948             
2949           if (dustbin.ne.0) then
2950         
2951             call wstats(ngrid,'tau','taudust','SI',2,tau(1,1))
2952             
2953             if (doubleq) then
2954c            call wstats(ngrid,'qsurf','qsurf',
2955c     &                       'kg.m-2',2,qsurf(1,igcm_dust_mass))
2956c            call wstats(ngrid,'Nsurf','N particles',
2957c     &                       'N.m-2',2,qsurf(1,igcm_dust_number))
2958c            call wstats(ngrid,'dqsdev','ddevil lift',
2959c    &                       'kg.m-2.s-1',2,zdqsdev(1,1))
2960c            call wstats(ngrid,'dqssed','sedimentation',
2961c     &                       'kg.m-2.s-1',2,zdqssed(1,1))
2962c            call wstats(ngrid,'dqsdif','diffusion',
2963c     &                       'kg.m-2.s-1',2,zdqsdif(1,1))
2964               call wstats(ngrid,'dqsdust',
2965     &                        'deposited surface dust mass',
2966     &                        'kg.m-2.s-1',2,dqdustsurf)
2967               call wstats(ngrid,'dqndust',
2968     &                        'deposited surface dust number',
2969     &                        'number.m-2.s-1',2,dndustsurf)
2970               call wstats(ngrid,'reffdust','reffdust',
2971     &                        'm',3,rdust*ref_r0)
2972               call wstats(ngrid,'dustq','Dust mass mr',
2973     &                        'kg/kg',3,qdust)
2974               call wstats(ngrid,'dustN','Dust number',
2975     &                        'part/kg',3,ndust)
2976               if (rdstorm) then
2977                 call wstats(ngrid,'reffstormdust','reffdust',
2978     &                          'm',3,rstormdust*ref_r0)
2979                 call wstats(ngrid,'rdsdustq','Dust mass mr',
2980     &                          'kg/kg',3,rdsqdust)
2981                 call wstats(ngrid,'rdsdustN','Dust number',
2982     &                        'part/kg',3,rdsndust)
2983               end if
2984             else
2985               do iq=1,dustbin
2986                 write(str2(1:2),'(i2.2)') iq
2987                 call wstats(ngrid,'q'//str2,'mix. ratio',
2988     &                         'kg/kg',3,zq(1,1,iq))
2989                 call wstats(ngrid,'qsurf'//str2,'qsurf',
2990     &                         'kg.m-2',2,qsurf(1,iq,iflat))
2991               end do
2992             endif ! (doubleq)
2993
2994             if (scavenging) then
2995               call wstats(ngrid,'ccnq','CCN mass mr',
2996     &                        'kg/kg',3,qccn)
2997               call wstats(ngrid,'ccnN','CCN number',
2998     &                        'part/kg',3,nccn)
2999             endif ! (scavenging)
3000         
3001           endif ! (dustbin.ne.0)
3002
3003           if (photochem) then
3004              do iq=1,nq
3005                 if (noms(iq) .ne. "dust_mass" .and.
3006     $               noms(iq) .ne. "dust_number" .and.
3007     $               noms(iq) .ne. "ccn_mass" .and.
3008     $               noms(iq) .ne. "ccn_number" .and.
3009     $               noms(iq) .ne. "ccnco2_mass" .and.
3010     $               noms(iq) .ne. "ccnco2_number" .and.
3011     $               noms(iq) .ne. "stormdust_mass" .and.
3012     $               noms(iq) .ne. "stormdust_number" .and.
3013     $               noms(iq) .ne. "topdust_mass" .and.
3014     $               noms(iq) .ne. "topdust_number") then
3015!                   volume mixing ratio
3016
3017                    vmr(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,iq)
3018     &                            *mmean(1:ngrid,1:nlayer)/mmol(iq)
3019
3020                    call wstats(ngrid,"vmr_"//trim(noms(iq)),
3021     $                        "Volume mixing ratio","mol/mol",3,vmr)
3022                    if ((noms(iq).eq."o")
3023     $             .or. (noms(iq).eq."co2")
3024     $             .or. (noms(iq).eq."o3")
3025     $             .or. (noms(iq).eq."ar")
3026     $             .or. (noms(iq).eq."o2")
3027     $             .or. (noms(iq).eq."h2o_vap") ) then
3028                      call writediagfi(ngrid,"vmr_"//trim(noms(iq)),
3029     $                         "Volume mixing ratio","mol/mol",3,vmr)
3030                    end if
3031
3032!                   number density (molecule.cm-3)
3033
3034                    rhopart(1:ngrid,1:nlayer)=zq(1:ngrid,1:nlayer,iq)
3035     &                          *rho(1:ngrid,1:nlayer)*n_avog/
3036     &                           (1000*mmol(iq))
3037
3038                   call wstats(ngrid,"num_"//trim(noms(iq)),
3039     $                   "Number density","cm-3",3,rhopart)
3040                   call writediagfi(ngrid,"num_"//trim(noms(iq)),
3041     $                  "Number density","cm-3",3,rhopart)
3042
3043!                   vertical column (molecule.cm-2)
3044
3045                    do ig = 1,ngrid
3046                       colden(ig,iq) = 0.                           
3047                    end do
3048                    do l=1,nlayer                                   
3049                       do ig=1,ngrid                                 
3050                          colden(ig,iq) = colden(ig,iq) + zq(ig,l,iq)
3051     $                                   *(zplev(ig,l)-zplev(ig,l+1))
3052     $                                   *6.022e22/(mmol(iq)*g)       
3053                       end do                                       
3054                    end do                                         
3055
3056                    call wstats(ngrid,"c_"//trim(noms(iq)),           
3057     $                          "column","mol cm-2",2,colden(1,iq)) 
3058                    call writediagfi(ngrid,"c_"//trim(noms(iq)),
3059     $                          "column","mol cm-2",2,colden(1,iq))
3060
3061!                   global mass (g)
3062               
3063                    call planetwide_sumval(colden(:,iq)/6.022e23
3064     $                            *mmol(iq)*1.e4*cell_area(:),mass(iq))
3065
3066                    call writediagfi(ngrid,"mass_"//trim(noms(iq)),
3067     $                              "global mass","g",0,mass(iq))
3068
3069                 end if ! of if (noms(iq) .ne. "dust_mass" ...)
3070              end do ! of do iq=1,nq
3071           end if ! of if (photochem)
3072
3073
3074           IF(lastcall.and.callstats) THEN
3075             write (*,*) "Writing stats..."
3076             call mkstats(ierr)
3077           ENDIF
3078
3079
3080c        (Store EOF for Mars Climate database software)
3081         IF (calleofdump) THEN
3082            CALL eofdump(ngrid, nlayer, zu, zv, zt, rho, ps)
3083         ENDIF
3084#endif
3085!endif of ifndef MESOSCALE
3086
3087#ifdef MESOSCALE
3088     
3089      !! see comm_wrf.
3090      !! not needed when an array is already in a shared module.
3091      !! --> example : hfmax_th, zmax_th
3092
3093      CALL allocate_comm_wrf(ngrid,nlayer)
3094
3095      !state  real  HR_SW     ikj   misc  1  -  h  "HR_SW"     "HEATING RATE SW"                 "K/s"
3096      comm_HR_SW(1:ngrid,1:nlayer) = zdtsw(1:ngrid,1:nlayer)
3097      !state  real  HR_LW     ikj   misc  1  -  h  "HR_LW"     "HEATING RATE LW"                 "K/s"
3098      comm_HR_LW(1:ngrid,1:nlayer) = zdtlw(1:ngrid,1:nlayer)
3099      !state  real  SWDOWNZ    ij   misc  1  -  h  "SWDOWNZ"   "DOWNWARD SW FLUX AT SURFACE"     "W m-2"
3100      comm_SWDOWNZ(1:ngrid) = fluxsurf_dn_sw_tot(1:ngrid)
3101      !state  real  TAU_DUST   ij   misc  1  -  h  "TAU_DUST"  "REFERENCE VISIBLE DUST OPACITY"  ""
3102      comm_TAU_DUST(1:ngrid) = tau_pref_gcm(1:ngrid)
3103      !state  real  RDUST     ikj   misc  1  -  h  "RDUST"     "DUST RADIUS"                     "m"
3104      comm_RDUST(1:ngrid,1:nlayer) = rdust(1:ngrid,1:nlayer)
3105      !state  real  QSURFDUST  ij   misc  1  -  h  "QSURFDUST" "DUST MASS AT SURFACE"            "kg m-2"
3106      IF (igcm_dust_mass .ne. 0) THEN
3107        comm_QSURFDUST(1:ngrid) = qsurf(1:ngrid,igcm_dust_mass)
3108      ELSE
3109        comm_QSURFDUST(1:ngrid) = 0.
3110      ENDIF
3111      !state  real  MTOT       ij   misc  1  -  h  "MTOT"      "TOTAL MASS WATER VAPOR in pmic"  "pmic"
3112      comm_MTOT(1:ngrid) = mtot(1:ngrid) * 1.e6 / rho_ice
3113      !state  real  ICETOT     ij   misc  1  -  h  "ICETOT"    "TOTAL MASS WATER ICE"            "kg m-2"
3114      comm_ICETOT(1:ngrid) = icetot(1:ngrid) * 1.e6 / rho_ice
3115      !state  real  VMR_ICE   ikj   misc  1  -  h  "VMR_ICE"   "VOL. MIXING RATIO ICE"           "ppm"
3116      IF (igcm_h2o_ice .ne. 0) THEN
3117        comm_VMR_ICE(1:ngrid,1:nlayer) = 1.e6
3118     .    * zq(1:ngrid,1:nlayer,igcm_h2o_ice)
3119     .    * mmean(1:ngrid,1:nlayer) / mmol(igcm_h2o_ice)
3120      ELSE
3121        comm_VMR_ICE(1:ngrid,1:nlayer) = 0.
3122      ENDIF
3123      !state  real  TAU_ICE    ij   misc  1  -  h  "TAU_ICE"   "CLOUD OD at 825 cm-1 TES"        ""
3124      if (activice) then
3125        comm_TAU_ICE(1:ngrid) = taucloudtes(1:ngrid)
3126      else
3127        comm_TAU_ICE(1:ngrid) = tauTES(1:ngrid)
3128      endif
3129      !state  real  RICE      ikj   misc  1  -  h  "RICE"      "ICE RADIUS"                      "m"
3130      comm_RICE(1:ngrid,1:nlayer) = rice(1:ngrid,1:nlayer)
3131   
3132      !! calculate sensible heat flux in W/m2 for outputs
3133      !! -- the one computed in vdifc is not the real one
3134      !! -- vdifc must have been called
3135      if (.not.callrichsl) then
3136        sensibFlux(1:ngrid) = zflubid(1:ngrid)
3137     .         - capcal(1:ngrid)*zdtsdif(1:ngrid)
3138      else
3139        sensibFlux(1:ngrid) =
3140     &   (pplay(1:ngrid,1)/(r*pt(1:ngrid,1)))*cpp
3141     &   *sqrt(pu(1:ngrid,1)*pu(1:ngrid,1)+pv(1:ngrid,1)*pv(1:ngrid,1)
3142     &         +(log(1.+0.7*wstar(1:ngrid) + 2.3*wstar(1:ngrid)**2))**2)
3143     &   *zcdh(1:ngrid)*(tsurf(1:ngrid)-zh(1:ngrid,1))
3144      endif
3145         
3146#else
3147#ifndef MESOINI
3148
3149c        ==========================================================
3150c        WRITEDIAGFI: Outputs in netcdf file "DIAGFI", containing
3151c          any variable for diagnostic (output with period
3152c          "ecritphy", set in "run.def")
3153c        ==========================================================
3154c        WRITEDIAGFI can ALSO be called from any other subroutines
3155c        for any variables !!
3156         call WRITEDIAGFI(ngrid,"emis","Surface emissivity","w.m-1",2,
3157     &                  emis(:,iflat))
3158         do islope=1,nslope
3159           write(str2(1:2),'(i2.2)') islope
3160           call WRITEDIAGFI(ngrid,"emis_slope"//str2,
3161     &    "Surface emissivity","w.m-1",2, emis(:,islope))
3162         ENDDO
3163c        call WRITEDIAGFI(ngrid,"pplay","Pressure","Pa",3,zplay)
3164c        call WRITEDIAGFI(ngrid,"pplev","Pressure","Pa",3,zplev)
3165         call WRITEDIAGFI(ngrid,"zzlev","Interlayer altitude",
3166     &                    "m",3,zzlev(:,1:nlayer))
3167         call writediagfi(ngrid,"pphi","Geopotential","m2s-2",3,
3168     &                    pphi)
3169         call writediagfi(ngrid,"phisfi","Surface geopotential",
3170     &                    "m2s-2",2,phisfi)
3171         call WRITEDIAGFI(ngrid,"tsurf","Surface temperature","K",2,
3172     &                  tsurf(:,iflat))
3173         do islope=1,nslope
3174          write(str2(1:2),'(i2.2)') islope
3175         call WRITEDIAGFI(ngrid,"tsurf_slope"//str2,
3176     &            "Surface temperature","K",2,
3177     &                  tsurf(:,islope))
3178         ENDDO
3179         call WRITEDIAGFI(ngrid,"ps","surface pressure","Pa",2,ps)
3180         call WRITEDIAGFI(ngrid,"co2ice","co2 ice thickness"
3181     &                              ,"kg.m-2",2,qsurf(:,igcm_co2,iflat))
3182         do islope=1,nslope
3183          write(str2(1:2),'(i2.2)') islope
3184         call WRITEDIAGFI(ngrid,"co2ice_slope"//str2,"co2 ice thickness"
3185     &              ,"kg.m-2",2,qsurf(:,igcm_co2,islope))
3186         ENDDO
3187         call WRITEDIAGFI(ngrid,"watercap","Water ice thickness"
3188     &         ,"kg.m-2",2,watercap(:,iflat))
3189         do islope=1,nslope
3190          write(str2(1:2),'(i2.2)') islope
3191         call WRITEDIAGFI(ngrid,"watercap_slope"//str2,
3192     &         "Water ice thickness"
3193     &         ,"kg.m-2",2,watercap(:,islope))
3194         ENDDO
3195         call WRITEDIAGFI(ngrid,"temp_layer1","temperature in layer 1",
3196     &                  "K",2,zt(1,1))
3197         call WRITEDIAGFI(ngrid,"temp7","temperature in layer 7",
3198     &                  "K",2,zt(1,7))
3199         call WRITEDIAGFI(ngrid,"fluxsurf_lw","fluxsurf_lw","W.m-2",2,
3200     &                  fluxsurf_lw(:,iflat))
3201         do islope=1,nslope
3202          write(str2(1:2),'(i2.2)') islope
3203         call WRITEDIAGFI(ngrid,"fluxsurf_lw_slope"//str2,
3204     &              "fluxsurf_lw","W.m-2",2,
3205     &                  fluxsurf_lw(:,islope))
3206         ENDDO
3207         call WRITEDIAGFI(ngrid,"fluxsurf_dn_sw","fluxsurf_dn_sw",
3208     &                  "W.m-2",2,fluxsurf_dn_sw_tot(:,iflat))
3209         do islope=1,nslope
3210          write(str2(1:2),'(i2.2)') islope
3211         call WRITEDIAGFI(ngrid,"fluxsurf_dn_sw_slope"//str2,
3212     &                  "fluxsurf_dn_sw",
3213     &                  "W.m-2",2,fluxsurf_dn_sw_tot(:,islope))
3214         ENDDO
3215         call WRITEDIAGFI(ngrid,"fluxtop_lw","fluxtop_lw","W.m-2",2,
3216     &                  fluxtop_lw)
3217         call WRITEDIAGFI(ngrid,"fluxtop_up_sw","fluxtop_up_sw","W.m-2",
3218     &                  2,fluxtop_up_sw_tot)
3219         call WRITEDIAGFI(ngrid,"temp","temperature","K",3,zt)
3220         call WRITEDIAGFI(ngrid,"Sols","Time","sols",0,[zday])
3221         call WRITEDIAGFI(ngrid,"Ls","Solar longitude","deg",
3222     &                    0,[zls*180./pi])
3223         call WRITEDIAGFI(ngrid,"u","Zonal wind","m.s-1",3,zu)
3224         call WRITEDIAGFI(ngrid,"v","Meridional wind","m.s-1",3,zv)
3225         call WRITEDIAGFI(ngrid,"w","Vertical wind","m.s-1",3,pw)
3226         call WRITEDIAGFI(ngrid,"rho","density","kg.m-3",3,rho)
3227c        call WRITEDIAGFI(ngrid,"q2","q2","kg.m-3",3,q2)
3228c        call WRITEDIAGFI(ngrid,'Teta','T potentielle','K',3,zh)
3229         call WRITEDIAGFI(ngrid,"pressure","Pressure","Pa",3,zplay)
3230c        call WRITEDIAGFI(ngrid,"ssurf","Surface stress","N.m-2",2,
3231c    &                  zstress)
3232        call WRITEDIAGFI(ngrid,'sw_htrt','sw heat. rate',
3233     &                   'K/s',3,zdtsw)
3234        call WRITEDIAGFI(ngrid,'lw_htrt','lw heat. rate',
3235     &                   'K/s',3,zdtlw)
3236        call writediagfi(ngrid,"local_time","Local time",
3237     &                   'sol',2,local_time)
3238            if (.not.activice) then
3239               CALL WRITEDIAGFI(ngrid,'tauTESap',
3240     &                         'tau abs 825 cm-1',
3241     &                         '',2,tauTES)
3242             else
3243               CALL WRITEDIAGFI(ngrid,'tauTES',
3244     &                         'tau abs 825 cm-1',
3245     &                         '',2,taucloudtes)
3246             endif
3247#else
3248      !!! this is to ensure correct initialisation of mesoscale model
3249      call WRITEDIAGFI(ngrid,"tsurf","Surface temperature","K",2,
3250     &                 tsurf(:,iflat))
3251      call WRITEDIAGFI(ngrid,"ps","surface pressure","Pa",2,ps)
3252      call WRITEDIAGFI(ngrid,"co2ice","co2 ice thickness","kg.m-2",2,
3253     &                 qsurf(:,igcm_co2,iflat))
3254      call WRITEDIAGFI(ngrid,"temp","temperature","K",3,zt)
3255      call WRITEDIAGFI(ngrid,"u","Zonal wind","m.s-1",3,zu)
3256      call WRITEDIAGFI(ngrid,"v","Meridional wind","m.s-1",3,zv)
3257      call WRITEDIAGFI(ngrid,"emis","Surface emissivity","w.m-1",2,
3258     &                 emis)
3259      call WRITEDIAGFI(ngrid,"tsoil","Soil temperature",
3260     &                 "K",3,tsoil(:,:,iflat))
3261      call WRITEDIAGFI(ngrid,"inertiedat","Soil inertia",
3262     &                 "K",3,inertiedat)
3263#endif
3264
3265c        ----------------------------------------------------------
3266c        Outputs of the CO2 cycle
3267c        ----------------------------------------------------------
3268
3269      if (igcm_co2.ne.0) then
3270        call WRITEDIAGFI(ngrid,"co2","co2 mass mixing ratio",
3271     &                   "kg.kg-1",3,zq(:,:,igcm_co2))
3272
3273        if (co2clouds) then
3274          call WRITEDIAGFI(ngrid,'ccnqco2','CCNco2 mmr',
3275     &                     'kg.kg-1',3,zq(:,:,igcm_ccnco2_mass))
3276
3277          call WRITEDIAGFI(ngrid,'ccnNco2','CCNco2 number',
3278     &                     'part.kg-1',3,zq(:,:,igcm_ccnco2_number))
3279
3280          call WRITEDIAGFI(ngrid,'co2_ice','co2_ice mmr in atm',
3281     &                     'kg.kg-1', 3, zq(:,:,igcm_co2_ice))
3282
3283         call WRITEDIAGFI(ngrid,"mtotco2","total mass atm of co2",
3284     &                    "kg.m-2",2, mtotco2)
3285         call WRITEDIAGFI(ngrid,"icetotco2","total mass atm of co2 ice",
3286     &                    "kg.m-2", 2, icetotco2)
3287         call WRITEDIAGFI(ngrid,"vaptotco2","total mass atm of co2 "//
3288     &                    "vapor","kg.m-2", 2, vaptotco2)
3289         call WRITEDIAGFI(ngrid,"emis","Surface emissivity","w.m-1",2,
3290     &                  emis)
3291         if (co2useh2o) then
3292           call WRITEDIAGFI(ngrid,'ccnqco2_h2o_m_ice',
3293     &                      'CCNco2_h2o_mass_ice mmr',
3294     &                    'kg.kg-1',3,zq(:,:,igcm_ccnco2_h2o_mass_ice))
3295
3296           call WRITEDIAGFI(ngrid,'ccnqco2_h2o_m_ccn',
3297     &                      'CCNco2_h2o_mass_ccn mmr',
3298     &                   'kg.kg-1',3,zq(:,:,igcm_ccnco2_h2o_mass_ccn))
3299
3300           call WRITEDIAGFI(ngrid,'ccnNco2_h2o','CCNco2_h2o number',
3301     &                   'part.kg-1',3,zq(:,:,igcm_ccnco2_h2o_number))
3302         end if
3303
3304           if (meteo_flux) then
3305          call WRITEDIAGFI(ngrid,'ccnqco2_meteor','CCNco2_meteor mmr',
3306     &                  'kg.kg-1',3,zq(:,:,igcm_ccnco2_meteor_mass))
3307
3308        call WRITEDIAGFI(ngrid,'ccnNco2_meteor','CCNco2_meteor number',
3309     &                'part.kg-1',3,zq(:,:,igcm_ccnco2_meteor_number))
3310           end if
3311
3312        end if ! of if (co2clouds)
3313      end if ! of if (igcm_co2.ne.0)
3314
3315      ! Output He tracer, if there is one
3316      if (igcm_he.ne.0) then
3317        call WRITEDIAGFI(ngrid,"he","helium mass mixing ratio",
3318     &                   "kg/kg",3,zq(1,1,igcm_he))
3319        vmr = zq(1:ngrid,1:nlayer,igcm_he)
3320     &        * mmean(1:ngrid,1:nlayer)/mmol(igcm_he)
3321        call WRITEDIAGFI(ngrid,'vmr_he','helium vol. mixing ratio',
3322     &                   'mol/mol',3,vmr)
3323      end if
3324
3325c        ----------------------------------------------------------
3326c        Outputs of the water cycle
3327c        ----------------------------------------------------------
3328        if (water) then
3329#ifdef MESOINI
3330            !!!! waterice = q01, voir readmeteo.F90
3331          call WRITEDIAGFI(ngrid,'q01',noms(igcm_h2o_ice),
3332     &                     'kg/kg',3,
3333     &                     zq(1:ngrid,1:nlayer,igcm_h2o_ice))
3334            !!!! watervapor = q02, voir readmeteo.F90
3335          call WRITEDIAGFI(ngrid,'q02',noms(igcm_h2o_vap),
3336     &                     'kg/kg',3,
3337     &                     zq(1:ngrid,1:nlayer,igcm_h2o_vap))
3338            !!!! surface waterice qsurf02 (voir readmeteo)
3339          call WRITEDIAGFI(ngrid,'qsurf02','surface tracer',
3340     &                     'kg.m-2',2,
3341     &                     qsurf(1:ngrid,igcm_h2o_ice,iflat))
3342         do islope=1,nslope
3343          write(str2(1:2),'(i2.2)') islope
3344          call WRITEDIAGFI(ngrid,'qsurf02_slope'//str2,
3345     &         'surface tracer','kg.m-2',2,
3346     &                     qsurf(1:ngrid,igcm_h2o_ice,islope))
3347         ENDDO
3348#endif
3349          call WRITEDIAGFI(ngrid,'mtot',
3350     &                     'total mass of water vapor',
3351     &                     'kg/m2',2,mtot)
3352          call WRITEDIAGFI(ngrid,'icetot',
3353     &                     'total mass of water ice',
3354     &                     'kg/m2',2,icetot)
3355          vmr = zq(1:ngrid,1:nlayer,igcm_h2o_ice)
3356     &          * mmean(1:ngrid,1:nlayer)/mmol(igcm_h2o_ice)
3357          call WRITEDIAGFI(ngrid,'vmr_h2oice','h2o ice vmr',
3358     &                     'mol/mol',3,vmr)
3359          vmr = zq(1:ngrid,1:nlayer,igcm_h2o_vap)
3360     &          * mmean(1:ngrid,1:nlayer)/mmol(igcm_h2o_vap)
3361          call WRITEDIAGFI(ngrid,'vmr_h2ovap','h2o vap vmr',
3362     &                     'mol/mol',3,vmr)
3363          call WRITEDIAGFI(ngrid,'reffice',
3364     &                     'Mean reff',
3365     &                     'm',2,rave)
3366          call WRITEDIAGFI(ngrid,'h2o_ice','h2o_ice','kg/kg',
3367     &                     3,zq(:,:,igcm_h2o_ice))
3368          call WRITEDIAGFI(ngrid,'h2o_vap','h2o_vap','kg/kg',
3369     &                     3,zq(:,:,igcm_h2o_vap))
3370
3371            if (hdo) then
3372            vmr=zq(1:ngrid,1:nlayer,igcm_hdo_ice)
3373     &      *mmean(1:ngrid,1:nlayer)/mmol(igcm_hdo_ice)
3374            CALL WRITEDIAGFI(ngrid,'vmr_hdoice','hdo ice vmr',
3375     &                       'mol/mol',3,vmr)
3376            vmr=zq(1:ngrid,1:nlayer,igcm_hdo_vap)
3377     &      *mmean(1:ngrid,1:nlayer)/mmol(igcm_hdo_vap)
3378            CALL WRITEDIAGFI(ngrid,'vmr_hdovap','hdo vap vmr',
3379     &                       'mol/mol',3,vmr)
3380            call WRITEDIAGFI(ngrid,'hdo_ice','hdo_ice','kg/kg',
3381     &             3,zq(:,:,igcm_hdo_ice))
3382            call WRITEDIAGFI(ngrid,'hdo_vap','hdo_vap','kg/kg',
3383     &             3,zq(:,:,igcm_hdo_vap))
3384
3385            CALL WRITEDIAGFI(ngrid,'mtotD',
3386     &                       'total mass of HDO vapor',
3387     &                       'kg/m2',2,mtotD)
3388            CALL WRITEDIAGFI(ngrid,'icetotD',
3389     &                       'total mass of HDO ice',
3390     &                       'kg/m2',2,icetotD)
3391
3392C           Calculation of the D/H ratio
3393            do l=1,nlayer
3394                do ig=1,ngrid
3395                if (zq(ig,l,igcm_h2o_vap).gt.qperemin) then
3396                    DoH_vap(ig,l) = ( zq(ig,l,igcm_hdo_vap)/
3397     &              zq(ig,l,igcm_h2o_vap) )*1./(2.*155.76e-6)
3398                else
3399                    DoH_vap(ig,l) = 0.
3400                endif
3401                enddo
3402            enddo
3403
3404            do l=1,nlayer
3405                do ig=1,ngrid
3406                if (zq(ig,l,igcm_h2o_ice).gt.qperemin) then
3407                    DoH_ice(ig,l) = ( zq(ig,l,igcm_hdo_ice)/
3408     &                  zq(ig,l,igcm_h2o_ice) )/(2.*155.76e-6)
3409                else
3410                    DoH_ice(ig,l) = 0.
3411                endif
3412                enddo
3413            enddo
3414
3415            CALL WRITEDIAGFI(ngrid,'DoH_vap',
3416     &                       'D/H ratio in vapor',
3417     &                       ' ',3,DoH_vap)
3418            CALL WRITEDIAGFI(ngrid,'DoH_ice',
3419     &                       'D/H ratio in ice',
3420     &                       '',3,DoH_ice)
3421
3422            endif !hdo
3423
3424!A. Pottier
3425             CALL WRITEDIAGFI(ngrid,'rmoym',
3426     &                      'alternative reffice',
3427     &                      'm',2,rave2)
3428            call WRITEDIAGFI(ngrid,'saturation',
3429     &           'h2o vap saturation ratio','dimless',3,satu)
3430            if (scavenging) then
3431              CALL WRITEDIAGFI(ngrid,"Nccntot",
3432     &                    "condensation nuclei","Nbr/m2",
3433     &                    2,Nccntot)
3434              CALL WRITEDIAGFI(ngrid,"Mccntot",
3435     &                    "mass condensation nuclei","kg/m2",
3436     &                    2,Mccntot)
3437            endif
3438            call WRITEDIAGFI(ngrid,'rice','Ice particle size',
3439     &                       'm',3,rice)
3440            call WRITEDIAGFI(ngrid,'h2o_ice_s',
3441     &                       'surface h2o_ice',
3442     &            'kg.m-2',2,qsurf(1,igcm_h2o_ice,iflat))
3443         do islope=1,nslope
3444          write(str2(1:2),'(i2.2)') islope
3445            call WRITEDIAGFI(ngrid,'h2o_ice_s_slope'//str2,
3446     &                       'surface h2o_ice',
3447     &             'kg.m-2',2,qsurf(1,igcm_h2o_ice,islope))
3448         ENDDO
3449            if (hdo) then
3450            call WRITEDIAGFI(ngrid,'hdo_ice_s',
3451     &                       'surface hdo_ice',
3452     &           'kg.m-2',2,qsurf(1,igcm_hdo_ice,iflat))
3453         do islope=1,nslope
3454          write(str2(1:2),'(i2.2)') islope
3455            call WRITEDIAGFI(ngrid,'hdo_ice_s_slope'//str2,
3456     &                       'surface hdo_ice',
3457     &           'kg.m-2',2,qsurf(1,igcm_hdo_ice,islope))
3458         ENDDO
3459
3460                do ig=1,ngrid
3461                if (qsurf_meshavg(ig,igcm_h2o_ice).gt.qperemin) then
3462           DoH_surf(ig) = 0.5*( qsurf_meshavg(ig,igcm_hdo_ice)/
3463     &          qsurf_meshavg(ig,igcm_h2o_ice) )/155.76e-6
3464                else
3465                    DoH_surf(ig) = 0.
3466                endif
3467                enddo
3468
3469            call WRITEDIAGFI(ngrid,'DoH_surf',
3470     &                       'surface D/H',
3471     &                       '',2,DoH_surf)
3472            endif ! hdo
3473
3474            CALL WRITEDIAGFI(ngrid,'albedo',
3475     &                         'albedo',
3476     &                         '',2,albedo(1,1,iflat))
3477         do islope=1,nslope
3478          write(str2(1:2),'(i2.2)') islope
3479            CALL WRITEDIAGFI(ngrid,'albedo_slope'//str2,
3480     &                         'albedo',
3481     &                         '',2,albedo(1,1,islope))
3482         ENDDO
3483              if (tifeedback) then
3484                 call WRITEDIAGSOIL(ngrid,"soiltemp",
3485     &                              "Soil temperature","K",
3486     &                              3,tsoil)
3487                 call WRITEDIAGSOIL(ngrid,'soilti',
3488     &                       'Soil Thermal Inertia',
3489     &         'J.s-1/2.m-2.K-1',3,inertiesoil(:,:,iflat))
3490         do islope=1,nslope
3491          write(str2(1:2),'(i2.2)') islope
3492                 call WRITEDIAGSOIL(ngrid,'soilti_slope'//str2,
3493     &                       'Soil Thermal Inertia',
3494     &          'J.s-1/2.m-2.K-1',3,inertiesoil(:,:,islope))
3495         ENDDO
3496              endif
3497!A. Pottier
3498          if (CLFvarying) then !AP14 nebulosity
3499            call WRITEDIAGFI(ngrid,'totcloudfrac',
3500     &                       'Total cloud fraction',
3501     &                       ' ',2,totcloudfrac)
3502          end if !clf varying
3503        end if !(water)
3504
3505        if (water.and..not.photochem) then
3506          iq = nq
3507c          write(str2(1:2),'(i2.2)') iq
3508c          call WRITEDIAGFI(ngrid,'dqs'//str2,'dqscloud',
3509c    &                      'kg.m-2',2,zdqscloud(1,iq))
3510c          call WRITEDIAGFI(ngrid,'dqch'//str2,'var chim',
3511c    &                      'kg/kg',3,zdqchim(1,1,iq))
3512c          call WRITEDIAGFI(ngrid,'dqd'//str2,'var dif',
3513c    &                      'kg/kg',3,zdqdif(1,1,iq))
3514c          call WRITEDIAGFI(ngrid,'dqa'//str2,'var adj',
3515c    &                      'kg/kg',3,zdqadj(1,1,iq))
3516c          call WRITEDIAGFI(ngrid,'dqc'//str2,'var c',
3517c    &                      'kg/kg',3,zdqc(1,1,iq))
3518        end if  !(water.and..not.photochem)
3519
3520c        ----------------------------------------------------------
3521c        Outputs of the dust cycle
3522c        ----------------------------------------------------------
3523
3524      call WRITEDIAGFI(ngrid,'tau_pref_scenario',
3525     &                 'Prescribed visible dust optical depth at 610Pa',
3526     &                 'NU',2,tau_pref_scenario)
3527
3528      call WRITEDIAGFI(ngrid,'tau_pref_gcm',
3529     &                 'Visible dust optical depth at 610Pa in the GCM',
3530     &                 'NU',2,tau_pref_gcm)
3531     
3532      if (reff_driven_IRtoVIS_scenario) then
3533             call WRITEDIAGFI(ngrid,'IRtoVIScoef',
3534     &  'conversion coeff for dust tau from abs9.3um to ext0.67um',
3535     &                        '/',2,IRtoVIScoef)
3536      endif
3537
3538      if (dustbin.ne.0) then
3539
3540        call WRITEDIAGFI(ngrid,'tau','taudust','SI',2,tau(1,1))
3541
3542#ifndef MESOINI
3543           if (doubleq) then
3544c            call WRITEDIAGFI(ngrid,'qsurf','qsurf',
3545c     &                       'kg.m-2',2,qsurf(1,igcm_dust_mass))
3546c            call WRITEDIAGFI(ngrid,'Nsurf','N particles',
3547c     &                       'N.m-2',2,qsurf(1,igcm_dust_number))
3548c            call WRITEDIAGFI(ngrid,'dqsdev','ddevil lift',
3549c    &                       'kg.m-2.s-1',2,zdqsdev(1,1))
3550c            call WRITEDIAGFI(ngrid,'dqssed','sedimentation',
3551c     &                       'kg.m-2.s-1',2,zdqssed(1,1))
3552c            call WRITEDIAGFI(ngrid,'dqsdif','diffusion',
3553c     &                       'kg.m-2.s-1',2,zdqsdif(1,1))
3554c             call WRITEDIAGFI(ngrid,'sedice','sedimented ice',
3555c     &                       'kg.m-2.s-1',2,zdqssed(:,igcm_h2o_ice))
3556c             call WRITEDIAGFI(ngrid,'subice','sublimated ice',
3557c     &                       'kg.m-2.s-1',2,zdqsdif(:,igcm_h2o_ice))
3558             call WRITEDIAGFI(ngrid,'dqsdust',
3559     &                        'deposited surface dust mass',
3560     &                        'kg.m-2.s-1',2,dqdustsurf)
3561             call WRITEDIAGFI(ngrid,'dqndust',
3562     &                        'deposited surface dust number',
3563     &                        'number.m-2.s-1',2,dndustsurf)
3564             call WRITEDIAGFI(ngrid,'reffdust','reffdust',
3565     &                        'm',3,rdust*ref_r0)
3566             call WRITEDIAGFI(ngrid,'dustq','Dust mass mr',
3567     &                        'kg/kg',3,qdust)
3568             call WRITEDIAGFI(ngrid,'dustN','Dust number',
3569     &                        'part/kg',3,ndust)
3570
3571             select case (trim(dustiropacity))
3572              case ("tes")
3573               call WRITEDIAGFI(ngrid,'dsodust',
3574     &  'density scaled extinction opacity of std dust at 9.3um(TES)',
3575     &         'm2.kg-1',3,dsodust)
3576               call WRITEDIAGFI(ngrid,'dso',
3577     &  'density scaled extinction opacity of all dust at 9.3um(TES)',
3578     &         'm2.kg-1',3,dsodust+dsords+dsotop)
3579              case ("mcs")
3580               call WRITEDIAGFI(ngrid,'dsodust',
3581     &  'density scaled extinction opacity of std dust at 21.6um(MCS)',
3582     &         'm2.kg-1',3,dsodust)
3583               call WRITEDIAGFI(ngrid,'dso',
3584     &  'density scaled extinction opacity of all dust at 21.6um(MCS)',
3585     &         'm2.kg-1',3,dsodust+dsords+dsotop)
3586             end select
3587           else ! (doubleq=.false.)
3588             do iq=1,dustbin
3589               write(str2(1:2),'(i2.2)') iq
3590               call WRITEDIAGFI(ngrid,'q'//str2,'mix. ratio',
3591     &                         'kg/kg',3,zq(1,1,iq))
3592               call WRITEDIAGFI(ngrid,'qsurf'//str2,'qsurf',
3593     &                         'kg.m-2',2,qsurf(1,iq,iflat))
3594         do islope=1,nslope
3595          write(str2(1:2),'(i2.2)') islope
3596               call WRITEDIAGFI(ngrid,'qsurf_slope'//str2,'qsurf',
3597     &                         'kg.m-2',2,qsurf(1,iq,islope))
3598         ENDDO
3599             end do
3600           endif ! (doubleq)
3601
3602           if (rdstorm) then  ! writediagfi tendencies stormdust tracers
3603             call WRITEDIAGFI(ngrid,'reffstormdust','reffstormdust',
3604     &                        'm',3,rstormdust*ref_r0)
3605             call WRITEDIAGFI(ngrid,'mstormdtot',
3606     &                        'total mass of stormdust only',
3607     &                        'kg.m-2',2,mstormdtot)
3608             call WRITEDIAGFI(ngrid,'mdusttot',
3609     &                        'total mass of dust only',
3610     &                        'kg.m-2',2,mdusttot)
3611             call WRITEDIAGFI(ngrid,'rdsdqsdust',
3612     &                        'deposited surface stormdust mass',
3613     &                        'kg.m-2.s-1',2,rdsdqdustsurf)
3614             call WRITEDIAGFI(ngrid,'rdsdustq','storm Dust mass mr',
3615     &                        'kg/kg',3,rdsqdust)
3616             call WRITEDIAGFI(ngrid,'rdsdustqmodel','storm Dust massmr',
3617     &                        'kg/kg',3,pq(:,:,igcm_stormdust_mass))
3618             call WRITEDIAGFI(ngrid,'rdsdustN','storm Dust number',
3619     &                        'part/kg',3,rdsndust)
3620             call WRITEDIAGFI(ngrid,"stormfract",
3621     &                "fraction of the mesh, with stormdust","none",
3622     &                                               2,totstormfract)
3623             call WRITEDIAGFI(ngrid,'qsurf',
3624     &                 'stormdust injection',
3625     &                 'kg.m-2',2,qsurf(:,igcm_stormdust_mass,iflat))
3626         do islope=1,nslope
3627          write(str2(1:2),'(i2.2)') islope
3628             call WRITEDIAGFI(ngrid,'qsurf_slope'//str2,
3629     &                 'stormdust injection',
3630     &          'kg.m-2',2,qsurf(:,igcm_stormdust_mass,islope))
3631         ENDDO
3632             call WRITEDIAGFI(ngrid,'pdqsurf',
3633     &                  'tendancy stormdust mass at surface',
3634     &          'kg.m-2',2,dqsurf(:,igcm_stormdust_mass,iflat))
3635         do islope=1,nslope
3636          write(str2(1:2),'(i2.2)') islope
3637             call WRITEDIAGFI(ngrid,'pdqsurf_slope'//str2,
3638     &                  'tendancy stormdust mass at surface',
3639     &            'kg.m-2',2,dqsurf(:,igcm_stormdust_mass,islope))
3640         ENDDO
3641             call WRITEDIAGFI(ngrid,'wspeed','vertical speed stormdust',
3642     &                        'm/s',3,wspeed(:,1:nlayer))
3643             call WRITEDIAGFI(ngrid,'zdqsed_dustq'
3644     &          ,'sedimentation q','kg.m-2.s-1',3,
3645     &          zdqsed(:,:,igcm_dust_mass))
3646             call WRITEDIAGFI(ngrid,'zdqssed_dustq'
3647     &          ,'sedimentation q','kg.m-2.s-1',2,
3648     &          zdqssed(:,igcm_dust_mass))
3649             call WRITEDIAGFI(ngrid,'zdqsed_rdsq'
3650     &          ,'sedimentation q','kg.m-2.s-1',3,
3651     &          zdqsed(:,:,igcm_stormdust_mass))
3652             call WRITEDIAGFI(ngrid,'rdust','rdust',
3653     &                        'm',3,rdust)
3654             call WRITEDIAGFI(ngrid,'rstormdust','rstormdust',
3655     &                        'm',3,rstormdust)
3656             call WRITEDIAGFI(ngrid,'totaldustq','total dust mass',
3657     &                        'kg/kg',3,qdusttotal)
3658
3659             select case (trim(dustiropacity))
3660              case ("tes")
3661               call WRITEDIAGFI(ngrid,'dsords',
3662     &  'density scaled extinction opacity of stormdust at 9.3um(TES)',
3663     &         'm2.kg-1',3,dsords)
3664              case ("mcs")
3665               call WRITEDIAGFI(ngrid,'dsords',
3666     &  'density scaled extinction opacity of stormdust at 21.6um(MCS)',
3667     &         'm2.kg-1',3,dsords)
3668             end select
3669           endif ! (rdstorm)
3670
3671           if (topflows) then
3672             call WRITEDIAGFI(ngrid,'refftopdust','refftopdust',
3673     &                        'm',3,rtopdust*ref_r0)
3674             call WRITEDIAGFI(ngrid,'topdustq','top Dust mass mr',
3675     &                        'kg/kg',3,pq(:,:,igcm_topdust_mass))
3676             call WRITEDIAGFI(ngrid,'topdustN','top Dust number',
3677     &                        'part/kg',3,pq(:,:,igcm_topdust_number))
3678             select case (trim(dustiropacity))
3679              case ("tes")
3680               call WRITEDIAGFI(ngrid,'dsotop',
3681     &  'density scaled extinction opacity of topdust at 9.3um(TES)',
3682     &         'm2.kg-1',3,dsotop)
3683              case ("mcs")
3684               call WRITEDIAGFI(ngrid,'dsotop',
3685     &  'density scaled extinction opacity of topdust at 21.6um(MCS)',
3686     &         'm2.kg-1',3,dsotop)
3687             end select
3688           endif ! (topflows)
3689
3690           if (dustscaling_mode==2) then
3691             call writediagfi(ngrid,"dust_rad_adjust",
3692     &            "radiative adjustment coefficient for dust",
3693     &                        "",2,dust_rad_adjust)
3694           endif
3695
3696           if (scavenging) then
3697             call WRITEDIAGFI(ngrid,'ccnq','CCN mass mr',
3698     &                        'kg/kg',3,qccn)
3699             call WRITEDIAGFI(ngrid,'ccnN','CCN number',
3700     &                        'part/kg',3,nccn)
3701             call WRITEDIAGFI(ngrid,'surfccnq','Surf nuclei mass mr',
3702     &                        'kg.m-2',2,qsurf(1,igcm_ccn_mass,iflat))
3703         do islope=1,nslope
3704          write(str2(1:2),'(i2.2)') islope
3705             call WRITEDIAGFI(ngrid,'surfccnq_slope'//str2,
3706     &               'Surf nuclei mass mr',
3707     &               'kg.m-2',2,qsurf(1,igcm_ccn_mass,islope))
3708         ENDDO
3709             call WRITEDIAGFI(ngrid,'surfccnN','Surf nuclei number',
3710     &                        'kg.m-2',2,qsurf(1,igcm_ccn_number,iflat))
3711         do islope=1,nslope
3712          write(str2(1:2),'(i2.2)') islope
3713             call WRITEDIAGFI(ngrid,'surfccnN_slope'//str2,
3714     &                 'Surf nuclei number',
3715     &                 'kg.m-2',2,qsurf(1,igcm_ccn_number,islope))
3716         ENDDO
3717           endif ! (scavenging)
3718
3719c          if (submicron) then
3720c            call WRITEDIAGFI(ngrid,'dustsubm','subm mass mr',
3721c    &                        'kg/kg',3,pq(1,1,igcm_dust_submicron))
3722c          endif ! (submicron)
3723
3724#else
3725!     !!! to initialize mesoscale we need scaled variables
3726!     !!! because this must correspond to starting point for tracers
3727!             call WRITEDIAGFI(ngrid,'dustq','Dust mass mr',
3728!     &           'kg/kg',3,pq(1:ngrid,1:nlayer,igcm_dust_mass))
3729!             call WRITEDIAGFI(ngrid,'dustN','Dust number',
3730!     &           'part/kg',3,pq(1:ngrid,1:nlayer,igcm_dust_number))
3731!             call WRITEDIAGFI(ngrid,'ccn','Nuclei mass mr',
3732!     &           'kg/kg',3,pq(1:ngrid,1:nlayer,igcm_ccn_mass))
3733!             call WRITEDIAGFI(ngrid,'ccnN','Nuclei number',
3734!     &           'part/kg',3,pq(1:ngrid,1:nlayer,igcm_ccn_number))
3735      if (freedust) then
3736             call WRITEDIAGFI(ngrid,'dustq','Dust mass mr',
3737     &                        'kg/kg',3,qdust)
3738             call WRITEDIAGFI(ngrid,'dustN','Dust number',
3739     &                        'part/kg',3,ndust)
3740             call WRITEDIAGFI(ngrid,'ccn','CCN mass mr',
3741     &                        'kg/kg',3,qccn)
3742             call WRITEDIAGFI(ngrid,'ccnN','CCN number',
3743     &                        'part/kg',3,nccn)
3744      else
3745             call WRITEDIAGFI(ngrid,'dustq','Dust mass mr',
3746     &                        'kg/kg',3,pq(1,1,igcm_dust_mass))
3747             call WRITEDIAGFI(ngrid,'dustN','Dust number',
3748     &                        'part/kg',3,pq(1,1,igcm_dust_number))
3749             call WRITEDIAGFI(ngrid,'ccn','Nuclei mass mr',
3750     &                        'kg/kg',3,pq(1,1,igcm_ccn_mass))
3751             call WRITEDIAGFI(ngrid,'ccnN','Nuclei number',
3752     &                        'part/kg',3,pq(1,1,igcm_ccn_number))
3753      endif
3754#endif
3755
3756         end if  ! (dustbin.ne.0)
3757
3758c        ----------------------------------------------------------
3759c        GW non-oro outputs
3760c        ----------------------------------------------------------
3761
3762         if(calllott_nonoro) then
3763           call WRITEDIAGFI(ngrid,"dugwno","GW non-oro dU","m/s2",
3764     $           3,d_u_hin/ptimestep)
3765           call WRITEDIAGFI(ngrid,"dvgwno","GW non-oro dV","m/s2",
3766     $           3,d_v_hin/ptimestep)
3767         endif                  !(calllott_nonoro)
3768
3769c        ----------------------------------------------------------
3770c        Thermospheric outputs
3771c        ----------------------------------------------------------
3772
3773         if(callthermos) then
3774
3775            call WRITEDIAGFI(ngrid,"q15um","15 um cooling","K/s",
3776     $           3,zdtnlte)
3777            call WRITEDIAGFI(ngrid,"quv","UV heating","K/s",
3778     $           3,zdteuv)
3779            call WRITEDIAGFI(ngrid,"cond","Thermal conduction","K/s",
3780     $           3,zdtconduc)
3781            call WRITEDIAGFI(ngrid,"qnir","NIR heating","K/s",
3782     $           3,zdtnirco2)
3783
3784            !H, H2 and D escape fluxes
3785
3786            call WRITEDIAGFI(ngrid,"PhiH","H escape flux","s-1",
3787     $           0,[PhiEscH])
3788            call WRITEDIAGFI(ngrid,"PhiH2","H2 escape flux","s-1",
3789     $           0,[PhiEscH2])
3790            call WRITEDIAGFI(ngrid,"PhiD","D escape flux","s-1",
3791     $           0,[PhiEscD])
3792
3793!            call wstats(ngrid,"PhiH","H escape flux","s-1",
3794!     $           0,[PhiEscH])
3795!            call wstats(ngrid,"PhiH2","H2 escape flux","s-1",
3796!     $           0,[PhiEscH2])
3797!            call wstats(ngrid,"PhiD","D escape flux","s-1",
3798!     $           0,[PhiEscD])
3799           
3800!            call wstats(ngrid,"q15um","15 um cooling","K/s",
3801!     $           3,zdtnlte)
3802!            call wstats(ngrid,"quv","UV heating","K/s",
3803!     $           3,zdteuv)
3804!            call wstats(ngrid,"cond","Thermal conduction","K/s",
3805!     $           3,zdtconduc)
3806!            call wstats(ngrid,"qnir","NIR heating","K/s",
3807!     $           3,zdtnirco2)
3808
3809         endif  !(callthermos)
3810
3811            call WRITEDIAGFI(ngrid,"q15um","15 um cooling","K/s",
3812     $           3,zdtnlte)
3813            call WRITEDIAGFI(ngrid,"qnir","NIR heating","K/s",
3814     $           3,zdtnirco2)
3815
3816c        ----------------------------------------------------------
3817c        ----------------------------------------------------------
3818c        PBL OUTPUS
3819c        ----------------------------------------------------------
3820c        ----------------------------------------------------------
3821
3822c        ----------------------------------------------------------
3823c        Outputs of thermals
3824c        ----------------------------------------------------------
3825      if (calltherm) then
3826!        call WRITEDIAGFI(ngrid,'dtke',
3827!     &                   'tendance tke thermiques','m**2/s**2',
3828!     &                   3,dtke_th)
3829!        call WRITEDIAGFI(ngrid,'d_u_ajs',
3830!     &                   'tendance u thermiques','m/s',
3831!     &                   3,pdu_th*ptimestep)
3832!        call WRITEDIAGFI(ngrid,'d_v_ajs',
3833!     &                   'tendance v thermiques','m/s',
3834!     &                   3,pdv_th*ptimestep)
3835!          if (nq .eq. 2) then
3836!            call WRITEDIAGFI(ngrid,'deltaq_th',
3837!     &                       'delta q thermiques','kg/kg',
3838!     &                       3,ptimestep*pdq_th(:,:,2))
3839!          end if
3840
3841        call WRITEDIAGFI(ngrid,'zmax_th',
3842     &                   'hauteur du thermique','m',
3843     &                    2,zmax_th)
3844        call WRITEDIAGFI(ngrid,'hfmax_th',
3845     &                   'maximum TH heat flux','K.m/s',
3846     &                   2,hfmax_th)
3847        call WRITEDIAGFI(ngrid,'wstar',
3848     &                   'maximum TH vertical velocity','m/s',
3849     &                   2,wstar)
3850      end if
3851
3852c        ----------------------------------------------------------
3853c        ----------------------------------------------------------
3854c        END OF PBL OUTPUS
3855c        ----------------------------------------------------------
3856c        ----------------------------------------------------------
3857
3858
3859c        ----------------------------------------------------------
3860c        Output in netcdf file "diagsoil.nc" for subterranean
3861c          variables (output every "ecritphy", as for writediagfi)
3862c        ----------------------------------------------------------
3863
3864         ! Write soil temperature
3865        call writediagsoil(ngrid,"soiltemp","Soil temperature","K",
3866     &                     3,tsoil(:,:,iflat))
3867         do islope=1,nslope
3868          write(str2(1:2),'(i2.2)') islope
3869        call writediagsoil(ngrid,"soiltemp_slope"//str2,
3870     &                     "Soil temperature","K",
3871     &                     3,tsoil(:,:,islope))
3872         ENDDO
3873         ! Write surface temperature
3874!        call writediagsoil(ngrid,"tsurf","Surface temperature","K",
3875!    &                     2,tsurf)
3876
3877c        ==========================================================
3878c        END OF WRITEDIAGFI
3879c        ==========================================================
3880#endif
3881! of ifdef MESOSCALE
3882
3883      ELSE     ! if(ngrid.eq.1)
3884
3885#ifndef MESOSCALE
3886         write(*,
3887     &    '("Ls =",f11.6," tau_pref_scenario(",f4.0," Pa) =",f9.6)')
3888     &    zls*180./pi,odpref,tau_pref_scenario
3889c      ----------------------------------------------------------------------
3890c      Output in grads file "g1d" (ONLY when using testphys1d)
3891c      (output at every X physical timestep)
3892c      ----------------------------------------------------------------------
3893c
3894c        CALL writeg1d(ngrid,1,fluxsurf_lw,'Fs_ir','W.m-2')
3895c        CALL writeg1d(ngrid,1,tsurf,'tsurf','K')
3896c        CALL writeg1d(ngrid,1,ps,'ps','Pa')
3897c        CALL writeg1d(ngrid,nlayer,zt,'T','K')
3898c        CALL writeg1d(ngrid,nlayer,pu,'u','m.s-1')
3899c        CALL writeg1d(ngrid,nlayer,pv,'v','m.s-1')
3900c        CALL writeg1d(ngrid,nlayer,pw,'w','m.s-1')
3901        call writediagsoil(ngrid,"soiltemp","Soil temperature","K",
3902     &                     3,tsoil(:,:,iflat))
3903         do islope=1,nslope
3904          write(str2(1:2),'(i2.2)') islope
3905        call writediagsoil(ngrid,"soiltemp_slope"//str2,
3906     &                     "Soil temperature","K",
3907     &                     3,tsoil(:,:,islope))
3908         ENDDO
3909
3910! THERMALS STUFF 1D
3911         if(calltherm) then
3912
3913        call WRITEDIAGFI(ngrid,'lmax_th',
3914     &              'hauteur du thermique','point',
3915     &                         0,lmax_th_out)
3916        call WRITEDIAGFI(ngrid,'zmax_th',
3917     &              'hauteur du thermique','m',
3918     &                         0,zmax_th)
3919        call WRITEDIAGFI(ngrid,'hfmax_th',
3920     &              'maximum TH heat flux','K.m/s',
3921     &                         0,hfmax_th)
3922        call WRITEDIAGFI(ngrid,'wstar',
3923     &              'maximum TH vertical velocity','m/s',
3924     &                         0,wstar)
3925
3926         end if ! of if (calltherm)
3927
3928         call WRITEDIAGFI(ngrid,'w','vertical velocity'
3929     &                              ,'m/s',1,pw)
3930         call WRITEDIAGFI(ngrid,"q2","q2","kg.m-3",1,q2)
3931         call WRITEDIAGFI(ngrid,"tsurf","Surface temperature","K",0,
3932     &                  tsurf(:,iflat))
3933         do islope=1,nslope
3934          write(str2(1:2),'(i2.2)') islope
3935         call WRITEDIAGFI(ngrid,"tsurf_slope"//str2,
3936     &             "Surface temperature","K",0,
3937     &                  tsurf(:,islope))
3938         ENDDO
3939         call WRITEDIAGFI(ngrid,"u","u wind","m/s",1,zu)
3940         call WRITEDIAGFI(ngrid,"v","v wind","m/s",1,zv)
3941
3942         call WRITEDIAGFI(ngrid,"pplay","Pressure","Pa",1,zplay)
3943         call WRITEDIAGFI(ngrid,"pplev","Pressure","Pa",1,zplev)
3944         call WRITEDIAGFI(ngrid,"rho","rho","kg.m-3",1,rho)
3945         call WRITEDIAGFI(ngrid,"dtrad","rad. heat. rate",
3946     &              "K.s-1",1,dtrad)
3947         call WRITEDIAGFI(ngrid,'sw_htrt','sw heat. rate',
3948     &                   'w.m-2',1,zdtsw)
3949         call WRITEDIAGFI(ngrid,'lw_htrt','lw heat. rate',
3950     &                   'w.m-2',1,zdtlw)
3951         call WRITEDIAGFI(ngrid,"co2ice","co2 ice thickness"
3952     &       ,"kg.m-2",0,qsurf(:,igcm_co2,iflat))
3953         do islope=1,nslope
3954          write(str2(1:2),'(i2.2)') islope
3955         call WRITEDIAGFI(ngrid,"co2ice_slope"//str2,
3956     &                  "co2 ice thickness"
3957     &       ,"kg.m-2",0,qsurf(:,igcm_co2,islope))
3958         ENDDO
3959
3960         if (igcm_co2.ne.0) then
3961          call co2sat(ngrid*nlayer,zt,zqsatco2)
3962           do ig=1,ngrid
3963            do l=1,nlayer
3964               satuco2(ig,l) = zq(ig,l,igcm_co2)*
3965     &              (mmean(ig,l)/44.01)*zplay(ig,l)/zqsatco2(ig,l)
3966                 
3967c               write(*,*) "In PHYSIQMOD, pt,zt,time ",pt(ig,l)
3968c     &              ,zt(ig,l),ptime
3969            enddo
3970           enddo
3971          endif
3972
3973         call WRITEDIAGFI(ngrid,'ps','Surface pressure','Pa',0,ps)
3974         call WRITEDIAGFI(ngrid,'temp','Temperature ',
3975     &                       'K JA',1,zt)
3976c         call WRITEDIAGFI(ngrid,'temp2','Temperature ',
3977c     &        'K JA2',1,pt)
3978
3979c           CALL writeg1d(ngrid,1,tau,'tau','SI')
3980            do iq=1,nq
3981c              CALL writeg1d(ngrid,nlayer,zq(1,1,iq),noms(iq),'kg/kg')
3982               call WRITEDIAGFI(ngrid,trim(noms(iq)),
3983     &              trim(noms(iq)),'kg/kg',1,zq(1,1,iq))
3984            end do
3985           if (doubleq) then
3986             call WRITEDIAGFI(ngrid,'rdust','rdust',
3987     &                        'm',1,rdust)
3988           endif ! doubleq 1D
3989           if (rdstorm) then
3990             call writediagfi(1,'aerosol_dust','opacity of env. dust',''
3991     &                        ,1,aerosol(:,:,iaer_dust_doubleq))
3992             call writediagfi(1,'aerosol_stormdust',
3993     &                         'opacity of storm dust',''
3994     &                        ,1,aerosol(:,:,iaer_stormdust_doubleq))
3995             call WRITEDIAGFI(ngrid,'dqsdifdustq','diffusion',
3996     &          'kg.m-2.s-1',0,zdqsdif(1,igcm_dust_mass,iflat))
3997         do islope=1,nslope
3998          write(str2(1:2),'(i2.2)') islope
3999             call WRITEDIAGFI(ngrid,'dqsdifdustq_slope'//str2,
4000     &             'diffusion',
4001     &          'kg.m-2.s-1',0,zdqsdif(1,igcm_dust_mass,islope))
4002         ENDDO
4003             call WRITEDIAGFI(ngrid,'dqsdifrdsq','diffusion',
4004     &          'kg.m-2.s-1',0,zdqsdif(1,igcm_stormdust_mass,iflat))
4005         do islope=1,nslope
4006          write(str2(1:2),'(i2.2)') islope
4007             call WRITEDIAGFI(ngrid,'dqsdifrdsq_slope'//str2,
4008     &           'diffusion',
4009     &          'kg.m-2.s-1',0,zdqsdif(1,igcm_stormdust_mass,islope))
4010         ENDDO
4011             call WRITEDIAGFI(ngrid,'mstormdtot',
4012     &                        'total mass of stormdust only',
4013     &                        'kg.m-2',0,mstormdtot)
4014             call WRITEDIAGFI(ngrid,'mdusttot',
4015     &                        'total mass of dust only',
4016     &                        'kg.m-2',0,mdusttot)
4017             call WRITEDIAGFI(ngrid,'tau_pref_scenario',
4018     &                        'Prescribed dust ref opt depth at 610 Pa',
4019     &                        'NU',0,tau_pref_scenario)
4020             call WRITEDIAGFI(ngrid,'tau_pref_gcm',
4021     &                        'Dust ref opt depth at 610 Pa in the GCM',
4022     &                        'NU',0,tau_pref_gcm)
4023             call WRITEDIAGFI(ngrid,'rdsdqsdust',
4024     &                        'deposited surface stormdust mass',
4025     &                        'kg.m-2.s-1',0,rdsdqdustsurf)
4026             call WRITEDIAGFI(ngrid,'rdsdustq','storm Dust mass mr',
4027     &                        'kg/kg',1,rdsqdust)
4028             call WRITEDIAGFI(ngrid,"stormfract",
4029     &                         "fraction of the mesh,with stormdust",
4030     &                         "none",0,totstormfract)
4031             call WRITEDIAGFI(ngrid,'rdsqsurf',
4032     &                 'stormdust at surface',
4033     &                 'kg.m-2',0,qsurf(:,igcm_stormdust_mass,iflat))
4034         do islope=1,nslope
4035          write(str2(1:2),'(i2.2)') islope
4036             call WRITEDIAGFI(ngrid,'rdsqsurf_slope'//str2,
4037     &                 'stormdust at surface',
4038     &                 'kg.m-2',0,qsurf(:,igcm_stormdust_mass,islope))
4039         ENDDO
4040             call WRITEDIAGFI(ngrid,'qsurf',
4041     &                  'dust mass at surface',
4042     &                  'kg.m-2',0,qsurf(:,igcm_dust_mass,iflat))
4043         do islope=1,nslope
4044          write(str2(1:2),'(i2.2)') islope
4045             call WRITEDIAGFI(ngrid,'qsurf_slope'//str2,
4046     &                  'dust mass at surface',
4047     &                  'kg.m-2',0,qsurf(:,igcm_dust_mass,islope))
4048         ENDDO
4049             call WRITEDIAGFI(ngrid,'wspeed','vertical speed stormdust',
4050     &                        'm/s',1,wspeed)
4051             call WRITEDIAGFI(ngrid,'totaldustq','total dust mass',
4052     &                        'kg/kg',1,qdusttotal)
4053             call WRITEDIAGFI(ngrid,'dsords',
4054     &                       'density scaled opacity of stormdust',
4055     &                       'm2.kg-1',1,dsords)
4056             call WRITEDIAGFI(ngrid,'zdqsed_dustq'
4057     &          ,'sedimentation q','kg.m-2.s-1',1,
4058     &          zdqsed(1,:,igcm_dust_mass))
4059             call WRITEDIAGFI(ngrid,'zdqsed_rdsq'
4060     &          ,'sedimentation q','kg.m-2.s-1',1,
4061     &          zdqsed(1,:,igcm_stormdust_mass))
4062           endif !(rdstorm 1D)
4063
4064           if (water.AND.tifeedback) then
4065             call WRITEDIAGFI(ngrid,"soiltemp",
4066     &                              "Soil temperature","K",
4067     &                              1,tsoil)
4068             call WRITEDIAGFI(ngrid,'soilti',
4069     &                       'Soil Thermal Inertia',
4070     &                       'J.s-1/2.m-2.K-1',1,inertiesoil)
4071           endif
4072         
4073cccccccccccccccccc scavenging & water outputs 1D TN ccccccccccccccc
4074ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
4075         IF (water) THEN
4076
4077           if (.not.activice) then
4078
4079             tauTES=0
4080             do l=1,nlayer
4081               Qabsice = min(
4082     &             max(0.4e6*rice(1,l)*(1.+nuice_ref)-0.05 ,0.),1.2
4083     &                      )
4084               opTES(1,l)= 0.75 * Qabsice *
4085     &             zq(1,l,igcm_h2o_ice) *
4086     &             (zplev(1,l) - zplev(1,l+1)) / g
4087     &             / (rho_ice * rice(1,l) * (1.+nuice_ref))
4088               tauTES=tauTES+ opTES(1,l)
4089             enddo
4090             CALL WRITEDIAGFI(ngrid,'tauTESap',
4091     &                         'tau abs 825 cm-1',
4092     &                         '',0,tauTES)
4093           else
4094
4095             CALL WRITEDIAGFI(ngrid,'tauTES',
4096     &                         'tau abs 825 cm-1',
4097     &                         '',0,taucloudtes)
4098           endif
4099     
4100           mtot = 0
4101           icetot = 0
4102           h2otot = qsurf(1,igcm_h2o_ice,iflat)
4103           if (hdo) THEN
4104           mtotD = 0
4105           icetotD = 0
4106           hdotot = qsurf(1,igcm_hdo_ice,iflat)
4107           ENDIF !hdo
4108
4109           do l=1,nlayer
4110             mtot = mtot +  zq(1,l,igcm_h2o_vap)
4111     &                 * (zplev(1,l) - zplev(1,l+1)) / g
4112             icetot = icetot +  zq(1,l,igcm_h2o_ice)
4113     &                 * (zplev(1,l) - zplev(1,l+1)) / g
4114               if (hdo) THEN
4115                 mtotD = mtotD +  zq(1,l,igcm_hdo_vap)
4116     &                 * (zplev(1,l) - zplev(1,l+1)) / g
4117                 icetotD = icetotD +  zq(1,l,igcm_hdo_ice)
4118     &                 * (zplev(1,l) - zplev(1,l+1)) / g
4119               ENDIF !hdo
4120           end do
4121           h2otot = h2otot+mtot+icetot
4122               IF (hdo) then
4123                   hdotot = hdotot+mtotD+icetotD
4124               ENDIF ! hdo
4125
4126
4127             CALL WRITEDIAGFI(ngrid,'h2otot',
4128     &                         'h2otot',
4129     &                         'kg/m2',0,h2otot)
4130             CALL WRITEDIAGFI(ngrid,'mtot',
4131     &                         'mtot',
4132     &                         'kg/m2',0,mtot)
4133             CALL WRITEDIAGFI(ngrid,'icetot',
4134     &                         'icetot',
4135     &                         'kg/m2',0,icetot)
4136             CALL WRITEDIAGFI(ngrid,'albedo',
4137     &                         'albedo',
4138     &                         '',2,albedo(1,1,iflat))
4139         do islope=1,nslope
4140          write(str2(1:2),'(i2.2)') islope
4141             CALL WRITEDIAGFI(ngrid,'albedo_slope'//str2,
4142     &                         'albedo',
4143     &                         '',2,albedo(1,1,islope))
4144         ENDDO
4145             IF (hdo) THEN
4146             CALL WRITEDIAGFI(ngrid,'mtotD',
4147     &                         'mtotD',
4148     &                         'kg/m2',0,mtotD)
4149             CALL WRITEDIAGFI(ngrid,'icetotD',
4150     &                         'icetotD',
4151     &                         'kg/m2',0,icetotD)
4152             CALL WRITEDIAGFI(ngrid,'hdotot',
4153     &                         'hdotot',
4154     &                         'kg/m2',0,hdotot)
4155
4156C           Calculation of the D/H ratio
4157            do l=1,nlayer
4158                if (zq(1,l,igcm_h2o_vap).gt.qperemin) then
4159                    DoH_vap(1,l) = 0.5*( zq(1,l,igcm_hdo_vap)/
4160     &                zq(1,l,igcm_h2o_vap) )/155.76e-6
4161                else
4162                    DoH_vap(1,l) = 0.
4163                endif
4164            enddo
4165
4166            do l=1,nlayer
4167                if (zq(1,l,igcm_h2o_ice).gt.qperemin) then
4168                    DoH_ice(1,l) = 0.5*( zq(1,l,igcm_hdo_ice)/
4169     &                  zq(1,l,igcm_h2o_ice) )/155.76e-6
4170                else
4171                    DoH_ice(1,l) = 0.
4172                endif
4173            enddo
4174
4175            CALL WRITEDIAGFI(ngrid,'DoH_vap',
4176     &                       'D/H ratio in vapor',
4177     &                       ' ',1,DoH_vap)
4178            CALL WRITEDIAGFI(ngrid,'DoH_ice',
4179     &                       'D/H ratio in ice',
4180     &                       '',1,DoH_ice)
4181
4182             ENDIF !Hdo
4183
4184
4185           if (scavenging) then
4186
4187             rave = 0
4188             do l=1,nlayer
4189cccc Column integrated effective ice radius
4190cccc is weighted by total ice surface area (BETTER)
4191             rave = rave + tauscaling(1) *
4192     &              zq(1,l,igcm_ccn_number) *
4193     &              (zplev(1,l) - zplev(1,l+1)) / g *
4194     &              rice(1,l) * rice(1,l)*  (1.+nuice_ref)
4195             enddo
4196             rave=icetot*0.75/max(rave*pi*rho_ice,1.e-30) ! surface weight
4197
4198              Nccntot= 0
4199              call watersat(ngrid*nlayer,zt,zplay,zqsat)
4200               do l=1,nlayer
4201                   Nccntot = Nccntot +
4202     &              zq(1,l,igcm_ccn_number)*tauscaling(1)
4203     &              *(zplev(1,l) - zplev(1,l+1)) / g
4204                   satu(1,l) = zq(1,l,igcm_h2o_vap)/zqsat(1,l)
4205                   satu(1,l) = (max(satu(1,l),float(1))-1)
4206!     &                      * zq(1,l,igcm_h2o_vap) *
4207!     &                      (zplev(1,l) - zplev(1,l+1)) / g
4208               enddo
4209               call WRITEDIAGFI(ngrid,"satu","vap in satu","kg/kg",1,
4210     &                    satu)
4211               CALL WRITEDIAGFI(ngrid,'Nccntot',
4212     &                         'Nccntot',
4213     &                         'nbr/m2',0,Nccntot)
4214
4215             call WRITEDIAGFI(ngrid,'zdqsed_dustq'
4216     & ,'sedimentation q','kg.m-2.s-1',1,zdqsed(1,:,igcm_dust_mass))
4217             call WRITEDIAGFI(ngrid,'zdqsed_dustN'
4218     &,'sedimentation N','Nbr.m-2.s-1',1,
4219     &                                 zdqsed(1,:,igcm_dust_number))
4220
4221           else ! of if (scavenging)
4222
4223cccc Column integrated effective ice radius
4224cccc is weighted by total ice mass         (LESS GOOD)
4225             rave = 0
4226             do l=1,nlayer
4227               rave = rave + zq(1,l,igcm_h2o_ice)
4228     &              * (zplev(1,l) - zplev(1,l+1)) / g
4229     &              *  rice(1,l) * (1.+nuice_ref)
4230             enddo
4231             rave=max(rave/max(icetot,1.e-30),1.e-30) ! mass weight
4232           endif ! of if (scavenging)
4233
4234
4235           CALL WRITEDIAGFI(ngrid,'reffice',
4236     &                      'reffice',
4237     &                      'm',0,rave)
4238
4239          !Alternative A. Pottier weighting
4240           rave2 = 0.
4241           totrave2 = 0.
4242           do l=1,nlayer
4243              rave2 =rave2+ zq(1,l,igcm_h2o_ice)*rice(1,l)
4244              totrave2 = totrave2 + zq(1,l,igcm_h2o_ice)
4245           end do
4246           rave2=max(rave2/max(totrave2,1.e-30),1.e-30)
4247          CALL WRITEDIAGFI(ngrid,'rmoym',
4248     &                      'reffice',
4249     &                      'm',0,rave2)
4250
4251           do iq=1,nq
4252             call WRITEDIAGFI(ngrid,trim(noms(iq))//'_s',
4253     &       trim(noms(iq))//'_s','kg/kg',0,qsurf(1,iq,iflat))
4254           end do
4255     
4256        call WRITEDIAGFI(ngrid,"watercap","Water ice thickness"
4257     &                                  ,"kg.m-2",0,watercap)
4258        call WRITEDIAGFI(ngrid,'zdqcloud_ice','cloud ice',
4259     &            'kg.m-2.s-1',1,zdqcloud(1,:,igcm_h2o_ice))
4260        call WRITEDIAGFI(ngrid,'zdqcloud_vap','cloud vap',
4261     &            'kg.m-2.s-1',1,zdqcloud(1,:,igcm_h2o_vap))
4262        call WRITEDIAGFI(ngrid,'zdqcloud','cloud ice',
4263     &            'kg.m-2.s-1',1,zdqcloud(1,:,igcm_h2o_ice)
4264     &                          +zdqcloud(1,:,igcm_h2o_vap))
4265        IF (hdo) THEN
4266        call WRITEDIAGFI(ngrid,'zdqcloud_iceD','cloud ice hdo',
4267     &            'kg.m-2.s-1',1,zdqcloud(1,:,igcm_hdo_ice))
4268        call WRITEDIAGFI(ngrid,'zdqcloud_vapD','cloud vap hdo',
4269     &            'kg.m-2.s-1',1,zdqcloud(1,:,igcm_hdo_vap))
4270
4271        ENDIF ! hdo
4272
4273        call WRITEDIAGFI(ngrid,"rice","ice radius","m",1,
4274     &                    rice)
4275             
4276              if (CLFvarying) then
4277                 call WRITEDIAGFI(ngrid,'totcloudfrac',
4278     &                'Total cloud fraction',
4279     &                       ' ',0,totcloudfrac)
4280              endif !clfvarying
4281
4282        ENDIF ! of IF (water)
4283         
4284ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
4285ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
4286
4287         zlocal(1)=-log(zplay(1,1)/zplev(1,1))* Rnew(1,1)*zt(1,1)/g
4288
4289         do l=2,nlayer-1
4290            tmean=zt(1,l)
4291            if(zt(1,l).ne.zt(1,l-1))
4292     &        tmean=(zt(1,l)-zt(1,l-1))/log(zt(1,l)/zt(1,l-1))
4293              zlocal(l)= zlocal(l-1)
4294     &        -log(zplay(1,l)/zplay(1,l-1))*rnew(1,l)*tmean/g
4295         enddo
4296         zlocal(nlayer)= zlocal(nlayer-1)-
4297     &                   log(zplay(1,nlayer)/zplay(1,nlayer-1))*
4298     &                   rnew(1,nlayer)*tmean/g
4299
4300
4301#endif
4302
4303      END IF       ! if(ngrid.ne.1)
4304! test for co2 conservation with co2 microphysics
4305      if (igcm_co2_ice.ne.0) then
4306        co2totB = 0. ! added by C.M.
4307        do ig=1,ngrid
4308          do l=1,nlayer
4309            co2totB = co2totB + (zplev(ig,l)-zplev(ig,l+1))/g*
4310     &             (pq(ig,l,igcm_co2)+pq(ig,l,igcm_co2_ice)
4311     &        +(pdq(ig,l,igcm_co2)+pdq(ig,l,igcm_co2_ice))*ptimestep)
4312          enddo
4313          co2totB = co2totB + qsurf(ig,igcm_co2,iflat)
4314        enddo
4315      else
4316        co2totB = 0. ! added by C.M.
4317        do ig=1,ngrid
4318          do l=1,nlayer
4319            co2totB = co2totB + (zplev(ig,l)-zplev(ig,l+1))/g*
4320     &             (pq(ig,l,igcm_co2)+pdq(ig,l,igcm_co2)*ptimestep)
4321          enddo
4322          co2totB = co2totB + qsurf(ig,igcm_co2,iflat)
4323        enddo
4324      endif ! of if (igcm_co2_ice.ne.0)
4325      co2conservation = (co2totA-co2totB)/co2totA
4326        call WRITEDIAGFI(ngrid, 'co2conservation',
4327     &                     'Total CO2 mass conservation in physic',
4328     &                     ' ', 0, co2conservation)
4329! XIOS outputs
4330#ifdef CPP_XIOS     
4331      ! Send fields to XIOS: (NB these fields must also be defined as
4332      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
4333      CALL send_xios_field("ls",zls*180./pi)
4334
4335      CALL send_xios_field("controle",tab_cntrl_mod,1)
4336
4337      CALL send_xios_field("ap",ap,1)
4338      CALL send_xios_field("bp",bp,1)
4339      CALL send_xios_field("aps",aps,1)
4340      CALL send_xios_field("bps",bps,1)
4341
4342      CALL send_xios_field("phisinit",phisfi)
4343     
4344      CALL send_xios_field("ps",ps)
4345      CALL send_xios_field("area",cell_area)
4346
4347!      CALL send_xios_field("ISR",fluxtop_dn_sw_tot)
4348      CALL send_xios_field("OLR",fluxtop_lw)
4349
4350      CALL send_xios_field("tsurf",tsurf(:,iflat))
4351!      CALL send_xios_field("inertiedat",inertiedat)
4352      CALL send_xios_field("tsoil",tsoil(:,:,iflat))
4353      CALL send_xios_field("co2ice",qsurf(:,igcm_co2,iflat))
4354     
4355!      CALL send_xios_field("temp",zt)
4356      CALL send_xios_field("u",zu)
4357      CALL send_xios_field("v",zv)
4358
4359!      CALL send_xios_field("rho",rho)
4360      ! Orographic Gravity waves tendencies
4361!      if (calllott) then
4362!      CALL send_xios_field("dugw",zdugw/ptimestep)
4363!      CALL send_xios_field("dvgw",zdvgw/ptimestep)
4364!      CALL send_xios_field("dtgw",zdtgw/ptimestep)
4365!      endif
4366      !CREATE IF CO2CYCLE
4367!      if (igcm_co2.ne.0) then
4368!         CALL send_xios_field("co2",zq(:,:,igcm_co2))
4369!      endif
4370      ! Water cycle
4371!      if (water) then
4372!         CALL send_xios_field("watercap",watercap)
4373         !CALL send_xios_field("watercaptag",watercaptag)
4374!         CALL send_xios_field("mtot",mtot)
4375!         CALL send_xios_field("icetot",icetot)
4376!         if (igcm_h2o_vap.ne.0 .and. igcm_h2o_ice.ne.0) then
4377!            CALL send_xios_field("h2o_vap",zq(:,:,igcm_h2o_vap))
4378!            CALL send_xios_field("h2o_ice",zq(:,:,igcm_h2o_ice))
4379!         endif
4380!      endif
4381!            if (.not.activice) then
4382!      CALL send_xios_field("tauTESap",tauTES)
4383!             else
4384!      CALL send_xios_field("tauTES",taucloudtes)
4385!             endif
4386
4387!      CALL send_xios_field("h2o_ice_s",qsurf(:,igcm_h2o_ice))
4388
4389
4390      if (lastcall.and.is_omp_master) then
4391        write(*,*) "physiq lastcall: call xios_context_finalize"
4392        call xios_context_finalize
4393      endif
4394#endif
4395
4396      if (check_physics_outputs) then
4397        ! Check the validity of updated fields at the end of the physics step
4398        call check_physics_fields("end of physiq:",zt,zu,zv,zplev,zq)
4399      endif
4400
4401      icount=icount+1
4402
4403      END SUBROUTINE physiq
4404
4405      END MODULE physiq_mod
Note: See TracBrowser for help on using the repository browser.