Index: /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/iophy.F90
===================================================================
--- /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/iophy.F90	(revision 3926)
+++ /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/iophy.F90	(revision 3927)
@@ -78,5 +78,5 @@
     
 #ifdef CPP_XIOS
-      CALL wxios_domain_param_unstructured
+      CALL wxios_domain_param_unstructured("dom_glo")
 #endif
     
@@ -160,8 +160,5 @@
       ! Initialize the XIOS domain coreesponding to this process:
     
-        CALL wxios_domain_param("dom_glo", is_sequential, nbp_lon, jj_nb, nbp_lon, nbp_lat, &
-                                1, nbp_lon, ii_begin, ii_end, jj_begin, jj_end,             &
-                                klon_mpi+2*(nbp_lon-1), data_ibegin, data_iend,             &
-                               io_lat, io_lon,is_south_pole,mpi_rank)
+        CALL wxios_domain_param("dom_glo")
 #endif
 !$OMP END MASTER
Index: /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/wxios.F90
===================================================================
--- /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/wxios.F90	(revision 3926)
+++ /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/wxios.F90	(revision 3927)
@@ -240,63 +240,65 @@
     ! Pour initialiser un domaine !!!!!!!!!!!!!!!!!!!!
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-    SUBROUTINE wxios_domain_param(dom_id, is_sequential, ni, nj, ni_glo, nj_glo,        &
-                                    ibegin, iend, ii_begin, ii_end, jbegin, jend,       &
-                                    data_ni, data_ibegin, data_iend,                    &
-                                    io_lat, io_lon,is_south_pole,mpi_rank)
-         
-
-        USE print_control_mod, ONLY : prt_level, lunout
-        IMPLICIT NONE
-
+    SUBROUTINE wxios_domain_param(dom_id)
+       USE dimphy, only: klon
+       USE mod_phys_lmdz_para, only: gather, bcast, &
+                                     jj_nb, jj_begin, jj_end, ii_begin, ii_end, &
+                                     mpi_size, mpi_rank, klon_mpi, &
+                                     is_sequential, is_south_pole
+       USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo         
+       USE print_control_mod, ONLY : prt_level, lunout
+       USE geometry_mod
+       IMPLICIT NONE
         CHARACTER(len=*),INTENT(IN) :: dom_id ! domain identifier
-        LOGICAL,INTENT(IN) :: is_sequential ! flag
-        INTEGER,INTENT(IN) :: ni ! local MPI domain number of longitudes
-        INTEGER,INTENT(IN) :: nj ! local MPI domain number of latitudes
-        INTEGER,INTENT(IN) :: ni_glo ! global grid number of longitudes
-        INTEGER,INTENT(IN) :: nj_glo ! global grid number of latitudes
-        INTEGER,INTENT(IN) :: ibegin ! start index, on global grid, of local MPI domain 
-        INTEGER,INTENT(IN) :: iend ! end index, on global grid, of local MPI domain
-        INTEGER,INTENT(IN) :: ii_begin ! i index at which local data starts (first row)
-        INTEGER,INTENT(IN) :: ii_end ! i index at which local data ends (last row)
-        INTEGER,INTENT(IN) :: jbegin ! start index, on global grid, of local MPI domain 
-        INTEGER,INTENT(IN) :: jend ! end index, on global grid, of local MPI domain
-        INTEGER,INTENT(IN) :: data_ni
-        INTEGER,INTENT(IN) :: data_ibegin
-        INTEGER,INTENT(IN) :: data_iend
-        REAL,INTENT(IN) :: io_lat(:) ! latitudes (of global grid)
-        REAL,INTENT(IN) :: io_lon(:) ! longitudes (of global grid)
-        logical,intent(in) :: is_south_pole ! does this process include the south pole?
-        integer,intent(in) :: mpi_rank ! rank of process
-        
+
+        REAL   :: rlat_glo(klon_glo)
+        REAL   :: rlon_glo(klon_glo)
+        REAL   :: io_lat(nbp_lat)
+        REAL   :: io_lon(nbp_lon)
+        LOGICAL :: mask(nbp_lon,jj_nb) !Masque pour les problèmes de recouvrement MPI
         TYPE(xios_domain) :: dom
+        INTEGER :: i
         LOGICAL :: boool
         
