Index: XIOS3/branches/xios-3.0-beta/bld.cfg
===================================================================
--- XIOS3/branches/xios-3.0-beta/bld.cfg	(revision 2414)
+++ XIOS3/branches/xios-3.0-beta/bld.cfg	(revision 2417)
@@ -53,4 +53,5 @@
 bld::target xios_server.exe 
 bld::target generic_testcase.exe 
+bld::target test_ugrid.exe
 #bld::target test_regular.exe
 #bld::target test_xios2_cmip6.exe
Index: XIOS3/branches/xios-3.0-beta/inputs/ugrid_test/iodef.xml
===================================================================
--- XIOS3/branches/xios-3.0-beta/inputs/ugrid_test/iodef.xml	(revision 2417)
+++ XIOS3/branches/xios-3.0-beta/inputs/ugrid_test/iodef.xml	(revision 2417)
@@ -0,0 +1,55 @@
+<?xml version="1.0"?>
+<simulation>
+
+ <context id="client">
+   <calendar type="Gregorian" start_date="2012-03-01 15:00:00" />
+
+
+   <field_definition>
+     <field id="field_cell"  operation="once" domain_ref="cells" />
+     <field id="field_edge"  operation="once" domain_ref="edges" />
+     <field id="field_node"  operation="once" domain_ref="nodes" />
+   </field_definition>
+
+
+   <file_definition type="one_file" convention="UGRID">
+     <file id="test_ugrid" name="output" output_freq="4h" enabled=".TRUE." >
+       <field field_ref="field_node" enabled="false"/>
+       <field field_ref="field_edge" enabled="false"/>
+       <field field_ref="field_cell" enabled="true"/>
+     </file>
+   </file_definition>
+
+
+   <domain_definition>
+     <domain id="cells" name="cube"/>
+     <domain id="edges" name="cube"/>
+     <domain id="nodes" name="cube"/>
+   </domain_definition>
+
+  </context>
+
+
+
+  <context id="xios">
+      <variable_definition>
+         <variable_group id="server">
+            <variable id="using_server2" type="bool">false</variable>
+            <variable id="ratio_server2" type="int">50</variable>
+         </variable_group>
+
+        <variable_group id="buffer">
+            <variable id="optimal_buffer_size" type="string">performance</variable>
+            <variable id="buffer_size_factor" type="double">10.0</variable>
+         </variable_group>
+
+        <variable_group id="parameters" >
+          <variable id="using_server" type="bool">false</variable>
+          <variable id="info_level" type="int">80</variable>
+          <variable id="print_file" type="bool">true</variable>
+        </variable_group>
+      </variable_definition>
+  </context>
+
+</simulation>
+
Index: XIOS3/branches/xios-3.0-beta/src/io/nc4_data_output.cpp
===================================================================
--- XIOS3/branches/xios-3.0-beta/src/io/nc4_data_output.cpp	(revision 2414)
+++ XIOS3/branches/xios-3.0-beta/src/io/nc4_data_output.cpp	(revision 2417)
@@ -492,6 +492,4 @@
       std::vector<StdString> dim0;
       StdString domainName = domain->name;
-      domain->assignMesh(domainName, domain->nvertex);
-      domain->mesh->createMeshEpsilon(context->intraComm_, domain->lonvalue, domain->latvalue, domain->bounds_lonvalue, domain->bounds_latvalue);
 
       StdString node_x = domainName + "_node_x";
@@ -609,4 +607,6 @@
                 SuperClassWriter::addAttribute("edge_coordinates", edge_x + " " + edge_y, &domainName);
                 SuperClassWriter::addAttribute("edge_node_connectivity", edge_nodes, &domainName);
+                SuperClassWriter::addAttribute("face_edge_connectivity", face_edges, &domainName);
+                SuperClassWriter::addAttribute("edge_face_connectivity", edge_faces, &domainName);
                 SuperClassWriter::addDimension(dimEdge, domain->mesh->nbEdgesGlo);
                 dim0.clear();
