Index: trunk/NEMO/OPA_SRC/DIA/diawri.F90
===================================================================
--- trunk/NEMO/OPA_SRC/DIA/diawri.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/DIA/diawri.F90	(revision 1359)
@@ -27,4 +27,5 @@
    USE in_out_manager  ! I/O manager
    USE diadimg         ! dimg direct access file format output
+   USE iom
    USE ioipsl
 
@@ -411,4 +412,101 @@
          WRITE(numout,*) '~~~~~~ '
       ENDIF
+      
+!---------------------------------------------------------------------------
+
+      CALL iom_put("votemper",tn)
+      CALL iom_put("vosaline",sn)
+      CALL iom_put("sosstsst",tn(:,:,1))   ! sea surface temperature
+      CALL iom_put("sosaline",sn(:,:,1))   ! sea surface salinity
+#if defined key_dynspg_rl
+      CALL iom_put("sobarstf",bsfn)   ! barotropic streamfunction
+#else
+      CALL iom_put("sossheig",sshn)   ! sea surface height
+#endif
+
+      CALL iom_put("sowaflup",emp )   ! upward water flux
+      CALL iom_put("sowaflcd",emps)   ! c/d water flux
+      zw2d(:,:) = emps(:,:) * sn(:,:,1) * tmask(:,:,1)
+      CALL iom_put("sosalflx",zw2d)   ! c/d salt flux
+      CALL iom_put("sohefldo",qns + qsr )   ! total heat flux
+      CALL iom_put("soshfldo",qsr)   ! solar heat flux
+      CALL iom_put("somxl010",hmlp)   ! mixed layer depth
+      CALL iom_put("somixhgt",hmld)   ! turbocline depth
+      CALL iom_put("soicecov",fr_i)   ! ice fraction 
+#if ! defined key_coupled
+      CALL iom_put("sohefldp",qrp)   ! heat flux damping
+      CALL iom_put("sowafldp",erp)   ! freshwater flux damping
+      zw2d(:,:) = erp(:,:) * sn(:,:,1) * tmask(:,:,1)
+      CALL iom_put("sosafldp", zw2d)   ! salt flux damping
+#endif
+
+#if ( defined key_coupled && ! defined key_lim3 && ! defined key_lim2 ) 
+      CALL iom_put("sohefldp",qrp)   ! heat flux damping
+      CALL iom_put("sowafldp",erp)   ! freshwater flux damping
+         zw2d(:,:) = erp(:,:) * sn(:,:,1) * tmask(:,:,1)
+      CALL iom_put("sosafldp",zw2d)   ! salt flux damping
+#endif
+#if defined key_diaspr
+      CALL iom_put("sosurfps",gps)   ! surface pressure
+#endif
+         zw2d(:,:) = FLOAT( nmln(:,:) ) * tmask(:,:,1)
+      CALL iom_put("sobowlin",zw2d)   ! ???
+
+#if defined key_diahth
+      CALL iom_put("sothedep",hth)   ! depth of the thermocline
+      CALL iom_put("so20chgt",hd20)   ! depth of the 20 isotherm
+      CALL iom_put("so28chgt",hd28)   ! depth of the 28 isotherm
+      CALL iom_put("sohtc300",htc3)   ! first 300m heaat content
+#endif
+
+#if defined key_coupled 
+#  if defined key_lim3
+      Must be adapted for LIM3
+#  else
+      CALL iom_put("soicetem",tn_ice)   ! surf. ice temperature
+      CALL iom_put("soicealb",alb_ice)   ! ice albedo
+#  endif
+#endif
+         ! Write fields on U grid
+      CALL iom_put("vozocrtx",un)    ! i-current
+#if defined key_diaeiv
+      CALL iom_put("vozoeivu",u_eiv)    ! i-eiv current
+#endif
+      CALL iom_put("sozotaux",utau)   ! i-wind stress
+#if defined key_dynspg_rl
+      CALL lbc_lnk( spgu, 'U', -1. )
+      CALL iom_put("sozospgx",spgu)   ! i-surf. press. grad.
+#endif
+
+         ! Write fields on V grid
+      CALL iom_put("vomecrty",vn)   ! j-current
+#if defined key_diaeiv
+      CALL iom_put("vomeeivv",v_eiv)   ! j-eiv current
+#endif
+      CALL iom_put("sometauy", vtau)   ! j-wind stress
+#if defined key_dynspg_rl
+      CALL lbc_lnk( spgv, 'V', -1. )
+      CALL iom_put("somespgy",spgv)   ! j-surf. pressure grad.
+#endif
+
+         ! Write fields on W grid
+      CALL iom_put("vovecrtz",wn)    ! vert. current
+#   if defined key_diaeiv
+      CALL iom_put("voveeivw",w_eiv)    ! vert. eiv current
+#   endif
+      CALL iom_put("votkeavt",avt)    ! T vert. eddy diff. coef.
+      CALL iom_put("votkeevd",avt_evd)    ! T enhan. vert. eddy diff. coef.
+      CALL iom_put("votkeavm",avmu)    ! T vert. eddy visc. coef.
+      CALL iom_put("votkeevm",avmu_evd)    ! T enhan. vert. eddy visc. coef.
+      IF( lk_zdfddm ) THEN
+         CALL iom_put("voddmavs",fsavs(:,:,:) )    ! S vert. eddy diff. coef.
+      ENDIF
+#if defined key_traldf_c2d
+      CALL iom_put("soleahtw",ahtw)   ! lateral eddy diff. coef.
+# if defined key_traldf_eiv
+      CALL iom_put("soleaeiw",aeiw)   ! EIV coefficient at w-point
+# endif
+#endif
+!---------------------------------------------------------------------------
 
       ! Write fields on T grid
