3e08e4b51bdf169f962bb88ec079dd709c360f18
[MNH-git_open_source-lfs.git] / src / SURFEX / diag_isba_initn.F90
1 !SURFEX_LIC Copyright 1994-2014 Meteo-France 
2 !SURFEX_LIC This is part of the SURFEX software governed by the CeCILL-C  licence
3 !SURFEX_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt
4 !SURFEX_LIC for details. version 1.
5 !     #########
6       SUBROUTINE DIAG_ISBA_INIT_n(HPROGRAM,KLU,KSW)
7 !     #####################
8 !
9 !!****  *DIAG_ISBA_INIT_n* - routine to initialize ISBA-AGS diagnostic variables
10 !!
11 !!    PURPOSE
12 !!    -------
13 !!
14 !!**  METHOD
15 !!    ------
16 !!
17 !!    EXTERNAL
18 !!    --------
19 !!
20 !!
21 !!    IMPLICIT ARGUMENTS
22 !!    ------------------
23 !!
24 !!    REFERENCE
25 !!    ---------
26 !!
27 !!
28 !!    AUTHOR
29 !!    ------
30 !!      V. Masson   *Meteo France*      
31 !!
32 !!    MODIFICATIONS
33 !!    -------------
34 !!      Original    02/2003 
35 !!      modified    11/2003 by P. LeMoigne: surface cumulated energy budget
36 !!      modified    10/2004 by P. LeMoigne: surface miscellaneous fields
37 !!      B. Decharme    2008    New diag for water budget and allow to reset
38 !                               cumulatives variables at the beginning of a run
39 !!      B. Decharme 06/2009    add patch budget switch 
40 !!      B. Decharme 08/2009    add cummulative diag
41 !!      A.L. Gibelin 04/2009 : Add respiration diagnostics
42 !!      A.L. Gibelin 05/2009 : Add carbon spinup
43 !!      A.L. Gibelin 07/2009 : Suppress RDK and transform GPP as a diagnostic
44 !!      B. Decharme  05/12   : Carbon fluxes in diag_evap
45 !!      B. Decharme  10/12     Isba water budget diag
46 !!      B. Decharme  10/12     New diag for DIF:
47 !!                             F2 stress
48 !!                             Root zone swi, wg and wgi
49 !!                             swi, wg and wgi comparable to ISBA-FR-DG2 and DG3 layers
50 !!                             active layer thickness over permafrost
51 !!                             frozen layer thickness over non-permafrost
52 !-------------------------------------------------------------------------------
53 !
54 !*       0.0    DECLARATIONS
55 !              ------------
56 !
57 USE MODD_SURF_PAR,       ONLY : XUNDEF
58 USE MODD_ISBA_n,         ONLY : NPATCH, NGROUND_LAYER, LFLOOD, CHORT, LGLACIER,  &
59                                 LTR_ML, TSNOW, CISBA, XABC, CPHOTO
60 USE MODD_CH_ISBA_n,      ONLY : LCH_BIO_FLUX, NBEQ 
61 USE MODD_TYPE_DATE_SURF
62 USE MODD_AGRI,           ONLY : LAGRIP
63 USE MODD_DIAG_SURF_ATM_n,ONLY : LREAD_BUDGETC
64 USE MODD_DIAG_ISBA_n,    ONLY : N2M, LSURF_BUDGET, LCOEF, LSURF_VARS,            &
65                                 LPATCH_BUDGET,                                   &
66                                 XRN, XH, XGFLUX, XLEI, XRI, XCD, XCH, XCE,       &
67                                 XTS, XTSRAD, XT2M, XQ2M, XHU2M,                  &
68                                 XZON10M, XMER10M,                                &
69                                 XZ0_WITH_SNOW, XZ0H_WITH_SNOW, XZ0EFF, XQS,      &
70                                 XSWD, XSWU, XSWBD, XSWBU, XLWD, XLWU, XFMU, XFMV,&
71                                 XAVG_RN, XAVG_H, XAVG_LE, XAVG_LEI,              &
72                                 XAVG_GFLUX, XAVG_RI, XAVG_CD, XAVG_CH, XAVG_CE,  &
73                                 XAVG_T2M, XAVG_Q2M, XAVG_HU2M, XAVG_T2M_MIN,     &
74                                 XAVG_ZON10M, XAVG_MER10M, XAVG_T2M_MAX,          &
75                                 XAVG_Z0, XAVG_Z0H, XAVG_Z0EFF, XAVG_QS,          &
76                                 XAVG_SWD, XAVG_SWU, XAVG_SWBD, XAVG_SWBU,        &
77                                 XAVG_LWD, XAVG_LWU, XAVG_FMU, XAVG_FMV,          &
78                                 XSWDC, XSWUC, XLWDC, XLWUC, XFMUC, XFMVC,        &
79                                 XAVG_SWDC, XAVG_SWUC, XAVG_LWDC, XAVG_LWUC,      &
80                                 XAVG_FMUC, XAVG_FMVC, XAVG_TS, XAVG_TSRAD,       &
81                                 XAVG_HU2M_MIN, XAVG_HU2M_MAX, XWIND10M,          &
82                                 XAVG_WIND10M, XAVG_WIND10M_MAX, XAVG_SFCO2  
83 !
84 USE MODD_DIAG_EVAP_ISBA_n, ONLY : LSURF_EVAP_BUDGET, LSURF_BUDGETC, LRESET_BUDGETC,&
85                                   LWATER_BUDGET,                                   &
86                                   XRNC, XAVG_RNC, XHC, XAVG_HC,                    &
87                                   XLEC, XAVG_LEC, XGFLUXC, XAVG_GFLUXC,            &
88                                   XLEIC, XAVG_LEIC,                                &
89                                   XLEG, XLEGC, XAVG_LEG, XAVG_LEGC,                &
90                                   XLEGI, XLEGIC, XAVG_LEGI, XAVG_LEGIC,            &
91                                   XLEV, XLEVC, XAVG_LEV, XAVG_LEVC,                &
92                                   XLES, XLESC, XAVG_LES, XAVG_LESC,                &
93                                   XLESL, XLESLC, XAVG_LESL, XAVG_LESLC,            &
94                                   XLER, XLERC, XAVG_LER, XAVG_LERC,                &
95                                   XLETR, XLETRC, XAVG_LETR, XAVG_LETRC,            &
96                                   XEVAP, XEVAPC, XAVG_EVAP, XAVG_EVAPC,            &
97                                   XDRAIN, XDRAINC, XAVG_DRAIN, XAVG_DRAINC,        &
98                                   XRUNOFF, XRUNOFFC, XAVG_RUNOFF, XAVG_RUNOFFC,    &
99                                   XHORT, XHORTC, XAVG_HORT, XAVG_HORTC,            &
100                                   XDRIP, XDRIPC, XAVG_DRIP, XAVG_DRIPC,            &
101                                   XMELT, XMELTC, XAVG_MELT, XAVG_MELTC,            &
102                                   XIFLOOD, XIFLOODC, XAVG_IFLOOD, XAVG_IFLOODC,    &
103                                   XPFLOOD, XPFLOODC, XAVG_PFLOOD, XAVG_PFLOODC,    &
104                                   XLE_FLOOD, XLE_FLOODC, XAVG_LE_FLOOD,            &
105                                   XAVG_LE_FLOODC, XLEI_FLOOD, XLEI_FLOODC,         &
106                                   XAVG_LEI_FLOOD, XAVG_LEI_FLOODC,                 &
107                                   XICEFLUXC, XAVG_ICEFLUXC,                        &
108                                   XRRVEG, XRRVEGC, XAVG_RRVEG, XAVG_RRVEGC,        &
109                                   XIRRIG_FLUX, XIRRIG_FLUXC, XAVG_IRRIG_FLUX,      &
110                                   XAVG_IRRIG_FLUXC,                                &
111                                   XGPP,XGPPC,XAVG_GPP,XAVG_GPPC, XRESP_AUTO,       &
112                                   XRESPC_AUTO,XAVG_RESP_AUTO,XAVG_RESPC_AUTO,      &
113                                   XRESP_ECO,XRESPC_ECO,XAVG_RESP_ECO,              &
114                                   XAVG_RESPC_ECO,XDWG,XDWGC,XAVG_DWG,XAVG_DWGC,    &     
115                                   XDWGI,XDWGIC,XAVG_DWGI,XAVG_DWGIC,               &
116                                   XDWR,XDWRC,XAVG_DWR,XAVG_DWRC,                   &
117                                   XDSWE,XDSWEC,XAVG_DSWE,XAVG_DSWEC,               &
118                                   XRAINFALL,XRAINFALLC,XSNOWFALL,XSNOWFALLC,       &
119                                   XWATBUD,XWATBUDC,XAVG_WATBUD,XAVG_WATBUDC
120
121 USE MODD_DIAG_MISC_ISBA_n, ONLY : LSURF_MISC_BUDGET, LSURF_MISC_DIF,              &
122                                   XHV,  XSWI, XTSWI, XTWSNOW, XTDSNOW, XTTSNOW,   &
123                                   XDPSNG, XDPSNV, XDPSN,                          &
124                                   XAVG_HV, XAVG_SWI, XAVG_TSWI,                   &
125                                   XALT, XFLT, XAVG_ALT, XAVG_FLT,                 &
126                                   XAVG_TWSNOW, XAVG_TDSNOW, XAVG_TTSNOW,          &
127                                   XAVG_PSNG, XAVG_PSNV, XAVG_PSN, XSEUIL,         &
128                                   XAVG_ALBT, XALBT,                               &
129                                   XSOIL_TSWI, XSOIL_TWG, XSOIL_TWGI,              &
130                                   XDFFG, XDFFV, XDFF, XAVG_FFG, XAVG_FFV, XAVG_FF,&
131                                   XSNOWLIQ, XSNOWTEMP, XDFSAT, XAVG_FSAT,         &
132                                   XSURF_TSWI, XSURF_TWG, XSURF_TWGI,  XROOT_TSWI, &
133                                   XROOT_TWG,  XROOT_TWGI, XFRD2_TSWI, XFRD2_TWG,  &
134                                   XFRD2_TWGI, XFRD3_TSWI, XFRD3_TWG, XFRD3_TWGI,  &
135                                   XFAPAR, XFAPIR, XDFAPARC, XDFAPIRC,             &
136                                   XFAPAR_BS, XFAPIR_BS, XDLAI_EFFC, XAVG_LAI                                  
137 !
138 USE MODD_GR_BIOG_n,        ONLY : XFISO, XFMONO, XIACAN
139 !
140 USE MODI_READ_SURF
141 !
142 !
143 USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
144 USE PARKIND1  ,ONLY : JPRB
145 !
146 IMPLICIT NONE
147 !
148 !*       0.01   Declarations of arguments
149 !              -------------------------
150 !
151 INTEGER, INTENT(IN)         :: KLU       ! size of arrays
152 INTEGER, INTENT(IN)         :: KSW       ! spectral bands
153  CHARACTER(LEN=6), INTENT(IN):: HPROGRAM  ! program calling
154 !
155 !*       0.02   Declarations of local variables
156 !              -------------------------------
157 !
158 INTEGER           :: IVERSION, IBUG
159 INTEGER           :: IRESP          ! IRESP  : return-code if a problem appears
160  CHARACTER(LEN=12) :: YREC           ! Name of the article to be read
161  CHARACTER(LEN=4) :: YREC2
162 REAL(KIND=JPRB) :: ZHOOK_HANDLE
163 !
164 !-------------------------------------------------------------------------------
165 !
166 !* surface energy budget
167 !
168 IF (LHOOK) CALL DR_HOOK('DIAG_ISBA_INIT_N',0,ZHOOK_HANDLE)
169 !
170 IF (LSURF_BUDGET) THEN
171   ALLOCATE(XAVG_RN           (KLU))
172   ALLOCATE(XAVG_H            (KLU))
173   ALLOCATE(XAVG_LE           (KLU))
174   ALLOCATE(XAVG_LEI          (KLU))
175   ALLOCATE(XAVG_GFLUX        (KLU))
176   ALLOCATE(XAVG_SWD          (KLU))
177   ALLOCATE(XAVG_SWU          (KLU))
178   ALLOCATE(XAVG_SWBD         (KLU,KSW))
179   ALLOCATE(XAVG_SWBU         (KLU,KSW))
180   ALLOCATE(XAVG_LWD          (KLU))
181   ALLOCATE(XAVG_LWU          (KLU))
182   ALLOCATE(XAVG_FMU          (KLU))
183   ALLOCATE(XAVG_FMV          (KLU))
184   !
185   XAVG_RN      = XUNDEF
186   XAVG_H       = XUNDEF
187   XAVG_LE      = XUNDEF
188   XAVG_LEI     = XUNDEF
189   XAVG_GFLUX   = XUNDEF
190   XAVG_SWD     = XUNDEF
191   XAVG_SWU     = XUNDEF
192   XAVG_SWBD    = XUNDEF
193   XAVG_SWBU    = XUNDEF
194   XAVG_LWD     = XUNDEF
195   XAVG_LWU     = XUNDEF
196   XAVG_FMU     = XUNDEF
197   XAVG_FMV     = XUNDEF
198   !
199   ALLOCATE(XRN       (KLU,NPATCH))
200   ALLOCATE(XH        (KLU,NPATCH))
201   ALLOCATE(XGFLUX    (KLU,NPATCH))
202   ALLOCATE(XLEI      (KLU,NPATCH))
203   ALLOCATE(XSWD      (KLU,NPATCH))
204   ALLOCATE(XSWU      (KLU,NPATCH))
205   ALLOCATE(XSWBD     (KLU,KSW,NPATCH))
206   ALLOCATE(XSWBU     (KLU,KSW,NPATCH))
207   ALLOCATE(XLWD      (KLU,NPATCH))
208   ALLOCATE(XLWU      (KLU,NPATCH))
209   ALLOCATE(XFMU      (KLU,NPATCH))
210   ALLOCATE(XFMV      (KLU,NPATCH))
211   !
212   XRN      = XUNDEF
213   XH       = XUNDEF
214   XGFLUX   = XUNDEF
215   XLEI     = XUNDEF
216   XSWD     = XUNDEF
217   XSWU     = XUNDEF
218   XSWBD    = XUNDEF
219   XSWBU    = XUNDEF
220   XLWD     = XUNDEF
221   XLWU     = XUNDEF
222   XFMU     = XUNDEF
223   XFMV     = XUNDEF
224   !
225 ELSE
226   ALLOCATE(XAVG_RN           (0))
227   ALLOCATE(XAVG_H            (0))
228   ALLOCATE(XAVG_LE           (0))
229   ALLOCATE(XAVG_LEI          (0))
230   ALLOCATE(XAVG_GFLUX        (0))
231   ALLOCATE(XAVG_SWD          (0))
232   ALLOCATE(XAVG_SWU          (0))
233   ALLOCATE(XAVG_SWBD         (0,0))
234   ALLOCATE(XAVG_SWBU         (0,0))
235   ALLOCATE(XAVG_LWD          (0))
236   ALLOCATE(XAVG_LWU          (0))
237   ALLOCATE(XAVG_FMU          (0))
238   ALLOCATE(XAVG_FMV          (0))
239 !
240   ALLOCATE(XRN       (0,0))
241   ALLOCATE(XH        (0,0))
242   ALLOCATE(XGFLUX    (0,0))
243   ALLOCATE(XLEI      (0,0))
244   ALLOCATE(XSWD      (0,0))
245   ALLOCATE(XSWU      (0,0))
246   ALLOCATE(XSWBD     (0,0,0))
247   ALLOCATE(XSWBU     (0,0,0))
248   ALLOCATE(XLWD      (0,0))
249   ALLOCATE(XLWU      (0,0))
250   ALLOCATE(XFMU      (0,0))
251   ALLOCATE(XFMV      (0,0))
252 END IF
253 !
254 !* detailed surface energy budget
255 !
256 IF (LSURF_EVAP_BUDGET) THEN
257   ALLOCATE(XAVG_LEG       (KLU))
258   ALLOCATE(XAVG_LEGI      (KLU))
259   ALLOCATE(XAVG_LEV       (KLU))
260   ALLOCATE(XAVG_LES       (KLU))
261   ALLOCATE(XAVG_LESL      (KLU))
262   ALLOCATE(XAVG_LER       (KLU))
263   ALLOCATE(XAVG_LETR      (KLU))
264   ALLOCATE(XAVG_EVAP      (KLU))
265   ALLOCATE(XAVG_DRAIN     (KLU))
266   ALLOCATE(XAVG_RUNOFF    (KLU))
267   ALLOCATE(XAVG_HORT      (KLU))
268   ALLOCATE(XAVG_DRIP      (KLU))
269   ALLOCATE(XAVG_RRVEG     (KLU))
270   ALLOCATE(XAVG_MELT      (KLU))
271   ALLOCATE(XAVG_IRRIG_FLUX(KLU))
272   ALLOCATE(XAVG_GPP       (KLU))
273   ALLOCATE(XAVG_RESP_AUTO (KLU))
274   ALLOCATE(XAVG_RESP_ECO  (KLU))   
275   !
276   XAVG_LEG        = XUNDEF
277   XAVG_LEGI       = XUNDEF
278   XAVG_LEV        = XUNDEF
279   XAVG_LES        = XUNDEF
280   XAVG_LESL       = XUNDEF
281   XAVG_LER        = XUNDEF
282   XAVG_LETR       = XUNDEF
283   XAVG_EVAP       = XUNDEF
284   XAVG_DRAIN      = XUNDEF
285   XAVG_RUNOFF     = XUNDEF
286   XAVG_HORT       = XUNDEF
287   XAVG_DRIP       = XUNDEF
288   XAVG_RRVEG      = XUNDEF
289   XAVG_MELT       = XUNDEF
290   XAVG_IRRIG_FLUX = XUNDEF
291   XAVG_GPP        = XUNDEF
292   XAVG_RESP_AUTO  = XUNDEF
293   XAVG_RESP_ECO   = XUNDEF  
294   !
295   ALLOCATE(XAVG_IFLOOD   (KLU))
296   ALLOCATE(XAVG_PFLOOD   (KLU))
297   ALLOCATE(XAVG_LE_FLOOD (KLU))
298   ALLOCATE(XAVG_LEI_FLOOD(KLU))
299   XAVG_IFLOOD    = XUNDEF
300   XAVG_PFLOOD    = XUNDEF
301   XAVG_LE_FLOOD  = XUNDEF
302   XAVG_LEI_FLOOD = XUNDEF
303   !
304   ALLOCATE(XLEG       (KLU,NPATCH))
305   ALLOCATE(XLEGI      (KLU,NPATCH))
306   ALLOCATE(XLEV       (KLU,NPATCH))
307   ALLOCATE(XLES       (KLU,NPATCH))
308   ALLOCATE(XLESL      (KLU,NPATCH))
309   ALLOCATE(XLER       (KLU,NPATCH))
310   ALLOCATE(XLETR      (KLU,NPATCH))
311   ALLOCATE(XEVAP      (KLU,NPATCH))
312   ALLOCATE(XDRAIN     (KLU,NPATCH))
313   ALLOCATE(XRUNOFF    (KLU,NPATCH))
314   ALLOCATE(XHORT      (KLU,NPATCH))
315   ALLOCATE(XDRIP      (KLU,NPATCH))
316   ALLOCATE(XRRVEG     (KLU,NPATCH))
317   ALLOCATE(XMELT      (KLU,NPATCH))
318   ALLOCATE(XIRRIG_FLUX(KLU,NPATCH))
319   ALLOCATE(XGPP       (KLU,NPATCH))
320   ALLOCATE(XRESP_AUTO (KLU,NPATCH))
321   ALLOCATE(XRESP_ECO  (KLU,NPATCH))
322   !
323   XLEG           = XUNDEF 
324   XLEGI          = XUNDEF
325   XLEV           = XUNDEF
326   XLES           = XUNDEF
327   XLESL          = XUNDEF
328   XLER           = XUNDEF
329   XLETR          = XUNDEF
330   XEVAP          = XUNDEF
331   XDRAIN         = XUNDEF
332   XRUNOFF        = XUNDEF
333   XHORT          = XUNDEF
334   XDRIP          = XUNDEF
335   XRRVEG         = XUNDEF
336   XMELT          = XUNDEF
337   XIRRIG_FLUX    = XUNDEF
338   XGPP           = XUNDEF
339   XRESP_AUTO     = XUNDEF
340   XRESP_ECO      = XUNDEF  
341   !
342   ALLOCATE(XIFLOOD (KLU,NPATCH))
343   ALLOCATE(XPFLOOD (KLU,NPATCH))
344   ALLOCATE(XLE_FLOOD(KLU,NPATCH))
345   ALLOCATE(XLEI_FLOOD(KLU,NPATCH))
346   XIFLOOD        = XUNDEF
347   XPFLOOD        = XUNDEF
348   XLE_FLOOD      = XUNDEF
349   XLEI_FLOOD     = XUNDEF
350   !
351   IF(LWATER_BUDGET)THEN
352     !      
353     ALLOCATE(XRAINFALL  (KLU))
354     ALLOCATE(XSNOWFALL  (KLU))
355     ALLOCATE(XAVG_DWG   (KLU))
356     ALLOCATE(XAVG_DWGI  (KLU))
357     ALLOCATE(XAVG_DWR   (KLU))
358     ALLOCATE(XAVG_DSWE  (KLU))
359     ALLOCATE(XAVG_WATBUD(KLU))
360     XRAINFALL   = XUNDEF
361     XSNOWFALL   = XUNDEF
362     XAVG_DWG    = XUNDEF
363     XAVG_DWGI   = XUNDEF
364     XAVG_DWR    = XUNDEF
365     XAVG_DSWE   = XUNDEF
366     XAVG_WATBUD = XUNDEF
367     !
368     ALLOCATE(XDWG   (KLU,NPATCH))
369     ALLOCATE(XDWGI  (KLU,NPATCH))
370     ALLOCATE(XDWR   (KLU,NPATCH))
371     ALLOCATE(XDSWE  (KLU,NPATCH))
372     ALLOCATE(XWATBUD(KLU,NPATCH))
373     XDWG    = XUNDEF
374     XDWGI   = XUNDEF
375     XDWR    = XUNDEF
376     XDSWE   = XUNDEF
377     XWATBUD = XUNDEF
378     ! 
379   ELSE
380     !
381     ALLOCATE(XRAINFALL  (0))
382     ALLOCATE(XSNOWFALL  (0))
383     ALLOCATE(XAVG_DWG   (0))
384     ALLOCATE(XAVG_DWGI  (0))
385     ALLOCATE(XAVG_DWR   (0))
386     ALLOCATE(XAVG_DSWE  (0))
387     ALLOCATE(XAVG_WATBUD(0))
388     !
389     ALLOCATE(XDWG   (0,0))
390     ALLOCATE(XDWGI  (0,0))
391     ALLOCATE(XDWR   (0,0))
392     ALLOCATE(XDSWE  (0,0))
393     ALLOCATE(XWATBUD(0,0))
394     !
395   ENDIF
396   !
397 ELSE
398   ALLOCATE(XAVG_LEG       (0))
399   ALLOCATE(XAVG_LEGI      (0))
400   ALLOCATE(XAVG_LEV       (0))
401   ALLOCATE(XAVG_LES       (0))
402   ALLOCATE(XAVG_LESL      (0))
403   ALLOCATE(XAVG_LER       (0))
404   ALLOCATE(XAVG_LETR      (0))
405   ALLOCATE(XAVG_EVAP      (0))
406   ALLOCATE(XAVG_DRAIN     (0))
407   ALLOCATE(XAVG_RUNOFF    (0))
408   ALLOCATE(XAVG_HORT      (0))
409   ALLOCATE(XAVG_DRIP      (0))
410   ALLOCATE(XAVG_RRVEG     (0))
411   ALLOCATE(XAVG_MELT      (0))
412   ALLOCATE(XAVG_IRRIG_FLUX(0))
413   ALLOCATE(XAVG_GPP       (0))
414   ALLOCATE(XAVG_RESP_AUTO (0))
415   ALLOCATE(XAVG_RESP_ECO  (0))
416   ALLOCATE(XAVG_IFLOOD    (0))
417   ALLOCATE(XAVG_PFLOOD    (0))
418   ALLOCATE(XAVG_LE_FLOOD  (0))
419   ALLOCATE(XAVG_LEI_FLOOD (0))
420 !
421   ALLOCATE(XLEG       (0,0))
422   ALLOCATE(XLEGI      (0,0))
423   ALLOCATE(XLEV       (0,0))
424   ALLOCATE(XLES       (0,0))
425   ALLOCATE(XLESL      (0,0))
426   ALLOCATE(XLER       (0,0))
427   ALLOCATE(XLETR      (0,0))
428   ALLOCATE(XEVAP      (0,0))
429   ALLOCATE(XDRAIN     (0,0))
430   ALLOCATE(XRUNOFF    (0,0))
431   ALLOCATE(XHORT      (0,0))
432   ALLOCATE(XDRIP      (0,0))
433   ALLOCATE(XRRVEG     (0,0))
434   ALLOCATE(XMELT      (0,0))
435   ALLOCATE(XIRRIG_FLUX(0,0))
436   ALLOCATE(XGPP       (0,0))
437   ALLOCATE(XRESP_AUTO (0,0))
438   ALLOCATE(XRESP_ECO  (0,0))  
439   ALLOCATE(XIFLOOD    (0,0))
440   ALLOCATE(XPFLOOD    (0,0))
441   ALLOCATE(XLE_FLOOD  (0,0))
442   ALLOCATE(XLEI_FLOOD (0,0))
443   !
444   ALLOCATE(XRAINFALL  (0))
445   ALLOCATE(XSNOWFALL  (0))
446   ALLOCATE(XAVG_DWG   (0))
447   ALLOCATE(XAVG_DWGI  (0))
448   ALLOCATE(XAVG_DWR   (0))
449   ALLOCATE(XAVG_DSWE  (0))
450   ALLOCATE(XAVG_WATBUD(0))
451   !
452   ALLOCATE(XDWG   (0,0))
453   ALLOCATE(XDWGI  (0,0))
454   ALLOCATE(XDWR   (0,0))
455   ALLOCATE(XDSWE  (0,0))
456   ALLOCATE(XWATBUD(0,0))
457   ! 
458 END IF
459 !
460 !* surface cumulated energy budget
461 !
462 IF (LSURF_BUDGETC) THEN
463   ALLOCATE(XAVG_RNC        (KLU))
464   ALLOCATE(XAVG_HC         (KLU))
465   ALLOCATE(XAVG_LEC        (KLU))
466   ALLOCATE(XAVG_LEIC       (KLU))
467   ALLOCATE(XAVG_GFLUXC     (KLU))
468   ALLOCATE(XAVG_LEGC       (KLU))
469   ALLOCATE(XAVG_LEGIC      (KLU))
470   ALLOCATE(XAVG_LEVC       (KLU))
471   ALLOCATE(XAVG_LESC       (KLU))
472   ALLOCATE(XAVG_LESLC      (KLU))
473   ALLOCATE(XAVG_LERC       (KLU))
474   ALLOCATE(XAVG_LETRC      (KLU))
475   ALLOCATE(XAVG_EVAPC      (KLU))
476   ALLOCATE(XAVG_DRAINC     (KLU))
477   ALLOCATE(XAVG_RUNOFFC    (KLU))
478   ALLOCATE(XAVG_HORTC      (KLU))
479   ALLOCATE(XAVG_DRIPC      (KLU))
480   ALLOCATE(XAVG_RRVEGC     (KLU))
481   ALLOCATE(XAVG_MELTC      (KLU))
482   ALLOCATE(XAVG_IRRIG_FLUXC(KLU))
483   ALLOCATE(XAVG_GPPC       (KLU))
484   ALLOCATE(XAVG_RESPC_AUTO (KLU))
485   ALLOCATE(XAVG_RESPC_ECO  (KLU))
486   ALLOCATE(XAVG_IFLOODC    (KLU))
487   ALLOCATE(XAVG_PFLOODC    (KLU))
488   ALLOCATE(XAVG_LE_FLOODC  (KLU))
489   ALLOCATE(XAVG_LEI_FLOODC (KLU))  
490   !
491   ALLOCATE(XRNC        (KLU,NPATCH))
492   ALLOCATE(XHC         (KLU,NPATCH))
493   ALLOCATE(XLEC        (KLU,NPATCH))
494   ALLOCATE(XLEIC       (KLU,NPATCH))
495   ALLOCATE(XGFLUXC     (KLU,NPATCH))
496   ALLOCATE(XLEGC       (KLU,NPATCH))
497   ALLOCATE(XLEGIC      (KLU,NPATCH))
498   ALLOCATE(XLEVC       (KLU,NPATCH))
499   ALLOCATE(XLESC       (KLU,NPATCH))
500   ALLOCATE(XLESLC      (KLU,NPATCH))
501   ALLOCATE(XLERC       (KLU,NPATCH))
502   ALLOCATE(XLETRC      (KLU,NPATCH))
503   ALLOCATE(XEVAPC      (KLU,NPATCH))
504   ALLOCATE(XDRAINC     (KLU,NPATCH))
505   ALLOCATE(XRUNOFFC    (KLU,NPATCH))
506   ALLOCATE(XHORTC      (KLU,NPATCH))
507   ALLOCATE(XDRIPC      (KLU,NPATCH))
508   ALLOCATE(XRRVEGC     (KLU,NPATCH))
509   ALLOCATE(XMELTC      (KLU,NPATCH))
510   ALLOCATE(XIRRIG_FLUXC(KLU,NPATCH))
511   ALLOCATE(XGPPC       (KLU,NPATCH))
512   ALLOCATE(XRESPC_AUTO (KLU,NPATCH))
513   ALLOCATE(XRESPC_ECO  (KLU,NPATCH))
514   ALLOCATE(XIFLOODC    (KLU,NPATCH))
515   ALLOCATE(XPFLOODC    (KLU,NPATCH))
516   ALLOCATE(XLE_FLOODC  (KLU,NPATCH))
517   ALLOCATE(XLEI_FLOODC (KLU,NPATCH))
518   !
519   ALLOCATE(XAVG_SWDC        (KLU))
520   ALLOCATE(XAVG_SWUC        (KLU))
521   ALLOCATE(XAVG_LWDC        (KLU))
522   ALLOCATE(XAVG_LWUC        (KLU))
523   ALLOCATE(XAVG_FMUC        (KLU))
524   ALLOCATE(XAVG_FMVC        (KLU))
525   ALLOCATE(XSWDC     (KLU,NPATCH))
526   ALLOCATE(XSWUC     (KLU,NPATCH))
527   ALLOCATE(XLWDC     (KLU,NPATCH))
528   ALLOCATE(XLWUC     (KLU,NPATCH))
529   ALLOCATE(XFMUC     (KLU,NPATCH))
530   ALLOCATE(XFMVC     (KLU,NPATCH))
531   !
532   IF(LGLACIER)THEN
533     ALLOCATE(XAVG_ICEFLUXC(KLU))
534     ALLOCATE(XICEFLUXC(KLU,NPATCH))
535   ENDIF
536   !
537   IF(LWATER_BUDGET)THEN
538     !      
539     ALLOCATE(XRAINFALLC  (KLU))
540     ALLOCATE(XSNOWFALLC  (KLU))
541     ALLOCATE(XAVG_DWGC   (KLU))
542     ALLOCATE(XAVG_DWGIC  (KLU))
543     ALLOCATE(XAVG_DWRC   (KLU))
544     ALLOCATE(XAVG_DSWEC  (KLU))
545     ALLOCATE(XAVG_WATBUDC(KLU))
546     !
547     ALLOCATE(XDWGC   (KLU,NPATCH))
548     ALLOCATE(XDWGIC  (KLU,NPATCH))
549     ALLOCATE(XDWRC   (KLU,NPATCH))
550     ALLOCATE(XDSWEC  (KLU,NPATCH))
551     ALLOCATE(XWATBUDC(KLU,NPATCH))
552     !
553   ELSE
554     !      
555     ALLOCATE(XRAINFALLC  (0))
556     ALLOCATE(XSNOWFALLC  (0))
557     ALLOCATE(XAVG_DWGC   (0))
558     ALLOCATE(XAVG_DWGIC  (0))
559     ALLOCATE(XAVG_DWRC   (0))
560     ALLOCATE(XAVG_DSWEC  (0))
561     ALLOCATE(XAVG_WATBUDC(0))
562     !
563     ALLOCATE(XDWGC   (0,0))
564     ALLOCATE(XDWGIC  (0,0))
565     ALLOCATE(XDWRC   (0,0))
566     ALLOCATE(XDSWEC  (0,0))
567     ALLOCATE(XWATBUDC(0,0))
568     !          
569   ENDIF
570   !
571   IF (.NOT.LREAD_BUDGETC) THEN
572       XAVG_RNC         = 0.0
573       XAVG_HC          = 0.0
574       XAVG_LEC         = 0.0
575       XAVG_LEIC        = 0.0
576       XAVG_GFLUXC      = 0.0
577       XAVG_LEGC        = 0.0
578       XAVG_LEGIC       = 0.0
579       XAVG_LEVC        = 0.0
580       XAVG_LESC        = 0.0
581       XAVG_LESLC       = 0.0
582       XAVG_LERC        = 0.0
583       XAVG_LETRC       = 0.0
584       XAVG_EVAPC       = 0.0
585       XAVG_DRAINC      = 0.0
586       XAVG_RUNOFFC     = 0.0
587       XAVG_HORTC       = 0.0
588       XAVG_DRIPC       = 0.0
589       XAVG_RRVEGC      = 0.0
590       XAVG_MELTC       = 0.0
591       XAVG_IRRIG_FLUXC = 0.0
592       XAVG_GPPC        = 0.0
593       XAVG_RESPC_AUTO  = 0.0
594       XAVG_RESPC_ECO   = 0.0  
595       XAVG_IFLOODC     = 0.0
596       XAVG_PFLOODC     = 0.0
597       XAVG_LE_FLOODC   = 0.0
598       XAVG_LEI_FLOODC  = 0.0      
599       !
600       XRNC         = 0.0
601       XHC          = 0.0
602       XLEC         = 0.0
603       XLEIC        = 0.0
604       XGFLUXC      = 0.0
605       XLEGC        = 0.0 
606       XLEGIC       = 0.0
607       XLEVC        = 0.0
608       XLESC        = 0.0
609       XLESLC       = 0.0
610       XLERC        = 0.0
611       XLETRC       = 0.0
612       XEVAPC       = 0.0
613       XDRAINC      = 0.0
614       XRUNOFFC     = 0.0
615       XHORTC       = 0.0
616       XDRIPC       = 0.0
617       XRRVEGC      = 0.0
618       XMELTC       = 0.0
619       XIRRIG_FLUXC = 0.0
620       XGPPC        = 0.0
621       XRESPC_AUTO  = 0.0
622       XRESPC_ECO   = 0.0    
623       XIFLOODC     = 0.0
624       XPFLOODC     = 0.0
625       XLE_FLOODC   = 0.0
626       XLEI_FLOODC  = 0.0
627       !
628       XAVG_SWDC = 0.0
629       XAVG_SWUC = 0.0
630       XAVG_LWDC = 0.0
631       XAVG_LWUC = 0.0
632       XAVG_FMUC = 0.0
633       XAVG_FMVC = 0.0
634       XSWDC     = 0.0
635       XSWUC     = 0.0
636       XLWDC     = 0.0
637       XLWUC     = 0.0
638       XFMUC     = 0.0
639       XFMVC     = 0.0
640       !
641       IF(LGLACIER)THEN
642          XAVG_ICEFLUXC = 0.0
643          XICEFLUXC     = 0.0
644       ENDIF
645       !
646       IF(LWATER_BUDGET)THEN
647         !      
648         XRAINFALLC   = 0.0
649         XSNOWFALLC   = 0.0
650         XAVG_DWGC    = 0.0
651         XAVG_DWGIC   = 0.0
652         XAVG_DWRC    = 0.0
653         XAVG_DSWEC   = 0.0
654         XAVG_WATBUDC = 0.0
655         !
656         XDWGC    = 0.0
657         XDWGIC   = 0.0
658         XDWRC    = 0.0
659         XDSWEC   = 0.0
660         XWATBUDC = 0.0
661         !
662       ENDIF
663       !     
664   ELSEIF (LREAD_BUDGETC.AND.LRESET_BUDGETC) THEN
665       !
666       XAVG_RNC         = 0.0
667       XAVG_HC          = 0.0
668       XAVG_LEC         = 0.0
669       XAVG_LEIC        = 0.0
670       XAVG_GFLUXC      = 0.0
671       XAVG_LEGC        = 0.0
672       XAVG_LEGIC       = 0.0
673       XAVG_LEVC        = 0.0
674       XAVG_LESC        = 0.0
675       XAVG_LESLC       = 0.0
676       XAVG_LERC        = 0.0
677       XAVG_LETRC       = 0.0
678       XAVG_EVAPC       = 0.0
679       XAVG_DRAINC      = 0.0
680       XAVG_RUNOFFC     = 0.0
681       XAVG_HORTC       = 0.0
682       XAVG_DRIPC       = 0.0
683       XAVG_RRVEGC      = 0.0
684       XAVG_MELTC       = 0.0
685       XAVG_IRRIG_FLUXC = 0.0
686       XAVG_GPPC        = 0.0
687       XAVG_RESPC_AUTO  = 0.0
688       XAVG_RESPC_ECO   = 0.0    
689       XAVG_IFLOODC     = 0.0
690       XAVG_PFLOODC     = 0.0
691       XAVG_LE_FLOODC   = 0.0
692       XAVG_LEI_FLOODC  = 0.0      
693       !
694       XRNC         = 0.0
695       XHC          = 0.0
696       XLEC         = 0.0
697       XLEIC        = 0.0
698       XGFLUXC      = 0.0
699       XLEGC        = 0.0 
700       XLEGIC       = 0.0
701       XLEVC        = 0.0
702       XLESC        = 0.0
703       XLESLC       = 0.0
704       XLERC        = 0.0
705       XLETRC       = 0.0
706       XEVAPC       = 0.0
707       XDRAINC      = 0.0
708       XRUNOFFC     = 0.0
709       XHORTC       = 0.0
710       XDRIPC       = 0.0
711       XRRVEGC      = 0.0
712       XMELTC       = 0.0
713       XIRRIG_FLUXC = 0.0
714       XGPPC        = 0.0
715       XRESPC_AUTO  = 0.0
716       XRESPC_ECO   = 0.0   
717       XIFLOODC     = 0.0
718       XPFLOODC     = 0.0
719       XLE_FLOODC   = 0.0
720       XLEI_FLOODC  = 0.0
721       !
722       XAVG_SWDC = 0.0
723       XAVG_SWUC = 0.0
724       XAVG_LWDC = 0.0
725       XAVG_LWUC = 0.0
726       XAVG_FMUC = 0.0
727       XAVG_FMVC = 0.0
728       XSWDC     = 0.0
729       XSWUC     = 0.0
730       XLWDC     = 0.0
731       XLWUC     = 0.0
732       XFMUC     = 0.0
733       XFMVC     = 0.0      
734       !
735       IF(LGLACIER)THEN
736          XAVG_ICEFLUXC = 0.0
737          XICEFLUXC     = 0.0
738       ENDIF
739       !
740       IF(LWATER_BUDGET)THEN
741         !      
742         XRAINFALLC   = 0.0
743         XSNOWFALLC   = 0.0
744         XAVG_DWGC    = 0.0
745         XAVG_DWGIC   = 0.0
746         XAVG_DWRC    = 0.0
747         XAVG_DSWEC   = 0.0
748         XAVG_WATBUDC = 0.0
749         !
750         XDWGC    = 0.0
751         XDWGIC   = 0.0
752         XDWRC    = 0.0
753         XDSWEC   = 0.0
754         XWATBUDC = 0.0
755         !
756       ENDIF
757       !
758   ELSE
759     !      
760     CALL READ_SURF(HPROGRAM,'VERSION',IVERSION,IRESP)
761     CALL READ_SURF(HPROGRAM,'BUG ',IBUG,IRESP)
762     !
763     YREC='RNC_ISBA'
764     CALL READ_SURF(HPROGRAM,YREC,XAVG_RNC    ,IRESP)
765     YREC='HC_ISBA'
766     CALL READ_SURF(HPROGRAM,YREC,XAVG_HC     ,IRESP)
767     YREC='LEC_ISBA'
768     CALL READ_SURF(HPROGRAM,YREC,XAVG_LEC    ,IRESP)
769     YREC='LEIC_ISBA'
770     CALL READ_SURF(HPROGRAM,YREC,XAVG_LEIC   ,IRESP)      
771     YREC='GFLUXC_ISBA'
772     CALL READ_SURF(HPROGRAM,YREC,XAVG_GFLUXC ,IRESP)
773     YREC='LEGC_ISBA'
774     CALL READ_SURF(HPROGRAM,YREC,XAVG_LEGC   ,IRESP)
775     YREC='LEGIC_ISBA'
776     CALL READ_SURF(HPROGRAM,YREC,XAVG_LEGIC  ,IRESP)
777     YREC='LEVC_ISBA'
778     CALL READ_SURF(HPROGRAM,YREC,XAVG_LEVC   ,IRESP)
779     YREC='LESC_ISBA'
780     CALL READ_SURF(HPROGRAM,YREC,XAVG_LESC   ,IRESP)
781     IF( (TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO') .AND. &
782         (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3) ) THEN  
783       YREC='LESLC_ISBA'
784       CALL READ_SURF(HPROGRAM,YREC,XAVG_LESLC  ,IRESP)      
785     ELSE
786       XAVG_LESLC = 0.0
787     ENDIF
788     YREC='LERC_ISBA'
789     CALL READ_SURF(HPROGRAM,YREC,XAVG_LERC   ,IRESP)
790     YREC='LETRC_ISBA'
791     CALL READ_SURF(HPROGRAM,YREC,XAVG_LETRC  ,IRESP)
792     YREC='EVAPC_ISBA'
793     CALL READ_SURF(HPROGRAM,YREC,XAVG_EVAPC  ,IRESP)
794     YREC='DRAINC_ISBA'
795     CALL READ_SURF(HPROGRAM,YREC,XAVG_DRAINC ,IRESP)
796     YREC='RUNOFFC_ISBA'
797     CALL READ_SURF(HPROGRAM,YREC,XAVG_RUNOFFC,IRESP)
798     YREC='DRIVEGC_ISBA'
799     CALL READ_SURF(HPROGRAM,YREC,XAVG_DRIPC  ,IRESP)
800     YREC='RRVEGC_ISBA'
801     CALL READ_SURF(HPROGRAM,YREC,XAVG_RRVEGC ,IRESP)
802     YREC='SNOMLTC_ISBA'
803     CALL READ_SURF(HPROGRAM,YREC,XAVG_MELTC,IRESP)
804     IF (LAGRIP) THEN
805       YREC='IRRIGC_ISBA'
806       CALL READ_SURF(HPROGRAM,YREC,XAVG_IRRIG_FLUXC,IRESP)    
807     ELSE
808       XAVG_IRRIG_FLUXC = 0.0
809     ENDIF
810     !
811     IF(CPHOTO/='NON' .AND. (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3))THEN
812       YREC='GPPC_ISBA'
813       CALL READ_SURF(HPROGRAM,YREC,XAVG_GPPC,IRESP)
814       YREC='RC_AUTO_ISBA'
815       CALL READ_SURF(HPROGRAM,YREC,XAVG_RESPC_AUTO,IRESP)
816       YREC='RC_ECO_ISBA'
817       CALL READ_SURF(HPROGRAM,YREC,XAVG_RESPC_ECO,IRESP)        
818     ELSE
819       XAVG_GPPC = 0.0
820       XAVG_RESPC_AUTO = 0.0
821       XAVG_RESPC_ECO = 0.0
822     ENDIF
823     !
824     IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
825       YREC='HORTONC_ISBA'
826       CALL READ_SURF(HPROGRAM,YREC,XAVG_HORTC,IRESP)
827     ELSE
828       XAVG_HORTC = 0.0
829     ENDIF
830     !
831     IF(LFLOOD)THEN
832       YREC='IFLOODC_ISBA'
833       CALL READ_SURF(HPROGRAM,YREC,XAVG_IFLOODC,IRESP)
834       YREC='PFLOODC_ISBA'
835       CALL READ_SURF(HPROGRAM,YREC,XAVG_PFLOODC,IRESP)
836       YREC='LEFC_ISBA'
837       CALL READ_SURF(HPROGRAM,YREC,XAVG_LE_FLOODC,IRESP)
838       YREC='LEIFC_ISBA'
839       CALL READ_SURF(HPROGRAM,YREC,XAVG_LEI_FLOODC,IRESP)
840     ELSE
841       XAVG_IFLOODC   = 0.0
842       XAVG_PFLOODC   = 0.0
843       XAVG_LE_FLOODC = 0.0
844       XAVG_LEI_FLOODC = 0.0
845     ENDIF
846 !      
847     YREC='SWDC_ISBA'
848     CALL READ_SURF(HPROGRAM,YREC,XAVG_SWDC,IRESP)
849     YREC='SWUC_ISBA'
850     CALL READ_SURF(HPROGRAM,YREC,XAVG_SWUC,IRESP)
851     YREC='LWDC_ISBA'
852     CALL READ_SURF(HPROGRAM,YREC,XAVG_LWDC,IRESP)
853     YREC='LWUC_ISBA'
854     CALL READ_SURF(HPROGRAM,YREC,XAVG_LWUC,IRESP)
855     YREC='FMUC_ISBA'
856     CALL READ_SURF(HPROGRAM,YREC,XAVG_FMUC,IRESP)
857     YREC='FMVC_ISBA'
858     CALL READ_SURF(HPROGRAM,YREC,XAVG_FMVC,IRESP)      
859     !
860     IF(LGLACIER)THEN
861       YREC='ICE_FC_ISBA'         
862       CALL READ_SURF(HPROGRAM,YREC,XAVG_ICEFLUXC,IRESP)      
863     ENDIF
864     !  
865     IF(LWATER_BUDGET .AND. (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3))THEN 
866       YREC='RAINFC_ISBA'
867       CALL READ_SURF(HPROGRAM,YREC,XRAINFALLC,IRESP)
868       YREC='SNOWFC_ISBA'
869       CALL READ_SURF(HPROGRAM,YREC,XSNOWFALLC,IRESP)
870       YREC='DWGC_ISBA'
871       CALL READ_SURF(HPROGRAM,YREC,XAVG_DWGC,IRESP)
872       YREC='DWGIC_ISBA'
873       CALL READ_SURF(HPROGRAM,YREC,XAVG_DWGIC,IRESP)
874       YREC='DWRC_ISBA'
875       CALL READ_SURF(HPROGRAM,YREC,XAVG_DWRC,IRESP)
876       YREC='DSWEC_ISBA'
877       CALL READ_SURF(HPROGRAM,YREC,XAVG_DSWEC,IRESP)
878       YREC='WATBUDC_ISBA'
879       CALL READ_SURF(HPROGRAM,YREC,XAVG_WATBUDC,IRESP)
880     ELSE
881       XRAINFALLC = 0.0
882       XSNOWFALLC = 0.0
883       XAVG_DWGC = 0.0
884       XAVG_DWGIC = 0.0
885       XAVG_DWRC = 0.0
886       XAVG_DSWEC = 0.0
887       XAVG_WATBUDC = 0.0
888     ENDIF
889     !
890     IF(LPATCH_BUDGET .AND. NPATCH>1)THEN
891       !
892       CALL READ_SURF(HPROGRAM,'VERSION',IVERSION,IRESP)
893       CALL READ_SURF(HPROGRAM,'BUG ',IBUG,IRESP)
894       YREC2=''
895       IF (IVERSION<7 .OR. IVERSION==7 .AND. IBUG<3) YREC2='ATCH'
896       YREC='RNC_P'
897       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XRNC    ,IRESP)
898       YREC='HC_P'
899       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XHC     ,IRESP)
900       YREC='LEC_P'
901       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEC    ,IRESP)
902       YREC='LEIC_P'
903       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEIC   ,IRESP)        
904       YREC='GFLUXC_P'
905       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XGFLUXC ,IRESP)
906       YREC='LEGC_P'
907       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEGC   ,IRESP)
908       YREC='LEGIC_P'
909       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEGIC  ,IRESP)
910       YREC='LEVC_P'
911       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEVC   ,IRESP)
912       YREC='LESC_P'
913       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLESC   ,IRESP)
914       IF((TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO') .AND. &
915           (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3))THEN  
916         YREC='LESLC_P'
917         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLESLC,IRESP)        
918       ELSE
919         XLESLC = 0.0
920       ENDIF
921       YREC='LERC_P'
922       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLERC   ,IRESP)
923       YREC='LETRC_P'
924       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLETRC  ,IRESP)
925       YREC='EVAPC_P'
926       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XEVAPC  ,IRESP)
927       YREC='DRAINC_P'
928       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDRAINC ,IRESP)
929       YREC='RUNOFFC_P'
930       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XRUNOFFC,IRESP)
931       YREC='DRIVEGC_P'
932       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDRIPC,IRESP)
933       YREC='RRVEGC_P'
934       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XRRVEGC,IRESP)
935       YREC='SNOMLTC_P'
936       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XMELTC,IRESP)
937       IF (LAGRIP) THEN
938         YREC='IRRIGC_P'
939         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XIRRIG_FLUXC,IRESP)
940       ELSE
941         XIRRIG_FLUXC = 0.0
942       ENDIF
943       !
944       IF(CPHOTO/='NON' .AND. (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3))THEN
945         YREC='GPPC_P'
946         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XGPPC,IRESP)
947         YREC='RC_AUTO_P'
948         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XRESPC_AUTO,IRESP)
949         YREC='RC_ECO_P'
950         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XRESPC_ECO,IRESP)  
951       ELSE
952         XGPPC      =0.0
953         XRESPC_AUTO=0.0
954         XRESPC_ECO =0.0
955       ENDIF
956       !
957       YREC='SWDC_P'
958       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XSWDC,IRESP)
959       YREC='SWUC_P'
960       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XSWUC,IRESP)
961       YREC='LWDC_P'
962       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLWDC,IRESP)
963       YREC='LWUC_P'
964       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLWUC,IRESP)
965       YREC='FMUC_P'
966       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XFMUC,IRESP)
967       YREC='FMVC_P'
968       CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XFMVC,IRESP)
969       !
970       IF(LGLACIER)THEN
971         YREC='ICE_FC_P'         
972         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XICEFLUXC,IRESP)      
973       ENDIF
974       !
975       IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
976         YREC='HORTONC_P'
977         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XHORTC    ,IRESP)
978       ELSE
979         XHORTC     = 0.0
980       ENDIF
981       !
982       IF(LFLOOD)THEN
983         YREC='IFLOODC_P'
984         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XIFLOODC,IRESP)
985         YREC='PFLOODC_P'
986         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XPFLOODC,IRESP)
987         YREC='LEFC_P'
988         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLE_FLOODC,IRESP)
989         YREC='LEIFC_P'
990         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XLEI_FLOODC,IRESP)
991       ELSE
992         XIFLOODC       = 0.0
993         XPFLOODC       = 0.0
994         XLE_FLOODC     = 0.0
995         XLEI_FLOODC    = 0.0
996       ENDIF
997       !  
998       IF(LWATER_BUDGET .AND. (IVERSION>7 .OR. IVERSION==7 .AND. IBUG>=3))THEN 
999         YREC='DWGC_P'
1000         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDWGC,IRESP)
1001         YREC='DWGIC_P'
1002         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDWGIC,IRESP)
1003         YREC='DWRC_P'
1004         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDWRC,IRESP)
1005         YREC='DSWEC_P'
1006         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XDSWEC,IRESP)
1007         YREC='WATBUDC_P'
1008         CALL READ_SURF(HPROGRAM,TRIM(YREC)//YREC2,XWATBUDC,IRESP)
1009       ELSE
1010         XDWGC = 0.0
1011         XDWGIC = 0.0
1012         XDWRC = 0.0
1013         XDSWEC = 0.0
1014         XWATBUDC = 0.0
1015       ENDIF
1016       !
1017     ELSE
1018       !
1019       XRNC         = 0.0
1020       XHC          = 0.0
1021       XLEC         = 0.0
1022       XLEIC        = 0.0
1023       XGFLUXC      = 0.0
1024       XLEGC        = 0.0 
1025       XLEGIC       = 0.0
1026       XLEVC        = 0.0
1027       XLESC        = 0.0
1028       XLESLC       = 0.0
1029       XLERC        = 0.0
1030       XLETRC       = 0.0
1031       XEVAPC       = 0.0
1032       XDRAINC      = 0.0
1033       XRUNOFFC     = 0.0
1034       XHORTC       = 0.0
1035       XDRIPC       = 0.0
1036       XRRVEGC      = 0.0
1037       XMELTC       = 0.0
1038       XIRRIG_FLUXC = 0.0
1039       XGPPC        = 0.0
1040       XRESPC_AUTO  = 0.0
1041       XRESPC_ECO   = 0.0   
1042       XIFLOODC     = 0.0
1043       XPFLOODC     = 0.0
1044       XLE_FLOODC   = 0.0
1045       XLEI_FLOODC  = 0.0
1046       !
1047       XSWDC     = 0.0
1048       XSWUC     = 0.0
1049       XLWDC     = 0.0
1050       XLWUC     = 0.0
1051       XFMUC     = 0.0
1052       XFMVC     = 0.0
1053       !
1054       IF(LGLACIER)THEN
1055         XICEFLUXC=0.0      
1056       ENDIF
1057       !   
1058       IF(LWATER_BUDGET)THEN
1059         XDWGC    = 0.0
1060         XDWGIC   = 0.0
1061         XDWRC    = 0.0
1062         XDSWEC   = 0.0
1063         XWATBUDC = 0.0
1064       ENDIF
1065       !   
1066     ENDIF
1067     !
1068   ENDIF
1069 ELSE
1070   ALLOCATE(XAVG_RNC        (0))
1071   ALLOCATE(XAVG_HC         (0))
1072   ALLOCATE(XAVG_LEC        (0))
1073   ALLOCATE(XAVG_LEIC       (0))
1074   ALLOCATE(XAVG_GFLUXC     (0))
1075   ALLOCATE(XAVG_LEGC       (0))
1076   ALLOCATE(XAVG_LEGIC      (0))
1077   ALLOCATE(XAVG_LEVC       (0))
1078   ALLOCATE(XAVG_LESC       (0))
1079   ALLOCATE(XAVG_LESLC      (0))
1080   ALLOCATE(XAVG_LERC       (0))
1081   ALLOCATE(XAVG_LETRC      (0))
1082   ALLOCATE(XAVG_EVAPC      (0))
1083   ALLOCATE(XAVG_DRAINC     (0))
1084   ALLOCATE(XAVG_RUNOFFC    (0))
1085   ALLOCATE(XAVG_HORTC      (0))
1086   ALLOCATE(XAVG_DRIPC      (0))
1087   ALLOCATE(XAVG_RRVEGC     (0))
1088   ALLOCATE(XAVG_MELTC      (0))
1089   ALLOCATE(XAVG_IRRIG_FLUXC(0))
1090   ALLOCATE(XAVG_GPPC       (0))
1091   ALLOCATE(XAVG_RESPC_AUTO (0))
1092   ALLOCATE(XAVG_RESPC_ECO  (0)) 
1093   ALLOCATE(XAVG_IFLOODC    (0))
1094   ALLOCATE(XAVG_PFLOODC    (0))
1095   ALLOCATE(XAVG_LE_FLOODC  (0))
1096   ALLOCATE(XAVG_LEI_FLOODC (0))
1097   
1098 !
1099   ALLOCATE(XRNC        (0,0))
1100   ALLOCATE(XHC         (0,0))
1101   ALLOCATE(XLEC        (0,0))
1102   ALLOCATE(XLEIC       (0,0))
1103   ALLOCATE(XGFLUXC     (0,0))
1104   ALLOCATE(XLEGC       (0,0))
1105   ALLOCATE(XLEGIC      (0,0))
1106   ALLOCATE(XLEVC       (0,0))
1107   ALLOCATE(XLESC       (0,0))
1108   ALLOCATE(XLESLC      (0,0))
1109   ALLOCATE(XLERC       (0,0))
1110   ALLOCATE(XLETRC      (0,0))
1111   ALLOCATE(XEVAPC      (0,0))
1112   ALLOCATE(XDRAINC     (0,0))
1113   ALLOCATE(XRUNOFFC    (0,0))
1114   ALLOCATE(XHORTC      (0,0))
1115   ALLOCATE(XDRIPC      (0,0))
1116   ALLOCATE(XRRVEGC     (0,0))
1117   ALLOCATE(XMELTC      (0,0))
1118   ALLOCATE(XIRRIG_FLUXC(0,0))
1119   ALLOCATE(XGPPC       (0,0))
1120   ALLOCATE(XRESPC_AUTO (0,0))
1121   ALLOCATE(XRESPC_ECO  (0,0))  
1122   ALLOCATE(XIFLOODC    (0,0))
1123   ALLOCATE(XPFLOODC    (0,0))
1124   ALLOCATE(XLE_FLOODC  (0,0))
1125   ALLOCATE(XLEI_FLOODC (0,0))
1126 !
1127   ALLOCATE(XAVG_SWDC   (0))
1128   ALLOCATE(XAVG_SWUC   (0))
1129   ALLOCATE(XAVG_LWDC   (0))
1130   ALLOCATE(XAVG_LWUC   (0))
1131   ALLOCATE(XAVG_FMUC   (0))
1132   ALLOCATE(XAVG_FMVC   (0))
1133   ALLOCATE(XSWDC     (0,0))
1134   ALLOCATE(XSWUC     (0,0))
1135   ALLOCATE(XLWDC     (0,0))
1136   ALLOCATE(XLWUC     (0,0))
1137   ALLOCATE(XFMUC     (0,0))
1138   ALLOCATE(XFMVC     (0,0))
1139   !      
1140   ALLOCATE(XRAINFALLC  (0))
1141   ALLOCATE(XSNOWFALLC  (0))
1142   ALLOCATE(XAVG_DWGC   (0))
1143   ALLOCATE(XAVG_DWGIC  (0))
1144   ALLOCATE(XAVG_DWRC   (0))
1145   ALLOCATE(XAVG_DSWEC  (0))
1146   ALLOCATE(XAVG_WATBUDC(0))
1147   !
1148   ALLOCATE(XDWGC   (0,0))
1149   ALLOCATE(XDWGIC  (0,0))
1150   ALLOCATE(XDWRC   (0,0))
1151   ALLOCATE(XDSWEC  (0,0))
1152   ALLOCATE(XWATBUDC(0,0))
1153   !
1154 ENDIF
1155 !
1156 IF(.NOT.LGLACIER)THEN
1157   ALLOCATE(XAVG_ICEFLUXC(0))
1158   ALLOCATE(XICEFLUXC(0,0))
1159 ENDIF
1160 !
1161 !
1162 !* surface temperature and parameters at 2m
1163 !
1164 ALLOCATE(XTS    (KLU,NPATCH))
1165 ALLOCATE(XAVG_TS(KLU))
1166 XTS     = XUNDEF
1167 XAVG_TS = XUNDEF
1168 ALLOCATE(XTSRAD    (KLU,NPATCH))
1169 ALLOCATE(XAVG_TSRAD(KLU))
1170 XTSRAD     = XUNDEF
1171 XAVG_TSRAD = XUNDEF
1172
1173 !
1174 IF (N2M>=1) THEN
1175   ALLOCATE(XAVG_RI           (KLU))
1176   ALLOCATE(XAVG_T2M          (KLU))
1177   ALLOCATE(XAVG_T2M_MIN      (KLU))
1178   ALLOCATE(XAVG_T2M_MAX      (KLU))
1179   ALLOCATE(XAVG_Q2M          (KLU))
1180   ALLOCATE(XAVG_HU2M         (KLU))
1181   ALLOCATE(XAVG_HU2M_MIN     (KLU))
1182   ALLOCATE(XAVG_HU2M_MAX     (KLU))
1183   ALLOCATE(XAVG_ZON10M       (KLU))
1184   ALLOCATE(XAVG_MER10M       (KLU))
1185   ALLOCATE(XAVG_WIND10M      (KLU))
1186   ALLOCATE(XAVG_WIND10M_MAX  (KLU))
1187   ALLOCATE(XAVG_SFCO2        (KLU))
1188   XAVG_RI      = XUNDEF
1189   XAVG_T2M     = XUNDEF
1190   XAVG_T2M_MIN = XUNDEF
1191   XAVG_T2M_MAX = 0.0
1192   XAVG_Q2M     = XUNDEF
1193   XAVG_HU2M    = XUNDEF
1194   XAVG_HU2M_MIN= XUNDEF
1195   XAVG_HU2M_MAX= -XUNDEF
1196   XAVG_ZON10M  = XUNDEF
1197   XAVG_MER10M  = XUNDEF
1198   XAVG_WIND10M = XUNDEF
1199   XAVG_WIND10M_MAX = 0.0
1200   XAVG_SFCO2       = XUNDEF
1201   !
1202   ALLOCATE(XRI     (KLU,NPATCH))
1203   ALLOCATE(XT2M    (KLU,NPATCH))
1204   ALLOCATE(XQ2M    (KLU,NPATCH))
1205   ALLOCATE(XHU2M   (KLU,NPATCH))
1206   ALLOCATE(XZON10M (KLU,NPATCH))
1207   ALLOCATE(XMER10M (KLU,NPATCH))
1208   ALLOCATE(XWIND10M(KLU,NPATCH))
1209   !
1210   XRI      = XUNDEF
1211   XT2M     = XUNDEF
1212   XQ2M     = XUNDEF
1213   XHU2M    = XUNDEF
1214   XZON10M  = XUNDEF
1215   XMER10M  = XUNDEF
1216   XWIND10M = XUNDEF
1217 ELSE
1218   ALLOCATE(XAVG_RI           (0))
1219   ALLOCATE(XAVG_T2M          (0))
1220   ALLOCATE(XAVG_T2M_MIN      (0))
1221   ALLOCATE(XAVG_T2M_MAX      (0))
1222   ALLOCATE(XAVG_Q2M          (0))
1223   ALLOCATE(XAVG_HU2M         (0))
1224   ALLOCATE(XAVG_HU2M_MIN     (0))
1225   ALLOCATE(XAVG_HU2M_MAX     (0))
1226   ALLOCATE(XAVG_ZON10M       (0))
1227   ALLOCATE(XAVG_MER10M       (0))
1228   ALLOCATE(XAVG_WIND10M      (0))
1229   ALLOCATE(XAVG_WIND10M_MAX  (0))
1230 !
1231   ALLOCATE(XRI     (0,0))
1232   ALLOCATE(XT2M    (0,0))
1233   ALLOCATE(XQ2M    (0,0))
1234   ALLOCATE(XHU2M   (0,0))
1235   ALLOCATE(XZON10M (0,0))
1236   ALLOCATE(XMER10M (0,0))
1237   ALLOCATE(XWIND10M(0,0))
1238 END IF
1239 !
1240 !* miscellaneous surface fields
1241 !
1242 IF (LSURF_MISC_BUDGET) THEN
1243   ALLOCATE(XAVG_HV           (KLU))
1244   ALLOCATE(XAVG_PSNG         (KLU))
1245   ALLOCATE(XAVG_PSNV         (KLU))
1246   ALLOCATE(XAVG_PSN          (KLU))
1247   ALLOCATE(XAVG_ALBT         (KLU))
1248   ALLOCATE(XAVG_LAI          (KLU))
1249   !
1250   ALLOCATE(XAVG_FSAT        (KLU))  
1251   ALLOCATE(XAVG_FFG         (KLU))
1252   ALLOCATE(XAVG_FFV         (KLU))
1253   ALLOCATE(XAVG_FF          (KLU))
1254   !
1255   ALLOCATE(XSOIL_TSWI        (KLU))
1256   ALLOCATE(XSOIL_TWG         (KLU))
1257   ALLOCATE(XSOIL_TWGI        (KLU))
1258   ALLOCATE(XAVG_SWI          (KLU,NGROUND_LAYER))
1259   ALLOCATE(XAVG_TSWI         (KLU,NGROUND_LAYER))
1260   !
1261   ALLOCATE(XAVG_TWSNOW       (KLU))
1262   ALLOCATE(XAVG_TDSNOW       (KLU))
1263   ALLOCATE(XAVG_TTSNOW       (KLU))
1264   !
1265   XAVG_HV      = XUNDEF
1266   XAVG_SWI     = XUNDEF
1267   XAVG_TSWI    = XUNDEF
1268   XSOIL_TSWI   = XUNDEF
1269   XSOIL_TWG    = XUNDEF
1270   XSOIL_TWGI   = XUNDEF
1271   XAVG_PSNG    = XUNDEF
1272   XAVG_PSNV    = XUNDEF
1273   XAVG_PSN     = XUNDEF
1274   XAVG_ALBT    = XUNDEF
1275   XAVG_LAI     = XUNDEF
1276   XAVG_FSAT    = XUNDEF  
1277   XAVG_FFG     = XUNDEF
1278   XAVG_FFV     = XUNDEF
1279   XAVG_FF      = XUNDEF
1280   XAVG_TWSNOW  = XUNDEF
1281   XAVG_TDSNOW  = XUNDEF
1282   XAVG_TTSNOW  = XUNDEF
1283   !
1284   ALLOCATE(XHV     (KLU,NPATCH))
1285   ALLOCATE(XSWI    (KLU,NGROUND_LAYER,NPATCH))
1286   ALLOCATE(XTSWI   (KLU,NGROUND_LAYER,NPATCH))
1287   ALLOCATE(XTWSNOW (KLU,NPATCH))
1288   ALLOCATE(XTDSNOW (KLU,NPATCH))
1289   ALLOCATE(XTTSNOW (KLU,NPATCH))
1290   ALLOCATE(XDPSNG  (KLU,NPATCH))
1291   ALLOCATE(XDPSNV  (KLU,NPATCH))
1292   ALLOCATE(XDPSN   (KLU,NPATCH))
1293   ALLOCATE(XALBT   (KLU,NPATCH))
1294   !
1295   ALLOCATE(XDFSAT  (KLU,NPATCH))
1296   ALLOCATE(XDFFG   (KLU,NPATCH))
1297   ALLOCATE(XDFFV   (KLU,NPATCH))
1298   ALLOCATE(XDFF    (KLU,NPATCH))
1299   !
1300   ALLOCATE(XSNOWLIQ  (KLU,TSNOW%NLAYER,NPATCH))
1301   ALLOCATE(XSNOWTEMP (KLU,TSNOW%NLAYER,NPATCH))
1302   !
1303   XHV      = XUNDEF
1304   XSWI     = XUNDEF
1305   XTSWI    = XUNDEF
1306   XTWSNOW  = XUNDEF
1307   XTDSNOW  = XUNDEF
1308   XTTSNOW  = XUNDEF
1309   XDPSNG   = XUNDEF
1310   XDPSNV   = XUNDEF
1311   XDPSN    = XUNDEF
1312   XALBT    = XUNDEF
1313   XDFSAT   = XUNDEF  
1314   XDFFG    = XUNDEF
1315   XDFFV    = XUNDEF
1316   XDFF     = XUNDEF
1317   XSNOWLIQ = XUNDEF
1318   XSNOWTEMP= XUNDEF
1319   !
1320   IF(CISBA=='DIF'.AND.LSURF_MISC_DIF)THEN
1321     ALLOCATE(XSURF_TSWI(KLU))
1322     ALLOCATE(XSURF_TWG (KLU))
1323     ALLOCATE(XSURF_TWGI(KLU))
1324     ALLOCATE(XROOT_TSWI(KLU))
1325     ALLOCATE(XROOT_TWG (KLU))
1326     ALLOCATE(XROOT_TWGI(KLU))
1327     ALLOCATE(XFRD2_TSWI(KLU))
1328     ALLOCATE(XFRD2_TWG (KLU))
1329     ALLOCATE(XFRD2_TWGI(KLU))
1330     ALLOCATE(XFRD3_TSWI(KLU))
1331     ALLOCATE(XFRD3_TWG (KLU))
1332     ALLOCATE(XFRD3_TWGI(KLU))    
1333     XSURF_TSWI = XUNDEF
1334     XSURF_TWG  = XUNDEF
1335     XSURF_TWGI = XUNDEF
1336     XROOT_TSWI = XUNDEF
1337     XROOT_TWG  = XUNDEF
1338     XROOT_TWGI = XUNDEF
1339     XFRD2_TSWI = XUNDEF
1340     XFRD2_TWG  = XUNDEF
1341     XFRD2_TWGI = XUNDEF
1342     XFRD3_TSWI = XUNDEF
1343     XFRD3_TWG  = XUNDEF
1344     XFRD3_TWGI = XUNDEF  
1345   ENDIF
1346   !
1347   IF(CISBA=='DIF')THEN
1348     ALLOCATE(XALT(KLU,NPATCH))
1349     ALLOCATE(XFLT(KLU,NPATCH))
1350     ALLOCATE(XAVG_ALT(KLU))
1351     ALLOCATE(XAVG_FLT(KLU))
1352     XALT     = XUNDEF
1353     XFLT     = XUNDEF
1354     XAVG_ALT = XUNDEF
1355     XAVG_FLT = XUNDEF          
1356   ENDIF  
1357   !
1358   IF (LTR_ML) THEN
1359     ALLOCATE (XFAPAR      (KLU, NPATCH))
1360     ALLOCATE (XFAPIR      (KLU, NPATCH))
1361     ALLOCATE (XFAPAR_BS   (KLU, NPATCH))
1362     ALLOCATE (XFAPIR_BS   (KLU, NPATCH))
1363     ALLOCATE (XDFAPARC    (KLU, NPATCH))
1364     ALLOCATE (XDFAPIRC    (KLU, NPATCH))
1365     ALLOCATE (XDLAI_EFFC  (KLU, NPATCH))
1366     !
1367     XFAPAR      = XUNDEF
1368     XFAPIR      = XUNDEF
1369     XFAPAR_BS   = XUNDEF
1370     XFAPIR_BS   = XUNDEF
1371     XDFAPARC    = 0.
1372     XDFAPIRC    = 0.
1373     XDLAI_EFFC  = 0.
1374   ENDIF
1375   !
1376 ELSE
1377   ALLOCATE(XAVG_HV           (0))
1378   ALLOCATE(XAVG_PSNG         (0))
1379   ALLOCATE(XAVG_PSNV         (0))
1380   ALLOCATE(XAVG_PSN          (0))
1381   ALLOCATE(XAVG_ALBT         (0))
1382   ALLOCATE(XAVG_LAI          (0))
1383 !
1384   ALLOCATE(XAVG_FSAT        (0))  
1385   ALLOCATE(XAVG_FFG         (0))
1386   ALLOCATE(XAVG_FFV         (0))
1387   ALLOCATE(XAVG_FF          (0))
1388 !
1389   ALLOCATE(XSOIL_TSWI        (0))
1390   ALLOCATE(XSOIL_TWG         (0))
1391   ALLOCATE(XSOIL_TWGI        (0))
1392   ALLOCATE(XAVG_SWI          (0,0))
1393   ALLOCATE(XAVG_TSWI         (0,0))
1394 !
1395   ALLOCATE(XAVG_TWSNOW       (0))
1396   ALLOCATE(XAVG_TDSNOW       (0))
1397   ALLOCATE(XAVG_TTSNOW       (0))
1398 !
1399   ALLOCATE(XHV     (0,0))
1400   ALLOCATE(XSWI    (0,0,0))
1401   ALLOCATE(XTSWI   (0,0,0))
1402   ALLOCATE(XTWSNOW (0,0))
1403   ALLOCATE(XTDSNOW (0,0))
1404   ALLOCATE(XTTSNOW (0,0))
1405   ALLOCATE(XDPSNG  (0,0))
1406   ALLOCATE(XDPSNV  (0,0))
1407   ALLOCATE(XDPSN   (0,0))
1408   ALLOCATE(XALBT   (0,0))
1409 !
1410   ALLOCATE(XDFSAT  (0,0))
1411   ALLOCATE(XDFFG   (0,0))
1412   ALLOCATE(XDFFV   (0,0))
1413   ALLOCATE(XDFF    (0,0))
1414 !
1415   ALLOCATE(XSNOWLIQ  (0,0,0))
1416   ALLOCATE(XSNOWTEMP (0,0,0))
1417 END IF
1418 !
1419 IF (CISBA/='DIF') THEN
1420   ALLOCATE(XSURF_TSWI(0))
1421   ALLOCATE(XSURF_TWG (0))
1422   ALLOCATE(XSURF_TWGI(0))
1423   ALLOCATE(XROOT_TSWI(0))
1424   ALLOCATE(XROOT_TWG (0))
1425   ALLOCATE(XROOT_TWGI(0))
1426   ALLOCATE(XFRD2_TSWI(0))
1427   ALLOCATE(XFRD2_TWG (0))
1428   ALLOCATE(XFRD2_TWGI(0))
1429   ALLOCATE(XFRD3_TSWI(0))
1430   ALLOCATE(XFRD3_TWG (0))
1431   ALLOCATE(XFRD3_TWGI(0))    
1432   ALLOCATE(XALT(0,0))
1433   ALLOCATE(XFLT(0,0))
1434   ALLOCATE(XAVG_ALT(0))
1435   ALLOCATE(XAVG_FLT(0))      
1436 ENDIF
1437 !
1438 IF (.NOT.LTR_ML) THEN
1439   ALLOCATE (XFAPAR      (0, 0))
1440   ALLOCATE (XFAPIR      (0, 0))
1441   ALLOCATE (XFAPAR_BS   (0, 0))
1442   ALLOCATE (XFAPIR_BS   (0, 0))
1443   ALLOCATE (XDFAPARC    (0, 0))
1444   ALLOCATE (XDFAPIRC    (0, 0))
1445   ALLOCATE (XDLAI_EFFC  (0, 0))
1446 ENDIF
1447 !
1448 !* transfer coefficients
1449 !
1450 IF (LCOEF) THEN
1451   ALLOCATE(XAVG_CD   (KLU))
1452   ALLOCATE(XAVG_CH   (KLU))
1453   ALLOCATE(XAVG_CE   (KLU))
1454   ALLOCATE(XAVG_Z0   (KLU))
1455   ALLOCATE(XAVG_Z0H  (KLU))
1456   ALLOCATE(XAVG_Z0EFF(KLU))
1457   !
1458   XAVG_CD      = XUNDEF
1459   XAVG_CH      = XUNDEF
1460   XAVG_CE      = XUNDEF
1461   XAVG_Z0      = XUNDEF
1462   XAVG_Z0H     = XUNDEF
1463   XAVG_Z0EFF   = XUNDEF
1464   !
1465   ALLOCATE(XCD            (KLU,NPATCH))
1466   ALLOCATE(XCH            (KLU,NPATCH))
1467   ALLOCATE(XCE            (KLU,NPATCH))
1468   ALLOCATE(XZ0_WITH_SNOW  (KLU,NPATCH))
1469   ALLOCATE(XZ0H_WITH_SNOW (KLU,NPATCH))
1470   ALLOCATE(XZ0EFF         (KLU,NPATCH))
1471   !
1472   XCD            = XUNDEF
1473   XCH            = XUNDEF
1474   XCE            = XUNDEF
1475   XZ0_WITH_SNOW  = XUNDEF
1476   XZ0H_WITH_SNOW = XUNDEF
1477   XZ0EFF         = XUNDEF
1478 ELSE
1479   ALLOCATE(XAVG_CD   (0))
1480   ALLOCATE(XAVG_CH   (0))
1481   ALLOCATE(XAVG_CE   (0))
1482   ALLOCATE(XAVG_Z0   (0))
1483   ALLOCATE(XAVG_Z0H  (0))
1484   ALLOCATE(XAVG_Z0EFF(0))
1485 !
1486   ALLOCATE(XCD            (0,0))
1487   ALLOCATE(XCH            (0,0))
1488   ALLOCATE(XCE            (0,0))
1489   ALLOCATE(XZ0_WITH_SNOW  (0,0))
1490   ALLOCATE(XZ0H_WITH_SNOW (0,0))
1491   ALLOCATE(XZ0EFF         (0,0))
1492 END IF
1493 !
1494 !
1495 !* surface humidity
1496 !
1497 IF (LSURF_VARS) THEN
1498   ALLOCATE(XAVG_QS   (KLU))
1499   !
1500   XAVG_QS      = XUNDEF
1501   !
1502   ALLOCATE(XQS            (KLU,NPATCH))
1503   !
1504   XQS            = XUNDEF
1505 ELSE
1506   ALLOCATE(XQS            (0,0))
1507 END IF
1508 !
1509 !* Irrigation threshold
1510 !
1511 IF (LAGRIP) THEN
1512   ALLOCATE(XSEUIL         (KLU,NPATCH))
1513   !
1514   XSEUIL         = XUNDEF
1515 ELSE
1516   ALLOCATE(XSEUIL         (0,0))
1517 END IF
1518 !
1519 !* Chemical fluxes
1520 IF (NBEQ>0 .AND. LCH_BIO_FLUX) THEN
1521   ALLOCATE(XFISO(KLU))
1522   ALLOCATE(XFMONO(KLU))
1523   !
1524   XFISO         = XUNDEF
1525   XFMONO        = XUNDEF
1526 ELSE
1527   ALLOCATE(XFISO(0))
1528   ALLOCATE(XFMONO(0))
1529 ENDIF
1530 !
1531 IF (CPHOTO/='NON') THEN
1532   ALLOCATE(XIACAN(KLU,SIZE(XABC),NPATCH))
1533   !
1534   XIACAN        = XUNDEF
1535   !
1536 ELSE
1537   ALLOCATE(XIACAN(0,0,0))
1538 ENDIF
1539 !
1540 IF (LHOOK) CALL DR_HOOK('DIAG_ISBA_INIT_N',1,ZHOOK_HANDLE)
1541 !-------------------------------------------------------------------------------
1542 !
1543 END SUBROUTINE DIAG_ISBA_INIT_n