Index: branches/ORCHIDEE_2_2/ORCHIDEE/src_oasisdriver/driver2oasis.f90
===================================================================
--- branches/ORCHIDEE_2_2/ORCHIDEE/src_oasisdriver/driver2oasis.f90	(revision 5804)
+++ branches/ORCHIDEE_2_2/ORCHIDEE/src_oasisdriver/driver2oasis.f90	(revision 8377)
@@ -16,9 +16,14 @@
   !
   USE grid
+  USE time
+  USE timer
+  USE constantes
+  USE constantes_soil
+  USE forcing_tools
   USE globgrd
-  USE forcing_tools
   !
   USE mod_oasis
   USE timer
+  USE xios
   !-
   IMPLICIT NONE
@@ -42,5 +47,5 @@
   INTEGER(i_std), ALLOCATABLE, DIMENSION(:) :: kindex_g
   REAL(r_std), DIMENSION(2) :: zoom_lon, zoom_lat
-  CHARACTER(LEN=20) :: calendar
+  CHARACTER(LEN=20) :: calendar, calXIOS
   !-
   !- Variables local to each processors.
@@ -53,4 +58,6 @@
   INTEGER(i_std) :: rest_id, rest_id_stom
   INTEGER(i_std) :: hist_id, hist2_id, hist_id_stom, hist_id_stom_IPCC
+  INTEGER(i_std)                 :: yeardr, monthdr, daydr    !! Time origin date information
+  REAL(r_std)                    :: secdr                     !! Time origin date information
 !-
 !- input fields
@@ -74,5 +81,6 @@
   !- output fields
   !-
-  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: z0            !! Surface roughness
+  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: z0m           !! Momentum Surface roughness
+  REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: z0h           !! Temperature Surface roughness
   REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: coastalflow   !! Diffuse flow of water into the ocean (m^3/dt)
   REAL(r_std), ALLOCATABLE, DIMENSION (:)            :: riverflow     !! Largest rivers flowing into the ocean (m^3/dt)
@@ -92,5 +100,5 @@
   INTEGER(i_std) :: glo_rank, glo_size ! rank and  number of pe
   INTEGER(i_std) :: loc_rank, loc_size ! rank and  number of pe
-  INTEGER(i_std) :: LOCAL_OASIS_COMM  ! local MPI communicator and Initialized
+  INTEGER(i_std) :: LOCAL_OASIS_COMM, TMP_COMM ! local MPI communicator and Initialized
   INTEGER(i_std) :: comp_id    ! component identification
   INTEGER(i_std) :: ierror, flag, oasis_info
@@ -105,5 +113,5 @@
   INTEGER(i_std) :: vevapp_id, fluxsens_id, fluxlat_id, coastal_id, river_id
   INTEGER(i_std) :: netco2_id, carblu_id, tsolrad_id, tsolnew_id, qsurf_id
-  INTEGER(i_std) :: albnir_id, albvis_id, emis_id, z0_id
+  INTEGER(i_std) :: albnir_id, albvis_id, emis_id, z0m_id, z0h_id
   !
   INTEGER(i_std), DIMENSION(2) :: var_nodims, var_shape
@@ -112,4 +120,17 @@
   LOGICAL :: initmode = .FALSE.
   !-
+  !- XIOS
+  !-
+  LOGICAL                        :: xios_driver_ok = .FALSE.
+  TYPE(xios_context)             :: ctx_hdl               !! Handel for driver2oasis
+  CHARACTER(len=*),PARAMETER     :: id="driver"           !! Id for initialization of driver2oasis in XIOS
+  TYPE(xios_duration)            :: dtime_xios
+  TYPE(xios_date)                :: start_date
+  TYPE(xios_date)                :: time_origin
+  TYPE(xios_fieldgroup)          :: fieldgroup_handle
+  TYPE(xios_field)               :: field_handle
+  TYPE(xios_file)                :: file_handle
+  !-
+  ! -
   INTEGER(i_std) :: freq_diag=10, debug_lev=1
   INTEGER(i_std) :: w_unit=737