@@ -628,4 +628,5 @@
                 SuperClassWriter::addAttribute("start_index", 0, &edge_nodes);
               }
+              SuperClassWriter::addAttribute("face_face_connectivity", face_faces, &domainName);
               SuperClassWriter::addAttribute("face_coordinates", face_x + " " + face_y, &domainName);
               SuperClassWriter::addAttribute("face_node_connectivity", face_nodes, &domainName);
@@ -661,5 +662,5 @@
               dim0.push_back(dimTwo);
               SuperClassWriter::addVariable(edge_faces, NC_INT, dim0, compressionLevel);
-              SuperClassWriter::addAttribute("cf_role", StdString("edge_face connectivity"), &edge_faces);
+              SuperClassWriter::addAttribute("cf_role", StdString("edge_face_connectivity"), &edge_faces);
               SuperClassWriter::addAttribute("long_name", StdString("neighbor faces for edges"), &edge_faces);
               SuperClassWriter::addAttribute("start_index", 0, &edge_faces);
@@ -670,5 +671,5 @@
               dim0.push_back(dimVertex);
               SuperClassWriter::addVariable(face_faces, NC_INT, dim0, compressionLevel);
-              SuperClassWriter::addAttribute("cf_role", StdString("face_face connectivity"), &face_faces);
+              SuperClassWriter::addAttribute("cf_role", StdString("face_face_connectivity"), &face_faces);
               SuperClassWriter::addAttribute("long_name", StdString("Indicates which other faces neighbor each face"), &face_faces);
               SuperClassWriter::addAttribute("start_index", 0, &face_faces);
@@ -729,4 +730,5 @@
                  startNodes[0] = domain->mesh->node_start;
                  countNodes[0] = domain->mesh->node_count;
+                 if (countNodes[0]==0) startNodes[0]=0 ; // for netcdf error
                  startEdgeNodes[0] = domain->ibegin;
                  startEdgeNodes[1] = 0;
@@ -765,17 +767,25 @@
                  startNodes[0] = domain->mesh->node_start;
                  countNodes[0] = domain->mesh->node_count;
+                 if (countNodes[0]==0) startNodes[0]=0;
+
                  startEdges[0] = domain->mesh->edge_start;
                  countEdges[0] = domain->mesh->edge_count;
+                 if (countEdges[0]==0) startEdges[0]=0 ; // for netcdf error
+
                  startEdgeNodes[0] = domain->mesh->edge_start;
+                 countEdgeNodes[0] = domain->mesh->edge_count;
+                 if (countEdgeNodes[0]==0) startEdgeNodes[0]=0; // for netcdf error
                  startEdgeNodes[1] = 0;
-                 countEdgeNodes[0] = domain->mesh->edge_count;
                  countEdgeNodes[1]= 2;
+                 
                  startEdgeFaces[0] = domain->mesh->edge_start;
+                 countEdgeFaces[0] = domain->mesh->edge_count;
+                 if (countEdgeFaces[0]==0) startEdgeFaces[0]=0 ; // for netcdf error
                  startEdgeFaces[1]= 0;
-                 countEdgeFaces[0] = domain->mesh->edge_count;
                  countEdgeFaces[1]= 2;
+                 
                  startFaceConctv[0] = domain->ibegin;
+                 countFaceConctv[0] = domain->ni;
                  startFaceConctv[1] = 0;
-                 countFaceConctv[0] = domain->ni;
                  countFaceConctv[1] = domain->nvertex;
                }
Index: XIOS3/branches/xios-3.0-beta/src/node/file.cpp
===================================================================
--- XIOS3/branches/xios-3.0-beta/src/node/file.cpp	(revision 2414)
+++ XIOS3/branches/xios-3.0-beta/src/node/file.cpp	(revision 2417)
@@ -863,4 +863,5 @@
      std::vector<int> domainNvertices;
      std::vector<StdString> domainNames;
+     std::map<string, tuple<set<CDomain*>,set<CDomain*>,set<CDomain*>>> registeredDomains ;
 
      for (int i = 0; i < size; ++i)