Index: trunk/NEMO/OPA_SRC/IOM/io_interface.F90
===================================================================
--- trunk/NEMO/OPA_SRC/IOM/io_interface.F90	(revision 1359)
+++ trunk/NEMO/OPA_SRC/IOM/io_interface.F90	(revision 1359)
@@ -0,0 +1,51 @@
+MODULE io_interface
+
+   !! $Id$
+
+#if defined key_ioserver
+   USE mod_ioclient
+   USE mod_event_client
+   USE iomanager
+#endif
+
+   IMPLICIT NONE
+
+CONTAINS
+
+   SUBROUTINE init_ioserver( kcomm, is_mode_server )
+      LOGICAL, intent(in   )           :: is_mode_server
+      INTEGER, intent(  out), OPTIONAL :: kcomm
+#if defined key_ioserver
+      !
+      INTEGER :: nb_server
+      INTEGER :: server_rank
+      INTEGER :: ierr
+# if defined key_mpp_mpi
+      !$AGRIF_DO_NOT_TREAT
+#include <mpif.h>
+      !$AGRIF_END_DO_NOT_TREAT
+# endif
+
+      IF (is_mode_server) THEN
+         CALL init_ioclient(kcomm)
+         using_server=.TRUE.
+      ELSE
+# if defined key_mpp_mpi
+         kcomm = MPI_COMM_WORLD
+         CALL MPI_INIT( ierr )
+         CALL MPI_COMM_SIZE( kcomm, nb_server, ierr )     
+         CALL MPI_COMM_RANK( kcomm, server_rank, ierr )
+# else
+         nb_server = 1
+         server_rank = 0
+# endif
+         CALL iom__init(1,nb_server,server_rank)
+         CALL iom__set_current_rank(1)
+         using_server=.FALSE.
+      ENDIF
+#endif
+
+   END SUBROUTINE init_ioserver
+
+
+END MODULE io_interface
Index: trunk/NEMO/OPA_SRC/IOM/iom.F90
===================================================================
--- trunk/NEMO/OPA_SRC/IOM/iom.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/IOM/iom.F90	(revision 1359)
@@ -26,11 +26,20 @@
    USE iom_rstdimg     ! restarts access direct format "dimg" style...
 
