Index: trunk/LMDZ.COMMON/libf/dyn3d/gcm.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/dyn3d/gcm.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/dyn3d/gcm.F90	(revision 1682)
@@ -417,19 +417,4 @@
 
   !-----------------------------------------------------------------------
-  !   Initialisation de la physique :
-  !   -------------------------------
-
-  IF ((iflag_phys==1).or.(iflag_phys>=100)) THEN
-     ! Physics:
-#ifdef CPP_PHYS
-     CALL iniphysiq(iim,jjm,llm, &
-          (jjm-1)*iim+2,comm_lmdz, &
-          daysec,day_ini,dtphys/nsplit_phys, &
-          rlatu,rlatv,rlonu,rlonv,aire,cu,cv,rad,g,r,cpp, &
-          iflag_phys)
-#endif
-  ENDIF ! of IF ((iflag_phys==1).or.(iflag_phys>=100))
-
-  !-----------------------------------------------------------------------
   !   Initialisation des I/O :
   !   ------------------------
@@ -465,4 +450,21 @@
 #endif
 
+
+  !-----------------------------------------------------------------------
+  !   Initialisation de la physique :
+  !   -------------------------------
+
+  IF ((iflag_phys==1).or.(iflag_phys>=100)) THEN
+     ! Physics:
+#ifdef CPP_PHYS
+     CALL iniphysiq(iim,jjm,llm, &
+          (jjm-1)*iim+2,comm_lmdz, &
+          daysec,day_ini,dtphys/nsplit_phys, &
+          rlatu,rlatv,rlonu,rlonv,aire,cu,cv,rad,g,r,cpp, &
+          iflag_phys)
+#endif
+  ENDIF ! of IF ((iflag_phys==1).or.(iflag_phys>=100))
+
+
   if (planet_type=="mars") then
     ! For Mars we transmit day_ini
Index: trunk/LMDZ.COMMON/libf/dyn3dpar/gcm.F
===================================================================
--- trunk/LMDZ.COMMON/libf/dyn3dpar/gcm.F	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/dyn3dpar/gcm.F	(revision 1682)
@@ -441,47 +441,4 @@
 
 c-----------------------------------------------------------------------
-c   Initialisation de la physique :
-c   -------------------------------
-
-      IF ((iflag_phys==1).or.(iflag_phys>=100)) THEN
-!         latfi(1)=rlatu(1)
-!         lonfi(1)=0.
-!         zcufi(1) = cu(1)
-!         zcvfi(1) = cv(1)
-!         DO j=2,jjm
-!            DO i=1,iim
-!               latfi((j-2)*iim+1+i)= rlatu(j)
-!               lonfi((j-2)*iim+1+i)= rlonv(i)
-!               zcufi((j-2)*iim+1+i) = cu((j-1)*iip1+i)
-!               zcvfi((j-2)*iim+1+i) = cv((j-1)*iip1+i)
-!            ENDDO
-!         ENDDO
-!         latfi(ngridmx)= rlatu(jjp1)
-!         lonfi(ngridmx)= 0.
-!         zcufi(ngridmx) = cu(ip1jm+1)
-!         zcvfi(ngridmx) = cv(ip1jm-iim)
-
-         ! build airefi(), mesh area on physics grid
-!         CALL gr_dyn_fi(1,iip1,jjp1,ngridmx,aire,airefi)
-         ! Poles are single points on physics grid
-!         airefi(1)=airefi(1)*iim
-!         airefi(ngridmx)=airefi(ngridmx)*iim
-
-! Physics
-#ifdef CPP_PHYS
-!         CALL iniphysiq(ngridmx,llm,daysec,day_ini,dtphys/nsplit_phys,
-!     &                latfi,lonfi,airefi,zcufi,zcvfi,rad,g,r,cpp,
-!     &                iflag_phys)
-         CALL iniphysiq(iim,jjm,llm,
-     &                distrib_phys(mpi_rank),comm_lmdz,
-     &                daysec,day_ini,dtphys/nsplit_phys,
-     &                rlatu,rlatv,rlonu,rlonv,aire,cu,cv,rad,g,r,cpp,
-     &                iflag_phys)
-#endif
-!         call_iniphys=.false.
-      ENDIF ! of IF (call_iniphys.and.(iflag_phys==1.or.iflag_phys>=100))
-
-
-c-----------------------------------------------------------------------
 c   Initialisation des I/O :
 c   ------------------------
