Index: XIOS/trunk/inputs/COMPLETE/context_atmosphere.xml
===================================================================
--- XIOS/trunk/inputs/COMPLETE/context_atmosphere.xml	(revision 486)
+++ XIOS/trunk/inputs/COMPLETE/context_atmosphere.xml	(revision 486)
@@ -0,0 +1,30 @@
+<!-- Context atm -->
+  
+<context id="atmopshere" calendar_type="Gregorian" start_date="2012-03-01 15:00:00" time_origin="2012-02-29 15:00:00">
+
+  <field_definition level="1" enabled=".FALSE." default_value="9.96921e+36">
+    <field id="field_A_atm"  name="field_A_atm" operation="average" freq_op="1ts" domain_ref="domain_atm"  axis_ref="axis_atm" />
+  </field_definition>
+  
+    
+  <file_definition type="multiple_file" par_access="collective" output_freq="6h" sync_freq="6h" output_level="10" enabled=".TRUE.">
+    <file id="output_atmosphere" name="output_atmosphere"> 
+      <field field_ref="field_A_atm" />
+    </file>
+    <file id="output_atmosphere_zoom" name="output_atmosphere_zoom"> 
+      <field field_ref="field_A_atm" name="field_A_atm_zoom" domain_ref="domain_atm_zoom" />
+    </file>
+  </file_definition>
+   
+   <axis_definition>
+     <axis id="axis_atm" zoom_size="2" zoom_end="3" />
+   </axis_definition>
+    
+   <domain_definition>
+     <domain id="domain_atm" />
+     <domain id="domain_atm_zoom" />
+   </domain_definition>    
+  
+   <grid_definition />    
+  
+</context>
Index: XIOS/trunk/inputs/COMPLETE/context_surface.xml
===================================================================
--- XIOS/trunk/inputs/COMPLETE/context_surface.xml	(revision 486)
+++ XIOS/trunk/inputs/COMPLETE/context_surface.xml	(revision 486)
@@ -0,0 +1,53 @@
+<!-- Context surface -->
+  
+<context id="surface" calendar_type="Julian" start_date="2012-03-01 15:00:00" time_origin="2012-02-29 15:00:00">
+
+  <field_definition level="1" enabled=".TRUE." domain_ref="domain_srf"  axis_ref="axis_srf" operation="average" freq_op="1ts" default_value="9.96921e+36" prec="4">
+    <field id="field_A_srf"  name="field_A_srf" long_name="Champ en degC" operation="average" unit="degC" />
+    <field id="field_A_srf_K" name="field_A_srf_K" field_ref="field_A_srf" long_name="Champ en degK" unit="degK"> field_A_srf + 273.15 </field>
+    <field id="field_A_srf_max" name="field_A_srf_max" field_ref="field_A_srf" long_name="maximum field_A_srf" operation="maximum" />
+    <field id="field_D_srf" name="field_D_srf" long_name="Somme champ A et C" > field_A_srf + field_C_srf </field>
+    
+  </field_definition>
+    
+  <file_definition type="multiple_file" par_access="collective" output_level="10" enabled=".TRUE.">
+    <file id="output_surface" name="output_surface_6h" output_freq="6h" > 
+      <field field_ref="field_A_srf" >
+	<variable id="my_attribute1" type="string" >surf_att</variable>
+	<variable id="my_attribute2" type="integer" > 10 </variable>
+	<variable id="my_attribute3" type="float" >6.8</variable>
+      </field>
+      <field field_ref="field_A_srf_K" />
+      <field field_ref="field_A_srf" name="field_A_srf_min" operation="minimum" />
+      <field field_ref="field_A_srf" name="field_A_srf_max" operation="maximum" />
+      <field field_ref="field_A_srf" name="field_A_srf_prec4" prec="8"/>
+      <field field_ref="field_A_srf_max" name="field_A_srf_mean_max" long_name="Average of hourly maximum over 6h" operation="average" freq_op="1h" > @field_A_srf_max </field>
+      <variable id="my_global_attribute" type="string" >file_6h</variable>
+    </file>
+
+    <file type="one_file" id="output_surface_1d" name="output_surface_1d" output_freq="1d" > 
+      <field field_ref="field_A_srf" >
+	<variable id="my_attribute1" type="string" >surf_att</variable>
+	<variable id="my_attribute2" type="integer" >10</variable>
+	<variable id="my_attribute3" type="float" >6.8</variable>
+      </field>
+      <field field_ref="field_A_srf_K" />
+      <field field_ref="field_A_srf" name="field_A_srf_min" operation="minimum" />
+      <field field_ref="field_A_srf" name="field_A_srf_max" operation="maximum" />
+      <field field_ref="field_A_srf" name="field_A_srf_prec4" prec="8"/>
+      <field field_ref="field_A_srf_max" name="field_A_srf_mean_max" long_name="Average of hourly maximum over 1d" operation="average" freq_op="1h" > @field_A_srf_max </field>
+      <variable id="my_global_attribute" type="string" >file_1d</variable>
+    </file>
+  </file_definition>
+ 
+   <axis_definition>
+     <axis id="axis_srf"/>
+   </axis_definition>
+    
+   <domain_definition>
+     <domain id="domain_srf" />
+   </domain_definition>    
+  
+   <grid_definition />    
+  
+</context>
Index: XIOS/trunk/inputs/COMPLETE/iodef.xml
===================================================================
--- XIOS/trunk/inputs/COMPLETE/iodef.xml	(revision 486)
+++ XIOS/trunk/inputs/COMPLETE/iodef.xml	(revision 486)
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<simulation> 
+
+  <context id="atmosphere" src="./context_atmosphere.xml"/>
+
+  <context id="surface" src="./context_surface.xml"/>
+
+  <context id="xios">
+      <variable_definition>
+        <variable_group id="buffer">
+            buffer_size = 80000000
+            buffer_server_factor_size = 2 
+         </variable_group>
+
+        <variable_group id="parameters" >
+          <variable id="using_server" type="boolean">false</variable>
+          <variable id="info_level" type="int">1</variable>
+        </variable_group>
+      </variable_definition>
+  </context>
+
+</simulation>
Index: XIOS/trunk/inputs/iodef_unstruct.xml
===================================================================
--- XIOS/trunk/inputs/iodef_unstruct.xml	(revision 485)
+++ 	(revision )
@@ -1,49 +1,0 @@
- <?xml version="1.0"?>
-<simulation> 
-
- <context id="test" calendar_type="Gregorian" start_date="2012-02-20 01:02:30">
- 
-   <field_definition level="1" enabled=".TRUE." prec="4" default_value="-1">
-     <field id="field_A"  operation="instant" domain_ref="domain_A"  axis_ref="axis_A"/>
-   </field_definition>
-    
-    
-   <file_definition type="one_file" output_freq="1d" output_level="10" enabled=".TRUE.">
-     <file id="output" name="output"> 
-       <field field_ref="field_A" />
-     </file>
-   </file_definition>
-    
-    
-   <axis_definition>
-     <axis id="axis_A" size="1" zoom_begin="1" zoom_size="1"/>
-   </axis_definition>
-    
-   <domain_definition>
-     <domain id="domain_A"/>
-   </domain_definition>    
-  
-   <grid_definition />    
-  
-  </context>
-  
-
-  <context id="xios">
-      <variable_definition>
-
-        <variable_group id="buffer">
-            buffer_size = 1000000000
-            buffer_server_factor_size = 2 
-            info_level = 50
-         </variable_group>
-         
-         <variable_group id="coupling">
-            <variable id="using_server" type="boolean">false</variable>
-            <variable id="using_oasis" type="boolean">false</variable>
-            <variable id="oasis_codes_id" type="string">oceanx</variable>
-         </variable_group>
-
-      </variable_definition>
-  </context>
-
-</simulation>
Index: XIOS/trunk/src/test/test_client.f90
===================================================================
--- XIOS/trunk/src/test/test_client.f90	(revision 485)
+++ XIOS/trunk/src/test/test_client.f90	(revision 486)
@@ -72,11 +72,6 @@
   
   CALL xios_set_context_attr("test",calendar_type="Gregorian") 