+#if defined key_ioserver
+   USE mod_event_client
+# endif
+
    IMPLICIT NONE
    PUBLIC   !   must be public to be able to access iom_def through iom
    
-   PUBLIC iom_open, iom_close, iom_varid, iom_get, iom_gettime, iom_rstput
+   PUBLIC iom_init, iom_open, iom_close, iom_setkt, iom_varid, iom_get, iom_gettime, iom_rstput, iom_put
 
    PRIVATE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
    PRIVATE iom_g0d, iom_g1d, iom_g2d, iom_g3d, iom_get_123d
+   PRIVATE iom_p2d, iom_p3d
+#if defined key_ioserver
+   PRIVATE iom_init_chkcpp
+   PRIVATE set_grid
+# endif
 
    INTERFACE iom_get
@@ -40,4 +49,12 @@
       MODULE PROCEDURE iom_rp0d, iom_rp1d, iom_rp2d, iom_rp3d
    END INTERFACE
+  INTERFACE iom_put
+     MODULE PROCEDURE iom_p2d, iom_p3d
+  END INTERFACE
+#if defined key_ioserver
+   INTERFACE iom_setkt
+      MODULE PROCEDURE event__set_timestep
+   END INTERFACE
+# endif
 
    !!----------------------------------------------------------------------
@@ -48,4 +65,42 @@
 
 CONTAINS
+
+   SUBROUTINE iom_init( pjulian )
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE   ***
+      !!
+      !! ** Purpose :   
+      !!
+      !!----------------------------------------------------------------------
+      REAL(wp), INTENT(in) ::  pjulian   !: julian day at nit000 = 0
+#if defined key_ioserver
+      !!----------------------------------------------------------------------
+      ! read the xml file
+      CALL event__parse_xml_file( 'iodef.xml' )   ! <- to get from the nameliste (namrun)...
+
+      ! calendar parameters
+      CALL event__set_time_parameters( nit000 - 1, pjulian, rdt )
+
+      ! horizontal grid definition
+      CALL set_grid( "grid_T", glamt, gphit )
+      CALL set_grid( "grid_U", glamu, gphiu )
+      CALL set_grid( "grid_V", glamv, gphiv )
+      CALL set_grid( "grid_W", glamt, gphit )
+
+      ! vertical grid definition
+      CALL event__set_vert_axis( "deptht", gdept_0 )
+      CALL event__set_vert_axis( "depthu", gdept_0 )
+      CALL event__set_vert_axis( "depthv", gdept_0 )
+      CALL event__set_vert_axis( "depthw", gdepw_0 )
+
+      ! consistency regarding CPP keys...
+      CALL iom_init_chkcpp
+
+      ! end file definition
+      CALL event__close_io_definition
+#endif
+
+   END SUBROUTINE iom_init
+
 
    SUBROUTINE iom_open( cdname, kiomid, ldwrt, kdom, kiolib, ldstop, ldiof )
@@ -92,7 +147,7 @@
       ! if iom_open is called for the first time: initialize iom_file(:)%nfid to 0
       ! (could be done when defining iom_file in f95 but not in f90)
-      IF( iom_init == 0 ) THEN
+      IF( iom_open_init == 0 ) THEN
          iom_file(:)%nfid = 0
-         iom_init = 1
+         iom_open_init = 1
       ENDIF
       ! do we read or write the file?
@@ -244,4 +299,7 @@
          i_s = 1
          i_e = jpmax_files
+#if defined key_ioserver
+         CALL event__stop_ioserver
+#endif
       ENDIF
 
@@ -822,5 +880,129 @@
       ENDIF
    END SUBROUTINE iom_rp3d
+
+
    !!----------------------------------------------------------------------