@@ -519,4 +476,22 @@
 #endif
 
+
+c-----------------------------------------------------------------------
+c   Initialisation de la physique :
+c   -------------------------------
+
+      IF ((iflag_phys==1).or.(iflag_phys>=100)) THEN
+! Physics
+#ifdef CPP_PHYS
+         CALL iniphysiq(iim,jjm,llm,
+     &                distrib_phys(mpi_rank),comm_lmdz,
+     &                daysec,day_ini,dtphys/nsplit_phys,
+     &                rlatu,rlatv,rlonu,rlonv,aire,cu,cv,rad,g,r,cpp,
+     &                iflag_phys)
+#endif
+!         call_iniphys=.false.
+      ENDIF ! of IF (call_iniphys.and.(iflag_phys==1.or.iflag_phys>=100))
+
+
       if (planet_type=="mars") then
          ! For Mars we transmit day_ini
Index: trunk/LMDZ.COMMON/libf/dynphy_lonlat/inigeomphy_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/dynphy_lonlat/inigeomphy_mod.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/dynphy_lonlat/inigeomphy_mod.F90	(revision 1682)
@@ -73,5 +73,6 @@
   REAL,ALLOCATABLE,SAVE :: boundslonfi(:,:)
   REAL,ALLOCATABLE,SAVE :: boundslatfi(:,:)
-!$OMP THREADPRIVATE (latfi,lonfi,cufi,cvfi,airefi,boundslonfi,boundslatfi)
+  INTEGER,ALLOCATABLE,SAVE :: ind_cell_glo_fi(:)
+!$OMP THREADPRIVATE (latfi,lonfi,cufi,cvfi,airefi,boundslonfi,boundslatfi,ind_cell_glo_fi)
 
   ! Initialize Physics distibution and parameters and interface with dynamics
@@ -201,5 +202,6 @@
   ALLOCATE(boundslonfi(klon_omp,4))
   ALLOCATE(boundslatfi(klon_omp,4))
-!  CALL initcomgeomphy
+  ALLOCATE(ind_cell_glo_fi(klon_omp))
+
 
   offset = klon_mpi_begin - 1
@@ -211,8 +213,9 @@
   boundslonfi(1:klon_omp,:) = boundslonfi_glo(offset+klon_omp_begin:offset+klon_omp_end,:)
   boundslatfi(1:klon_omp,:) = boundslatfi_glo(offset+klon_omp_begin:offset+klon_omp_end,:)
+  ind_cell_glo_fi(1:klon_omp)=(/ (i,i=offset+klon_omp_begin,offset+klon_omp_end) /)
 
   ! copy over local grid longitudes and latitudes
   CALL init_geometry(klon_omp,lonfi,latfi,boundslonfi,boundslatfi, &
-                     airefi,cufi,cvfi)
+                     airefi,ind_cell_glo_fi,cufi,cvfi)
 
   ! copy over preff , ap(), bp(), etc 
Index: trunk/LMDZ.COMMON/libf/dynphy_lonlat/phyvenus/iniphysiq_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/dynphy_lonlat/phyvenus/iniphysiq_mod.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/dynphy_lonlat/phyvenus/iniphysiq_mod.F90	(revision 1682)
@@ -72,6 +72,6 @@
 !$OMP PARALLEL
 
-  ! Initialize dimphy module
-  call init_dimphy(klon_omp,nlayer)
+  ! Initialize dimphy module => Now done in physics_distribution_mod
+  !call init_dimphy(klon_omp,nlayer)
 
   ! Initialize some physical constants
Index: trunk/LMDZ.COMMON/libf/misc/handle_err_m.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/misc/handle_err_m.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/misc/handle_err_m.F90	(revision 1682)
@@ -39,5 +39,5 @@
           end if
        end if
-       call abort_gcm("NetCDF95 handle_err", "", 1)
+       call abort_physic("NetCDF95 handle_err", "", 1)
     end if
 