@@ -905,4 +906,60 @@
          domainNvertices.push_back(nvertex);
        }
+
+       if (nvertex==1)  std::get<0>(registeredDomains[domainName]).insert(domain[0]) ;
+       else if (nvertex==2) std::get<1>(registeredDomains[domainName]).insert(domain[0]) ;
+       else  std::get<2>(registeredDomains[domainName]).insert(domain[0]) ;
+     }
+
+     for(auto& it:registeredDomains)
+     {
+       list<CDomain*> domains ;
+       string domainName=it.first ;
+
+       for(auto& domain : std::get<0>(it.second) ) domains.push_back(domain) ;
+       for(auto& domain : std::get<1>(it.second) ) domains.push_back(domain) ;
+       for(auto& domain : std::get<2>(it.second) ) domains.push_back(domain) ;
+       
+       // for each component of a given mesh (i.e. domains with same name but different number of vertices)
+       // associate the UGRID mesh in increasing order
+       for(auto& domain : domains )
+       {
+         //domain-> computeWrittenIndex();
+         //CArray<int, 1>& indexToWrite = domain->localIndexToWriteOnServer;
+         CArray<int, 1> indexToWrite = domain->getLocalView(CElementView::WORKFLOW)->getIndex();
+         int nbWritten = indexToWrite.numElements();
+
+         CArray<double,1> writtenLat, writtenLon;
+         CArray<double,2> writtenBndsLat, writtenBndsLon;
+
+         writtenLat.resize(nbWritten);
+         writtenLon.resize(nbWritten);
+         for (int idx = 0; idx < nbWritten; ++idx)
+         {
+           writtenLat(idx) = domain->latvalue(indexToWrite(idx));
+           writtenLon(idx) = domain->lonvalue(indexToWrite(idx));
+         }
+    
+         int nvertex = domain->nvertex, idx;
+        if (nvertex>1)
+         {
+           writtenBndsLat.resize(nvertex, nbWritten);
+           writtenBndsLon.resize(nvertex, nbWritten);
+           CArray<double,2>& boundslat = domain->bounds_latvalue;
+           CArray<double,2>& boundslon = domain->bounds_lonvalue;
+           for (idx = 0; idx < nbWritten; ++idx)
+             for (int nv = 0; nv < nvertex; ++nv)
+             {
+                writtenBndsLat(nv, idx) = boundslat(nv, int(indexToWrite(idx)));
+                writtenBndsLon(nv, idx) = boundslon(nv, int(indexToWrite(idx)));
+             }
+         }
+         domain->assignMesh(domainName, domain->nvertex);
+         //CContextServer* server=CContext::getCurrent()->server ;
+         //domain->mesh->createMeshEpsilon(server->intraComm, writtenLon, writtenLat, writtenBndsLon, writtenBndsLat);
+         MPI_Comm intraComm =CContext::getCurrent()->getIntraComm() ;
+         domain->mesh->createMeshEpsilon(intraComm, writtenLon, writtenLat, writtenBndsLon, writtenBndsLat);
+       }
+
      }
    }
Index: XIOS3/branches/xios-3.0-beta/src/node/mesh.cpp
===================================================================
--- XIOS3/branches/xios-3.0-beta/src/node/mesh.cpp	(revision 2414)
+++ XIOS3/branches/xios-3.0-beta/src/node/mesh.cpp	(revision 2417)
@@ -430,4 +430,6 @@
     if (nvertex == 1)
     {
+      if (nodesAreWritten) return ;
+
       nbNodes_ = lonvalue.numElements();
       node_lon.resize(nbNodes_);
@@ -435,4 +437,15 @@
       node_lon = lonvalue;
       node_lat = latvalue;
+
+      unsigned long nodeCount = nbNodes_;
+      unsigned long nodeStart, nbNodes;
+      MPI_Scan(&nodeCount, &nodeStart, 1, MPI_UNSIGNED_LONG, MPI_SUM, comm);
+      int nNodes = nodeStart;
+      MPI_Bcast(&nNodes, 1, MPI_UNSIGNED_LONG, mpiSize-1, comm);
+      nbNodesGlo = nNodes;
+
+      nodeStart -= nodeCount;
+      node_start = nodeStart;
+      node_count = nodeCount;
 
       // Global node indexes
@@ -444,5 +457,5 @@
         for (size_t nh = 0; nh < 4; ++nh)
         {
-          nodeHash2IdxGlo[hashValues[nh]].push_back(mpiRank*nbNodes_ + nn);
+          nodeHash2IdxGlo[hashValues[nh]].push_back(nodeStart + nn);
         }
       }