+   !!                   INTERFACE iom_rstput
+   !!----------------------------------------------------------------------
+   SUBROUTINE iom_p2d( cdname, pfield2d )
+      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
+      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   pfield2d
+#if defined key_ioserver
+      CALL event__write_field2D( cdname, pfield2d(nldi:nlei, nldj:nlej) )
+#endif
+   END SUBROUTINE iom_p2d
+
+   SUBROUTINE iom_p3d( cdname, pfield3d )
+      CHARACTER(LEN=*)                , INTENT(in) ::   cdname
+      REAL(wp), DIMENSION(jpi,jpj,jpk), INTENT(in) ::   pfield3d
+#if defined key_ioserver
+      CALL event__write_field3D( cdname, pfield3d(nldi:nlei, nldj:nlej, :) )
+#endif
+   END SUBROUTINE iom_p3d
+   !!----------------------------------------------------------------------
+
+
+#if defined key_ioserver
+
+   SUBROUTINE set_grid( cdname, plon, plat )
+      !!----------------------------------------------------------------------
+      !!                     ***  ROUTINE   ***
+      !!
+      !! ** Purpose :   
+      !!
+      !!----------------------------------------------------------------------
+      CHARACTER(LEN=*)            , INTENT(in) ::   cdname
+      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plon
+      REAL(wp), DIMENSION(jpi,jpj), INTENT(in) ::   plat
+
+      CALL event__set_grid_dimension( cdname, jpiglo, jpjglo)
+      CALL event__set_grid_domain( cdname, nlei-nldi+1, nlej-nldj+1, nimpp+nldi-1, njmpp+nldj-1, &
+         &                         plon(nldi:nlei, nldj:nlej), plat(nldi:nlei, nldj:nlej) )
+      CALL event__set_grid_type_nemo( cdname )
+
+   END SUBROUTINE set_grid
+
+
+   SUBROUTINE iom_init_chkcpp
+      !!---------------------------------------------------------------------
+      !!                   ***  SUBROUTINE    ***
+      !!
+      !! ** Purpose :  
+      !!---------------------------------------------------------------------
+      USE zdfddm, ONLY :   lk_zdfddm      ! vertical  physics: double diffusion
+      
+#if defined key_dynspg_rl
+      CALL event__disable_field( "sossheig" )
+#else
+      CALL event__disable_field( "sobarstf" )
+#endif
+
+      !!#if ! ( ! defined key_dynspg_rl && defined key_ice_lim)
+      !!        CALL disable_field( "iowaflup" )
+      !!         CALL disable_field( "sowaflep" )
+      !!#endif
+
+#if ! defined key_coupled
+      CALL event__enable_field( "sohefldp" )
+      CALL event__enable_field( "sowafldp" )
+      CALL event__enable_field( "sosafldp" )
+#endif
+
+#if ( defined key_coupled && ! defined key_lim3 && ! defined key_lim2 ) 
+      CALL event__enable_field( "sohefldp" )
+      CALL event__enable_field( "sowafldp" )
+      CALL event__enable_field( "sosafldp" )
+#endif
+
+#if ! defined key_diaspr
+      CALL event__disable_field( "sosurfps" )
+#endif
+
+#if ! defined key_diahth
+      CALL event__disable_field( "sothedep" )
+      CALL event__disable_field( "so20chgt" )
+      CALL event__disable_field( "so28chgt" )
+      CALL event__disable_field( "sohtc300" )
+#endif
+
+#if defined key_coupled 
+# if defined key_lim3
+      Must be adapted to LIM3
+# else
+      CALL event__enable_field( "soicetem" )
+      CALL event__enable_field( "soicealb" )
+# endif 
+#endif 
+
+#if ! defined key_diaeiv
+      CALL event__disable_field( "vozoeivu" )
+      CALL event__disable_field( "vomeeivv" )
+      CALL event__disable_field( "voveeivw" )
+#endif
+
+#if ! defined key_dynspg_rl
+      CALL event__disable_field( "sozospgx" )
+      CALL event__disable_field( "somespgy" )
+#endif
+
+      IF( lk_zdfddm ) CALL event__enable_field( "voddmavs" )
+
+#if ! defined key_traldf_c2d
+      CALL event__disable_field( "soleahtw" )
+#endif
+
+#if ! defined key_traldf_eiv 
+      CALL event__disable_field( "soleaeiw" )
+#endif
+
+   END SUBROUTINE iom_init_chkcpp
+
+#else
+
+   SUBROUTINE iom_setkt( kt )
+      INTEGER, INTENT(in   )::   kt 
+   END SUBROUTINE iom_setkt
+
+#endif
 
 
Index: trunk/NEMO/OPA_SRC/IOM/iom_def.F90
===================================================================
--- trunk/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/IOM/iom_def.F90	(revision 1359)
@@ -49,5 +49,5 @@
 
 !$AGRIF_DO_NOT_TREAT