-        !Masque pour les problèmes de recouvrement MPI:
-        LOGICAL :: mask(ni,nj)
+
+
+        CALL gather(lat_degrees,rlat_glo) 
+        CALL bcast(rlat_glo)
+        CALL gather(lon_degrees,rlon_glo)
+        CALL bcast(rlon_glo)
+    
+  !$OMP MASTER  
+        io_lat(1)=rlat_glo(1)
+        io_lat(nbp_lat)=rlat_glo(klon_glo)
+        IF ((nbp_lon*nbp_lat) > 1) then
+          DO i=2,nbp_lat-1
+            io_lat(i)=rlat_glo(2+(i-2)*nbp_lon)
+          ENDDO
+        ENDIF
+
+        IF (klon_glo == 1) THEN
+          io_lon(1)=rlon_glo(1)
+        ELSE 
+          io_lon(1:nbp_lon)=rlon_glo(2:nbp_lon+1)
+        ENDIF
+
         
         !On récupère le handle:
         CALL xios_get_domain_handle(dom_id, dom)
         
-        IF (prt_level >= 10) THEN
-          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ni:",ni," ni_glo:", ni_glo, " nj:", nj, " nj_glo:", nj_glo
-          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ibegin:",ibegin," iend:", iend, " jbegin:", jbegin, " jend:", jend
-          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ii_begin:",ii_begin," ii_end:", ii_end
-          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," Size io_lon:", SIZE(io_lon(ibegin:iend)), " io_lat:", SIZE(io_lat(jbegin:jend))
-        ENDIF
-        
         !On parametrise le domaine:
-        CALL xios_set_domain_attr_hdl(dom, ni_glo=ni_glo, ibegin=ibegin-1, ni=ni, type="rectilinear")
-        CALL xios_set_domain_attr_hdl(dom, nj_glo=nj_glo, jbegin=jbegin-1, nj=nj, data_dim=2)
-        CALL xios_set_domain_attr_hdl(dom, lonvalue_1d=io_lon(ibegin:iend), latvalue_1d=io_lat(jbegin:jend))
+        CALL xios_set_domain_attr_hdl(dom, ni_glo=nbp_lon, ibegin=0, ni=nbp_lon, type="rectilinear")
+        CALL xios_set_domain_attr_hdl(dom, nj_glo=nbp_lat, jbegin=jj_begin-1, nj=jj_nb, data_dim=2)
+        CALL xios_set_domain_attr_hdl(dom, lonvalue_1d=io_lon(1:nbp_lon), latvalue_1d=io_lat(jj_begin:jj_end))
 
         IF (.NOT.is_sequential) THEN
             mask(:,:)=.TRUE.
             if (ii_begin>1) mask(1:ii_begin-1,1) = .FALSE.
-            if (ii_end<ni) mask(ii_end+1:ni,nj) = .FALSE.
+            if (ii_end<nbp_lon) mask(ii_end+1:nbp_lon,jj_nb) = .FALSE.
             ! special case for south pole
-            if ((ii_end.eq.1).and.(is_south_pole)) mask(1:ni,nj)=.true.
+            if ((ii_end==1).and.(is_south_pole)) mask(1:nbp_lon,jj_nb)=.true.
             IF (prt_level >= 10) THEN
               WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," mask(:,1)=",mask(:,1)
-              WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," mask(:,nj)=",mask(:,nj)
+              WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," mask(:,jj_nb)=",mask(:,jj_nb)
             ENDIF
             CALL xios_set_domain_attr_hdl(dom, mask_2d=mask)
@@ -310,8 +312,10 @@
             IF (prt_level >= 10) WRITE(lunout,*) "wxios_domain_param: Invalid domain: ", trim(dom_id)
         END IF
+!$OMP END MASTER
+        
     END SUBROUTINE wxios_domain_param
     
 
-    SUBROUTINE wxios_domain_param_unstructured
+    SUBROUTINE wxios_domain_param_unstructured(dom_id)
         USE geometry_mod, ONLY : longitude, latitude, boundslon, boundslat,ind_cell_glo
         USE mod_grid_phy_lmdz, ONLY : nvertex, klon_glo
@@ -319,4 +323,5 @@
         USE nrtype, ONLY : PI
         IMPLICIT NONE
+        CHARACTER(len=*),INTENT(IN) :: dom_id ! domain identifier
         REAL :: lon_mpi(klon_mpi)
         REAL :: lat_mpi(klon_mpi)
@@ -335,5 +340,5 @@
 
 !$OMP MASTER
-        CALL xios_get_domaingroup_handle("dom_glo", dom)
+        CALL xios_get_domaingroup_handle(dom_id, dom)
         
         !On parametrise le domaine:
