source: branches/ORCHIDEE_2_2/ORCHIDEE/src_driver/orchideedriver.f90 @ 7256

Last change on this file since 7256 was 6319, checked in by josefine.ghattas, 5 years ago

Improvment for the ESM CO2 configuration:

  • Separate variable fco2_lu into 3 parts: fco2_lu, fco2_wh and fco2_ha
  • Move calculation of co2_flux from dt_sechiba time-step to daily time-step (in the part for do_slow)
  • Removed co2_flux and fco2_lu from stomate_intialize argument list. These variables were never used in the intialization phase.
  • Add co2_flux, and fco2_wh, fco2_ha to restart file
  • Corrected output unit for nee to be consistent with LMDZ and stomate output variables. It is now in kgC/m2/s.
  • Corrected output for znetco2
  • Added fCO2_fWoodharvest and fCO2_fHarvest as new possible tracers in LMDZ (intersurf).
  • Added diagnostic output for fCO2_fWoodharvest and fCO2_fHarvest
  1. Cadule
File size: 39.6 KB
Line 
1! =================================================================================================================================
2! PROGRAM       : orchideedriver
3!
4! CONTACT       : jan.polcher@lmd.jussieu.fr
5!
6! LICENCE      : IPSL (2016)
7! This software is governed by the CeCILL licence see ORCHIDEE/ORCHIDEE_CeCILL.LIC
8!
9!>\BRIEF      This is the main program for the new driver. This only organises the data and calls sechiba_main.
10!!            The main work is done in glogrid.f90 and forcing_tools.f90.
11!!
12!!\n DESCRIPTION: Call the various modules to get the forcing data and provide it to SECHIBA. The only complexity
13!!                is setting-up the domain decomposition and distributing the grid information.
14!!                The code is parallel from tip to toe using the domain decomposition inherited from LMDZ.
15!!
16!! RECENT CHANGE(S): None
17!!
18!! REFERENCE(S) :
19!!
20!! SVN          :
21!! $HeadURL:  $
22!! $Date:  $
23!! $Revision: $
24!! \n
25!_
26!================================================================================================================================
27!
28PROGRAM orchidedriver
29  !---------------------------------------------------------------------
30  !-
31  !-
32  !---------------------------------------------------------------------
33  USE defprec
34  USE netcdf
35  !
36  !
37  USE ioipsl_para
38  USE mod_orchidee_para
39  !
40  USE grid
41  USE time
42  USE timer
43  USE constantes
44  USE constantes_soil
45  USE forcing_tools
46  USE globgrd
47  !
48  USE sechiba
49  USE control
50  USE ioipslctrl
51  USE xios_orchidee
52  !
53  !-
54  IMPLICIT NONE
55  !-
56  CHARACTER(LEN=80) :: gridfilename
57  CHARACTER(LEN=80), DIMENSION(100) :: forfilename
58  INTEGER(i_std) :: nb_forcefile
59  CHARACTER(LEN=8)  :: model_guess
60  INTEGER(i_std)    :: iim_glo, jjm_glo, file_id
61  !-
62  INTEGER(i_std)    :: nbseg
63  REAL(r_std), ALLOCATABLE, DIMENSION(:,:) :: lon_glo, lat_glo, area_glo
64  REAL(r_std), ALLOCATABLE, DIMENSION(:,:) :: mask_glo
65  REAL(r_std), ALLOCATABLE, DIMENSION(:,:,:,:) :: corners_glo
66  REAL(r_std), ALLOCATABLE, DIMENSION(:,:,:) :: corners_lon, corners_lat
67  INTEGER(i_std) :: nbindex_g, kjpindex
68  INTEGER(i_std), ALLOCATABLE, DIMENSION(:) :: kindex, kindex_g
69  REAL(r_std), DIMENSION(2) :: zoom_lon, zoom_lat
70  !
71  ! Variables for the global grid available on all procs and used
72  ! to fill the ORCHIDEE variable on the root_proc
73  !
74  REAL(r_std), ALLOCATABLE, DIMENSION(:,:)    :: lalo_glo
75  REAL(r_std), ALLOCATABLE, DIMENSION(:)      :: contfrac_glo
76  CHARACTER(LEN=20)                           :: calendar
77  !-
78  !- Variables local to each processors.
79  !-
80  INTEGER(i_std) :: i, j, ik, in, nbdt, first_point
81  INTEGER(i_std) :: itau, itau_offset, itau_sechiba
82  REAL(r_std)    :: date0, date0_shifted, dt, julian
83  REAL(r_std)    :: date0_tmp, dt_tmp
84  INTEGER(i_std) :: nbdt_tmp
85  REAL(r_std)    :: timestep_interval(2), timestep_int_next(2)
86  !
87  INTEGER(i_std) :: rest_id, rest_id_stom
88  INTEGER(i_std) ::  hist_id, hist2_id, hist_id_stom, hist_id_stom_IPCC
89  REAL(r_std), ALLOCATABLE, DIMENSION(:,:) :: lalo_loc
90  INTEGER(i_std) :: iim, jjm, ier
91  REAL(r_std), ALLOCATABLE, DIMENSION(:,:) :: lon, lat
92  !-
93  !- input fields
94  !-
95  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: u             !! Lowest level wind speed
96  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: v             !! Lowest level wind speed
97  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: zlev_uv       !! Height of first layer
98  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: zlev_tq       !! Height of first layer
99  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: qair          !! Lowest level specific humidity
100  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: precip_rain   !! Rain precipitation
101  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: precip_snow   !! Snow precipitation
102  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: lwdown        !! Down-welling long-wave flux
103  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: swdown        !! Downwelling surface short-wave flux
104  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: sinang        !! cosine of solar zenith angle
105  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: temp_air      !! Air temperature in Kelvin
106  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: epot_air      !! Air potential energy
107  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: ccanopy       !! CO2 concentration in the canopy
108  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: petAcoef      !! Coeficients A from the PBL resolution
109  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: peqAcoef      !! One for T and another for q
110  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: petBcoef      !! Coeficients B from the PBL resolution
111  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: peqBcoef      !! One for T and another for q
112  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: cdrag         !! Cdrag
113  REAL(r_std), ALLOCATABLE, DIMENSION (:)             :: pb            !! Lowest level pressure
114  !-
115  !- output fields
116  !-
117  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: z0m           !! Surface roughness for momentum (m)
118  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: z0h           !! Surface roughness for heat (m)
119  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
120  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
121  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: tsol_rad      !! Radiative surface temperature
122  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: vevapp        !! Total of evaporation
123  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: temp_sol_new  !! New soil temperature
124  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: qsurf         !! Surface specific humidity
125  REAL(r_std), ALLOCATABLE, DIMENSION (:,:)          :: albedo        !! Albedo
126  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: fluxsens      !! Sensible chaleur flux
127  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: fluxlat       !! Latent chaleur flux
128  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: emis          !! Emissivity
129  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: netco2        !! netco2flux: Sum CO2 flux over PFTs
130  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: carblu        !! fco2_lu: Land Cover Change CO2 flux
131  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: carbwh        !! fco2_wh: Wood harvest CO2 flux
132  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: carbha        !! fco2_ha: Crop harvest CO2 flux
133  REAL(r_std), ALLOCATABLE, DIMENSION (:,:)          :: veget_diag    !! Fraction of vegetation type (unitless, 0-1)
134  REAL(r_std), ALLOCATABLE, DIMENSION (:,:)          :: lai_diag      !! Leaf area index (m^2 m^{-2}
135  REAL(r_std), ALLOCATABLE, DIMENSION (:,:)          :: height_diag   !! Vegetation Height (m)
136  !-
137  !-
138  !-
139  REAL(r_std) :: atmco2
140  REAL(r_std), ALLOCATABLE, DIMENSION (:)  :: u_tq, v_tq, swnet
141  LOGICAL :: lrestart_read = .TRUE. !! Logical for _restart_ file to read
142  LOGICAL :: lrestart_write = .FALSE. !! Logical for _restart_ file to write'
143  !
144  ! Timer variables
145  !
146  LOGICAL, PARAMETER :: timemeasure=.TRUE.
147  REAL(r_std) :: waitput_cputime=0.0, waitget_cputime=0.0, orchidee_cputime=0.0
148  REAL(r_std) :: waitput_walltime=0.0, waitget_walltime=0.0, orchidee_walltime=0.0
149  !
150  !
151  ! Print point
152  !
153!!  REAL(r_std), DIMENSION(2) :: testpt=(/44.8,-25.3/)
154!!  REAL(r_std), DIMENSION(2) :: testpt=(/44.8,-18.3/)
155!!  REAL(r_std), DIMENSION(2) :: testpt=(/-60.25,-5.25/)
156!!  REAL(r_std), DIMENSION(2) :: testpt=(/46.7,10.3/)
157!!  REAL(r_std), DIMENSION(2) :: testpt=(/0.25,49.25/)
158  ! Case when no ouput is desired.
159  REAL(r_std), DIMENSION(2) :: testpt=(/9999.99,9999.99/)
160  INTEGER(i_std) :: ktest
161
162  OFF_LINE_MODE = .TRUE. 
163
164  !-
165  !---------------------------------------------------------------------------------------
166  !-
167  !- Define MPI communicator
168  !-
169  !---------------------------------------------------------------------------------------
170  !-
171  !
172  ! Set parallel processing in ORCHIDEE
173  !
174  CALL Init_orchidee_para()
175  !
176  !====================================================================================
177  !
178  ! Start timer now that the paralelisation is initialized.
179  !
180  IF ( timemeasure ) THEN
181     CALL init_timer
182     CALL start_timer(timer_global)
183     CALL start_timer(timer_mpi)
184  ENDIF
185  !
186  !
187  !---------------------------------------------------------------------------------------
188  !-
189  !- Start the getconf processes
190  !-
191  !---------------------------------------------------------------------------------------
192  !-
193!!  CALL getin_name("run.def")
194  !-
195  !Config Key   = GRID_FILE
196  !Config Desc  = Name of file containing the forcing data
197  !Config If    = [-]
198  !Config Def   = grid_file.nc
199  !Config Help  = This is the name of the file from which we will read
200  !Config         or write into it the description of the grid from
201  !Config         the forcing file.
202  !Config         compliant.
203  !Config Units = [FILE]
204  !-
205  gridfilename='NONE'
206  CALL getin_p('GRID_FILE', gridfilename)
207  !-
208  forfilename(:)=" "
209  forfilename(1)='forcing_file.nc'
210  CALL getin_p('FORCING_FILE', forfilename)
211  !-
212  !- Define the zoom
213  !-
214  zoom_lon=(/-180,180/)
215  zoom_lat=(/-90,90/)
216  !
217  !Config Key   = LIMIT_WEST
218  !Config Desc  = Western limit of region
219  !Config If    = [-]
220  !Config Def   = -180.
221  !Config Help  = Western limit of the region we are
222  !Config         interested in. Between -180 and +180 degrees
223  !Config         The model will use the smalest regions from
224  !Config         region specified here and the one of the forcing file.
225  !Config Units = [Degrees]
226  !-
227  CALL getin_p('LIMIT_WEST',zoom_lon(1))
228  !-
229  !Config Key   = LIMIT_EAST
230  !Config Desc  = Eastern limit of region
231  !Config If    = [-]
232  !Config Def   = 180.
233  !Config Help  = Eastern limit of the region we are
234  !Config         interested in. Between -180 and +180 degrees
235  !Config         The model will use the smalest regions from
236  !Config         region specified here and the one of the forcing file.
237  !Config Units = [Degrees]
238  !-
239  CALL getin_p('LIMIT_EAST',zoom_lon(2))
240  !-
241  !Config Key   = LIMIT_NORTH
242  !Config Desc  = Northern limit of region
243  !Config If    = [-]
244  !Config Def   = 90.
245  !Config Help  = Northern limit of the region we are
246  !Config         interested in. Between +90 and -90 degrees
247  !Config         The model will use the smalest regions from
248  !Config         region specified here and the one of the forcing file.
249  !Config Units = [Degrees]
250  !-
251  CALL getin_p('LIMIT_NORTH',zoom_lat(2))
252  !-
253  !Config Key   = LIMIT_SOUTH
254  !Config Desc  = Southern limit of region
255  !Config If    = [-]
256  !Config Def   = -90.
257  !Config Help  = Southern limit of the region we are
258  !Config         interested in. Between 90 and -90 degrees
259  !Config         The model will use the smalest regions from
260  !Config         region specified here and the one of the forcing file.
261  !Config Units = [Degrees]
262  !-
263  CALL getin_p('LIMIT_SOUTH',zoom_lat(1))
264  IF ( (zoom_lon(1)+180 < EPSILON(zoom_lon(1))) .AND. (zoom_lon(2)-180 < EPSILON(zoom_lon(2))) .AND.&
265       &(zoom_lat(1)+90 < EPSILON(zoom_lat(1))) .AND. (zoom_lat(2)-90 < EPSILON(zoom_lat(2))) ) THEN
266     !
267     !Config Key   = WEST_EAST
268     !Config Desc  = Longitude interval to use from the forcing data
269     !Config If    = [-]
270     !Config Def   = -180, 180
271     !Config Help  = This function allows to zoom into the forcing data
272     !Config Units = [degrees east]
273     !-
274     CALL getin_p('WEST_EAST', zoom_lon)
275     !
276     !Config Key   = SOUTH_NORTH
277     !Config Desc  = Latitude interval to use from the forcing data
278     !Config If    = [-]
279     !Config Def   = -90, 90
280     !Config Help  = This function allows to zoom into the forcing data
281     !Config Units = [degrees north]
282     !-
283     CALL getin_p('SOUTH_NORTH', zoom_lat)
284  ENDIF
285  !-
286  !-
287  !- Get some basic variables from the run.def
288  !-
289  atmco2=350.
290  CALL getin_p('ATM_CO2',atmco2)
291  !
292  !====================================================================================
293  !-
294  !-
295  !- Get the grid on all processors.
296  !-
297  !---------------------------------------------------------------------------------------
298  !-
299  !- Read the grid, only on the root proc. from the forcing file using tools in the globgrd module.
300  !- The grid is then broadcast to all other broadcast.
301  !
302  nb_forcefile = 0
303  DO ik=1,100
304     IF ( INDEX(forfilename(ik), '.nc') > 0 ) nb_forcefile = nb_forcefile+1
305  ENDDO
306  !
307  IF ( is_root_prc) THEN
308     CALL globgrd_getdomsz(gridfilename, iim_glo, jjm_glo, nbindex_g, model_guess, file_id, forfilename, zoom_lon, zoom_lat)
309     nbseg = 4
310  ENDIF
311  !
312  CALL bcast(iim_glo)
313  CALL bcast(jjm_glo)
314  CALL bcast(nbindex_g)
315  CALL bcast(nbseg)
316  !-
317  !- Allocation of memory
318  !- variables over the entire grid (thus in x,y)
319  ALLOCATE(lon_glo(iim_glo, jjm_glo))
320  ALLOCATE(lat_glo(iim_glo, jjm_glo))
321  ALLOCATE(mask_glo(iim_glo, jjm_glo))
322  ALLOCATE(area_glo(iim_glo, jjm_glo))
323  ALLOCATE(corners_glo(iim_glo, jjm_glo, nbseg, 2))
324  !
325  ! Gathered variables
326  ALLOCATE(kindex_g(nbindex_g))
327  ALLOCATE(contfrac_glo(nbindex_g))
328  !-
329  IF ( is_root_prc) THEN
330     CALL globgrd_getgrid(file_id, iim_glo, jjm_glo, nbindex_g, model_guess, &
331          &               lon_glo, lat_glo, mask_glo, area_glo, corners_glo,&
332          &               kindex_g, contfrac_glo, calendar)
333  ENDIF
334  !
335  CALL bcast(lon_glo)
336  CALL bcast(lat_glo)
337  CALL bcast(mask_glo)
338  CALL bcast(area_glo)
339  CALL bcast(corners_glo)
340  CALL bcast(kindex_g)
341  CALL bcast(contfrac_glo)
342  CALL bcast(calendar)
343  CALL bcast(model_guess)
344  !
345  ALLOCATE(lalo_glo(nbindex_g,2))
346  DO ik=1,nbindex_g
347     !
348     j = ((kindex_g(ik)-1)/iim_glo)+1
349     i = (kindex_g(ik)-(j-1)*iim_glo)
350     !
351     IF ( i > iim_glo .OR. j > jjm_glo ) THEN
352        WRITE(100+mpi_rank,*) "Error in the indexing (ik, kindex, i, j) : ", ik, kindex(ik), i, j
353        STOP "ERROR in orchideedriver"
354     ENDIF
355     !
356     lalo_glo(ik,1) = lat_glo(i,j)
357     lalo_glo(ik,2) = lon_glo(i,j)
358     !
359  ENDDO
360  !
361  WRITE(*,*) "Rank", mpi_rank, " Before parallel region All land points : ",  nbindex_g
362  WRITE(*,*) "Rank", mpi_rank, " from ", iim_glo, " point in Lon. and ", jjm_glo, "in Lat."
363  !-
364  !---------------------------------------------------------------------------------------
365  !-
366  !- Now that the grid is distributed on all procs we can start
367  !- initialise the ORCHIDEE domain on each proc (longitude, latitude, indices)
368  !-
369  !---------------------------------------------------------------------------------------
370  !-
371  !- init_data_para also transfers kindex_g to index_g (the variable used in ORCHIDEE)
372  !-
373  CALL grid_set_glo(iim_glo, jjm_glo, nbindex_g)
374  CALL grid_allocate_glo(nbseg)
375  ! Copy the list of indexes of land points into index_g used by ORCHIDEE and then broacast to all
376  ! processors
377  CALL bcast(nbindex_g)
378  IF ( is_root_prc) index_g = kindex_g
379  CALL bcast(index_g)
380  !
381  WRITE(numout,*) "Rank", mpi_rank, "Into Init_orchidee_data_para_driver with ", nbindex_g
382  WRITE(numout,*) "Rank", mpi_rank, "Into ", index_g(1), index_g(nbindex_g)
383  !
384  CALL Init_orchidee_data_para_driver(nbindex_g,index_g)
385  CALL init_ioipsl_para 
386  !
387  WRITE(numout,*) "Rank", mpi_rank, "After init_data_para global size : ",  nbp_glo, SIZE(index_g), iim_g, iim_glo, jjm_g, jjm_glo
388  WRITE(numout,'("After init_data_para local : ij_nb, jj_nb",2I4)') iim_glo, jj_nb
389  !
390  ! Allocate grid on the local processor
391  !
392  IF ( model_guess == "regular") THEN
393     CALL grid_init (nbp_loc, nbseg, regular_lonlat, "ForcingGrid")
394  ELSE IF ( model_guess == "WRF") THEN
395     CALL grid_init (nbp_loc, nbseg, regular_xy, "WRFGrid")
396  ELSE
397     CALL ipslerr(3, "orchidedriver", "The grid found in the GRID_FILE is not supported by ORCHIDEE", "", "")
398  ENDIF
399  !
400  !
401  ! Transfer the global grid variables to the ORCHIDEE version on the root proc
402  ! *_glo -> *_g
403  ! Variables *_g were allocated with the CALL init_grid
404  !
405  IF ( is_root_prc) THEN
406     !
407     lalo_g(:,:) = lalo_glo(:,:)
408     lon_g(:,:) = lon_glo(:,:)
409     lat_g(:,:) = lat_glo(:,:)
410     !
411  ENDIF
412  !
413  !
414  ! Set the local dimensions of the fields
415  !
416  iim = iim_glo
417  jjm = jj_nb
418  kjpindex = nbp_loc
419  !
420  WRITE(numout,*) mpi_rank, "DIMENSIONS of grid on processor : iim, jjm, kjpindex = ", iim, jjm, kjpindex
421  !
422  !  Allocate the local arrays we need :
423  !
424  ALLOCATE(lon(iim,jjm), lat(iim,jjm))
425  ALLOCATE(corners_lon(nbseg,iim,jjm), corners_lat(nbseg,iim,jjm))
426  ALLOCATE(kindex(kjpindex))
427  !
428  lon=lon_glo(:,jj_para_begin(mpi_rank):jj_para_end(mpi_rank))
429  lat=lat_glo(:,jj_para_begin(mpi_rank):jj_para_end(mpi_rank))
430  DO in=1,nbseg
431     corners_lon(in,:,:)=corners_glo(:,jj_para_begin(mpi_rank):jj_para_end(mpi_rank),in,1)
432     corners_lat(in,:,:)=corners_glo(:,jj_para_begin(mpi_rank):jj_para_end(mpi_rank),in,2)
433  ENDDO
434  !
435  !
436  ! Redistribute the indeces on all procs (apple distribution of land points)
437  !
438  CALL bcast(lon_g)
439  CALL bcast(lat_g)
440  CALL scatter(index_g, kindex)
441  !
442  !
443  ! Apply the offset needed so that kindex refers to the index of the land point
444  ! on the current region, i.e. the local lon lat domain.
445  !
446  kindex(1:kjpindex)=kindex(1:kjpindex)-(jj_begin-1)*iim_glo
447  !
448  ! This routine transforms the global grid into a series of polygons for all land
449  ! points identified by index_g.
450  !
451  CALL grid_stuff(nbindex_g, iim_g, jjm_g, lon_g, lat_g, index_g, contfrac_glo)
452  !
453  ! Distribute the global lalo to the local processor level lalo
454  !
455  ALLOCATE(lalo_loc(kjpindex,2))
456  CALL scatter(lalo_glo, lalo_loc)
457  lalo(:,:) = lalo_loc(:,:)
458  !
459  !====================================================================================
460  !-
461  !- Prepare the time for the simulation
462  !-
463  !- Set the calendar and get some information
464  !-
465  CALL ioconf_calendar(calendar)
466  CALL ioget_calendar(one_year, one_day)
467  !-
468  !- get the time period for the run
469  !-
470  CALL forcing_integration_time(date0, dt, nbdt)
471  !
472  !-
473  !- Set the start date in IOIPSL for the calendar and initialize the module time
474  !-
475  CALL ioconf_startdate(date0)
476  CALL time_initialize(0, date0, dt, "END")
477  !
478  !
479  !====================================================================================
480  !-
481  !- Initialize the forcing files and prepare the time stepping through the data.
482  !-
483  !
484  CALL forcing_open(forfilename, iim_glo,  jjm_glo, lon_glo, lat_glo, nbindex_g, zoom_lon, zoom_lat, &
485       &            index_g, kjpindex, numout)
486  !
487  !-
488  !====================================================================================
489  !-
490  !- Initialise the ORCHIDEE system in 4 steps :
491  !- 1 The control flags,
492  !- 2 Allocate memory (needs to be done after initializing the control flags because of nvm).
493  !- 2 the restart system of IOIPSL
494  !- 3 The history mechanism
495  !- 4 Finally the first call to SECHIBA will initialise all the internal variables
496  !
497  ! 1 Setting flags and some parameters (nvm)
498  !
499  CALL control_initialize
500  !
501  ! 2 Allocation
502  !
503  ALLOCATE(zlev_tq(kjpindex), zlev_uv(kjpindex))
504  ALLOCATE(u(kjpindex), v(kjpindex), pb(kjpindex))
505  ALLOCATE(temp_air(kjpindex))
506  ALLOCATE(qair(kjpindex))
507  ALLOCATE(petAcoef(kjpindex), peqAcoef(kjpindex), petBcoef(kjpindex), peqBcoef(kjpindex))
508  ALLOCATE(ccanopy(kjpindex))
509  ALLOCATE(cdrag(kjpindex))
510  ALLOCATE(precip_rain(kjpindex))
511  ALLOCATE(precip_snow(kjpindex))
512  ALLOCATE(swdown(kjpindex))
513  ALLOCATE(swnet(kjpindex))
514  ALLOCATE(lwdown(kjpindex))
515  ALLOCATE(sinang(kjpindex))
516  ALLOCATE(vevapp(kjpindex))
517  ALLOCATE(fluxsens(kjpindex))
518  ALLOCATE(fluxlat(kjpindex))
519  ALLOCATE(coastalflow(kjpindex))
520  ALLOCATE(riverflow(kjpindex))
521  ALLOCATE(netco2(kjpindex))
522  ALLOCATE(carblu(kjpindex))
523  ALLOCATE(carbwh(kjpindex))
524  ALLOCATE(carbha(kjpindex))
525  ALLOCATE(tsol_rad(kjpindex))
526  ALLOCATE(temp_sol_new(kjpindex))
527  ALLOCATE(qsurf(kjpindex))
528  ALLOCATE(albedo(kjpindex,2))
529  ALLOCATE(emis(kjpindex))
530  ALLOCATE(epot_air(kjpindex))
531  ALLOCATE(u_tq(kjpindex), v_tq(kjpindex))
532  ALLOCATE(z0m(kjpindex))
533  ALLOCATE(z0h(kjpindex))
534  ALLOCATE(veget_diag(kjpindex,nvm))
535  ALLOCATE(lai_diag(kjpindex,nvm))
536  ALLOCATE(height_diag(kjpindex,nvm))
537  !-
538  !---------------------------------------------------------------------------------------
539  !-
540  !- Get a first set of forcing data
541  !-
542  !---------------------------------------------------------------------------------------
543  !-
544  !- Some default values so that the operations before the ORCHIDEE initialisation do not fail.
545  !-
546  z0m(:) = 0.1
547  albedo(:,:) = 0.13
548  !
549  itau = 0
550  !
551  CALL ioipslctrl_restini(itau, date0, dt, rest_id, rest_id_stom, itau_offset, date0_shifted)
552  WRITE(numout,*) "itau_offset : ", itau_offset, date0, date0_shifted
553  WRITE(numout,*) "itau_offset diff = ", date0_shifted, date0, date0_shifted-date0
554  !
555  ! To ensure that itau starts with 0 at date0 for the restart, we have to set an off-set to achieve this.
556  ! itau_offset will get used to prduce itau_sechiba.
557  !
558  itau_offset=-itau_offset-1
559  !
560  ! Get the date of the first time step
561  !
562  WRITE(*,*) "itau_offset : date0 : ", year_end, month_end, day_end, sec_end
563  !
564  CALL xios_orchidee_init( MPI_COMM_ORCH,                &
565       date0,    year_end, month_end, day_end, julian_diff,  &
566       lon,      lat,      znt)
567  !
568  !- Initialize IOIPSL sechiba output files
569  itau_sechiba = itau+itau_offset
570  CALL ioipslctrl_history(iim, jjm, lon, lat,  kindex, kjpindex, itau_sechiba, &
571       date0, dt, hist_id, hist2_id, hist_id_stom, hist_id_stom_IPCC)
572  WRITE(*,*) "HISTORY : Defined for ", itau_sechiba, date0, dt
573  !
574  !-
575  !---------------------------------------------------------------------------------------
576  !-
577  !- Go into the time loop
578  !-
579  !---------------------------------------------------------------------------------------
580  !-
581  DO itau = 1,nbdt
582     !
583     CALL time_nextstep( itau )
584     !     
585     timestep_interval(1) = julian_start
586     timestep_interval(2) = julian_end
587     julian = julian_end
588     !
589     ! Get the forcing data
590     !
591     CALL forcing_getvalues(timestep_interval, dt, zlev_tq, zlev_uv, temp_air, qair, &
592          &                 precip_rain, precip_snow, swdown, lwdown, sinang, u, v, pb)
593     !-
594     !
595     IF ( itau == nbdt ) lrestart_write = .TRUE.
596     !
597     ! Adaptation of the forcing data to SECHIBA's needs
598     !
599     ! Contrary to what the documentation says, ORCHIDEE expects surface pressure in hPa.
600     pb(:) = pb(:)/100.
601     epot_air(:) = cp_air*temp_air(:)+cte_grav*zlev_tq(:)
602     ccanopy(:) = atmco2
603     cdrag(:) = 0.0
604     !
605     petBcoef(:) = epot_air(:)
606     peqBcoef(:) = qair(:)
607     petAcoef(:) = zero
608     peqAcoef(:) = zero
609     !
610     ! Interpolate the wind (which is at hight zlev_uv) to the same height
611     ! as the temperature and humidity (at zlev_tq).
612     !
613     u_tq(:) = u(:)*LOG(zlev_tq(:)/z0m(:))/LOG(zlev_uv(:)/z0m(:))
614     v_tq(:) = v(:)*LOG(zlev_tq(:)/z0m(:))/LOG(zlev_uv(:)/z0m(:))
615     !
616     !
617     swnet(:) =(1.-(albedo(:,1)+albedo(:,2))/2.)*swdown(:)
618     !
619     !
620     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, temp_air, "RECEIVED Air temperature")
621     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, qair, "RECEIVED Air humidity")
622     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, precip_rain*one_day, "RECEIVED Rainfall")
623     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, precip_snow*one_day, "RECEIVED Snowfall")
624     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, swnet, "RECEIVED net solar")
625     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, lwdown, "RECEIVED lwdown")
626     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, u, "RECEIVED East-ward wind")
627     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, v, "RECEIVED North-ward wind")
628     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, pb*100, "RECEIVED surface pressure")
629     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, zlev_uv, "RECEIVED UV height")
630     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, zlev_tq, "RECEIVED TQ height")
631     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, sinang, "RECEIVED sinang")
632     !
633     IF ( itau .NE. 1 ) THEN
634        IF ( timemeasure ) THEN
635           waitget_cputime = waitget_cputime + Get_cpu_Time(timer_global)
636           waitget_walltime = waitget_walltime + Get_real_Time(timer_global)
637           CALL stop_timer(timer_global)
638           CALL start_timer(timer_global)
639        ENDIF
640     ENDIF
641     !
642     !---------------------------------------------------------------------------------------
643     !-
644     !- IF first time step : Call to SECHIBA_initialize to set-up ORCHIDEE before doing an actual call
645     !- which will provide the first fluxes.
646     !-
647     !---------------------------------------------------------------------------------------
648     !
649     itau_sechiba = itau+itau_offset
650     !
651     ! Update the calendar in xios by sending the new time step
652     !CALL xios_orchidee_update_calendar(itau_sechiba)
653     CALL xios_orchidee_update_calendar(itau_sechiba)
654     !
655     IF ( itau == 1 ) THEN
656        !
657        IF ( timemeasure ) THEN
658           WRITE(numout,*) '------> CPU Time for start-up of main : ',Get_cpu_Time(timer_global)
659           WRITE(numout,*) '------> Real Time for start-up of main : ',Get_real_Time(timer_global)
660           CALL stop_timer(timer_global)
661           CALL start_timer(timer_global)
662        ENDIF
663        !
664        CALL sechiba_initialize( &
665             itau_sechiba,  iim*jjm,      kjpindex,      kindex,                   &
666             lalo_loc,     contfrac,     neighbours,    resolution,  zlev_tq,      &
667             u_tq,         v_tq,         qair,          temp_air,                  &
668             petAcoef,     peqAcoef,     petBcoef,      peqBcoef,                  &
669             precip_rain,  precip_snow,  lwdown,        swnet,       swdown,       &
670             pb,           rest_id,      hist_id,       hist2_id,                  &
671             rest_id_stom, hist_id_stom, hist_id_stom_IPCC,                        &
672             coastalflow,  riverflow,    tsol_rad,      vevapp,      qsurf,        &
673             z0m,          z0h,          albedo,        fluxsens,    fluxlat,  emis, &
674             temp_sol_new, cdrag)
675        !
676        CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, temp_sol_new, "Init temp_sol_new")
677        !
678        ! Net solar and the wind at the right hight are recomputed with the correct values.
679        !
680        swnet(:) =(1.-(albedo(:,1)+albedo(:,2))/2.)*swdown(:)
681        u_tq(:) = u(:)*LOG(zlev_tq(:)/z0m(:))/LOG(zlev_uv(:)/z0m(:))
682        v_tq(:) = v(:)*LOG(zlev_tq(:)/z0m(:))/LOG(zlev_uv(:)/z0m(:))
683        !
684        lrestart_read = .FALSE.
685        !
686        CALL histwrite_p(hist_id, 'LandPoints',  itau+1, (/ REAL(kindex) /), kjpindex, kindex)
687        CALL histwrite_p(hist_id, 'Areas',  itau+1, area, kjpindex, kindex)
688        CALL histwrite_p(hist_id, 'Contfrac',  itau+1, contfrac, kjpindex, kindex)
689        !
690        IF ( timemeasure ) THEN
691           WRITE(numout,*) '------> CPU Time for set-up of ORCHIDEE : ',Get_cpu_Time(timer_global)
692           WRITE(numout,*) '------> Real Time for set-up of ORCHIDEE : ',Get_real_Time(timer_global)
693           CALL stop_timer(timer_global)
694           CALL start_timer(timer_global)
695        ENDIF
696        !
697     ENDIF
698     !
699     !---------------------------------------------------------------------------------------
700     !-
701     !- Main call to SECHIBA 
702     !-
703     !---------------------------------------------------------------------------------------
704     !
705     !
706     !
707     CALL sechiba_main (itau_sechiba, iim*jjm, kjpindex, kindex, &
708          & lrestart_read, lrestart_write, &
709          & lalo_loc, contfrac, neighbours, resolution, &
710          ! First level conditions
711          & zlev_tq, u_tq, v_tq, qair, temp_air, epot_air, ccanopy, &
712          ! Variables for the implicit coupling
713          & cdrag, petAcoef, peqAcoef, petBcoef, peqBcoef, &
714          ! Rain, snow, radiation and surface pressure
715          & precip_rain ,precip_snow, lwdown, swnet, swdown, sinang, pb, &
716          ! Output : Fluxes
717          & vevapp, fluxsens, fluxlat, coastalflow, riverflow, &
718          ! CO2 fluxes
719          & netco2, carblu, carbwh, carbha, &
720          ! Surface temperatures and surface properties
721          & tsol_rad, temp_sol_new, qsurf, albedo, emis, z0m, z0h, &
722          ! Vegetation, lai and vegetation height
723          & veget_diag, lai_diag, height_diag, &
724          ! File ids
725          & rest_id, hist_id, hist2_id, rest_id_stom, hist_id_stom, hist_id_stom_IPCC)
726     !
727     !
728     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, temp_sol_new, "Produced temp_sol_new")
729     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, fluxsens, "Produced fluxsens")
730     CALL forcing_printpoint(julian, testpt(1), testpt(2), kjpindex, lalo_loc, fluxlat, "Produced fluxlat")
731     !
732     IF ( timemeasure ) THEN
733        orchidee_cputime = orchidee_cputime + Get_cpu_Time(timer_global)
734        orchidee_walltime = orchidee_walltime + Get_real_Time(timer_global)
735        CALL stop_timer(timer_global)
736        CALL start_timer(timer_global)
737     ENDIF
738     !
739     !---------------------------------------------------------------------------------------
740     !-
741     !- Write diagnostics
742     !-
743     !---------------------------------------------------------------------------------------
744     !
745     CALL xios_orchidee_send_field("LandPoints" ,(/ ( REAL(ik), ik=1,kjpindex ) /))
746     CALL xios_orchidee_send_field("areas", area)
747     CALL xios_orchidee_send_field("contfrac",contfrac)
748     CALL xios_orchidee_send_field("temp_air",temp_air)
749     CALL xios_orchidee_send_field("qair",qair)
750     CALL xios_orchidee_send_field("swnet",swnet)
751     CALL xios_orchidee_send_field("swdown",swdown)
752     ! zpb in hPa, output in Pa
753     CALL xios_orchidee_send_field("pb",pb)
754     !
755     IF ( .NOT. almaoutput ) THEN
756        !
757        !  ORCHIDEE INPUT variables
758        !
759        CALL histwrite_p (hist_id, 'swdown',   itau_sechiba, swdown,   kjpindex, kindex)
760        CALL histwrite_p (hist_id, 'tair',     itau_sechiba, temp_air, kjpindex, kindex)
761        CALL histwrite_p (hist_id, 'qair',     itau_sechiba, qair, kjpindex, kindex)
762        CALL histwrite_p (hist_id, 'evap',     itau_sechiba, vevapp, kjpindex, kindex)
763        CALL histwrite_p (hist_id, 'coastalflow',itau_sechiba, coastalflow, kjpindex, kindex)
764        CALL histwrite_p (hist_id, 'riverflow',itau_sechiba, riverflow, kjpindex, kindex)
765        !
766        CALL histwrite_p (hist_id, 'temp_sol', itau_sechiba, temp_sol_new, kjpindex, kindex)
767        CALL histwrite_p (hist_id, 'tsol_max', itau_sechiba, temp_sol_new, kjpindex, kindex)
768        CALL histwrite_p (hist_id, 'tsol_min', itau_sechiba, temp_sol_new, kjpindex, kindex)
769        CALL histwrite_p (hist_id, 'fluxsens', itau_sechiba, fluxsens, kjpindex, kindex)
770        CALL histwrite_p (hist_id, 'fluxlat',  itau_sechiba, fluxlat,  kjpindex, kindex)
771        CALL histwrite_p (hist_id, 'swnet',    itau_sechiba, swnet,    kjpindex, kindex)
772        CALL histwrite_p (hist_id, 'alb_vis',  itau_sechiba, albedo(:,1), kjpindex, kindex)
773        CALL histwrite_p (hist_id, 'alb_nir',  itau_sechiba, albedo(:,2), kjpindex, kindex)
774        !
775        IF ( hist2_id > 0 ) THEN
776           CALL histwrite_p (hist2_id, 'swdown',   itau_sechiba, swdown, kjpindex, kindex)
777           CALL histwrite_p (hist2_id, 'tair',     itau_sechiba, temp_air, kjpindex, kindex)
778           CALL histwrite_p (hist2_id, 'qair',     itau_sechiba, qair, kjpindex, kindex)
779           !
780           CALL histwrite_p (hist2_id, 'evap',     itau_sechiba, vevapp, kjpindex, kindex)
781           CALL histwrite_p (hist2_id, 'coastalflow',itau_sechiba, coastalflow, kjpindex, kindex)
782           CALL histwrite_p (hist2_id, 'riverflow',itau_sechiba, riverflow, kjpindex, kindex)
783           !
784           CALL histwrite_p (hist2_id, 'temp_sol', itau_sechiba, temp_sol_new, kjpindex, kindex)
785           CALL histwrite_p (hist2_id, 'tsol_max', itau_sechiba, temp_sol_new, kjpindex, kindex)
786           CALL histwrite_p (hist2_id, 'tsol_min', itau_sechiba, temp_sol_new, kjpindex, kindex)
787           CALL histwrite_p (hist2_id, 'fluxsens', itau_sechiba, fluxsens, kjpindex, kindex)
788           CALL histwrite_p (hist2_id, 'fluxlat',  itau_sechiba, fluxlat,  kjpindex, kindex)
789           CALL histwrite_p (hist2_id, 'swnet',    itau_sechiba, swnet,    kjpindex, kindex)
790           !
791           CALL histwrite_p (hist2_id, 'alb_vis',  itau_sechiba, albedo(:,1), kjpindex, kindex)
792           CALL histwrite_p (hist2_id, 'alb_nir',  itau_sechiba, albedo(:,2), kjpindex, kindex)
793        ENDIF
794     ELSE
795        !
796        ! Input variables
797        !
798        CALL histwrite_p (hist_id, 'SinAng', itau_sechiba, sinang, kjpindex, kindex)
799        CALL histwrite_p (hist_id, 'LWdown', itau_sechiba, lwdown, kjpindex, kindex)
800        CALL histwrite_p (hist_id, 'SWdown', itau_sechiba, swdown, kjpindex, kindex)
801        CALL histwrite_p (hist_id, 'Tair', itau_sechiba, temp_air, kjpindex, kindex)
802        CALL histwrite_p (hist_id, 'Qair', itau_sechiba, qair, kjpindex, kindex)
803        CALL histwrite_p (hist_id, 'SurfP', itau_sechiba, pb, kjpindex, kindex)
804        CALL histwrite_p (hist_id, 'Windu', itau_sechiba, u_tq, kjpindex, kindex)
805        CALL histwrite_p (hist_id, 'Windv', itau_sechiba, v_tq, kjpindex, kindex)
806        !
807        CALL histwrite_p (hist_id, 'Evap', itau_sechiba, vevapp, kjpindex, kindex)
808        CALL histwrite_p (hist_id, 'SWnet',    itau_sechiba, swnet, kjpindex, kindex)
809        CALL histwrite_p (hist_id, 'Qh', itau_sechiba, fluxsens, kjpindex, kindex)
810        CALL histwrite_p (hist_id, 'Qle',  itau_sechiba, fluxlat, kjpindex, kindex)
811        CALL histwrite_p (hist_id, 'AvgSurfT', itau_sechiba, temp_sol_new, kjpindex, kindex)
812        CALL histwrite_p (hist_id, 'RadT', itau_sechiba, temp_sol_new, kjpindex, kindex)
813        !
814        ! There is a mess with the units passed to the coupler. To be checked with Marc
815        !
816        IF ( river_routing ) THEN
817           CALL histwrite_p (hist_id, 'CoastalFlow',itau_sechiba, coastalflow, kjpindex, kindex)
818           CALL histwrite_p (hist_id, 'RiverFlow',itau_sechiba, riverflow, kjpindex, kindex)
819        ENDIF
820        !
821        IF ( hist2_id > 0 ) THEN
822           CALL histwrite_p (hist2_id, 'Evap', itau_sechiba, vevapp, kjpindex, kindex)
823           CALL histwrite_p (hist2_id, 'SWnet',    itau_sechiba, swnet, kjpindex, kindex)
824           CALL histwrite_p (hist2_id, 'Qh', itau_sechiba, fluxsens, kjpindex, kindex)
825           CALL histwrite_p (hist2_id, 'Qle',  itau_sechiba, fluxlat, kjpindex, kindex)
826           CALL histwrite_p (hist2_id, 'AvgSurfT', itau_sechiba, temp_sol_new, kjpindex, kindex)
827           CALL histwrite_p (hist2_id, 'RadT', itau_sechiba, temp_sol_new, kjpindex, kindex)
828        ENDIF
829     ENDIF
830     !
831     !
832  ENDDO
833  !-
834  !-
835  !---------------------------------------------------------------------------------------
836  !-
837  !- Close eveything
838  !-
839  !--
840  !
841  CALL xios_orchidee_context_finalize
842  CALL histclo
843  IF(is_root_prc) THEN
844     CALL restclo
845     CALL getin_dump
846  ENDIF
847  !-
848  !- Deallocate all variables and reset initialization variables
849  !-
850  CALL orchideedriver_clear()
851  !
852  WRITE(numout,*) "End at proc ", mpi_rank
853  !
854  !
855  !---------------------------------------------------------------------------------------
856  !-
857  !- Get time and close IOIPSL, OASIS and MPI
858  !-
859  !---------------------------------------------------------------------------------------
860  !-
861  IF ( timemeasure ) THEN
862     WRITE(numout,*) '------> Total CPU Time waiting to get forcing : ',waitget_cputime
863     WRITE(numout,*) '------> Total Real Time waiting to get forcing : ',waitget_walltime
864     WRITE(numout,*) '------> Total CPU Time for ORCHIDEE : ', orchidee_cputime
865     WRITE(numout,*) '------> Total Real Time for ORCHIDEE : ', orchidee_walltime
866     WRITE(numout,*) '------> Total CPU Time waiting to put fluxes : ',waitput_cputime
867     WRITE(numout,*) '------> Total Real Time waiting to put fluxes : ',waitput_walltime
868     WRITE(numout,*) '------> Total CPU Time for closing : ',  Get_cpu_Time(timer_global)
869     WRITE(numout,*) '------> Total Real Time for closing : ', Get_real_Time(timer_global)
870     WRITE(numout,*) '------> Total without MPI : CPU Time  : ', Get_cpu_Time(timer_mpi)
871     WRITE(numout,*) '------> Total without MPI : Real Time : ', Get_real_Time(timer_mpi)
872     CALL stop_timer(timer_global)
873     CALL stop_timer(timer_mpi)
874  ENDIF
875  !
876  CALL Finalize_mpi
877  !
878CONTAINS
879!
880!! ================================================================================================================================
881!! SUBROUTINE   : orchideedriver_clear
882!!
883!>\BRIEF         Clear orchideedriver
884!!
885!! DESCRIPTION  :  Deallocate memory and reset initialization variables to there original values
886!!                 This subroutine calls forcing_tools_clear and sechiba_clear.
887!!
888!_ ================================================================================================================================
889!
890  SUBROUTINE orchideedriver_clear
891    !- Deallocate all variables existing on all procs
892    !-
893    !- Deallocate all variables existing on all procs (list still incomplete)
894    !-
895    IF ( ALLOCATED(lon_glo) ) DEALLOCATE(lon_glo)
896    IF ( ALLOCATED(lat_glo) ) DEALLOCATE(lat_glo)
897    IF ( ALLOCATED(mask_glo) ) DEALLOCATE(mask_glo)
898    IF ( ALLOCATED(area_glo) ) DEALLOCATE(area_glo)
899    IF ( ALLOCATED(corners_glo) ) DEALLOCATE(corners_glo)
900    IF ( ALLOCATED(corners_lon) ) DEALLOCATE(corners_lon)
901    IF ( ALLOCATED(corners_lat) ) DEALLOCATE(corners_lat)
902    IF ( ALLOCATED(kindex_g) ) DEALLOCATE(kindex_g)
903    IF ( ALLOCATED(contfrac_glo) ) DEALLOCATE(contfrac_glo)
904    IF ( ALLOCATED(lalo_glo) ) DEALLOCATE(lalo_glo)
905    IF ( ALLOCATED(lon) ) DEALLOCATE(lon)
906    IF ( ALLOCATED(lat) ) DEALLOCATE(lat)
907    IF ( ALLOCATED(kindex) ) DEALLOCATE(kindex)
908    IF ( ALLOCATED(lalo_loc) ) DEALLOCATE(lalo_loc)
909    IF ( ALLOCATED(zlev_tq) ) DEALLOCATE(zlev_tq)
910    IF ( ALLOCATED(zlev_uv) ) DEALLOCATE(zlev_uv)
911    IF ( ALLOCATED(u) ) DEALLOCATE(u)
912    IF ( ALLOCATED(v) ) DEALLOCATE(v)
913    IF ( ALLOCATED(pb) ) DEALLOCATE(pb)
914    IF ( ALLOCATED(temp_air) ) DEALLOCATE(temp_air)
915    IF ( ALLOCATED(qair) ) DEALLOCATE(qair)
916    IF ( ALLOCATED(precip_rain) ) DEALLOCATE(precip_rain)
917    IF ( ALLOCATED(precip_snow) ) DEALLOCATE(precip_snow)
918    IF ( ALLOCATED(swdown) ) DEALLOCATE(swdown)
919    IF ( ALLOCATED(swnet) ) DEALLOCATE(swnet)
920    IF ( ALLOCATED(lwdown) ) DEALLOCATE(lwdown)
921    IF ( ALLOCATED(sinang) ) DEALLOCATE(sinang)
922    IF ( ALLOCATED(epot_air) ) DEALLOCATE(epot_air)
923    IF ( ALLOCATED(ccanopy) ) DEALLOCATE(ccanopy)
924    IF ( ALLOCATED(cdrag) ) DEALLOCATE(cdrag)
925    IF ( ALLOCATED(swnet) ) DEALLOCATE(swnet)
926    IF ( ALLOCATED(petAcoef) ) DEALLOCATE(petAcoef)
927    IF ( ALLOCATED(peqAcoef) ) DEALLOCATE(peqAcoef)
928    IF ( ALLOCATED(petBcoef) ) DEALLOCATE(petBcoef)
929    IF ( ALLOCATED(peqBcoef) ) DEALLOCATE(peqBcoef)
930    IF ( ALLOCATED(u_tq) ) DEALLOCATE(u_tq)
931    IF ( ALLOCATED(v_tq) ) DEALLOCATE(v_tq)
932    IF ( ALLOCATED(vevapp) ) DEALLOCATE(vevapp)
933    IF ( ALLOCATED(fluxsens) ) DEALLOCATE(fluxsens)
934    IF ( ALLOCATED(fluxlat) ) DEALLOCATE(fluxlat)
935    IF ( ALLOCATED(coastalflow) ) DEALLOCATE(coastalflow)
936    IF ( ALLOCATED(riverflow) ) DEALLOCATE(riverflow)
937    IF ( ALLOCATED(netco2) ) DEALLOCATE(netco2)
938    IF ( ALLOCATED(carblu) ) DEALLOCATE(carblu)
939    IF ( ALLOCATED(carbwh) ) DEALLOCATE(carbwh)
940    IF ( ALLOCATED(carbha) ) DEALLOCATE(carbha)
941    IF ( ALLOCATED(tsol_rad) ) DEALLOCATE(tsol_rad)
942    IF ( ALLOCATED(temp_sol_new) ) DEALLOCATE(temp_sol_new)
943    IF ( ALLOCATED(qsurf) ) DEALLOCATE(qsurf)
944    IF ( ALLOCATED(albedo) ) DEALLOCATE(albedo)
945    IF ( ALLOCATED(emis) ) DEALLOCATE(emis)
946    IF ( ALLOCATED(z0m) ) DEALLOCATE(z0m)
947    IF ( ALLOCATED(z0h) ) DEALLOCATE(z0h)
948    !
949    WRITE(numout,*) "Memory deallocated"
950    !
951  END SUBROUTINE orchideedriver_clear
952  !
953END PROGRAM orchidedriver
Note: See TracBrowser for help on using the repository browser.