@@ -453,4 +466,6 @@
     else if (nvertex == 2)
     {
+      if (edgesAreWritten) return ;
+
       nbEdges_ = bounds_lon.shape()[1];
       edge_lon.resize(nbEdges_);
@@ -463,6 +478,11 @@
       unsigned long nbEdgesOnProc = nbEdges_;
       unsigned long nbEdgesAccum;
+      unsigned long nbEdgesGlo;
       MPI_Scan(&nbEdgesOnProc, &nbEdgesAccum, 1, MPI_UNSIGNED_LONG, MPI_SUM, comm);
+      nbEdgesGlo = nbEdgesAccum ;
+      MPI_Bcast(&nbEdgesGlo, 1, MPI_UNSIGNED_LONG, mpiSize-1, comm);
       nbEdgesAccum -= nbEdges_;
+      edge_start = nbEdgesAccum ;
+      edge_count = nbEdgesOnProc ;
 
       CClientClientDHTSizet::Index2VectorInfoTypeMap edgeHash2IdxGlo;
@@ -674,6 +694,8 @@
     } //nvertex = 2
 
-    else
-    {
+    else // nvertex > 2
+    {
+      if (facesAreWritten) return ;
+
       nbFaces_ = bounds_lon.shape()[1];
       face_lon.resize(nbFaces_);
Index: XIOS3/branches/xios-3.0-beta/src/test/test_ugrid.f90
===================================================================
--- XIOS3/branches/xios-3.0-beta/src/test/test_ugrid.f90	(revision 2417)
+++ XIOS3/branches/xios-3.0-beta/src/test/test_ugrid.f90	(revision 2417)
@@ -0,0 +1,325 @@
+PROGRAM test_ugrid
+
+  USE xios
+  USE mod_wait
+  IMPLICIT NONE
+  INCLUDE "mpif.h"
+  INTEGER :: comm_rank
+  INTEGER :: comm_size
+  INTEGER :: ierr
+
+  CHARACTER(len=*),PARAMETER :: id="client"
+  INTEGER :: comm
+  TYPE(xios_duration) :: dtime
+  CHARACTER(len=20) :: dtime_str
+  TYPE(xios_date) :: date
+  CHARACTER(len=20) :: date_str
+  CHARACTER(len=15) :: calendar_type
+  TYPE(xios_context) :: ctx_hdl
+  INTEGER,PARAMETER :: ni_glo=100
+  INTEGER,PARAMETER :: nj_glo=100
+  INTEGER,PARAMETER :: llm=5
+  DOUBLE PRECISION  :: lval(llm)=1, scalar = 5
+  TYPE(xios_field) :: field_hdl
+  TYPE(xios_fieldgroup) :: fieldgroup_hdl
+  TYPE(xios_file) :: file_hdl
+  LOGICAL :: ok
+
+  DOUBLE PRECISION,DIMENSION(ni_glo,nj_glo) :: lon_glo,lat_glo
+  DOUBLE PRECISION :: field_A_glo(ni_glo,nj_glo,llm)
+  DOUBLE PRECISION,ALLOCATABLE :: lon(:,:),lat(:,:),field_A(:,:,:), lonvalue(:,:), axisValue(:), field_domain(:,:) ;
+  INTEGER :: ni,ibegin,iend,nj,jbegin,jend
+  INTEGER :: i,j,l,ts,n
+
+  REAL,PARAMETER    :: Pi = 4.*atan(1.)
+
+  INTEGER,PARAMETER :: ncells=6
+  REAL :: cellx(ncells),celly(ncells),cellz(ncells)
+  REAL :: cellx_bounds(4,ncells),celly_bounds(4,ncells),cellz_bounds(4,ncells)
+  REAL(8) :: cell_lon(ncells), cell_lat(ncells)
+  REAL(8) :: cell_bounds_lon(4,ncells), cell_bounds_lat(4,ncells)
+  REAL :: field_cell(ncells) = (/1,2,3,4,5,6/)
+
+  INTEGER,PARAMETER :: nedges=12
+  REAL :: edgex(nedges),edgey(nedges),edgez(nedges)
+  REAL :: edgex_bounds(2,nedges),edgey_bounds(2,nedges),edgez_bounds(2,nedges)
+  REAL(8) :: edge_lon(nedges), edge_lat(nedges)
+  REAL(8) :: edge_bounds_lon(2,nedges), edge_bounds_lat(2,nedges)
+  REAL :: field_edge(nedges) = (/1,2,3,4,5,6,7,8,9,10,11,12/)
+
+  INTEGER,PARAMETER :: nnodes=8
+  REAL :: nodex(nnodes),nodey(nnodes),nodez(nnodes)
+  REAL(8) :: node_lon(nnodes), node_lat(nnodes)
+  REAL :: field_node(nnodes) = (/1,2,3,4,5,6,7,8/)
+  
+    
+  cellx(1) = 1
+  celly(1) = 0  
+  cellz(1) = 0 
+  cellx_bounds(1,1)=  1 ; cellx_bounds(2,1)=  1 ; cellx_bounds(3,1)=  1  ; cellx_bounds(4,1)=  1
+  celly_bounds(1,1)= -1 ; celly_bounds(2,1)=  1 ; celly_bounds(3,1)=  1  ; celly_bounds(4,1)= -1
+  cellz_bounds(1,1)= -1 ; cellz_bounds(2,1)= -1 ; cellz_bounds(3,1)=  1  ; cellz_bounds(4,1)=  1
+  
+  cellx(2) = 0
+  celly(2) = 1  
+  cellz(2) = 0 
+  cellx_bounds(1,2)= -1 ; cellx_bounds(2,2)=  1 ; cellx_bounds(3,2)=  1  ; cellx_bounds(4,2)= -1
+  celly_bounds(1,2)=  1 ; celly_bounds(2,2)=  1 ; celly_bounds(3,2)=  1  ; celly_bounds(4,2)=  1
+  cellz_bounds(1,2)= -1 ; cellz_bounds(2,2)= -1 ; cellz_bounds(3,2)=  1  ; cellz_bounds(4,2)=  1
+    
+  cellx(3) = -1
+  celly(3) = 0  
+  cellz(3) = 0 
+  cellx_bounds(1,3)= -1 ; cellx_bounds(2,3)= -1 ; cellx_bounds(3,3)= -1  ; cellx_bounds(4,3)= -1
+  celly_bounds(1,3)= -1 ; celly_bounds(2,3)=  1 ; celly_bounds(3,3)=  1  ; celly_bounds(4,3)= -1
+  cellz_bounds(1,3)= -1 ; cellz_bounds(2,3)= -1 ; cellz_bounds(3,3)=  1  ; cellz_bounds(4,3)=  1
+  
+  cellx(4) = 0
+  celly(4) = -1  
+  cellz(4) = 0 
+  cellx_bounds(1,4)= -1 ; cellx_bounds(2,4)=  1 ; cellx_bounds(3,4)=  1  ; cellx_bounds(4,4)= -1
+  celly_bounds(1,4)= -1 ; celly_bounds(2,4)= -1 ; celly_bounds(3,4)= -1  ; celly_bounds(4,4)= -1
+  cellz_bounds(1,4)= -1 ; cellz_bounds(2,4)= -1 ; cellz_bounds(3,4)=  1  ; cellz_bounds(4,4)=  1
+    
+  cellx(5) = 0
+  celly(5) = 0  
+  cellz(5) = 1 
+  cellx_bounds(1,5)= -1 ; cellx_bounds(2,5)=  1 ; cellx_bounds(3,5)=  1  ; cellx_bounds(4,5)= -1
+  celly_bounds(1,5)= -1 ; celly_bounds(2,5)= -1 ; celly_bounds(3,5)=  1  ; celly_bounds(4,5)=  1
+  cellz_bounds(1,5)=  1 ; cellz_bounds(2,5)=  1 ; cellz_bounds(3,5)=  1  ; cellz_bounds(4,5)=  1
+  
+  cellx(6) = 0
+  celly(6) = 0  
+  cellz(6) = -1 
+  cellx_bounds(1,6)= -1 ; cellx_bounds(2,6)=  1 ; cellx_bounds(3,6)=  1  ; cellx_bounds(4,6)= -1
+  celly_bounds(1,6)= -1 ; celly_bounds(2,6)= -1 ; celly_bounds(3,6)=  1  ; celly_bounds(4,6)=  1
+  cellz_bounds(1,6)= -1 ; cellz_bounds(2,6)= -1 ; cellz_bounds(3,6)= -1  ; cellz_bounds(4,6)= -1
+
+  CALL cube2lonlat_1(cellx,celly,cellz,cell_lon,cell_lat)
+  CALL cube2lonlat_2(cellx_bounds,celly_bounds,cellz_bounds,cell_bounds_lon,cell_bounds_lat)
+
+  
+  edgex(1) =  1
+  edgey(1) =  0
+  edgez(1) =  1
+  edgex_bounds(1,1) =  1 ; edgex_bounds(2,1) =  1
+  edgey_bounds(1,1) = -1 ; edgey_bounds(2,1) =  1
+  edgez_bounds(1,1) =  1 ; edgez_bounds(2,1) =  1
+  
+  edgex(2) = -1
+  edgey(2) =  0
+  edgez(2) =  1
+  edgex_bounds(1,2) = -1 ; edgex_bounds(2,2) = -1
+  edgey_bounds(1,2) = -1 ; edgey_bounds(2,2) =  1
+  edgez_bounds(1,2) =  1 ; edgez_bounds(2,2) =  1
+  
+  edgex(3) =  0
+  edgey(3) =  1
+  edgez(3) =  1
+  edgex_bounds(1,3) = -1 ; edgex_bounds(2,3) =  1
+  edgey_bounds(1,3) =  1 ; edgey_bounds(2,3) =  1
+  edgez_bounds(1,3) =  1 ; edgez_bounds(2,3) =  1
+  
+  edgex(4) =  0
+  edgey(4) = -1
+  edgez(4) =  1
+  edgex_bounds(1,4) = -1 ; edgex_bounds(2,4) =  1
+  edgey_bounds(1,4) = -1 ; edgey_bounds(2,4) = -1
+  edgez_bounds(1,4) =  1 ; edgez_bounds(2,4) =  1
+
+  edgex(5) =  1
+  edgey(5) =  0
+  edgez(5) = -1
+  edgex_bounds(1,5) =  1 ; edgex_bounds(2,5) =  1
+  edgey_bounds(1,5) = -1 ; edgey_bounds(2,5) =  1
+  edgez_bounds(1,5) = -1 ; edgez_bounds(2,5) = -1
+  
+  edgex(6) = -1
+  edgey(6) =  0
+  edgez(6) =  -1
+  edgex_bounds(1,6) = -1 ; edgex_bounds(2,6) = -1
+  edgey_bounds(1,6) = -1 ; edgey_bounds(2,6) =  1
+  edgez_bounds(1,6) = -1 ; edgez_bounds(2,6) = -1
+  
+  edgex(7) =  0
+  edgey(7) =  1
+  edgez(7) =  -1
+  edgex_bounds(1,7) = -1 ; edgex_bounds(2,7) =  1
+  edgey_bounds(1,7) =  1 ; edgey_bounds(2,7) =  1
+  edgez_bounds(1,7) = -1 ; edgez_bounds(2,7) = -1
+  
+  edgex(8) =  0
+  edgey(8) = -1
+  edgez(8) =  -1
+  edgex_bounds(1,8) = -1 ; edgex_bounds(2,8) =  1
+  edgey_bounds(1,8) = -1 ; edgey_bounds(2,8) = -1
+  edgez_bounds(1,8) = -1 ; edgez_bounds(2,8) = -1
+ 
+  edgex(9) =  1
+  edgey(9) =  1
+  edgez(9) =  0
+  edgex_bounds(1,9) =  1 ; edgex_bounds(2,9) =  1
+  edgey_bounds(1,9) =  1 ; edgey_bounds(2,9) =  1
+  edgez_bounds(1,9) = -1 ; edgez_bounds(2,9) =  1
+
+  edgex(10) = -1
+  edgey(10) =  1
+  edgez(10) =  0
+  edgex_bounds(1,10) = -1 ; edgex_bounds(2,10) = -1
+  edgey_bounds(1,10) =  1 ; edgey_bounds(2,10) =  1
+  edgez_bounds(1,10) = -1 ; edgez_bounds(2,10) =  1
+
+  edgex(11) = -1
+  edgey(11) = -1
+  edgez(11) =  0
+  edgex_bounds(1,11) = -1 ; edgex_bounds(2,11) = -1
+  edgey_bounds(1,11) = -1 ; edgey_bounds(2,11) = -1
+  edgez_bounds(1,11) = -1 ; edgez_bounds(2,11) =  1
+
+  edgex(12) =  1
+  edgey(12) = -1
+  edgez(12) =  0
+  edgex_bounds(1,12) =  1 ; edgex_bounds(2,12) =  1
+  edgey_bounds(1,12) = -1 ; edgey_bounds(2,12) = -1
+  edgez_bounds(1,12) = -1 ; edgez_bounds(2,12) =  1
+
+  CALL cube2lonlat_1(edgex, edgey, edgez, edge_lon, edge_lat)
+  CALL cube2lonlat_2(edgex_bounds, edgey_bounds, edgez_bounds, edge_bounds_lon, edge_bounds_lat)
+
+  nodex(1) = -1
+  nodey(1) = -1
+  nodez(1) = -1
+
+  nodex(2) = 1
+  nodey(2) = -1
+  nodez(2) = -1
+
+  nodex(3) = 1
+  nodey(3) = 1
+  nodez(3) = -1
+
+  nodex(4) = -1
+  nodey(4) = 1
+  nodez(4) = -1
+
+  nodex(5) = -1
+  nodey(5) = -1
+  nodez(5) =  1
+
+  nodex(6) =  1
+  nodey(6) = -1
+  nodez(6) =  1
+
+  nodex(7) =  1
+  nodey(7) =  1
+  nodez(7) =  1
+
+  nodex(8) = -1
+  nodey(8) =  1
+  nodez(8) =  1
+
+  CALL cube2lonlat_1(nodex,nodey,nodez,node_lon,node_lat)
+    
+!!! MPI Initialization
+
+  CALL MPI_INIT(ierr)
+
+  CALL init_wait
+
+!!! XIOS Initialization (get the local communicator)
+
+  CALL xios_initialize(id,return_comm=comm)
+  CALL xios_context_initialize(id, comm)
+  
+  CALL MPI_COMM_RANK(comm,comm_rank,ierr)
+  CALL MPI_COMM_SIZE(comm,comm_size,ierr)
+
+  CALL xios_set_domain_attr("cells",ni_glo=ncells, ibegin=0, ni=ncells, nvertex=4 , type='unstructured')
+  CALL xios_set_domain_attr("cells",lonvalue_1d=cell_lon, latvalue_1d=cell_lat, bounds_lon_1d=cell_bounds_lon, bounds_lat_1d= cell_bounds_lat)
+ 
+  CALL xios_set_domain_attr("edges",ni_glo=nedges, ibegin=0, ni=nedges, nvertex=2 , type='unstructured')
+  CALL xios_set_domain_attr("edges",lonvalue_1d=edge_lon, latvalue_1d=edge_lat, bounds_lon_1d=edge_bounds_lon, bounds_lat_1d= edge_bounds_lat)
+ 
+  CALL xios_set_domain_attr("nodes",ni_glo=nnodes, ibegin=0, ni=nnodes, nvertex=1 , type='unstructured')
+  CALL xios_set_domain_attr("nodes",lonvalue_1d=node_lon, latvalue_1d=node_lat)
+
+  dtime%second = 3600
+  CALL xios_set_timestep(dtime)
+  CALL xios_close_context_definition
+  
+  DO ts=1,1
+    CALL xios_update_calendar(ts)
+    CALL xios_send_field("field_node",field_node) 
+    CALL xios_send_field("field_edge",field_edge) 
+    CALL xios_send_field("field_cell",field_cell) 
+    CALL wait_us(5000) ;
+  ENDDO
+
+  CALL xios_context_finalize()
+  CALL xios_finalize()
+
+  CALL MPI_FINALIZE(ierr)
+
+CONTAINS 
+  
+  SUBROUTINE cube2sphere(xc,yc,zc,xs,ys,zs)
+  IMPLICIT NONE
+  REAL,INTENT(IN)  :: xc, yc, zc
+  REAL,INTENT(OUT) :: xs, ys, zs
+  
+    xs=xc/sqrt(1-yc*yc/2-zc*zc/2+yc*yc*zc*zc/3)
+    ys=yc/sqrt(1-xc*xc/2-zc*zc/2+xc*xc*zc*zc/3)
+    zs=zc/sqrt(1-xc*xc/2-yc*yc/2+xc*xc*yc*yc/3)
+  END SUBROUTINE cube2sphere
+  
+  SUBROUTINE xyz2lonlat(x,y,z,lon,lat)
+  IMPLICIT NONE
+  REAL,INTENT(IN)  :: x,y,z
+  REAL(8),INTENT(OUT) :: lon,lat
+  REAL             :: norm
+  
+    norm=sqrt(x*x+y*y+z*z)
+  
+    lat=asin(z/norm)*180./Pi
+    lon=atan2(y/norm,x/norm)*180/Pi
+  END SUBROUTINE xyz2lonlat
+  
+  SUBROUTINE cube2lonlat_0(xc,yc,zc,lon,lat)
+  IMPLICIT NONE
+    REAL,INTENT(IN)  :: xc,yc,zc
+    REAL(8),INTENT(OUT) :: lon,lat
+    REAL             :: xs,ys,zs
+    CALL cube2sphere(xc,yc,zc,xs,ys,zs)
+    CALL xyz2lonlat(xs,ys,zs,lon,lat)
+  END SUBROUTINE  cube2lonlat_0
+
+  SUBROUTINE cube2lonlat_1(xc,yc,zc,lon,lat)
+  IMPLICIT NONE
+  REAL,INTENT(IN),DIMENSION(:)  :: xc,yc,zc
+  REAL(8),INTENT(OUT),DIMENSION(:) :: lon,lat
+  INTEGER :: i
+    DO i=1,SIZE(xc,1)
+      CALL cube2lonlat_0(xc(i),yc(i),zc(i),lon(i),lat(i))
+    ENDDO
+  END SUBROUTINE cube2lonlat_1
+ 
+  SUBROUTINE cube2lonlat_2(xc,yc,zc,lon,lat)
+  IMPLICIT NONE
+  REAL,INTENT(IN),DIMENSION(:,:)  :: xc,yc,zc
+  REAL(8),INTENT(OUT),DIMENSION(:,:) :: lon,lat
+  INTEGER :: i,j
+  
+    DO j=1,SIZE(xc,2)
+      DO i=1,SIZE(xc,1)
+        CALL cube2lonlat_0(xc(i,j),yc(i,j),zc(i,j),lon(i,j),lat(i,j))
+      ENDDO
+    ENDDO
+
+  END SUBROUTINE cube2lonlat_2
+
+END PROGRAM test_ugrid
+
+
+
+
+
