source: branches/ORCHIDEE_EXT/ORCHIDEE/src_sechiba/intersurf.f90 @ 116

Last change on this file since 116 was 116, checked in by didier.solyga, 13 years ago

update the calling to the routines of pft_parameters.f90

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