Index: trunk/LMDZ.COMMON/libf/misc/wxios.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/misc/wxios.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/misc/wxios.F90	(revision 1682)
@@ -15,5 +15,5 @@
     
     INTEGER, SAVE :: g_comm
-    CHARACTER(len=100), SAVE :: g_ctx_name
+    CHARACTER(len=100), SAVE :: g_ctx_name ="LMDZ"
     TYPE(xios_context), SAVE :: g_ctx
 !$OMP THREADPRIVATE(g_comm,g_cts_name,g_ctx)
@@ -136,8 +136,8 @@
         g_ctx_name = xios_ctx_name
         
-        ! Si couple alors init fait dans cpl_init
-        IF (.not. PRESENT(type_ocean)) THEN
-            CALL wxios_context_init()
-        ENDIF
+!        ! Si couple alors init fait dans cpl_init
+!        IF (.not. PRESENT(type_ocean)) THEN
+!            CALL wxios_context_init()
+!        ENDIF
 
     END SUBROUTINE wxios_init
@@ -145,5 +145,5 @@
     SUBROUTINE wxios_context_init()
         USE print_control_mod, ONLY : prt_level, lunout
-!        USE mod_phys_lmdz_mpi_data, ONLY : COMM_LMDZ_PHY
+        USE mod_phys_lmdz_mpi_data, ONLY : COMM_LMDZ_PHY
         IMPLICIT NONE
 
@@ -152,5 +152,5 @@
 !$OMP MASTER
         !Initialisation du contexte:
-        CALL xios_context_initialize(g_ctx_name, g_comm)
+        CALL xios_context_initialize(g_ctx_name, COMM_LMDZ_PHY)
         CALL xios_get_handle(g_ctx_name, xios_ctx)    !Récupération
         CALL xios_set_current_context(xios_ctx)            !Activation
@@ -165,4 +165,16 @@
 !$OMP END MASTER
     END SUBROUTINE wxios_context_init
+
+
+    SUBROUTINE wxios_set_context()
+        IMPLICIT NONE
+        TYPE(xios_context) :: xios_ctx
+
+       !$OMP MASTER
+        CALL xios_get_handle(g_ctx_name, xios_ctx)    !Récupération
+        CALL xios_set_current_context(xios_ctx)            !Activation
+       !$OMP END MASTER
+
+    END SUBROUTINE wxios_set_context
 
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -203,5 +215,5 @@
             CASE DEFAULT
                 abort_message = 'wxios_set_cal: Mauvais choix de calendrier'
-                CALL abort_gcm('Gcm:Xios',abort_message,1)
+                CALL abort_physic('Gcm:Xios',abort_message,1)
         END SELECT
         
@@ -210,4 +222,5 @@
         
         IF (prt_level >= 10) WRITE(lunout,*) "wxios_set_cal: Time origin: ", date
+
         CALL xios_set_time_origin(xios_date(annee,mois,jour,int(heure),0,0))
 
@@ -237,62 +250,75 @@
     ! 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,flip_coordinates)
+       USE dimphy, only: klon
+       USE mod_phys_lmdz_transfert_para, ONLY: gather, bcast
+       USE mod_phys_lmdz_para, only: jj_nb, jj_begin, jj_end, ii_begin, ii_end, &
+                                     mpi_size, mpi_rank, klon_mpi, &
+                                     is_sequential, is_south_pole_dyn
+       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
-        
+        LOGICAL,OPTIONAL,INTENT(IN) :: flip_coordinates ! .true. to change signs
+                                                        ! of coordinates 
+        LOGICAL :: flip_axes=.false. ! default; do not swap axes
+        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)
+
+        IF (flip_axes) THEN
+          ! change signs of axes
+          CALL gather(-latitude_deg,rlat_glo) 
+          CALL bcast(rlat_glo)
+          CALL gather(-longitude_deg,rlon_glo)
+          CALL bcast(rlon_glo)
+        ELSE
+          CALL gather(latitude_deg,rlat_glo) 
+          CALL bcast(rlat_glo)
+          CALL gather(longitude_deg,rlon_glo)
+          CALL bcast(rlon_glo)
+        ENDIF
+    
+  !$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_dyn)) 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)
@@ -306,6 +332,58 @@
             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(dom_id,flip_coordinates)
