Juan 8/12/2016: add management of LEN_HREC in MNH & SURFEX
[MNH-git_open_source-lfs.git] / src / SURFEX / write_diag_seb_isban.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 WRITE_DIAG_SEB_ISBA_n(HPROGRAM)
7 !     #################################
8 !
9 !!****  *WRITE_DIAG_SEB_ISBA* - writes the ISBA diagnostic fields
10 !!
11 !!    PURPOSE
12 !!    -------
13 !!
14 !!
15 !!**  METHOD
16 !!    ------
17 !!
18 !!    REFERENCE
19 !!    ---------
20 !!
21 !!
22 !!    AUTHOR
23 !!    ------
24 !!      V. Masson   *Meteo France*      
25 !!
26 !!    MODIFICATIONS
27 !!    -------------
28 !!      Original    01/2004
29 !!      B. Decharme 06/2009  key to write (or not) patch result
30 !!      B. Decharme 08/2009  cumulative radiative budget
31 !!      B. Decharme  09/2012 : Bug in local variables declaration in PROVAR_TO_DIAG
32 !!      B. Decharme 09/2012  New diag :
33 !!                           carbon fluxes and reservoirs
34 !!                           soil liquid and ice water content in kg/m2 and m3/m3
35 !!      M.Moge    01/2016  using WRITE_SURF_FIELD2D/3D for 2D/3D surfex fields writes
36 !-------------------------------------------------------------------------------
37 !
38 !*       0.    DECLARATIONS
39 !              ------------
40 !
41 USE MODD_SURFEX_MPI, ONLY : NWG_SIZE
42 !
43 USE MODD_SURF_PAR,   ONLY : XUNDEF, NUNDEF
44 !
45 USE MODD_CSTS,       ONLY : XRHOLW, XTT, XLMTT
46 !
47 USE MODD_DIAG_SURF_ATM_n,ONLY : LPROVAR_TO_DIAG,  LRESET_BUDGETC
48 !
49 USE MODD_ISBA_n,     ONLY :   NPATCH, XPATCH, LFLOOD, CISBA, CHORT,   &
50                               LGLACIER, NGROUND_LAYER, LTEMP_ARP,     &
51                               NTEMPLAYER_ARP, TSNOW, XLE, XDG, XTG,   &
52                               XWG, XWGI, XWR, XICE_STO, XWSAT, XDZG,  &
53                               NWG_LAYER, CPHOTO, CRESPSL, XBIOMASS,   &
54                               XLITTER, XSOILCARB, XLIGNIN_STRUC,      &
55                               NNBIOMASS, NNLITTER, NNSOILCARB,        &
56                               NNLITTLEVS
57 !         
58 USE MODD_AGRI  ,     ONLY : LAGRIP
59 !
60 USE MODD_DIAG_ISBA_n,ONLY :   N2M, LSURF_BUDGET, LRAD_BUDGET, LCOEF,            &
61                               LSURF_VARS,LPATCH_BUDGET,                         &
62                               XAVG_RN, XAVG_H, XAVG_LE, XAVG_LEI, XAVG_GFLUX,   &
63                               XAVG_RI, XAVG_CD, XAVG_CH, XAVG_CE,               &
64                               XAVG_T2M, XAVG_Q2M, XAVG_HU2M,                    &
65                               XAVG_ZON10M, XAVG_MER10M, XAVG_Z0, XAVG_Z0H,      &
66                               XAVG_QS, XAVG_T2M_MIN, XAVG_T2M_MAX,              &
67                               XAVG_SWD, XAVG_SWU, XAVG_SWBD, XAVG_SWBU,         &
68                               XAVG_LWD, XAVG_LWU, XAVG_FMU, XAVG_FMV,           &
69                               XRN, XH, XGFLUX, XLEI,                            &
70                               XRI,XT2M, XQ2M, XHU2M, XZON10M, XMER10M,          &
71                               XZ0_WITH_SNOW, XZ0H_WITH_SNOW, XQS, XWIND10M,     &
72                               XSWD, XSWU, XSWBD, XSWBU, XLWD, XLWU, XFMU, XFMV, &
73                               XSWDC, XSWUC, XLWDC, XLWUC, XFMUC, XFMVC,         &
74                               XAVG_SWDC, XAVG_SWUC, XAVG_LWDC, XAVG_LWUC,       &
75                               XAVG_FMUC, XAVG_FMVC, XAVG_HU2M_MIN,              &
76                               XAVG_HU2M_MAX, XAVG_WIND10M, XAVG_WIND10M_MAX,    &  
77                               XAVG_SFCO2
78 !
79 USE MODI_INIT_IO_SURF_n
80 USE MODI_WRITE_SURF
81 USE MODI_WRITE_SURF_FIELD2D
82 USE MODI_END_IO_SURF_n
83 USE MODD_DIAG_EVAP_ISBA_n,ONLY :   LSURF_EVAP_BUDGET, LSURF_BUDGETC,              &
84                                    LWATER_BUDGET,                                 &
85                                    XRNC, XAVG_RNC, XHC, XAVG_HC,                  &
86                                    XLEC, XAVG_LEC, XGFLUXC, XAVG_GFLUXC,          &
87                                    XLEIC, XAVG_LEIC,                              &
88                                    XLEG, XLEGC, XAVG_LEG, XAVG_LEGC,              &
89                                    XLEGI, XLEGIC, XAVG_LEGI, XAVG_LEGIC,          &
90                                    XLEV, XLEVC, XAVG_LEV, XAVG_LEVC,              &
91                                    XLES, XLESC, XAVG_LES, XAVG_LESC,              &
92                                    XLESL, XLESLC, XAVG_LESL, XAVG_LESLC,          &
93                                    XLER, XLERC, XAVG_LER, XAVG_LERC,              &
94                                    XLETR, XLETRC, XAVG_LETR, XAVG_LETRC,          &
95                                    XEVAP, XEVAPC, XAVG_EVAP, XAVG_EVAPC,          &
96                                    XDRAIN, XDRAINC, XAVG_DRAIN, XAVG_DRAINC,      &
97                                    XRUNOFF, XRUNOFFC, XAVG_RUNOFF, XAVG_RUNOFFC,  &
98                                    XHORT, XHORTC, XAVG_HORT, XAVG_HORTC,          &
99                                    XDRIP, XDRIPC, XAVG_DRIP, XAVG_DRIPC,          &
100                                    XMELT, XMELTC, XAVG_MELT, XAVG_MELTC,          &
101                                    XIFLOOD, XIFLOODC, XAVG_IFLOOD, XAVG_IFLOODC,  &
102                                    XPFLOOD, XPFLOODC, XAVG_PFLOOD, XAVG_PFLOODC,  &
103                                    XLE_FLOOD, XLE_FLOODC, XAVG_LE_FLOOD,          &
104                                    XAVG_LE_FLOODC, XLEI_FLOOD, XLEI_FLOODC,       &
105                                    XAVG_LEI_FLOOD, XAVG_LEI_FLOODC,               &
106                                    XICEFLUXC, XAVG_ICEFLUXC,                      &
107                                    XRRVEG, XRRVEGC, XAVG_RRVEG, XAVG_RRVEGC,      &
108                                    XIRRIG_FLUX, XIRRIG_FLUXC, XAVG_IRRIG_FLUX,    &
109                                    XAVG_IRRIG_FLUXC,                              &
110                                    XGPP,XGPPC,XAVG_GPP,XAVG_GPPC, XRESP_AUTO,     &
111                                    XRESPC_AUTO,XAVG_RESP_AUTO,XAVG_RESPC_AUTO,    &
112                                    XRESP_ECO,XRESPC_ECO,XAVG_RESP_ECO,            &
113                                    XAVG_RESPC_ECO,                                & 
114                                    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_CH_ISBA_n,    ONLY : XDEP, CCH_DRY_DEP, LCH_BIO_FLUX, CCH_NAMES, NBEQ, &
122                               NDSTEQ, LCH_NO_FLUX
123 USE MODD_GR_BIOG_n,    ONLY : XFISO, XFMONO, XNOFLUX
124 USE MODD_DST_n
125 USE MODD_DST_SURF
126 !
127 USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
128 USE PARKIND1  ,ONLY : JPRB
129 !
130 IMPLICIT NONE
131 !
132 !*       0.1   Declarations of arguments
133 !              -------------------------
134 !
135  CHARACTER(LEN=6),  INTENT(IN)  :: HPROGRAM ! program calling
136 !
137 !*       0.2   Declarations of local variables
138 !              -------------------------------
139 !
140 INTEGER           :: IRESP          ! IRESP  : return-code if a problem appears
141  CHARACTER(LEN=LEN_HREC) :: YRECFM         ! Name of the article to be write
142  CHARACTER(LEN=100):: YCOMMENT       ! Comment string
143  CHARACTER(LEN=100):: YCOMMENTUNIT   ! Comment string : unit of the datas in the field to write
144  CHARACTER(LEN=2)  :: YNUM
145 !
146 INTEGER           :: JSV, JSW
147 REAL(KIND=JPRB) :: ZHOOK_HANDLE
148 !
149 !-------------------------------------------------------------------------------
150 !
151 !         Initialisation for IO
152 !
153 IF (LHOOK) CALL DR_HOOK('WRITE_DIAG_SEB_ISBA_N',0,ZHOOK_HANDLE)
154  CALL INIT_IO_SURF_n(HPROGRAM,'NATURE','ISBA  ','WRITE')
155 !
156 !-------------------------------------------------------------------------------
157 !
158 !*       2.     Richardson number :
159 !               -----------------
160 !
161 IF (N2M>=1) THEN
162   !
163   YRECFM='RI_ISBA'
164   YCOMMENT='Richardson number over tile nature'
165   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RI(:),IRESP,HCOMMENT=YCOMMENT)
166   !
167 END IF
168 !
169 !*       3.     Energy fluxes :
170 !               -------------
171 !
172 IF (LSURF_BUDGET) THEN
173   !
174   YRECFM='RN_ISBA'
175   YCOMMENT='Net radiation over tile nature'//' (W/m2)'
176   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RN(:),IRESP,HCOMMENT=YCOMMENT)
177   !
178   YRECFM='H_ISBA'
179   YCOMMENT='Sensible heat flux over tile nature'//' (W/m2)'
180   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_H(:),IRESP,HCOMMENT=YCOMMENT)
181   !
182   YRECFM='LE_ISBA'
183   YCOMMENT='total latent heat flux over tile nature'//' (W/m2)'
184   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LE(:),IRESP,HCOMMENT=YCOMMENT)
185   !
186   YRECFM='LEI_ISBA'
187   YCOMMENT='sublimation latent heat flux over tile nature'//' (W/m2)'
188   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEI(:),IRESP,HCOMMENT=YCOMMENT)
189   !
190   YRECFM='GFLUX_ISBA'
191   YCOMMENT='Ground flux over tile nature'//' (W/m2)'
192   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_GFLUX(:),IRESP,HCOMMENT=YCOMMENT)
193   !
194   IF (LRAD_BUDGET  .OR. (LSURF_BUDGETC .AND. .NOT.LRESET_BUDGETC)) THEN
195     !
196     YRECFM='SWD_ISBA'
197     YCOMMENT='short wave downward radiation over tile nature'//' (W/m2)'
198     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWD(:),IRESP,HCOMMENT=YCOMMENT)
199     !
200     YRECFM='SWU_ISBA'
201     YCOMMENT='short wave upward radiation over tile nature'//' (W/m2)'
202     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWU(:),IRESP,HCOMMENT=YCOMMENT)
203     !
204     YRECFM='LWD_ISBA'
205     YCOMMENT='long wave downward radiation over tile nature'//' (W/m2)'
206     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LWD(:),IRESP,HCOMMENT=YCOMMENT)
207     !
208     YRECFM='LWU_ISBA'
209     YCOMMENT='long wave upward radiation over tile nature'//' (W/m2)'
210     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LWU(:),IRESP,HCOMMENT=YCOMMENT)
211     !
212     DO JSW=1, SIZE(XSWBD,2)
213       YNUM=ACHAR(48+JSW)
214       !
215       YRECFM='SWD_ISBA_'//YNUM
216       YCOMMENT='short wave downward radiation over tile nature for spectral band'//YNUM//' (W/m2)'
217       CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWBD(:,JSW),IRESP,HCOMMENT=YCOMMENT)
218       !
219       YRECFM='SWU_ISBA_'//YNUM
220       YCOMMENT='short wave upward radiation over tile nature for spectral band'//YNUM//' (W/m2)'
221       CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWBU(:,JSW),IRESP,HCOMMENT=YCOMMENT)
222       !
223     ENDDO
224     !
225   ENDIF
226   !
227   YRECFM='FMU_ISBA'
228   YCOMMENT='u component of wind stress'//' (Pa)'  
229   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_FMU(:),IRESP,HCOMMENT=YCOMMENT)
230   !
231   YRECFM='FMV_ISBA'
232   YCOMMENT='v component of wind stress'//' (Pa)'  
233   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_FMV(:),IRESP,HCOMMENT=YCOMMENT)
234   !
235 END IF
236 !
237 !*       4.    Specific Energy fluxes :(for each patch)
238 !              ----------------------------------------
239 !
240 IF (LSURF_EVAP_BUDGET) THEN
241   !
242   YRECFM='LEG_ISBA'
243   YCOMMENT='bare ground evaporation for tile nature'//' (W/m2)'
244   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEG(:),IRESP,HCOMMENT=YCOMMENT)
245   !
246   YRECFM='LEGI_ISBA'
247   YCOMMENT='bare ground sublimation for tile nature'//' (W/m2)'
248   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEGI(:),IRESP,HCOMMENT=YCOMMENT)
249   !
250   YRECFM='LEV_ISBA'
251   YCOMMENT='total vegetation evaporation for tile nature'//' (W/m2)'
252   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEV(:),IRESP,HCOMMENT=YCOMMENT)
253   !
254   YRECFM='LES_ISBA'
255   YCOMMENT='snow sublimation for tile nature'//' (W/m2)'  
256   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LES(:),IRESP,HCOMMENT=YCOMMENT)
257   !
258   IF(TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO')THEN  
259     YRECFM='LESL_ISBA'
260     YCOMMENT='liquid water evaporation over snow for tile nature'//' (W/m2)'
261     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LESL(:),IRESP,HCOMMENT=YCOMMENT)
262   ENDIF
263   !  
264   YRECFM='LER_ISBA'
265   YCOMMENT='canopy direct evaporation for tile nature'//' (W/m2)'
266   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LER(:),IRESP,HCOMMENT=YCOMMENT)
267   !
268   YRECFM='LETR_ISBA'
269   YCOMMENT='vegetation transpiration for tile nature'//' (W/m2)'
270   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LETR(:),IRESP,HCOMMENT=YCOMMENT)
271   !
272   YRECFM='EVAP_ISBA'
273   YCOMMENT='total evaporative flux for tile nature'//' (Kg/m2/s)'
274   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_EVAP(:),IRESP,HCOMMENT=YCOMMENT)
275   !
276   YRECFM='DRAIN_ISBA'
277   YCOMMENT='drainage for tile nature'//' (Kg/m2/s)'
278   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DRAIN(:),IRESP,HCOMMENT=YCOMMENT)
279   !
280   YRECFM='RUNOFF_ISBA'
281   YCOMMENT='runoff for tile nature'//' (Kg/m2/s)'
282   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RUNOFF(:),IRESP,HCOMMENT=YCOMMENT)
283   !
284   IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
285     YRECFM='HORTON_ISBA'
286     YCOMMENT='horton runoff for tile nature'//' (Kg/m2/s)'
287     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HORT(:),IRESP,HCOMMENT=YCOMMENT)
288   ENDIF
289   !
290   YRECFM='DRIVEG_ISBA'
291   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2/s)'
292   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DRIP(:),IRESP,HCOMMENT=YCOMMENT)
293   !
294   YRECFM='RRVEG_ISBA'
295   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2/s)'
296   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RRVEG(:),IRESP,HCOMMENT=YCOMMENT)
297   !
298   YRECFM='SNOMLT_ISBA'
299   YCOMMENT='snow melting rate'//' (Kg/m2/s)'
300   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_MELT(:),IRESP,HCOMMENT=YCOMMENT)
301   !
302   IF(LAGRIP)THEN
303     YRECFM='IRRIG_ISBA'
304     YCOMMENT='irrigation rate'//' (Kg/m2/s)'
305     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_IRRIG_FLUX(:),IRESP,HCOMMENT=YCOMMENT)
306   ENDIF  
307   !
308   IF(LFLOOD)THEN
309     !        
310     YRECFM='IFLOOD_ISBA'
311     YCOMMENT='flood soil infiltration (Kg/m2/s)'    
312     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_IFLOOD(:),IRESP,HCOMMENT=YCOMMENT)
313     !
314     YRECFM='PFLOOD_ISBA'
315     YCOMMENT='intercepted precipitation by floodplains (Kg/m2/s)'    
316     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_PFLOOD(:),IRESP,HCOMMENT=YCOMMENT)
317     !
318     YRECFM='LEF_ISBA'
319     YCOMMENT='total floodplains evaporation (W/m2)'   
320     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LE_FLOOD(:),IRESP,HCOMMENT=YCOMMENT)
321     !
322     YRECFM='LEIF_ISBA'
323     YCOMMENT='solid floodplains evaporation (W/m2)'    
324     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEI_FLOOD(:),IRESP,HCOMMENT=YCOMMENT)
325     !
326   ENDIF
327   !
328   IF(CPHOTO/='NON')THEN
329     !
330     YRECFM='GPP_ISBA'
331     YCOMMENT='gross primary production over tile nature (kgCO2/m2/s)'
332     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_GPP(:),IRESP,HCOMMENT=YCOMMENT)
333     !
334     YRECFM='R_AUTO_ISBA'
335     YCOMMENT='autotrophic respiration over tile nature (kgCO2/m2/s)'
336     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RESP_AUTO(:),IRESP,HCOMMENT=YCOMMENT)
337     !
338     YRECFM='R_ECO_ISBA'
339     YCOMMENT='ecosystem respiration over tile nature (kgCO2/m2/s)'
340     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RESP_ECO(:),IRESP,HCOMMENT=YCOMMENT)
341     !
342   ENDIF
343   !  
344   IF(LWATER_BUDGET)THEN 
345     !
346     YRECFM='RAINF_ISBA'
347     YCOMMENT='input rainfall rate (Kg/m2/s)'
348     CALL WRITE_SURF(HPROGRAM,YRECFM,XRAINFALL(:),IRESP,HCOMMENT=YCOMMENT)
349     !
350     YRECFM='SNOWF_ISBA'
351     YCOMMENT='input snowfall rate (Kg/m2/s)'
352     CALL WRITE_SURF(HPROGRAM,YRECFM,XSNOWFALL(:),IRESP,HCOMMENT=YCOMMENT)
353     !
354     YRECFM='DWG_ISBA'
355     YCOMMENT='change in liquid soil moisture (Kg/m2/s)'
356     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWG(:),IRESP,HCOMMENT=YCOMMENT)
357     !
358     YRECFM='DWGI_ISBA'
359     YCOMMENT='change in solid soil moisture (Kg/m2/s)'
360     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWGI(:),IRESP,HCOMMENT=YCOMMENT)
361     !
362     YRECFM='DWR_ISBA'
363     YCOMMENT='change in water on canopy (Kg/m2/s)'
364     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWR(:),IRESP,HCOMMENT=YCOMMENT)
365     !
366     YRECFM='DSWE_ISBA'
367     YCOMMENT='change in snow water equivalent (Kg/m2/s)'
368     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DSWE(:),IRESP,HCOMMENT=YCOMMENT)
369     !
370     YRECFM='WATBUD_ISBA'
371     YCOMMENT='isba water budget as residue (Kg/m2/s)'
372     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_WATBUD(:),IRESP,HCOMMENT=YCOMMENT)
373     !
374   ENDIF
375   !  
376 ENDIF
377 !
378 !*       5.    Cumulated Energy fluxes
379 !              -----------------------
380 !
381 IF (LSURF_BUDGETC) THEN
382   !
383   YRECFM='LEGC_ISBA'
384   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
385   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEGC(:),IRESP,HCOMMENT=YCOMMENT)
386   !
387   YRECFM='LEGIC_ISBA'
388   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
389   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEGIC(:),IRESP,HCOMMENT=YCOMMENT)
390   !
391   YRECFM='LEVC_ISBA'
392   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
393   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEVC(:),IRESP,HCOMMENT=YCOMMENT)
394   !
395   YRECFM='LESC_ISBA'
396   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
397   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LESC(:),IRESP,HCOMMENT=YCOMMENT)
398   !
399   IF(TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO')THEN  
400     YRECFM='LESLC_ISBA'
401     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
402     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LESLC(:),IRESP,HCOMMENT=YCOMMENT)
403   ENDIF
404   !  
405   YRECFM='LERC_ISBA'
406   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
407   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LERC(:),IRESP,HCOMMENT=YCOMMENT)
408   !
409   YRECFM='LETRC_ISBA'
410   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
411   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LETRC(:),IRESP,HCOMMENT=YCOMMENT)
412   !
413   YRECFM='EVAPC_ISBA'
414   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
415   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_EVAPC(:),IRESP,HCOMMENT=YCOMMENT)
416   !
417   YRECFM='DRAINC_ISBA'
418   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
419   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DRAINC(:),IRESP,HCOMMENT=YCOMMENT)
420   !
421   YRECFM='RUNOFFC_ISBA'
422   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
423   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RUNOFFC(:),IRESP,HCOMMENT=YCOMMENT)
424   !
425   IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
426     YRECFM='HORTONC_ISBA'
427     YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
428     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HORTC(:),IRESP,HCOMMENT=YCOMMENT)
429   ENDIF
430   !
431   YRECFM='DRIVEGC_ISBA'
432   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
433   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DRIPC(:),IRESP,HCOMMENT=YCOMMENT)
434   !
435   YRECFM='RRVEGC_ISBA'
436   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
437   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RRVEGC(:),IRESP,HCOMMENT=YCOMMENT)
438   !
439   YRECFM='SNOMLTC_ISBA'
440   YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
441   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_MELTC(:),IRESP,HCOMMENT=YCOMMENT)
442   !
443   IF(LAGRIP)THEN
444     YRECFM='IRRIGC_ISBA'
445     YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
446     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_IRRIG_FLUXC(:),IRESP,HCOMMENT=YCOMMENT)
447   ENDIF
448   !  
449   IF(LGLACIER)THEN
450     YRECFM='ICE_FC_ISBA'
451     YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
452     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_ICEFLUXC(:),IRESP,HCOMMENT=YCOMMENT)
453   ENDIF
454   !
455   IF(LFLOOD)THEN
456     !
457     YRECFM='IFLOODC_ISBA'
458     YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
459     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_IFLOODC(:),IRESP,HCOMMENT=YCOMMENT)
460     !
461     YRECFM='PFLOODC_ISBA'
462     YCOMMENT='X_Y_'//YRECFM//' (Kg/m2)'
463     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_PFLOODC(:),IRESP,HCOMMENT=YCOMMENT)
464     !
465     YRECFM='LEFC_ISBA'
466     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
467     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LE_FLOODC(:),IRESP,HCOMMENT=YCOMMENT)
468     !
469     YRECFM='LEIFC_ISBA'
470     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
471     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEI_FLOODC(:),IRESP,HCOMMENT=YCOMMENT)
472     !
473   ENDIF
474   !
475   YRECFM='RNC_ISBA'
476   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
477   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RNC(:),IRESP,HCOMMENT=YCOMMENT)
478   !
479   YRECFM='HC_ISBA'
480   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
481   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HC(:),IRESP,HCOMMENT=YCOMMENT)
482   !
483   YRECFM='LEC_ISBA'
484   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
485   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEC(:),IRESP,HCOMMENT=YCOMMENT)
486   !
487   YRECFM='LEIC_ISBA'
488   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
489   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LEIC(:),IRESP,HCOMMENT=YCOMMENT)
490   !
491   YRECFM='GFLUXC_ISBA'
492   YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
493   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_GFLUXC(:),IRESP,HCOMMENT=YCOMMENT)
494   !
495   IF (LRAD_BUDGET .OR. (LSURF_BUDGETC .AND. .NOT.LRESET_BUDGETC)) THEN
496     !
497     YRECFM='SWDC_ISBA'
498     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
499     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWDC(:),IRESP,HCOMMENT=YCOMMENT)
500     !
501     YRECFM='SWUC_ISBA'
502     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
503     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SWUC(:),IRESP,HCOMMENT=YCOMMENT)
504     !
505     YRECFM='LWDC_ISBA'
506     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
507     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LWDC(:),IRESP,HCOMMENT=YCOMMENT)
508     !
509     YRECFM='LWUC_ISBA'
510     YCOMMENT='X_Y_'//YRECFM//' (J/m2)'
511     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_LWUC(:),IRESP,HCOMMENT=YCOMMENT)
512     !
513   ENDIF
514   !
515   YRECFM='FMUC_ISBA'
516   YCOMMENT='X_Y_'//YRECFM//' (Pa.s)'  
517   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_FMUC(:),IRESP,HCOMMENT=YCOMMENT)
518   !
519   YRECFM='FMVC_ISBA'
520   YCOMMENT='X_Y_'//YRECFM//' (Pa.s)'  
521   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_FMVC(:),IRESP,HCOMMENT=YCOMMENT)
522   !
523   IF(CPHOTO/='NON')THEN
524     !
525     YRECFM='GPPC_ISBA'
526     YCOMMENT='X_Y_'//YRECFM//' (kgCO2/m2/s)'
527     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_GPPC(:),IRESP,HCOMMENT=YCOMMENT)
528     !
529     YRECFM='RC_AUTO_ISBA'
530     YCOMMENT='X_Y_'//YRECFM//' (kgCO2/m2/s)'
531     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RESPC_AUTO(:),IRESP,HCOMMENT=YCOMMENT)
532     !
533     YRECFM='RC_ECO_ISBA'
534     YCOMMENT='X_Y_'//YRECFM//' (kgCO2/m2/s)'
535     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_RESPC_ECO(:),IRESP,HCOMMENT=YCOMMENT)
536     !
537   ENDIF
538   !  
539   IF(LWATER_BUDGET .OR. (LSURF_BUDGETC .AND. .NOT.LRESET_BUDGETC))THEN 
540     !
541     YRECFM='RAINFC_ISBA'
542     YCOMMENT='cumulated input rainfall rate (Kg/m2)'
543     CALL WRITE_SURF(HPROGRAM,YRECFM,XRAINFALLC(:),IRESP,HCOMMENT=YCOMMENT)
544     !
545     YRECFM='SNOWFC_ISBA'
546     YCOMMENT='cumulated input snowfall rate (Kg/m2)'
547     CALL WRITE_SURF(HPROGRAM,YRECFM,XSNOWFALLC(:),IRESP,HCOMMENT=YCOMMENT)
548     !
549     YRECFM='DWGC_ISBA'
550     YCOMMENT='cumulated change in liquid soil moisture (Kg/m2)'
551     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWGC(:),IRESP,HCOMMENT=YCOMMENT)
552     !
553     YRECFM='DWGIC_ISBA'
554     YCOMMENT='cumulated change in solid soil moisture (Kg/m2)'
555     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWGIC(:),IRESP,HCOMMENT=YCOMMENT)
556     !
557     YRECFM='DWRC_ISBA'
558     YCOMMENT='cumulated change in water on canopy (Kg/m2)'
559     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DWRC(:),IRESP,HCOMMENT=YCOMMENT)
560     !
561     YRECFM='DSWEC_ISBA'
562     YCOMMENT='cumulated change in snow water equivalent (Kg/m2)'
563     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_DSWEC(:),IRESP,HCOMMENT=YCOMMENT)
564     !
565     YRECFM='WATBUDC_ISBA'
566     YCOMMENT='cumulated isba water budget as residue (Kg/m2)'
567     CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_WATBUDC(:),IRESP,HCOMMENT=YCOMMENT)
568     !
569   ENDIF 
570   !  
571 ENDIF
572 !
573 !*       6.     parameters at 2 and 10 meters :
574 !               -------------------------------
575 !
576 IF (N2M>=1) THEN
577   !
578   YRECFM='T2M_ISBA'
579   YCOMMENT='X_Y_'//YRECFM//' (K)'
580   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_T2M(:),IRESP,HCOMMENT=YCOMMENT)
581   !
582   YRECFM='T2MMIN_ISBA'
583   YCOMMENT='X_Y_'//YRECFM//' (K)'
584   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_T2M_MIN(:),IRESP,HCOMMENT=YCOMMENT)
585   XAVG_T2M_MIN(:)=XUNDEF
586   !
587   YRECFM='T2MMAX_ISBA'
588   YCOMMENT='X_Y_'//YRECFM//' (K)'
589   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_T2M_MAX(:),IRESP,HCOMMENT=YCOMMENT)
590   XAVG_T2M_MAX(:)=0.0
591   !
592   YRECFM='Q2M_ISBA'
593   YCOMMENT='X_Y_'//YRECFM//' (KG/KG)'
594   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_Q2M(:),IRESP,HCOMMENT=YCOMMENT)
595   !
596   YRECFM='HU2M_ISBA'
597   YCOMMENT='X_Y_'//YRECFM//' (-)'
598   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HU2M(:),IRESP,HCOMMENT=YCOMMENT)
599   !
600   YRECFM='HU2MMIN_ISBA'
601   YCOMMENT='X_Y_'//YRECFM//' (-)'
602   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HU2M_MIN(:),IRESP,HCOMMENT=YCOMMENT)
603   XAVG_HU2M_MIN(:)=XUNDEF
604   !
605   YRECFM='HU2MMAX_ISBA'
606   YCOMMENT='X_Y_'//YRECFM//' (-)'
607   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_HU2M_MAX(:),IRESP,HCOMMENT=YCOMMENT)
608   XAVG_HU2M_MAX(:)=-XUNDEF
609   !
610   YRECFM='ZON10M_ISBA'
611   YCOMMENT='X_Y_'//YRECFM//' (M/S)'
612   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_ZON10M(:),IRESP,HCOMMENT=YCOMMENT)
613   !
614   YRECFM='MER10M_ISBA'
615   YCOMMENT='X_Y_'//YRECFM//' (M/S)'
616   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_MER10M(:),IRESP,HCOMMENT=YCOMMENT)
617   !
618   YRECFM='W10M_ISBA'
619   YCOMMENT='X_Y_'//YRECFM//' (M/S)'
620   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_WIND10M(:),IRESP,HCOMMENT=YCOMMENT)
621   !
622   YRECFM='W10MMAX_ISBA'
623   YCOMMENT='X_Y_'//YRECFM//' (M/S)'
624   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_WIND10M_MAX(:),IRESP,HCOMMENT=YCOMMENT)
625   XAVG_WIND10M_MAX(:)=0.0
626   !
627   YRECFM='SFCO2_ISBA'
628   YCOMMENT='X_Y_'//YRECFM//' (KG/M2/S)'
629   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_SFCO2(:),IRESP,HCOMMENT=YCOMMENT)
630   !  
631 END IF
632 !----------------------------------------------------------------------------
633 !
634 !*       7.     Transfer coefficients
635 !               ---------------------
636 !
637 IF (LCOEF) THEN
638   !
639   YRECFM='CD_ISBA'
640   YCOMMENT='X_Y_'//YRECFM
641   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_CD(:),IRESP,HCOMMENT=YCOMMENT)
642   !
643   YRECFM='CH_ISBA'
644   YCOMMENT='X_Y_'//YRECFM
645   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_CH(:),IRESP,HCOMMENT=YCOMMENT)
646   !
647   YRECFM='CE_ISBA'
648   YCOMMENT='X_Y_'//YRECFM
649   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_CE(:),IRESP,HCOMMENT=YCOMMENT)
650   !
651   YRECFM='Z0_ISBA'
652   YCOMMENT='X_Y_'//YRECFM//' (M)'
653   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_Z0(:),IRESP,HCOMMENT=YCOMMENT)
654   !
655   YRECFM='Z0H_ISBA'
656   YCOMMENT='X_Y_'//YRECFM//' (M)'
657   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_Z0H(:),IRESP,HCOMMENT=YCOMMENT)
658   !
659 ENDIF
660 !
661 !----------------------------------------------------------------------------
662 !
663 !*       8.     Surface humidity
664 !               ----------------
665 IF (LSURF_VARS) THEN
666   !
667   YRECFM='QS_ISBA'
668   YCOMMENT='X_Y_'//YRECFM//' (KG/KG)'
669   CALL WRITE_SURF(HPROGRAM,YRECFM,XAVG_QS(:),IRESP,HCOMMENT=YCOMMENT)
670   !
671 ENDIF
672 !
673 !----------------------------------------------------------------------------
674 !
675 !*       9.     Diag of prognostic fields
676 !               -------------------------
677 !
678 IF (LPROVAR_TO_DIAG) CALL PROVAR_TO_DIAG
679 !
680 !----------------------------------------------------------------------------
681 !
682 !User want (or not) patch output
683 IF(LPATCH_BUDGET.AND.(NPATCH >1))THEN
684     !----------------------------------------------------------------------------
685     !
686     !*      10.     Richardson number (for each patch)
687     !               -----------------
688     !
689     IF (N2M>=1) THEN
690       !
691       YRECFM='RI_P'
692       YCOMMENT='X_Y_'//YRECFM
693       YCOMMENTUNIT='-'
694       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRI(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
695       !
696     END IF
697     !
698     !*       11.     Energy fluxes :(for each patch)
699     !                -------------
700     !
701     IF (LSURF_BUDGET) THEN
702       !
703       YRECFM='RN_P'
704       YCOMMENT='X_Y_'//YRECFM
705       YCOMMENTUNIT='W/m2'
706       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRN(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
707       !
708       YRECFM='H_P'
709       YCOMMENT='X_Y_'//YRECFM
710       YCOMMENTUNIT='W/m2'
711       CALL WRITE_SURF_FIELD2D(HPROGRAM,XH(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
712       !
713       YRECFM='LE_P'
714       YCOMMENT='X_Y_'//YRECFM
715       YCOMMENTUNIT='W/m2'
716       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLE(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
717       !
718       YRECFM='LEI_P'
719       YCOMMENT='X_Y_'//YRECFM
720       YCOMMENTUNIT='W/m2'
721       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEI(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
722       !
723       YRECFM='GFLUX_P'
724       YCOMMENT='X_Y_'//YRECFM
725       YCOMMENTUNIT='W/m2'
726       CALL WRITE_SURF_FIELD2D(HPROGRAM,XGFLUX(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
727       !
728       IF (LRAD_BUDGET .OR. (LSURF_BUDGETC .AND. .NOT.LRESET_BUDGETC)) THEN
729         !
730         YRECFM='SWD_P'
731         YCOMMENT='X_Y_'//YRECFM
732         YCOMMENTUNIT='W/m2'
733         CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
734         !
735         YRECFM='SWU_P'
736         YCOMMENT='X_Y_'//YRECFM
737         YCOMMENTUNIT='W/m2'
738         CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWU(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
739         !
740         YRECFM='LWD_P'
741         YCOMMENT='X_Y_'//YRECFM
742         YCOMMENTUNIT='W/m2'
743         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLWD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
744         !
745         YRECFM='LWU_P'
746         YCOMMENT='X_Y_'//YRECFM
747         YCOMMENTUNIT='W/m2'
748         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLWU(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
749         !
750         DO JSW=1, SIZE(XSWBD,2)
751           YNUM=ACHAR(48+JSW)
752           !
753           YRECFM='SWD_P'//YNUM
754           YCOMMENT='X_Y_'//YRECFM
755           YCOMMENTUNIT='W/m2'
756           CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWBD(:,JSW,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
757           !
758           YRECFM='SWU_P'//YNUM
759           YCOMMENT='X_Y_'//YRECFM
760           YCOMMENTUNIT='W/m2'
761           CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWBU(:,JSW,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
762           !
763         ENDDO
764         !
765       ENDIF
766       !
767       YRECFM='FMU_P'
768       YCOMMENT='X_Y_'//YRECFM
769       YCOMMENTUNIT='Pa'
770       CALL WRITE_SURF_FIELD2D(HPROGRAM,XFMU(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
771       !
772       YRECFM='FMV_P'
773       YCOMMENT='X_Y_'//YRECFM
774       YCOMMENTUNIT='Pa'
775       CALL WRITE_SURF_FIELD2D(HPROGRAM,XFMV(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
776       !
777     END IF
778     !
779     !*       12.    Specific Energy fluxes :(for each patch)
780     !               ----------------------------------------
781     !
782     IF (LSURF_EVAP_BUDGET) THEN
783       !
784       YRECFM='LEG_P'
785       YCOMMENT='X_Y_'//YRECFM
786       YCOMMENTUNIT='W/m2'
787       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEG(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
788       !
789       YRECFM='LEGI_P'
790       YCOMMENT='X_Y_'//YRECFM
791       YCOMMENTUNIT='W/m2'
792       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEGI(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
793       !
794       YRECFM='LEV_P'
795       YCOMMENT='X_Y_'//YRECFM
796       YCOMMENTUNIT='W/m2'
797       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEV(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
798       !
799       YRECFM='LES_P'
800       YCOMMENT='X_Y_'//YRECFM
801       YCOMMENTUNIT='W/m2'
802       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLES(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
803       !
804       IF(TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO')THEN  
805         YRECFM='LESL_P'
806         YCOMMENT='X_Y_'//YRECFM
807         YCOMMENTUNIT='W/m2'
808         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLESL(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
809       ENDIF
810       !      
811       YRECFM='LER_P'
812       YCOMMENT='X_Y_'//YRECFM
813       YCOMMENTUNIT='W/m2'
814       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLER(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
815       !
816       YRECFM='LETR_P'
817       YCOMMENT='X_Y_'//YRECFM
818       YCOMMENTUNIT='W/m2'
819       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLETR(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
820       !
821       YRECFM='EVAP_P'
822       YCOMMENT='X_Y_'//YRECFM
823       YCOMMENTUNIT='Kg/m2/s'
824       CALL WRITE_SURF_FIELD2D(HPROGRAM,XEVAP(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
825       !
826       YRECFM='DRAIN_P'
827       YCOMMENT='X_Y_'//YRECFM
828       YCOMMENTUNIT='Kg/m2/s'
829       CALL WRITE_SURF_FIELD2D(HPROGRAM,XDRAIN(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
830       !
831       YRECFM='RUNOFF_P'
832       YCOMMENT='X_Y_'//YRECFM
833       YCOMMENTUNIT='Kg/m2/s'
834       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRUNOFF(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
835       !
836       IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
837         YRECFM='HORTON_P'
838         YCOMMENT='X_Y_'//YRECFM
839         YCOMMENTUNIT='Kg/m2/s'
840         CALL WRITE_SURF_FIELD2D(HPROGRAM,XHORT(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
841       ENDIF
842       !
843       YRECFM='DRIVEG_P'
844       YCOMMENT='X_Y_'//YRECFM
845       YCOMMENTUNIT='Kg/m2/s'
846       CALL WRITE_SURF_FIELD2D(HPROGRAM,XDRIP(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
847       !
848       YRECFM='RRVEG_P'
849       YCOMMENT='X_Y_'//YRECFM
850       YCOMMENTUNIT='Kg/m2/s'
851       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRRVEG(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
852       !
853       YRECFM='SNOMLT_P'
854       YCOMMENT='X_Y_'//YRECFM
855       YCOMMENTUNIT='Kg/m2/s'
856       CALL WRITE_SURF_FIELD2D(HPROGRAM,XMELT(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
857       !
858       IF(LAGRIP)THEN
859         YRECFM='IRRIG_P'
860         YCOMMENT='X_Y_'//YRECFM
861         YCOMMENTUNIT='Kg/m2/s'
862         CALL WRITE_SURF_FIELD2D(HPROGRAM,XIRRIG_FLUX(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
863       ENDIF
864       !      
865       IF(LFLOOD)THEN
866         !
867         YRECFM='IFLOOD_P'
868         YCOMMENT='X_Y_'//YRECFM
869         YCOMMENTUNIT='Kg/m2/s'
870         CALL WRITE_SURF_FIELD2D(HPROGRAM,XIFLOOD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
871         !
872         YRECFM='PFLOOD_P'
873         YCOMMENT='X_Y_'//YRECFM
874         YCOMMENTUNIT='Kg/m2/s'
875         CALL WRITE_SURF_FIELD2D(HPROGRAM,XPFLOOD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
876         !
877         YRECFM='LEF_P'
878         YCOMMENT='X_Y_'//YRECFM
879         YCOMMENTUNIT='W/m2'
880         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLE_FLOOD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
881         !
882         YRECFM='LEIF_P'
883         YCOMMENT='X_Y_'//YRECFM
884         YCOMMENTUNIT='W/m2'
885         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEI_FLOOD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
886         !
887       ENDIF
888       !
889       IF(CPHOTO/='NON')THEN
890         !
891         YRECFM='GPP_P'
892         YCOMMENT='gross primary production per patch'
893         YCOMMENTUNIT='kgCO2/m2/s'
894         CALL WRITE_SURF_FIELD2D(HPROGRAM,XGPP(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
895         !
896         YRECFM='R_AUTO_P'
897         YCOMMENT='autotrophic respiration per patch'
898         YCOMMENTUNIT='kgCO2/m2/s'
899         CALL WRITE_SURF_FIELD2D(HPROGRAM,XRESP_AUTO(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
900         !
901         YRECFM='R_ECO_P'
902         YCOMMENT='ecosystem respiration per patch'
903         YCOMMENTUNIT='kgCO2/m2/s'
904         CALL WRITE_SURF_FIELD2D(HPROGRAM,XRESP_ECO(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
905         !
906       ENDIF
907       !
908       IF(LWATER_BUDGET)THEN 
909         !
910         YRECFM='DWG_P'
911         YCOMMENT='change in liquid soil moisture per patch'
912         YCOMMENTUNIT='Kg/m2/s'
913         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWG(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
914         !
915         YRECFM='DWGI_P'
916         YCOMMENT='change in solid soil moisture per patch'
917         YCOMMENTUNIT='Kg/m2/s'
918         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWGI(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
919         !
920         YRECFM='DWR_P'
921         YCOMMENT='change in water on canopy per patch'
922         YCOMMENTUNIT='Kg/m2/s'
923         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWR(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
924         !
925         YRECFM='DSWE_P'
926         YCOMMENT='change in snow water equivalent per patch'
927         YCOMMENTUNIT='Kg/m2/s'
928         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDSWE(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
929         !
930         YRECFM='WATBUD_P'
931         YCOMMENT='isba water budget as residue per patch'
932         YCOMMENTUNIT='Kg/m2/s'
933         CALL WRITE_SURF_FIELD2D(HPROGRAM,XWATBUD(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
934         !
935       ENDIF
936       !      
937     ENDIF
938     !
939     !*       13.    surface temperature parameters at 2 and 10 meters (for each patch):
940     !               -------------------------------------------------------------------
941     !
942     IF (N2M>=1) THEN
943       !
944       YRECFM='T2M_P'
945       YCOMMENT='X_Y_'//YRECFM
946       YCOMMENTUNIT='K'
947       CALL WRITE_SURF_FIELD2D(HPROGRAM,XT2M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
948       !
949       YRECFM='Q2M_P'
950       YCOMMENT='X_Y_'//YRECFM
951       YCOMMENTUNIT='KG/KG'
952       CALL WRITE_SURF_FIELD2D(HPROGRAM,XQ2M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
953       !
954       YRECFM='HU2M_P'
955       YCOMMENT='X_Y_'//YRECFM
956       YCOMMENTUNIT='PERCENT'
957       CALL WRITE_SURF_FIELD2D(HPROGRAM,XHU2M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
958       !
959       YRECFM='ZON10M_P'
960       YCOMMENT='X_Y_'//YRECFM
961       YCOMMENTUNIT='M/S'
962       CALL WRITE_SURF_FIELD2D(HPROGRAM,XZON10M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
963       !
964       YRECFM='MER10M_P'
965       YCOMMENT='X_Y_'//YRECFM
966       YCOMMENTUNIT='M/S'
967       CALL WRITE_SURF_FIELD2D(HPROGRAM,XMER10M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
968       !
969       YRECFM='W10M_P'
970       YCOMMENT='X_Y_'//YRECFM
971       YCOMMENTUNIT='M/S'
972       CALL WRITE_SURF_FIELD2D(HPROGRAM,XWIND10M(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
973       !
974     END IF
975     !
976     !*       14.    Cumulated Energy fluxes :(for each patch)
977     !               -----------------------------------------
978     !
979     IF (LSURF_BUDGETC) THEN
980       !
981       YRECFM='LEGC_P'
982       YCOMMENT='X_Y_'//YRECFM
983       YCOMMENTUNIT='J/m2'
984       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEGC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
985       !
986       YRECFM='LEGIC_P'
987       YCOMMENT='X_Y_'//YRECFM
988       YCOMMENTUNIT='J/m2'
989       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEGIC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
990       !
991       YRECFM='LEVC_P'
992       YCOMMENT='X_Y_'//YRECFM
993       YCOMMENTUNIT='J/m2'
994       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEVC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
995       !
996       YRECFM='LESC_P'
997       YCOMMENT='X_Y_'//YRECFM
998       YCOMMENTUNIT='J/m2'
999       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLESC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1000       !
1001       IF(TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO')THEN  
1002         YRECFM='LESLC_P'
1003         YCOMMENT='X_Y_'//YRECFM
1004         YCOMMENTUNIT='J/m2'
1005         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLESLC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1006       ENDIF      
1007       !
1008       YRECFM='LERC_P'
1009       YCOMMENT='X_Y_'//YRECFM
1010       YCOMMENTUNIT='J/m2'
1011       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLERC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1012       !
1013       YRECFM='LETRC_P'
1014       YCOMMENT='X_Y_'//YRECFM
1015       YCOMMENTUNIT='J/m2'
1016       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLETRC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1017       !
1018       YRECFM='EVAPC_P'
1019       YCOMMENT='X_Y_'//YRECFM
1020       YCOMMENTUNIT='Kg/m2'
1021       CALL WRITE_SURF_FIELD2D(HPROGRAM,XEVAPC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1022       !
1023       YRECFM='DRAINC_P'
1024       YCOMMENT='X_Y_'//YRECFM
1025       YCOMMENTUNIT='Kg/m2'
1026       CALL WRITE_SURF_FIELD2D(HPROGRAM,XDRAINC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1027       !
1028       YRECFM='RUNOFFC_P'
1029       YCOMMENT='X_Y_'//YRECFM
1030       YCOMMENTUNIT='Kg/m2'
1031       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRUNOFFC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1032       !
1033       IF(CHORT=='SGH'.OR.CISBA=='DIF')THEN
1034         YRECFM='HORTONC_P'
1035         YCOMMENT='X_Y_'//YRECFM
1036         YCOMMENTUNIT='Kg/m2'
1037         CALL WRITE_SURF_FIELD2D(HPROGRAM,XHORTC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1038       ENDIF
1039       !
1040       YRECFM='DRIVEGC_P'
1041       YCOMMENT='X_Y_'//YRECFM
1042       YCOMMENTUNIT='Kg/m2'
1043       CALL WRITE_SURF_FIELD2D(HPROGRAM,XDRIPC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1044       !
1045       YRECFM='RRVEGC_P'
1046       YCOMMENT='X_Y_'//YRECFM
1047       YCOMMENTUNIT='Kg/m2'
1048       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRRVEGC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1049       !
1050       YRECFM='SNOMLTC_P'
1051       YCOMMENT='X_Y_'//YRECFM
1052       YCOMMENTUNIT='Kg/m2'
1053       CALL WRITE_SURF_FIELD2D(HPROGRAM,XMELTC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1054       !
1055       IF(LAGRIP)THEN
1056         YRECFM='IRRIGC_P'
1057         YCOMMENT='X_Y_'//YRECFM
1058         YCOMMENTUNIT='Kg/m2'
1059         CALL WRITE_SURF_FIELD2D(HPROGRAM,XIRRIG_FLUXC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1060       ENDIF      
1061       !
1062       IF(LGLACIER)THEN
1063         YRECFM='ICE_FC_P'
1064         YCOMMENT='X_Y_'//YRECFM
1065         YCOMMENTUNIT='Kg/m2'
1066         CALL WRITE_SURF_FIELD2D(HPROGRAM,XICEFLUXC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1067       ENDIF
1068       !
1069       IF(LFLOOD)THEN
1070         !        
1071         YRECFM='IFLOODC_P'
1072         YCOMMENT='X_Y_'//YRECFM
1073         YCOMMENTUNIT='Kg/m2/s'
1074         CALL WRITE_SURF_FIELD2D(HPROGRAM,XIFLOODC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1075         !
1076         YRECFM='PFLOODC_P'
1077         YCOMMENT='X_Y_'//YRECFM
1078         YCOMMENTUNIT='Kg/m2/s'
1079         CALL WRITE_SURF_FIELD2D(HPROGRAM,XPFLOODC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1080         !
1081         YRECFM='LEFC_P'
1082         YCOMMENT='X_Y_'//YRECFM
1083         YCOMMENTUNIT='W/m2'
1084         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLE_FLOODC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1085         !
1086         YRECFM='LEIFC_P'
1087         YCOMMENT='X_Y_'//YRECFM
1088         YCOMMENTUNIT='W/m2'
1089         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEI_FLOODC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1090         !
1091       ENDIF
1092       !
1093       YRECFM='RNC_P'
1094       YCOMMENT='X_Y_'//YRECFM
1095       YCOMMENTUNIT='J/m2'
1096       CALL WRITE_SURF_FIELD2D(HPROGRAM,XRNC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1097       !
1098       YRECFM='HC_P'
1099       YCOMMENT='X_Y_'//YRECFM
1100       YCOMMENTUNIT='J/m2'
1101       CALL WRITE_SURF_FIELD2D(HPROGRAM,XHC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1102       !
1103       YRECFM='LEC_P'
1104       YCOMMENT='X_Y_'//YRECFM
1105       YCOMMENTUNIT='J/m2'
1106       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1107       !
1108       YRECFM='LEIC_P'
1109       YCOMMENT='X_Y_'//YRECFM
1110       YCOMMENTUNIT='J/m2'
1111       CALL WRITE_SURF_FIELD2D(HPROGRAM,XLEIC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1112       !
1113       YRECFM='GFLUXC_P'
1114       YCOMMENT='X_Y_'//YRECFM
1115       YCOMMENTUNIT='J/m2'
1116       CALL WRITE_SURF_FIELD2D(HPROGRAM,XGFLUXC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1117       !
1118       IF (LRAD_BUDGET) THEN
1119         !
1120         YRECFM='SWDC_P'
1121         YCOMMENT='X_Y_'//YRECFM
1122         YCOMMENTUNIT='J/m2'
1123         CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWDC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1124         !
1125         YRECFM='SWUC_P'
1126         YCOMMENT='X_Y_'//YRECFM
1127         YCOMMENTUNIT='J/m2'
1128         CALL WRITE_SURF_FIELD2D(HPROGRAM,XSWUC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1129         !
1130         YRECFM='LWDC_P'
1131         YCOMMENT='X_Y_'//YRECFM
1132         YCOMMENTUNIT='J/m2'
1133         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLWDC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1134         !
1135         YRECFM='LWUC_P'
1136         YCOMMENT='X_Y_'//YRECFM
1137         YCOMMENTUNIT='J/m2'
1138         CALL WRITE_SURF_FIELD2D(HPROGRAM,XLWUC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1139         !
1140       ENDIF
1141       !
1142       YRECFM='FMUC_P'
1143       YCOMMENT='X_Y_'//YRECFM
1144       YCOMMENTUNIT='Pa.s'
1145       CALL WRITE_SURF_FIELD2D(HPROGRAM,XFMUC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1146       !
1147       YRECFM='FMVC_P'
1148       YCOMMENT='X_Y_'//YRECFM
1149       YCOMMENTUNIT='Pa.s'
1150       CALL WRITE_SURF_FIELD2D(HPROGRAM,XFMVC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1151       !
1152       IF(CPHOTO/='NON')THEN
1153         !
1154         YRECFM='GPPC_P'
1155         YCOMMENT='cumulated gross primary production per patch'
1156         YCOMMENTUNIT='kgCO2/m2'
1157         CALL WRITE_SURF_FIELD2D(HPROGRAM,XGPPC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1158         !
1159         YRECFM='RC_AUTO_P'
1160         YCOMMENT='cumulated autotrophic respiration per patch'
1161         YCOMMENTUNIT='kgCO2/m2'
1162         CALL WRITE_SURF_FIELD2D(HPROGRAM,XRESPC_AUTO(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1163         !
1164         YRECFM='RC_ECO_P'
1165         YCOMMENT='cumulated ecosystem respiration per patch'
1166         YCOMMENTUNIT='kgCO2/m2'
1167         CALL WRITE_SURF_FIELD2D(HPROGRAM,XRESPC_ECO(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1168         !
1169       ENDIF
1170       !  
1171       IF(LWATER_BUDGET .OR. (LSURF_BUDGETC .AND. .NOT.LRESET_BUDGETC))THEN 
1172         !
1173         YRECFM='DWGC_P'
1174         YCOMMENT='cumulated change in liquid soil moisture per patch'
1175         YCOMMENTUNIT='Kg/m2'
1176         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWGC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1177         !
1178         YRECFM='DWGIC_P'
1179         YCOMMENT='cumulated change in solid soil moisture per patch'
1180         YCOMMENTUNIT='Kg/m2'
1181         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWGIC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1182         !
1183         YRECFM='DWRC_P'
1184         YCOMMENT='cumulated change in water soil moisture per patch'
1185         YCOMMENTUNIT='Kg/m2'
1186         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDWRC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1187         !
1188         YRECFM='DSWEC_P'
1189         YCOMMENT='cumulated change in snow water equivalent per patch'
1190         YCOMMENTUNIT='Kg/m2'
1191         CALL WRITE_SURF_FIELD2D(HPROGRAM,XDSWEC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1192         !
1193         YRECFM='WATBUDC_P'
1194         YCOMMENT='cumulated isba water budget as residue per patch'
1195         YCOMMENTUNIT='Kg/m2'
1196         CALL WRITE_SURF_FIELD2D(HPROGRAM,XWATBUDC(:,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1197         !
1198       ENDIF
1199       !      
1200     ENDIF
1201     !-------------------------------------------------------------------------------
1202 ENDIF
1203 !User want (or not) patch output
1204 !-------------------------------------------------------------------------------
1205 !
1206 !*       15.     chemical diagnostics:
1207 !               --------------------
1208 !
1209 IF (NBEQ>0 .AND. CCH_DRY_DEP=="WES89 ") THEN
1210   !
1211   DO JSV = 1,SIZE(CCH_NAMES,1)
1212     YRECFM='DV_NAT_'//TRIM(CCH_NAMES(JSV))
1213     WRITE(YCOMMENT,'(A13,I3.3)')'DV_NAT_',JSV
1214     YCOMMENTUNIT='m/s'
1215     CALL WRITE_SURF_FIELD2D(HPROGRAM,XDEP(:,JSV,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1216   END DO
1217   !
1218 ENDIF
1219 !
1220 IF (NBEQ>0 .AND. LCH_BIO_FLUX) THEN
1221   !
1222   IF (ASSOCIATED(XFISO)) THEN
1223     YRECFM='FISO'
1224     WRITE(YCOMMENT,'(A21)')'FISO (molecules/m2/s)'
1225     CALL WRITE_SURF(HPROGRAM,YRECFM,XFISO(:),IRESP,HCOMMENT=YCOMMENT)
1226   END IF
1227   !
1228   IF (ASSOCIATED(XFISO)) THEN
1229     YRECFM='FMONO'
1230     WRITE(YCOMMENT,'(A22)')'FMONO (molecules/m2/s)'
1231     CALL WRITE_SURF(HPROGRAM,YRECFM,XFMONO(:),IRESP,HCOMMENT=YCOMMENT)
1232   END IF
1233   !
1234 ENDIF
1235 !
1236 IF (LCH_NO_FLUX) THEN
1237   IF (ASSOCIATED(XNOFLUX)) THEN
1238     YRECFM='NOFLUX'
1239     WRITE(YCOMMENT,'(A21)')'NOFLUX (molecules/m2/s)'
1240     CALL WRITE_SURF(HPROGRAM,YRECFM,XNOFLUX(:),IRESP,HCOMMENT=YCOMMENT)
1241   END IF
1242 END IF
1243 !
1244 IF (NDSTEQ > 0)THEN
1245   !
1246   DO JSV = 1,NDSTMDE ! for all dust modes
1247     WRITE(YRECFM,'(A7,I3.3)')'FLX_DST',JSV
1248     YCOMMENT='X_Y_'//YRECFM
1249     YCOMMENTUNIT='kg/m2/s'
1250     CALL WRITE_SURF_FIELD2D(HPROGRAM,XSFDST(:,JSV,:),YRECFM,YCOMMENT,YCOMMENTUNIT)
1251   END DO
1252   !
1253 ENDIF
1254 !
1255 !-------------------------------------------------------------------------------
1256 !
1257 !         End of IO
1258 !
1259  CALL END_IO_SURF_n(HPROGRAM)
1260 IF (LHOOK) CALL DR_HOOK('WRITE_DIAG_SEB_ISBA_N',1,ZHOOK_HANDLE)
1261 !
1262 CONTAINS
1263 !
1264 !-------------------------------------------------------------------------------
1265 !
1266 SUBROUTINE PROVAR_TO_DIAG
1267 !
1268 REAL, DIMENSION(SIZE(XTG,1))             :: ZPATCH, ZWORK
1269 REAL, DIMENSION(SIZE(XWG,1),SIZE(XWG,2)) :: ZWG
1270 REAL, DIMENSION(SIZE(XWG,1),SIZE(XWG,2)) :: ZWGI
1271 REAL, DIMENSION(SIZE(XWG,1),SIZE(XWG,2)) :: ZMOIST
1272 REAL, DIMENSION(SIZE(XWG,1),SIZE(XWG,2)) :: ZICE
1273 REAL, DIMENSION(SIZE(XTG,1),SIZE(XTG,2)) :: ZTG
1274 REAL, DIMENSION(SIZE(XDG,1),SIZE(XDG,2)) :: ZDG_TOT
1275 REAL, DIMENSION(SIZE(XDG,1),SIZE(XDG,2),SIZE(XDG,3)) :: ZDG
1276 !
1277 REAL, DIMENSION(SIZE(XDG,1),NNBIOMASS)   :: ZBIOMASS
1278 REAL, DIMENSION(SIZE(XDG,1),NNSOILCARB)  :: ZSOILCARB
1279 REAL, DIMENSION(SIZE(XDG,1),NNLITTLEVS)  :: ZLIGNIN_STRUC
1280 REAL, DIMENSION(SIZE(XDG,1),NNLITTER,NNLITTLEVS)  :: ZLITTER
1281 !
1282  CHARACTER(LEN=8 ) :: YUNIT
1283  CHARACTER(LEN=4 ) :: YLVL
1284 INTEGER :: JLAYER, JPATCH, JJ, INI, IWORK, IDEPTH
1285 REAL(KIND=JPRB) :: ZHOOK_HANDLE
1286 !
1287 IF (LHOOK) CALL DR_HOOK('WRITE_DIAG_SEB_ISBA_N:PROVAR_TO_DIAG',0,ZHOOK_HANDLE)
1288 !
1289 INI=SIZE(XDG,1)
1290 !
1291 ! * soil temperatures (K)
1292 !
1293 IF(LTEMP_ARP)THEN
1294   IWORK=NTEMPLAYER_ARP
1295 ELSEIF(CISBA/='DIF')THEN
1296   IWORK=NGROUND_LAYER-1
1297 ELSE
1298   IWORK=NGROUND_LAYER
1299 ENDIF
1300 !
1301 ZTG(:,:)=0.0
1302 DO JPATCH=1,NPATCH
1303    DO JLAYER=1,IWORK
1304       DO JJ=1,INI 
1305          ZTG(JJ,JLAYER) = ZTG(JJ,JLAYER) + XPATCH(JJ,JPATCH) * XTG(JJ,JLAYER,JPATCH)
1306       ENDDO
1307    ENDDO
1308 ENDDO
1309 !
1310 DO JLAYER=1,IWORK
1311   WRITE(YLVL,'(I4)') JLAYER
1312   YRECFM='TG'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1313   YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1314   YCOMMENT='X_Y_'//YRECFM//' (K)'
1315   CALL WRITE_SURF(HPROGRAM,YRECFM,ZTG(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)
1316 END DO
1317 !
1318 ! * Compute soil liquid and ice water content (kg/m2 and m3/m3) 
1319 !
1320 ZWG (:,:)=0.0
1321 ZWGI(:,:)=0.0
1322 ZDG_TOT(:,:)=0.0
1323 ZMOIST (:,:)=XUNDEF
1324 ZICE   (:,:)=XUNDEF
1325 !  
1326 IF(CISBA=='DIF')THEN
1327   !
1328   IWORK = NWG_SIZE
1329   !
1330   DO JPATCH=1,NPATCH
1331      DO JLAYER=1,NGROUND_LAYER
1332         DO JJ=1,INI 
1333 !
1334 !          liquid and ice water content
1335            IDEPTH=NWG_LAYER(JJ,JPATCH)
1336            IF(JLAYER<=IDEPTH)THEN    
1337              ZWG    (JJ,JLAYER)=ZWG    (JJ,JLAYER)+XPATCH(JJ,JPATCH)*XWG (JJ,JLAYER,JPATCH)*XDZG(JJ,JLAYER,JPATCH)
1338              ZWGI   (JJ,JLAYER)=ZWGI   (JJ,JLAYER)+XPATCH(JJ,JPATCH)*XWGI(JJ,JLAYER,JPATCH)*XDZG(JJ,JLAYER,JPATCH)
1339              ZDG_TOT(JJ,JLAYER)=ZDG_TOT(JJ,JLAYER)+XPATCH(JJ,JPATCH)*XDZG(JJ,JLAYER,JPATCH)
1340            ENDIF
1341 !                      
1342         ENDDO
1343      ENDDO
1344   ENDDO
1345 !  
1346 ELSE
1347   !  
1348   IWORK = NGROUND_LAYER
1349   !
1350   ZDG(:,1,:) = XDG(:,1,:)
1351   ZDG(:,2,:) = XDG(:,2,:)
1352   IF(CISBA=='3-L')THEN
1353     ZDG(:,3,:) = XDG(:,3,:)-XDG(:,2,:)
1354   ENDIF
1355 !
1356   DO JPATCH=1,NPATCH
1357      DO JLAYER=1,NGROUND_LAYER
1358         DO JJ=1,INI 
1359            ZWG    (JJ,JLAYER)=ZWG    (JJ,JLAYER)+XPATCH(JJ,JPATCH)*XWG (JJ,JLAYER,JPATCH)*ZDG(JJ,JLAYER,JPATCH)
1360            ZWGI   (JJ,JLAYER)=ZWGI   (JJ,JLAYER)+XPATCH(JJ,JPATCH)*XWGI(JJ,JLAYER,JPATCH)*ZDG(JJ,JLAYER,JPATCH)
1361            ZDG_TOT(JJ,JLAYER)=ZDG_TOT(JJ,JLAYER)+XPATCH(JJ,JPATCH)*ZDG(JJ,JLAYER,JPATCH)
1362         ENDDO
1363      ENDDO
1364   ENDDO
1365 !  
1366 ENDIF
1367 !
1368 WHERE(ZDG_TOT(:,:)>0.0)
1369       ZMOIST(:,:)=ZWG (:,:)*XRHOLW
1370       ZICE  (:,:)=ZWGI(:,:)*XRHOLW        
1371       ZWG   (:,:)=ZWG (:,:)/ZDG_TOT(:,:)
1372       ZWGI  (:,:)=ZWGI(:,:)/ZDG_TOT(:,:)
1373 ELSEWHERE
1374       ZMOIST(:,:)=XUNDEF
1375       ZICE  (:,:)=XUNDEF       
1376       ZWG   (:,:)=XUNDEF
1377       ZWGI  (:,:)=XUNDEF      
1378 ENDWHERE
1379 !
1380 ! * soil liquid water content (m3/m3) and soil moisture (kg/m2)
1381 !
1382 YUNIT=' (m3/m3)'
1383 DO JLAYER=1,IWORK
1384   WRITE(YLVL,'(I4)') JLAYER
1385   YRECFM='WG'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1386   YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1387   YCOMMENT='X_Y_'//YRECFM//YUNIT  
1388   CALL WRITE_SURF(HPROGRAM,YRECFM,ZWG(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)
1389 END DO
1390 !
1391 YUNIT=' (kg/m2)'
1392 DO JLAYER=1,IWORK
1393   WRITE(YLVL,'(I4)') JLAYER
1394   YRECFM='SOILM'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1395   YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1396   YCOMMENT='X_Y_'//YRECFM//YUNIT
1397   CALL WRITE_SURF(HPROGRAM,YRECFM,ZMOIST(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)
1398 END DO
1399 !
1400 ! * soil ice water content (m3/m3) and soil ice mass (kg/m2)
1401 !
1402 IWORK=NGROUND_LAYER
1403 IF(CISBA/='DIF')THEN
1404   IWORK=NGROUND_LAYER-1 ! No ice in the FR 3-layers
1405 ENDIF
1406 !
1407 YUNIT=' (m3/m3)'
1408 DO JLAYER=1,IWORK
1409   WRITE(YLVL,'(I4)') JLAYER
1410   YRECFM='WGI'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1411   YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1412   YCOMMENT='X_Y_'//YRECFM//' (kg/m2)'
1413   CALL WRITE_SURF(HPROGRAM,YRECFM,ZWGI(:,JLAYER),IRESP,HCOMMENT=YCOMMENT) 
1414 END DO   
1415 !
1416 YUNIT=' (kg/m2)'
1417 DO JLAYER=1,IWORK
1418   WRITE(YLVL,'(I4)') JLAYER
1419   YRECFM='SOILI'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1420   YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1421   YCOMMENT='X_Y_'//YRECFM//YUNIT
1422   CALL WRITE_SURF(HPROGRAM,YRECFM,ZICE(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)
1423 END DO
1424 !
1425 ! * water intercepted on leaves (kg/m2)
1426 !
1427 ZWORK(:)=0.0
1428 DO JPATCH=1,NPATCH
1429   ZWORK(:) = ZWORK(:) + XPATCH(:,JPATCH) * XWR(:,JPATCH)
1430 ENDDO
1431 !
1432 YRECFM='WR_ISBA'
1433 YCOMMENT='X_Y_'//YRECFM//' (kg/m2)'
1434  CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1435 !
1436 ! * Glacier ice storage (semi-prognostic) (kg/m2)
1437 !
1438 IF(LGLACIER)THEN
1439   !
1440   ZWORK(:)=0.0
1441   DO JPATCH=1,NPATCH
1442     ZWORK(:) = ZWORK(:) + XPATCH(:,JPATCH) * XICE_STO(:,JPATCH)
1443   ENDDO    
1444   !
1445   YRECFM='ICE_STO_ISBA'
1446   YCOMMENT='X_Y_'//YRECFM//' (kg/m2)'
1447   CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1448   !
1449 ENDIF
1450 !
1451 ! * Snow albedo (-) 
1452 !
1453 ZPATCH(:) = 0.0
1454 ZWORK (:) = 0.0
1455 DO JPATCH=1,NPATCH
1456   WHERE(TSNOW%ALB(:,JPATCH)/=XUNDEF)
1457     ZWORK (:) = ZWORK(:)  + XPATCH(:,JPATCH) * TSNOW%ALB(:,JPATCH)
1458     ZPATCH(:) = ZPATCH(:) + XPATCH(:,JPATCH)
1459   ENDWHERE
1460 ENDDO
1461 !
1462 WHERE(ZPATCH(:)>0.0)
1463   ZWORK(:) = ZWORK(:) / ZPATCH(:)
1464 ELSEWHERE
1465   ZWORK(:) = XUNDEF
1466 ENDWHERE
1467 !
1468 YRECFM='ASNOW_ISBA'
1469 YCOMMENT='X_Y_'//YRECFM//' (-)'
1470  CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1471 !  
1472 IF(TSNOW%SCHEME=='3-L' .OR. TSNOW%SCHEME=='CRO')THEN
1473   !
1474   ! * Snow reservoir (kg/m2) by layer
1475   !
1476   DO JLAYER = 1,TSNOW%NLAYER
1477     !
1478     ZWORK(:)=0.0
1479     DO JPATCH=1,NPATCH
1480       ZWORK(:) = ZWORK(:) + XPATCH(:,JPATCH) * TSNOW%WSNOW(:,JLAYER,JPATCH)
1481     ENDDO
1482     !
1483     WRITE(YLVL,'(I4)') JLAYER
1484     YRECFM='WSNOW_'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1485     YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1486     YCOMMENT='X_Y_'//YRECFM//' (kg/m2)'
1487     CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1488     !
1489   ENDDO
1490   !
1491   ! * Snow depth (m)
1492   !
1493   DO JLAYER = 1,TSNOW%NLAYER
1494     !
1495     ZWORK(:)=0.0
1496     DO JPATCH=1,NPATCH
1497       ZWORK(:) = ZWORK(:) + XPATCH(:,JPATCH) * TSNOW%WSNOW(:,JLAYER,JPATCH)/TSNOW%RHO(:,JLAYER,JPATCH)
1498     ENDDO
1499     !
1500     WRITE(YLVL,'(I4)') JLAYER
1501     YRECFM='DSNOW_'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1502     YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1503     YCOMMENT='X_Y_'//YRECFM//' (kg/m2)'
1504     CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1505     !
1506   ENDDO
1507   !
1508   ! * Snow temperature (k)
1509   !    
1510   DO JLAYER = 1,TSNOW%NLAYER
1511     !
1512     ZWORK (:) = 0.0
1513     ZPATCH(:) = 0.0
1514     DO JPATCH=1,NPATCH
1515       WHERE(TSNOW%WSNOW(:,JLAYER,JPATCH)>0.)
1516         ZWORK (:) = ZWORK (:) + XPATCH(:,JPATCH) * TSNOW%TEMP(:,JLAYER,JPATCH) 
1517         ZPATCH(:) = ZPATCH(:) + XPATCH(:,JPATCH)
1518       ENDWHERE
1519     ENDDO
1520     !
1521     WHERE(ZPATCH(:)>0.0)
1522       ZWORK(:) = ZWORK(:) / ZPATCH(:)
1523     ELSEWHERE
1524       ZWORK(:) = XUNDEF
1525     ENDWHERE
1526     !
1527     WRITE(YLVL,'(I4)') JLAYER
1528     YRECFM='TSNOW_'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1529     YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1530     YCOMMENT='X_Y_'//YRECFM//' (K)'
1531     CALL WRITE_SURF(HPROGRAM,YRECFM,ZWORK(:),IRESP,HCOMMENT=YCOMMENT)
1532     !
1533   ENDDO
1534   !
1535 ENDIF
1536 !
1537 ! * Isba-Ags biomass reservoir
1538 !
1539 ! * Isba-Ags biomass reservoir
1540 !
1541 IF(CPHOTO=='NIT'.OR.CPHOTO=='NCB')THEN
1542 !
1543   ZBIOMASS(:,:)=0.0
1544   DO JPATCH=1,NPATCH
1545      DO JLAYER=1,NNBIOMASS
1546         DO JJ=1,INI 
1547          ZBIOMASS(JJ,JLAYER) = ZBIOMASS(JJ,JLAYER) + XPATCH(JJ,JPATCH) * XBIOMASS(JJ,JLAYER,JPATCH)
1548         ENDDO
1549      ENDDO
1550   ENDDO
1551 !
1552   DO JLAYER = 1,NNBIOMASS
1553     WRITE(YLVL,'(I4)') JLAYER
1554     YRECFM='BIOM'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1555     YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1556     YCOMMENT='X_Y_'//YRECFM//' (kgDM/m2)'
1557     CALL WRITE_SURF(HPROGRAM,YRECFM,ZBIOMASS(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)  
1558   ENDDO
1559 !
1560 ENDIF
1561 !
1562 ! * Isba-CC carbon reservoir
1563 !
1564 IF(CRESPSL=='CNT')THEN
1565 !
1566   ZLITTER(:,:,:)=0.0
1567   ZLIGNIN_STRUC(:,:)=0.0
1568   DO JPATCH=1,NPATCH
1569      DO JLAYER=1,NNLITTLEVS
1570        DO JJ=1,INI 
1571           ZLITTER(JJ,1,JLAYER) = ZLITTER(JJ,1,JLAYER) + XPATCH(JJ,JPATCH) * XLITTER(JJ,1,JLAYER,JPATCH)
1572           ZLITTER(JJ,2,JLAYER) = ZLITTER(JJ,2,JLAYER) + XPATCH(JJ,JPATCH) * XLITTER(JJ,2,JLAYER,JPATCH)
1573           ZLIGNIN_STRUC(JJ,JLAYER) = ZLIGNIN_STRUC(JJ,JLAYER) + XPATCH(JJ,JPATCH) * XLIGNIN_STRUC(JJ,JLAYER,JPATCH)
1574        ENDDO
1575     ENDDO
1576   ENDDO
1577 !       
1578   DO JLAYER=1,NNLITTLEVS
1579      WRITE(YLVL,'(I4)') JLAYER
1580      YRECFM='LIT1_'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1581      YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1582      YCOMMENT='X_Y_'//YRECFM//' (gC/m2)'
1583      CALL WRITE_SURF(HPROGRAM,YRECFM,ZLITTER(:,1,JLAYER),IRESP,HCOMMENT=YCOMMENT)  
1584      WRITE(YLVL,'(I4)') JLAYER
1585      YRECFM='LIT2_'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1586      YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1587      YCOMMENT='X_Y_'//YRECFM//' (gC/m2)'
1588      CALL WRITE_SURF(HPROGRAM,YRECFM,ZLITTER(:,2,JLAYER),IRESP,HCOMMENT=YCOMMENT)
1589      WRITE(YLVL,'(I4)') JLAYER
1590      YRECFM='LIGSTR'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1591      YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1592      YCOMMENT='X_Y_'//YRECFM//' (-)'
1593      CALL WRITE_SURF(HPROGRAM,YRECFM,ZLIGNIN_STRUC(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)      
1594   END DO
1595 !
1596   ZSOILCARB(:,:)=0.0
1597   DO JPATCH=1,NPATCH
1598      DO JLAYER=1,NNSOILCARB
1599        DO JJ=1,INI 
1600           ZSOILCARB(JJ,JLAYER) = ZSOILCARB(JJ,JLAYER) + XPATCH(JJ,JPATCH) * XSOILCARB(JJ,JLAYER,JPATCH)
1601        ENDDO
1602     ENDDO
1603   ENDDO
1604 !
1605   DO JLAYER = 1,NNSOILCARB
1606     WRITE(YLVL,'(I4)') JLAYER
1607     YRECFM='SCARB'//ADJUSTL(YLVL(:LEN_TRIM(YLVL)))
1608     YRECFM=YRECFM(:LEN_TRIM(YRECFM))//'_ISBA'
1609     YCOMMENT='X_Y_'//YRECFM//' (gC/m2)'
1610     CALL WRITE_SURF(HPROGRAM,YRECFM,ZSOILCARB(:,JLAYER),IRESP,HCOMMENT=YCOMMENT)  
1611   ENDDO
1612 !
1613 ENDIF
1614 !
1615 IF (LHOOK) CALL DR_HOOK('WRITE_DIAG_SEB_ISBA_N:PROVAR_TO_DIAG',1,ZHOOK_HANDLE)
1616 !
1617 END SUBROUTINE PROVAR_TO_DIAG
1618 !
1619 END SUBROUTINE WRITE_DIAG_SEB_ISBA_n