-   INTEGER, PUBLIC            ::   iom_init = 0        !: used to initialize iom_file(:)%nfid to 0
+   INTEGER, PUBLIC            ::   iom_open_init = 0   !: used to initialize iom_file(:)%nfid to 0
 
    TYPE, PUBLIC ::   file_descriptor
Index: trunk/NEMO/OPA_SRC/daymod.F90
===================================================================
--- trunk/NEMO/OPA_SRC/daymod.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/daymod.F90	(revision 1359)
@@ -30,4 +30,5 @@
    USE in_out_manager  ! I/O manager
    USE iom             ! 
+   USE ioipsl, ONLY :   ymds2ju        ! for calendar
    USE prtctl          ! Print control
    USE restart         ! 
@@ -48,4 +49,5 @@
    REAL(wp), PUBLIC ::   rsec_day    !: current time step counted in second since 00h of the current day
 
+   REAL(wp), PUBLIC ::   fjulday     !: julian day 
    REAL(wp), PUBLIC ::   adatrj      !: number of elapsed days since the begining of the run
    !                                 !: it is the accumulated duration of previous runs
@@ -101,4 +103,7 @@
       nday    =   ndastp - (nyear * 10000) - ( nmonth * 100 ) 
 
+      CALL ymds2ju( nyear, nmonth, nday, 0.0, fjulday )  ! we assume that we start run at 00:00
+      fjulday = fjulday + 1.                             ! move back to the day at nit000 (and not at nit000 - 1)
+
       sec1jan000 = 0.e0
       CALL day_mth
@@ -207,5 +212,6 @@
       rsec_month = rsec_month + rdttra(1)                 
       rsec_day   = rsec_day   + rdttra(1)                 
-      adatrj = adatrj + rdttra(1) / rday
+      adatrj  = adatrj  + rdttra(1) / rday
+      fjulday = fjulday + rdttra(1) / rday
       
       IF( rsec_day > rday ) THEN                        ! NEW day
@@ -300,14 +306,17 @@
             ! define ndastp and adatrj
             IF ( nrstdt == 2 ) THEN 
+               ! read the parameters correspondting to nit000 - 1 (last time step of previous run)
                CALL iom_get( numror, 'ndastp', zndastp )
                ndastp = NINT( zndastp )
                CALL iom_get( numror, 'adatrj', adatrj  )
             ELSE 
-               ndastp = ndate0 - 1     ! ndate0 read in the namelist in dom_nam
+               ! parameters correspondting to nit000 - 1 (as we start the step loop with a call to day)
+               ndastp = ndate0 - 1     ! ndate0 read in the namelist in dom_nam, we assume that we start run at 00:00
                adatrj = ( REAL( nit000-1, wp ) * rdttra(1) ) / rday 
                ! note this is wrong if time step has changed during run 
             ENDIF
          ELSE
-            ndastp = ndate0 - 1        ! ndate0 read in the namelist in dom_nam
+            ! parameters correspondting to nit000 - 1 (as we start the step loop with a call to day)
+            ndastp = ndate0 - 1        ! ndate0 read in the namelist in dom_nam, we assume that we start run at 00:00
             adatrj = ( REAL( nit000-1, wp ) * rdttra(1) ) / rday 
          ENDIF