+        USE geometry_mod, ONLY : longitude, latitude, boundslon, boundslat,ind_cell_glo
+        USE mod_grid_phy_lmdz, ONLY : nvertex, klon_glo
+        USE mod_phys_lmdz_para 
+        USE nrtype, ONLY : PI
+        IMPLICIT NONE
+        CHARACTER(len=*),INTENT(IN) :: dom_id ! domain identifier
+        LOGICAL,OPTIONAL,INTENT(IN) :: flip_coordinates ! .true. to change signs
+                                                        ! of coordinates 
+        LOGICAL :: flip_axes=.false. ! default; do not swap axes
+        REAL :: lon_mpi(klon_mpi)
+        REAL :: lat_mpi(klon_mpi)
+        REAL :: boundslon_mpi(klon_mpi,nvertex)
+        REAL :: boundslat_mpi(klon_mpi,nvertex)
+        INTEGER :: ind_cell_glo_mpi(klon_mpi)
+        TYPE(xios_domaingroup) :: dom
+
+        IF (PRESENT(flip_coordinates)) flip_axes=flip_coordinates
+        
+        IF (flip_axes) THEN
+          ! change signs of axes and boundaries
+          CALL gather_omp(-longitude*180/PI,lon_mpi)
+          CALL gather_omp(-latitude*180/PI,lat_mpi)
+          CALL gather_omp(-boundslon*180/PI,boundslon_mpi)
+          CALL gather_omp(-boundslat*180/PI,boundslat_mpi)
+        ELSE
+          CALL gather_omp(longitude*180/PI,lon_mpi)
+          CALL gather_omp(latitude*180/PI,lat_mpi)
+          CALL gather_omp(boundslon*180/PI,boundslon_mpi)
+          CALL gather_omp(boundslat*180/PI,boundslat_mpi)
+        ENDIF
+        CALL gather_omp(ind_cell_glo,ind_cell_glo_mpi)
+        
+
+!$OMP MASTER
+        CALL xios_get_domaingroup_handle(dom_id, dom)
+        
+        !On parametrise le domaine:
+        CALL xios_set_attr(dom, ni_glo=klon_glo, ibegin=ij_begin-1, ni=ij_nb, type="unstructured")
+        CALL xios_set_attr(dom, nvertex=nvertex, lonvalue_1d=lon_mpi, latvalue_1d=lat_mpi, &
+                           bounds_lon_1d=TRANSPOSE(boundslon_mpi), bounds_lat_1d=TRANSPOSE(boundslat_mpi) )
+        CALL xios_set_attr(dom, i_index=ind_cell_glo_mpi(:)-1)
+!$OMP END MASTER
+
+    END SUBROUTINE wxios_domain_param_unstructured
+
+
+
+
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     ! Pour déclarer un axe vertical !!!!!!!!!!!!!!!
Index: trunk/LMDZ.COMMON/libf/phy_common/geometry_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/geometry_mod.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/geometry_mod.F90	(revision 1682)
@@ -30,4 +30,6 @@
 !$OMP THREADPRIVATE(cell_area)
 
+  INTEGER,SAVE,ALLOCATABLE :: ind_cell_glo(:)      ! global index of a local cell
+!$OMP THREADPRIVATE(ind_cell_glo)
 
 CONTAINS
@@ -35,5 +37,5 @@
   SUBROUTINE init_geometry(klon,longitude_,latitude_, &
                            boundslon_,boundslat_, &
-                           cell_area_,dx_,dy_)
+                           cell_area_,ind_cell_glo_,dx_,dy_)
   USE mod_grid_phy_lmdz, ONLY: nvertex
   USE nrtype, ONLY : PI
@@ -45,4 +47,5 @@
     REAL,INTENT(IN) :: boundslat_(klon,nvertex)
     REAL,INTENT(IN) :: cell_area_(klon)
+    INTEGER,OPTIONAL,INTENT(IN) :: ind_cell_glo_(klon)
     REAL,OPTIONAL,INTENT(IN) :: dx_(klon)
     REAL,OPTIONAL,INTENT(IN) :: dy_(klon)
@@ -55,4 +58,5 @@
     ALLOCATE(boundslat(klon,nvertex))
     ALLOCATE(cell_area(klon))