@@ -300,5 +321,7 @@
      freq_diag=96
   ENDIF
-  WRITE(w_unit,*) "Debug_lev, freq_diag = ", debug_lev, freq_diag
+  !
+  xios_driver_ok=.TRUE.
+  WRITE(w_unit,*) 'Using XIOS :', xios_driver_ok
   !-
   !---------------------------------------------------------------------------------------
@@ -323,9 +346,11 @@
      is_root_prc = .TRUE.
      !
-     CALL forcing_getglogrid (nb_forcefile, forfilename, iim_glo, jjm_glo, nbindex_g, .TRUE.)
+     CALL globgrd_getdomsz("NONE", iim_glo, jjm_glo, nbindex_g, model_guess, file_id, forfilename, zoom_lon, zoom_lat)
+     !
+     CALL forcing_getglogrid(nb_forcefile, forfilename, iim_glo, jjm_glo, nbindex_g, .TRUE.)
 
-     CALL forcing_zoomgrid (zoom_lon, zoom_lat, forfilename(1), .TRUE.)
+     CALL forcing_zoomgrid(zoom_lon, zoom_lat, forfilename(1), model_guess, .TRUE.)
  
-     CALL globgrd_writegrid (gridfilename)
+     CALL globgrd_writegrid(gridfilename)
 
      STOP "Grid file sucessfully generated"
@@ -343,7 +368,13 @@
   !-
   CALL oasis_init_comp(comp_id, comp_name, ierror)
-  CALL oasis_get_localcomm(LOCAL_OASIS_COMM, ierror)
-  !
-  CALL Init_orchidee_para(LOCAL_OASIS_COMM)
+  CALL oasis_get_localcomm(TMP_COMM, ierror)
+  !
+  IF ( xios_driver_ok ) THEN
+     CALL xios_initialize(id,local_comm=TMP_COMM, return_comm=LOCAL_OASIS_COMM)
+  ELSE
+     LOCAL_OASIS_COMM = TMP_COMM
+  ENDIF
+  !
+  CALL Init_orchidee_para(LOCAL_OASIS_COMM, .FALSE.)
   !-
   !-
@@ -427,5 +458,5 @@
   !
   CALL forcing_open(forfilename, iim_glo,  jjm_glo, lon_glo, lat_glo, nbindex_g, zoom_lon, zoom_lat, &
-       &            kindex_g, nbindex_g, w_unit)
+       &            kindex_g, nbindex_g, w_unit, model_guess)
   CALL forcing_integration_time(date_start, dt, nbdt)
   !
@@ -455,5 +486,6 @@
   ALLOCATE(albedo(nbindex_g,2))
   ALLOCATE(emis(nbindex_g))
-  ALLOCATE(z0(nbindex_g))
+  ALLOCATE(z0m(nbindex_g))
+  ALLOCATE(z0h(nbindex_g))
   !
   !-
@@ -482,5 +514,4 @@
        &           " Global rank : ", glo_rank
   WRITE (w_unit,*) '----------------------------------------------------------'
-  CALL flush(w_unit)
   !
   !
@@ -488,5 +519,4 @@
   WRITE (w_unit,*) 'DD Local number of processors :', loc_size, " Global value :", glo_size
   WRITE (w_unit,*) 'DD Local MPI communicator is :', LOCAL_OASIS_COMM,  MPI_COMM_WORLD
-  CALL flush(w_unit)
   !-
   !-
@@ -530,6 +560,4 @@
   ENDIF
   !
-  WRITE(w_unit,*) 'After grids writing'
-  call flush(w_unit)
   !-
   !---------------------------------------------------------------------------------------
@@ -646,9 +674,45 @@
   CALL oasis_def_var(emis_id, varname, il_part_id, var_nodims, OASIS_In, var_shape, OASIS_Real, ierror)
   ! 
