source: perso/abdelouhab.djerrah/ORCHIDEEE/src_sechiba/intersurf.f90 @ 937

Last change on this file since 937 was 392, checked in by martial.mancip, 13 years ago

Add CHECKTIME parameter to switch print of date informations from sechiba.def.

  • Property svn:keywords set to Revision Date HeadURL Date Author Revision
File size: 275.8 KB
Line 
1
2!! This subroutine is the interface between the main program
3!! (LMDZ or dim2_driver) and SECHIBA.
4!! - Input fields are gathered to keep just continental points
5!! - call sechiba_main That's SECHIBA process.
6!! - Output fields are scattered to complete global fields
7!!
8!! @call sechiba_main
9!! @Version : $Revision$, $Date$
10!!
11!! @author Marie-Alice Foujols and Jan Polcher
12!!
13!< $HeadURL$
14!< $Date$
15!< $Author$
16!< $Revision$
17!! IPSL (2006)
18!!  This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC
19!!
20!f90doc MODULEintersurf
21MODULE intersurf
22
23  USE IOIPSL
24
25  USE defprec
26  USE sechiba
27  USE constantes
28  USE constantes_soil
29  USE constantes_veg
30  USE parallel
31  USE watchout
32  USE solar
33  USE grid
34!    USE Write_Field_p
35
36  IMPLICIT NONE
37
38  PRIVATE
39  PUBLIC :: intersurf_main, stom_define_history, stom_IPCC_define_history, intsurf_time
40
41  INTERFACE intersurf_main
42    MODULE PROCEDURE intersurf_main_2d, intersurf_main_1d, intersurf_gathered, intersurf_gathered_2m
43  END INTERFACE
44  !
45  !  Global variables
46  !
47  INTEGER(i_std),PARAMETER                           :: max_hist_level = 11
48  !
49  LOGICAL, SAVE                                     :: l_first_intersurf=.TRUE. !! Initialisation has to be done one time
50  !
51  INTEGER(i_std), SAVE                               :: hist_id, rest_id        !! IDs for history and restart files
52  INTEGER(i_std), SAVE                               :: hist2_id                !! ID for the second history files (Hi-frequency ?)
53  INTEGER(i_std), SAVE                               :: hist_id_stom, hist_id_stom_IPCC, rest_id_stom !! Dito for STOMATE
54  REAL(r_std), SAVE                                  :: dw                      !! frequency of history write (sec.)
55  !
56  INTEGER(i_std), SAVE                               :: itau_offset  !! This offset is used to phase the
57  !                                                                 !! calendar of the GCM or the driver.
58  REAL(r_std)                                        :: date0_shifted
59  !
60  TYPE(control_type), SAVE                          :: control_flags !! Flags that (de)activate parts of the model
61  !
62  !
63  !! first day of this year
64  REAL(r_std) :: julian0
65  !
66  LOGICAL, PARAMETER :: check_INPUTS = .FALSE.         !! (very) long print of INPUTs in intersurf
67  LOGICAL, SAVE :: OFF_LINE_MODE = .FALSE. 
68  LOGICAL, SAVE :: check_time = .FALSE.
69  PUBLIC check_time, l_first_intersurf
70  !
71CONTAINS
72  !
73  !f90doc CONTAINS
74  !
75  SUBROUTINE intersurf_main_2d (kjit, iim, jjm, kjpindex, kindex, xrdt, &
76     & lrestart_read, lrestart_write, lon, lat, zcontfrac, zneighbours, zresolution, date0, &
77! First level conditions
78     & zlev, u, v, qair, temp_air, epot_air, ccanopy, &
79! Variables for the implicit coupling
80     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
81! Rain, snow, radiation and surface pressure
82     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
83! Output : Fluxes
84     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
85! Surface temperatures and surface properties
86     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0) 
87   
88    ! routines called : sechiba_main
89    !
90    IMPLICIT NONE
91    !   
92    ! interface description for dummy arguments
93    ! input scalar
94    INTEGER(i_std),INTENT (in)                            :: kjit          !! Time step number
95    INTEGER(i_std),INTENT (in)                            :: iim, jjm      !! Dimension of input fields
96    INTEGER(i_std),INTENT (in)                            :: kjpindex      !! Number of continental points
97    REAL(r_std),INTENT (in)                               :: xrdt          !! Time step in seconds
98    LOGICAL, INTENT (in)                                 :: lrestart_read !! Logical for _restart_ file to read
99    LOGICAL, INTENT (in)                                 :: lrestart_write!! Logical for _restart_ file to write'
100    REAL(r_std), INTENT (in)                              :: date0         !! Date at which kjit = 0
101    ! input fields
102    INTEGER(i_std),DIMENSION (kjpindex), INTENT (in)      :: kindex        !! Index for continental points
103    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: u             !! Lowest level wind speed
104    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: v             !! Lowest level wind speed
105    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: zlev          !! Height of first layer
106    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: qair          !! Lowest level specific humidity
107    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: precip_rain   !! Rain precipitation
108    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: precip_snow   !! Snow precipitation
109    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: lwdown        !! Down-welling long-wave flux
110    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: swnet         !! Net surface short-wave flux
111    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: swdown        !! Downwelling surface short-wave flux
112    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: temp_air      !! Air temperature in Kelvin
113    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: epot_air      !! Air potential energy
114    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: ccanopy       !! CO2 concentration in the canopy
115    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: petAcoef      !! Coeficients A from the PBL resolution
116    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: peqAcoef      !! One for T and another for q
117    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: petBcoef      !! Coeficients B from the PBL resolution
118    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: peqBcoef      !! One for T and another for q
119    REAL(r_std),DIMENSION (iim,jjm), INTENT(inout)          :: cdrag         !! Cdrag
120    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: pb            !! Lowest level pressure
121    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: lon, lat      !! Geographical coordinates
122    REAL(r_std),DIMENSION (iim,jjm), INTENT(in)             :: zcontfrac      !! Fraction of continent in the grid
123    INTEGER, DIMENSION (iim,jjm,8), INTENT(in)             :: zneighbours   !! land neighbours
124    REAL(r_std),DIMENSION (iim,jjm,2), INTENT(in)           :: zresolution   !! resolution in x and y dimensions
125    ! output fields
126    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: z0            !! Surface roughness
127    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
128    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
129    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: tsol_rad      !! Radiative surface temperature
130    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: vevapp        !! Total of evaporation
131    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: temp_sol_new  !! New soil temperature
132    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: qsurf         !! Surface specific humidity
133    REAL(r_std),DIMENSION (iim,jjm,2), INTENT(out)          :: albedo        !! Albedo
134    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: fluxsens      !! Sensible chaleur flux
135    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: fluxlat       !! Latent chaleur flux
136    REAL(r_std),DIMENSION (iim,jjm), INTENT(out)            :: emis          !! Emissivity
137    ! LOCAL declaration
138    ! work arrays to scatter and/or gather information just before/after sechiba_main call's
139    ! and to keep output value for next call
140    REAL(r_std),DIMENSION (kjpindex)                      :: zu            !! Work array to keep u
141    REAL(r_std),DIMENSION (kjpindex)                      :: zv            !! Work array to keep v
142    REAL(r_std),DIMENSION (kjpindex)                      :: zzlev         !! Work array to keep zlev
143    REAL(r_std),DIMENSION (kjpindex)                      :: zqair         !! Work array to keep qair
144    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_rain  !! Work array to keep precip_rain
145    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_snow  !! Work array to keep precip_snow
146    REAL(r_std),DIMENSION (kjpindex)                      :: zlwdown       !! Work array to keep lwdown
147    REAL(r_std),DIMENSION (kjpindex)                      :: zswnet        !! Work array to keep swnet
148    REAL(r_std),DIMENSION (kjpindex)                      :: zswdown       !! Work array to keep swdown
149    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_air     !! Work array to keep temp_air
150    REAL(r_std),DIMENSION (kjpindex)                      :: zepot_air     !! Work array to keep epot_air
151    REAL(r_std),DIMENSION (kjpindex)                      :: zccanopy      !! Work array to keep ccanopy
152    REAL(r_std),DIMENSION (kjpindex)                      :: zpetAcoef     !! Work array to keep petAcoef
153    REAL(r_std),DIMENSION (kjpindex)                      :: zpeqAcoef     !! Work array to keep peqAcoef
154    REAL(r_std),DIMENSION (kjpindex)                      :: zpetBcoef     !! Work array to keep petBcoef
155    REAL(r_std),DIMENSION (kjpindex)                      :: zpeqBcoef     !! Work array to keep peqVcoef
156    REAL(r_std),DIMENSION (kjpindex)                      :: zcdrag        !! Work array to keep cdrag
157    REAL(r_std),DIMENSION (kjpindex)                      :: zpb           !! Work array to keep pb
158    REAL(r_std),DIMENSION (kjpindex)                      :: zz0           !! Work array to keep z0
159    REAL(r_std),DIMENSION (kjpindex)                      :: zcoastal      !! Work array to keep coastalflow
160    REAL(r_std),DIMENSION (kjpindex)                      :: zriver        !! Work array to keep riverflow
161    REAL(r_std),DIMENSION (kjpindex)                      :: dcoastal      !! Work array to keep coastalflow
162    REAL(r_std),DIMENSION (kjpindex)                      :: driver        !! Work array to keep riverflow
163    REAL(r_std),DIMENSION (kjpindex)                      :: znetco2       !! Work array to keep netco2flux
164    REAL(r_std),DIMENSION (kjpindex)                      :: zcarblu       !! Work array to keep fco2_land_use
165    REAL(r_std),DIMENSION (kjpindex)                      :: ztsol_rad     !! Work array to keep tsol_rad
166    REAL(r_std),DIMENSION (kjpindex)                      :: zvevapp       !! Work array to keep vevapp
167    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_sol_new !! Work array to keep temp_sol_new
168    REAL(r_std),DIMENSION (kjpindex)                      :: zqsurf        !! Work array to keep qsurf
169    REAL(r_std),DIMENSION (kjpindex,2)                    :: zalbedo       !! Work array to keep albedo
170    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxsens     !! Work array to keep fluxsens
171    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxlat      !! Work array to keep fluxlat
172    REAL(r_std),DIMENSION (kjpindex)                      :: zemis         !! Work array to keep emis
173    !
174    ! Local variables with shape of the inputs
175    !
176    REAL(r_std),DIMENSION (iim,jjm)                       :: dswnet         !! Net surface short-wave flux
177    REAL(r_std),DIMENSION (iim,jjm)                       :: dswdown         !! Incident surface short-wave flux
178    !
179    INTEGER(i_std)                                       :: i, j, ik
180    INTEGER(i_std)                                       :: itau_sechiba
181    REAL(r_std)                                           :: zlev_mean
182    LOGICAL                                              :: do_watch      !! if it's time, write watchout
183    INTEGER                                              :: old_fileout   !! old Logical Int for std IO output
184    LOGICAL :: check = .FALSE.
185    !
186    CALL ipslnlf(new_number=numout,old_number=old_fileout)
187    !
188    IF (l_first_intersurf) THEN
189!       CALL Init_WriteField_p(kindex)
190       !
191       CALL intsurf_time( kjit, date0, xrdt )
192       !
193       IF ( check ) WRITE(numout,*) 'Initialisation of intersurf_main_2d'
194       !
195       OFF_LINE_MODE = .TRUE. 
196       !
197       DO ik=1,kjpindex
198         
199          j = ((kindex(ik)-1)/iim) + 1
200          i = (kindex(ik) - (j-1)*iim)
201
202          !- Create the internal coordinate table
203          !-
204          lalo(ik,1) = lat(i,j)
205          lalo(ik,2) = lon(i,j)
206          !
207          !- Store the fraction of the continents only once so that the user
208          !- does not change them afterwards.
209          !-
210          contfrac(ik) = zcontfrac(i,j)
211       ENDDO
212       CALL gather(contfrac,contfrac_g)
213       CALL gather(lalo,lalo_g)
214       CALL gather2D(lon,lon_g)
215       CALL gather2D(lat,lat_g)
216       CALL gather2D(zlev,zlev_g)
217       !
218       !  Configuration of SSL specific parameters
219       !
220       CALL intsurf_config(control_flags, xrdt)
221       !
222       CALL intsurf_restart(kjit, iim, jjm, lon, lat, date0, xrdt, control_flags, rest_id, rest_id_stom, itau_offset)
223       itau_sechiba = kjit + itau_offset
224       !
225       CALL intsurf_history(iim, jjm, lon, lat, itau_sechiba, date0_shifted, xrdt, control_flags, hist_id, &
226            & hist2_id, hist_id_stom, hist_id_stom_IPCC)
227       !
228       IF ( ok_watchout ) THEN
229          IF (is_root_prc) THEN
230             zlev_mean = zero
231             DO ik=1, nbp_glo
232                j = ((index_g(ik)-1)/iim_g) + 1
233                i = (index_g(ik) - (j-1)*iim_g)
234       
235                zlev_mean = zlev_mean + zlev_g(i,j)
236             ENDDO
237             zlev_mean = zlev_mean / REAL(nbp_glo,r_std)
238          ENDIF
239
240          last_action_watch = itau_sechiba
241          last_check_watch  = last_action_watch
242
243          ! Only root proc write watchout file
244          CALL watchout_init (iim_g, jjm_g, kjpindex, nbp_glo, &
245               & date0_shifted, last_action_watch, dt_watch, index_g, lon_g, lat_g, zlev_mean)
246       ENDIF
247       !
248       IF ( check ) WRITE(numout,*) 'End of Initialisation of intersurf'
249       !
250    ENDIF
251    !
252    !  Shift the time step to phase the two models
253    !
254    itau_sechiba = kjit + itau_offset
255    !
256    CALL intsurf_time( itau_sechiba, date0_shifted, xrdt )
257    !
258    ! 1. gather input fields from kindex array
259    !    Warning : I'm not sure this interface with one dimension array is the good one
260    !
261    DO ik=1, kjpindex
262     
263       j = ((kindex(ik)-1)/iim) + 1
264       i = (kindex(ik) - (j-1)*iim)
265       
266       zu(ik)           = u(i,j)
267       zv(ik)           = v(i,j)
268       zzlev(ik)        = zlev(i,j)
269       zqair(ik)        = qair(i,j)
270       zprecip_rain(ik) = precip_rain(i,j)*xrdt
271       zprecip_snow(ik) = precip_snow(i,j)*xrdt
272       zlwdown(ik)      = lwdown(i,j)
273       zswnet(ik)       = swnet(i,j)
274       zswdown(ik)      = swdown(i,j)
275       ztemp_air(ik)    = temp_air(i,j)
276       zepot_air(ik)    = epot_air(i,j)
277       zccanopy(ik)     = ccanopy(i,j)
278       zpetAcoef(ik)    = petAcoef(i,j)
279       zpeqAcoef(ik)    = peqAcoef(i,j)
280       zpetBcoef(ik)    = petBcoef(i,j)
281       zpeqBcoef(ik)    = peqBcoef(i,j)
282       zcdrag(ik)       = cdrag(i,j)
283       zpb(ik)          = pb(i,j)
284       
285    ENDDO
286    !
287    IF (check_INPUTS) THEN
288       WRITE(numout,*) "Intersurf_main_2D :"
289       WRITE(numout,*) "Time step number = ",kjit
290       WRITE(numout,*) "Dimension of input fields = ",iim, jjm
291       WRITE(numout,*) "Number of continental points = ",kjpindex
292       WRITE(numout,*) "Time step in seconds = ",xrdt
293       WRITE(numout,*) "Logical for _restart_ file to read, write = ",lrestart_read,lrestart_write
294       WRITE(numout,*) "Date at which kjit = 0  =  ",date0
295       WRITE(numout,*) "Index for continental points = ",kindex
296       WRITE(numout,*) "Lowest level wind speed North = ",zu
297       WRITE(numout,*) "Lowest level wind speed East = ",zv
298       WRITE(numout,*) "Height of first layer = ",zzlev
299       WRITE(numout,*) "Lowest level specific humidity = ",zqair
300       WRITE(numout,*) "Rain precipitation = ",zprecip_rain
301       WRITE(numout,*) "Snow precipitation = ",zprecip_snow
302       WRITE(numout,*) "Down-welling long-wave flux = ",zlwdown
303       WRITE(numout,*) "Net surface short-wave flux = ",zswnet
304       WRITE(numout,*) "Downwelling surface short-wave flux = ",zswdown
305       WRITE(numout,*) "Air temperature in Kelvin = ",ztemp_air
306       WRITE(numout,*) "Air potential energy = ",zepot_air
307       WRITE(numout,*) "CO2 concentration in the canopy = ",zccanopy
308       WRITE(numout,*) "Coeficients A from the PBL resolution = ",zpetAcoef
309       WRITE(numout,*) "One for T and another for q = ",zpeqAcoef
310       WRITE(numout,*) "Coeficients B from the PBL resolution = ",zpetBcoef
311       WRITE(numout,*) "One for T and another for q = ",zpeqBcoef
312       WRITE(numout,*) "Cdrag = ",zcdrag
313       WRITE(numout,*) "Lowest level pressure = ",zpb
314       WRITE(numout,*) "Geographical coordinates lon = ", (/ ( lon(ilandindex(ik), jlandindex(ik)), ik=1,kjpindex ) /)
315       WRITE(numout,*) "Geographical coordinates lat = ", (/ ( lat(ilandindex(ik), jlandindex(ik)), ik=1,kjpindex ) /) 
316       WRITE(numout,*) "Fraction of continent in the grid = ",contfrac
317    ENDIF
318    !
319    ! 2. save the grid
320    !
321    IF ( check ) WRITE(numout,*) 'Save the grid'
322    !
323    IF (l_first_intersurf) THEN
324       CALL histwrite(hist_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
325       CALL histwrite(hist_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
326       IF ( control_flags%ok_stomate ) THEN
327          CALL histwrite(hist_id_stom, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
328          IF ( hist_id_stom_IPCC > 0 ) THEN
329             CALL histwrite(hist_id_stom_IPCC, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
330          ENDIF
331       ENDIF
332       CALL histwrite(hist_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
333       CALL histsync(hist_id)
334       !
335       IF ( hist2_id > 0 ) THEN
336          CALL histwrite(hist2_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
337          CALL histwrite(hist2_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
338          CALL histwrite(hist2_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
339          CALL histsync(hist2_id)
340       ENDIF
341       !
342    ENDIF
343    !
344    ! 3. call sechiba for continental points only
345    !
346    IF ( check ) WRITE(numout,*) 'Calling sechiba'
347    !
348    CALL sechiba_main (itau_sechiba, iim*jjm, kjpindex, kindex, xrdt, date0_shifted, &
349       & lrestart_read, lrestart_write, control_flags, &
350       & lalo, contfrac, neighbours, resolution, &
351! First level conditions
352! Ajout Nathalie - Juin 2006 - passage q2m/t2m pour calcul rveget
353!       & zzlev, zu, zv, zqair, ztemp_air, zepot_air, zccanopy, &
354       & zzlev, zu, zv, zqair, zqair, ztemp_air, ztemp_air, zepot_air, zccanopy, &
355! Variables for the implicit coupling
356       & zcdrag, zpetAcoef, zpeqAcoef, zpetBcoef, zpeqBcoef, &
357! Rain, snow, radiation and surface pressure
358       & zprecip_rain ,zprecip_snow,  zlwdown, zswnet, zswdown, zpb, &
359! Output : Fluxes
360       & zvevapp, zfluxsens, zfluxlat, zcoastal, zriver, znetco2, zcarblu, &
361! Surface temperatures and surface properties
362       & ztsol_rad, ztemp_sol_new, zqsurf, zalbedo, zemis, zz0, &
363! File ids
364       & rest_id, hist_id, hist2_id, rest_id_stom, hist_id_stom, hist_id_stom_IPCC ) 
365   
366    !
367    IF ( check ) WRITE(numout,*) 'out of SECHIBA'
368    !
369    ! 4. save watchout
370    !
371    IF ( ok_watchout .AND. .NOT. l_first_intersurf ) THEN
372       ! Accumulate last time step
373       sum_zlev(:) = sum_zlev(:) + zzlev(:)
374       sum_u(:) = sum_u(:) + zu(:)
375       sum_v(:) = sum_v(:) + zv(:)
376       sum_qair(:) = sum_qair(:) + zqair(:) 
377       sum_temp_air(:) = sum_temp_air(:) + ztemp_air(:)
378       sum_epot_air(:) = sum_epot_air(:) + zepot_air(:)
379       sum_ccanopy(:) = sum_ccanopy(:) + zccanopy(:)
380       sum_cdrag(:) = sum_cdrag(:) + zcdrag(:)
381       sum_petAcoef(:) = sum_petAcoef(:) + zpetAcoef(:)
382       sum_peqAcoef(:) = sum_peqAcoef(:) + zpeqAcoef(:)
383       sum_petBcoef(:) = sum_petBcoef(:) + zpetBcoef(:)
384       sum_peqBcoef(:) = sum_peqBcoef(:) + zpeqBcoef(:)
385       sum_rain(:) = sum_rain(:) + zprecip_rain(:)
386       sum_snow(:) = sum_snow(:) + zprecip_snow(:)
387       sum_lwdown(:) = sum_lwdown(:) + zlwdown(:)
388       sum_pb(:) = sum_pb(:) + zpb(:)
389
390!!$       IF ( dt_watch > 3600 ) THEN
391!!$          julian_watch = date0_shifted+((itau_sechiba-0.5)/dt_split_watch)*dt_watch/one_day
392!!$          WRITE(numout, *) "WATCH register : julian_watch ",julian_watch, " julian0",julian0,"date0_shifted ",date0_shifted, &
393!!$               "itau_sechiba ",itau_sechiba, &
394!!$               dt_split_watch,dt_watch,one_day
395!!$          CALL solarang (julian_watch, julian0, iim, jjm, lon, lat, sinang)
396!!$          WHERE ( sinang(:,:) .LT. EPSILON(un) )
397!!$             isinang(:,:) = isinang(:,:) - 1
398!!$          ENDWHERE
399!!$          mean_sinang(:,:) = mean_sinang(:,:)+sinang(:,:)
400!!$          WRITE(numout, *) "WATCH sinang : ",sinang, mean_sinang
401!!$          WRITE(numout,*) "sum_swdown",sum_swdown
402!!$          !
403!!$          DO ik=1,kjpindex         
404!!$             j = ((kindex(ik)-1)/iim) + 1
405!!$             i = (kindex(ik) - (j-1)*iim)
406!!$             
407!!$             sum_swnet(ik) = sum_swnet(ik) + sinang(i,j)*zswnet(ik)
408!!$             sum_swdown(ik) = sum_swdown(ik) + sinang(i,j)*zswdown(ik)
409!!$          ENDDO
410!!$       ELSE
411          sum_swnet(:) = sum_swnet(:) + zswnet(:)
412          sum_swdown(:) = sum_swdown(:) + zswdown(:)
413!!$       ENDIF
414
415       do_watch = .FALSE.
416       call isittime &
417            &  (itau_sechiba,date0_shifted,xrdt,dt_watch,&
418            &   last_action_watch,last_check_watch,do_watch)
419       last_check_watch = itau_sechiba
420       IF (do_watch) THEN
421          !
422          IF ( check ) WRITE(numout,*) 'save watchout'
423          !
424          IF (long_print) THEN
425             WRITE(numout,*) "intersurf : write watchout for date ",date0,date0_shifted,itau_sechiba,&
426                  & last_action_watch, last_check_watch
427          ENDIF
428          last_action_watch = itau_sechiba
429
430          sum_zlev(:) = sum_zlev(:) / dt_split_watch
431          sum_u(:) = sum_u(:) / dt_split_watch
432          sum_v(:) = sum_v(:) / dt_split_watch
433          sum_qair(:) = sum_qair(:) / dt_split_watch
434          sum_temp_air(:) = sum_temp_air(:) / dt_split_watch
435          sum_epot_air(:) = sum_epot_air(:) / dt_split_watch
436          sum_ccanopy(:) = sum_ccanopy(:) / dt_split_watch
437          sum_cdrag(:) = sum_cdrag(:) / dt_split_watch
438          sum_petAcoef(:) = sum_petAcoef(:) / dt_split_watch
439          sum_peqAcoef(:) = sum_peqAcoef(:) / dt_split_watch
440          sum_petBcoef(:) = sum_petBcoef(:) / dt_split_watch
441          sum_peqBcoef(:) = sum_peqBcoef(:) / dt_split_watch
442          sum_rain(:) = sum_rain(:) / dt_split_watch
443          sum_snow(:) = sum_snow(:) / dt_split_watch
444          sum_lwdown(:) = sum_lwdown(:) / dt_split_watch
445          sum_pb(:) = sum_pb(:) / dt_split_watch
446
447!!$          IF ( dt_watch > 3600 ) THEN
448!!$             WRITE(numout, *) "WATCH mean_sinang before norm : ",mean_sinang,isinang
449!!$             WHERE ( isinang(:,:) .LT. dt_split_watch )
450!!$                mean_sinang(:,:) = mean_sinang(:,:) / isinang(:,:)
451!!$             ENDWHERE
452!!$             WRITE(numout, *) "WATCH mean_sinang norm : ",mean_sinang
453!!$             WRITE(numout,*) "SWDOWN 0 : ",sum_swdown(:)
454!!$             !
455!!$             DO ik=1,kjpindex         
456!!$                j = ((kindex(ik)-1)/iim) + 1
457!!$                i = (kindex(ik) - (j-1)*iim)
458!!$                IF (mean_sinang(i,j) > zero) THEN
459!!$                   sum_swdown(ik) = sum_swdown(ik)/mean_sinang(i,j)
460!!$                   sum_swnet(ik) =  sum_swnet(ik)/mean_sinang(i,j)
461!!$                ELSE
462!!$                   sum_swdown(ik) = zero
463!!$                   sum_swnet(ik) =  zero
464!!$                ENDIF
465!!$             ENDDO
466!!$          ELSE
467             sum_swnet(:) = sum_swnet(:) / dt_split_watch
468             sum_swdown(:) = sum_swdown(:) / dt_split_watch
469!!$          ENDIF
470
471          CALL watchout_write_p(kjpindex, itau_sechiba, xrdt, sum_zlev, sum_swdown, sum_rain, &
472               &   sum_snow, sum_lwdown, sum_pb, sum_temp_air, sum_epot_air, sum_qair, &
473               &   sum_u, sum_v, sum_swnet, sum_petAcoef, sum_peqAcoef, sum_petBcoef, sum_peqBcoef, &
474               &   sum_cdrag, sum_ccanopy )
475       ENDIF
476    ENDIF
477    !
478    ! 5. scatter output fields
479    !
480    z0(:,:)           = undef_sechiba
481    coastalflow(:,:)  = undef_sechiba
482    riverflow(:,:)    = undef_sechiba
483    tsol_rad(:,:)     = undef_sechiba
484    vevapp(:,:)       = undef_sechiba
485    temp_sol_new(:,:) = undef_sechiba 
486    qsurf(:,:)        = undef_sechiba 
487    albedo(:,:,:)     = undef_sechiba
488    fluxsens(:,:)     = undef_sechiba
489    fluxlat(:,:)      = undef_sechiba
490    emis(:,:)         = undef_sechiba 
491    cdrag(:,:)        = undef_sechiba 
492    dswnet(:,:)       = undef_sechiba 
493    dswdown(:,:)      = undef_sechiba 
494    !
495    DO ik=1, kjpindex
496     
497   
498       j = ((kindex(ik)-1)/iim) + 1
499       i = (kindex(ik) - (j-1)*iim)
500
501       z0(i,j)           = zz0(ik)
502       coastalflow(i,j)  = zcoastal(ik)/1000.
503       riverflow(i,j)    = zriver(ik)/1000.
504       tsol_rad(i,j)     = ztsol_rad(ik)
505       vevapp(i,j)       = zvevapp(ik)
506       temp_sol_new(i,j) = ztemp_sol_new(ik)
507       qsurf(i,j)        = zqsurf(ik)
508       albedo(i,j,1)     = zalbedo(ik,1)
509       albedo(i,j,2)     = zalbedo(ik,2)
510       fluxsens(i,j)     = zfluxsens(ik)
511       fluxlat(i,j)      = zfluxlat(ik)
512       emis(i,j)         = zemis(ik)
513       cdrag(i,j)        = zcdrag(ik)
514       dswnet(i,j)       = zswnet(ik)
515       dswdown(i,j)      = zswdown(ik)
516
517    ENDDO
518    !
519    ! Modified fields for variables scattered during the writing
520    !
521    dcoastal(:) = (zcoastal(:))/1000.     
522    driver(:)   = (zriver(:))/1000.
523    !
524    IF ( .NOT. l_first_intersurf) THEN
525       !
526       IF ( .NOT. almaoutput ) THEN
527       !
528       !  scattered during the writing
529       !
530          CALL histwrite (hist_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
531          CALL histwrite (hist_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
532          CALL histwrite (hist_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
533       !
534          CALL histwrite (hist_id, 'temp_sol', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
535          CALL histwrite (hist_id, 'tsol_max', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
536          CALL histwrite (hist_id, 'tsol_min', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
537          CALL histwrite (hist_id, 'fluxsens', itau_sechiba, fluxsens, kjpindex, kindex)
538          CALL histwrite (hist_id, 'fluxlat',  itau_sechiba, fluxlat, kjpindex, kindex)
539          CALL histwrite (hist_id, 'swnet',    itau_sechiba, dswnet, kjpindex, kindex)
540          CALL histwrite (hist_id, 'swdown',   itau_sechiba, dswdown, kjpindex, kindex)
541          CALL histwrite (hist_id, 'alb_vis',  itau_sechiba, albedo(:,:,1), kjpindex, kindex)
542          CALL histwrite (hist_id, 'alb_nir',  itau_sechiba, albedo(:,:,2), kjpindex, kindex)
543          CALL histwrite (hist_id, 'tair',     itau_sechiba, temp_air, kjpindex, kindex)
544          CALL histwrite (hist_id, 'qair',     itau_sechiba, qair, kjpindex, kindex)
545          ! Ajout Nathalie - Juin 2006 - on conserve q2m/t2m
546          CALL histwrite (hist_id, 'q2m',     itau_sechiba, qair, kjpindex, kindex)
547          CALL histwrite (hist_id, 't2m',     itau_sechiba, temp_air, kjpindex, kindex)
548          IF ( hist2_id > 0 ) THEN
549             CALL histwrite (hist2_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
550             CALL histwrite (hist2_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
551             CALL histwrite (hist2_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
552             !
553             CALL histwrite (hist2_id, 'temp_sol', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
554             CALL histwrite (hist2_id, 'tsol_max', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
555             CALL histwrite (hist2_id, 'tsol_min', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
556             CALL histwrite (hist2_id, 'fluxsens', itau_sechiba, fluxsens, kjpindex, kindex)
557             CALL histwrite (hist2_id, 'fluxlat',  itau_sechiba, fluxlat, kjpindex, kindex)
558             CALL histwrite (hist2_id, 'swnet',    itau_sechiba, dswnet, kjpindex, kindex)
559             CALL histwrite (hist2_id, 'swdown',   itau_sechiba, dswdown, kjpindex, kindex)
560             CALL histwrite (hist2_id, 'alb_vis',  itau_sechiba, albedo(:,:,1), kjpindex, kindex)
561             CALL histwrite (hist2_id, 'alb_nir',  itau_sechiba, albedo(:,:,2), kjpindex, kindex)
562             CALL histwrite (hist2_id, 'tair',     itau_sechiba, temp_air, kjpindex, kindex)
563             CALL histwrite (hist2_id, 'qair',     itau_sechiba, qair, kjpindex, kindex)
564             CALL histwrite (hist2_id, 'q2m',     itau_sechiba, qair, kjpindex, kindex)
565             CALL histwrite (hist2_id, 't2m',     itau_sechiba, temp_air, kjpindex, kindex)
566          ENDIF
567       ELSE
568          CALL histwrite (hist_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
569          CALL histwrite (hist_id, 'SWnet',    itau_sechiba, dswnet, kjpindex, kindex)
570          CALL histwrite (hist_id, 'Qh', itau_sechiba, fluxsens, kjpindex, kindex)
571          CALL histwrite (hist_id, 'Qle',  itau_sechiba, fluxlat, kjpindex, kindex)
572          CALL histwrite (hist_id, 'AvgSurfT', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
573          CALL histwrite (hist_id, 'RadT', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
574          IF ( hist2_id > 0 ) THEN
575             CALL histwrite (hist2_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
576             CALL histwrite (hist2_id, 'SWnet',    itau_sechiba, dswnet, kjpindex, kindex)
577             CALL histwrite (hist2_id, 'Qh', itau_sechiba, fluxsens, kjpindex, kindex)
578             CALL histwrite (hist2_id, 'Qle',  itau_sechiba, fluxlat, kjpindex, kindex)
579             CALL histwrite (hist2_id, 'AvgSurfT', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
580             CALL histwrite (hist2_id, 'RadT', itau_sechiba, temp_sol_NEW, kjpindex, kindex)
581          ENDIF
582       ENDIF
583       !
584       IF (dw .EQ. xrdt) THEN
585          CALL histsync(hist_id)
586       ENDIF
587       !
588    ENDIF
589    !
590    ! 6. Transform the water fluxes into Kg/m^2s and m^3/s
591    !
592    DO ik=1, kjpindex
593   
594       j = ((kindex(ik)-1)/iim) + 1
595       i = (kindex(ik) - (j-1)*iim)
596
597       vevapp(i,j) = vevapp(i,j)/xrdt
598       coastalflow(i,j) = coastalflow(i,j)/xrdt
599       riverflow(i,j) = riverflow(i,j)/xrdt
600
601    ENDDO
602    !
603    IF ( lrestart_write .AND. ok_watchout .AND. is_root_prc ) THEN
604       CALL watchout_close()
605    ENDIF
606    !
607    IF(l_first_intersurf .AND. is_root_prc) CALL getin_dump
608    l_first_intersurf = .FALSE.
609    !
610    IF (long_print) WRITE (numout,*) ' intersurf_main done '
611    !
612    CALL ipslnlf(new_number=old_fileout)
613    !
614  END SUBROUTINE intersurf_main_2d
615!
616  SUBROUTINE intersurf_main_1d (kjit, iim, jjm, kjpindex, kindex, xrdt, &
617     & lrestart_read, lrestart_write, lon, lat, zcontfrac, zneighbours, zresolution, date0, &
618! First level conditions
619     & zlev,  u, v, qair, temp_air, epot_air, ccanopy, &
620! Variables for the implicit coupling
621     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
622! Rain, snow, radiation and surface pressure
623     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
624! Output : Fluxes
625     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
626! Surface temperatures and surface properties
627     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0) 
628   
629    ! routines called : sechiba_main
630    !
631    IMPLICIT NONE
632    !   
633    ! interface description for dummy arguments
634    ! input scalar
635    INTEGER(i_std),INTENT (in)                            :: kjit          !! Time step number
636    INTEGER(i_std),INTENT (in)                            :: iim, jjm      !! Dimension of input fields
637    INTEGER(i_std),INTENT (in)                            :: kjpindex      !! Number of continental points
638    REAL(r_std),INTENT (in)                               :: xrdt          !! Time step in seconds
639    LOGICAL, INTENT (in)                                 :: lrestart_read !! Logical for _restart_ file to read
640    LOGICAL, INTENT (in)                                 :: lrestart_write!! Logical for _restart_ file to write'
641    REAL(r_std), INTENT (in)                              :: date0         !! Date at which kjit = 0
642    ! input fields
643    INTEGER(i_std),DIMENSION (kjpindex), INTENT (in)      :: kindex        !! Index for continental points
644    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: u             !! Lowest level wind speed
645    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: v             !! Lowest level wind speed
646    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: zlev          !! Height of first layer
647    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: qair          !! Lowest level specific humidity
648    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: precip_rain   !! Rain precipitation
649    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: precip_snow   !! Snow precipitation
650    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: lwdown        !! Down-welling long-wave flux
651    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: swnet         !! Net surface short-wave flux
652    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: swdown        !! Downwelling surface short-wave flux
653    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: temp_air      !! Air temperature in Kelvin
654    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: epot_air      !! Air potential energy
655    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: ccanopy       !! CO2 concentration in the canopy
656    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: petAcoef      !! Coeficients A from the PBL resolution
657    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: peqAcoef      !! One for T and another for q
658    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: petBcoef      !! Coeficients B from the PBL resolution
659    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: peqBcoef      !! One for T and another for q
660    REAL(r_std),DIMENSION (iim*jjm), INTENT(inout)          :: cdrag         !! Cdrag
661    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: pb            !! Lowest level pressure
662    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: lon, lat      !! Geographical coordinates
663    REAL(r_std),DIMENSION (iim*jjm), INTENT(in)             :: zcontfrac     !! Fraction of continent
664    INTEGER, DIMENSION (iim*jjm,8), INTENT(in)             :: zneighbours   !! land neighbours
665    REAL(r_std),DIMENSION (iim*jjm,2), INTENT(in)           :: zresolution   !! resolution in x and y dimensions
666    ! output fields
667    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: z0            !! Surface roughness
668    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
669    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
670    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: tsol_rad      !! Radiative surface temperature
671    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: vevapp        !! Total of evaporation
672    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: temp_sol_new  !! New soil temperature
673    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: qsurf         !! Surface specific humidity
674    REAL(r_std),DIMENSION (iim*jjm,2), INTENT(out)          :: albedo        !! Albedo
675    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: fluxsens      !! Sensible chaleur flux
676    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: fluxlat       !! Latent chaleur flux
677    REAL(r_std),DIMENSION (iim*jjm), INTENT(out)            :: emis          !! Emissivity
678    ! LOCAL declaration
679    ! work arrays to scatter and/or gather information just before/after sechiba_main call's
680    ! and to keep output value for next call
681    REAL(r_std),DIMENSION (kjpindex)                      :: zu            !! Work array to keep u
682    REAL(r_std),DIMENSION (kjpindex)                      :: zv            !! Work array to keep v
683    REAL(r_std),DIMENSION (kjpindex)                      :: zzlev         !! Work array to keep zlev
684    REAL(r_std),DIMENSION (kjpindex)                      :: zqair         !! Work array to keep qair
685    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_rain  !! Work array to keep precip_rain
686    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_snow  !! Work array to keep precip_snow
687    REAL(r_std),DIMENSION (kjpindex)                      :: zlwdown       !! Work array to keep lwdown
688    REAL(r_std),DIMENSION (kjpindex)                      :: zswnet        !! Work array to keep swnet
689    REAL(r_std),DIMENSION (kjpindex)                      :: zswdown       !! Work array to keep swdown
690    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_air     !! Work array to keep temp_air
691    REAL(r_std),DIMENSION (kjpindex)                      :: zepot_air     !! Work array to keep epot_air
692    REAL(r_std),DIMENSION (kjpindex)                      :: zccanopy      !! Work array to keep ccanopy
693    REAL(r_std),DIMENSION (kjpindex)                      :: zpetAcoef     !! Work array to keep petAcoef
694    REAL(r_std),DIMENSION (kjpindex)                      :: zpeqAcoef     !! Work array to keep peqAcoef
695    REAL(r_std),DIMENSION (kjpindex)                      :: zpetBcoef     !! Work array to keep petBcoef
696    REAL(r_std),DIMENSION (kjpindex)                      :: zpeqBcoef     !! Work array to keep peqVcoef
697    REAL(r_std),DIMENSION (kjpindex)                      :: zcdrag        !! Work array to keep cdrag
698    REAL(r_std),DIMENSION (kjpindex)                      :: zpb           !! Work array to keep pb
699    REAL(r_std),DIMENSION (kjpindex)                      :: zz0           !! Work array to keep z0
700    REAL(r_std),DIMENSION (kjpindex)                      :: zcoastal      !! Work array to keep coastal flow
701    REAL(r_std),DIMENSION (kjpindex)                      :: zriver        !! Work array to keep river out flow
702    REAL(r_std),DIMENSION (kjpindex)                      :: dcoastal      !! Work array to keep coastal flow
703    REAL(r_std),DIMENSION (kjpindex)                      :: driver        !! Work array to keep river out flow
704    REAL(r_std),DIMENSION (kjpindex)                      :: znetco2       !! Work array to keep netco2flux
705    REAL(r_std),DIMENSION (kjpindex)                      :: zcarblu       !! Work array to keep fco2_land_use
706    REAL(r_std),DIMENSION (kjpindex)                      :: ztsol_rad     !! Work array to keep tsol_rad
707    REAL(r_std),DIMENSION (kjpindex)                      :: zvevapp       !! Work array to keep vevapp
708    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_sol_new !! Work array to keep temp_sol_new
709    REAL(r_std),DIMENSION (kjpindex)                      :: zqsurf        !! Work array to keep qsurf
710    REAL(r_std),DIMENSION (kjpindex,2)                    :: zalbedo       !! Work array to keep albedo
711    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxsens     !! Work array to keep fluxsens
712    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxlat      !! Work array to keep fluxlat
713    REAL(r_std),DIMENSION (kjpindex)                      :: zemis         !! Work array to keep emis
714    !
715    ! Local but with input shape
716    !
717    REAL(r_std),DIMENSION (iim*jjm)                       :: dswnet         !! Net surface short-wave flux
718    REAL(r_std),DIMENSION (iim*jjm)                       :: dswdown        !! Incident surface short-wave flux
719    !
720    INTEGER(i_std)                                        :: i, j, ik
721    INTEGER(i_std)                                        :: itau_sechiba
722    REAL(r_std), ALLOCATABLE, DIMENSION(:,:)              :: tmp_lon, tmp_lat, tmp_lev
723    REAL(r_std)                                           :: zlev_mean
724    LOGICAL                                               :: do_watch      !! if it's time, write watchout
725    INTEGER                                               :: old_fileout   !! old Logical Int for std IO output
726    LOGICAL :: check = .FALSE.
727    !
728    CALL ipslnlf(new_number=numout,old_number=old_fileout)
729    !
730    IF (l_first_intersurf) THEN
731       !
732       CALL intsurf_time( kjit, date0, xrdt )
733       !
734       IF ( check ) WRITE(numout,*) 'Initialisation of intersurf_main_1d'
735       !
736       OFF_LINE_MODE = .TRUE. 
737       !
738       !  Create the internal coordinate table
739       !
740       IF ( (.NOT.ALLOCATED(tmp_lon))) THEN
741          ALLOCATE(tmp_lon(iim,jjm))
742       ENDIF
743       IF ( (.NOT.ALLOCATED(tmp_lat))) THEN
744          ALLOCATE(tmp_lat(iim,jjm))
745       ENDIF
746       IF ( (.NOT.ALLOCATED(tmp_lev))) THEN
747          ALLOCATE(tmp_lev(iim,jjm))
748       ENDIF
749       !
750       DO i=1,iim
751          DO j=1,jjm
752             ik = (j-1)*iim + i
753             tmp_lon(i,j) = lon(ik)
754             tmp_lat(i,j) = lat(ik)
755             tmp_lev(i,j) = zlev(kindex(ik)) 
756          ENDDO
757       ENDDO
758       !
759       lalo(:,1) = lat(:)
760       lalo(:,2) = lon(:)
761       !
762       !- Store the fraction of the continents only once so that the user
763       !- does not change them afterwards.
764       !
765       DO ik=1,kjpindex
766
767          contfrac(ik) = zcontfrac(kindex(ik))
768
769       ENDDO
770       contfrac_g(:) = contfrac(:)
771       lalo_g(:,:) = lalo(:,:)
772       lon_g(:,:) = tmp_lon(:,:)
773       lat_g(:,:) = tmp_lat(:,:)
774       zlev_g(:,:) = tmp_lev(:,:)
775       !
776       !  Configuration of SSL specific parameters
777       !
778       CALL intsurf_config(control_flags, xrdt)
779       !
780       CALL intsurf_restart(kjit, iim, jjm, tmp_lon, tmp_lat, date0, xrdt, control_flags, rest_id, rest_id_stom, itau_offset)
781       itau_sechiba = kjit + itau_offset
782       !
783       CALL intsurf_history(iim, jjm, tmp_lon, tmp_lat, itau_sechiba, date0_shifted, xrdt, control_flags, hist_id, &
784            & hist2_id, hist_id_stom, hist_id_stom_IPCC)
785       !
786       IF ( ok_watchout ) THEN
787          zlev_mean = zero
788          DO ik=1, kjpindex
789
790             zlev_mean = zlev_mean + zlev(ik)
791          ENDDO
792          ! Divide by one
793          zlev_mean = zlev_mean / REAL(kjpindex,r_std)
794
795          last_action_watch = itau_sechiba
796          last_check_watch  = last_action_watch
797
798          CALL watchout_init(iim, jjm, kjpindex, kjpindex, &
799               & date0_shifted, last_action_watch, dt_watch, index_g, lon_g, lat_g, zlev_mean)
800       ENDIF
801       !
802       IF ( check ) WRITE(numout,*) 'End of Initialisation of intersurf'
803       !
804    ENDIF
805    !
806    !  Shift the time step to phase the two models
807    !
808    itau_sechiba = kjit + itau_offset
809    !
810    CALL intsurf_time( itau_sechiba, date0_shifted, xrdt )
811    !
812    ! 1. gather input fields from kindex array
813    !
814    DO ik=1, kjpindex
815       
816       zu(ik)           = u(kindex(ik))
817       zv(ik)           = v(kindex(ik))
818       zzlev(ik)        = zlev(kindex(ik))
819       zqair(ik)        = qair(kindex(ik))
820       zprecip_rain(ik) = precip_rain(kindex(ik))*xrdt
821       zprecip_snow(ik) = precip_snow(kindex(ik))*xrdt
822       zlwdown(ik)      = lwdown(kindex(ik))
823       zswnet(ik)       = swnet(kindex(ik))
824       zswdown(ik)      = swdown(kindex(ik))
825       ztemp_air(ik)    = temp_air(kindex(ik))
826       zepot_air(ik)    = epot_air(kindex(ik))
827       zccanopy(ik)     = ccanopy(kindex(ik))
828       zpetAcoef(ik)    = petAcoef(kindex(ik))
829       zpeqAcoef(ik)    = peqAcoef(kindex(ik))
830       zpetBcoef(ik)    = petBcoef(kindex(ik))
831       zpeqBcoef(ik)    = peqBcoef(kindex(ik))
832       zcdrag(ik)       = cdrag(kindex(ik))
833       zpb(ik)          = pb(kindex(ik))
834       
835    ENDDO
836    !
837    ! 2. save the grid
838    !
839    IF ( check ) WRITE(numout,*) 'Save the grid'
840    !
841    IF (l_first_intersurf) THEN
842       !
843       CALL histwrite(hist_id, 'LandPoints',  itau_sechiba+1, (/ REAL(kindex) /), kjpindex, kindex)
844       CALL histwrite(hist_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
845       IF ( control_flags%ok_stomate ) THEN
846          CALL histwrite(hist_id_stom, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
847          IF ( hist_id_stom_IPCC > 0 ) THEN
848             CALL histwrite(hist_id_stom_IPCC, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
849          ENDIF
850       ENDIF
851       CALL histwrite(hist_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
852       CALL histsync(hist_id)
853       !
854       IF ( hist2_id > 0 ) THEN
855          CALL histwrite(hist2_id, 'LandPoints',  itau_sechiba+1, (/ REAL(kindex) /), kjpindex, kindex)
856          CALL histwrite(hist2_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
857          CALL histwrite(hist2_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
858          CALL histsync(hist2_id)
859       ENDIF
860       !
861    ENDIF
862    !
863    ! 3. call sechiba
864    !
865    IF ( check ) WRITE(numout,*) 'Calling sechiba'
866    !
867    CALL sechiba_main (itau_sechiba, iim*jjm, kjpindex, kindex, xrdt, date0_shifted, &
868       & lrestart_read, lrestart_write, control_flags, &
869       & lalo, contfrac, neighbours, resolution, &
870! First level conditions
871! Ajout Nathalie - Juin 2006 - passage q2m/t2m pour calcul rveget
872!       & zzlev, zu, zv, zqair, ztemp_air, zepot_air, zccanopy, &
873       & zzlev, zu, zv, zqair, zqair, ztemp_air, ztemp_air, zepot_air, zccanopy, &
874! Variables for the implicit coupling
875       & zcdrag, zpetAcoef, zpeqAcoef, zpetBcoef, zpeqBcoef, &
876! Rain, snow, radiation and surface pressure
877       & zprecip_rain ,zprecip_snow,  zlwdown, zswnet, zswdown, zpb, &
878! Output : Fluxes
879       & zvevapp, zfluxsens, zfluxlat, zcoastal, zriver, znetco2, zcarblu, &
880! Surface temperatures and surface properties
881       & ztsol_rad, ztemp_sol_new, zqsurf, zalbedo, zemis, zz0, &
882! File ids
883       & rest_id, hist_id, hist2_id, rest_id_stom, hist_id_stom, hist_id_stom_IPCC ) 
884   
885    !
886    IF ( check ) WRITE(numout,*) 'out of SECHIBA'
887    !
888    ! 4. save watchout
889    !
890    IF ( ok_watchout .AND. .NOT. l_first_intersurf ) THEN
891       ! Accumulate last time step
892       sum_zlev(:) = sum_zlev(:) + zzlev(:)
893       sum_u(:) = sum_u(:) + zu(:)
894       sum_v(:) = sum_v(:) + zv(:)
895       sum_qair(:) = sum_qair(:) + zqair(:) 
896       sum_temp_air(:) = sum_temp_air(:) + ztemp_air(:)
897       sum_epot_air(:) = sum_epot_air(:) + zepot_air(:)
898       sum_ccanopy(:) = sum_ccanopy(:) + zccanopy(:)
899       sum_cdrag(:) = sum_cdrag(:) + zcdrag(:)
900       sum_petAcoef(:) = sum_petAcoef(:) + zpetAcoef(:)
901       sum_peqAcoef(:) = sum_peqAcoef(:) + zpeqAcoef(:)
902       sum_petBcoef(:) = sum_petBcoef(:) + zpetBcoef(:)
903       sum_peqBcoef(:) = sum_peqBcoef(:) + zpeqBcoef(:)
904       sum_rain(:) = sum_rain(:) + zprecip_rain(:)
905       sum_snow(:) = sum_snow(:) + zprecip_snow(:)
906       sum_lwdown(:) = sum_lwdown(:) + zlwdown(:)
907       sum_pb(:) = sum_pb(:) + zpb(:)
908       
909!!$       IF ( dt_watch > 3600 ) THEN
910!!$          julian_watch = date0_shifted+((itau_sechiba-0.5)/dt_split_watch)*dt_watch/one_day
911!!$          CALL solarang (julian_watch, julian0, iim, jjm, lon, lat, sinang)
912!!$          WHERE ( sinang(:,:) .LT. EPSILON(un) )
913!!$             isinang(:,:) = isinang(:,:) - 1
914!!$          ENDWHERE
915!!$          mean_sinang(:,:) = mean_sinang(:,:)+sinang(:,:)
916!!$          !
917!!$          DO ik=1,kjpindex         
918!!$             j = ((kindex(ik)-1)/iim) + 1
919!!$             i = (kindex(ik) - (j-1)*iim)
920!!$             
921!!$             sum_swnet(ik) = sum_swnet(ik) + sinang(i,j)*zswnet(ik)
922!!$             sum_swdown(ik) = sum_swdown(ik) + sinang(i,j)*zswdown(ik)
923!!$          ENDDO
924!!$       ELSE
925          sum_swnet(:) = sum_swnet(:) + zswnet(:)
926          sum_swdown(:) = sum_swdown(:) + zswdown(:)
927!!$       ENDIF
928         
929       do_watch = .FALSE.
930       call isittime &
931            &  (itau_sechiba,date0_shifted,xrdt,dt_watch,&
932            &   last_action_watch,last_check_watch,do_watch)
933       last_check_watch = itau_sechiba
934       IF (do_watch) THEN
935          !
936          IF ( check ) WRITE(numout,*) 'save watchout'
937          !
938          IF (long_print) THEN
939             WRITE(numout,*) "intersurf : write watchout for date ",date0,date0_shifted,itau_sechiba,&
940                  & last_action_watch, last_check_watch
941          ENDIF
942          last_action_watch = itau_sechiba
943
944          sum_zlev(:) = sum_zlev(:) / dt_split_watch
945          sum_u(:) = sum_u(:) / dt_split_watch
946          sum_v(:) = sum_v(:) / dt_split_watch
947          sum_qair(:) = sum_qair(:) / dt_split_watch
948          sum_temp_air(:) = sum_temp_air(:) / dt_split_watch
949          sum_epot_air(:) = sum_epot_air(:) / dt_split_watch
950          sum_ccanopy(:) = sum_ccanopy(:) / dt_split_watch
951          sum_cdrag(:) = sum_cdrag(:) / dt_split_watch
952          sum_petAcoef(:) = sum_petAcoef(:) / dt_split_watch
953          sum_peqAcoef(:) = sum_peqAcoef(:) / dt_split_watch
954          sum_petBcoef(:) = sum_petBcoef(:) / dt_split_watch
955          sum_peqBcoef(:) = sum_peqBcoef(:) / dt_split_watch
956          sum_rain(:) = sum_rain(:) / dt_split_watch
957          sum_snow(:) = sum_snow(:) / dt_split_watch
958          sum_lwdown(:) = sum_lwdown(:) / dt_split_watch
959          sum_pb(:) = sum_pb(:) / dt_split_watch
960
961!!$          IF ( dt_watch > 3600 ) THEN
962!!$             WHERE ( isinang(:,:) .GT. 0 )
963!!$                mean_sinang(:,:) = mean_sinang(:,:) / isinang(:,:)
964!!$             ENDWHERE
965!!$             !
966!!$             DO ik=1,kjpindex         
967!!$                j = ((kindex(ik)-1)/iim) + 1
968!!$                i = (kindex(ik) - (j-1)*iim)
969!!$                IF (mean_sinang(i,j) > zero) THEN
970!!$                   sum_swdown(ik) = sum_swdown(ik)/mean_sinang(i,j)
971!!$                   sum_swnet(ik) =  sum_swnet(ik)/mean_sinang(i,j)
972!!$                ELSE
973!!$                   sum_swdown(ik) = zero
974!!$                   sum_swnet(ik) =  zero
975!!$                ENDIF
976!!$             ENDDO
977!!$          ELSE
978             sum_swnet(:) = sum_swnet(:) / dt_split_watch
979             sum_swdown(:) = sum_swdown(:) / dt_split_watch
980!!$          ENDIF
981
982          CALL watchout_write_p(kjpindex, itau_sechiba, xrdt, sum_zlev, sum_swdown, sum_rain, &
983               &   sum_snow, sum_lwdown, sum_pb, sum_temp_air, sum_epot_air, sum_qair, &
984               &   sum_u, sum_v, sum_swnet, sum_petAcoef, sum_peqAcoef, sum_petBcoef, sum_peqBcoef, &
985               &   sum_cdrag, sum_ccanopy )
986       ENDIF
987    ENDIF
988    !
989    ! 5. scatter output fields
990    !
991    !
992    z0(:)           = undef_sechiba
993    coastalflow(:)  = undef_sechiba
994    riverflow(:)    = undef_sechiba
995    tsol_rad(:)     = undef_sechiba
996    vevapp(:)       = undef_sechiba
997    temp_sol_new(:) = undef_sechiba 
998    qsurf(:)        = undef_sechiba 
999    albedo(:,:)     = undef_sechiba
1000    fluxsens(:)     = undef_sechiba
1001    fluxlat(:)      = undef_sechiba
1002    emis(:)         = undef_sechiba 
1003    cdrag(:)        = undef_sechiba 
1004    dswnet(:)       = undef_sechiba 
1005    dswdown(:)      = undef_sechiba 
1006    !
1007    DO ik=1, kjpindex
1008       
1009       z0(kindex(ik))           = zz0(ik)
1010       coastalflow(kindex(ik))  = zcoastal(ik)/1000.
1011       riverflow(kindex(ik))    = zriver(ik)/1000.
1012       tsol_rad(kindex(ik))     = ztsol_rad(ik)
1013       vevapp(kindex(ik))       = zvevapp(ik)
1014       temp_sol_new(kindex(ik)) = ztemp_sol_new(ik)
1015       qsurf(kindex(ik))        = zqsurf(ik)
1016       albedo(kindex(ik),1)     = zalbedo(ik,1)
1017       albedo(kindex(ik),2)     = zalbedo(ik,2)
1018       fluxsens(kindex(ik))     = zfluxsens(ik)
1019       fluxlat(kindex(ik))      = zfluxlat(ik)
1020       emis(kindex(ik))         = zemis(ik)
1021       cdrag(kindex(ik))        = zcdrag(ik)
1022       dswnet(kindex(ik))       = zswnet(ik)
1023       dswdown(kindex(ik))      = zswdown(ik)
1024
1025    ENDDO
1026    !
1027    ! Modified fields for variables scattered during the writing
1028    !
1029    dcoastal(:) = (zcoastal(:))/1000.
1030    driver(:)   = (zriver(:))/1000.
1031    !
1032    IF ( .NOT. l_first_intersurf) THEN
1033       !
1034       IF ( .NOT. almaoutput ) THEN
1035          !
1036          !  scattered during the writing
1037          !
1038          CALL histwrite (hist_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
1039          CALL histwrite (hist_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
1040          CALL histwrite (hist_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
1041          !
1042          CALL histwrite (hist_id, 'temp_sol', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1043          CALL histwrite (hist_id, 'tsol_max', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1044          CALL histwrite (hist_id, 'tsol_min', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1045          CALL histwrite (hist_id, 'fluxsens', itau_sechiba, fluxsens, iim*jjm, kindex)
1046          CALL histwrite (hist_id, 'fluxlat',  itau_sechiba, fluxlat, iim*jjm, kindex)
1047          CALL histwrite (hist_id, 'swnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1048          CALL histwrite (hist_id, 'swdown',   itau_sechiba, dswdown, iim*jjm, kindex)
1049          CALL histwrite (hist_id, 'alb_vis',  itau_sechiba, albedo(:,1), iim*jjm, kindex)
1050          CALL histwrite (hist_id, 'alb_nir',  itau_sechiba, albedo(:,2), iim*jjm, kindex)
1051          CALL histwrite (hist_id, 'tair',     itau_sechiba, temp_air, iim*jjm, kindex)
1052          CALL histwrite (hist_id, 'qair',     itau_sechiba, qair, iim*jjm, kindex)
1053          ! Ajouts Nathalie - Juin 2006 - sauvegarde de t2m et q2m
1054          CALL histwrite (hist_id, 'q2m',     itau_sechiba, qair, iim*jjm, kindex)
1055          CALL histwrite (hist_id, 't2m',     itau_sechiba, temp_air, iim*jjm, kindex)
1056          IF ( hist2_id > 0 ) THEN
1057             CALL histwrite (hist2_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
1058             CALL histwrite (hist2_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
1059             CALL histwrite (hist2_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
1060             !
1061             CALL histwrite (hist2_id, 'temp_sol', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1062             CALL histwrite (hist2_id, 'tsol_max', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1063             CALL histwrite (hist2_id, 'tsol_min', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1064             CALL histwrite (hist2_id, 'fluxsens', itau_sechiba, fluxsens, iim*jjm, kindex)
1065             CALL histwrite (hist2_id, 'fluxlat',  itau_sechiba, fluxlat, iim*jjm, kindex)
1066             CALL histwrite (hist2_id, 'swnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1067             CALL histwrite (hist2_id, 'swdown',   itau_sechiba, dswdown, iim*jjm, kindex)
1068             CALL histwrite (hist2_id, 'alb_vis',  itau_sechiba, albedo(:,1), iim*jjm, kindex)
1069             CALL histwrite (hist2_id, 'alb_nir',  itau_sechiba, albedo(:,2), iim*jjm, kindex)
1070             CALL histwrite (hist2_id, 'tair',     itau_sechiba, temp_air, iim*jjm, kindex)
1071             CALL histwrite (hist2_id, 'qair',     itau_sechiba, qair, iim*jjm, kindex)
1072             CALL histwrite (hist2_id, 'q2m',     itau_sechiba, qair, iim*jjm, kindex)
1073             CALL histwrite (hist2_id, 't2m',     itau_sechiba, temp_air, iim*jjm, kindex)
1074          ENDIF
1075       ELSE
1076          CALL histwrite (hist_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
1077          CALL histwrite (hist_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1078          CALL histwrite (hist_id, 'Qh', itau_sechiba, fluxsens, iim*jjm, kindex)
1079          CALL histwrite (hist_id, 'Qle',  itau_sechiba, fluxlat, iim*jjm, kindex)
1080          CALL histwrite (hist_id, 'AvgSurfT', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1081          CALL histwrite (hist_id, 'RadT', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1082          IF ( hist2_id > 0 ) THEN
1083             CALL histwrite (hist2_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
1084             CALL histwrite (hist2_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1085             CALL histwrite (hist2_id, 'Qh', itau_sechiba, fluxsens, iim*jjm, kindex)
1086             CALL histwrite (hist2_id, 'Qle',  itau_sechiba, fluxlat, iim*jjm, kindex)
1087             CALL histwrite (hist2_id, 'AvgSurfT', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1088             CALL histwrite (hist2_id, 'RadT', itau_sechiba, temp_sol_NEW, iim*jjm, kindex)
1089          ENDIF
1090       ENDIF
1091       !
1092       IF (dw .EQ. xrdt) THEN
1093          CALL histsync(hist_id)
1094       ENDIF
1095       !
1096    ENDIF
1097    !
1098    ! 6. Transform the water fluxes into Kg/m^2s and m^3/s
1099    !
1100    DO ik=1, kjpindex
1101
1102       vevapp(kindex(ik)) = vevapp(kindex(ik))/xrdt
1103       coastalflow(kindex(ik)) = coastalflow(kindex(ik))/xrdt
1104       riverflow(kindex(ik)) = riverflow(kindex(ik))/xrdt
1105
1106    ENDDO
1107    !
1108    IF ( lrestart_write .AND. ok_watchout ) THEN
1109       CALL watchout_close()
1110    ENDIF
1111    !
1112    IF(l_first_intersurf .AND. is_root_prc) CALL getin_dump
1113    l_first_intersurf = .FALSE.
1114    !
1115    IF (long_print) WRITE (numout,*) ' intersurf_main done '
1116    !
1117    CALL ipslnlf(new_number=old_fileout)
1118    !   
1119  END SUBROUTINE intersurf_main_1d
1120!
1121!-------------------------------------------------------------------------------------
1122!
1123#ifdef CPP_PARA
1124  SUBROUTINE intersurf_gathered (kjit, iim_glo, jjm_glo, offset, kjpindex, kindex, communicator, xrdt, &
1125     & lrestart_read, lrestart_write, latlon, zcontfrac, zneighbours, zresolution, date0, &
1126! First level conditions
1127     & zlev,  u, v, qair, temp_air, epot_air, ccanopy, &
1128! Variables for the implicit coupling
1129     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
1130! Rain, snow, radiation and surface pressure
1131     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
1132! Output : Fluxes
1133     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
1134! Surface temperatures and surface properties
1135     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g) 
1136#else
1137  SUBROUTINE intersurf_gathered (kjit, iim_glo, jjm_glo, kjpindex, kindex, xrdt, &
1138     & lrestart_read, lrestart_write, latlon, zcontfrac, zneighbours, zresolution, date0, &
1139! First level conditions
1140     & zlev,  u, v, qair, temp_air, epot_air, ccanopy, &
1141! Variables for the implicit coupling
1142     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
1143! Rain, snow, radiation and surface pressure
1144     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
1145! Output : Fluxes
1146     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
1147! Surface temperatures and surface properties
1148     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g) 
1149#endif
1150    ! routines called : sechiba_main
1151    !
1152    IMPLICIT NONE
1153    !   
1154    ! interface description for dummy arguments
1155    ! input scalar
1156    INTEGER(i_std),INTENT (in)                            :: kjit          !! Time step number
1157    INTEGER(i_std),INTENT (in)                            :: iim_glo, jjm_glo  !! Dimension of global fields
1158#ifdef CPP_PARA
1159    INTEGER(i_std),INTENT (in)                            :: offset        !! offset between the first global 2D point
1160                                                                           !! and the first local 2D point.
1161    INTEGER(i_std),INTENT(IN)                             :: communicator  !! Orchidee communicator
1162#endif
1163    INTEGER(i_std),INTENT (in)                            :: kjpindex      !! Number of continental points
1164    REAL(r_std),INTENT (in)                               :: xrdt          !! Time step in seconds
1165    LOGICAL, INTENT (in)                                 :: lrestart_read !! Logical for _restart_ file to read
1166    LOGICAL, INTENT (in)                                 :: lrestart_write!! Logical for _restart_ file to write'
1167    REAL(r_std), INTENT (in)                              :: date0         !! Date at which kjit = 0
1168    ! input fields
1169    INTEGER(i_std),DIMENSION (kjpindex), INTENT (in)      :: kindex        !! Index for continental points
1170    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: u             !! Lowest level wind speed
1171    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: v             !! Lowest level wind speed
1172    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: zlev          !! Height of first layer
1173    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: qair          !! Lowest level specific humidity
1174    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: precip_rain   !! Rain precipitation
1175    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: precip_snow   !! Snow precipitation
1176    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: lwdown        !! Down-welling long-wave flux
1177    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: swnet         !! Net surface short-wave flux
1178    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: swdown        !! Downwelling surface short-wave flux
1179    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: temp_air      !! Air temperature in Kelvin
1180    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: epot_air      !! Air potential energy
1181    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: ccanopy       !! CO2 concentration in the canopy
1182    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: petAcoef      !! Coeficients A from the PBL resolution
1183    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: peqAcoef      !! One for T and another for q
1184    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: petBcoef      !! Coeficients B from the PBL resolution
1185    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: peqBcoef      !! One for T and another for q
1186    REAL(r_std),DIMENSION (kjpindex), INTENT(inout)       :: cdrag         !! Cdrag
1187    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: pb            !! Lowest level pressure
1188    REAL(r_std),DIMENSION (kjpindex,2), INTENT(in)        :: latlon        !! Geographical coordinates
1189    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: zcontfrac     !! Fraction of continent
1190    INTEGER(i_std),DIMENSION (kjpindex,8), INTENT(in)    :: zneighbours   !! neighbours
1191    REAL(r_std),DIMENSION (kjpindex,2), INTENT(in)        :: zresolution   !! size of the grid box
1192    ! output fields
1193    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: z0            !! Surface roughness
1194    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
1195    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
1196    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: tsol_rad      !! Radiative surface temperature
1197    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: vevapp        !! Total of evaporation
1198    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: temp_sol_new  !! New soil temperature
1199    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: qsurf         !! Surface specific humidity
1200    REAL(r_std),DIMENSION (kjpindex,2), INTENT(out)       :: albedo        !! Albedo
1201    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: fluxsens      !! Sensible chaleur flux
1202    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: fluxlat       !! Latent chaleur flux
1203    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: emis          !! Emissivity
1204    ! LOCAL declaration
1205    ! work arrays to scatter and/or gather information just before/after sechiba_main call's
1206    ! and to keep output value for next call
1207    REAL(r_std),DIMENSION (kjpindex)                      :: zccanopy      !! Work array to keep ccanopy
1208    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_rain  !! Work array to keep precip_rain
1209    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_snow  !! Work array to keep precip_snow
1210    REAL(r_std),DIMENSION (kjpindex)                      :: zz0           !! Work array to keep z0
1211    REAL(r_std),DIMENSION (kjpindex)                      :: zcdrag        !! Work array for surface drag
1212    REAL(r_std),DIMENSION (kjpindex)                      :: zcoastal      !! Work array to keep coastal flow
1213    REAL(r_std),DIMENSION (kjpindex)                      :: zriver        !! Work array to keep river out flow
1214    REAL(r_std),DIMENSION (kjpindex)                      :: dcoastal      !! Work array to keep coastal flow
1215    REAL(r_std),DIMENSION (kjpindex)                      :: driver        !! Work array to keep river out flow
1216    REAL(r_std),DIMENSION (kjpindex)                      :: znetco2       !! Work array to keep netco2flux
1217    REAL(r_std),DIMENSION (kjpindex)                      :: zcarblu       !! Work array to keep fco2_land_use
1218    REAL(r_std),DIMENSION (kjpindex)                      :: ztsol_rad     !! Work array to keep tsol_rad
1219    REAL(r_std),DIMENSION (kjpindex)                      :: zvevapp       !! Work array to keep vevapp
1220    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_sol_new !! Work array to keep temp_sol_new
1221    REAL(r_std),DIMENSION (kjpindex)                      :: zqsurf        !! Work array to keep qsurf
1222    REAL(r_std),DIMENSION (kjpindex,2)                    :: zalbedo       !! Work array to keep albedo
1223    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxsens     !! Work array to keep fluxsens
1224    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxlat      !! Work array to keep fluxlat
1225    REAL(r_std),DIMENSION (kjpindex)                      :: zemis         !! Work array to keep emis
1226    !
1227    ! Optional arguments
1228    !
1229    REAL(r_std),DIMENSION (iim_glo,jjm_glo), INTENT(IN), OPTIONAL :: lon_scat_g, lat_scat_g !! The scattered values for longitude
1230    !
1231    INTEGER(i_std)                          :: iim,jjm                                  !! local sizes
1232    REAL(r_std),DIMENSION (:,:),ALLOCATABLE :: lon_scat, lat_scat !! The scattered values for longitude
1233    !                                                                          !! and latitude.
1234    !
1235    ! Scattered variables for diagnostics
1236    !
1237!    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dvevapp       !! Diagnostic array for evaporation
1238    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dtemp_sol     !! for surface temperature
1239    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dfluxsens     !! for sensible heat flux
1240    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dfluxlat      !! for latent heat flux
1241    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dswnet        !! net solar radiation
1242    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dswdown       !! Incident solar radiation
1243    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:,:)                     :: dalbedo       !! albedo
1244    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dtair         !! air temperature
1245    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dqair         !! specific air humidity
1246    !
1247    !
1248    INTEGER(i_std)                                        :: i, j, ik
1249    INTEGER(i_std)                                        :: itau_sechiba
1250    REAL(r_std)                                           :: mx, zlev_mean
1251    REAL(r_std), ALLOCATABLE, DIMENSION(:,:)              :: tmp_lon, tmp_lat, tmp_lev
1252    LOGICAL                                               :: do_watch      !! if it's time, write watchout
1253    INTEGER                                               :: old_fileout   !! old Logical Int for std IO output
1254    LOGICAL :: check = .FALSE.
1255    INTEGER(i_std),DIMENSION (kjpindex)                  :: kindex_p
1256    !
1257    LOGICAL, SAVE                                         :: fatmco2       !! Flag to force the value of atmospheric CO2 for vegetation.
1258    REAL(r_std), SAVE                                     :: atmco2        !! atmospheric CO2
1259    !
1260    CALL ipslnlf(old_number=old_fileout)
1261    !
1262    IF (l_first_intersurf) THEN
1263       !
1264       CALL intsurf_time( kjit, date0, xrdt )
1265       !
1266       IF ( check ) WRITE(numout,*) 'Initialisation of intersurf'
1267       !
1268       CALL ioget_calendar (one_year, one_day)
1269       !
1270#ifdef CPP_PARA
1271       CALL init_para(.TRUE.,communicator)
1272       kindex_p(:)=kindex(:) + offset
1273#else
1274       CALL init_para(.FALSE.)
1275       kindex_p(:)=kindex(:)
1276#endif
1277       CALL ipslnlf(new_number=numout)
1278       !
1279       CALL init_data_para(iim_glo,jjm_glo,kjpindex,kindex_p)
1280       iim=iim_glo
1281       jjm=jj_nb
1282       ALLOCATE(lon_scat(iim,jjm))
1283       ALLOCATE(lat_scat(iim,jjm))
1284!       ALLOCATE(dvevapp(iim*jjm))
1285       ALLOCATE(dtemp_sol(iim*jjm))
1286       ALLOCATE(dfluxsens(iim*jjm))
1287       ALLOCATE(dfluxlat(iim*jjm))
1288       ALLOCATE(dswnet(iim*jjm))
1289       ALLOCATE(dswdown(iim*jjm))
1290       ALLOCATE(dalbedo(iim*jjm,2))
1291       ALLOCATE(dtair(iim*jjm))
1292       ALLOCATE(dqair(iim*jjm))
1293       
1294!       CALL init_WriteField_p(kindex)
1295       !
1296       ! Allocation of grid variables
1297       !
1298       CALL init_grid ( kjpindex )
1299       !
1300       !  Create the internal coordinate table
1301       !
1302       lalo(:,:) = latlon(:,:)
1303       CALL gather(lalo,lalo_g)
1304       !
1305       !-
1306       !- Store variable to help describe the grid
1307       !- once the points are gathered.
1308       !-
1309       neighbours(:,:) = zneighbours(:,:)
1310       CALL gather(neighbours,neighbours_g)
1311       !
1312       resolution(:,:) = zresolution(:,:)
1313       CALL gather(resolution,resolution_g)
1314       !
1315       area(:) = resolution(:,1)*resolution(:,2)
1316       CALL gather(area,area_g)
1317       !
1318       !- Store the fraction of the continents only once so that the user
1319       !- does not change them afterwards.
1320       !
1321       contfrac(:) = zcontfrac(:)
1322       CALL gather(contfrac,contfrac_g)
1323       !
1324       !
1325       !  Create the internal coordinate table
1326       !
1327       IF ( (.NOT.ALLOCATED(tmp_lon))) THEN
1328          ALLOCATE(tmp_lon(iim,jjm))
1329       ENDIF
1330       IF ( (.NOT.ALLOCATED(tmp_lat))) THEN
1331          ALLOCATE(tmp_lat(iim,jjm))
1332       ENDIF
1333       IF ( (.NOT.ALLOCATED(tmp_lev))) THEN
1334          ALLOCATE(tmp_lev(iim,jjm))
1335       ENDIF
1336       !
1337       !  Either we have the scattered coordinates as arguments or
1338       !  we have to do the work here.
1339       !
1340       IF ( PRESENT(lon_scat_g) .AND. PRESENT(lat_scat_g)) THEN
1341         
1342          lon_scat(:,:)=zero
1343          lat_scat(:,:)=zero 
1344          CALL scatter2D(lon_scat_g,lon_scat)
1345          CALL scatter2D(lat_scat_g,lat_scat)
1346          lon_scat(:,1)=lon_scat(:,2)
1347          lon_scat(:,jj_nb)=lon_scat(:,2)
1348          lat_scat(:,1)=lat_scat(iim,1)
1349          lat_scat(:,jj_nb)=lat_scat(1,jj_nb)
1350         
1351          tmp_lon(:,:) = lon_scat(:,:)
1352          tmp_lat(:,:) = lat_scat(:,:)
1353
1354          IF (is_root_prc) THEN
1355             lon_g(:,:) = lon_scat_g(:,:)
1356             lat_g(:,:) = lat_scat_g(:,:)
1357          ENDIF
1358
1359       ELSE IF ( PRESENT(lon_scat_g) .OR. PRESENT(lat_scat_g)) THEN
1360
1361          WRITE(numout,*) 'You need to provide the longitude AND latitude on the'
1362          WRITE(numout,*) 'gathered grid in order to start ORCHIDEE.'
1363          STOP 'intersurf_gathered'
1364
1365       ELSE
1366          !
1367          WRITE(numout,*) 'intersurf_gathered : We try to guess to full grid of the model.' 
1368          WRITE(numout,*) 'I might fail, please report if it does. '
1369          !
1370          tmp_lon(:,:) = val_exp
1371          tmp_lat(:,:) = val_exp
1372          !
1373          DO ik=1, kjpindex
1374             j = INT( (kindex(ik)-1) / iim ) + 1
1375             i = kindex(ik) - (j-1) * iim
1376             tmp_lon(i,j) = lalo(ik,2)
1377             tmp_lat(i,j) = lalo(ik,1)
1378          ENDDO
1379          !
1380          ! Here we fill out the grid. To do this we do the strong hypothesis
1381          ! that the grid is regular. Will this work in all cases ????
1382          !
1383          DO i=1,iim
1384             mx = MAXVAL(tmp_lon(i,:), MASK=tmp_lon(i,:) .LT. val_exp)
1385             IF ( mx .LT. val_exp ) THEN
1386                tmp_lon(i,:) = mx
1387             ELSE
1388                WRITE(numout,*) 'Could not find a continental point on this longitude. Thus the grid'
1389                WRITE(numout,*) 'could not be completed.'
1390                STOP 'intersurf_gathered'
1391             ENDIF
1392          ENDDO
1393          !
1394          DO j=1,jjm
1395             mx = MAXVAL(tmp_lat(:,j), MASK=tmp_lat(:,j) .LT. val_exp)
1396             IF ( mx .LT. val_exp ) THEN
1397                tmp_lat(:,j) = mx
1398             ELSE
1399                WRITE(numout,*) 'Could not find a continental point on this latitude. Thus the grid'
1400                WRITE(numout,*) 'could not be completed.'
1401                STOP 'intersurf_gathered'
1402             ENDIF
1403          ENDDO
1404
1405          CALL gather2D(tmp_lon,lon_g)
1406          CALL gather2D(tmp_lat,lat_g)
1407
1408       ENDIF
1409       !
1410       DO ik=1, kjpindex
1411          j = INT( (kindex(ik)-1) / iim ) + 1
1412          i = kindex(ik) - (j-1) * iim
1413          tmp_lev(i,j) = zlev(ik)
1414       ENDDO
1415       CALL gather2D(tmp_lev,zlev_g)
1416       !
1417       !
1418       !  Configuration of SSL specific parameters
1419       !
1420       CALL intsurf_config(control_flags,xrdt)
1421       !
1422       !Config  Key  = FORCE_CO2_VEG
1423       !Config  Desc = Flag to force the value of atmospheric CO2 for vegetation.
1424       !Config  Def  = FALSE
1425       !Config  Help = If this flag is set to true, the ATM_CO2 parameter is used
1426       !Config         to prescribe the atmospheric CO2.
1427       !Config         This Flag is only use in couple mode.
1428       !
1429       fatmco2=.FALSE.
1430       CALL getin_p('FORCE_CO2_VEG',fatmco2)
1431       !
1432       ! Next flag is only use in couple mode with a gcm in intersurf.
1433       ! In forced mode, it has already been read and set in driver.
1434       IF ( fatmco2 ) THEN
1435          !Config  Key  = ATM_CO2
1436          !Config  IF   = FORCE_CO2_VEG (in not forced mode)
1437          !Config  Desc = Value for atm CO2
1438          !Config  Def  = 350.
1439          !Config  Help = Value to prescribe the atm CO2.
1440          !Config         For pre-industrial simulations, the value is 286.2 .
1441          !Config         348. for 1990 year.
1442          !
1443          atmco2=350.
1444          CALL getin_p('ATM_CO2',atmco2)
1445          WRITE(numout,*) 'atmco2 ',atmco2
1446       ENDIF
1447       
1448       !
1449       CALL intsurf_restart(kjit, iim, jjm, tmp_lon, tmp_lat, date0, xrdt, control_flags, rest_id, rest_id_stom, itau_offset)
1450       itau_sechiba = kjit + itau_offset
1451       !
1452       CALL intsurf_history(iim, jjm, tmp_lon, tmp_lat, itau_sechiba, &
1453 &                          date0_shifted, xrdt, control_flags, hist_id, hist2_id, hist_id_stom, hist_id_stom_IPCC)
1454       !
1455       IF ( ok_watchout ) THEN
1456          IF (is_root_prc) THEN
1457             zlev_mean = zero
1458             DO ik=1, nbp_glo
1459                j = ((index_g(ik)-1)/iim_g) + 1
1460                i = (index_g(ik) - (j-1)*iim_g)
1461               
1462                zlev_mean = zlev_mean + zlev_g(i,j)
1463             ENDDO
1464             zlev_mean = zlev_mean / REAL(nbp_glo,r_std)
1465          ENDIF
1466
1467          last_action_watch = itau_sechiba
1468          last_check_watch =  last_action_watch
1469
1470          CALL watchout_init(iim_g, jjm_g, kjpindex, nbp_glo, &
1471               & date0_shifted, last_action_watch, dt_watch, index_g, lon_g, lat_g, zlev_mean)
1472       ENDIF
1473       !
1474       IF ( check ) WRITE(numout,*) 'End of Initialisation of intersurf'
1475       !
1476    ENDIF
1477    !
1478    CALL ipslnlf(new_number=numout)
1479    !
1480    !  Shift the time step to phase the two models
1481    !
1482    itau_sechiba = kjit + itau_offset
1483    !
1484    CALL intsurf_time( itau_sechiba, date0_shifted, xrdt )
1485    !
1486    ! 1. Just change the units of some input fields
1487    !
1488    DO ik=1, kjpindex
1489       
1490       zprecip_rain(ik) = precip_rain(ik)*xrdt
1491       zprecip_snow(ik) = precip_snow(ik)*xrdt
1492       zcdrag(ik)       = cdrag(ik)
1493       
1494    ENDDO
1495    !
1496    IF (check_INPUTS) THEN
1497       WRITE(numout,*) "Intersurf_main_gathered :"
1498       WRITE(numout,*) "Time step number = ",kjit
1499       WRITE(numout,*) "Dimension of input fields = ",iim, jjm
1500       WRITE(numout,*) "Number of continental points = ",kjpindex
1501       WRITE(numout,*) "Time step in seconds = ",xrdt
1502       WRITE(numout,*) "Logical for _restart_ file to read, write = ",lrestart_read,lrestart_write
1503       WRITE(numout,*) "Date at which kjit = 0  =  ",date0
1504       WRITE(numout,*) "Index for continental points = ",kindex
1505       WRITE(numout,*) "Lowest level wind speed North = ",u
1506       WRITE(numout,*) "Lowest level wind speed East = ",v
1507       WRITE(numout,*) "Height of first layer = ",zlev
1508       WRITE(numout,*) "Lowest level specific humidity = ",qair
1509       WRITE(numout,*) "Rain precipitation = ",zprecip_rain
1510       WRITE(numout,*) "Snow precipitation = ",zprecip_snow
1511       WRITE(numout,*) "Down-welling long-wave flux = ",lwdown
1512       WRITE(numout,*) "Net surface short-wave flux = ",swnet
1513       WRITE(numout,*) "Downwelling surface short-wave flux = ",swdown
1514       WRITE(numout,*) "Air temperature in Kelvin = ",temp_air
1515       WRITE(numout,*) "Air potential energy = ",epot_air
1516       WRITE(numout,*) "CO2 concentration in the canopy = ",ccanopy
1517       WRITE(numout,*) "Coeficients A from the PBL resolution = ",petAcoef
1518       WRITE(numout,*) "One for T and another for q = ",peqAcoef
1519       WRITE(numout,*) "Coeficients B from the PBL resolution = ",petBcoef
1520       WRITE(numout,*) "One for T and another for q = ",peqBcoef
1521       WRITE(numout,*) "Cdrag = ",zcdrag
1522       WRITE(numout,*) "Lowest level pressure = ",pb
1523       WRITE(numout,*) "Geographical coordinates lon = ", lon_scat
1524       WRITE(numout,*) "Geographical coordinates lat = ", lat_scat 
1525       WRITE(numout,*) "Fraction of continent in the grid = ",zcontfrac
1526    ENDIF
1527    !
1528    ! 2. modification of co2
1529    !
1530    IF ( fatmco2 ) THEN
1531       zccanopy(:) = atmco2
1532       WRITE (numout,*) 'Modification of the ccanopy value. CO2 = ',atmco2
1533    ELSE
1534       zccanopy(:) = ccanopy(:)
1535    ENDIF
1536    !
1537    ! 3. save the grid
1538    !
1539    IF ( check ) WRITE(numout,*) 'Save the grid'
1540    !
1541    IF (l_first_intersurf) THEN
1542       CALL histwrite(hist_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
1543       CALL histwrite(hist_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
1544       IF ( control_flags%ok_stomate ) THEN
1545            CALL histwrite(hist_id_stom, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
1546          IF ( hist_id_stom_IPCC > 0 ) THEN
1547             CALL histwrite(hist_id_stom_IPCC, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
1548          ENDIF
1549       ENDIF
1550       CALL histwrite(hist_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
1551       CALL histsync(hist_id)
1552       !
1553       IF ( hist2_id > 0 ) THEN
1554          CALL histwrite(hist2_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
1555          CALL histwrite(hist2_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
1556          CALL histwrite(hist2_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
1557          CALL histsync(hist2_id)
1558       ENDIF
1559       !
1560    ENDIF
1561    !
1562    ! 4. call sechiba for continental points only
1563    !
1564    IF ( check ) WRITE(numout,*) 'Calling sechiba'
1565    !
1566    CALL sechiba_main (itau_sechiba, iim*jjm, kjpindex, kindex, xrdt, date0_shifted, &
1567       & lrestart_read, lrestart_write, control_flags, &
1568       & lalo, contfrac, neighbours, resolution, &
1569! First level conditions
1570! Ajout Nathalie - Juin 2006 - passage q2m/t2m pour calcul rveget
1571!       & zlev, u, v, qair, temp_air, epot_air, ccanopy, &
1572       & zlev, u, v, qair, qair, temp_air, temp_air, epot_air, zccanopy, &
1573! Variables for the implicit coupling
1574       & zcdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
1575! Rain, snow, radiation and surface pressure
1576       & zprecip_rain ,zprecip_snow,  lwdown, swnet, swdown, pb, &
1577! Output : Fluxes
1578       & zvevapp, zfluxsens, zfluxlat, zcoastal, zriver, znetco2, zcarblu, &
1579! Surface temperatures and surface properties
1580       & ztsol_rad, ztemp_sol_new, zqsurf, zalbedo, zemis, zz0, &
1581! File ids
1582       & rest_id, hist_id, hist2_id, rest_id_stom, hist_id_stom, hist_id_stom_IPCC ) 
1583   
1584    !
1585    IF ( check ) WRITE(numout,*) 'out of SECHIBA'
1586    !
1587    ! 5. save watchout
1588    !
1589    IF ( ok_watchout .AND. .NOT. l_first_intersurf ) THEN
1590       ! Accumulate last time step
1591       sum_zlev(:) = sum_zlev(:) + zlev(:)
1592       sum_u(:) = sum_u(:) + u(:)
1593       sum_v(:) = sum_v(:) + v(:)
1594       sum_qair(:) = sum_qair(:) + qair(:) 
1595       sum_temp_air(:) = sum_temp_air(:) + temp_air(:)
1596       sum_epot_air(:) = sum_epot_air(:) + epot_air(:)
1597       sum_ccanopy(:) = sum_ccanopy(:) + ccanopy(:)
1598       sum_cdrag(:) = sum_cdrag(:) + zcdrag(:)
1599       sum_petAcoef(:) = sum_petAcoef(:) + petAcoef(:)
1600       sum_peqAcoef(:) = sum_peqAcoef(:) + peqAcoef(:)
1601       sum_petBcoef(:) = sum_petBcoef(:) + petBcoef(:)
1602       sum_peqBcoef(:) = sum_peqBcoef(:) + peqBcoef(:)
1603       sum_rain(:) = sum_rain(:) + zprecip_rain(:)
1604       sum_snow(:) = sum_snow(:) + zprecip_snow(:)
1605       sum_lwdown(:) = sum_lwdown(:) + lwdown(:)
1606       sum_pb(:) = sum_pb(:) + pb(:)
1607
1608!!$       IF ( dt_watch > 3600 ) THEN
1609!!$          julian_watch = date0_shifted+((itau_sechiba-0.5)/dt_split_watch)*dt_watch/one_day
1610!!$          CALL solarang (julian_watch, julian0, iim, jjm, tmp_lon, tmp_lat, sinang)
1611!!$          WHERE ( sinang(:,:) .LT. EPSILON(un) )
1612!!$             isinang(:,:) = isinang(:,:) - 1
1613!!$          ENDWHERE
1614!!$          mean_sinang(:,:) = mean_sinang(:,:)+sinang(:,:)
1615!!$          !
1616!!$          DO ik=1,kjpindex         
1617!!$             j = ((kindex(ik)-1)/iim) + 1
1618!!$             i = (kindex(ik) - (j-1)*iim)
1619!!$             
1620!!$             sum_swnet(ik) = sum_swnet(ik) + sinang(i,j)*swnet(ik)
1621!!$             sum_swdown(ik) = sum_swdown(ik) + sinang(i,j)*swdown(ik)
1622!!$          ENDDO
1623!!$       ELSE
1624          sum_swnet(:) = sum_swnet(:) + swnet(:)
1625          sum_swdown(:) = sum_swdown(:) + swdown(:)
1626!!$       ENDIF
1627       
1628       do_watch = .FALSE.
1629       call isittime &
1630            &  (itau_sechiba,date0_shifted,xrdt,dt_watch,&
1631            &   last_action_watch,last_check_watch,do_watch)
1632       last_check_watch = itau_sechiba
1633       IF (do_watch) THEN
1634          !
1635          IF ( check ) WRITE(numout,*) 'save watchout'
1636          !
1637          IF (long_print) THEN
1638             WRITE(numout,*) "intersurf : write watchout for date ",date0,date0_shifted,itau_sechiba, & 
1639                  & last_action_watch,last_check_watch
1640          ENDIF
1641          last_action_watch = itau_sechiba
1642
1643          sum_zlev(:) = sum_zlev(:) / dt_split_watch
1644          sum_u(:) = sum_u(:) / dt_split_watch
1645          sum_v(:) = sum_v(:) / dt_split_watch
1646          sum_qair(:) = sum_qair(:) / dt_split_watch
1647          sum_temp_air(:) = sum_temp_air(:) / dt_split_watch
1648          sum_epot_air(:) = sum_epot_air(:) / dt_split_watch
1649          sum_ccanopy(:) = sum_ccanopy(:) / dt_split_watch
1650          sum_cdrag(:) = sum_cdrag(:) / dt_split_watch
1651          sum_petAcoef(:) = sum_petAcoef(:) / dt_split_watch
1652          sum_peqAcoef(:) = sum_peqAcoef(:) / dt_split_watch
1653          sum_petBcoef(:) = sum_petBcoef(:) / dt_split_watch
1654          sum_peqBcoef(:) = sum_peqBcoef(:) / dt_split_watch
1655          sum_rain(:) = sum_rain(:) / dt_split_watch
1656          sum_snow(:) = sum_snow(:) / dt_split_watch
1657          sum_lwdown(:) = sum_lwdown(:) / dt_split_watch
1658          sum_pb(:) = sum_pb(:) / dt_split_watch
1659
1660!!$          IF ( dt_watch > 3600 ) THEN
1661!!$             WHERE ( isinang(:,:) .GT. 0 )
1662!!$                mean_sinang(:,:) = mean_sinang(:,:) / isinang(:,:)
1663!!$             ENDWHERE
1664!!$             !
1665!!$             DO ik=1,kjpindex         
1666!!$                j = ((kindex(ik)-1)/iim) + 1
1667!!$                i = (kindex(ik) - (j-1)*iim)
1668!!$                IF (mean_sinang(i,j) > zero) THEN
1669!!$                   sum_swdown(ik) = sum_swdown(ik)/mean_sinang(i,j)
1670!!$                   sum_swnet(ik) =  sum_swnet(ik)/mean_sinang(i,j)
1671!!$                ELSE
1672!!$                   sum_swdown(ik) = zero
1673!!$                   sum_swnet(ik) =  zero
1674!!$                ENDIF
1675!!$             ENDDO
1676!!$          ELSE
1677             sum_swnet(:) = sum_swnet(:) / dt_split_watch
1678             sum_swdown(:) = sum_swdown(:) / dt_split_watch
1679!!$          ENDIF
1680
1681          CALL watchout_write_p(kjpindex, itau_sechiba, xrdt, sum_zlev, sum_swdown, sum_rain, &
1682               &   sum_snow, sum_lwdown, sum_pb, sum_temp_air, sum_epot_air, sum_qair, &
1683               &   sum_u, sum_v, sum_swnet, sum_petAcoef, sum_peqAcoef, sum_petBcoef, sum_peqBcoef, &
1684               &   sum_cdrag, sum_ccanopy )
1685       ENDIF       
1686    ENDIF
1687    !
1688    ! 6. scatter output fields
1689    !
1690    z0(:)           = undef_sechiba
1691    coastalflow(:)  = undef_sechiba
1692    riverflow(:)    = undef_sechiba
1693    tsol_rad(:)     = undef_sechiba
1694    vevapp(:)       = undef_sechiba
1695    temp_sol_new(:) = undef_sechiba
1696    qsurf(:)        = undef_sechiba
1697    albedo(:,1)     = undef_sechiba
1698    albedo(:,2)     = undef_sechiba
1699    fluxsens(:)     = undef_sechiba
1700    fluxlat(:)      = undef_sechiba
1701    emis(:)         = undef_sechiba
1702    cdrag(:)        = undef_sechiba
1703    !   
1704!    dvevapp(:)    = undef_sechiba
1705    dtemp_sol(:)  = undef_sechiba
1706    dfluxsens(:)  = undef_sechiba
1707    dfluxlat(:)   = undef_sechiba
1708    dswnet (:)    = undef_sechiba
1709    dswdown (:)   = undef_sechiba
1710    dalbedo (:,1) = undef_sechiba
1711    dalbedo (:,2) = undef_sechiba
1712    dtair (:)     = undef_sechiba
1713    dqair (:)     = undef_sechiba
1714    !
1715    DO ik=1, kjpindex
1716       
1717       z0(ik)           = zz0(ik)
1718       coastalflow(ik)  = zcoastal(ik)/1000.
1719       riverflow(ik)    = zriver(ik)/1000.
1720       tsol_rad(ik)     = ztsol_rad(ik)
1721       vevapp(ik)       = zvevapp(ik)
1722       temp_sol_new(ik) = ztemp_sol_new(ik)
1723       qsurf(ik)        = zqsurf(ik)
1724       albedo(ik,1)     = zalbedo(ik,1)
1725       albedo(ik,2)     = zalbedo(ik,2)
1726       fluxsens(ik)     = zfluxsens(ik)
1727       fluxlat(ik)      = zfluxlat(ik)
1728       emis(ik)         = zemis(ik)
1729       cdrag(ik)        = zcdrag(ik)
1730       
1731       ! Fill up the diagnostic arrays
1732
1733!       dvevapp(kindex(ik))    = zvevapp(ik)
1734       dtemp_sol(kindex(ik))  = ztemp_sol_new(ik)
1735       dfluxsens(kindex(ik))  = zfluxsens(ik)
1736       dfluxlat(kindex(ik))   = zfluxlat(ik)
1737       dswnet (kindex(ik))    = swnet(ik)
1738       dswdown (kindex(ik))   = swdown(ik)
1739       dalbedo (kindex(ik),1) = zalbedo(ik,1)
1740       dalbedo (kindex(ik),2) = zalbedo(ik,2)   
1741       dtair (kindex(ik))     = temp_air(ik)
1742       dqair (kindex(ik))     = qair(ik)
1743       !
1744    ENDDO
1745    !
1746    ! Modified fields for variables scattered during the writing
1747    !
1748    dcoastal(:) = (zcoastal(:))/1000.
1749    driver(:)   = (zriver(:))/1000.
1750    !
1751    IF ( .NOT. l_first_intersurf) THEN
1752       !
1753       IF ( .NOT. almaoutput ) THEN
1754          !
1755          !  scattered during the writing
1756          !           
1757          CALL histwrite (hist_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
1758          CALL histwrite (hist_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
1759          CALL histwrite (hist_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
1760          !
1761          CALL histwrite (hist_id, 'temp_sol', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1762          CALL histwrite (hist_id, 'tsol_max', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1763          CALL histwrite (hist_id, 'tsol_min', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1764          CALL histwrite (hist_id, 'fluxsens', itau_sechiba, dfluxsens, iim*jjm, kindex)
1765          CALL histwrite (hist_id, 'fluxlat',  itau_sechiba, dfluxlat,  iim*jjm, kindex)
1766          CALL histwrite (hist_id, 'swnet',    itau_sechiba, dswnet,    iim*jjm, kindex)
1767          CALL histwrite (hist_id, 'swdown',   itau_sechiba, dswdown,   iim*jjm, kindex)
1768          CALL histwrite (hist_id, 'alb_vis',  itau_sechiba, dalbedo(:,1), iim*jjm, kindex)
1769          CALL histwrite (hist_id, 'alb_nir',  itau_sechiba, dalbedo(:,2), iim*jjm, kindex)
1770          CALL histwrite (hist_id, 'tair',     itau_sechiba, dtair, iim*jjm, kindex)
1771          CALL histwrite (hist_id, 'qair',     itau_sechiba, dqair, iim*jjm, kindex)
1772          CALL histwrite (hist_id, 't2m',      itau_sechiba, dtair, iim*jjm, kindex)
1773          CALL histwrite (hist_id, 'q2m',      itau_sechiba, dqair, iim*jjm, kindex)
1774          !
1775          IF ( hist2_id > 0 ) THEN
1776             CALL histwrite (hist2_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
1777             CALL histwrite (hist2_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
1778             CALL histwrite (hist2_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
1779             !
1780             CALL histwrite (hist2_id, 'temp_sol', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1781             CALL histwrite (hist2_id, 'tsol_max', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1782             CALL histwrite (hist2_id, 'tsol_min', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1783             CALL histwrite (hist2_id, 'fluxsens', itau_sechiba, dfluxsens, iim*jjm, kindex)
1784             CALL histwrite (hist2_id, 'fluxlat',  itau_sechiba, dfluxlat,  iim*jjm, kindex)
1785             CALL histwrite (hist2_id, 'swnet',    itau_sechiba, dswnet,    iim*jjm, kindex)
1786             CALL histwrite (hist2_id, 'swdown',   itau_sechiba, dswdown,   iim*jjm, kindex)
1787             CALL histwrite (hist2_id, 'alb_vis',  itau_sechiba, dalbedo(:,1), iim*jjm, kindex)
1788             CALL histwrite (hist2_id, 'alb_nir',  itau_sechiba, dalbedo(:,2), iim*jjm, kindex)
1789             CALL histwrite (hist2_id, 'tair',     itau_sechiba, dtair, iim*jjm, kindex)
1790             CALL histwrite (hist2_id, 'qair',     itau_sechiba, dqair, iim*jjm, kindex)
1791             CALL histwrite (hist2_id, 't2m',      itau_sechiba, dtair, iim*jjm, kindex)
1792             CALL histwrite (hist2_id, 'q2m',      itau_sechiba, dqair, iim*jjm, kindex)
1793          ENDIF
1794       ELSE
1795          CALL histwrite (hist_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
1796          CALL histwrite (hist_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1797          CALL histwrite (hist_id, 'Qh', itau_sechiba, dfluxsens, iim*jjm, kindex)
1798          CALL histwrite (hist_id, 'Qle',  itau_sechiba, dfluxlat, iim*jjm, kindex)
1799          CALL histwrite (hist_id, 'AvgSurfT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1800          CALL histwrite (hist_id, 'RadT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1801          !
1802          IF ( hist2_id > 0 ) THEN
1803             CALL histwrite (hist2_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
1804             CALL histwrite (hist2_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
1805             CALL histwrite (hist2_id, 'Qh', itau_sechiba, dfluxsens, iim*jjm, kindex)
1806             CALL histwrite (hist2_id, 'Qle',  itau_sechiba, dfluxlat, iim*jjm, kindex)
1807             CALL histwrite (hist2_id, 'AvgSurfT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1808             CALL histwrite (hist2_id, 'RadT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
1809          ENDIF
1810       ENDIF
1811       !
1812       IF (dw .EQ. xrdt) THEN
1813          CALL histsync(hist_id)
1814       ENDIF
1815    !
1816    ENDIF
1817    !
1818    ! 7. Transform the water fluxes into Kg/m^2s and m^3/s
1819    !
1820    DO ik=1, kjpindex
1821
1822       vevapp(ik) = vevapp(ik)/xrdt
1823       coastalflow(ik) = coastalflow(ik)/xrdt
1824       riverflow(ik) = riverflow(ik)/xrdt
1825
1826    ENDDO
1827    !
1828    IF ( lrestart_write .AND. ok_watchout .AND. is_root_prc ) THEN
1829       CALL watchout_close()
1830    ENDIF
1831    !
1832    IF(l_first_intersurf .AND. is_root_prc) CALL getin_dump
1833    l_first_intersurf = .FALSE.
1834    !
1835    IF (long_print) WRITE (numout,*) ' intersurf_main done '
1836    !
1837    CALL ipslnlf(new_number=old_fileout)
1838    !       
1839  END SUBROUTINE intersurf_gathered
1840!
1841!
1842#ifdef CPP_PARA
1843  SUBROUTINE intersurf_gathered_2m (kjit, iim_glo, jjm_glo, offset, kjpindex, kindex, communicator, xrdt, &
1844     & lrestart_read, lrestart_write, latlon, zcontfrac, zneighbours, zresolution, date0, &
1845! First level conditions
1846     & zlev,  u, v, qair, temp_air, epot_air, ccanopy, &
1847! Variables for the implicit coupling
1848     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
1849! Rain, snow, radiation and surface pressure
1850     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
1851! Output : Fluxes
1852     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
1853! Surface temperatures and surface properties
1854!     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g)
1855     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g, &
1856! Ajout Nathalie - passage q2m/t2m pour calcul Rveget
1857     & q2m, t2m, &
1858! Add emission/deposit fields
1859     & field_out_names, fields_out, field_in_names, fields_in) 
1860#else
1861  SUBROUTINE intersurf_gathered_2m (kjit, iim_glo, jjm_glo, kjpindex, kindex, xrdt, &
1862     & lrestart_read, lrestart_write, latlon, zcontfrac, zneighbours, zresolution, date0, &
1863! First level conditions
1864     & zlev,  u, v, qair, temp_air, epot_air, ccanopy, &
1865! Variables for the implicit coupling
1866     & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
1867! Rain, snow, radiation and surface pressure
1868     & precip_rain, precip_snow, lwdown, swnet, swdown, pb, &
1869! Output : Fluxes
1870     & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
1871! Surface temperatures and surface properties
1872!     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g)
1873     & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0, lon_scat_g, lat_scat_g, &
1874! Ajout Nathalie - passage q2m/t2m pour calcul Rveget
1875     & q2m, t2m, &
1876! Add emission/deposit fields
1877     & field_out_names, fields_out, field_in_names, fields_in)
1878#endif
1879    ! routines called : sechiba_main
1880    !
1881    IMPLICIT NONE
1882    !   
1883    ! interface description for dummy arguments
1884    ! input scalar
1885    INTEGER(i_std),INTENT (in)                            :: kjit          !! Time step number
1886    INTEGER(i_std),INTENT (in)                            :: iim_glo, jjm_glo  !! Dimension of global fields
1887#ifdef CPP_PARA
1888    INTEGER(i_std),INTENT (in)                            :: offset        !! offset between the first global 2D point
1889                                                                           !! and the first local 2D point.
1890    INTEGER(i_std),INTENT(IN)                             :: communicator  !! Orchidee communicator
1891#endif
1892    INTEGER(i_std),INTENT (in)                            :: kjpindex      !! Number of continental points
1893    REAL(r_std),INTENT (in)                               :: xrdt          !! Time step in seconds
1894    LOGICAL, INTENT (in)                                 :: lrestart_read !! Logical for _restart_ file to read
1895    LOGICAL, INTENT (in)                                 :: lrestart_write!! Logical for _restart_ file to write'
1896    REAL(r_std), INTENT (in)                              :: date0         !! Date at which kjit = 0
1897    ! input fields
1898    INTEGER(i_std),DIMENSION (kjpindex), INTENT (in)      :: kindex        !! Index for continental points
1899    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: u             !! Lowest level wind speed
1900    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: v             !! Lowest level wind speed
1901    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: zlev          !! Height of first layer
1902    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: qair          !! Lowest level specific humidity
1903    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: precip_rain   !! Rain precipitation
1904    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: precip_snow   !! Snow precipitation
1905    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: lwdown        !! Down-welling long-wave flux
1906    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: swnet         !! Net surface short-wave flux
1907    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: swdown        !! Downwelling surface short-wave flux
1908    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: temp_air      !! Air temperature in Kelvin
1909    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: epot_air      !! Air potential energy
1910    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: ccanopy       !! CO2 concentration in the canopy
1911    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: petAcoef      !! Coeficients A from the PBL resolution
1912    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: peqAcoef      !! One for T and another for q
1913    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: petBcoef      !! Coeficients B from the PBL resolution
1914    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: peqBcoef      !! One for T and another for q
1915    REAL(r_std),DIMENSION (kjpindex), INTENT(inout)       :: cdrag         !! Cdrag
1916    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: pb            !! Lowest level pressure
1917    REAL(r_std),DIMENSION (kjpindex,2), INTENT(in)        :: latlon        !! Geographical coordinates
1918    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: zcontfrac     !! Fraction of continent
1919    INTEGER(i_std),DIMENSION (kjpindex,8), INTENT(in)    :: zneighbours   !! neighbours
1920    REAL(r_std),DIMENSION (kjpindex,2), INTENT(in)        :: zresolution   !! size of the grid box
1921! Ajout Nathalie - Juin 2006 - q2m/t2m pour calcul Rveget
1922    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: q2m          !! Surface specific humidity
1923    REAL(r_std),DIMENSION (kjpindex), INTENT(in)          :: t2m          !! Surface air temperature
1924    ! output fields
1925    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: z0            !! Surface roughness
1926    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
1927    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
1928    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: tsol_rad      !! Radiative surface temperature
1929    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: vevapp        !! Total of evaporation
1930    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: temp_sol_new  !! New soil temperature
1931    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: qsurf         !! Surface specific humidity
1932    REAL(r_std),DIMENSION (kjpindex,2), INTENT(out)       :: albedo        !! Albedo
1933    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: fluxsens      !! Sensible chaleur flux
1934    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: fluxlat       !! Latent chaleur flux
1935    REAL(r_std),DIMENSION (kjpindex), INTENT(out)         :: emis          !! Emissivity
1936    !
1937    ! Optional arguments
1938    !
1939    ! Names and fields for emission variables : to be transport by GCM to chemistry model.
1940    CHARACTER(LEN=*),DIMENSION(:), OPTIONAL, INTENT(IN) :: field_out_names
1941    REAL(r_std),DIMENSION(:,:), OPTIONAL, INTENT(OUT) :: fields_out
1942    !
1943    ! Names and fields for deposit variables : to be transport from chemistry model by GCM to ORCHIDEE.
1944    CHARACTER(LEN=*),DIMENSION(:), OPTIONAL, INTENT(IN) :: field_in_names
1945    REAL(r_std),DIMENSION(:,:), OPTIONAL, INTENT(IN) :: fields_in
1946    !
1947    ! LOCAL declaration
1948    ! work arrays to scatter and/or gather information just before/after sechiba_main call's
1949    ! and to keep output value for next call
1950    REAL(r_std),DIMENSION (kjpindex)                      :: zccanopy      !! Work array to keep ccanopy
1951    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_rain  !! Work array to keep precip_rain
1952    REAL(r_std),DIMENSION (kjpindex)                      :: zprecip_snow  !! Work array to keep precip_snow
1953    REAL(r_std),DIMENSION (kjpindex)                      :: zz0           !! Work array to keep z0
1954    REAL(r_std),DIMENSION (kjpindex)                      :: zcdrag        !! Work array for surface drag
1955    REAL(r_std),DIMENSION (kjpindex)                      :: zcoastal      !! Work array to keep coastal flow
1956    REAL(r_std),DIMENSION (kjpindex)                      :: zriver        !! Work array to keep river out flow
1957    REAL(r_std),DIMENSION (kjpindex)                      :: dcoastal      !! Work array to keep coastal flow
1958    REAL(r_std),DIMENSION (kjpindex)                      :: driver        !! Work array to keep river out flow
1959    REAL(r_std),DIMENSION (kjpindex)                      :: znetco2       !! Work array to keep netco2flux
1960    REAL(r_std),DIMENSION (kjpindex)                      :: zcarblu       !! Work array to keep fco2_land_use
1961    REAL(r_std),DIMENSION (kjpindex)                      :: ztsol_rad     !! Work array to keep tsol_rad
1962    REAL(r_std),DIMENSION (kjpindex)                      :: zvevapp       !! Work array to keep vevapp
1963    REAL(r_std),DIMENSION (kjpindex)                      :: ztemp_sol_new !! Work array to keep temp_sol_new
1964    REAL(r_std),DIMENSION (kjpindex)                      :: zqsurf        !! Work array to keep qsurf
1965    REAL(r_std),DIMENSION (kjpindex,2)                    :: zalbedo       !! Work array to keep albedo
1966    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxsens     !! Work array to keep fluxsens
1967    REAL(r_std),DIMENSION (kjpindex)                      :: zfluxlat      !! Work array to keep fluxlat
1968    REAL(r_std),DIMENSION (kjpindex)                      :: zemis         !! Work array to keep emis
1969    !
1970    ! Optional arguments
1971    !
1972    REAL(r_std),DIMENSION (iim_glo,jjm_glo), INTENT(IN) :: lon_scat_g, lat_scat_g !! The scattered values for longitude
1973    !
1974    INTEGER(i_std)                          :: iim,jjm                                  !! local sizes
1975    REAL(r_std),DIMENSION (:,:),ALLOCATABLE :: lon_scat, lat_scat !! The scattered values for longitude
1976    !                                                                          !! and latitude.
1977    !
1978    ! Scattered variables for diagnostics
1979    !
1980!    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dvevapp       !! Diagnostic array for evaporation
1981    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dtemp_sol     !! for surface temperature
1982    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dfluxsens     !! for sensible heat flux
1983    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dfluxlat      !! for latent heat flux
1984    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dswnet        !! net solar radiation
1985    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dswdown       !! Incident solar radiation
1986    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:,:)                     :: dalbedo       !! albedo
1987    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dtair         !! air temperature
1988    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dqair         !! specific air humidity
1989    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dq2m          !! Surface specific humidity
1990    REAL(r_std),ALLOCATABLE,SAVE,DIMENSION (:)                       :: dt2m          !! Surface air temperature
1991    !
1992    !
1993    INTEGER(i_std)                                        :: i, j, ik
1994    INTEGER(i_std)                                        :: itau_sechiba
1995    REAL(r_std)                                           :: mx, zlev_mean
1996    REAL(r_std), ALLOCATABLE, DIMENSION(:,:)              :: tmp_lon, tmp_lat, tmp_lev
1997    LOGICAL                                               :: do_watch      !! if it's time, write watchout
1998    INTEGER                                               :: old_fileout   !! old Logical Int for std IO output
1999    LOGICAL :: check = .FALSE.
2000    INTEGER(i_std),DIMENSION (kjpindex)                  :: kindex_p
2001    !
2002    LOGICAL, SAVE                                         :: fatmco2       !! Flag to force the value of atmospheric CO2 for vegetation.
2003    REAL(r_std), SAVE                                     :: atmco2        !! atmospheric CO2
2004    !
2005    ! Number of fields to give (nb_fields_out) or get from (nb_fields_in) GCM :
2006    INTEGER(i_std), SAVE                                  :: nb_fields_out, nb_fields_in
2007    ! Id of fields to give (nb_fields_out) or get from (nb_fields_in) GCM :
2008    INTEGER(i_std)                                        :: i_fields_out, i_fields_in
2009    !
2010    CALL ipslnlf(old_number=old_fileout)
2011    !
2012    IF (l_first_intersurf) THEN
2013       !
2014       CALL intsurf_time( kjit, date0, xrdt )
2015       !
2016       IF ( check ) WRITE(numout,*) 'Initialisation of intersurf'
2017       !
2018       CALL ioget_calendar (one_year, one_day)
2019       !
2020#ifdef CPP_PARA
2021       CALL init_para(.TRUE.,communicator)
2022       kindex_p(:)=kindex(:) + offset
2023#else
2024       CALL init_para(.FALSE.)
2025       kindex_p(:)=kindex(:)
2026#endif
2027       CALL ipslnlf(new_number=numout)
2028       !
2029       CALL init_data_para(iim_glo,jjm_glo,kjpindex,kindex_p)
2030       iim=iim_glo
2031       jjm=jj_nb
2032       ALLOCATE(lon_scat(iim,jjm))
2033       ALLOCATE(lat_scat(iim,jjm))
2034!       ALLOCATE(dvevapp(iim*jjm))
2035       ALLOCATE(dtemp_sol(iim*jjm))
2036       ALLOCATE(dfluxsens(iim*jjm))
2037       ALLOCATE(dfluxlat(iim*jjm))
2038       ALLOCATE(dswnet(iim*jjm))
2039       ALLOCATE(dswdown(iim*jjm))
2040       ALLOCATE(dalbedo(iim*jjm,2))
2041       ALLOCATE(dtair(iim*jjm))
2042       ALLOCATE(dqair(iim*jjm)) 
2043       ALLOCATE(dq2m(iim*jjm))
2044       ALLOCATE(dt2m(iim*jjm))
2045     
2046!       CALL init_WriteField_p(kindex)
2047       !
2048       ! Allocation of grid variables
2049       !
2050       CALL init_grid ( kjpindex )
2051       !
2052       !  Create the internal coordinate table
2053       !
2054       lalo(:,:) = latlon(:,:)
2055       CALL gather(lalo,lalo_g)
2056       !
2057       !-
2058       !- Store variable to help describe the grid
2059       !- once the points are gathered.
2060       !-
2061       neighbours(:,:) = zneighbours(:,:)
2062       CALL gather(neighbours,neighbours_g)
2063       !
2064       resolution(:,:) = zresolution(:,:)
2065       CALL gather(resolution,resolution_g)
2066       !
2067       area(:) = resolution(:,1)*resolution(:,2)
2068       CALL gather(area,area_g)
2069       !
2070       !- Store the fraction of the continents only once so that the user
2071       !- does not change them afterwards.
2072       !
2073       contfrac(:) = zcontfrac(:)
2074       CALL gather(contfrac,contfrac_g)
2075       !
2076       !
2077       !  Create the internal coordinate table
2078       !
2079       IF ( (.NOT.ALLOCATED(tmp_lon))) THEN
2080          ALLOCATE(tmp_lon(iim,jjm))
2081       ENDIF
2082       IF ( (.NOT.ALLOCATED(tmp_lat))) THEN
2083          ALLOCATE(tmp_lat(iim,jjm))
2084       ENDIF
2085       IF ( (.NOT.ALLOCATED(tmp_lev))) THEN
2086          ALLOCATE(tmp_lev(iim,jjm))
2087       ENDIF
2088       !
2089       !  Either we have the scattered coordinates as arguments or
2090       !  we have to do the work here.
2091       !
2092       IF ( .TRUE. ) THEN
2093         
2094          lon_scat(:,:)=zero
2095          lat_scat(:,:)=zero 
2096          CALL scatter2D(lon_scat_g,lon_scat)
2097          CALL scatter2D(lat_scat_g,lat_scat)
2098          lon_scat(:,1)=lon_scat(:,2)
2099          lon_scat(:,jj_nb)=lon_scat(:,2)
2100          lat_scat(:,1)=lat_scat(iim,1)
2101          lat_scat(:,jj_nb)=lat_scat(1,jj_nb)
2102         
2103          tmp_lon(:,:) = lon_scat(:,:)
2104          tmp_lat(:,:) = lat_scat(:,:)
2105
2106          IF (is_root_prc) THEN
2107             lon_g(:,:) = lon_scat_g(:,:)
2108             lat_g(:,:) = lat_scat_g(:,:)
2109          ENDIF
2110
2111       ELSE
2112          !
2113          WRITE(numout,*) 'intersurf_gathered : We try to guess to full grid of the model.' 
2114          WRITE(numout,*) 'I might fail, please report if it does. '
2115          !
2116          tmp_lon(:,:) = val_exp
2117          tmp_lat(:,:) = val_exp
2118          !
2119          DO ik=1, kjpindex
2120             j = INT( (kindex(ik)-1) / iim ) + 1
2121             i = kindex(ik) - (j-1) * iim
2122             tmp_lon(i,j) = lalo(ik,2)
2123             tmp_lat(i,j) = lalo(ik,1)
2124          ENDDO
2125          !
2126          ! Here we fill out the grid. To do this we do the strong hypothesis
2127          ! that the grid is regular. Will this work in all cases ????
2128          !
2129          DO i=1,iim
2130             mx = MAXVAL(tmp_lon(i,:), MASK=tmp_lon(i,:) .LT. val_exp)
2131             IF ( mx .LT. val_exp ) THEN
2132                tmp_lon(i,:) = mx
2133             ELSE
2134                WRITE(numout,*) 'Could not find a continental point on this longitude. Thus the grid'
2135                WRITE(numout,*) 'could not be completed.'
2136                STOP 'intersurf_gathered'
2137             ENDIF
2138          ENDDO
2139          !
2140          DO j=1,jjm
2141             mx = MAXVAL(tmp_lat(:,j), MASK=tmp_lat(:,j) .LT. val_exp)
2142             IF ( mx .LT. val_exp ) THEN
2143                tmp_lat(:,j) = mx
2144             ELSE
2145                WRITE(numout,*) 'Could not find a continental point on this latitude. Thus the grid'
2146                WRITE(numout,*) 'could not be completed.'
2147                STOP 'intersurf_gathered'
2148             ENDIF
2149          ENDDO
2150
2151          CALL gather2D(tmp_lon,lon_g)
2152          CALL gather2D(tmp_lat,lat_g)
2153
2154       ENDIF
2155       !
2156       DO ik=1, kjpindex
2157          j = INT( (kindex(ik)-1) / iim ) + 1
2158          i = kindex(ik) - (j-1) * iim
2159          tmp_lev(i,j) = zlev(ik)
2160       ENDDO
2161       CALL gather2D(tmp_lev,zlev_g)
2162       !
2163       !
2164       !  Configuration of SSL specific parameters
2165       !
2166       CALL intsurf_config(control_flags,xrdt)
2167       !
2168       !Config  Key  = FORCE_CO2_VEG
2169       !Config  Desc = Flag to force the value of atmospheric CO2 for vegetation.
2170       !Config  Def  = FALSE
2171       !Config  Help = If this flag is set to true, the ATM_CO2 parameter is used
2172       !Config         to prescribe the atmospheric CO2.
2173       !Config         This Flag is only use in couple mode.
2174       !
2175       fatmco2=.FALSE.
2176       CALL getin_p('FORCE_CO2_VEG',fatmco2)
2177       !
2178       ! Next flag is only use in couple mode with a gcm in intersurf.
2179       ! In forced mode, it has already been read and set in driver.
2180       IF ( fatmco2 ) THEN
2181          !Config  Key  = ATM_CO2
2182          !Config  IF   = FORCE_CO2_VEG (in not forced mode)
2183          !Config  Desc = Value for atm CO2
2184          !Config  Def  = 350.
2185          !Config  Help = Value to prescribe the atm CO2.
2186          !Config         For pre-industrial simulations, the value is 286.2 .
2187          !Config         348. for 1990 year.
2188          !
2189          atmco2=350.
2190          CALL getin_p('ATM_CO2',atmco2)
2191          WRITE(numout,*) 'atmco2 ',atmco2
2192       ENDIF
2193       
2194       !
2195       CALL intsurf_restart(kjit, iim, jjm, tmp_lon, tmp_lat, date0, xrdt, control_flags, rest_id, rest_id_stom, itau_offset)
2196       itau_sechiba = kjit + itau_offset
2197       !
2198       CALL intsurf_history(iim, jjm, tmp_lon, tmp_lat, itau_sechiba, &
2199 &                          date0_shifted, xrdt, control_flags, hist_id, hist2_id, hist_id_stom, hist_id_stom_IPCC)
2200       !
2201       IF ( ok_watchout ) THEN
2202          IF (is_root_prc) THEN
2203             zlev_mean = zero
2204             DO ik=1, nbp_glo
2205                j = ((index_g(ik)-1)/iim_g) + 1
2206                i = (index_g(ik) - (j-1)*iim_g)
2207               
2208                zlev_mean = zlev_mean + zlev_g(i,j)
2209             ENDDO
2210             zlev_mean = zlev_mean / REAL(nbp_glo,r_std)
2211          ENDIF
2212
2213          last_action_watch = itau_sechiba
2214          last_check_watch =  last_action_watch
2215
2216          ! Only root proc write watchout file
2217          CALL watchout_init(iim_g, jjm_g, kjpindex, nbp_glo, &
2218               & date0_shifted, last_action_watch, dt_watch, index_g, lon_g, lat_g, zlev_mean)
2219       ENDIF
2220       !
2221
2222       ! Prepare fieds out/in for interface with GCM.
2223       IF (PRESENT(field_out_names)) THEN
2224          nb_fields_out=SIZE(field_out_names)
2225       ELSE
2226          nb_fields_out=0
2227       ENDIF
2228       IF (PRESENT(field_in_names)) THEN
2229          nb_fields_in=SIZE(field_in_names)
2230       ELSE
2231          nb_fields_in=0
2232       ENDIF
2233
2234       IF ( check ) WRITE(numout,*) 'End of Initialisation of intersurf'
2235       !
2236    ENDIF
2237    !
2238    CALL ipslnlf(new_number=numout)
2239    !
2240    !  Shift the time step to phase the two models
2241    !
2242    itau_sechiba = kjit + itau_offset
2243    !
2244    CALL intsurf_time( itau_sechiba, date0_shifted, xrdt )
2245    !
2246    ! 1. Just change the units of some input fields
2247    !
2248    DO ik=1, kjpindex
2249       
2250       zprecip_rain(ik) = precip_rain(ik)*xrdt
2251       zprecip_snow(ik) = precip_snow(ik)*xrdt
2252       zcdrag(ik)       = cdrag(ik)
2253       
2254    ENDDO
2255    !
2256    IF (check_INPUTS) THEN
2257       WRITE(numout,*) "Intersurf_main_gathered :"
2258       WRITE(numout,*) "Time step number = ",kjit
2259       WRITE(numout,*) "Dimension of input fields = ",iim, jjm
2260       WRITE(numout,*) "Number of continental points = ",kjpindex
2261       WRITE(numout,*) "Time step in seconds = ",xrdt
2262       WRITE(numout,*) "Logical for _restart_ file to read, write = ",lrestart_read,lrestart_write
2263       WRITE(numout,*) "Date at which kjit = 0  =  ",date0
2264       WRITE(numout,*) "Index for continental points = ",kindex
2265       WRITE(numout,*) "Lowest level wind speed North = ",u
2266       WRITE(numout,*) "Lowest level wind speed East = ",v
2267       WRITE(numout,*) "Height of first layer = ",zlev
2268       WRITE(numout,*) "Lowest level specific humidity = ",qair
2269       WRITE(numout,*) "Rain precipitation = ",zprecip_rain
2270       WRITE(numout,*) "Snow precipitation = ",zprecip_snow
2271       WRITE(numout,*) "Down-welling long-wave flux = ",lwdown
2272       WRITE(numout,*) "Net surface short-wave flux = ",swnet
2273       WRITE(numout,*) "Downwelling surface short-wave flux = ",swdown
2274       WRITE(numout,*) "Air temperature in Kelvin = ",temp_air
2275       WRITE(numout,*) "Air potential energy = ",epot_air
2276       WRITE(numout,*) "CO2 concentration in the canopy = ",ccanopy
2277       WRITE(numout,*) "Coeficients A from the PBL resolution = ",petAcoef
2278       WRITE(numout,*) "One for T and another for q = ",peqAcoef
2279       WRITE(numout,*) "Coeficients B from the PBL resolution = ",petBcoef
2280       WRITE(numout,*) "One for T and another for q = ",peqBcoef
2281       WRITE(numout,*) "Cdrag = ",zcdrag
2282       WRITE(numout,*) "Lowest level pressure = ",pb
2283       WRITE(numout,*) "Geographical coordinates lon = ", lon_scat
2284       WRITE(numout,*) "Geographical coordinates lat = ", lat_scat 
2285       WRITE(numout,*) "Fraction of continent in the grid = ",zcontfrac
2286    ENDIF
2287
2288
2289    ! Fields for deposit variables : to be transport from chemistry model by GCM to ORCHIDEE.
2290    WRITE(numout,*) "Get fields from atmosphere."
2291
2292    DO i_fields_in=1,nb_fields_in
2293       WRITE(numout,*) i_fields_in," Champ = ",TRIM(field_in_names(i_fields_in)) 
2294       SELECT CASE(TRIM(field_in_names(i_fields_in)))
2295       CASE DEFAULT 
2296          CALL ipslerr (3,'intsurf_gathered_2m', &
2297            &          'You ask in GCM an unknown field '//TRIM(field_in_names(i_fields_in))//&
2298            &          ' to give to ORCHIDEE for this specific version.',&
2299            &          'This model won''t be able to continue.', &
2300            &          '(check your tracer parameters in GCM)')
2301       END SELECT
2302    ENDDO
2303   
2304    !
2305    ! 2. modification of co2
2306    !
2307    IF ( fatmco2 ) THEN
2308       zccanopy(:) = atmco2
2309       WRITE (numout,*) 'Modification of the ccanopy value. CO2 = ',atmco2
2310    ELSE
2311       zccanopy(:) = ccanopy(:)
2312    ENDIF
2313    !
2314    ! 3. save the grid
2315    !
2316    IF ( check ) WRITE(numout,*) 'Save the grid'
2317    !
2318    IF (l_first_intersurf) THEN
2319       CALL histwrite(hist_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
2320       CALL histwrite(hist_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
2321       IF ( control_flags%ok_stomate ) THEN
2322          CALL histwrite(hist_id_stom, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
2323          IF ( hist_id_stom_ipcc > 0 ) &
2324               CALL histwrite(hist_id_stom_IPCC, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
2325       ENDIF
2326       CALL histwrite(hist_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
2327       CALL histsync(hist_id)
2328       !
2329       IF ( hist2_id > 0 ) THEN
2330          CALL histwrite(hist2_id, 'LandPoints',  itau_sechiba+1, (/ ( REAL(ik), ik=1,kjpindex ) /), kjpindex, kindex)
2331          CALL histwrite(hist2_id, 'Areas',  itau_sechiba+1, area, kjpindex, kindex)
2332          CALL histwrite(hist2_id, 'Contfrac',  itau_sechiba+1, contfrac, kjpindex, kindex)
2333          CALL histsync(hist2_id)
2334       ENDIF
2335       !
2336    ENDIF
2337    !
2338    ! 4. call sechiba for continental points only
2339    !
2340    IF ( check ) WRITE(numout,*) 'Calling sechiba'
2341    !
2342    CALL sechiba_main (itau_sechiba, iim*jjm, kjpindex, kindex, xrdt, date0_shifted, &
2343       & lrestart_read, lrestart_write, control_flags, &
2344       & lalo, contfrac, neighbours, resolution, &
2345! First level conditions
2346! Ajout Nathalie - Juin 2006 - passage q2m/t2m pour calcul rveget
2347!       & zlev, u, v, qair, temp_air, epot_air, ccanopy, &
2348       & zlev, u, v, qair, q2m, t2m, temp_air, epot_air, zccanopy, &
2349! Variables for the implicit coupling
2350       & zcdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
2351! Rain, snow, radiation and surface pressure
2352       & zprecip_rain ,zprecip_snow,  lwdown, swnet, swdown, pb, &
2353! Output : Fluxes
2354       & zvevapp, zfluxsens, zfluxlat, zcoastal, zriver, znetco2, zcarblu, &
2355! Surface temperatures and surface properties
2356       & ztsol_rad, ztemp_sol_new, zqsurf, zalbedo, zemis, zz0, &
2357! File ids
2358       & rest_id, hist_id, hist2_id, rest_id_stom, hist_id_stom, hist_id_stom_IPCC ) 
2359   
2360    !
2361    IF ( check ) WRITE(numout,*) 'out of SECHIBA'
2362    !
2363    ! 5. save watchout
2364    !
2365    IF ( ok_watchout .AND. .NOT. l_first_intersurf ) THEN
2366       ! Accumulate last time step
2367       sum_zlev(:) = sum_zlev(:) + zlev(:)
2368       sum_u(:) = sum_u(:) + u(:)
2369       sum_v(:) = sum_v(:) + v(:)
2370       sum_qair(:) = sum_qair(:) + qair(:) 
2371       sum_temp_air(:) = sum_temp_air(:) + temp_air(:)
2372       sum_epot_air(:) = sum_epot_air(:) + epot_air(:)
2373       sum_ccanopy(:) = sum_ccanopy(:) + ccanopy(:)
2374       sum_cdrag(:) = sum_cdrag(:) + zcdrag(:)
2375       sum_petAcoef(:) = sum_petAcoef(:) + petAcoef(:)
2376       sum_peqAcoef(:) = sum_peqAcoef(:) + peqAcoef(:)
2377       sum_petBcoef(:) = sum_petBcoef(:) + petBcoef(:)
2378       sum_peqBcoef(:) = sum_peqBcoef(:) + peqBcoef(:)
2379       sum_rain(:) = sum_rain(:) + zprecip_rain(:)
2380       sum_snow(:) = sum_snow(:) + zprecip_snow(:)
2381       sum_lwdown(:) = sum_lwdown(:) + lwdown(:)
2382       sum_pb(:) = sum_pb(:) + pb(:)
2383
2384!!$       IF ( dt_watch > 3600 ) THEN
2385!!$          julian_watch = date0_shifted+((itau_sechiba-0.5)/dt_split_watch)*dt_watch/one_day
2386!!$          CALL solarang (julian_watch, julian0, iim, jjm, tmp_lon, tmp_lat, sinang)
2387!!$          WHERE ( sinang(:,:) .LT. EPSILON(un) )
2388!!$             isinang(:,:) = isinang(:,:) - 1
2389!!$          ENDWHERE
2390!!$          mean_sinang(:,:) = mean_sinang(:,:)+sinang(:,:)
2391!!$          !
2392!!$          DO ik=1,kjpindex         
2393!!$             j = ((kindex(ik)-1)/iim) + 1
2394!!$             i = (kindex(ik) - (j-1)*iim)
2395!!$             
2396!!$             sum_swnet(ik) = sum_swnet(ik) + sinang(i,j)*swnet(ik)
2397!!$             sum_swdown(ik) = sum_swdown(ik) + sinang(i,j)*swdown(ik)
2398!!$          ENDDO
2399!!$       ELSE
2400          sum_swnet(:) = sum_swnet(:) + swnet(:)
2401          sum_swdown(:) = sum_swdown(:) + swdown(:)
2402!!$       ENDIF
2403         
2404       do_watch = .FALSE.
2405       call isittime &
2406            &  (itau_sechiba,date0_shifted,xrdt,dt_watch,&
2407            &   last_action_watch,last_check_watch,do_watch)
2408       last_check_watch = itau_sechiba
2409       IF (do_watch) THEN
2410          !
2411          IF ( check ) WRITE(numout,*) 'save watchout'
2412          !
2413          IF (long_print) THEN
2414             WRITE(numout,*) "intersurf : write watchout for date ",date0,date0_shifted,itau_sechiba, &
2415                  & last_action_watch,last_check_watch
2416          ENDIF
2417          last_action_watch = itau_sechiba
2418
2419          sum_zlev(:) = sum_zlev(:) / dt_split_watch
2420          sum_u(:) = sum_u(:) / dt_split_watch
2421          sum_v(:) = sum_v(:) / dt_split_watch
2422          sum_qair(:) = sum_qair(:) / dt_split_watch
2423          sum_temp_air(:) = sum_temp_air(:) / dt_split_watch
2424          sum_epot_air(:) = sum_epot_air(:) / dt_split_watch
2425          sum_ccanopy(:) = sum_ccanopy(:) / dt_split_watch
2426          sum_cdrag(:) = sum_cdrag(:) / dt_split_watch
2427          sum_petAcoef(:) = sum_petAcoef(:) / dt_split_watch
2428          sum_peqAcoef(:) = sum_peqAcoef(:) / dt_split_watch
2429          sum_petBcoef(:) = sum_petBcoef(:) / dt_split_watch
2430          sum_peqBcoef(:) = sum_peqBcoef(:) / dt_split_watch
2431          sum_rain(:) = sum_rain(:) / dt_split_watch
2432          sum_snow(:) = sum_snow(:) / dt_split_watch
2433          sum_lwdown(:) = sum_lwdown(:) / dt_split_watch
2434          sum_pb(:) = sum_pb(:) / dt_split_watch
2435
2436!!$          IF ( dt_watch > 3600 ) THEN
2437!!$             WHERE ( isinang(:,:) .GT. 0 )
2438!!$                mean_sinang(:,:) = mean_sinang(:,:) / isinang(:,:)
2439!!$             ENDWHERE
2440!!$             !
2441!!$             DO ik=1,kjpindex         
2442!!$                j = ((kindex(ik)-1)/iim) + 1
2443!!$                i = (kindex(ik) - (j-1)*iim)
2444!!$                IF (mean_sinang(i,j) > zero) THEN
2445!!$                   sum_swdown(ik) = sum_swdown(ik)/mean_sinang(i,j)
2446!!$                   sum_swnet(ik) =  sum_swnet(ik)/mean_sinang(i,j)
2447!!$                ELSE
2448!!$                   sum_swdown(ik) = zero
2449!!$                   sum_swnet(ik) =  zero
2450!!$                ENDIF
2451!!$             ENDDO
2452!!$          ELSE
2453             sum_swnet(:) = sum_swnet(:) / dt_split_watch
2454             sum_swdown(:) = sum_swdown(:) / dt_split_watch
2455!!$          ENDIF
2456
2457          CALL watchout_write_p(kjpindex, itau_sechiba, xrdt, sum_zlev, sum_swdown, sum_rain, &
2458               &   sum_snow, sum_lwdown, sum_pb, sum_temp_air, sum_epot_air, sum_qair, &
2459               &   sum_u, sum_v, sum_swnet, sum_petAcoef, sum_peqAcoef, sum_petBcoef, sum_peqBcoef, &
2460               &   sum_cdrag, sum_ccanopy )
2461       ENDIF       
2462    ENDIF
2463    !
2464    ! 6. scatter output fields
2465    !
2466    z0(:)           = undef_sechiba
2467    coastalflow(:)  = undef_sechiba
2468    riverflow(:)    = undef_sechiba
2469    tsol_rad(:)     = undef_sechiba
2470    vevapp(:)       = undef_sechiba
2471    temp_sol_new(:) = undef_sechiba
2472    qsurf(:)        = undef_sechiba
2473    albedo(:,1)     = undef_sechiba
2474    albedo(:,2)     = undef_sechiba
2475    fluxsens(:)     = undef_sechiba
2476    fluxlat(:)      = undef_sechiba
2477    emis(:)         = undef_sechiba
2478    cdrag(:)        = undef_sechiba
2479    !   
2480!    dvevapp(:)    = undef_sechiba
2481    dtemp_sol(:)  = undef_sechiba
2482    dfluxsens(:)  = undef_sechiba
2483    dfluxlat(:)   = undef_sechiba
2484    dswnet (:)    = undef_sechiba
2485    dswdown (:)   = undef_sechiba
2486    dalbedo (:,1) = undef_sechiba
2487    dalbedo (:,2) = undef_sechiba
2488    dtair (:)     = undef_sechiba
2489    dqair (:)     = undef_sechiba
2490    dt2m (:)      = undef_sechiba
2491    dq2m (:)      = undef_sechiba
2492    !
2493    DO ik=1, kjpindex
2494       
2495       z0(ik)           = zz0(ik)
2496       coastalflow(ik)  = zcoastal(ik)/1000.
2497       riverflow(ik)    = zriver(ik)/1000.
2498       tsol_rad(ik)     = ztsol_rad(ik)
2499       vevapp(ik)       = zvevapp(ik)
2500       temp_sol_new(ik) = ztemp_sol_new(ik)
2501       qsurf(ik)        = zqsurf(ik)
2502       albedo(ik,1)     = zalbedo(ik,1)
2503       albedo(ik,2)     = zalbedo(ik,2)
2504       fluxsens(ik)     = zfluxsens(ik)
2505       fluxlat(ik)      = zfluxlat(ik)
2506       emis(ik)         = zemis(ik)
2507       cdrag(ik)        = zcdrag(ik)
2508       
2509       ! Fill up the diagnostic arrays
2510
2511!       dvevapp(kindex(ik))    = zvevapp(ik)
2512       dtemp_sol(kindex(ik))  = ztemp_sol_new(ik)
2513       dfluxsens(kindex(ik))  = zfluxsens(ik)
2514       dfluxlat(kindex(ik))   = zfluxlat(ik)
2515       dswnet (kindex(ik))    = swnet(ik)
2516       dswdown (kindex(ik))   = swdown(ik)
2517       dalbedo (kindex(ik),1) = zalbedo(ik,1)
2518       dalbedo (kindex(ik),2) = zalbedo(ik,2)   
2519       dtair (kindex(ik))     = temp_air(ik)
2520       dqair (kindex(ik))     = qair(ik)
2521       dt2m (kindex(ik))      = t2m(ik)
2522       dq2m (kindex(ik))      = q2m(ik)
2523       !
2524    ENDDO
2525    !
2526    ! Modified fields for variables scattered during the writing
2527    !
2528    dcoastal(:) = (zcoastal(:))/1000.
2529    driver(:)   = (zriver(:))/1000.
2530    !
2531    IF ( .NOT. l_first_intersurf) THEN
2532       !
2533       IF ( .NOT. almaoutput ) THEN
2534          !
2535          !  scattered during the writing
2536          !           
2537          CALL histwrite (hist_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
2538          CALL histwrite (hist_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
2539          CALL histwrite (hist_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
2540          !
2541          CALL histwrite (hist_id, 'temp_sol', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2542          CALL histwrite (hist_id, 'tsol_max', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2543          CALL histwrite (hist_id, 'tsol_min', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2544          CALL histwrite (hist_id, 'fluxsens', itau_sechiba, dfluxsens, iim*jjm, kindex)
2545          CALL histwrite (hist_id, 'fluxlat',  itau_sechiba, dfluxlat,  iim*jjm, kindex)
2546          CALL histwrite (hist_id, 'swnet',    itau_sechiba, dswnet,    iim*jjm, kindex)
2547          CALL histwrite (hist_id, 'swdown',   itau_sechiba, dswdown,   iim*jjm, kindex)
2548          CALL histwrite (hist_id, 'alb_vis',  itau_sechiba, dalbedo(:,1), iim*jjm, kindex)
2549          CALL histwrite (hist_id, 'alb_nir',  itau_sechiba, dalbedo(:,2), iim*jjm, kindex)
2550          CALL histwrite (hist_id, 'tair',     itau_sechiba, dtair, iim*jjm, kindex)
2551          CALL histwrite (hist_id, 'qair',     itau_sechiba, dqair, iim*jjm, kindex)
2552          CALL histwrite (hist_id, 't2m',      itau_sechiba, dq2m, iim*jjm, kindex)
2553          CALL histwrite (hist_id, 'q2m',      itau_sechiba, dt2m, iim*jjm, kindex)
2554          !
2555          IF ( hist2_id > 0 ) THEN
2556             CALL histwrite (hist2_id, 'evap',     itau_sechiba, zvevapp, kjpindex, kindex)
2557             CALL histwrite (hist2_id, 'coastalflow',itau_sechiba, dcoastal, kjpindex, kindex)
2558             CALL histwrite (hist2_id, 'riverflow',itau_sechiba, driver, kjpindex, kindex)
2559             !
2560             CALL histwrite (hist2_id, 'temp_sol', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2561             CALL histwrite (hist2_id, 'tsol_max', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2562             CALL histwrite (hist2_id, 'tsol_min', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2563             CALL histwrite (hist2_id, 'fluxsens', itau_sechiba, dfluxsens, iim*jjm, kindex)
2564             CALL histwrite (hist2_id, 'fluxlat',  itau_sechiba, dfluxlat,  iim*jjm, kindex)
2565             CALL histwrite (hist2_id, 'swnet',    itau_sechiba, dswnet,    iim*jjm, kindex)
2566             CALL histwrite (hist2_id, 'swdown',   itau_sechiba, dswdown,   iim*jjm, kindex)
2567             CALL histwrite (hist2_id, 'alb_vis',  itau_sechiba, dalbedo(:,1), iim*jjm, kindex)
2568             CALL histwrite (hist2_id, 'alb_nir',  itau_sechiba, dalbedo(:,2), iim*jjm, kindex)
2569             CALL histwrite (hist2_id, 'tair',     itau_sechiba, dtair, iim*jjm, kindex)
2570             CALL histwrite (hist2_id, 'qair',     itau_sechiba, dqair, iim*jjm, kindex)
2571             CALL histwrite (hist2_id, 't2m',      itau_sechiba, dq2m, iim*jjm, kindex)
2572             CALL histwrite (hist2_id, 'q2m',      itau_sechiba, dt2m, iim*jjm, kindex)
2573          ENDIF
2574       ELSE
2575          CALL histwrite (hist_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
2576          CALL histwrite (hist_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
2577          CALL histwrite (hist_id, 'Qh', itau_sechiba, dfluxsens, iim*jjm, kindex)
2578          CALL histwrite (hist_id, 'Qle',  itau_sechiba, dfluxlat, iim*jjm, kindex)
2579          CALL histwrite (hist_id, 'AvgSurfT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2580          CALL histwrite (hist_id, 'RadT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2581          !
2582          IF ( hist2_id > 0 ) THEN
2583             CALL histwrite (hist2_id, 'Evap', itau_sechiba, zvevapp, kjpindex, kindex)
2584             CALL histwrite (hist2_id, 'SWnet',    itau_sechiba, dswnet, iim*jjm, kindex)
2585             CALL histwrite (hist2_id, 'Qh', itau_sechiba, dfluxsens, iim*jjm, kindex)
2586             CALL histwrite (hist2_id, 'Qle',  itau_sechiba, dfluxlat, iim*jjm, kindex)
2587             CALL histwrite (hist2_id, 'AvgSurfT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2588             CALL histwrite (hist2_id, 'RadT', itau_sechiba, dtemp_sol, iim*jjm, kindex)
2589          ENDIF
2590       ENDIF
2591       !
2592       IF (dw .EQ. xrdt) THEN
2593          CALL histsync(hist_id)
2594       ENDIF
2595    !
2596    ENDIF
2597    !
2598    ! 7. Transform the water fluxes into Kg/m^2s and m^3/s
2599    !
2600    DO ik=1, kjpindex
2601
2602       vevapp(ik) = vevapp(ik)/xrdt
2603       coastalflow(ik) = coastalflow(ik)/xrdt
2604       riverflow(ik) = riverflow(ik)/xrdt
2605
2606    ENDDO
2607    !
2608    WRITE(numout,*) "Give fields to atmosphere."
2609   
2610    ! Fields for emission variables : to be transport by GCM to chemistry model.
2611    DO i_fields_out=1,nb_fields_out
2612       SELECT CASE(TRIM(field_out_names(i_fields_out)))
2613       CASE("fCO2_land") 
2614          fields_out(:,i_fields_out)=znetco2(:)
2615       CASE("fCO2_land_use")
2616          fields_out(:,i_fields_out)=zcarblu(:)
2617       CASE DEFAULT 
2618          CALL ipslerr (3,'intsurf_gathered_2m', &
2619            &          'You ask from GCM an unknown field '//TRIM(field_out_names(i_fields_out))//&
2620            &          ' to ORCHIDEE for this specific version.',&
2621            &          'This model won''t be able to continue.', &
2622            &          '(check your tracer parameters in GCM)')
2623       END SELECT
2624    ENDDO
2625    !
2626    IF ( lrestart_write .AND. ok_watchout .AND. is_root_prc ) THEN
2627       CALL watchout_close()
2628    ENDIF
2629    !
2630    IF(l_first_intersurf .AND. is_root_prc) CALL getin_dump
2631    l_first_intersurf = .FALSE.
2632    !
2633    IF (long_print) WRITE (numout,*) ' intersurf_main done '
2634    !
2635    CALL ipslnlf(new_number=old_fileout)
2636    !       
2637  END SUBROUTINE intersurf_gathered_2m
2638!
2639  !-------------------------------------------------------------------------------------
2640  !
2641  SUBROUTINE intsurf_time(istp, date0, dt)
2642    !
2643    !  This subroutine initialized the time global variables in grid module.
2644    !
2645    IMPLICIT NONE
2646    !
2647    INTEGER(i_std), INTENT(in)                  :: istp      !! Time step of the restart file
2648    REAL(r_std), INTENT(in)                     :: date0     !! The date at which itau = 0
2649    REAL(r_std), INTENT(in)                     :: dt        !! Time step
2650    !
2651
2652    IF (l_first_intersurf) THEN
2653       CALL ioget_calendar(calendar_str)
2654       CALL ioget_calendar(one_year, one_day)
2655       CALL tlen2itau('1Y',dt,date0,year_length)
2656       IF ( TRIM(calendar_str) .EQ. 'gregorian' ) THEN 
2657          year_spread=un
2658       ELSE
2659          year_spread = one_year/365.2425
2660       ENDIF
2661
2662       IF (check_time) THEN
2663          write(numout,*) "calendar_str =",calendar_str
2664          write(numout,*) "one_year=",one_year,", one_day=",one_day
2665          write(numout,*) "dt=",dt,", date0=",date0,", year_length=",year_length,", year_spread=",year_spread
2666       ENDIF
2667    ENDIF
2668
2669    !
2670    IF (check_time) &
2671         WRITE(numout,*) "---" 
2672    ! Dans diffuco (ie date0 == date0_shift !!)
2673
2674    IF ( TRIM(calendar_str) .EQ. 'gregorian' ) THEN 
2675       !
2676       ! Get Julian date
2677       in_julian = itau2date(istp, date0, dt)
2678
2679       ! Real date
2680       CALL ju2ymds (in_julian, year, month, day, sec)
2681!!$       jur=zero
2682!!$       julian_diff = in_julian
2683!!$       month_len = ioget_mon_len (year,month)
2684!!$       IF (check_time) THEN
2685!!$          write(numout,*) "in_julian, jur, julian_diff=",in_julian, jur, julian_diff
2686!!$          write(numout,*) 'DATE ymds', year, month,'(',month_len,'d)', day, sec, '-- stp --', istp
2687!!$       ENDIF
2688
2689       ! julian number for january, the first.
2690       CALL ymds2ju (year,1,1,zero, julian0)
2691       julian_diff = in_julian-julian0
2692       ! real number of seconds
2693!       sec = (julian_diff-REAL(INT(julian_diff)))*one_day
2694       sec = NINT((julian_diff-REAL(INT(julian_diff)))*one_day)
2695       month_len = ioget_mon_len (year,month)
2696       IF (check_time) THEN
2697          write(numout,*) "2 in_julian, julian0, julian_diff=",in_julian, julian0, julian_diff
2698          write(numout,*) '2 DATE ymds', year, month,'(',month_len,'d)', day, sec, '-- stp --', istp
2699       ENDIF
2700    ELSE 
2701!!$       in_julian = itau2date(istp-1, zero, dt)
2702!!$       CALL ju2ymds (in_julian, year, month, day, sec)
2703!!$       jur=zero
2704!!$       julian_diff = in_julian
2705!!$       month_len = ioget_mon_len (year,month)
2706!!$       IF (check_time) THEN
2707!!$          write(numout,*) "in_julian=",in_julian, jur, julian_diff
2708!!$          write(numout,*) 'DATE ymds', year, month,'(',month_len,'d)', day, sec, '-- stp --', istp
2709!!$       ENDIF
2710!!$
2711!!$
2712!!$       CALL ymds2ju (year,1,1,zero, jur)
2713!!$       julian_diff = in_julian-jur
2714!!$       CALL ju2ymds (julian_diff, year, month, day, sec)
2715!!$!       sec = (julian_diff-REAL(INT(julian_diff)))*one_day
2716!!$       sec = NINT((julian_diff-REAL(INT(julian_diff)))*one_day)
2717!!$       month_len = ioget_mon_len (year,month)
2718!!$       IF (check_time) THEN
2719!!$          write(numout,*) "2 in_julian, jur, julian_diff=",in_julian, jur, julian_diff
2720!!$          write(numout,*) '2 DATE ymds', year, month,'(',month_len,'d)', day, sec, '-- stp --', istp
2721!!$       ENDIF
2722
2723
2724!!$       IF (check_time) &
2725!!$            WRITE(numout,*) "-"
2726
2727!MM
2728!PB date0 = celui de Soenke (à tester avec un autre date0)
2729!       in_julian = itau2date(istp, 153116., dt)
2730       in_julian = itau2date(istp, date0, dt)
2731       CALL itau2ymds(istp, dt, year, month, day, sec)
2732       CALL ymds2ju (year,1,1,zero, julian0)
2733       julian_diff = in_julian
2734       month_len = ioget_mon_len (year,month)
2735       IF (check_time) THEN
2736          write(numout,*) "in_julian=",in_julian, julian0, julian_diff
2737          write(numout,*) 'DATE ymds', year, month,'(',month_len,'d)', day, sec, '-- stp --', istp
2738       ENDIF
2739    ENDIF
2740!!$    IF (check_time) &
2741!!$         WRITE(numout,*) "---"
2742
2743  END SUBROUTINE intsurf_time
2744!
2745
2746!-------------------------------------------------------------------------------------
2747!
2748  SUBROUTINE intsurf_config(control_flags,dt)
2749    !
2750    !  This subroutine reads all the configuration flags which control the behaviour of the model
2751    !
2752    IMPLICIT NONE
2753    !
2754    REAL, INTENT(in)                           :: dt            !! Time step in seconds
2755    !
2756    TYPE(control_type), INTENT(out)            :: control_flags !! Flags that (de)activate parts of the model
2757
2758    !
2759    !Config Key  = LONGPRINT
2760    !Config Desc = ORCHIDEE will print more messages
2761    !Config Def  = n
2762    !Config Help = This flag permits to print more debug messages in the run.
2763    !
2764    long_print = .FALSE.
2765    CALL getin_p('LONGPRINT',long_print)
2766    !
2767    !Config Key  = CHECKTIME
2768    !Config Desc = ORCHIDEE will print messages on time
2769    !Config Def  = n
2770    !Config Help = This flag permits to print debug messages on the time.
2771    !
2772    check_time = .FALSE.
2773    CALL getin_p('CHECKTIME',check_time)
2774    !
2775    !
2776    !Config Key  = ORCHIDEE_WATCHOUT
2777    !Config Desc = ORCHIDEE will write out its forcing to a file
2778    !Config Def  = n
2779    !Config Help = This flag allows to write to a file all the variables
2780    !Config        which are used to force the land-surface. The file
2781    !Config        has exactly the same format than a normal off-line forcing
2782    !Config        and thus this forcing can be used for forcing ORCHIDEE.
2783    !
2784    ok_watchout = .FALSE.
2785    CALL getin_p('ORCHIDEE_WATCHOUT',ok_watchout)
2786    !
2787    IF (ok_watchout) THEN
2788       !Config Key  = DT_WATCHOUT
2789       !Config Desc = ORCHIDEE will write out with this frequency
2790       !Config IF   = ORCHIDEE_WATCHOUT
2791       !Config Def  = dt
2792       !Config Help = This flag indicates the frequency of the write of the variables.
2793       !
2794       dt_watch = dt
2795       CALL getin_p('DT_WATCHOUT',dt_watch)
2796       dt_split_watch = dt_watch / dt
2797       !
2798       !Config Key  = WATCHOUT_FILE
2799       !Config Desc = Filenane for the ORCHIDEE forcing file
2800       !Config IF   = ORCHIDEE_WATCHOUT
2801       !Config Def  = orchidee_watchout.nc
2802       !Config Help = This is the name of the file in which the
2803       !Config        forcing used here will be written for later use.
2804       !
2805       watchout_file = "orchidee_watchout.nc"
2806       CALL getin_p('WATCHOUT_FILE',watchout_file)
2807       
2808       WRITE(numout,*) 'WATCHOUT flag :', ok_watchout
2809       WRITE(numout,*) 'WATCHOUT file :', watchout_file
2810    ENDIF
2811    !
2812    !Config Key  = RIVER_ROUTING
2813    !Config Desc = Decides if we route the water or not
2814    !Config Def  = n
2815    !Config Help = This flag allows the user to decide if the runoff
2816    !Config        and drainage should be routed to the ocean
2817    !Config        and to downstream grid boxes.
2818    !
2819    control_flags%river_routing = .FALSE.
2820    CALL getin_p('RIVER_ROUTING', control_flags%river_routing)
2821    WRITE(numout,*) "RIVER routing is activated : ",control_flags%river_routing
2822    !
2823    !Config key = HYDROL_CWRR
2824    !Config Desc = Allows to switch on the multilayer hydrology of CWRR
2825    !Config Def  = n
2826    !Config Help = This flag allows the user to decide if the vertical
2827    !Config        hydrology should be treated using the multi-layer
2828    !Config        diffusion scheme adapted from CWRR by Patricia de Rosnay.
2829    !Config        by default the Choisnel hydrology is used.
2830    !
2831    control_flags%hydrol_cwrr = .FALSE.
2832    CALL getin_p('HYDROL_CWRR', control_flags%hydrol_cwrr)
2833    IF ( control_flags%hydrol_cwrr ) then
2834       CALL ipslerr (2,'intsurf_config', &
2835            &          'You will use in this run the second version of CWRR hydrology in ORCHIDEE.',&
2836            &          'This model hasn''t been tested for global run yet.', &
2837            &          '(check your parameters)')
2838    ENDIF
2839    !
2840    !Config Key  = STOMATE_OK_CO2
2841    !Config Desc = Activate CO2?
2842    !Config Def  = n
2843    !Config Help = set to TRUE if photosynthesis is to be activated
2844    !
2845    control_flags%ok_co2 = .FALSE.
2846    CALL getin_p('STOMATE_OK_CO2', control_flags%ok_co2)
2847    WRITE(numout,*) 'photosynthesis: ', control_flags%ok_co2
2848    !
2849    !Config Key  = STOMATE_OK_STOMATE
2850    !Config Desc = Activate STOMATE?
2851    !Config Def  = n
2852    !Config Help = set to TRUE if STOMATE is to be activated
2853    !
2854    control_flags%ok_stomate = .FALSE.
2855    CALL getin_p('STOMATE_OK_STOMATE',control_flags%ok_stomate)
2856    WRITE(numout,*) 'STOMATE is activated: ',control_flags%ok_stomate
2857    !
2858    !Config Key  = STOMATE_OK_DGVM
2859    !Config Desc = Activate DGVM?
2860    !Config Def  = n
2861    !Config Help = set to TRUE if DGVM is to be activated
2862    !
2863    control_flags%ok_dgvm = .FALSE.
2864    CALL getin_p('STOMATE_OK_DGVM',control_flags%ok_dgvm)
2865
2866    !
2867    ! control initialisation with sechiba
2868    !
2869    control_flags%ok_sechiba = .TRUE.
2870    !
2871    !
2872    ! Ensure consistency
2873    !
2874    IF ( control_flags%ok_dgvm ) control_flags%ok_stomate = .TRUE.
2875    IF ( control_flags%ok_stomate ) control_flags%ok_co2 = .TRUE.
2876    !
2877    !Config Key  = STOMATE_WATCHOUT
2878    !Config Desc = STOMATE does minimum service
2879    !Config Def  = n
2880    !Config Help = set to TRUE if you want STOMATE to read
2881    !Config        and write its start files and keep track
2882    !Config        of longer-term biometeorological variables.
2883    !Config        This is useful if OK_STOMATE is not set,
2884    !Config        but if you intend to activate STOMATE later.
2885    !Config        In that case, this run can serve as a
2886    !Config        spinup for longer-term biometeorological
2887    !Config        variables.
2888    !
2889    control_flags%stomate_watchout = .FALSE.
2890    CALL getin_p('STOMATE_WATCHOUT',control_flags%stomate_watchout)
2891    WRITE(numout,*) 'STOMATE keeps an eye open: ',control_flags%stomate_watchout
2892    !
2893    ! Here we need the same initialisation as above
2894    !
2895    control_flags%ok_pheno = .TRUE.
2896    !
2897    !
2898    RETURN
2899    !
2900  END SUBROUTINE intsurf_config
2901  !
2902  !
2903  !
2904  SUBROUTINE intsurf_restart(istp, iim, jjm, lon, lat, date0, dt, control_flags, rest_id, rest_id_stom, itau_offset)
2905    !
2906    !  This subroutine initialized the restart file for the land-surface scheme
2907    !
2908    IMPLICIT NONE
2909    !
2910    INTEGER(i_std), INTENT(in)                  :: istp      !! Time step of the restart file
2911    INTEGER(i_std), INTENT(in)                  :: iim, jjm  !! Size in x and y of the data to be handeled
2912    REAL(r_std),DIMENSION (iim,jjm), INTENT(in) :: lon, lat  !! Logitude and latitude of the data points
2913    REAL(r_std)                                 :: date0     !! The date at which itau = 0
2914    REAL(r_std)                                 :: dt        !! Time step
2915    INTEGER(i_std), INTENT(out)                 :: rest_id, rest_id_stom   !! ID of the restart file
2916    INTEGER(i_std), INTENT(out)                 :: itau_offset
2917    !
2918    TYPE(control_type), INTENT(in)             :: control_flags !! Flags that (de)activate parts of the model
2919    !
2920    !  LOCAL
2921    !
2922    CHARACTER(LEN=80)          :: restname_in, restname_out, stom_restname_in, stom_restname_out
2923    REAL(r_std)                 :: dt_rest, date0_rest
2924    INTEGER(i_std)              :: itau_dep
2925    INTEGER(i_std),PARAMETER    :: llm=1
2926    REAL(r_std), DIMENSION(llm) :: lev
2927    LOGICAL                    :: overwrite_time
2928    REAL(r_std)                 :: in_julian, rest_julian
2929    INTEGER(i_std)              :: yy, mm, dd
2930    REAL(r_std)                 :: ss
2931    !
2932    !Config  Key  = SECHIBA_restart_in
2933    !Config  Desc = Name of restart to READ for initial conditions
2934    !Config  Def  = NONE
2935    !Config  Help = This is the name of the file which will be opened
2936    !Config         to extract the initial values of all prognostic
2937    !Config         values of the model. This has to be a netCDF file.
2938    !Config         Not truly COADS compliant. NONE will mean that
2939    !Config         no restart file is to be expected.
2940!-
2941    restname_in = 'NONE'
2942    CALL getin_p('SECHIBA_restart_in',restname_in)
2943    WRITE(numout,*) 'INPUT RESTART_FILE', restname_in
2944    !-
2945    !Config Key  = SECHIBA_rest_out
2946    !Config Desc = Name of restart files to be created by SECHIBA
2947    !Config Def  = sechiba_rest_out.nc
2948    !Config Help = This variable give the name for
2949    !Config        the restart files. The restart software within
2950    !Config        IOIPSL will add .nc if needed.
2951    !
2952    restname_out = 'restart_out.nc'
2953    CALL getin_p('SECHIBA_rest_out', restname_out)
2954    !
2955    !Config Key  = SECHIBA_reset_time
2956    !Config Desc = Option to overrides the time of the restart
2957    !Config Def  = n
2958    !Config Help = This option allows the model to override the time
2959    !Config        found in the restart file of SECHIBA with the time
2960    !Config        of the first call. That is the restart time of the GCM.
2961    !
2962    overwrite_time = .FALSE.
2963    CALL getin_p('SECHIBA_reset_time', overwrite_time)
2964    !
2965    lev(:) = zero
2966    itau_dep = istp
2967    in_julian = itau2date(istp, date0, dt)
2968    date0_rest = date0
2969    dt_rest = dt
2970    !
2971    IF (is_root_prc) THEN
2972      CALL restini( restname_in, iim_g, jjm_g, lon_g, lat_g, llm, lev, &
2973         &  restname_out, itau_dep, date0_rest, dt_rest, rest_id, overwrite_time)
2974    ELSE
2975       rest_id=0
2976    ENDIF
2977    CALL bcast (itau_dep)
2978    CALL bcast (date0_rest)
2979    CALL bcast (dt_rest)
2980    !
2981    !  itau_dep of SECHIBA is phased with the GCM if needed
2982    !
2983    rest_julian = itau2date(itau_dep, date0_rest, dt_rest)
2984    !
2985    IF ( ABS( in_julian - rest_julian) .GT. dt/one_day .AND. .NOT. OFF_LINE_MODE ) THEN
2986       IF ( overwrite_time ) THEN
2987          WRITE(numout,*) 'The SECHIBA restart is not for the same timestep as the GCM,'
2988          WRITE(numout,*) 'the two are synchronized. The land-surface conditions can not impose'
2989          WRITE(numout,*) 'the chronology of the simulation.'
2990          WRITE(numout,*) 'Time step of the GCM :', istp, 'Julian day : ', in_julian
2991          CALL ju2ymds(in_julian, yy, mm, dd, ss)
2992          WRITE(numout,*) 'In other word (yy,mm,dd,ss) : ', yy, mm, dd, ss
2993          WRITE(numout,*) 'Time step of SECHIBA :', itau_dep, 'Julian day : ', rest_julian
2994          CALL ju2ymds(rest_julian, yy, mm, dd, ss)
2995          WRITE(numout,*) 'In other word (yy,mm,dd,ss) : ', yy, mm, dd, ss
2996
2997          itau_offset = itau_dep - istp
2998          date0_shifted = date0 - itau_offset*dt/one_day
2999!MM_ A VOIR : dans le TAG 1.4 :
3000!         date0_shifted = in_julian - itau_dep*dt/one_day
3001!MM_ Bon calcul ?
3002
3003          WRITE(numout,*) 'The new starting date is :', date0_shifted
3004          CALL ju2ymds(date0_shifted, yy, mm, dd, ss)
3005          WRITE(numout,*) 'In other word (yy,mm,dd,ss) : ', yy, mm, dd, ss
3006       ELSE
3007          WRITE(numout,*) 'IN -> OUT :', istp, '->', itau_dep
3008          WRITE(numout,*) 'IN -> OUT :', in_julian, '->', rest_julian
3009          WRITE(numout,*) 'SECHIBA''s restart file is not consistent with the one of the GCM'
3010          WRITE(numout,*) 'Correct the time axis of the restart file or force the code to change it.'
3011          STOP
3012       ENDIF
3013    ELSE
3014       itau_offset = 0
3015       date0_shifted = date0
3016    ENDIF
3017    !
3018!!!    CALL ioconf_startdate(date0_shifted)
3019    !
3020    !=====================================================================
3021    !- 1.5 Restart file for STOMATE
3022    !=====================================================================
3023    IF ( control_flags%ok_stomate .OR. control_flags%stomate_watchout ) THEN 
3024       !-
3025       ! STOMATE IS ACTIVATED
3026       !-
3027       !Config  Key  = STOMATE_RESTART_FILEIN
3028       !Config  Desc = Name of restart to READ for initial conditions
3029       !Config         of STOMATE
3030       !Config  If   = STOMATE_OK_STOMATE || STOMATE_WATCHOUT
3031       !Config  Def  = NONE
3032       !Config  Help = This is the name of the file which will be opened
3033       !Config         to extract the initial values of all prognostic
3034       !Config         values of STOMATE.
3035       !-
3036       stom_restname_in = 'NONE'
3037       CALL getin_p('STOMATE_RESTART_FILEIN',stom_restname_in)
3038       WRITE(numout,*) 'STOMATE INPUT RESTART_FILE', stom_restname_in
3039       !-
3040       !Config Key  = STOMATE_RESTART_FILEOUT
3041       !Config Desc = Name of restart files to be created by STOMATE
3042       !Config  If   = STOMATE_OK_STOMATE || STOMATE_WATCHOUT
3043       !Config Def  = stomate_restart.nc
3044       !Config Help = This is the name of the file which will be opened
3045       !Config        to write the final values of all prognostic values
3046       !Config        of STOMATE.
3047       !-
3048       stom_restname_out = 'stomate_restart.nc'
3049       CALL getin_p('STOMATE_RESTART_FILEOUT', stom_restname_out)
3050       WRITE(numout,*) 'STOMATE OUTPUT RESTART_FILE', stom_restname_out
3051       !-
3052       IF (is_root_prc) THEN
3053         CALL restini (stom_restname_in, iim_g, jjm_g, lon_g, lat_g, llm, lev, &
3054            &  stom_restname_out, itau_dep, date0_rest, dt_rest, rest_id_stom, overwrite_time)
3055       ELSE
3056         rest_id_stom=0
3057       ENDIF
3058       CALL bcast (itau_dep)
3059       CALL bcast (date0_rest)
3060       CALL bcast (dt_rest)
3061       !-
3062    ENDIF
3063    !
3064  END SUBROUTINE intsurf_restart
3065 
3066  SUBROUTINE intsurf_history(iim, jjm, lon, lat, istp_old, date0, dt, control_flags, hist_id, hist2_id, &
3067       & hist_id_stom, hist_id_stom_IPCC)
3068    !
3069    !   
3070    !  This subroutine initialized the history files for the land-surface scheme
3071    !
3072    IMPLICIT NONE
3073    !
3074    INTEGER(i_std), INTENT(in)                  :: iim, jjm  !! Size in x and y of the data to be handeled
3075    REAL(r_std),DIMENSION (iim,jjm), INTENT(in) :: lon, lat  !! Longitude and latitude of the data points
3076    INTEGER(i_std), INTENT(in)                  :: istp_old  !! Time step counter
3077    REAL(r_std), INTENT(in)                     :: date0     !! Julian day at which istp=0
3078    REAL(r_std), INTENT(in)                     :: dt        !! Time step of the counter in seconds
3079    !
3080    TYPE(control_type), INTENT(in)             :: control_flags !! Flags that (de)activate parts of the model
3081    !
3082    INTEGER(i_std), INTENT(out)                 :: hist_id !! History file identification for SECHIBA
3083    INTEGER(i_std), INTENT(out)                 :: hist2_id !! History file 2 identification for SECHIBA (Hi-frequency ?)
3084    !! History file identification for STOMATE and IPCC
3085    INTEGER(i_std), INTENT(out)                 :: hist_id_stom, hist_id_stom_IPCC 
3086    !
3087    !  LOCAL
3088    !
3089    CHARACTER(LEN=80) :: histname,histname2                    !! Name of history files for SECHIBA
3090    CHARACTER(LEN=80) :: stom_histname, stom_ipcc_histname     !! Name of history files for STOMATE
3091    LOGICAL           :: ok_histfile2                 !! Flag to switch on histfile 2 for SECHIBA
3092    REAL(r_std)       :: dw2                          !! frequency of history write (sec.)
3093    CHARACTER(LEN=30)   :: flux_op                    !! Operations to be performed on fluxes
3094    CHARACTER(LEN=30)   :: flux_sc                    !! Operations which do not include a scatter
3095    CHARACTER(LEN=30)   :: flux_insec, flux_scinsec   !! Operation in seconds
3096    INTEGER(i_std)     :: hist_level, hist2_level     !! history output level (default is 10 => maximum output)
3097    CHARACTER(LEN=40),DIMENSION(max_hist_level) :: &
3098         & ave, avecels, avescatter, fluxop, &
3099         & fluxop_scinsec, tmincels, tmaxcels, once, sumscatter  !! The various operation to be performed
3100!!, tmax, fluxop_sc, fluxop_insec, &
3101    CHARACTER(LEN=40),DIMENSION(max_hist_level) :: &
3102         & ave2, avecels2, avescatter2, fluxop2, &
3103         & fluxop_scinsec2, tmincels2, tmaxcels2, once2, sumscatter2  !! The various operation to be performed
3104!!, tmax2, fluxop_sc2, fluxop_insec2, &
3105    INTEGER(i_std)     :: i, jst
3106    ! SECHIBA AXIS
3107    INTEGER(i_std)     :: hori_id                      !! ID of the default horizontal longitude and latitude map.
3108    INTEGER(i_std)     :: vegax_id, solax_id, soltax_id, nobioax_id !! ID's for two vertical coordinates
3109    INTEGER(i_std)     :: solayax_id                   !! ID for the vertical axis of the CWRR hydrology
3110    INTEGER(i_std)     :: hori_id2                      !! ID of the default horizontal longitude and latitude map.
3111    INTEGER(i_std)     :: vegax_id2, solax_id2, soltax_id2, nobioax_id2, albax_id2 !! ID's for two vertical coordinates
3112    INTEGER(i_std)     :: solayax_id2                   !! ID for the vertical axis of the CWRR hydrology
3113    ! STOMATE AXIS
3114    INTEGER(i_std)     :: hist_PFTaxis_id
3115! deforestation
3116    INTEGER(i_std)     :: hist_pool_10axis_id
3117    INTEGER(i_std)     :: hist_pool_100axis_id
3118    INTEGER(i_std)     :: hist_pool_11axis_id
3119    INTEGER(i_std)     :: hist_pool_101axis_id
3120    ! STOMATE IPCC AXIS
3121    INTEGER(i_std)     :: hist_IPCC_PFTaxis_id
3122    !
3123    LOGICAL                               :: rectilinear
3124    INTEGER(i_std)                         :: ier
3125    REAL(r_std), ALLOCATABLE, DIMENSION(:) :: lon_rect, lat_rect
3126    !
3127    REAL(r_std),DIMENSION(nvm)   :: veg
3128    REAL(r_std),DIMENSION(ngrnd) :: sol
3129    REAL(r_std),DIMENSION(nstm)  :: soltyp
3130    REAL(r_std),DIMENSION(nnobio):: nobiotyp
3131    REAL(r_std),DIMENSION(2)     :: albtyp
3132    REAL(r_std),DIMENSION(nslm)  :: solay
3133    !
3134    CHARACTER(LEN=80)           :: var_name           !! To store variables names
3135    !
3136    ! STOMATE history file
3137    REAL(r_std)                  :: hist_days_stom     !!- GK time step in days for this history file
3138    REAL(r_std)                  :: hist_dt_stom       !!- GK time step in seconds for this history file
3139    REAL(r_std)                  :: dt_slow_           !!  for test : time step of slow processes and STOMATE
3140    REAL(r_std),DIMENSION(nvm)   :: hist_PFTaxis       !!- GK An axis we need for the history files
3141!
3142    REAL(r_std),DIMENSION(10)  :: hist_pool_10axis     !! Deforestation axis
3143    REAL(r_std),DIMENSION(100)  :: hist_pool_100axis     !! Deforestation axis
3144    REAL(r_std),DIMENSION(11)  :: hist_pool_11axis     !! Deforestation axis
3145    REAL(r_std),DIMENSION(101)  :: hist_pool_101axis     !! Deforestation axis
3146    !
3147    ! IPCC history file
3148    REAL(r_std)                  :: hist_days_stom_ipcc     !!- GK time step in days for this history file
3149    REAL(r_std)                  :: hist_dt_stom_ipcc       !!- GK time step in seconds for this history file
3150!
3151    !
3152    !
3153    !=====================================================================
3154    !- 3.0 Setting up the history files
3155    !=====================================================================
3156    !- 3.1 SECHIBA
3157    !=====================================================================
3158    !Config  Key  = ALMA_OUTPUT
3159    !Config  Desc = Should the output follow the ALMA convention
3160    !Config  Def  = n
3161    !Config  Help = If this logical flag is set to true the model
3162    !Config         will output all its data according to the ALMA
3163    !Config         convention. It is the recommended way to write
3164    !Config         data out of ORCHIDEE.
3165    !-
3166    almaoutput = .FALSE.
3167    CALL getin_p('ALMA_OUTPUT', almaoutput)   
3168    WRITE(numout,*) 'ALMA_OUTPUT', almaoutput
3169    !-
3170    !Config  Key  = OUTPUT_FILE
3171    !Config  Desc = Name of file in which the output is going
3172    !Config         to be written
3173    !Config  Def  = cabauw_out.nc
3174    !Config  Help = This file is going to be created by the model
3175    !Config         and will contain the output from the model.
3176    !Config         This file is a truly COADS compliant netCDF file.
3177    !Config         It will be generated by the hist software from
3178    !Config         the IOIPSL package.
3179    !-
3180    histname='cabauw_out.nc'
3181    CALL getin_p('OUTPUT_FILE', histname)
3182    WRITE(numout,*) 'OUTPUT_FILE', histname
3183    !-
3184    !Config  Key  = WRITE_STEP
3185    !Config  Desc = Frequency in seconds at which to WRITE output
3186    !Config  Def  = one_day
3187    !Config  Help = This variables gives the frequency the output of
3188    !Config         the model should be written into the netCDF file.
3189    !Config         It does not affect the frequency at which the
3190    !Config         operations such as averaging are done.
3191    !Config         That is IF the coding of the calls to histdef
3192    !Config         are correct !
3193    !-
3194    dw = one_day
3195    CALL getin_p('WRITE_STEP', dw)
3196    !
3197    veg(1:nvm)   = (/ (REAL(i,r_std),i=1,nvm) /)
3198    sol(1:ngrnd) = (/ (REAL(i,r_std),i=1,ngrnd) /)   
3199    soltyp(1:nstm) = (/ (REAL(i,r_std),i=1,nstm) /)
3200    nobiotyp(1:nnobio) = (/ (REAL(i,r_std),i=1,nnobio) /)
3201    albtyp(1:2) = (/ (REAL(i,r_std),i=1,2) /)
3202    solay(1:nslm) = (/ (REAL(i,r_std),i=1,nslm) /)
3203    !
3204    !- We need to flux averaging operation as when the data is written
3205    !- from within SECHIBA a scatter is needed. In the driver on the other
3206    !- hand the data is 2D and can be written is it is.
3207    !-
3208    WRITE(flux_op,'("ave(scatter(X*",F8.1,"))")') one_day/dt
3209    ! WRITE(flux_op,'("(ave(scatter(X))*",F8.1,")")') one_day/dt
3210    WRITE(flux_sc,'("ave(X*",F8.1,")")') one_day/dt
3211    !WRITE(flux_sc,'("(ave(X)*",F8.1,")")') one_day/dt
3212    WRITE(flux_insec,'("ave(X*",F8.6,")")') un/dt
3213    WRITE(flux_scinsec,'("ave(scatter(X*",F8.6,"))")') un/dt
3214    WRITE(numout,*) flux_op, one_day/dt, dt, dw
3215    !-
3216    !Config  Key  = SECHIBA_HISTLEVEL
3217    !Config  Desc = SECHIBA history output level (0..10)
3218    !Config  Def  = 5
3219    !Config  Help = Chooses the list of variables in the history file.
3220    !Config         Values between 0: nothing is written; 10: everything is
3221    !Config         written are available More details can be found on the web under documentation.
3222    !Config         web under documentation.
3223    !-
3224    hist_level = 5
3225    CALL getin_p('SECHIBA_HISTLEVEL', hist_level)
3226    !-
3227    WRITE(numout,*) 'SECHIBA history level: ',hist_level
3228    IF ( (hist_level > max_hist_level).OR.(hist_level < 0) ) THEN
3229       STOP 'This history level is not allowed'
3230    ENDIF
3231    !-
3232    !- define operations as a function of history level.
3233    !- Above hist_level, operation='never'
3234    !-
3235    ave(1:max_hist_level) = 'ave(X)'
3236    IF (hist_level < max_hist_level) THEN
3237       ave(hist_level+1:max_hist_level) = 'never'
3238    ENDIF
3239    sumscatter(1:max_hist_level) = 't_sum(scatter(X))'
3240    IF (hist_level < max_hist_level) THEN
3241       sumscatter(hist_level+1:max_hist_level) = 'never'
3242    ENDIF
3243    avecels(1:max_hist_level) = 'ave(cels(X))'
3244    IF (hist_level < max_hist_level) THEN
3245       avecels(hist_level+1:max_hist_level) = 'never'
3246    ENDIF
3247    avescatter(1:max_hist_level) = 'ave(scatter(X))'
3248    IF (hist_level < max_hist_level) THEN
3249       avescatter(hist_level+1:max_hist_level) = 'never'
3250    ENDIF
3251    tmincels(1:max_hist_level) = 't_min(cels(X))'
3252    IF (hist_level < max_hist_level) THEN
3253       tmincels(hist_level+1:max_hist_level) = 'never'
3254    ENDIF
3255    tmaxcels(1:max_hist_level) = 't_max(cels(X))'
3256    IF (hist_level < max_hist_level) THEN
3257       tmaxcels(hist_level+1:max_hist_level) = 'never'
3258    ENDIF
3259!!$    tmax(1:max_hist_level) = 't_max(X)'
3260!!$    IF (hist_level < max_hist_level) THEN
3261!!$       tmax(hist_level+1:max_hist_level) = 'never'
3262!!$    ENDIF
3263    fluxop(1:max_hist_level) = flux_op
3264    IF (hist_level < max_hist_level) THEN
3265       fluxop(hist_level+1:max_hist_level) = 'never'
3266    ENDIF
3267!!$    fluxop_sc(1:max_hist_level) = flux_sc
3268!!$    IF (hist_level < max_hist_level) THEN
3269!!$       fluxop_sc(hist_level+1:max_hist_level) = 'never'
3270!!$    ENDIF
3271!!$    fluxop_insec(1:max_hist_level) = flux_insec
3272!!$    IF (hist_level < max_hist_level) THEN
3273!!$       fluxop_insec(hist_level+1:max_hist_level) = 'never'
3274!!$    ENDIF
3275    fluxop_scinsec(1:max_hist_level) = flux_scinsec
3276    IF (hist_level < max_hist_level) THEN
3277       fluxop_scinsec(hist_level+1:max_hist_level) = 'never'
3278    ENDIF
3279    once(1:max_hist_level) = 'once(scatter(X))'
3280    IF (hist_level < max_hist_level) THEN
3281       once(hist_level+1:max_hist_level) = 'never'
3282    ENDIF
3283    !
3284    !-
3285    !- Check if we have by any change a rectilinear grid. This would allow us to
3286    !- simplify the output files.
3287    !
3288    rectilinear = .FALSE.
3289    IF ( ALL(lon(:,:) == SPREAD(lon(:,1), 2, SIZE(lon,2))) .AND. &
3290       & ALL(lat(:,:) == SPREAD(lat(1,:), 1, SIZE(lat,1))) ) THEN
3291       rectilinear = .TRUE.
3292       ALLOCATE(lon_rect(iim),stat=ier)
3293       IF (ier .NE. 0) THEN
3294          WRITE (numout,*) ' error in lon_rect allocation. We stop. We need iim words = ',iim
3295          STOP 'intersurf_history'
3296       ENDIF
3297       ALLOCATE(lat_rect(jjm),stat=ier)
3298       IF (ier .NE. 0) THEN
3299          WRITE (numout,*) ' error in lat_rect allocation. We stop. We need jjm words = ',jjm
3300          STOP 'intersurf_history'
3301       ENDIF
3302       lon_rect(:) = lon(:,1)
3303       lat_rect(:) = lat(1,:)
3304    ENDIF
3305    !-
3306    !-
3307    hist_id = -1
3308    !-
3309    IF ( .NOT. almaoutput ) THEN
3310       !-
3311       IF ( rectilinear ) THEN
3312#ifdef CPP_PARA
3313          CALL histbeg(histname, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3314               &     istp_old, date0, dt, hori_id, hist_id,orch_domain_id)
3315#else
3316          CALL histbeg(histname, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3317               &     istp_old, date0, dt, hori_id, hist_id)
3318#endif
3319          WRITE(numout,*)  'HISTBEG --->',istp_old,date0,dt,dw,hist_id
3320       ELSE
3321#ifdef CPP_PARA
3322          CALL histbeg(histname, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3323               &     istp_old, date0, dt, hori_id, hist_id,domain_id=orch_domain_id)
3324#else
3325          CALL histbeg(histname, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3326               &     istp_old, date0, dt, hori_id, hist_id)
3327#endif
3328       ENDIF
3329       !-
3330       CALL histvert(hist_id, 'veget', 'Vegetation types', '1', &
3331            &    nvm,   veg, vegax_id)
3332       CALL histvert(hist_id, 'solth', 'Soil levels',      'm', &
3333            &    ngrnd, sol, solax_id)
3334       CALL histvert(hist_id, 'soiltyp', 'Soil types',      '1', &
3335            &    nstm, soltyp, soltax_id)
3336       CALL histvert(hist_id, 'nobio', 'Other surface types',      '1', &
3337            &    nnobio, nobiotyp, nobioax_id)
3338       IF (  control_flags%hydrol_cwrr ) THEN
3339          CALL histvert(hist_id, 'solay', 'Hydrol soil levels',      'm', &
3340               &    nslm, solay, solayax_id)
3341       ENDIF
3342       !-
3343       !- SECHIBA_HISTLEVEL = 1
3344       !-
3345       CALL histdef(hist_id, 'evap', 'Evaporation', 'mm/d', &
3346            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(1), dt,dw)
3347       CALL histdef(hist_id, 'coastalflow', 'Diffuse coastal flow', 'm^3/s', &
3348            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3349       CALL histdef(hist_id, 'riverflow', 'River flow to the oceans', 'm^3/s', &
3350            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw) 
3351       CALL histdef(hist_id, 'temp_sol', 'Surface Temperature', 'C', &
3352            & iim,jjm, hori_id, 1,1,1, -99, 32, avecels(1), dt,dw)
3353       CALL histdef(hist_id, 'rain', 'Rainfall', 'mm/d',  &
3354            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(1), dt,dw)
3355       CALL histdef(hist_id, 'snowf', 'Snowfall', 'mm/d',  &
3356            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(1), dt,dw)
3357       CALL histdef(hist_id, 'netrad', 'Net radiation', 'W/m^2',  &
3358            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3359       CALL histdef(hist_id, 'lai', 'Leaf Area Index', '1', &
3360            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(1), dt,dw)
3361       IF ( control_flags%river_routing ) THEN
3362          CALL histdef(hist_id, 'basinmap', 'Aproximate map of the river basins', ' ', &
3363               & iim,jjm, hori_id, 1,1,1, -99, 32, once(1), dt,dw) 
3364          CALL histdef(hist_id, 'nbrivers', 'Number or rivers in the outflow grid box', ' ', &
3365               & iim,jjm, hori_id, 1,1,1, -99, 32, once(1), dt,dw) 
3366       ENDIF
3367       !-
3368       !- SECHIBA_HISTLEVEL = 2
3369       !-
3370       CALL histdef(hist_id, 'subli', 'Sublimation', 'mm/d', &
3371            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(2), dt,dw)
3372       CALL histdef(hist_id, 'evapnu', 'Bare soil evaporation', 'mm/d', &
3373            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(2), dt,dw)
3374       CALL histdef(hist_id, 'runoff', 'Surface runoff', 'mm/d', &
3375            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(2), dt,dw)
3376       CALL histdef(hist_id, 'drainage', 'Deep drainage', 'mm/d', &
3377            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(2), dt,dw)
3378       IF ( control_flags%river_routing ) THEN
3379          CALL histdef(hist_id, 'riversret', 'Return from endorheic rivers', 'mm/d', &
3380               & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(2), dt,dw)
3381          CALL histdef(hist_id, 'hydrographs', 'Hydrographs of gridbox outflow', 'm^3/s', &
3382               & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(2), dt,dw)
3383       ENDIF
3384       IF ( control_flags%hydrol_cwrr ) THEN
3385          CALL histdef(hist_id, 'evapnu_soil', 'Bare soil evap for soil type', 'mm/d', &
3386               & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, fluxop(2), dt,dw)
3387          CALL histdef(hist_id, 'drainage_soil', 'Drainage for soil type', 'mm/d', &
3388               & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, fluxop(2), dt,dw)
3389          CALL histdef(hist_id, 'transpir_soil', 'Transpir for soil type', 'mm/d', &
3390               & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, fluxop(2), dt,dw)
3391          CALL histdef(hist_id, 'runoff_soil', 'Runoff for soil type', 'mm/d', &
3392               & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, fluxop(2), dt,dw)
3393       ENDIF
3394       !
3395       CALL histdef(hist_id, 'tair', 'Air Temperature', 'K',  &
3396            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3397       CALL histdef(hist_id, 'qair', 'Air humidity', 'g/g',  &
3398            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3399       ! Ajouts Nathalie - Juillet 2006
3400       CALL histdef(hist_id, 'q2m', '2m Air humidity', 'g/g',  &
3401            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3402       CALL histdef(hist_id, 't2m', '2m Air Temperature', 'K',  &
3403            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3404       ! Fin ajouts Nathalie
3405       CALL histdef(hist_id, 'alb_vis', 'Albedo visible', '1', &
3406            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3407       CALL histdef(hist_id, 'alb_nir', 'Albedo near infrared', '1', &
3408            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3409       ! Ajouts Nathalie - Septembre 2008
3410       CALL histdef(hist_id, 'soilalb_vis', 'Soil Albedo visible', '1', &
3411            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3412       CALL histdef(hist_id, 'soilalb_nir', 'Soil Albedo near infrared', '1', &
3413            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3414       CALL histdef(hist_id, 'vegalb_vis', 'Vegetation Albedo visible', '1', &
3415            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3416       CALL histdef(hist_id, 'vegalb_nir', 'Vegetation Albedo near infrared', '1', &
3417            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3418       ! Fin ajouts Nathalie - Septembre 2008
3419       CALL histdef(hist_id, 'z0', 'Surface roughness', 'm',  &
3420            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3421       CALL histdef(hist_id, 'roughheight', 'Effective roughness height', 'm',  &
3422            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(2), dt,dw)
3423       CALL histdef(hist_id, 'transpir', 'Transpiration', 'mm/d', &
3424            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop(2), dt,dw)
3425       CALL histdef(hist_id, 'inter', 'Interception loss', 'mm/d', &
3426            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop(2), dt,dw)
3427       !-
3428       !- SECHIBA_HISTLEVEL = 3
3429       !-
3430       CALL histdef(hist_id, 'tsol_max', 'Maximum Surface Temperature',&
3431            & 'C', iim,jjm, hori_id, 1,1,1, -99, 32, tmaxcels(3), dt,dw)
3432       CALL histdef(hist_id, 'tsol_min', 'Minimum Surface Temperature',&
3433            & 'C', iim,jjm, hori_id, 1,1,1, -99, 32, tmincels(3), dt,dw)
3434       CALL histdef(hist_id, 'fluxsens', 'Sensible Heat Flux', 'W/m^2',  &
3435            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(3), dt,dw)
3436       CALL histdef(hist_id, 'fluxlat', 'Latent Heat Flux', 'W/m^2',  &
3437            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(3), dt,dw)
3438       CALL histdef(hist_id, 'snow', 'Snow mass', 'kg/m^2', &
3439            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(3), dt,dw)
3440       CALL histdef(hist_id, 'snowage', 'Snow age', '?', &
3441            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(3), dt,dw)
3442       CALL histdef(hist_id, 'snownobio', 'Snow on other surfaces', 'kg/m^2', &
3443            & iim,jjm, hori_id, nnobio,1, nnobio, nobioax_id, 32, avescatter(3), dt,dw)
3444       CALL histdef(hist_id, 'snownobioage', 'Snow age on other surfaces', 'd', &
3445            & iim,jjm, hori_id, nnobio,1, nnobio, nobioax_id, 32, avescatter(3), dt,dw)
3446       CALL histdef(hist_id, 'vegetfrac', 'Fraction of vegetation', '1', &
3447            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(3), dt,dw)
3448       CALL histdef(hist_id, 'maxvegetfrac', 'Maximum fraction of vegetation', '1', &
3449            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(3), dt,dw)
3450       CALL histdef(hist_id, 'nobiofrac', 'Fraction of other surface types', '1', &
3451            & iim,jjm, hori_id, nnobio, 1, nnobio, nobioax_id, 32, avescatter(3), dt,dw)
3452       IF ( control_flags%hydrol_cwrr ) THEN
3453          DO jst=1,nstm
3454             
3455             ! var_name= "mc_1" ... "mc_3"
3456             WRITE (var_name,"('moistc_',i1)") jst
3457             CALL histdef(hist_id, var_name, 'Soil Moisture profile for soil type', '%', &
3458                  & iim,jjm, hori_id, nslm, 1, nslm, solayax_id, 32, avescatter(3),  dt,dw)
3459             
3460             ! var_name= "vegetsoil_1" ... "vegetsoil_3"
3461             WRITE (var_name,"('vegetsoil_',i1)") jst
3462             CALL histdef(hist_id, var_name, 'Fraction of vegetation on soil types', '%', &
3463                  & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(3),  dt,dw)
3464             
3465          ENDDO
3466       ENDIF
3467       !-
3468       !- SECHIBA_HISTLEVEL = 4
3469       !-
3470       IF ( .NOT. control_flags%hydrol_cwrr ) THEN
3471          CALL histdef(hist_id, 'dss', 'Up-reservoir Height', 'm',  &
3472               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(4), dt,dw)
3473          CALL histdef(hist_id, 'gqsb', 'Upper Soil Moisture', '1',  &
3474               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(4), dt,dw)
3475          CALL histdef(hist_id, 'bqsb', 'Lower Soil Moisture', '1',  &
3476               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(4), dt,dw)
3477       ELSE
3478          CALL histdef(hist_id, 'humtot', 'Total Soil Moisture', 'Kg/m2', &
3479               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(4), dt,dw)
3480          CALL histdef(hist_id, 'humtot_soil', 'Soil Moisture for soil type', 'Kg/m2', &
3481               & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, avescatter(4), dt,dw)
3482       ENDIF
3483       CALL histdef(hist_id, 'qsintveg', 'Water on canopy', 'Kg/m^2', &
3484            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(4), dt,dw)
3485       CALL histdef(hist_id, 'rstruct', 'Structural resistance', 's/m', &
3486            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(4), dt,dw)
3487       IF ( control_flags%ok_co2 ) THEN
3488          CALL histdef(hist_id, 'gpp', 'Net assimilation of carbon by the vegetation', 'gC/m^2/s', &
3489               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(4), dt,dw)
3490       ENDIF
3491       IF ( control_flags%ok_stomate ) THEN
3492          CALL histdef(hist_id, 'nee', 'Net Ecosystem Exchange', 'gC/m^2/s', &
3493               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(4), dt,dw)
3494          CALL histdef(hist_id, 'maint_resp', 'Maintenance respiration', 'gC/m^2/s', &
3495               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(4), dt,dw)
3496          CALL histdef(hist_id, 'hetero_resp', 'Heterotrophic respiration', 'gC/m^2/s', &
3497               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(4), dt,dw)
3498          CALL histdef(hist_id, 'growth_resp', 'Growth respiration', 'gC/m^2/s', &
3499               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(4), dt,dw)
3500          CALL histdef(hist_id, 'npp', 'Net Primary Production', 'gC/m^2/s', &
3501               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt, dw)
3502       ENDIF
3503       CALL histdef(hist_id, 'precisol', 'Throughfall', 'mm/d',  &
3504            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop(4), dt,dw)
3505       CALL histdef(hist_id, 'drysoil_frac', 'Fraction of visibly dry soil', '1',  &
3506            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(4), dt,dw)
3507       CALL histdef(hist_id, 'evapot', 'Potential evaporation', 'mm/d',  &
3508            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(4), dt,dw)
3509       CALL histdef(hist_id, 'evapot_corr', 'Potential evaporation', 'mm/d',  &
3510            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(4), dt,dw)
3511       !-
3512       !- SECHIBA_HISTLEVEL = 5
3513       !-
3514       CALL histdef(hist_id, 'swnet', 'Net solar radiation', 'W/m^2',  &
3515            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(5), dt,dw)
3516       CALL histdef(hist_id, 'swdown', 'Incident solar radiation', 'W/m^2',  &
3517            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(5), dt,dw)
3518       CALL histdef(hist_id, 'lwdown', 'Absorbed downward longwave radiation', 'W/m^2',  &
3519            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(5), dt,dw)
3520       CALL histdef(hist_id, 'lwnet', 'Net surface longwave radiation', 'W/m^2',  &
3521            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(5), dt,dw)
3522       CALL histdef(hist_id, 'temp_pheno', 'Temperature for Pheno', 'K',  &
3523            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(5), dt,dw)
3524       !-
3525       !- SECHIBA_HISTLEVEL = 6
3526       !-
3527       CALL histdef(hist_id, 'ptn', 'Deep ground temperature', 'K', &
3528            & iim,jjm, hori_id, ngrnd, 1, ngrnd, solax_id, 32, avescatter(6),  dt,dw)
3529       !-
3530       !- SECHIBA_HISTLEVEL = 7
3531       !-
3532       IF ( control_flags%river_routing ) THEN
3533          CALL histdef(hist_id, 'fastr', 'Fast flow reservoir', 'kg/m^2', &
3534               & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(7), dt,dw)
3535          CALL histdef(hist_id, 'slowr', 'Slow flow reservoir', 'kg/m^2', &
3536               & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(7), dt,dw)
3537          CALL histdef(hist_id, 'streamr', 'Stream flow reservoir', 'kg/m^2', &
3538               & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(7), dt,dw)
3539          CALL histdef(hist_id, 'lakevol', 'Volume in lake reservoir', 'kg/m^2', &
3540               & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(7), dt,dw)
3541          CALL histdef(hist_id, 'irrigation', 'Net irrigation', 'mm/d', &
3542               & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(7), dt,dw)
3543          CALL histdef(hist_id, 'netirrig', 'Net irrigation requirement', 'mm/d', &
3544               & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop(7), dt,dw)
3545          CALL histdef(hist_id, 'irrigmap', 'Map of irrigated areas', 'm^2', &
3546               & iim,jjm, hori_id, 1,1,1, -99, 32, once(7), dt,dw)
3547       ENDIF
3548       !-
3549       !- SECHIBA_HISTLEVEL = 8
3550       !-
3551       CALL histdef(hist_id, 'beta', 'Beta Function', '1',  &
3552            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3553       CALL histdef(hist_id, 'raero', 'Aerodynamic resistance', 's/m',  &
3554            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3555       ! Ajouts Nathalie - Novembre 2006
3556       CALL histdef(hist_id, 'cdrag', 'Drag coefficient for LE and SH', '?',  &
3557            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3558       CALL histdef(hist_id, 'Wind', 'Wind speed', 'm/s',  &
3559            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3560       ! Fin ajouts Nathalie
3561!MM
3562       CALL histdef(hist_id, 'qsatt' , 'Surface saturated humidity', 'g/g', &
3563            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3564       CALL histdef(hist_id, 'vbeta1', 'Beta for sublimation', '1',  &
3565            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3566       CALL histdef(hist_id, 'vbeta4', 'Beta for bare soil', '1',  &
3567            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(8), dt,dw)
3568       CALL histdef(hist_id, 'vbetaco2', 'beta for CO2', 'mm/d', &
3569            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(8), dt,dw)
3570       CALL histdef(hist_id, 'soiltype', 'Fraction of soil textures', '%', &
3571            & iim,jjm, hori_id, nstm, 1, nstm, soltax_id, 32, once(8),  dt,dw)
3572       CALL histdef(hist_id, 'humrel', 'Soil moisture stress', '1',  &
3573            & iim,jjm, hori_id, nvm,1,nvm, vegax_id, 32, avescatter(8), dt,dw)
3574       !-
3575       !- SECHIBA_HISTLEVEL = 9
3576       !-
3577       !-
3578       !- SECHIBA_HISTLEVEL = 10
3579       !-
3580       CALL histdef(hist_id, 'cimean', 'Stomatal CO2 concentation', 'mmole/m2/s', &
3581            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(10), dt,dw)
3582       CALL histdef(hist_id, 'vbeta3', 'Beta for Transpiration', 'mm/d', &
3583            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(10), dt,dw)
3584       CALL histdef(hist_id, 'rveget', 'Canopy resistance', 's/m', &
3585            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(10), dt,dw)
3586       CALL histdef(hist_id, 'rsol', 'Soil resistance', 's/m',  &
3587            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(10), dt,dw)
3588       CALL histdef(hist_id,'vbeta2','Beta for Interception loss','mm/d', &
3589            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(10), dt,dw)
3590       CALL histdef(hist_id, 'qsintmax', 'Maximum Interception capacity', 'Kg/m^2', &
3591            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(10), dt,dw)
3592
3593       !- SECHIBA_HISTLEVEL = 11
3594       !-
3595
3596       IF ( .NOT. control_flags%hydrol_cwrr ) THEN
3597          CALL histdef(hist_id, 'mrsos', "Moisture in Upper 0.1 m of Soil Column", "kg m-2", &
3598               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3599
3600          CALL histdef(hist_id, 'mrso', "Total Soil Moisture Content", "kg m-2", &
3601               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3602
3603          CALL histdef(hist_id, 'mrros', "Surface Runoff", "kg m-2 s-1", &
3604               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3605
3606          CALL histdef(hist_id, 'mrro', "Total Runoff", "kg m-2 s-1", &
3607               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3608
3609          CALL histdef(hist_id, 'prveg', "Precipitation onto Canopy", "kg m-2 s-1", &
3610               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3611
3612       ENDIF
3613
3614
3615       CALL histdef(hist_id, 'evspsblveg', "Evaporation from Canopy", "kg m-2 s-1", &
3616            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3617
3618       CALL histdef(hist_id, 'evspsblsoi', "Water Evaporation from Soil", "kg m-2 s-1", &
3619            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3620
3621       CALL histdef(hist_id, 'tran', "Transpiration", "kg m-2 s-1", &
3622            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3623
3624       CALL histdef(hist_id, 'treeFrac', "Tree Cover Fraction", "%", &
3625            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3626
3627       CALL histdef(hist_id, 'grassFrac', "Natural Grass Fraction", "%", &
3628            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3629
3630       CALL histdef(hist_id, 'cropFrac', "Crop Fraction", "%", &
3631            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3632
3633       CALL histdef(hist_id, 'baresoilFrac', "Bare Soil Fraction", "%", &
3634            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3635
3636       CALL histdef(hist_id, 'residualFrac', &
3637            & "Fraction of Grid Cell that is Land but Neither Vegetation-Covered nor Bare Soil", "%", &
3638            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(11), dt,dw)
3639
3640    ELSE 
3641       !-
3642       !- This is the ALMA convention output now
3643       !-
3644       !-
3645       IF ( rectilinear ) THEN
3646#ifdef CPP_PARA
3647          CALL histbeg(histname, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3648               &     istp_old, date0, dt, hori_id, hist_id,orch_domain_id)
3649#else
3650          CALL histbeg(histname, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3651               &     istp_old, date0, dt, hori_id, hist_id)
3652#endif
3653       ELSE
3654#ifdef CPP_PARA
3655          CALL histbeg(histname, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3656               &     istp_old, date0, dt, hori_id, hist_id,domain_id=orch_domain_id)
3657#else
3658          CALL histbeg(histname, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3659               &     istp_old, date0, dt, hori_id, hist_id)
3660#endif
3661       ENDIF
3662       !-
3663       CALL histvert(hist_id, 'veget', 'Vegetation types', '1', &
3664            &    nvm,   veg, vegax_id)
3665       CALL histvert(hist_id, 'solth', 'Soil levels',      'm', &
3666            &    ngrnd, sol, solax_id)
3667       CALL histvert(hist_id, 'soiltyp', 'Soil types',      '1', &
3668            &    nstm, soltyp, soltax_id)
3669       CALL histvert(hist_id, 'nobio', 'Other surface types',      '1', &
3670            &    nnobio, nobiotyp, nobioax_id)
3671       IF (  control_flags%hydrol_cwrr ) THEN
3672          CALL histvert(hist_id, 'solay', 'Hydrol soil levels',      'm', &
3673               &    nslm, solay, solayax_id)
3674       ENDIF
3675     !-
3676     !-  Vegetation
3677     !-
3678       CALL histdef(hist_id, 'vegetfrac', 'Fraction of vegetation', '1', &
3679            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(3), dt,dw)
3680       CALL histdef(hist_id, 'maxvegetfrac', 'Maximum fraction of vegetation', '1', &
3681            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter(3), dt,dw)
3682       CALL histdef(hist_id, 'nobiofrac', 'Fraction of other surface types', '1', &
3683            & iim,jjm, hori_id, nnobio, 1, nnobio, nobioax_id, 32, avescatter(3), dt,dw)
3684     !-
3685     !-  General energy balance
3686     !-
3687       CALL histdef(hist_id, 'SWnet', 'Net shortwave radiation', 'W/m^2',  &
3688            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(1), dt,dw)
3689       CALL histdef(hist_id, 'LWnet', 'Net longwave radiation', 'W/m^2',  &
3690            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3691       CALL histdef(hist_id, 'Qh', 'Sensible heat flux', 'W/m^2',  &
3692            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(1), dt,dw)
3693       CALL histdef(hist_id, 'Qle', 'Latent heat flux', 'W/m^2',  &
3694            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(1), dt,dw)
3695       CALL histdef(hist_id, 'Qg', 'Ground heat flux', 'W/m^2',  &
3696            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3697       CALL histdef(hist_id, 'Qf', 'Energy of fusion', 'W/m^2',  &
3698            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(2), dt,dw)
3699       CALL histdef(hist_id, 'Qv', 'Energy of sublimation', 'W/m^2',  &
3700            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3701       CALL histdef(hist_id, 'DelSurfHeat', 'Change in surface layer heat', 'J/m^2',  &
3702            & iim,jjm, hori_id, 1,1,1, -99, 32, sumscatter(1), dt,dw)
3703       CALL histdef(hist_id, 'DelColdCont', 'Change in snow surface layer cold content', 'J/m^2',  &
3704            & iim,jjm, hori_id, 1,1,1, -99, 32, sumscatter(1), dt,dw)
3705    !-
3706    !- General water balance
3707    !-
3708       CALL histdef(hist_id, 'Snowf', 'Snowfall rate', 'kg/m^2/s',  &
3709            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3710       CALL histdef(hist_id, 'Rainf', 'Rainfall rate', 'kg/m^2/s',  &
3711            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3712       CALL histdef(hist_id, 'Evap', 'Total Evapotranspiration', 'kg/m^2/s', &
3713            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3714       CALL histdef(hist_id, 'Qs', 'Surface runoff', 'kg/m^2/s', &
3715            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3716       CALL histdef(hist_id, 'Qsb', 'Sub-surface runoff', 'kg/m^2/s', &
3717            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3718       CALL histdef(hist_id, 'Qsm', 'Snowmelt', 'kg/m^2/s', &
3719            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3720       CALL histdef(hist_id, 'DelSoilMoist', 'Change in soil moisture', 'kg/m^2',  &
3721            & iim,jjm, hori_id, 1, 1, 1, -99, 32, sumscatter(1), dt,dw)
3722       CALL histdef(hist_id, 'DelSWE', 'Change in SWE','kg/m^2',&
3723            & iim,jjm, hori_id, 1, 1, 1, -99, 32, sumscatter(1), dt,dw)
3724       CALL histdef(hist_id, 'DelIntercept', 'Change in interception storage', 'kg/m^2',  &
3725            & iim,jjm, hori_id, 1, 1, 1, -99, 32, sumscatter(1), dt,dw)
3726    !-
3727    !- Surface state
3728    !-
3729       CALL histdef(hist_id, 'AvgSurfT', 'Average surface temperature', 'K', &
3730            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(1), dt,dw)
3731       CALL histdef(hist_id, 'RadT', 'Surface radiative temperature', 'K', &
3732            & iim,jjm, hori_id, 1,1,1, -99, 32, ave(1), dt,dw)
3733       CALL histdef(hist_id, 'Albedo', 'Albedo', '1', &
3734            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3735       CALL histdef(hist_id, 'SWE', '3D soil water equivalent','kg/m^2',  &
3736            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3737    !!-
3738    !-  Sub-surface state
3739    !-
3740       IF ( .NOT. control_flags%hydrol_cwrr ) THEN
3741          CALL histdef(hist_id, 'SoilMoist', '3D average layer soil moisture', 'kg/m^2',  &
3742               & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(1), dt,dw)
3743       ELSE
3744          CALL histdef(hist_id, 'SoilMoist', '3D average layer soil moisture', 'kg/m^2',  &
3745               & iim,jjm, hori_id, nslm, 1, nslm, solayax_id, 32, avescatter(1), dt,dw)
3746       ENDIF
3747       CALL histdef(hist_id, 'SoilWet', 'Total soil wetness', 'kg/m^2',  &
3748            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(1), dt,dw)
3749       CALL histdef(hist_id, 'SoilTemp', '3D layer average soil temperature', 'K', &
3750            & iim,jjm, hori_id, ngrnd, 1, ngrnd, solax_id, 32, avescatter(1),  dt,dw)
3751    !-
3752    !-  Evaporation components
3753    !-
3754       CALL histdef(hist_id, 'PotEvap', 'Potential evapotranspiration', 'kg/m^2/s', &
3755            & iim,jjm, hori_id, 1, 1, 1, -99, 32, fluxop_scinsec(1), dt,dw)
3756       CALL histdef(hist_id, 'ECanop', 'Interception evaporation', 'kg/m^2/s', &
3757            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3758       CALL histdef(hist_id, 'TVeg', 'Transpiration', 'kg/m^2/s', &
3759            & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3760       CALL histdef(hist_id, 'ESoil', 'Bare soil evaporation', 'kg/m^2/s', &
3761            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3762       CALL histdef(hist_id, 'RootMoist','Root zone soil water', 'kg/m^2',  &
3763            & iim,jjm, hori_id, 1, 1, 1, -99, 32, avescatter(1), dt,dw)
3764       CALL histdef(hist_id, 'SubSnow','Snow sublimation','kg/m^2/s', &
3765            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(1), dt,dw)
3766       CALL histdef(hist_id, 'ACond', 'Aerodynamic conductance', 'm/s',  &
3767            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3768    !-
3769    !-
3770    !-  Cold Season Processes
3771    !-
3772       CALL histdef(hist_id, 'SnowFrac', 'Snow cover fraction', '1',  &
3773            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3774       CALL histdef(hist_id, 'SAlbedo', 'Snow albedo', '1', &
3775            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3776       CALL histdef(hist_id, 'SnowDepth', '3D snow depth', 'm', &
3777            & iim,jjm, hori_id, 1,1,1, -99, 32, avescatter(1), dt,dw)
3778    !-
3779    !- Hydrologic variables
3780    !-
3781       CALL histdef(hist_id, 'IrrigationMap', 'Map of irrigated areas', 'm^2', &
3782            & iim,jjm, hori_id, 1,1,1, -99, 32, once(7), dt,dw)
3783       CALL histdef(hist_id, 'dis', 'Simulated River Discharge', 'm^3/s', &
3784            & iim,jjm, hori_id, 1,1,1, -99, 32, fluxop_scinsec(2), dt,dw)
3785       CALL histdef(hist_id, 'humrel', 'Soil moisture stress', '1',  &
3786            & iim,jjm, hori_id, nvm,1,nvm, vegax_id, 32, avescatter(8), dt,dw)
3787    !-
3788    !-  The carbon budget
3789    !-
3790       IF ( control_flags%ok_co2 ) THEN
3791          CALL histdef(hist_id, 'GPP', 'Net assimilation of carbon by the vegetation', 'gC/m^2/s', &
3792               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3793       ENDIF
3794       IF ( control_flags%ok_stomate ) THEN
3795          CALL histdef(hist_id, 'NEE', 'Net Ecosystem Exchange', 'gC/m^2/s', &
3796               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3797          CALL histdef(hist_id, 'maint_resp', 'Maintenance respiration', 'gC/m^2/s', &
3798               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3799          CALL histdef(hist_id, 'hetero_resp', 'Heterotrophic respiration', 'gC/m^2/s', &
3800               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3801          CALL histdef(hist_id, 'growth_resp', 'Growth respiration', 'gC/m^2/s', &
3802               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3803          CALL histdef(hist_id, 'npp', 'Net Primary Production', 'gC/m^2/s', &
3804               & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, fluxop_scinsec(1), dt,dw)
3805       ENDIF
3806    !
3807    ENDIF
3808    !-
3809    CALL histdef(hist_id, 'LandPoints', 'Land Points', '1', &
3810               & iim,jjm, hori_id, 1,1,1, -99, 32, once(1), dt,dw) 
3811    CALL histdef(hist_id, 'Areas', 'Mesh areas', 'm2', &
3812         & iim,jjm, hori_id, 1,1,1, -99, 32, once(1), dt, dw)
3813    CALL histdef(hist_id, 'Contfrac', 'Continental fraction', '1', &
3814         & iim,jjm, hori_id, 1,1,1, -99, 32, once(1), dt, dw)
3815    !-
3816    CALL histend(hist_id)
3817    !
3818    !
3819    ! Second SECHIBA hist file
3820    !
3821    !-
3822    !Config  Key  = SECHIBA_HISTFILE2
3823    !Config  Desc = Flag to switch on histfile 2 for SECHIBA (hi-frequency ?)
3824    !Config  Def  = FALSE
3825    !Config  Help = This Flag switch on the second SECHIBA writing for hi (or low)
3826    !Config         frequency writing. This second output is optional and not written
3827    !Config         by default.
3828    !Config MM is it right ? Second output file is produced with the same level
3829    !Config         as the first one.
3830    !-
3831    ok_histfile2=.FALSE.
3832    CALL getin_p('SECHIBA_HISTFILE2', ok_histfile2)
3833    WRITE(numout,*) 'SECHIBA_HISTFILE2 ', ok_histfile2
3834    !
3835    hist2_id = -1
3836    !
3837    IF (ok_histfile2) THEN
3838       !-
3839       !Config  Key  = SECHIBA_OUTPUT_FILE2
3840       !Config  Desc = Name of file in which the output number 2 is going
3841       !Config         to be written
3842       !Config  If   = SECHIBA_HISTFILE2
3843       !Config  Def  = sechiba_out_2.nc
3844       !Config  Help = This file is going to be created by the model
3845       !Config         and will contain the output 2 from the model.
3846       !-
3847       histname2='sechiba_out_2.nc'
3848       CALL getin_p('SECHIBA_OUTPUT_FILE2', histname2)
3849       WRITE(numout,*) 'SECHIBA_OUTPUT_FILE2 ', histname2
3850       !-
3851       !Config  Key  = WRITE_STEP2
3852       !Config  Desc = Frequency in seconds at which to WRITE output
3853       !Config  If   = SECHIBA_HISTFILE2
3854       !Config  Def  = 1800.0
3855       !Config  Help = This variables gives the frequency the output 2 of
3856       !Config         the model should be written into the netCDF file.
3857       !Config         It does not affect the frequency at which the
3858       !Config         operations such as averaging are done.
3859       !Config         That is IF the coding of the calls to histdef
3860       !Config         are correct !
3861       !-
3862       dw2 = 1800.0
3863       CALL getin_p('WRITE_STEP2', dw2)
3864       !-
3865       !Config  Key  = SECHIBA_HISTLEVEL2
3866       !Config  Desc = SECHIBA history 2 output level (0..10)
3867       !Config  If   = SECHIBA_HISTFILE2
3868       !Config  Def  = 1
3869       !Config  Help = Chooses the list of variables in the history file.
3870       !Config         Values between 0: nothing is written; 10: everything is
3871       !Config         written are available More details can be found on the web under documentation.
3872       !Config         web under documentation.
3873       !Config         First level contains all ORCHIDEE outputs.
3874       !-
3875       hist2_level = 1
3876       CALL getin_p('SECHIBA_HISTLEVEL2', hist2_level)
3877       !-
3878       WRITE(numout,*) 'SECHIBA history level 2 : ',hist2_level
3879       IF ( (hist2_level > max_hist_level).OR.(hist2_level < 0) ) THEN
3880          STOP 'This history level 2 is not allowed'
3881       ENDIF
3882       !
3883       !-
3884       !- define operations as a function of history level.
3885       !- Above hist2_level, operation='never'
3886       !-
3887       ave2(1:max_hist_level) = 'ave(X)'
3888       IF (hist2_level < max_hist_level) THEN
3889          ave2(hist2_level+1:max_hist_level) = 'never'
3890       ENDIF
3891       sumscatter2(1:max_hist_level) = 't_sum(scatter(X))'
3892       IF (hist2_level < max_hist_level) THEN
3893          sumscatter2(hist2_level+1:max_hist_level) = 'never'
3894       ENDIF
3895       avecels2(1:max_hist_level) = 'ave(cels(X))'
3896       IF (hist2_level < max_hist_level) THEN
3897          avecels2(hist2_level+1:max_hist_level) = 'never'
3898       ENDIF
3899       avescatter2(1:max_hist_level) = 'ave(scatter(X))'
3900       IF (hist2_level < max_hist_level) THEN
3901          avescatter2(hist2_level+1:max_hist_level) = 'never'
3902       ENDIF
3903       tmincels2(1:max_hist_level) = 't_min(cels(X))'
3904       IF (hist2_level < max_hist_level) THEN
3905          tmincels2(hist2_level+1:max_hist_level) = 'never'
3906       ENDIF
3907       tmaxcels2(1:max_hist_level) = 't_max(cels(X))'
3908       IF (hist2_level < max_hist_level) THEN
3909          tmaxcels2(hist2_level+1:max_hist_level) = 'never'
3910       ENDIF
3911!!$       tmax2(1:max_hist_level) = 't_max(X)'
3912!!$       IF (hist2_level < max_hist_level) THEN
3913!!$          tmax2(hist2_level+1:max_hist_level) = 'never'
3914!!$       ENDIF
3915       fluxop2(1:max_hist_level) = flux_op
3916       IF (hist2_level < max_hist_level) THEN
3917          fluxop2(hist2_level+1:max_hist_level) = 'never'
3918       ENDIF
3919!!$       fluxop_sc2(1:max_hist_level) = flux_sc
3920!!$       IF (hist2_level < max_hist_level) THEN
3921!!$          fluxop_sc2(hist2_level+1:max_hist_level) = 'never'
3922!!$       ENDIF
3923!!$       fluxop_insec2(1:max_hist_level) = flux_insec
3924!!$       IF (hist2_level < max_hist_level) THEN
3925!!$          fluxop_insec2(hist2_level+1:max_hist_level) = 'never'
3926!!$       ENDIF
3927       fluxop_scinsec2(1:max_hist_level) = flux_scinsec
3928       IF (hist2_level < max_hist_level) THEN
3929          fluxop_scinsec2(hist2_level+1:max_hist_level) = 'never'
3930       ENDIF
3931       once2(1:max_hist_level) = 'once(scatter(X))'
3932       IF (hist2_level < max_hist_level) THEN
3933          once2(hist2_level+1:max_hist_level) = 'never'
3934       ENDIF
3935       !
3936       IF ( .NOT. almaoutput ) THEN
3937          !-
3938          IF ( rectilinear ) THEN
3939#ifdef CPP_PARA
3940             CALL histbeg(histname2, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3941                  &     istp_old, date0, dt, hori_id2, hist2_id,orch_domain_id)
3942#else
3943             CALL histbeg(histname2, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
3944                  &     istp_old, date0, dt, hori_id2, hist2_id)
3945#endif
3946             WRITE(numout,*)  'HISTBEG2 --->',istp_old,date0,dt,dw2,hist2_id
3947          ELSE
3948#ifdef CPP_PARA
3949             CALL histbeg(histname2, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3950                  &     istp_old, date0, dt, hori_id2, hist2_id,domain_id=orch_domain_id)
3951#else
3952             CALL histbeg(histname2, iim, lon, jjm, lat, 1, iim, 1, jjm, &
3953                  &     istp_old, date0, dt, hori_id2, hist2_id)
3954#endif
3955          ENDIF
3956          !-
3957          CALL histvert(hist2_id, 'veget', 'Vegetation types', '1', &
3958               &    nvm,   veg, vegax_id2)
3959          CALL histvert(hist2_id, 'solth', 'Soil levels',      'm', &
3960               &    ngrnd, sol, solax_id2)
3961          CALL histvert(hist2_id, 'soiltyp', 'Soil types',      '1', &
3962               &    nstm, soltyp, soltax_id2)
3963          CALL histvert(hist2_id, 'nobio', 'Other surface types',      '1', &
3964               &    nnobio, nobiotyp, nobioax_id2)
3965          CALL histvert(hist2_id, 'albtyp', 'Albedo Types',     '1', &
3966               &    2, albtyp, albax_id2)
3967          IF (  control_flags%hydrol_cwrr ) THEN
3968             CALL histvert(hist2_id, 'solay', 'Hydrol soil levels',      'm', &
3969                  &    nslm, solay, solayax_id2)
3970          ENDIF
3971          !-
3972          !- SECHIBA_HISTLEVEL2 = 1
3973          !-
3974          CALL histdef(hist2_id, 'ptn', 'Deep ground temperature', 'K', &
3975               & iim,jjm, hori_id2, ngrnd, 1, ngrnd, solax_id2, 32, avescatter2(1),  dt, dw2)
3976          IF ( .NOT. control_flags%hydrol_cwrr ) THEN
3977             CALL histdef(hist2_id, 'mrsos', "Moisture in Upper 0.1 m of Soil Column", "kg m-2", &
3978                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(1), dt,dw2)
3979
3980             CALL histdef(hist2_id, 'mrro', "Total Runoff", "kg m-2 s-1", &
3981                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(1), dt,dw2)
3982          ENDIF
3983          !-
3984          !- SECHIBA_HISTLEVEL2 = 2
3985          !-
3986          CALL histdef(hist2_id, 'cdrag', 'Drag coefficient for LE and SH', '?',  &
3987               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt, dw2)
3988          ! Ajouts Nathalie - Septembre 2008
3989          CALL histdef(hist2_id, 'soilalb_vis', 'Soil Albedo visible', '1', &
3990               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt,dw2)
3991          CALL histdef(hist2_id, 'soilalb_nir', 'Soil Albedo near infrared', '1', &
3992               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt,dw2)
3993          CALL histdef(hist2_id, 'vegalb_vis', 'Vegetation Albedo visible', '1', &
3994               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt,dw2)
3995          CALL histdef(hist2_id, 'vegalb_nir', 'Vegetation Albedo near infrared', '1', &
3996               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt,dw2)
3997          ! Fin ajouts Nathalie - Septembre 2008
3998          CALL histdef(hist2_id, 'z0', 'Surface roughness', 'm',  &
3999               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(2), dt, dw2)
4000          CALL histdef(hist2_id, 'coastalflow', 'Diffuse coastal flow', 'm^3/s', &
4001               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(2), dt, dw2)
4002          CALL histdef(hist2_id, 'riverflow', 'River flow to the oceans', 'm^3/s', &
4003               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(2), dt, dw2) 
4004          CALL histdef(hist2_id, 'tsol_rad', 'Radiative surface temperature', 'C', &
4005               & iim,jjm, hori_id2, 1,1,1, -99, 32, avecels2(2), dt, dw2)
4006          CALL histdef(hist2_id, 'vevapnu', 'Bare soil evaporation', 'mm/d', &
4007               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(2), dt, dw2)
4008          CALL histdef(hist2_id, 'temp_sol', 'New Surface Temperature', 'C', &
4009               & iim,jjm, hori_id2, 1,1,1, -99, 32, avecels2(2), dt, dw2)
4010          CALL histdef(hist2_id, 'qsurf', 'Near surface specific humidity', 'g/g',  &
4011               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(2), dt, dw2)
4012          CALL histdef(hist2_id, 'albedo', 'Albedo', '1', &
4013               & iim,jjm, hori_id2, 2,1,2, albax_id2, 32, avescatter2(2), dt, dw2)
4014          CALL histdef(hist2_id, 'fluxsens', 'Sensible Heat Flux', 'W/m^2',  &
4015               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(2), dt, dw2)
4016          CALL histdef(hist2_id, 'fluxlat', 'Latent Heat Flux', 'W/m^2',  &
4017               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(2), dt, dw2)
4018          CALL histdef(hist2_id, 'emis', 'Surface emissivity', '?', &
4019               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(2), dt, dw2)
4020          !-
4021          !- SECHIBA_HISTLEVEL2 = 3
4022          !-
4023          CALL histdef(hist2_id, 'evap', 'Evaporation', 'mm/d', &
4024               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(3), dt, dw2)
4025          CALL histdef(hist2_id, 'rain', 'Rainfall', 'mm/d',  &
4026               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(3), dt, dw2)
4027          CALL histdef(hist2_id, 'snowf', 'Snowfall', 'mm/d',  &
4028               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(3), dt, dw2)
4029          CALL histdef(hist2_id, 'netrad', 'Net radiation', 'W/m^2',  &
4030               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(3), dt, dw2)
4031          CALL histdef(hist2_id, 'lai', 'Leaf Area Index', '1', &
4032               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(3), dt, dw2)
4033          IF ( control_flags%river_routing ) THEN
4034             CALL histdef(hist2_id, 'basinmap', 'Aproximate map of the river basins', ' ', &
4035                  & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(3), dt, dw2) 
4036             CALL histdef(hist2_id, 'nbrivers', 'Number or rivers in the outflow grid box', ' ', &
4037                  & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(3), dt, dw2) 
4038          ENDIF
4039          !-
4040          !- SECHIBA_HISTLEVEL2 = 4
4041          !-
4042          CALL histdef(hist2_id, 'subli', 'Sublimation', 'mm/d', &
4043               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(4), dt, dw2)
4044          CALL histdef(hist2_id, 'runoff', 'Surface runoff', 'mm/d', &
4045               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(4), dt, dw2)
4046          CALL histdef(hist2_id, 'drainage', 'Deep drainage', 'mm/d', &
4047               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(4), dt, dw2)
4048          IF ( control_flags%river_routing ) THEN
4049             CALL histdef(hist2_id, 'riversret', 'Return from endorheic rivers', 'mm/d', &
4050                  & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(4), dt, dw2)
4051             CALL histdef(hist2_id, 'hydrographs', 'Hydrographs of gridbox outflow', 'm^3/s', &
4052                  & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(4), dt, dw2)
4053          ENDIF
4054          IF ( control_flags%hydrol_cwrr ) THEN
4055             CALL histdef(hist2_id, 'evapnu_soil', 'Bare soil evap for soil type', 'mm/d', &
4056                  & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, fluxop2(4), dt, dw2)
4057             CALL histdef(hist2_id, 'drainage_soil', 'Drainage for soil type', 'mm/d', &
4058                  & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, fluxop2(4), dt, dw2)
4059             CALL histdef(hist2_id, 'transpir_soil', 'Transpir for soil type', 'mm/d', &
4060                  & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, fluxop2(4), dt, dw2)
4061             CALL histdef(hist2_id, 'runoff_soil', 'Runoff for soil type', 'mm/d', &
4062                  & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, fluxop2(4), dt, dw2)
4063          ENDIF
4064          !
4065          CALL histdef(hist2_id, 'tair', 'Air Temperature', 'K',  &
4066               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4067          CALL histdef(hist2_id, 'qair', 'Air humidity', 'g/g',  &
4068               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4069          ! Ajouts Nathalie - Juillet 2006
4070          CALL histdef(hist2_id, 'q2m', '2m Air humidity', 'g/g',  &
4071               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4072          CALL histdef(hist2_id, 't2m', '2m Air Temperature', 'K',  &
4073               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4074          ! Fin ajouts Nathalie
4075          CALL histdef(hist2_id, 'alb_vis', 'Albedo visible', '1', &
4076               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4077          CALL histdef(hist2_id, 'alb_nir', 'Albedo near infrared', '1', &
4078               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(4), dt, dw2)
4079          CALL histdef(hist2_id, 'roughheight', 'Effective roughness height', 'm',  &
4080               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(4), dt, dw2)
4081          CALL histdef(hist2_id, 'transpir', 'Transpiration', 'mm/d', &
4082               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop2(4), dt, dw2)
4083          CALL histdef(hist2_id, 'inter', 'Interception loss', 'mm/d', &
4084               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop2(4), dt, dw2)
4085          !-
4086          !- SECHIBA_HISTLEVEL2 = 5
4087          !-
4088          CALL histdef(hist2_id, 'tsol_max', 'Maximum Surface Temperature',&
4089               & 'C', iim,jjm, hori_id2, 1,1,1, -99, 32, tmaxcels2(5), dt, dw2)
4090          CALL histdef(hist2_id, 'tsol_min', 'Minimum Surface Temperature',&
4091               & 'C', iim,jjm, hori_id2, 1,1,1, -99, 32, tmincels2(5), dt, dw2)
4092          CALL histdef(hist2_id, 'snow', 'Snow mass', 'kg/m^2', &
4093               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(5), dt, dw2)
4094          CALL histdef(hist2_id, 'snowage', 'Snow age', '?', &
4095               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(5), dt, dw2)
4096          CALL histdef(hist2_id, 'snownobio', 'Snow on other surfaces', 'kg/m^2', &
4097               & iim,jjm, hori_id2, nnobio,1, nnobio, nobioax_id2, 32, avescatter2(5), dt, dw2)
4098          CALL histdef(hist2_id, 'snownobioage', 'Snow age on other surfaces', 'd', &
4099               & iim,jjm, hori_id2, nnobio,1, nnobio, nobioax_id2, 32, avescatter2(5), dt, dw2)
4100          CALL histdef(hist2_id, 'vegetfrac', 'Fraction of vegetation', '1', &
4101               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(5), dt, dw2)
4102          CALL histdef(hist2_id, 'maxvegetfrac', 'Maximum fraction of vegetation', '1', &
4103               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(5), dt, dw2)
4104          CALL histdef(hist2_id, 'nobiofrac', 'Fraction of other surface types', '1', &
4105               & iim,jjm, hori_id2, nnobio, 1, nnobio, nobioax_id2, 32, avescatter2(5), dt, dw2)
4106          IF ( control_flags%hydrol_cwrr ) THEN
4107             DO jst=1,nstm
4108
4109                ! var_name= "mc_1" ... "mc_3"
4110                WRITE (var_name,"('moistc_',i1)") jst
4111                CALL histdef(hist2_id, var_name, 'Soil Moisture profile for soil type', '%', &
4112                     & iim,jjm, hori_id2, nslm, 1, nslm, solayax_id2, 32, avescatter2(5), dt, dw2)
4113
4114                ! var_name= "vegetsoil_1" ... "vegetsoil_3"
4115                WRITE (var_name,"('vegetsoil_',i1)") jst
4116                CALL histdef(hist2_id, var_name, 'Fraction of vegetation on soil types', '%', &
4117                     & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(5), dt, dw2)
4118
4119             ENDDO
4120          ENDIF
4121          !-
4122          !- SECHIBA_HISTLEVEL2 = 6
4123          !-
4124          IF ( .NOT. control_flags%hydrol_cwrr ) THEN
4125             CALL histdef(hist2_id, 'dss', 'Up-reservoir Height', 'm',  &
4126                  & iim,jjm, hori_id, nvm, 1, nvm, vegax_id, 32, avescatter2(6), dt,dw)
4127             CALL histdef(hist2_id, 'gqsb', 'Upper Soil Moisture', '1',  &
4128                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(6), dt, dw2)
4129             CALL histdef(hist2_id, 'bqsb', 'Lower Soil Moisture', '1',  &
4130                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(6), dt, dw2)
4131          ELSE
4132             CALL histdef(hist2_id, 'humtot', 'Total Soil Moisture', 'Kg/m2', &
4133                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(6), dt, dw2)
4134             CALL histdef(hist2_id, 'humtot_soil', 'Soil Moisture for soil type', 'Kg/m2', &
4135                  & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, avescatter2(6), dt, dw2)
4136          ENDIF
4137          CALL histdef(hist2_id, 'qsintveg', 'Water on canopy', 'Kg/m^2', &
4138               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(6), dt, dw2)
4139          CALL histdef(hist2_id, 'rstruct', 'Structural resistance', 's/m', &
4140               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(6), dt, dw2)
4141          IF ( control_flags%ok_co2 ) THEN
4142             CALL histdef(hist2_id, 'gpp', 'Net assimilation of carbon by the vegetation', 'gC/m^2/s', &
4143                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(6), dt,dw2)
4144          ENDIF
4145          IF ( control_flags%ok_stomate ) THEN
4146             CALL histdef(hist2_id, 'nee', 'Net Ecosystem Exchange', 'gC/m^2/s', &
4147                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(2), dt,dw2)
4148             CALL histdef(hist2_id, 'maint_resp', 'Maintenance respiration', 'gC/m^2/s', &
4149                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(2), dt,dw2)
4150             CALL histdef(hist2_id, 'hetero_resp', 'Heterotrophic respiration', 'gC/m^2/s', &
4151                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(2), dt,dw2)
4152             CALL histdef(hist2_id, 'growth_resp', 'Growth respiration', 'gC/m^2/s', &
4153                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(2), dt, dw2)
4154             CALL histdef(hist2_id, 'npp', 'Net Primary Production', 'gC/m^2/s', &
4155                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(2), dt, dw2)
4156          ENDIF
4157          CALL histdef(hist2_id, 'precisol', 'Throughfall', 'mm/d',  &
4158               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop2(6), dt, dw2)
4159          CALL histdef(hist2_id, 'drysoil_frac', 'Fraction of visibly dry soil', '1',  &
4160               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(6), dt, dw2)
4161          CALL histdef(hist2_id, 'evapot', 'Potential evaporation', 'mm/d',  &
4162               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(6), dt, dw2)
4163          CALL histdef(hist2_id, 'evapot_corr', 'Potential evaporation', 'mm/d',  &
4164               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(6), dt, dw2)
4165          !-
4166          !- SECHIBA_HISTLEVEL2 = 7
4167          !-
4168          CALL histdef(hist2_id, 'swnet', 'Net solar radiation', 'W/m^2',  &
4169               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(7), dt, dw2)
4170          CALL histdef(hist2_id, 'swdown', 'Incident solar radiation', 'W/m^2',  &
4171               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(7), dt, dw2)
4172          CALL histdef(hist2_id, 'lwdown', 'Absorbed downward longwave radiation', 'W/m^2',  &
4173               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(7), dt, dw2)
4174          CALL histdef(hist2_id, 'lwnet', 'Net surface longwave radiation', 'W/m^2',  &
4175               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(7), dt, dw2)
4176          CALL histdef(hist2_id, 'temp_pheno', 'Temperature for Pheno', 'K',  &
4177               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(7), dt, dw2)
4178          !-
4179          !- SECHIBA_HISTLEVEL2 = 8
4180          !-
4181          IF ( control_flags%river_routing ) THEN
4182             CALL histdef(hist2_id, 'fastr', 'Fast flow reservoir', 'kg/m^2', &
4183                  & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(8), dt, dw2)
4184             CALL histdef(hist2_id, 'slowr', 'Slow flow reservoir', 'kg/m^2', &
4185                  & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(8), dt, dw2)
4186             CALL histdef(hist2_id, 'streamr', 'Stream flow reservoir', 'kg/m^2', &
4187                  & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(8), dt, dw2)
4188             CALL histdef(hist2_id, 'lakevol', 'Volume in lake reservoir', 'kg/m^2', &
4189                  & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(8), dt, dw2)
4190             CALL histdef(hist2_id, 'irrigation', 'Net irrigation', 'mm/d', &
4191                  & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(8), dt, dw2)
4192             CALL histdef(hist2_id, 'netirrig', 'Net irrigation requirement', 'mm/d', &
4193                  & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop2(8), dt, dw2)
4194             CALL histdef(hist2_id, 'irrigmap', 'Map of irrigated areas', 'm^2', &
4195                  & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(8), dt, dw2)
4196          ENDIF
4197          !-
4198          !- SECHIBA_HISTLEVEL2 = 9
4199          !-
4200          CALL histdef(hist2_id, 'beta', 'Beta Function', '1',  &
4201               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4202          CALL histdef(hist2_id, 'raero', 'Aerodynamic resistance', 's/m',  &
4203               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4204          ! Ajouts Nathalie - Novembre 2006
4205          CALL histdef(hist2_id, 'Wind', 'Wind speed', 'm/s',  &
4206               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4207          ! Fin ajouts Nathalie
4208          CALL histdef(hist2_id, 'qsatt' , 'Surface saturated humidity', 'g/g', &
4209               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4210          CALL histdef(hist2_id, 'vbeta1', 'Beta for sublimation', '1',  &
4211               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4212          CALL histdef(hist2_id, 'vbeta4', 'Beta for bare soil', '1',  &
4213               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(9), dt, dw2)
4214          CALL histdef(hist2_id, 'vbetaco2', 'beta for CO2', 'mm/d', &
4215               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(9), dt, dw2)
4216          CALL histdef(hist2_id, 'soiltype', 'Fraction of soil textures', '%', &
4217               & iim,jjm, hori_id2, nstm, 1, nstm, soltax_id2, 32, once2(9),  dt, dw2)
4218          CALL histdef(hist2_id, 'humrel', 'Soil moisture stress', '1',  &
4219               & iim,jjm, hori_id2, nvm,1,nvm, vegax_id2, 32, avescatter2(9), dt, dw2)
4220          !-
4221          !- SECHIBA_HISTLEVEL2 = 10
4222          !-
4223          CALL histdef(hist2_id, 'cimean', 'Stomatal CO2 concentation', 'mmole/m2/s', &
4224               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(10), dt, dw2)
4225          CALL histdef(hist2_id, 'vbeta3', 'Beta for Transpiration', 'mm/d', &
4226               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(10), dt, dw2)
4227          CALL histdef(hist2_id, 'rveget', 'Canopy resistance', 's/m', &
4228               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(10), dt, dw2)
4229          CALL histdef(hist2_id, 'rsol', 'Soil resistance', 's/m',  &
4230               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(10), dt, dw2)
4231          CALL histdef(hist2_id,'vbeta2','Beta for Interception loss','mm/d', &
4232               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(10), dt, dw2)
4233          CALL histdef(hist2_id, 'qsintmax', 'Maximum Interception capacity', 'Kg/m^2', &
4234               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(10), dt, dw2)
4235          !
4236       ELSE 
4237          !-
4238          !- This is the ALMA convention output now
4239          !-
4240          !-
4241          IF ( rectilinear ) THEN
4242#ifdef CPP_PARA
4243             CALL histbeg(histname2, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
4244                  &     istp_old, date0, dt, hori_id2, hist2_id,orch_domain_id)
4245#else
4246             CALL histbeg(histname2, iim, lon_rect, jjm, lat_rect, 1, iim, 1, jjm, &
4247                  &     istp_old, date0, dt, hori_id2, hist2_id)
4248#endif
4249             WRITE(numout,*)  'HISTBEG2 --->',istp_old,date0,dt,dw2,hist2_id
4250          ELSE
4251#ifdef CPP_PARA
4252             CALL histbeg(histname2, iim, lon, jjm, lat, 1, iim, 1, jjm, &
4253                  &     istp_old, date0, dt, hori_id2, hist2_id,domain_id=orch_domain_id)
4254#else
4255             CALL histbeg(histname2, iim, lon, jjm, lat, 1, iim, 1, jjm, &
4256                  &     istp_old, date0, dt, hori_id2, hist2_id)
4257#endif
4258          ENDIF
4259          !-
4260          CALL histvert(hist2_id, 'veget', 'Vegetation types', '1', &
4261               &    nvm,   veg, vegax_id2)
4262          CALL histvert(hist2_id, 'solth', 'Soil levels',      'm', &
4263               &    ngrnd, sol, solax_id2)
4264          CALL histvert(hist2_id, 'soiltyp', 'Soil types',      '1', &
4265               &    nstm, soltyp, soltax_id2)
4266          CALL histvert(hist2_id, 'nobio', 'Other surface types',      '1', &
4267               &    nnobio, nobiotyp, nobioax_id2)
4268          IF (  control_flags%hydrol_cwrr ) THEN
4269             CALL histvert(hist2_id, 'solay', 'Hydrol soil levels',      'm', &
4270                  &    nslm, solay, solayax_id2)
4271          ENDIF
4272          !-
4273          !-  Vegetation
4274          !-
4275          CALL histdef(hist2_id, 'vegetfrac', 'Fraction of vegetation', '1', &
4276               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(3), dt, dw2)
4277          CALL histdef(hist2_id, 'maxvegetfrac', 'Maximum fraction of vegetation', '1', &
4278               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, avescatter2(3), dt, dw2)
4279          CALL histdef(hist2_id, 'nobiofrac', 'Fraction of other surface types', '1', &
4280               & iim,jjm, hori_id2, nnobio, 1, nnobio, nobioax_id2, 32, avescatter2(3), dt, dw2)
4281          !-
4282          !-  General energy balance
4283          !-
4284          CALL histdef(hist2_id, 'SWnet', 'Net shortwave radiation', 'W/m^2',  &
4285               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(1), dt, dw2)
4286          CALL histdef(hist2_id, 'LWnet', 'Net longwave radiation', 'W/m^2',  &
4287               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4288          CALL histdef(hist2_id, 'Qh', 'Sensible heat flux', 'W/m^2',  &
4289               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(1), dt, dw2)
4290          CALL histdef(hist2_id, 'Qle', 'Latent heat flux', 'W/m^2',  &
4291               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(1), dt, dw2)
4292          CALL histdef(hist2_id, 'Qg', 'Ground heat flux', 'W/m^2',  &
4293               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4294          CALL histdef(hist2_id, 'Qf', 'Energy of fusion', 'W/m^2',  &
4295               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(2), dt, dw2)
4296          CALL histdef(hist2_id, 'Qv', 'Energy of sublimation', 'W/m^2',  &
4297               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4298          CALL histdef(hist2_id, 'DelSurfHeat', 'Change in surface layer heat', 'J/m^2',  &
4299               & iim,jjm, hori_id2, 1,1,1, -99, 32, sumscatter2(1), dt, dw2)
4300          CALL histdef(hist2_id, 'DelColdCont', 'Change in snow surface layer cold content', 'J/m^2',  &
4301               & iim,jjm, hori_id2, 1,1,1, -99, 32, sumscatter2(1), dt, dw2)
4302          !-
4303          !- General water balance
4304          !-
4305          CALL histdef(hist2_id, 'Snowf', 'Snowfall rate', 'kg/m^2/s',  &
4306               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4307          CALL histdef(hist2_id, 'Rainf', 'Rainfall rate', 'kg/m^2/s',  &
4308               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4309          CALL histdef(hist2_id, 'Evap', 'Total Evapotranspiration', 'kg/m^2/s', &
4310               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4311          CALL histdef(hist2_id, 'Qs', 'Surface runoff', 'kg/m^2/s', &
4312               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4313          CALL histdef(hist2_id, 'Qsb', 'Sub-surface runoff', 'kg/m^2/s', &
4314               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4315          CALL histdef(hist2_id, 'Qsm', 'Snowmelt', 'kg/m^2/s', &
4316               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4317          CALL histdef(hist2_id, 'DelSoilMoist', 'Change in soil moisture', 'kg/m^2',  &
4318               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, sumscatter2(1), dt, dw2)
4319          CALL histdef(hist2_id, 'DelSWE', 'Change in SWE','kg/m^2',&
4320               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, sumscatter2(1), dt, dw2)
4321          CALL histdef(hist2_id, 'DelIntercept', 'Change in interception storage', 'kg/m^2',  &
4322               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, sumscatter2(1), dt, dw2)
4323          !-
4324          !- Surface state
4325          !-
4326          CALL histdef(hist2_id, 'AvgSurfT', 'Average surface temperature', 'K', &
4327               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(1), dt, dw2)
4328          CALL histdef(hist2_id, 'RadT', 'Surface radiative temperature', 'K', &
4329               & iim,jjm, hori_id2, 1,1,1, -99, 32, ave2(1), dt, dw2)
4330          CALL histdef(hist2_id, 'Albedo', 'Albedo', '1', &
4331               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4332          CALL histdef(hist2_id, 'SWE', '3D soil water equivalent','kg/m^2',  &
4333               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4334          !!-
4335          !-  Sub-surface state
4336          !-
4337          IF ( .NOT. control_flags%hydrol_cwrr ) THEN
4338             CALL histdef(hist2_id, 'SoilMoist', '3D average layer soil moisture', 'kg/m^2',  &
4339                  & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(1), dt, dw2)
4340          ELSE
4341             CALL histdef(hist2_id, 'SoilMoist', '3D average layer soil moisture', 'kg/m^2',  &
4342                  & iim,jjm, hori_id2, nslm, 1, nslm, solayax_id2, 32, avescatter2(1), dt, dw2)
4343          ENDIF
4344          CALL histdef(hist2_id, 'SoilWet', 'Total soil wetness', 'kg/m^2',  &
4345               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(1), dt, dw2)
4346          CALL histdef(hist2_id, 'SoilTemp', '3D layer average soil temperature', 'K', &
4347               & iim,jjm, hori_id2, ngrnd, 1, ngrnd, solax_id2, 32, avescatter2(1), dt, dw2)
4348          !-
4349          !-  Evaporation components
4350          !-
4351          CALL histdef(hist2_id, 'PotEvap', 'Potential evapotranspiration', 'kg/m^2/s', &
4352               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4353          CALL histdef(hist2_id, 'ECanop', 'Interception evaporation', 'kg/m^2/s', &
4354               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt, dw2)
4355          CALL histdef(hist2_id, 'TVeg', 'Transpiration', 'kg/m^2/s', &
4356               & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt, dw2)
4357          CALL histdef(hist2_id, 'ESoil', 'Bare soil evaporation', 'kg/m^2/s', &
4358               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4359          CALL histdef(hist2_id, 'RootMoist','Root zone soil water', 'kg/m^2',  &
4360               & iim,jjm, hori_id2, 1, 1, 1, -99, 32, avescatter2(1), dt, dw2)
4361          CALL histdef(hist2_id, 'SubSnow','Snow sublimation','kg/m^2/s', &
4362               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(1), dt, dw2)
4363          CALL histdef(hist2_id, 'ACond', 'Aerodynamic conductance', 'm/s',  &
4364               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4365          !-
4366          !-
4367          !-  Cold Season Processes
4368          !-
4369          CALL histdef(hist2_id, 'SnowFrac', 'Snow cover fraction', '1',  &
4370               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4371          CALL histdef(hist2_id, 'SAlbedo', 'Snow albedo', '1', &
4372               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4373          CALL histdef(hist2_id, 'SnowDepth', '3D snow depth', 'm', &
4374               & iim,jjm, hori_id2, 1,1,1, -99, 32, avescatter2(1), dt, dw2)
4375          !-
4376          !- Hydrologic variables
4377          !-
4378          CALL histdef(hist2_id, 'IrrigationMap', 'Map of irrigated areas', 'm^2', &
4379               & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(7), dt, dw2)
4380          CALL histdef(hist2_id, 'dis', 'Simulated River Discharge', 'm^3/s', &
4381               & iim,jjm, hori_id2, 1,1,1, -99, 32, fluxop_scinsec2(2), dt, dw2)
4382          CALL histdef(hist2_id, 'humrel', 'Soil moisture stress', '1',  &
4383               & iim,jjm, hori_id2, nvm,1,nvm, vegax_id2, 32, avescatter2(9), dt, dw2)
4384          !-
4385          !-  The carbon budget
4386          !-
4387          IF ( control_flags%ok_co2 ) THEN
4388             CALL histdef(hist2_id, 'GPP', 'Net assimilation of carbon by the vegetation', 'gC/m^2/s', &
4389                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt, dw2)
4390          ENDIF
4391          IF ( control_flags%ok_stomate ) THEN
4392             CALL histdef(hist2_id, 'NEE', 'Net Ecosystem Exchange', 'gC/m^2/s', &
4393                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt,dw2)
4394             CALL histdef(hist2_id, 'maint_resp', 'Maintenance respiration', 'gC/m^2/s', &
4395                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt,dw2)
4396             CALL histdef(hist2_id, 'hetero_resp', 'Heterotrophic respiration', 'gC/m^2/s', &
4397                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt,dw2)
4398             CALL histdef(hist2_id, 'growth_resp', 'Growth respiration', 'gC/m^2/s', &
4399                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt, dw2)
4400             CALL histdef(hist2_id, 'npp', 'Net Primary Production', 'gC/m^2/s', &
4401                  & iim,jjm, hori_id2, nvm, 1, nvm, vegax_id2, 32, fluxop_scinsec2(1), dt, dw2)
4402          ENDIF
4403          !
4404       ENDIF
4405       !-
4406       CALL histdef(hist2_id, 'LandPoints', 'Land Points', '1', &
4407            & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(1), dt, dw2) 
4408       CALL histdef(hist2_id, 'Areas', 'Mesh areas', 'm2', &
4409            & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(1), dt, dw2)
4410       CALL histdef(hist2_id, 'Contfrac', 'Continental fraction', '1', &
4411            & iim,jjm, hori_id2, 1,1,1, -99, 32, once2(1), dt, dw2)
4412       !-
4413       CALL histend(hist2_id)
4414    ENDIF
4415    !-
4416    !=====================================================================
4417    !- 3.2 STOMATE's history file
4418    !=====================================================================
4419    IF ( control_flags%ok_stomate ) THEN
4420       !-
4421       ! STOMATE IS ACTIVATED
4422       !-
4423       !Config  Key  = STOMATE_OUTPUT_FILE
4424       !Config  Desc = Name of file in which STOMATE's output is going
4425       !Config         to be written
4426       !Config  Def  = stomate_history.nc
4427       !Config  Help = This file is going to be created by the model
4428       !Config         and will contain the output from the model.
4429       !Config         This file is a truly COADS compliant netCDF file.
4430       !Config         It will be generated by the hist software from
4431       !Config         the IOIPSL package.
4432       !-
4433       stom_histname='stomate_history.nc'
4434       CALL getin_p('STOMATE_OUTPUT_FILE', stom_histname)       
4435       WRITE(numout,*) 'STOMATE_OUTPUT_FILE', TRIM(stom_histname)
4436       !-
4437       !Config  Key  = STOMATE_HIST_DT
4438       !Config  Desc = STOMATE history time step (d)
4439       !Config  Def  = 10.
4440       !Config  Help = Time step of the STOMATE history file
4441       !-
4442       hist_days_stom = 10.
4443       CALL getin_p('STOMATE_HIST_DT', hist_days_stom)       
4444       IF ( hist_days_stom == moins_un ) THEN
4445          hist_dt_stom = moins_un
4446          WRITE(numout,*) 'output frequency for STOMATE history file (d): one month.'
4447       ELSE
4448          hist_dt_stom = NINT( hist_days_stom ) * one_day
4449          WRITE(numout,*) 'output frequency for STOMATE history file (d): ', &
4450               hist_dt_stom/one_day
4451       ENDIF
4452
4453       ! test consistency between STOMATE_HIST_DT and DT_SLOW parameters
4454       dt_slow_ = one_day
4455       CALL getin_p('DT_SLOW', dt_slow_)
4456       IF ( hist_days_stom /= moins_un ) THEN
4457          IF (dt_slow_ > hist_dt_stom) THEN
4458             WRITE(numout,*) "DT_SLOW = ",dt_slow_,"  , STOMATE_HIST_DT = ",hist_dt_stom
4459             CALL ipslerr (3,'intsurf_history', &
4460                  &          'Problem with DT_SLOW > STOMATE_HIST_DT','', &
4461                  &          '(must be less or equal)')
4462          ENDIF
4463       ENDIF
4464       !-
4465       !- initialize
4466       IF ( rectilinear ) THEN
4467#ifdef CPP_PARA
4468          CALL histbeg(stom_histname, iim, lon_rect, jjm, lat_rect,  1, iim, 1, jjm, &
4469               &     istp_old, date0, dt, hori_id, hist_id_stom,domain_id=orch_domain_id)
4470#else
4471          CALL histbeg(stom_histname, iim, lon_rect, jjm, lat_rect,  1, iim, 1, jjm, &
4472               &     istp_old, date0, dt, hori_id, hist_id_stom)
4473#endif
4474       ELSE
4475#ifdef CPP_PARA
4476          CALL histbeg(stom_histname, iim, lon, jjm, lat,  1, iim, 1, jjm, &
4477               &     istp_old, date0, dt, hori_id, hist_id_stom,domain_id=orch_domain_id)
4478#else
4479          CALL histbeg(stom_histname, iim, lon, jjm, lat,  1, iim, 1, jjm, &
4480               &     istp_old, date0, dt, hori_id, hist_id_stom)
4481#endif
4482       ENDIF
4483       !- define PFT axis
4484       hist_PFTaxis = (/ ( REAL(i,r_std), i=1,nvm ) /)
4485       !- declare this axis
4486       CALL histvert (hist_id_stom, 'PFT', 'Plant functional type', &
4487            & '1', nvm, hist_PFTaxis, hist_PFTaxis_id)
4488! deforestation
4489       !- define Pool_10 axis
4490       hist_pool_10axis = (/ ( REAL(i,r_std), i=1,10 ) /)
4491       !- declare this axis
4492       CALL histvert (hist_id_stom, 'P10', 'Pool 10 years', &
4493            & '1', 10, hist_pool_10axis, hist_pool_10axis_id)
4494
4495       !- define Pool_100 axis
4496       hist_pool_100axis = (/ ( REAL(i,r_std), i=1,100 ) /)
4497       !- declare this axis
4498       CALL histvert (hist_id_stom, 'P100', 'Pool 100 years', &
4499            & '1', 100, hist_pool_100axis, hist_pool_100axis_id)
4500
4501       !- define Pool_11 axis
4502       hist_pool_11axis = (/ ( REAL(i,r_std), i=1,11 ) /)
4503       !- declare this axis
4504       CALL histvert (hist_id_stom, 'P11', 'Pool 10 years + 1', &
4505            & '1', 11, hist_pool_11axis, hist_pool_11axis_id)
4506
4507       !- define Pool_101 axis
4508       hist_pool_101axis = (/ ( REAL(i,r_std), i=1,101 ) /)
4509       !- declare this axis
4510       CALL histvert (hist_id_stom, 'P101', 'Pool 100 years + 1', &
4511            & '1', 101, hist_pool_101axis, hist_pool_101axis_id)
4512
4513       !- define STOMATE history file
4514       CALL stom_define_history (hist_id_stom, nvm, iim, jjm, &
4515            & dt, hist_dt_stom, hori_id, hist_PFTaxis_id, &
4516            & hist_pool_10axis_id, hist_pool_100axis_id, &
4517            & hist_pool_11axis_id, hist_pool_101axis_id)
4518
4519       !- end definition
4520       CALL histend(hist_id_stom)
4521       !-
4522       !-
4523       !-
4524       ! STOMATE IPCC OUTPUTS IS ACTIVATED
4525       !-
4526       !Config  Key  = STOMATE_IPCC_OUTPUT_FILE
4527       !Config  Desc = Name of file in which STOMATE's output is going
4528       !Config         to be written
4529       !Config  Def  = stomate_ipcc_history.nc
4530       !Config  Help = This file is going to be created by the model
4531       !Config         and will contain the output from the model.
4532       !Config         This file is a truly COADS compliant netCDF file.
4533       !Config         It will be generated by the hist software from
4534       !Config         the IOIPSL package.
4535       !-
4536       stom_ipcc_histname='stomate_ipcc_history.nc'
4537       CALL getin_p('STOMATE_IPCC_OUTPUT_FILE', stom_ipcc_histname)       
4538       WRITE(numout,*) 'STOMATE_IPCC_OUTPUT_FILE', TRIM(stom_ipcc_histname)
4539       !-
4540       !Config  Key  = STOMATE_IPCC_HIST_DT
4541       !Config  Desc = STOMATE IPCC history time step (d)
4542       !Config  Def  = 0.
4543       !Config  Help = Time step of the STOMATE IPCC history file
4544       !-
4545       hist_days_stom_ipcc = zero
4546       CALL getin_p('STOMATE_IPCC_HIST_DT', hist_days_stom_ipcc)       
4547       IF ( hist_days_stom_ipcc == moins_un ) THEN
4548          hist_dt_stom_ipcc = moins_un
4549          WRITE(numout,*) 'output frequency for STOMATE IPCC history file (d): one month.'
4550       ELSE
4551          hist_dt_stom_ipcc = NINT( hist_days_stom_ipcc ) * one_day
4552          WRITE(numout,*) 'output frequency for STOMATE IPCC history file (d): ', &
4553            hist_dt_stom_ipcc/one_day
4554       ENDIF
4555
4556       ! test consistency between STOMATE_IPCC_HIST_DT and DT_SLOW parameters
4557       dt_slow_ = one_day
4558       CALL getin_p('DT_SLOW', dt_slow_)
4559       IF ( hist_days_stom_ipcc > zero ) THEN
4560          IF (dt_slow_ > hist_dt_stom_ipcc) THEN
4561             WRITE(numout,*) "DT_SLOW = ",dt_slow_,"  , STOMATE_IPCC_HIST_DT = ",hist_dt_stom_ipcc
4562             CALL ipslerr (3,'intsurf_history', &
4563                  &          'Problem with DT_SLOW > STOMATE_IPCC_HIST_DT','', &
4564                  &          '(must be less or equal)')
4565          ENDIF
4566       ENDIF
4567
4568       IF ( hist_dt_stom_ipcc == 0 ) THEN
4569          hist_id_stom_ipcc = -1
4570       ELSE
4571          !-
4572          !- initialize
4573          IF ( rectilinear ) THEN
4574#ifdef CPP_PARA
4575             CALL histbeg(stom_ipcc_histname, iim, lon_rect, jjm, lat_rect,  1, iim, 1, jjm, &
4576                  &     istp_old, date0, dt, hori_id, hist_id_stom_ipcc,domain_id=orch_domain_id)
4577#else
4578             CALL histbeg(stom_ipcc_histname, iim, lon_rect, jjm, lat_rect,  1, iim, 1, jjm, &
4579                  &     istp_old, date0, dt, hori_id, hist_id_stom_ipcc)
4580#endif
4581          ELSE
4582#ifdef CPP_PARA
4583             CALL histbeg(stom_ipcc_histname, iim, lon, jjm, lat,  1, iim, 1, jjm, &
4584                  &     istp_old, date0, dt, hori_id, hist_id_stom_ipcc,domain_id=orch_domain_id)
4585#else
4586             CALL histbeg(stom_ipcc_histname, iim, lon, jjm, lat,  1, iim, 1, jjm, &
4587                  &     istp_old, date0, dt, hori_id, hist_id_stom_ipcc)
4588#endif
4589          ENDIF
4590          !- declare this axis
4591          CALL histvert (hist_id_stom_IPCC, 'PFT', 'Plant functional type', &
4592               & '1', nvm, hist_PFTaxis, hist_IPCC_PFTaxis_id)
4593
4594          !- define STOMATE history file
4595          CALL stom_IPCC_define_history (hist_id_stom_IPCC, nvm, iim, jjm, &
4596               & dt, hist_dt_stom_ipcc, hori_id, hist_IPCC_PFTaxis_id)
4597
4598          !- end definition
4599          CALL histend(hist_id_stom_IPCC)
4600         
4601       ENDIF
4602    ENDIF
4603
4604
4605    RETURN
4606
4607  END SUBROUTINE intsurf_history
4608 
4609  SUBROUTINE stom_define_history &
4610       & (hist_id_stom, nvm, iim, jjm, dt, &
4611       &  hist_dt, hist_hori_id, hist_PFTaxis_id, &
4612       & hist_pool_10axis_id, hist_pool_100axis_id, &
4613       & hist_pool_11axis_id, hist_pool_101axis_id)
4614    ! deforestation axis added as arguments
4615
4616    !---------------------------------------------------------------------
4617    !- Tell ioipsl which variables are to be written
4618    !- and on which grid they are defined
4619    !---------------------------------------------------------------------
4620    IMPLICIT NONE
4621    !-
4622    !- Input
4623    !-
4624    !- File id
4625    INTEGER(i_std),INTENT(in) :: hist_id_stom
4626    !- number of PFTs
4627    INTEGER(i_std),INTENT(in) :: nvm
4628    !- Domain size
4629    INTEGER(i_std),INTENT(in) :: iim, jjm
4630    !- Time step of STOMATE (seconds)
4631    REAL(r_std),INTENT(in)    :: dt
4632    !- Time step of history file (s)
4633    REAL(r_std),INTENT(in)    :: hist_dt
4634    !- id horizontal grid
4635    INTEGER(i_std),INTENT(in) :: hist_hori_id
4636    !- id of PFT axis
4637    INTEGER(i_std),INTENT(in) :: hist_PFTaxis_id
4638    !- id of Deforestation axis
4639    INTEGER(i_std),INTENT(in) :: hist_pool_10axis_id,hist_pool_100axis_id
4640    INTEGER(i_std),INTENT(in) :: hist_pool_11axis_id,hist_pool_101axis_id
4641    !-
4642    !- 1 local
4643    !-
4644    !- maximum history level
4645    INTEGER(i_std), PARAMETER  :: max_hist_level = 10
4646    !- output level (between 0 and 10)
4647    !-  ( 0:nothing is written, 10:everything is written)
4648    INTEGER(i_std)             :: hist_level
4649    !- Character strings to define operations for histdef
4650    CHARACTER(LEN=40),DIMENSION(max_hist_level) :: ave
4651
4652    !---------------------------------------------------------------------
4653    !=====================================================================
4654    !- 1 history level
4655    !=====================================================================
4656    !- 1.1 define history levelx
4657    !=====================================================================
4658    !Config  Key  = STOMATE_HISTLEVEL
4659    !Config  Desc = STOMATE history output level (0..10)
4660    !Config  Def  = 10
4661    !Config  Help = 0: nothing is written; 10: everything is written
4662    !-
4663    hist_level = 10
4664    CALL getin_p('STOMATE_HISTLEVEL', hist_level)
4665    !-
4666    WRITE(numout,*) 'STOMATE history level: ',hist_level
4667    IF ( (hist_level > max_hist_level).OR.(hist_level < 0) ) THEN
4668       STOP 'This history level is not allowed'
4669    ENDIF
4670    !=====================================================================
4671    !- 1.2 define operations according to output level
4672    !=====================================================================
4673    ave(1:hist_level) =  'ave(scatter(X))'
4674    ave(hist_level+1:max_hist_level) =  'never          '
4675    !=====================================================================
4676    !- 2 surface fields (2d)
4677    !- 3 PFT: 3rd dimension
4678    !=====================================================================
4679
4680
4681    ! structural litter above ground
4682    CALL histdef (hist_id_stom, &
4683         &               TRIM("LITTER_STR_AB       "), &
4684         &               TRIM("structural litter above ground                    "), &
4685         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4686         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4687
4688    ! metabolic litter above ground                     
4689    CALL histdef (hist_id_stom, &
4690         &               TRIM("LITTER_MET_AB       "), &
4691         &               TRIM("metabolic litter above ground                     "), &
4692         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4693         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4694
4695    ! structural litter below ground               
4696    CALL histdef (hist_id_stom, &
4697         &               TRIM("LITTER_STR_BE       "), &
4698         &               TRIM("structural litter below ground                    "), &
4699         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4700         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4701
4702    ! metabolic litter below ground               
4703    CALL histdef (hist_id_stom, &
4704         &               TRIM("LITTER_MET_BE       "), &
4705         &               TRIM("metabolic litter below ground                     "), &
4706         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4707         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4708
4709    ! fraction of soil covered by dead leaves           
4710    CALL histdef (hist_id_stom, &
4711         &               TRIM("DEADLEAF_COVER      "), &
4712         &               TRIM("fraction of soil covered by dead leaves           "), &
4713         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4714         &               1,1,1, -99,32, ave(5), dt, hist_dt)
4715
4716    ! total soil and litter carbon
4717    CALL histdef (hist_id_stom, &
4718         &               TRIM("TOTAL_SOIL_CARB     "), &
4719         &               TRIM("total soil and litter carbon                      "), &
4720         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4721         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4722
4723    ! active soil carbon in ground                 
4724    CALL histdef (hist_id_stom, &
4725         &               TRIM("CARBON_ACTIVE       "), &
4726         &               TRIM("active soil carbon in ground                      "), &
4727         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4728         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4729
4730    ! slow soil carbon in ground                   
4731    CALL histdef (hist_id_stom, &
4732         &               TRIM("CARBON_SLOW         "), &
4733         &               TRIM("slow soil carbon in ground                        "), &
4734         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4735         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4736
4737    ! passive soil carbon in ground               
4738    CALL histdef (hist_id_stom, &
4739         &               TRIM("CARBON_PASSIVE      "), &
4740         &               TRIM("passive soil carbon in ground                     "), &
4741         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4742         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
4743
4744    ! Long term 2 m temperature                           
4745    CALL histdef (hist_id_stom, &
4746         &               TRIM("T2M_LONGTERM        "), &
4747         &               TRIM("Longterm 2 m temperature                          "), &
4748         &               TRIM("K                   "), iim,jjm, hist_hori_id, &
4749         &               1,1,1, -99,32, ave(9), dt, hist_dt)
4750
4751    ! Monthly 2 m temperature                           
4752    CALL histdef (hist_id_stom, &
4753         &               TRIM("T2M_MONTH           "), &
4754         &               TRIM("Monthly 2 m temperature                           "), &
4755         &               TRIM("K                   "), iim,jjm, hist_hori_id, &
4756         &               1,1,1, -99,32, ave(1), dt, hist_dt)
4757
4758    ! Weekly 2 m temperature                           
4759    CALL histdef (hist_id_stom, &
4760         &               TRIM("T2M_WEEK            "), &
4761         &               TRIM("Weekly 2 m temperature                            "), &
4762         &               TRIM("K                   "), iim,jjm, hist_hori_id, &
4763         &               1,1,1, -99,32, ave(1), dt, hist_dt)
4764
4765    ! heterotr. resp. from ground                 
4766    CALL histdef (hist_id_stom, &
4767         &               TRIM("HET_RESP            "), &
4768         &               TRIM("heterotr. resp. from ground                       "), &
4769         &               TRIM("gC/m^2 tot/pft/day  "), iim,jjm, hist_hori_id, &
4770         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4771
4772    ! black carbon on average total ground             
4773    CALL histdef (hist_id_stom, &
4774         &               TRIM("BLACK_CARBON        "), &
4775         &               TRIM("black carbon on average total ground              "), &
4776         &               TRIM("gC/m^2 tot          "), iim,jjm, hist_hori_id, &
4777         &               1,1,1, -99,32, ave(10), dt, hist_dt)
4778
4779    ! Fire fraction on ground
4780    CALL histdef (hist_id_stom, &
4781         &               TRIM("FIREFRAC            "), &
4782         &               TRIM("Fire fraction on ground                           "), &
4783         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
4784         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4785
4786    ! Fire index on ground                     
4787    CALL histdef (hist_id_stom, &
4788         &               TRIM("FIREINDEX           "), &
4789         &               TRIM("Fire index on ground                              "), &
4790         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4791         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(10), dt, hist_dt)
4792
4793    ! Litter humidity                                   
4794    CALL histdef (hist_id_stom, &
4795         &               TRIM("LITTERHUM           "), &
4796         &               TRIM("Litter humidity                                   "), &
4797         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4798         &               1,1,1, -99,32, ave(5), dt, hist_dt)
4799
4800    ! CO2 flux                                 
4801    CALL histdef (hist_id_stom, &
4802         &               TRIM("CO2FLUX             "), &
4803         &               TRIM("CO2 flux                                          "), &
4804         &               TRIM("gC/m^2/pft/mth      "), iim,jjm, hist_hori_id, &
4805         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4806
4807!!$    CALL histdef(hist_id_stom, &
4808!!$         &               TRIM("CO2FLUX_MONTHLY_SUM "), &
4809!!$         &               TRIM("Monthly CO2 flux Sum                              "), &
4810!!$         &               TRIM("PgC/m^2/mth         "), iim,jjm, hist_hori_id, &
4811!!$         &               1,1,1, -99, 32, 'inst(scatter(X))', dt, hist_dt)
4812
4813    ! Output CO2 flux from fire                         
4814    CALL histdef (hist_id_stom, &
4815         &               TRIM("CO2_FIRE            "), &
4816         &               TRIM("Output CO2 flux from fire                         "), &
4817         &               TRIM("gC/day/m^2/pft      "), iim,jjm, hist_hori_id, &
4818         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4819
4820    ! CO2 taken from atmosphere for initiate growth     
4821    CALL histdef (hist_id_stom, &
4822         &               TRIM("CO2_TAKEN           "), &
4823         &               TRIM("CO2 taken from atmosphere for initiate growth     "), &
4824         &               TRIM("gC/day/m^2/pft      "), iim,jjm, hist_hori_id, &
4825         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4826
4827    ! Leaf Area Index                                   
4828    CALL histdef (hist_id_stom, &
4829         &               TRIM("LAI                 "), &
4830         &               TRIM("Leaf Area Index                                   "), &
4831         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4832         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4833
4834    ! Vegetation fraction                               
4835    CALL histdef (hist_id_stom, &
4836         &               TRIM("VEGET               "), &
4837         &               TRIM("Vegetation fraction                               "), &
4838         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4839         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4840
4841    ! Maximum vegetation fraction (LAI -> infinity)     
4842    CALL histdef (hist_id_stom, &
4843         &               TRIM("VEGET_MAX           "), &
4844         &               TRIM("Maximum vegetation fraction (LAI -> infinity)     "), &
4845         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4846         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4847
4848    ! Net primary productivity                         
4849    CALL histdef (hist_id_stom, &
4850         &               TRIM("NPP                 "), &
4851         &               TRIM("Net primary productivity                          "), &
4852         &               TRIM("gC/day/(m^2 tot)    "), iim,jjm, hist_hori_id, &
4853         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4854
4855    ! Gross primary productivity                       
4856    CALL histdef (hist_id_stom, &
4857         &               TRIM("GPP                 "), &
4858         &               TRIM("Gross primary productivity                        "), &
4859         &               TRIM("gC/day/m^2          "), iim,jjm, hist_hori_id, &
4860         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
4861
4862    ! Density of individuals                           
4863    CALL histdef (hist_id_stom, &
4864         &               TRIM("IND                 "), &
4865         &               TRIM("Density of individuals                            "), &
4866         &               TRIM("1/ m^2              "), iim,jjm, hist_hori_id, &
4867         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4868
4869    ! Adaptation to climate
4870    CALL histdef (hist_id_stom, &
4871         &               TRIM("ADAPTATION          "), &
4872         &               TRIM("Adaptation to climate (DGVM)                      "), &
4873         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4874         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4875   
4876    ! Probability from regenerative
4877    CALL histdef (hist_id_stom, &
4878         &               TRIM("REGENERATION        "), &
4879         &               TRIM("Probability from regenerative (DGVM)               "), &
4880         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
4881         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4882
4883    ! crown area of individuals (m**2)
4884    CALL histdef (hist_id_stom, &
4885         &               TRIM("CN_IND              "), &
4886         &               TRIM("crown area of individuals                         "), &
4887         &               TRIM("m^2                 "), iim,jjm, hist_hori_id, &
4888         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4889
4890    ! woodmass of individuals (gC)
4891    CALL histdef (hist_id_stom, &
4892         &               TRIM("WOODMASS_IND        "), &
4893         &               TRIM("Woodmass of individuals                           "), &
4894         &               TRIM("gC/pft              "), iim,jjm, hist_hori_id, &
4895         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
4896
4897    ! total living biomass
4898    CALL histdef (hist_id_stom, &
4899         &               TRIM("TOTAL_M             "), &
4900         &               TRIM("Total living biomass                              "), &
4901         &               TRIM("gC/m^2/pft          "), iim,jjm, hist_hori_id, &
4902         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4903
4904    ! Leaf mass                                         
4905    CALL histdef (hist_id_stom, &
4906         &               TRIM("LEAF_M              "), &
4907         &               TRIM("Leaf mass                                         "), &
4908         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4909         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4910
4911    ! Sap mass above ground                             
4912    CALL histdef (hist_id_stom, &
4913         &               TRIM("SAP_M_AB            "), &
4914         &               TRIM("Sap mass above ground                             "), &
4915         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4916         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4917
4918    ! Sap mass below ground                             
4919    CALL histdef (hist_id_stom, &
4920         &               TRIM("SAP_M_BE            "), &
4921         &               TRIM("Sap mass below ground                             "), &
4922         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4923         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4924
4925    ! Heartwood mass above ground                       
4926    CALL histdef (hist_id_stom, &
4927         &               TRIM("HEART_M_AB          "), &
4928         &               TRIM("Heartwood mass above ground                       "), &
4929         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4930         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4931
4932    ! Heartwood mass below ground                       
4933    CALL histdef (hist_id_stom, &
4934         &               TRIM("HEART_M_BE          "), &
4935         &               TRIM("Heartwood mass below ground                       "), &
4936         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4937         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4938
4939    ! Root mass                                         
4940    CALL histdef (hist_id_stom, &
4941         &               TRIM("ROOT_M              "), &
4942         &               TRIM("Root mass                                         "), &
4943         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4944         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4945
4946    ! Fruit mass                                       
4947    CALL histdef (hist_id_stom, &
4948         &               TRIM("FRUIT_M             "), &
4949         &               TRIM("Fruit mass                                        "), &
4950         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4951         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4952
4953    ! Carbohydrate reserve mass                         
4954    CALL histdef (hist_id_stom, &
4955         &               TRIM("RESERVE_M           "), &
4956         &               TRIM("Carbohydrate reserve mass                         "), &
4957         &               TRIM("gC/m^2              "), iim,jjm, hist_hori_id, &
4958         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
4959
4960    ! total turnover rate
4961    CALL histdef (hist_id_stom, &
4962         &               TRIM("TOTAL_TURN          "), &
4963         &               TRIM("total turnover rate                               "), &
4964         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
4965         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4966
4967    ! Leaf turnover                                     
4968    CALL histdef (hist_id_stom, &
4969         &               TRIM("LEAF_TURN           "), &
4970         &               TRIM("Leaf turnover                                     "), &
4971         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
4972         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4973
4974    ! Sap turnover above                               
4975    CALL histdef (hist_id_stom, &
4976         &               TRIM("SAP_AB_TURN         "), &
4977         &               TRIM("Sap turnover above                                "), &
4978         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
4979         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4980
4981    ! Root turnover                                     
4982    CALL histdef (hist_id_stom, &
4983         &               TRIM("ROOT_TURN           "), &
4984         &               TRIM("Root turnover                                     "), &
4985         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
4986         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4987
4988    ! Fruit turnover                                   
4989    CALL histdef (hist_id_stom, &
4990         &               TRIM("FRUIT_TURN          "), &
4991         &               TRIM("Fruit turnover                                    "), &
4992         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
4993         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
4994
4995    ! total conversion of biomass to litter
4996    CALL histdef (hist_id_stom, &
4997         &               TRIM("TOTAL_BM_LITTER     "), &
4998         &               TRIM("total conversion of biomass to litter             "), &
4999         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5000         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5001
5002    ! Leaf death                                       
5003    CALL histdef (hist_id_stom, &
5004         &               TRIM("LEAF_BM_LITTER      "), &
5005         &               TRIM("Leaf death                                        "), &
5006         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5007         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5008
5009    ! Sap death above ground                           
5010    CALL histdef (hist_id_stom, &
5011         &               TRIM("SAP_AB_BM_LITTER    "), &
5012         &               TRIM("Sap death above ground                            "), &
5013         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5014         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5015
5016    ! Sap death below ground                           
5017    CALL histdef (hist_id_stom, &
5018         &               TRIM("SAP_BE_BM_LITTER    "), &
5019         &               TRIM("Sap death below ground                            "), &
5020         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5021         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5022
5023    ! Heartwood death above ground                     
5024    CALL histdef (hist_id_stom, &
5025         &               TRIM("HEART_AB_BM_LITTER  "), &
5026         &               TRIM("Heartwood death above ground                      "), &
5027         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5028         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5029
5030    ! Heartwood death below ground                     
5031    CALL histdef (hist_id_stom, &
5032         &               TRIM("HEART_BE_BM_LITTER  "), &
5033         &               TRIM("Heartwood death below ground                      "), &
5034         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5035         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5036
5037    ! Root death                                       
5038    CALL histdef (hist_id_stom, &
5039         &               TRIM("ROOT_BM_LITTER      "), &
5040         &               TRIM("Root death                                        "), &
5041         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5042         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5043
5044    ! Fruit death                                       
5045    CALL histdef (hist_id_stom, &
5046         &               TRIM("FRUIT_BM_LITTER     "), &
5047         &               TRIM("Fruit death                                       "), &
5048         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5049         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5050
5051    ! Carbohydrate reserve death                       
5052    CALL histdef (hist_id_stom, &
5053         &               TRIM("RESERVE_BM_LITTER   "), &
5054         &               TRIM("Carbohydrate reserve death                        "), &
5055         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5056         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(4), dt, hist_dt)
5057
5058    ! Maintenance respiration                           
5059    CALL histdef (hist_id_stom, &
5060         &               TRIM("MAINT_RESP          "), &
5061         &               TRIM("Maintenance respiration                           "), &
5062         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5063         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
5064
5065    ! Growth respiration                               
5066    CALL histdef (hist_id_stom, &
5067         &               TRIM("GROWTH_RESP         "), &
5068         &               TRIM("Growth respiration                                "), &
5069         &               TRIM("gC/m^2/day          "), iim,jjm, hist_hori_id, &
5070         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(2), dt, hist_dt)
5071
5072    ! age                                               
5073    CALL histdef (hist_id_stom, &
5074         &               TRIM("AGE                 "), &
5075         &               TRIM("age                                               "), &
5076         &               TRIM("years               "), iim,jjm, hist_hori_id, &
5077         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(7), dt, hist_dt)
5078
5079    ! height                                           
5080    CALL histdef (hist_id_stom, &
5081         &               TRIM("HEIGHT              "), &
5082         &               TRIM("height                                            "), &
5083         &               TRIM("m                   "), iim,jjm, hist_hori_id, &
5084         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(7), dt, hist_dt)
5085
5086    ! weekly moisture stress                           
5087    CALL histdef (hist_id_stom, &
5088         &               TRIM("MOISTRESS           "), &
5089         &               TRIM("weekly moisture stress                            "), &
5090         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
5091         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(3), dt, hist_dt)
5092
5093    ! Maximum rate of carboxylation                     
5094    CALL histdef (hist_id_stom, &
5095         &               TRIM("VCMAX               "), &
5096         &               TRIM("Maximum rate of carboxylation                     "), &
5097         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
5098         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5099
5100    ! leaf age                                         
5101    CALL histdef (hist_id_stom, &
5102         &               TRIM("LEAF_AGE            "), &
5103         &               TRIM("leaf age                                          "), &
5104         &               TRIM("days                "), iim,jjm, hist_hori_id, &
5105         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5106
5107    ! Fraction of trees that dies (gap)                 
5108    CALL histdef (hist_id_stom, &
5109         &               TRIM("MORTALITY           "), &
5110         &               TRIM("Fraction of trees that dies (gap)                 "), &
5111         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5112         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5113
5114    ! Fraction of plants killed by fire                 
5115    CALL histdef (hist_id_stom, &
5116         &               TRIM("FIREDEATH           "), &
5117         &               TRIM("Fraction of plants killed by fire                 "), &
5118         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5119         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5120
5121    ! Density of newly established saplings             
5122    CALL histdef (hist_id_stom, &
5123         &               TRIM("IND_ESTAB           "), &
5124         &               TRIM("Density of newly established saplings             "), &
5125         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5126         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5127
5128    ! Establish tree
5129    CALL histdef (hist_id_stom, &
5130         &               TRIM("ESTABTREE           "), &
5131         &               TRIM("Rate of tree establishement                       "), &
5132         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5133         &               1,1,1, -99,32, ave(6), dt, hist_dt)
5134
5135    ! Establish grass
5136    CALL histdef (hist_id_stom, &
5137         &               TRIM("ESTABGRASS          "), &
5138         &               TRIM("Rate of grass establishement                      "), &
5139         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5140         &               1,1,1, -99,32, ave(6), dt, hist_dt)
5141
5142    ! Fraction of plants that dies (light competition) 
5143    CALL histdef (hist_id_stom, &
5144         &               TRIM("LIGHT_DEATH         "), &
5145         &               TRIM("Fraction of plants that dies (light competition)  "), &
5146         &               TRIM("1/day               "), iim,jjm, hist_hori_id, &
5147         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(6), dt, hist_dt)
5148
5149    ! biomass allocated to leaves                       
5150    CALL histdef (hist_id_stom, &
5151         &               TRIM("BM_ALLOC_LEAF       "), &
5152         &               TRIM("biomass allocated to leaves                       "), &
5153         &               TRIM("gC/m**2/pft/dt      "), iim,jjm, hist_hori_id, &
5154         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5155
5156    ! biomass allocated to sapwood above ground         
5157    CALL histdef (hist_id_stom, &
5158         &               TRIM("BM_ALLOC_SAP_AB     "), &
5159         &               TRIM("biomass allocated to sapwood above ground         "), &
5160         &               TRIM("gC/m**2/pft/dt      "), iim,jjm, hist_hori_id, &
5161         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5162
5163    ! biomass allocated to sapwood below ground         
5164    CALL histdef (hist_id_stom, &
5165         &               TRIM("BM_ALLOC_SAP_BE     "), &
5166         &               TRIM("biomass allocated to sapwood below ground         "), &
5167         &               TRIM("gC/m**2/pft/dt      "), iim,jjm, hist_hori_id, &
5168         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5169
5170    ! biomass allocated to roots                       
5171    CALL histdef (hist_id_stom, &
5172         &               TRIM("BM_ALLOC_ROOT       "), &
5173         &               TRIM("biomass allocated to roots                        "), &
5174         &               TRIM("gC/m**2/pft/dt          "), iim,jjm, hist_hori_id, &
5175         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5176
5177    ! biomass allocated to fruits                       
5178    CALL histdef (hist_id_stom, &
5179         &               TRIM("BM_ALLOC_FRUIT      "), &
5180         &               TRIM("biomass allocated to fruits                       "), &
5181         &               TRIM("gC/m**2/pft/dt          "), iim,jjm, hist_hori_id, &
5182         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5183
5184    ! biomass allocated to carbohydrate reserve         
5185    CALL histdef (hist_id_stom, &
5186         &               TRIM("BM_ALLOC_RES        "), &
5187         &               TRIM("biomass allocated to carbohydrate reserve         "), &
5188         &               TRIM("gC/m**2/pft/dt          "), iim,jjm, hist_hori_id, &
5189         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5190
5191    ! time constant of herbivore activity               
5192    CALL histdef (hist_id_stom, &
5193         &               TRIM("HERBIVORES          "), &
5194         &               TRIM("time constant of herbivore activity               "), &
5195         &               TRIM("days                "), iim,jjm, hist_hori_id, &
5196         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5197
5198    ! turnover time for grass leaves                   
5199    CALL histdef (hist_id_stom, &
5200         &               TRIM("TURNOVER_TIME       "), &
5201         &               TRIM("turnover time for grass leaves                    "), &
5202         &               TRIM("days                "), iim,jjm, hist_hori_id, &
5203         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(5), dt, hist_dt)
5204
5205    ! 10 year wood product pool                         
5206    CALL histdef (hist_id_stom, &
5207         &               TRIM("PROD10              "), &
5208         &               TRIM("10 year wood product pool                         "), &
5209         &               TRIM("gC/m**2             "), iim,jjm, hist_hori_id, &
5210         &               11,1,11, hist_pool_11axis_id,32, ave(5), dt, hist_dt)
5211
5212    ! annual flux for each 10 year wood product pool   
5213    CALL histdef (hist_id_stom, &
5214         &               TRIM("FLUX10              "), &
5215         &               TRIM("annual flux for each 10 year wood product pool    "), &
5216         &               TRIM("gC/m**2/yr          "), iim,jjm, hist_hori_id, &
5217         &               10,1,10, hist_pool_10axis_id,32, ave(5), dt, hist_dt)
5218
5219    ! 100 year wood product pool                       
5220    CALL histdef (hist_id_stom, &
5221         &               TRIM("PROD100             "), &
5222         &               TRIM("100 year wood product pool                        "), &
5223         &               TRIM("gC/m**2             "), iim,jjm, hist_hori_id, &
5224         &               101,1,101, hist_pool_101axis_id,32, ave(5), dt, hist_dt)
5225
5226    ! annual flux for each 100 year wood product pool   
5227    CALL histdef (hist_id_stom, &
5228         &               TRIM("FLUX100             "), &
5229         &               TRIM("annual flux for each 100 year wood product pool   "), &
5230         &               TRIM("gC/m**2/yr          "), iim,jjm, hist_hori_id, &
5231         &               100,1,100, hist_pool_100axis_id,32, ave(5), dt, hist_dt)
5232
5233    ! annual release right after deforestation         
5234    CALL histdef (hist_id_stom, &
5235         &               TRIM("CONVFLUX            "), &
5236         &               TRIM("annual release right after deforestation          "), &
5237         &               TRIM("gC/m**2/day          "), iim,jjm, hist_hori_id, &
5238         &               1,1,1, -99,32, ave(5), dt, hist_dt)
5239
5240    ! annual release from all 10 year wood product pools
5241    CALL histdef (hist_id_stom, &
5242         &               TRIM("CFLUX_PROD10        "), &
5243         &               TRIM("annual release from all 10 year wood product pools"), &
5244         &               TRIM("gC/m**2/day          "), iim,jjm, hist_hori_id, &
5245         &               1,1,1, -99,32, ave(5), dt, hist_dt)
5246
5247    ! annual release from all 100year wood product pools
5248    CALL histdef (hist_id_stom, &
5249         &               TRIM("CFLUX_PROD100       "), &
5250         &               TRIM("annual release from all 100year wood product pools"), &
5251         &               TRIM("gC/m**2/day          "), iim,jjm, hist_hori_id, &
5252         &               1,1,1, -99,32, ave(5), dt, hist_dt)
5253    ! agriculure product
5254    CALL histdef (hist_id_stom, &
5255         &               TRIM("HARVEST_ABOVE       "), &
5256         &               TRIM("annual release product after harvest              "), &
5257         &               TRIM("gC/m**2/day          "), iim,jjm, hist_hori_id, &
5258         &               1,1,1, -99,32, ave(5), dt, hist_dt)
5259
5260
5261    CALL histdef(hist_id_stom, 'RESOLUTION_X', 'E-W resolution', 'm', &
5262         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5263    CALL histdef(hist_id_stom, 'RESOLUTION_Y', 'N-S resolution', 'm', &
5264         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5265    CALL histdef(hist_id_stom, 'CONTFRAC', 'Continental fraction', '1', &
5266         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5267    CALL histdef(hist_id_stom, 'Areas', 'Mesh areas', 'm2', &
5268         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5269
5270    !  Special outputs for phenology
5271    CALL histdef (hist_id_stom, &
5272         &               TRIM("WHEN_GROWTHINIT     "), &
5273         &               TRIM("Time elapsed from season beginning                "), &
5274         &               TRIM("d                   "), iim,jjm, hist_hori_id, &
5275         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5276
5277    CALL histdef (hist_id_stom, &
5278         &               TRIM("TIME_LOWGPP         "), &
5279         &               TRIM("Time elapsed since the end of GPP                 "), &
5280         &               TRIM("d                   "), iim,jjm, hist_hori_id, &
5281         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5282
5283    CALL histdef (hist_id_stom, &
5284         &               TRIM("PFTPRESENT          "), &
5285         &               TRIM("PFT exists                                        "), &
5286         &               TRIM("d                   "), iim,jjm, hist_hori_id, &
5287         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5288
5289    CALL histdef (hist_id_stom, &
5290         &               TRIM("GDD_MIDWINTER       "), &
5291         &               TRIM("Growing degree days, since midwinter              "), &
5292         &               TRIM("degK                "), iim,jjm, hist_hori_id, &
5293         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5294
5295    CALL histdef (hist_id_stom, &
5296         &               TRIM("NCD_DORMANCE        "), &
5297         &               TRIM("Number of chilling days, since leaves were lost   "), &
5298         &               TRIM("d                   "), iim,jjm, hist_hori_id, &
5299         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5300
5301    CALL histdef (hist_id_stom, &
5302         &               TRIM("ALLOW_INITPHENO     "), &
5303         &               TRIM("Allow to declare beginning of the growing season  "), &
5304         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
5305         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5306
5307    CALL histdef (hist_id_stom, &
5308         &               TRIM("BEGIN_LEAVES        "), &
5309         &               TRIM("Signal to start putting leaves on                 "), &
5310         &               TRIM("-                   "), iim,jjm, hist_hori_id, &
5311         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(9), dt, hist_dt)
5312
5313    !---------------------------------
5314  END SUBROUTINE stom_define_history
5315  !
5316  SUBROUTINE stom_IPCC_define_history &
5317       & (hist_id_stom_IPCC, nvm, iim, jjm, dt, &
5318       &  hist_dt, hist_hori_id, hist_PFTaxis_id)
5319    ! deforestation axis added as arguments
5320
5321    !---------------------------------------------------------------------
5322    !- Tell ioipsl which variables are to be written
5323    !- and on which grid they are defined
5324    !---------------------------------------------------------------------
5325    IMPLICIT NONE
5326    !-
5327    !- Input
5328    !-
5329    !- File id
5330    INTEGER(i_std),INTENT(in) :: hist_id_stom_IPCC
5331    !- number of PFTs
5332    INTEGER(i_std),INTENT(in) :: nvm
5333    !- Domain size
5334    INTEGER(i_std),INTENT(in) :: iim, jjm
5335    !- Time step of STOMATE (seconds)
5336    REAL(r_std),INTENT(in)    :: dt
5337    !- Time step of history file (s)
5338    REAL(r_std),INTENT(in)    :: hist_dt
5339    !- id horizontal grid
5340    INTEGER(i_std),INTENT(in) :: hist_hori_id
5341    !- id of PFT axis
5342    INTEGER(i_std),INTENT(in) :: hist_PFTaxis_id
5343    !-
5344    !- 1 local
5345    !-
5346    !- Character strings to define operations for histdef
5347    CHARACTER(LEN=40),DIMENSION(max_hist_level) :: ave
5348
5349    !=====================================================================
5350    !- 1 define operations
5351    !=====================================================================
5352    ave(1) =  'ave(scatter(X))'
5353    !=====================================================================
5354    !- 2 surface fields (2d)
5355    !=====================================================================
5356    ! Carbon in Vegetation
5357    CALL histdef (hist_id_stom_IPCC, &
5358         &               TRIM("cVeg"), &
5359         &               TRIM("Carbon in Vegetation"), &
5360         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5361         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5362    ! Carbon in Litter Pool
5363    CALL histdef (hist_id_stom_IPCC, &
5364         &               TRIM("cLitter"), &
5365         &               TRIM("Carbon in Litter Pool"), &
5366         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5367         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5368    ! Carbon in Soil Pool
5369    CALL histdef (hist_id_stom_IPCC, &
5370         &               TRIM("cSoil"), &
5371         &               TRIM("Carbon in Soil Pool"), &
5372         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5373         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5374    ! Carbon in Products of Land Use Change
5375    CALL histdef (hist_id_stom_IPCC, &
5376         &               TRIM("cProduct"), &
5377         &               TRIM("Carbon in Products of Land Use Change"), &
5378         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5379         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5380    ! Carbon Mass Variation
5381    CALL histdef (hist_id_stom_IPCC, &
5382         &               TRIM("cMassVariation"), &
5383         &               TRIM("Terrestrial Carbon Mass Variation"), &
5384         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5385         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5386    ! Leaf Area Fraction
5387    CALL histdef (hist_id_stom_IPCC, &
5388         &               TRIM("lai"), &
5389         &               TRIM("Leaf Area Fraction"), &
5390         &               TRIM("1"), iim,jjm, hist_hori_id, &
5391         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5392    ! Gross Primary Production
5393    CALL histdef (hist_id_stom_IPCC, &
5394         &               TRIM("gpp"), &
5395         &               TRIM("Gross Primary Production"), &
5396         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5397         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5398    ! Autotrophic Respiration
5399    CALL histdef (hist_id_stom_IPCC, &
5400         &               TRIM("ra"), &
5401         &               TRIM("Autotrophic Respiration"), &
5402         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5403         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5404    ! Net Primary Production
5405    CALL histdef (hist_id_stom_IPCC, &
5406         &               TRIM("npp"), &
5407         &               TRIM("Net Primary Production"), &
5408         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5409         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5410    ! Heterotrophic Respiration
5411    CALL histdef (hist_id_stom_IPCC, &
5412         &               TRIM("rh"), &
5413         &               TRIM("Heterotrophic Respiration"), &
5414         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5415         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5416    ! CO2 Emission from Fire
5417    CALL histdef (hist_id_stom_IPCC, &
5418         &               TRIM("fFire"), &
5419         &               TRIM("CO2 Emission from Fire"), &
5420         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5421         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5422
5423    ! CO2 Flux to Atmosphere from Crop Harvesting
5424    CALL histdef (hist_id_stom_IPCC, &
5425         &               TRIM("fHarvest"), &
5426         &               TRIM("CO2 Flux to Atmosphere from Crop Harvesting"), &
5427         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5428         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5429    ! CO2 Flux to Atmosphere from Land Use Change
5430    CALL histdef (hist_id_stom_IPCC, &
5431         &               TRIM("fLuc"), &
5432         &               TRIM("CO2 Flux to Atmosphere from Land Use Change"), &
5433         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5434         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5435    ! Net Biospheric Production
5436    CALL histdef (hist_id_stom_IPCC, &
5437         &               TRIM("nbp"), &
5438         &               TRIM("Net Biospheric Production"), &
5439         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5440         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5441    ! Total Carbon Flux from Vegetation to Litter
5442    CALL histdef (hist_id_stom_IPCC, &
5443         &               TRIM("fVegLitter"), &
5444         &               TRIM("Total Carbon Flux from Vegetation to Litter"), &
5445         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5446         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5447    ! Total Carbon Flux from Litter to Soil
5448    CALL histdef (hist_id_stom_IPCC, &
5449         &               TRIM("fLitterSoil"), &
5450         &               TRIM("Total Carbon Flux from Litter to Soil"), &
5451         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5452         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5453
5454    ! Carbon in Leaves
5455    CALL histdef (hist_id_stom_IPCC, &
5456         &               TRIM("cLeaf"), &
5457         &               TRIM("Carbon in Leaves"), &
5458         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5459         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5460    ! Carbon in Wood
5461    CALL histdef (hist_id_stom_IPCC, &
5462         &               TRIM("cWood"), &
5463         &               TRIM("Carbon in Wood"), &
5464         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5465         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5466    ! Carbon in Roots
5467    CALL histdef (hist_id_stom_IPCC, &
5468         &               TRIM("cRoot"), &
5469         &               TRIM("Carbon in Roots"), &
5470         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5471         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5472    ! Carbon in Other Living Compartments
5473    CALL histdef (hist_id_stom_IPCC, &
5474         &               TRIM("cMisc"), &
5475         &               TRIM("Carbon in Other Living Compartments"), &
5476         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5477         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5478
5479    ! Carbon in Above-Ground Litter
5480    CALL histdef (hist_id_stom_IPCC, &
5481         &               TRIM("cLitterAbove"), &
5482         &               TRIM("Carbon in Above-Ground Litter"), &
5483         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5484         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5485    ! Carbon in Below-Ground Litter
5486    CALL histdef (hist_id_stom_IPCC, &
5487         &               TRIM("cLitterBelow"), &
5488         &               TRIM("Carbon in Below-Ground Litter"), &
5489         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5490         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5491    ! Carbon in Fast Soil Pool
5492    CALL histdef (hist_id_stom_IPCC, &
5493         &               TRIM("cSoilFast"), &
5494         &               TRIM("Carbon in Fast Soil Pool"), &
5495         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5496         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5497    ! Carbon in Medium Soil Pool
5498    CALL histdef (hist_id_stom_IPCC, &
5499         &               TRIM("cSoilMedium"), &
5500         &               TRIM("Carbon in Medium Soil Pool"), &
5501         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5502         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5503    ! Carbon in Slow Soil Pool
5504    CALL histdef (hist_id_stom_IPCC, &
5505         &               TRIM("cSoilSlow"), &
5506         &               TRIM("Carbon in Slow Soil Pool"), &
5507         &               TRIM("kg C m-2"), iim,jjm, hist_hori_id, &
5508         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5509
5510    !- 3 PFT: 3rd dimension
5511    ! Fractional Land Cover of PFT
5512    CALL histdef (hist_id_stom_IPCC, &
5513         &               TRIM("landCoverFrac"), &
5514         &               TRIM("Fractional Land Cover of PFT"), &
5515         &               TRIM("%"), iim,jjm, hist_hori_id, &
5516         &               nvm,1,nvm, hist_PFTaxis_id,32, ave(1), dt, hist_dt)
5517
5518
5519    ! Total Primary Deciduous Tree Cover Fraction
5520    CALL histdef (hist_id_stom_IPCC, &
5521         &               TRIM("treeFracPrimDec"), &
5522         &               TRIM("Total Primary Deciduous Tree Cover Fraction"), &
5523         &               TRIM("%"), iim,jjm, hist_hori_id, &
5524         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5525
5526    ! Total Primary Evergreen Tree Cover Fraction
5527    CALL histdef (hist_id_stom_IPCC, &
5528         &               TRIM("treeFracPrimEver"), &
5529         &               TRIM("Total Primary Evergreen Tree Cover Fraction"), &
5530         &               TRIM("%"), iim,jjm, hist_hori_id, &
5531         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5532
5533    ! Total C3 PFT Cover Fraction
5534    CALL histdef (hist_id_stom_IPCC, &
5535         &               TRIM("c3PftFrac"), &
5536         &               TRIM("Total C3 PFT Cover Fraction"), &
5537         &               TRIM("%"), iim,jjm, hist_hori_id, &
5538         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5539    ! Total C4 PFT Cover Fraction
5540    CALL histdef (hist_id_stom_IPCC, &
5541         &               TRIM("c4PftFrac"), &
5542         &               TRIM("Total C4 PFT Cover Fraction"), &
5543         &               TRIM("%"), iim,jjm, hist_hori_id, &
5544         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5545    ! Growth Autotrophic Respiration
5546    CALL histdef (hist_id_stom_IPCC, &
5547         &               TRIM("rGrowth"), &
5548         &               TRIM("Growth Autotrophic Respiration"), &
5549         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5550         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5551    ! Maintenance Autotrophic Respiration
5552    CALL histdef (hist_id_stom_IPCC, &
5553         &               TRIM("rMaint"), &
5554         &               TRIM("Maintenance Autotrophic Respiration"), &
5555         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5556         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5557    ! CO2 Flux from Atmosphere due to NPP Allocation to Leaf
5558    CALL histdef (hist_id_stom_IPCC, &
5559         &               TRIM("nppLeaf"), &
5560         &               TRIM("CO2 Flux from Atmosphere due to NPP Allocation to Leaf"), &
5561         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5562         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5563    ! CO2 Flux from Atmosphere due to NPP Allocation to Wood
5564    CALL histdef (hist_id_stom_IPCC, &
5565         &               TRIM("nppWood"), &
5566         &               TRIM("CO2 Flux from Atmosphere due to NPP Allocation to Wood"), &
5567         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5568         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5569    ! CO2 Flux from Atmosphere due to NPP Allocation to Root
5570    CALL histdef (hist_id_stom_IPCC, &
5571         &               TRIM("nppRoot"), &
5572         &               TRIM("CO2 Flux from Atmosphere due to NPP Allocation to Root"), &
5573         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5574         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5575    ! Net Carbon Mass Flux out of Atmophere due to Net Ecosystem Productivity on Land.
5576    CALL histdef (hist_id_stom_IPCC, &
5577         &               TRIM("nep"), &
5578         &               TRIM("Net Carbon Mass Flux out of Atmophere due to Net Ecosystem Productivity."), &
5579         &               TRIM("kg C m-2 s-1"), iim,jjm, hist_hori_id, &
5580         &               1,1,1, -99,32, ave(1), dt, hist_dt)
5581
5582    CALL histdef(hist_id_stom_IPCC, 'RESOLUTION_X', 'E-W resolution', 'm', &
5583         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5584    CALL histdef(hist_id_stom_IPCC, 'RESOLUTION_Y', 'N-S resolution', 'm', &
5585         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5586    CALL histdef(hist_id_stom_IPCC, 'CONTFRAC', 'Continental fraction', '1', &
5587         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5588    CALL histdef(hist_id_stom_IPCC, 'Areas', 'Mesh areas', 'm2', &
5589         & iim,jjm, hist_hori_id, 1,1,1, -99, 32, 'once(scatter(X))', dt, hist_dt)
5590
5591    !---------------------------------
5592  END SUBROUTINE stom_IPCC_define_history
5593END MODULE intersurf
Note: See TracBrowser for help on using the repository browser.