-  CALL xios_set_context_attr("test",calendar_type="Gregorian") 
-  CALL xios_set_context_attr("test",calendar_type="Gregorian") 
-!  CALL xios_set_context_attr("test",start_date="01/01/2000 - 00:00:00")
-  CALL xios_set_context_attr("test",calendar_type="Gregorian") 
   CALL xios_set_axis_attr("axis_A",size=llm ,value=lval) ;
   CALL xios_set_domain_attr("domain_A",ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, ni=ni,jbegin=jbegin,nj=nj)
-  !CALL xios_set_domain_attr("domain_A",zoom_ni=3,zoom_ibegin=3,zoom_nj=3,zoom_jbegin=6)
   CALL xios_set_domain_attr("domain_A",data_dim=2, data_ibegin=-1, data_ni=ni+2, data_jbegin=-2, data_nj=nj+4)
   CALL xios_set_domain_attr("domain_A",lonvalue=RESHAPE(lon,(/ni*nj/)),latvalue=RESHAPE(lat,(/ni*nj/)))
Index: XIOS/trunk/src/test/test_complete.f90
===================================================================
--- XIOS/trunk/src/test/test_complete.f90	(revision 486)
+++ XIOS/trunk/src/test/test_complete.f90	(revision 486)
@@ -0,0 +1,272 @@
+PROGRAM test_client
+
+  USE xios
+  USE mod_wait
+  IMPLICIT NONE
+  INCLUDE "mpif.h"
+  INTEGER :: rank
+  INTEGER :: size_loc
+  INTEGER :: ierr
+  
+  CHARACTER(len=*),PARAMETER :: id="client"
+  INTEGER :: comm
+  TYPE(xios_time)      :: dtime
+  TYPE(xios_context) :: ctx_hdl
+  INTEGER,PARAMETER :: ni_glo=100
+  INTEGER,PARAMETER :: nj_glo=100 
+  INTEGER,PARAMETER :: llm=5 
+  DOUBLE PRECISION  :: lval(llm)=1
+  TYPE(xios_field) :: field_hdl
+  TYPE(xios_fieldgroup) :: fieldgroup_hdl
+  TYPE(xios_file) :: file_hdl
+  LOGICAL :: ok
+  CHARACTER(len=256) :: crname
+
+  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_atm(:,:,:), field_A_srf(:,:), lonvalue(:)
+  INTEGER, ALLOCATABLE :: kindex(:)
+  INTEGER :: ni,ibegin,iend,nj,jbegin,jend
+  INTEGER :: i,j,l,ts,n, nb_pt
+
+!!! MPI Initialization
+
+  CALL MPI_INIT(ierr)
+  
+  CALL init_wait
+ 
+!!! XIOS Initialization (get the local communicator)
+
+  CALL xios_initialize(id,return_comm=comm)
+
+  CALL MPI_COMM_RANK(comm,rank,ierr)
+  CALL MPI_COMM_SIZE(comm,size_loc,ierr)  
+  
+
+!###########################################################################
+! Contexte ATM
+!###########################################################################
+
+!!! Initialisation des coordonnÃ©es globales et locales pour la grille rÃ©guliÃšre
+
+  DO j=1,nj_glo
+    DO i=1,ni_glo
+      lon_glo(i,j)=(i-1)+(j-1)*ni_glo
+      lat_glo(i,j)=1000+(i-1)+(j-1)*ni_glo
+      DO l=1,llm
+        field_A_glo(i,j,l)=(i-1)+(j-1)*ni_glo+10000*l
+      ENDDO
+    ENDDO
+  ENDDO
+  ni=ni_glo ; ibegin=1
+
+  jbegin=1
+  DO n=0,size_loc-1
+    nj=nj_glo/size_loc
+    IF (n<MOD(nj_glo,size_loc)) nj=nj+1
+    IF (n==rank) exit 
+    jbegin=jbegin+nj
+  ENDDO
+  
+  iend=ibegin+ni-1 ; jend=jbegin+nj-1
+
+  ALLOCATE(lon(ni,nj),lat(ni,nj),field_A_atm(0:ni+1,-1:nj+2,llm),lonvalue(ni*nj))
+  lon(:,:)=lon_glo(ibegin:iend,jbegin:jend)
+  lat(:,:)=lat_glo(ibegin:iend,jbegin:jend)
+  field_A_atm(1:ni,1:nj,:)=field_A_glo(ibegin:iend,jbegin:jend,:)
+ 
+
+!!! Context ATMOSPHERE
+
+  CALL xios_context_initialize("atmosphere",comm)
+  CALL xios_get_handle("atmosphere",ctx_hdl)
+  CALL xios_set_current_context(ctx_hdl)
+  
+  CALL xios_set_context_attr("atmosphere",calendar_type="Gregorian") 
+  CALL xios_set_context_attr("atmosphere",start_date="2000-01-01 00:00:00")
+  CALL xios_set_context_attr("atmosphere",time_origin="1999-01-01 15:00:00")
+
+  CALL xios_set_axis_attr("axis_atm",size=llm ,value=lval) ;
+
+  CALL xios_set_domain_attr("domain_atm",ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, ni=ni,jbegin=jbegin,nj=nj)
+  CALL xios_set_domain_attr("domain_atm",data_dim=2, data_ibegin=-1, data_ni=ni+2, data_jbegin=-2, data_nj=nj+4)
+  CALL xios_set_domain_attr("domain_atm",lonvalue=RESHAPE(lon,(/ni*nj/)),latvalue=RESHAPE(lat,(/ni*nj/)))
+
+  CALL xios_set_domain_attr("domain_atm_zoom",ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, ni=ni,jbegin=jbegin,nj=nj)
+  CALL xios_set_domain_attr("domain_atm_zoom",data_dim=2, data_ibegin=-1, data_ni=ni+2, data_jbegin=-2, data_nj=nj+4)
+  CALL xios_set_domain_attr("domain_atm_zoom",lonvalue=RESHAPE(lon,(/ni*nj/)),latvalue=RESHAPE(lat,(/ni*nj/)))
+
+!!! Activation du groupe field_definition
+
+  CALL xios_set_fieldgroup_attr("field_definition",enabled=.TRUE.)
+
+!!! CrÃ©ation d un nouveau champ 
+
+  CALL xios_get_handle("field_definition",fieldgroup_hdl)
+  CALL xios_add_child(fieldgroup_hdl,field_hdl,"field_B_atm")
+
+!!! Heritage des attributs d un autre champ
+
+  CALL xios_set_attr(field_hdl,field_ref="field_A_atm",name="field_B_atm")
+  
+!!! Affectation de ce nouveau champ au fichier avec un nouveau nom
+
+  CALL xios_get_handle("output_atmosphere",file_hdl)
+  CALL xios_add_child(file_hdl,field_hdl)
+  CALL xios_set_attr(field_hdl,field_ref="field_B_atm",name="field_C_atm")
+    
+!!! Definition du timestep
+
+  dtime%second=3600
+  CALL xios_set_timestep(dtime) 
+    
+!!! Recupration des valeurs des longitudes et de taille des domaines locaux (pour test de fonctionnalitÃ©)
+
+  ni=0 ; lonvalue(:)=0
+  CALL xios_get_domain_attr("domain_atm",ni=ni,lonvalue=lonvalue)
+    
+  PRINT *,"ni",ni
+  PRINT *,"lonvalue",lonvalue ;
+
+!!! Fin de la definition du contexte
+
+  CALL xios_close_context_definition()
+
+!!! Test des valeurs des champs/fichiers
+  
+  !!! Attribut defini ? 
+
+  CALL xios_is_defined_field_attr("field_A_atm",enabled=ok)
+  PRINT *,"field_A_atm : attribute enabled is defined ? ",ok
+
+  !!! Recuperer la valeur d un attribut
+  
+  CALL xios_get_field_attr("field_A_atm",name=crname)
+  PRINT *,"field_A_atm : attribute name is : ",TRIM(crname)
+
+  !!! Champ actif (besoin de fournir la valeur) ? 
+
+    PRINT*,"field field_A_atm is active ? ",xios_field_is_active("field_A_atm")
+
+  !!! Champ defini ?
+
+    PRINT*,"field field_A_atm is valid ?",xios_is_valid_field("field_A_atm")
+
+
+!###########################################################################
+! Contexte SRF
+!###########################################################################
+
+!!! Initialisation des coordonnÃ©es globales et locales pour la grille indexee (1 point sur 2)
+
+    nb_pt=ni*nj/2
+    ALLOCATE(kindex(nb_pt),field_A_srf(nb_pt,llm))
+    DO i=1,nb_pt
+      kindex(i)=2*i-1
+    ENDDO
+    field_A_srf(1:nb_pt,:)=RESHAPE(field_A_glo(ibegin:iend:2,jbegin:jend,:),(/ nb_pt,llm /))
+
+  CALL xios_context_initialize("surface",comm)
+  CALL xios_get_handle("surface",ctx_hdl)
+  CALL xios_set_current_context(ctx_hdl)
+  
+  CALL xios_set_context_attr("surface",calendar_type="Gregorian") 
+  CALL xios_set_context_attr("surface",start_date="2000-01-01 00:00:00")
+  CALL xios_set_context_attr("surface",time_origin="1999-01-01 15:00:00")
+
+  CALL xios_set_axis_attr("axis_srf",size=llm ,value=lval) ;
+  CALL xios_set_domain_attr("domain_srf",ni_glo=ni_glo, nj_glo=nj_glo, ibegin=ibegin, ni=ni,jbegin=jbegin,nj=nj)
+  CALL xios_set_domain_attr("domain_srf",data_dim=1, data_ibegin=0, data_ni=nb_pt)
+  CALL xios_set_domain_attr("domain_srf",data_n_index=nb_pt, data_i_index=kindex)
+  CALL xios_set_domain_attr("domain_srf",lonvalue=RESHAPE(lon,(/ni*nj/)),latvalue=RESHAPE(lat,(/ni*nj/)))
+
+!!! CrÃ©ation d un nouveau champ 
+
+  CALL xios_get_handle("field_definition",fieldgroup_hdl)
+  CALL xios_add_child(fieldgroup_hdl,field_hdl,"field_B_srf")
+
+!!! Heritage des attributs d un autre champ
+
+  CALL xios_set_attr(field_hdl,field_ref="field_A_srf",name="field_B_srf")
+  
+!!! Affectation de ce nouveau champ au fichier avec un nouveau nom
+
+  CALL xios_get_handle("output_surface",file_hdl)
+  CALL xios_add_child(file_hdl,field_hdl)
+  CALL xios_set_attr(field_hdl,field_ref="field_B_srf",name="field_C_srf")
+    
+!!! Definition du timestep
+
+  dtime%second=1800
+  CALL xios_set_timestep(dtime) 
+    
+!!! Recupration des valeurs des longitudes et de taille des domaines locaux (pour test de fonctionnalitÃ©)
+
+  ni=0 ; lonvalue(:)=0
+  CALL xios_get_domain_attr("domain_srf",ni=ni,lonvalue=lonvalue)
+    
+  PRINT *,"ni",ni
+  PRINT *,"lonvalue",lonvalue ;
+
+!!! Fin de la definition du contexte SRF
+
+  CALL xios_close_context_definition()
+
+
+!####################################################################################
+!!! Boucle temporelle
+!####################################################################################
+
+    DO ts=1,24*10
+
+      CALL xios_get_handle("atmosphere",ctx_hdl)
+      CALL xios_set_current_context(ctx_hdl)    
+
+!!! Mise a jour du pas de temps
+
+      CALL xios_update_calendar(ts)
+
+!!! On donne la valeur du champ atm
+
+      CALL xios_send_field("field_A_atm",field_A_atm)
+
+!!! On change de contexte
+
+      CALL xios_get_handle("surface",ctx_hdl)
+      CALL xios_set_current_context(ctx_hdl)    
+
+!!! Mise a jour du pas de temps
+
+      CALL xios_update_calendar(ts)
+
+!!! On donne la valeur du champ srf
+
+      CALL xios_send_field("field_A_srf",field_A_srf)
+
+      CALL wait_us(5000) ;
+    ENDDO
+
+!####################################################################################
+!!! Finalisation
+!####################################################################################
+
+!!! Fin des contextes
+    
+    CALL xios_context_finalize()
+    CALL xios_get_handle("atmosphere",ctx_hdl)
+    CALL xios_set_current_context(ctx_hdl)    
+    CALL xios_context_finalize()
+    
+!!! Fin de XIOS
+
+    CALL xios_finalize()
+  
+    CALL MPI_FINALIZE(ierr)
+  
+END PROGRAM test_client
+
+
+
+  
+
+  
Index: XIOS/trunk/src/test/test_unstruct_complete.f90
===================================================================
--- XIOS/trunk/src/test/test_unstruct_complete.f90	(revision 486)
+++ XIOS/trunk/src/test/test_unstruct_complete.f90	(revision 486)
@@ -0,0 +1,227 @@
+PROGRAM test_unstruct
+
+  USE xios
+  USE mod_wait
+  IMPLICIT NONE
+  INCLUDE "mpif.h"
+  INTEGER :: mpi_rank
+  INTEGER :: mpi_size
+  INTEGER :: ierr
+  
+  CHARACTER(len=*),PARAMETER :: id="client"
+  INTEGER :: comm
+  TYPE(xios_time)      :: dtime
+  TYPE(xios_context) :: ctx_hdl
+  INTEGER, PARAMETER :: nlon=60 
+  INTEGER, PARAMETER :: nlat=30
+  INTEGER,PARAMETER :: ni_glo=100
+  INTEGER,PARAMETER :: nj_glo=100 
+  INTEGER,PARAMETER :: llm=5 
+  DOUBLE PRECISION  :: lval(llm)=1
+  TYPE(xios_field) :: field_hdl
+  TYPE(xios_fieldgroup) :: fieldgroup_hdl
+  TYPE(xios_file) :: file_hdl
+  LOGICAL :: ok
+  
+  DOUBLE PRECISION,ALLOCATABLE :: lon_glo(:),lat_glo(:)
+  DOUBLE PRECISION,ALLOCATABLE :: bounds_lon_glo(:,:),bounds_lat_glo(:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: field_A_glo(:,:)
+  INTEGER,ALLOCATABLE :: i_index_glo(:)
+  INTEGER,ALLOCATABLE :: i_index(:)
+  LOGICAL,ALLOCATABLE :: mask_glo(:),mask(:)
+  DOUBLE PRECISION,ALLOCATABLE :: lon(:),lat(:),field_A_srf(:,:), lonvalue(:) ;
+  DOUBLE PRECISION,ALLOCATABLE :: bounds_lon(:,:),bounds_lat(:,:) ;
+  INTEGER :: ni,ibegin,iend,nj,jbegin,jend
+  INTEGER :: i,j,l,ts,n
+  INTEGER :: ncell_glo,ncell,ind
+  REAL :: ilon,ilat
+  DOUBLE PRECISION, PARAMETER :: Pi=3.14159265359
+  INTEGER :: list_ind(nlon,nlat)
+  INTEGER :: rank,j1,j2,np,ncell_x
+  INTEGER :: data_n_index
+  INTEGER,ALLOCATABLE :: data_i_index(:)
+  DOUBLE PRECISION,ALLOCATABLE :: field_A_compressed(:,:)
+  
+  CALL xios_initialize(id,return_comm=comm)
+  CALL MPI_COMM_RANK(comm,mpi_rank,ierr)
+  CALL MPI_COMM_SIZE(comm,mpi_size,ierr)
+  
+  CALL init_wait
+  
+  ncell_glo=0
+  DO j=1,nlat
+    n =  NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+    IF (n<8) n=8
+    ncell_glo=ncell_glo+n
+  ENDDO
+  
+  ALLOCATE(lon_glo(ncell_glo))
+  ALLOCATE(lat_glo(ncell_glo))
+  ALLOCATE(bounds_lon_glo(4,ncell_glo))
+  ALLOCATE(bounds_lat_glo(4,ncell_glo))
+  ALLOCATE(i_index_glo(ncell_glo))
+  ALLOCATE(field_A_glo(ncell_glo,llm))
+  ALLOCATE(mask_glo(ncell_glo))
+   
+  ind=0
+  DO j=1,nlat
+    n = NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+    if (j==1) PRINT*,"--- ",n
+    if (j==nlat) PRINT*,"--- ",n
+    IF (n<8) n=8
+    
+    DO i=1,n
+      ind=ind+1
+      list_ind(i,j)=ind
+      ilon=i-0.5
+      ilat=j-0.5
+      
+      lat_glo(ind)= 90-(ilat*180./nlat)
+      lon_glo(ind)= (ilon*360./n)
+      
+ 
+      bounds_lat_glo(1,ind)= 90-((ilat-0.5)*180./nlat)
+      bounds_lon_glo(1,ind)=((ilon-0.5)*360./n)
+      
+      bounds_lat_glo(2,ind)= 90-((ilat-0.5)*180./nlat)
+      bounds_lon_glo(2,ind)=((ilon+0.5)*360./n) 
+          
+      bounds_lat_glo(3,ind)= 90-((ilat+0.5)*180./nlat)
+      bounds_lon_glo(3,ind)=((ilon+0.5)*360./n)     
+
+      bounds_lat_glo(4,ind)= 90-((ilat+0.5)*180./nlat)
+      bounds_lon_glo(4,ind)=((ilon-0.5)*360./n)
+      
+    ENDDO
+  ENDDO
+
+!  mpi_size=32
+  rank=(mpi_size-1)/2
+  ncell_x=sqrt(ncell_glo*1./mpi_size)
+  
+  j1=nlat/2
+  DO WHILE(rank>=0)
+    j2=MAX(j1-ncell_x+1,1)
+    j=(j1+j2)/2
+    n=NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+    np = MIN(n/ncell_x,rank+1) ;
+    if (j2==1) np=rank+1 
+    
+    PRINT *,"domain ",j2,j1,rank,np ;
+    DO j=j2,j1  
+      n=NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+      IF (n<8) n=8
+      DO i=1,n
+        ind=list_ind(i,j)
+        IF ( (i-1) < MOD(n,np)*(n/np+1)) THEN 
+          i_index_glo(ind) = rank - (i-1)/(n/np+1)
+        ELSE 
+          i_index_glo(ind) = rank-(MOD(n,np)+ (i-1-MOD(n,np)*(n/np+1))/(n/np))
+        ENDIF
+      ENDDO
+    ENDDO
+    rank=rank-np
+    j1=j2-1
+  ENDDO
+        
+  rank=(mpi_size-1)/2+1
+  ncell_x=sqrt(ncell_glo*1./mpi_size)
+  
+  j1=nlat/2+1
+  DO WHILE(rank<=mpi_size-1)
+    j2=MIN(j1+ncell_x-1,nlat)
+    j=(j1+j2)/2
+    n=NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+    np = MIN(n/ncell_x,mpi_size-rank) ;
+    if (j2==nlat) np=mpi_size-rank 
+    
+    PRINT *,"domain ",j2,j1,rank,np ;
+    DO j=j1,j2  
+      n=NINT(COS(Pi/2-(j-0.5)*PI/nlat)*nlon)
+      IF (n<8) n=8
+      DO i=1,n
+        ind=list_ind(i,j)
+        IF ( (i-1) < MOD(n,np)*(n/np+1)) THEN 
+          i_index_glo(ind) = rank + (i-1)/(n/np+1)
+        ELSE 
+          i_index_glo(ind) = rank+(MOD(n,np)+ (i-1-MOD(n,np)*(n/np+1))/(n/np))
+        ENDIF
+      ENDDO
+    ENDDO
+    rank=rank+np
+    j1=j2+1
+  ENDDO
+    
+  ncell=0
+  DO ind=1,ncell_glo
+    IF (i_index_glo(ind)==mpi_rank) ncell=ncell+1
+  ENDDO
+  ALLOCATE(i_index(ncell))
+  ALLOCATE(lon(ncell))
+  ALLOCATE(lat(ncell))
+  ALLOCATE(bounds_lon(4,ncell))
+  ALLOCATE(bounds_lat(4,ncell))
+  ALLOCATE(field_A_srf(ncell,llm))
+  ALLOCATE(mask(ncell))
+  ncell=0
+  data_n_index=0
+  DO ind=1,ncell_glo
+    IF (i_index_glo(ind)==mpi_rank) THEN
+      ncell=ncell+1
+      i_index(ncell)=ind-1
+      lon(ncell)=lon_glo(ind)
+      lat(ncell)=lat_glo(ind)
+      bounds_lon(:,ncell)=bounds_lon_glo(:,ind)
+      bounds_lat(:,ncell)=bounds_lat_glo(:,ind)
+      field_A_srf(ncell,:)=i_index_glo(ind)
+      IF (MOD(ind,8)>=0 .AND. MOD(ind,8)<2) THEN
+        mask(ncell)=.FALSE.
+      ELSE
+        mask(ncell)=.TRUE.
+        data_n_index=data_n_index+1
+      ENDIF
+    ENDIF
+  ENDDO
+  
+  ALLOCATE(field_A_compressed(data_n_index,llm))
+  ALLOCATE(data_i_index(data_n_index))
+  data_n_index=0
+  DO ind=1,ncell
+    IF (mask(ind)) THEN
+      data_n_index=data_n_index+1
+      data_i_index(data_n_index)=ind
+      field_A_compressed(data_n_index,:)=field_A_srf(ind,:)
+    ENDIF
+  ENDDO
+      
+  
+  
+  CALL xios_context_initialize("surface",comm)
+  CALL xios_get_handle("surface",ctx_hdl)
+  CALL xios_set_current_context(ctx_hdl)
+  
+  CALL xios_set_axis_attr("axis_srf",size=llm ,value=lval) ;
+  CALL xios_set_domain_attr("domain_srf",ni_glo=ncell_glo, ni=ncell, ibegin=1, i_index=RESHAPE(i_index,(/ncell,1/) ))
+  CALL xios_set_domain_attr("domain_srf",data_dim=1, data_ni=data_n_index, data_n_index=data_n_index, data_i_index=data_i_index, type='unstructured')
+  CALL xios_set_domain_attr("domain_srf",lonvalue=lon,latvalue=lat)
+  CALL xios_set_domain_attr("domain_srf", nvertex=4, bounds_lon=bounds_lon, bounds_lat=bounds_lat )
+    
+ 
+  dtime%second=3600
+  CALL xios_set_timestep(dtime) 
+  CALL xios_close_context_definition()
+    
+   DO ts=1,24*10
+     CALL xios_update_calendar(ts)
+     CALL xios_send_field("field_A_srf",field_A_compressed)
+    ENDDO
+  
+    CALL xios_context_finalize()
+    CALL xios_finalize()
+  
+END PROGRAM test_unstruct
+
+
+  
+
+  