+    IF (PRESENT(ind_cell_glo_)) ALLOCATE(ind_cell_glo(klon))
     IF (PRESENT(dx_)) ALLOCATE(dx(klon))
     IF (PRESENT(dy_))ALLOCATE(dy(klon))
@@ -65,4 +69,5 @@
     boundslat(:,:) = boundslat_(:,:)
     cell_area(:) = cell_area_(:)
+    IF (PRESENT(ind_cell_glo_)) ind_cell_glo(:) = ind_cell_glo_(:)
     IF (PRESENT(dx_)) dx(:) = dx_(:)
     IF (PRESENT(dy_)) dy(:) = dy_(:)
Index: trunk/LMDZ.COMMON/libf/phy_common/init_print_control_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/init_print_control_mod.F90	(revision 1682)
+++ trunk/LMDZ.COMMON/libf/phy_common/init_print_control_mod.F90	(revision 1682)
@@ -0,0 +1,67 @@
+MODULE init_print_control_mod
+
+! init_print_control to initialize print_control_mod variables
+! not included there because of circular dependecy issues
+
+CONTAINS
+
+  SUBROUTINE init_print_control
+  USE print_control_mod, ONLY : set_print_control
+  USE ioipsl_getin_p_mod, ONLY : getin_p
+  USE mod_phys_lmdz_para, ONLY: is_omp_root, is_master
+  IMPLICIT NONE
+
+    INTEGER :: lunout ! default output file identifier (6==screen)
+    INTEGER :: prt_level ! Output level
+    LOGICAL :: debug ! flag to specify if in "debug mode"
+    LOGICAL :: opened
+    INTEGER :: number
+    
+    !Config  Key  = prt_level
+    !Config  Desc = niveau d'impressions de débogage
+    !Config  Def  = 0
+    !Config  Help = Niveau d'impression pour le débogage
+    !Config         (0 = minimum d'impression)
+    prt_level = 0
+    CALL getin_p('prt_level',prt_level)
+
+    !Config  Key  = lunout
+    !Config  Desc = unite de fichier pour les impressions
+    !Config  Def  = 6
+    !Config  Help = unite de fichier pour les impressions 
+    !Config         (defaut sortie standard = 6)
+    lunout=6
+    CALL getin_p('lunout', lunout)
+
+    IF (is_omp_root) THEN
+      IF (lunout /= 5 .and. lunout /= 6) THEN
+         INQUIRE(FILE='lmdz.out_0000',OPENED=opened,NUMBER=number)
+         IF (opened) THEN
+           lunout=number
+         ELSE
+           OPEN(UNIT=lunout,FILE='lmdz.out_0000',ACTION='write',  &
+                STATUS='unknown',FORM='formatted')
+         ENDIF
+      ENDIF
+    ENDIF
+
+    !Config  Key  = debug
+    !Config  Desc = mode debogage
+    !Config  Def  = false
+    !Config  Help = positionne le mode debogage
+
+    debug = .FALSE.
+    CALL getin_p('debug',debug)
+    
+    IF (is_master) THEN
+      WRITE(lunout,*)"init_print_control: prt_level=",prt_level
+      WRITE(lunout,*)"init_print_control: lunout=",lunout
+      WRITE(lunout,*)"init_print_control: debug=",debug      
+    ENDIF
+    
+    CALL set_print_control(lunout,prt_level,debug)
+
+  END SUBROUTINE init_print_control  
+
+END MODULE init_print_control_mod
+
Index: trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_mpi_data.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_mpi_data.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_mpi_data.F90	(revision 1682)
@@ -3,5 +3,4 @@
 !
 MODULE mod_phys_lmdz_mpi_data
-!  USE mod_const_mpi
   
   INTEGER,SAVE :: ii_begin
@@ -36,5 +35,4 @@
   INTEGER,SAVE :: mpi_size
   INTEGER,SAVE :: mpi_master
-!  INTEGER,SAVE :: mpi_root
   LOGICAL,SAVE :: is_mpi_root
   LOGICAL,SAVE :: is_using_mpi
@@ -48,15 +46,13 @@
 CONTAINS
   
-!  SUBROUTINE Init_phys_lmdz_mpi_data(iim,jjp1,nb_proc,distrib)
   SUBROUTINE init_phys_lmdz_mpi_data(nbp, nbp_lon, nbp_lat, communicator)
-!  USE mod_const_mpi, ONLY : COMM_LMDZ
   IMPLICIT NONE
 #ifdef CPP_MPI
     INCLUDE 'mpif.h'
 #endif
-    INTEGER,INTENT(in) :: nbp
-    INTEGER,INTENT(in) :: nbp_lon
-    INTEGER,INTENT(in) :: nbp_lat
-    INTEGER,INTENT(in) :: communicator
+    INTEGER,INTENT(IN) :: nbp
+    INTEGER,INTENT(IN) :: nbp_lon
+    INTEGER,INTENT(IN) :: nbp_lat
+    INTEGER,INTENT(IN) :: communicator
     
     INTEGER,ALLOCATABLE :: distrib(:)
@@ -187,7 +183,6 @@
 
   SUBROUTINE print_module_data
-!  USE print_control_mod, ONLY: lunout
+  USE print_control_mod, ONLY: lunout
   IMPLICIT NONE
-  INCLUDE "iniprint.h" 
   
     WRITE(lunout,*) 'ii_begin =', ii_begin
Index: trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_omp_data.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_omp_data.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_omp_data.F90	(revision 1682)
@@ -7,4 +7,5 @@
   INTEGER,SAVE :: omp_rank
   LOGICAL,SAVE :: is_omp_root
+  LOGICAL,SAVE :: is_omp_master  ! alias of is_omp_root
   LOGICAL,SAVE :: is_using_omp
   LOGICAL,SAVE :: is_north_pole_phy, is_south_pole_phy
@@ -17,5 +18,5 @@
   INTEGER,SAVE :: klon_omp_begin
   INTEGER,SAVE :: klon_omp_end
-!$OMP  THREADPRIVATE(omp_rank,klon_omp,is_omp_root,klon_omp_begin,klon_omp_end)
+!$OMP  THREADPRIVATE(omp_rank,klon_omp,is_omp_root,is_omp_master,klon_omp_begin,klon_omp_end)
 !$OMP  THREADPRIVATE(is_north_pole_phy, is_south_pole_phy)
 
@@ -60,8 +61,8 @@
    ELSE
      abort_message = 'ANORMAL : OMP_MASTER /= 0'
-     CALL abort_gcm (modname,abort_message,1)
+     CALL abort_physic (modname,abort_message,1)
    ENDIF
 !$OMP END MASTER
-
+   is_omp_master=is_omp_root
 
 !$OMP MASTER 
@@ -106,6 +107,6 @@
 
   SUBROUTINE Print_module_data
+  USE print_control_mod, ONLY: lunout
   IMPLICIT NONE
-  INCLUDE "iniprint.h"
 
 !$OMP CRITICAL  
Index: trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_para.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_para.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/mod_phys_lmdz_para.F90	(revision 1682)
@@ -19,8 +19,8 @@
   SUBROUTINE Init_phys_lmdz_para(nbp,nbp_lon,nbp_lat,communicator)
   IMPLICIT NONE
-    INTEGER,INTENT(in) :: nbp
-    INTEGER,INTENT(in) :: nbp_lon
-    INTEGER,INTENT(in) :: nbp_lat
-    INTEGER,INTENT(in) :: communicator
+    INTEGER,INTENT(IN) :: nbp
+    INTEGER,INTENT(IN) :: nbp_lon
+    INTEGER,INTENT(IN) :: nbp_lat
+    INTEGER,INTENT(IN) :: communicator
 
     CALL Init_phys_lmdz_mpi_data(nbp,nbp_lon,nbp_lat,communicator)
@@ -49,6 +49,6 @@
   SUBROUTINE Test_transfert
   USE mod_grid_phy_lmdz
+  USE print_control_mod, ONLY: lunout
   IMPLICIT NONE
-    INCLUDE "iniprint.h"
  
     REAL :: Test_Field1d_glo(klon_glo,nbp_lev)
Index: trunk/LMDZ.COMMON/libf/phy_common/physics_distribution_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/physics_distribution_mod.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/physics_distribution_mod.F90	(revision 1682)
@@ -10,6 +10,8 @@
                                        nbp, nbp_lon, nbp_lat, nbp_lev, &
                                        communicator)
