source: branches/ORCHIDEE_2_2/ORCHIDEE/src_oasisdriver/orchideeoasis.f90 @ 8579

Last change on this file since 8579 was 8377, checked in by jan.polcher, 6 months ago

The modifications performed on the trunk for the coupling to WRF and the interpolation by XIOS on curviliean grids has been backported.

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