-  varname="ROUGDRIV"
-  CALL oasis_def_var(z0_id, varname, il_part_id, var_nodims, OASIS_In, var_shape, OASIS_Real, ierror)
+  varname="Z0MODRIV"
+  CALL oasis_def_var(z0m_id, varname, il_part_id, var_nodims, OASIS_In, var_shape, OASIS_Real, ierror)
+  !
+  varname="Z0HEDRIV"
+  CALL oasis_def_var(z0h_id, varname, il_part_id, var_nodims, OASIS_In, var_shape, OASIS_Real, ierror)
   !
   CALL oasis_enddef (ierror)
   !-
+  WRITE(w_unit,*) 'OASIS Vars declared'
+  !
+  !---------------------------------------------------------------------------------------
+  !-
+  !- Set-up XIOS
+  !-
+  !---------------------------------------------------------------------------------------
+  !
+  IF ( xios_driver_ok ) THEN
+     CALL xios_context_initialize("driver2oasis", LOCAL_OASIS_COMM)
+     CALL xios_get_handle("driver2oasis",ctx_hdl)
+     CALL xios_set_current_context(ctx_hdl)
+     !
+     CALL ju2ymds(date_start, yeardr, monthdr, daydr, secdr)
+     !
+     dtime_xios%second=dt
+     IF (INDEX(calendar, 'gregor') > 0 .OR. INDEX(calendar, 'Gregor') > 0 .OR. &
+          &    calendar == 'standard') THEN
+        calXIOS = 'gregorian'
+     ELSE IF (calendar == 'noleap') THEN
+        calXIOS = 'noleap'
+     ELSE IF (calendar == '360d') THEN
+        calXIOS = 'd360'
+     END IF
+     
+     CALL xios_define_calendar(type=calXIOS, start_date=xios_date(yeardr,monthdr,daydr,0,0,0), &
+          time_origin=xios_date(yeardr,monthdr,daydr,0,0,0), timestep=dtime_xios)
+     CALL xios_set_domain_attr("domain_landpoints", ni_glo=iim_glo, nj_glo=jjm_glo, type="curvilinear")
+     CALL xios_set_domain_attr("domain_landpoints",lonvalue_2d=lon_glo,latvalue_2d=lat_glo)
+     CALL xios_close_context_definition()
+  ENDIF
+  WRITE(w_unit,*) 'XIOS Closed Context'
+  !
   !---------------------------------------------------------------------------------------
   !-
@@ -664,5 +728,6 @@
   cdrag(:) = 0.0
   ! Transform the swdown into a swnet
-  swnet(:) = 0.13*swdown(:)
+  albedo(:,:)=0.13
+  swnet(:) = (1.-(albedo(:,1)+albedo(:,2))/2.)*swdown(:)
   !-
   !---------------------------------------------------------------------------------------
@@ -684,4 +749,7 @@
      timestep_interval(2) = date_start + (itau+1)*(dt/one_day)
      julian = date_start + (itau+0.5)*(dt/one_day)
+     !
+     CALL xios_set_current_context(ctx_hdl)
+     CALL xios_update_calendar(itau+1)
      !
      ! Write some diagnostics to look at the shape of the maps
@@ -761,22 +829,23 @@
      ! Go into the blocking gets from OASIS
      !