-  USE mod_phys_lmdz_para, ONLY: init_phys_lmdz_para
+  USE mod_phys_lmdz_para, ONLY: init_phys_lmdz_para, klon_omp
   USE mod_grid_phy_lmdz, ONLY: init_grid_phy_lmdz
+  USE dimphy, ONLY : Init_dimphy
+
   IMPLICIT NONE
     INTEGER,INTENT(IN) :: grid_type 
@@ -24,4 +26,7 @@
     CALL init_grid_phy_lmdz(grid_type,nvertex, nbp_lon,nbp_lat,nbp_lev)
     CALL init_phys_lmdz_para(nbp,nbp_lon, nbp_lat, communicator)
+!$OMP PARALLEL
+    CALL init_dimphy(klon_omp,nbp_lev)
+!$OMP END PARALLEL
 
   END SUBROUTINE init_physics_distribution  
Index: trunk/LMDZ.COMMON/libf/phy_common/print_control_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/print_control_mod.F90	(revision 1672)
+++ trunk/LMDZ.COMMON/libf/phy_common/print_control_mod.F90	(revision 1682)
@@ -7,57 +7,21 @@
 !$OMP THREADPRIVATE(lunout,prt_level,debug)
 
+  ! NB: Module variable Initializations done by set_print_control
+  !     routine from init_print_control_mod to avoid circular
+  !     module dependencies
+
 CONTAINS
 