Index: /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/xios_writefield.F90
===================================================================
--- /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/xios_writefield.F90	(revision 3927)
+++ /dynamico_lmdz/aquaplanet/LMDZ5/libf/phylmd/xios_writefield.F90	(revision 3927)
@@ -0,0 +1,294 @@
+MODULE xios_writefield_mod
+
+  PRIVATE
+  
+  INTEGER,PARAMETER :: size_name=255
+  INTEGER, PARAMETER :: MaxWriteField = 100
+  CHARACTER(len=size_name), SAVE ::  FieldName(MaxWriteField) 
+!$OMP THREADPRIVATE(FieldName)
+  INTEGER,                  SAVE  :: FieldIt (MaxWriteField)
+!$OMP THREADPRIVATE(FieldIt)
+  INTEGER,SAVE :: NbField = 0
+!$OMP THREADPRIVATE(NbField) 
+
+  INTERFACE xios_writefield
+    MODULE PROCEDURE xios_writefield2d,xios_writefield3d
+  END INTERFACE
+
+  LOGICAL,SAVE :: output_native_grid
+!$OMP THREADPRIVATE( output_native_grid)  
+
+  INTEGER,SAVE :: ni_glo , nj_glo
+!$OMP THREADPRIVATE( ni_glo, nj_glo)  
+
+   
+  PUBLIC xios_writefield
+
+CONTAINS
+  
+  FUNCTION NameId(name_in)
+  IMPLICIT NONE
+    CHARACTER(LEN=*),INTENT(IN)      :: name_in
+    INTEGER                          :: nameId
+    CHARACTER(LEN=size_name)         :: name
+    INTEGER :: n  
+    
+    name=name_in
+     
+    DO n=1,NbField
+      IF (name==fieldName(n)) THEN
+        nameId=n
+        RETURN
+      ENDIF
+    ENDDO
+      
+    nameId=0
+    RETURN
+  END FUNCTION NameId
+     
+ 
+ 
+  SUBROUTINE xios_writefield2d(field,name_in)
+  USE dimphy
+  USE mod_phys_lmdz_para
+  USE xios
+  USE print_control_mod, ONLY:  lunout
+  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured, regular_lonlat
+  USE wxios, ONLY: wxios_domain_param_unstructured, wxios_domain_param,  wxios_set_context
+  USE ioipsl
+  IMPLICIT NONE
+  REAL, DIMENSION(:), INTENT(IN) :: field
+  CHARACTER(LEN=*)                 :: name_in
+  CHARACTER(LEN=size_name)         :: name 
+  TYPE(xios_context) :: xios_ctx
+  TYPE(xios_domaingroup) :: domain_definition
+  TYPE(xios_domaingroup) :: my_domaingroup
+  TYPE(xios_domain)      :: my_domain
+  TYPE(xios_domain)      :: my_regular_domain
+  TYPE(xios_filegroup)   :: file_definition
+  TYPE(xios_file)        :: my_file
+  TYPE(xios_field)       :: my_field
+  TYPE(xios_fieldgroup)  :: field_definition
+  TYPE(xios_generate_rectilinear_domain) :: generate_domain 
+  TYPE(xios_interpolate_domain) :: interpolate_domain
+  INTEGER                :: id
+  REAL,DIMENSION(klon_mpi) :: field_mpi  
+  REAL :: Field2d(nbp_lon,jj_nb)
+  
+    
+    IF (size(field,1) /= klon_omp) THEN
+      WRITE(lunout,*) 'xios_writefield :: '//FieldName//' is not on the model grid'
+      RETURN
+    ENDIF
+  
+    name = TRIM(ADJUSTL(name_in))
+    id=nameId(name)
+    
+    IF (id/=0) THEN
+      IF (is_omp_master) THEN
+        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
+        CALL xios_set_current_context(xios_ctx)            !Activation
+      ENDIF
+    ELSE
+      output_native_grid=.FALSE.
+      ni_glo=0
+      nj_glo=0
+      IF (is_master) CALL getin("xios_writefield_nlon",ni_glo)
+      IF (is_master) CALL getin("xios_writefield_nlat",nj_glo)
+      CALL bcast(ni_glo)
+      CALL bcast(nj_glo)
+      IF (ni_glo==0 .OR. nj_glo==0) output_native_grid=.TRUE.
+
+      IF (is_omp_master) THEN
+        CALL xios_context_initialize('context_lmdz_'//TRIM(name), COMM_LMDZ_PHY)
+        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
+        CALL xios_set_current_context(xios_ctx)            !Activation
+      
+        CALL xios_define_calendar("D360")
+        CALL xios_set_start_date(xios_date(2000,1,1,0,0,0))
+        CALL xios_set_time_origin(xios_date(2000,1,1,0,0,0))
+        CALL xios_set_timestep(xios_second)
+      
+        CALL xios_get_handle("domain_definition",domain_definition)
+        CALL xios_add_child(domain_definition,my_domaingroup,"domaingroup")
+        CALL xios_add_child(my_domaingroup,my_domain,"domain")
+        IF (grid_type==unstructured .AND. .NOT. output_native_grid ) THEN
+          CALL xios_add_child(domain_definition, my_regular_domain, "regular_domain")
+          CALL xios_set_attr(my_regular_domain,ni_glo=ni_glo, nj_glo=nj_glo, type="rectilinear")
+          CALL xios_add_child(my_regular_domain,generate_domain)
+          CALL xios_set_attr(generate_domain,lon_start=-180., lat_start=90., lat_end=-90.)
+          CALL xios_add_child(my_regular_domain,interpolate_domain)
+        ENDIF
+          
+      ENDIF
+      
+      IF (grid_type==regular_lonlat) THEN
+        CALL wxios_domain_param("domain")
+      ELSE IF (grid_type==unstructured) THEN
+        CALL wxios_domain_param_unstructured("domaingroup")
+      ENDIF       
+      
+      IF (is_omp_master) THEN
+        CALL xios_get_handle("file_definition",file_definition)
+        CALL xios_add_child(file_definition, my_file)
+        CALL xios_set_attr(my_file,name=TRIM(name),output_freq=xios_timestep,sync_freq=xios_timestep,type="one_file")
+        CALL xios_get_handle("field_definition",field_definition) 
+        CALL xios_add_child(field_definition, my_field, TRIM(name))
+        CALL xios_set_attr(my_field,domain_ref="domain",operation="instant")
+        CALL xios_add_child(my_file, my_field)
+        CALL xios_set_attr(my_field,field_ref=TRIM(name))
+        IF (grid_type==unstructured .AND. .NOT. output_native_grid) CALL xios_set_attr(my_field,domain_ref="regular_domain")
+
+        CALL xios_close_context_definition()
+      
+        NbField=NbField+1
+        FieldName(NbField)=TRIM(name)
+        FieldIt(NbField)=0
+        id=NbField
+      ENDIF
+    ENDIF
+ 
+    CALL Gather_omp(field,field_mpi)
+
+    IF (is_omp_master) THEN
+      FieldIt(id)=FieldIt(id)+1
+      CALL xios_update_calendar(FieldIt(id)) 
+
+      IF (grid_type==regular_lonlat) THEN
+        CALL grid1Dto2D_mpi(field_mpi,Field2d)   
+        CALL xios_send_field(TRIM(name), Field2d)
+      ELSE IF (grid_type==unstructured) THEN
+        CALL xios_send_field(TRIM(name), field_mpi)
+      ENDIF
+    ENDIF
+    
+    CALL wxios_set_context()
+  
+  END SUBROUTINE  xios_writefield2d
+
+  SUBROUTINE xios_writefield3d(field,name_in)
+  USE dimphy
+  USE mod_phys_lmdz_para
+  USE xios
+  USE print_control_mod, ONLY:  lunout
+  USE mod_grid_phy_lmdz, only: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured, regular_lonlat
+  USE wxios, ONLY: wxios_domain_param_unstructured, wxios_domain_param,  wxios_set_context
+  USE ioipsl
+  IMPLICIT NONE
+  REAL, DIMENSION(:,:), INTENT(IN) :: field
+  CHARACTER(LEN=*)                 :: name_in
+  CHARACTER(LEN=size_name)         :: name 
+  TYPE(xios_context) :: xios_ctx
+  TYPE(xios_domaingroup) :: domain_definition
+  TYPE(xios_domaingroup) :: my_domaingroup
+  TYPE(xios_domain)      :: my_domain
+  TYPE(xios_domain)      :: my_regular_domain
+  TYPE(xios_filegroup)   :: file_definition
+  TYPE(xios_file)        :: my_file
+  TYPE(xios_field)       :: my_field
+  TYPE(xios_fieldgroup)  :: field_definition
+  TYPE(xios_generate_rectilinear_domain) :: generate_domain 
+  TYPE(xios_interpolate_domain) :: interpolate_domain
+  TYPE(xios_axisgroup)   :: axis_definition
+  TYPE(xios_axis)        :: my_axis
+  INTEGER                :: id
+  REAL,DIMENSION(klon_mpi,size(field,2)) :: field_mpi  
+  REAL :: Field2d(nbp_lon,jj_nb,size(field,2))
+  REAL :: axis_value(size(field,2))
+  INTEGER :: i
+    
+    IF (size(field,1) /= klon_omp) THEN
+      WRITE(lunout,*) 'xios_writefield :: '//FieldName//' is not on the model grid'
+      RETURN
+    ENDIF
+  
+    name = TRIM(ADJUSTL(name_in))
+    id=nameId(name)
+    
+    IF (id/=0) THEN
+   
+      IF (is_omp_master) CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
+      IF (is_omp_master) CALL xios_set_current_context(xios_ctx)            !Activation
+   
+    ELSE
+      output_native_grid=.FALSE.
+      ni_glo=0
+      nj_glo=0
+      IF (is_master) CALL getin("xios_writefield_nlon",ni_glo)
+      IF (is_master) CALL getin("xios_writefield_nlat",nj_glo)
+      CALL bcast(ni_glo)
+      CALL bcast(nj_glo)
+      IF (ni_glo==0 .OR. nj_glo==0) output_native_grid=.TRUE.
+
+      IF (is_omp_master) THEN
+        CALL xios_context_initialize('context_lmdz_'//TRIM(name), COMM_LMDZ_PHY)
+        CALL xios_get_handle('context_lmdz_'//TRIM(name), xios_ctx)    !Récupération
+        CALL xios_set_current_context(xios_ctx)            !Activation
+      
+        CALL xios_define_calendar("D360")
+        CALL xios_set_start_date(xios_date(2000,1,1,0,0,0))
+        CALL xios_set_time_origin(xios_date(2000,1,1,0,0,0))
+        CALL xios_set_timestep(xios_second)
+      
+        CALL xios_get_handle("domain_definition",domain_definition)
+        CALL xios_add_child(domain_definition,my_domaingroup,"domaingroup")
+        CALL xios_add_child(my_domaingroup,my_domain,"domain")
+        IF (grid_type==unstructured .AND. .NOT. output_native_grid) THEN
+          CALL xios_add_child(domain_definition, my_regular_domain, "regular_domain")
+          CALL xios_set_attr(my_regular_domain,ni_glo=ni_glo, nj_glo=nj_glo, type="rectilinear")
+          CALL xios_add_child(my_regular_domain,generate_domain)
+          CALL xios_set_attr(generate_domain,lon_start=-180., lat_start=90., lat_end=-90.)
+          CALL xios_add_child(my_regular_domain,interpolate_domain)
+        ENDIF
+        CALL xios_get_handle("axis_definition",axis_definition)
+        CALL xios_add_child(axis_definition,my_axis,"axis")
+        axis_value=(/(i,i=1,size(field,2))/)
+        CALL xios_set_attr(my_axis,name="z",n_glo=size(field,2),value=axis_value, unit="level" )        
+      ENDIF
+      
+      IF (grid_type==regular_lonlat) THEN
+        CALL wxios_domain_param("domain")
+      ELSE IF (grid_type==unstructured) THEN
+        CALL wxios_domain_param_unstructured("domaingroup")
+      ENDIF       
+      
+      IF (is_omp_master) THEN
+        CALL xios_get_handle("file_definition",file_definition)
+        CALL xios_add_child(file_definition, my_file)
+        CALL xios_set_attr(my_file,name=TRIM(name),output_freq=xios_timestep,sync_freq=xios_timestep,type="one_file")
+        CALL xios_get_handle("field_definition",field_definition) 
+        CALL xios_add_child(field_definition, my_field, TRIM(name))
+        CALL xios_set_attr(my_field,domain_ref="domain",axis_ref="axis", operation="instant")
+        CALL xios_add_child(my_file, my_field)
+        CALL xios_set_attr(my_field,field_ref=TRIM(name))
+        IF (grid_type==unstructured .AND. .NOT. output_native_grid) CALL xios_set_attr(my_field,domain_ref="regular_domain")
+
+        CALL xios_close_context_definition()
+      
+        NbField=NbField+1
+        FieldName(NbField)=TRIM(name)
+        FieldIt(NbField)=0
+        id=NbField
+      ENDIF
+    ENDIF
+ 
+    CALL Gather_omp(field,field_mpi)
+
+    IF (is_omp_master) THEN
+      FieldIt(id)=FieldIt(id)+1
+      CALL xios_update_calendar(FieldIt(id)) 
+
+      IF (grid_type==regular_lonlat) THEN
+        CALL grid1Dto2D_mpi(field_mpi,Field2d)   
+        CALL xios_send_field(TRIM(name), Field2d)
+      ELSE IF (grid_type==unstructured) THEN
+        CALL xios_send_field(TRIM(name), field_mpi)
+      ENDIF
+    ENDIF
+    
+    CALL wxios_set_context()
+  
+  END SUBROUTINE  xios_writefield3d
+
+
+END MODULE