-     CALL oasis_get(vevapp_id, NINT(itau*dt), vevapp, oasis_info)
-     CALL oasis_get(fluxsens_id, NINT(itau*dt), fluxsens, oasis_info)
-     CALL oasis_get(fluxlat_id, NINT(itau*dt), fluxlat, oasis_info)
-     !
-     CALL oasis_get(coastal_id, NINT(itau*dt), coastalflow, oasis_info)
-     CALL oasis_get(river_id, NINT(itau*dt), riverflow, oasis_info)
-     !
-     CALL oasis_get(netco2_id, NINT(itau*dt), netco2, oasis_info)
-     CALL oasis_get(carblu_id, NINT(itau*dt), carblu, oasis_info)
-     !
-     CALL oasis_get(tsolrad_id, NINT(itau*dt), tsol_rad, oasis_info)
-     CALL oasis_get(tsolnew_id, NINT(itau*dt), temp_sol_new, oasis_info)
-     CALL oasis_get(qsurf_id, NINT(itau*dt), qsurf, oasis_info)
-     !
-     CALL oasis_get(albvis_id, NINT(itau*dt), albedo(:,1), oasis_info)
-     CALL oasis_get(albnir_id, NINT(itau*dt), albedo(:,2), oasis_info)
-     CALL oasis_get(emis_id, NINT(itau*dt), emis, oasis_info)
-     CALL oasis_get(z0_id, NINT(itau*dt), z0, oasis_info)
+     CALL oasis_get(vevapp_id, NINT((itau)*dt), vevapp, oasis_info)
+     CALL oasis_get(fluxsens_id, NINT((itau)*dt), fluxsens, oasis_info)
+     CALL oasis_get(fluxlat_id, NINT((itau)*dt), fluxlat, oasis_info)
+     !
+     CALL oasis_get(coastal_id, NINT((itau)*dt), coastalflow, oasis_info)
+     CALL oasis_get(river_id, NINT((itau)*dt), riverflow, oasis_info)
+     !
+     CALL oasis_get(netco2_id, NINT((itau)*dt), netco2, oasis_info)
+     CALL oasis_get(carblu_id, NINT((itau)*dt), carblu, oasis_info)
+     !
+     CALL oasis_get(tsolrad_id, NINT((itau)*dt), tsol_rad, oasis_info)
+     CALL oasis_get(tsolnew_id, NINT((itau)*dt), temp_sol_new, oasis_info)
+     CALL oasis_get(qsurf_id, NINT((itau)*dt), qsurf, oasis_info)
+     !
+     CALL oasis_get(albvis_id, NINT((itau)*dt), albedo(:,1), oasis_info)
+     CALL oasis_get(albnir_id, NINT((itau)*dt), albedo(:,2), oasis_info)
+     CALL oasis_get(emis_id, NINT((itau)*dt), emis, oasis_info)
+     CALL oasis_get(z0m_id, NINT((itau)*dt), z0m, oasis_info)
+     CALL oasis_get(z0h_id, NINT((itau)*dt), z0h, oasis_info)
      CALL forcing_printpoint(julian, testpt(1), testpt(2), vevapp, "Recived evap")
      !
@@ -812,5 +881,5 @@
         WRITE(w_unit,*) MINVAL(albedo(:,2)), " << ALBEDO NIR << ", MAXVAL(albedo(:,2))
         WRITE(w_unit,*) MINVAL(emis), " << EMIS << ", MAXVAL(emis)
-        WRITE(w_unit,*) MINVAL(z0), " << Z0 << ", MAXVAL(z0)
+        WRITE(w_unit,*) MINVAL(z0m), " << Z0M << ", MAXVAL(z0m)
         !
         CALL forcing_printdate(timestep_interval(2), "Diagnostics END", w_unit)
@@ -838,11 +907,17 @@
   !---------------------------------------------------------------------------------------
   !-
-  CALL oasis_terminate(ierror)
-  !
   CALL forcing_close()
-
-  ! Deallocate all variables and reset init flags
-  CALL forcing_tools_clear()
-
+  WRITE(w_unit,*) "Closed forcing"
+  !-
+  IF ( xios_driver_ok ) THEN
+     CALL xios_context_finalize()
+     CALL xios_finalize()
+     WRITE(w_unit,*) "XIOS finalized"
+     CALL oasis_terminate(ierror)
+  ELSE
+     CALL oasis_terminate(ierror)
+  ENDIF
+  WRITE(w_unit,*) "OASIS closed"
+  !-
   CLOSE(UNIT=w_unit)
   !-