Index: trunk/NEMO/OPA_SRC/lib_mpp.F90
===================================================================
--- trunk/NEMO/OPA_SRC/lib_mpp.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/lib_mpp.F90	(revision 1359)
@@ -2364,8 +2364,14 @@
       !!            08/04 :: R. Benshila, generalisation
       !!---------------------------------------------------------------------
+#if defined key_ioserver
+      USE io_interface
+#endif
       INTEGER                                 :: code, ierr
       LOGICAL                                 :: mpi_was_called
       !!---------------------------------------------------------------------
       !
+#if defined key_ioserver
+      CALL init_ioserver(mpi_comm_opa,.TRUE.)
+#else
       CALL mpi_initialized( mpi_was_called, code )      ! MPI initialization
       IF ( code /= MPI_SUCCESS ) THEN
@@ -2382,4 +2388,5 @@
          ENDIF
       ENDIF
+#endif
       !
       IF( nn_buffer > 0 ) THEN
Index: trunk/NEMO/OPA_SRC/opa.F90
===================================================================
--- trunk/NEMO/OPA_SRC/opa.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/opa.F90	(revision 1359)
@@ -68,7 +68,7 @@
    USE step            ! OPA time-stepping                  (stp     routine)
 #if defined key_oasis3
-   USE cpl_oasis3      ! OASIS3 coupling (to ECHAM5)
+   USE cpl_oasis3      ! OASIS3 coupling
 #elif defined key_oasis4
-   USE cpl_oasis4      ! OASIS4 coupling (to ECHAM5)
+   USE cpl_oasis4      ! OASIS4 coupling (not working)
 #endif
    USE dynspg_oce      ! Control choice of surface pressure gradient schemes
@@ -79,4 +79,8 @@
 
    USE trcini          ! Initialization of the passive tracers
+   USE iom
+#if defined key_ioserver
+   USE io_interface
+#endif
 
    IMPLICIT NONE
@@ -172,5 +176,5 @@
       !!
       !!----------------------------------------------------------------------
-#if defined key_oasis3 || defined key_oasis4
+#if defined key_oasis3 || defined key_oasis4 || defined key_ioserver
       INTEGER :: localComm
 #endif
@@ -209,4 +213,7 @@
       call cpl_prism_init(localComm)
       ! Nodes selection
+      narea = mynode(localComm)
+#elif key_ioserver
+      CALL init_ioserver(localcomm, .FALSE.)
       narea = mynode(localComm)
 #else
@@ -283,8 +290,10 @@
 
 #if defined key_top
-      CALL trc_ini                           ! Passive tracers
+      CALL trc_ini                          ! Passive tracers
 #endif
 
       CALL dia_ptr_init                     ! Poleward TRansports initialization
+
+      CALL iom_init( fjulday - adatrj )     ! iom_put initialization
 
       !                                     ! =============== !
@@ -437,12 +446,11 @@
       IF ( lk_mpp ) CALL mppsync
 
-      ! 1. Unit close
-      ! -------------
-
       CLOSE( numnam )           ! namelist
       CLOSE( numout )           ! standard model output file
 
       IF(lwp) CLOSE( numstp )   ! time-step file
-      IF(lwp) CLOSE( numsol )
+      IF(lwp) CLOSE( numsol )   ! solver file
+
+      CALL iom_close            ! close all input/output files
 
    END SUBROUTINE opa_closefile
Index: trunk/NEMO/OPA_SRC/step.F90
===================================================================
--- trunk/NEMO/OPA_SRC/step.F90	(revision 1358)
+++ trunk/NEMO/OPA_SRC/step.F90	(revision 1359)
@@ -178,4 +178,6 @@
       CALL day( kstp )             ! Calendar
 
+      CALL iom_setkt( kstp )       ! say to iom that we are at time step kstp
+      
       CALL rst_opn( kstp )         ! Open the restart file
 