-  SUBROUTINE init_print_control
-  USE ioipsl_getin_p_mod, ONLY : getin_p
-  USE mod_phys_lmdz_para, ONLY: is_omp_root, is_master
+  SUBROUTINE set_print_control(lunout_,prt_level_,debug_)
   IMPLICIT NONE
-
-    LOGICAL :: opened
-    INTEGER :: number
+    INTEGER :: lunout_
+    INTEGER :: prt_level_
+    LOGICAL :: debug_
+      
+    lunout = lunout_
+    prt_level = prt_level_
+    debug = debug_
     
-    !Config  Key  = prt_level
-    !Config  Desc = niveau d'impressions de débogage
-    !Config  Def  = 0
-    !Config  Help = Niveau d'impression pour le débogage
-    !Config         (0 = minimum d'impression)
-    prt_level = 0
-    CALL getin_p('prt_level',prt_level)
-
-    !Config  Key  = lunout
-    !Config  Desc = unite de fichier pour les impressions
-    !Config  Def  = 6
-    !Config  Help = unite de fichier pour les impressions 
-    !Config         (defaut sortie standard = 6)
-    lunout=6
-    CALL getin_p('lunout', lunout)
-
-    IF (is_omp_root) THEN
-      IF (lunout /= 5 .and. lunout /= 6) THEN
-         INQUIRE(FILE='lmdz.out_0000',OPENED=opened,NUMBER=number)
-         IF (opened) THEN
-           lunout=number
-         ELSE
-           OPEN(UNIT=lunout,FILE='lmdz.out_0000',ACTION='write',  &
-                STATUS='unknown',FORM='formatted')
-         ENDIF
-      ENDIF
-    ENDIF
-
-    !Config  Key  = debug
-    !Config  Desc = mode debogage
-    !Config  Def  = false
-    !Config  Help = positionne le mode debogage
-
-    debug = .FALSE.
-    CALL getin_p('debug',debug)
-    
-    IF (is_master) THEN
-      WRITE(lunout,*)"init_print_control: prt_level=",prt_level
-      WRITE(lunout,*)"init_print_control: lunout=",lunout
-      WRITE(lunout,*)"init_print_control: debug=",debug      
-    ENDIF
-    
-  END SUBROUTINE init_print_control  
+  END SUBROUTINE set_print_control
 
 END MODULE print_control_mod
Index: trunk/LMDZ.COMMON/libf/phy_common/xios_writefield.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/phy_common/xios_writefield.F90	(revision 1682)
+++ trunk/LMDZ.COMMON/libf/phy_common/xios_writefield.F90	(revision 1682)
@@ -0,0 +1,294 @@
+MODULE xios_writefield_mod
+#ifdef CPP_XIOS
+  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
+
+#endif
+END MODULE
