Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/README
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/README	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/README	(revision 2363)
@@ -0,0 +1,52 @@
+Making initial condition files for icosa_lmdz.exe from
+an LMDZ start_archive.nc file
+
+What you need to do/know before:
+---------------------------
+Run a dynamico simulation (with startphy_file=false for example) with the desired resolution to get
+start.nc and startfi.nc. These files will be used as reference (rename as *_ref.nc) in the script.
+
+Job_mpi is the file you need to run start_archive2icosa on Occigen
+
+This script is currently under development and still needs to be finalized.
+So you must be careful to adapt the script to your own needs and replace hardcoded variables.
+Any suggestion for improvement is welcome.
+
+In a nutshell:
+--------------
+1) input file start_archive.nc needs some minor adaptations:
+- variable "nivsigs" needs be renamed "sigs" (XIOS requires CF-compliant files)
+ncrename -vnivsigs,sigs start_archive.nc
+- input file start_archive.nc need be converted to NetCDF4 format
+nccopy -k 'netCDF-4' start_archive.nc start_archive_nc4.nc
+2) script "compile" can be used to compile main tool start_archive2icosa.f90
+   as well as rearrange_startphy.f90
+3) to run, start_archive2icosa needs input files:
+- "start_archive_nc4.nc" : from step 1
+- "start_icosa_ref.nc" : a start.nc file from an icosa_lmdz.exe run (contains
+  all the necessary information about the icosaedral grid at that resolution)
+- "startphy_icosa_ref.nc" : a startfi.nc file from an icosa_lmdz.exe run (contains
+  all the necessary information about colums from physics)
+- "surface_nc4.nc" : Mars topography file (also needs to be in NetCDF4 format)
+- "iodef.xml" : information about field names and grids for XIOS
+NB: interpolation of HR topography requires lots of memory; see e.g. job_mpi
+job script where 2 nodes (56 cores) are requested but only 28 processes
+are run.
+4) start_archive2icosa outputs files "start_icosa.nc" and "startphy_icosa.nc"
+  require a couple of extra modifications. Script "icosa_finalize.bash" does
+  the required changes and generates files "start.nc" and "startphy.nc" that
+  can be used as inputs by icosa_lmdz.exe
+5) When using these start one must add in run_icosa.def the extra flag
+etat0_start_file_colocated = true
+
+Additional comments:
+--------------------
+- script "compile" assumes XIOS is to be found in "/scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS"
+  to be adapted to your own settings
+- "nccopy" is a standard netcdf tool available via e.g.:
+module load intel/17.0 intelmpi/2017.0.098 hdf5/1.8.17 netcdf/4.4.0_fortran-4.4.2
+- "nco" tools (ncrename, ncks, ...) are available via e.g.:
+module load intel/17.0 openmpi/intel/2.0.1 hdf5/1.8.17 netcdf/4.4.0_fortran-4.4.2 nco/4.4.6
+- in start_archive2icosa.f90, rearrange_startphy.f90, iodef.xml and
+  icosa_finalize.bash input/output file names are hard-coded.
+  Modify/adapt with caution!
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/compile
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/compile	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/compile	(revision 2363)
@@ -0,0 +1,16 @@
+source /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/arch.env
+source /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/arch.path
+
+mpif90 -g -traceback start_archive2icosa.f90 \
+$NETCDF_INCDIR $NETCDF_LIBDIR $NETCDF_LIB \
+$HDF5_INCDIR $HDF5_LIBDIR $HDF5_LIB \
+-I /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/inc \
+-L /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/lib -lxios -lstdc++ \
+-o start_archive2icosa
+
+mpif90 -g -traceback rearrange_startphy.f90 \
+$NETCDF_INCDIR $NETCDF_LIBDIR $NETCDF_LIB \
+$HDF5_INCDIR $HDF5_LIBDIR $HDF5_LIB \
+-I /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/inc \
+-L /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/lib -lxios -lstdc++ \
+-o rearrange_startphy	
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/icosa_finalize_new_rearrange.bash
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/icosa_finalize_new_rearrange.bash	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/icosa_finalize_new_rearrange.bash	(revision 2363)
@@ -0,0 +1,48 @@
+#!/usr/bin/bash
+#
+# Bash script to post-process files startphy_icosa.nc and start_icosa.nc
+# generated by start_archive2icosa and create files
+# start.nc and startphy.nc (usable as inputs for icosa_lmdz.exe)
+
+# load nco module
+module purge
+module load intel/17.0 openmpi/intel/2.0.1 hdf5/1.8.17 netcdf/4.4.0_fortran-4.4.2 nco/4.4.6
+
+# cleanup
+\rm -rf start.nc startphy.nc
+
+nccopy -k 'classic' startphy_icosa.nc startphy_icosa_nc3.nc
+
+# rename lat and lon variables as latitude and longitude and put result in
+# file startphy.nc
+#ncrename -v lon,longitude -v lat,latitude -d cell,points_physiques \
+ncrename -v lon,longitude -v lat,latitude -d cell,physical_points\
+         startphy_icosa_nc3.nc startphy_nc3.nc
+#Temporary added because program is not yet able to read topography
+ncks -A -v albedodat,ZMEA,ZSTD,ZSIG,ZGAM,ZTHE \
+         startphy_icosa_ref.nc startphy_nc3.nc
+#Temporay soildepth array from Dynamico restartfi
+ncks -A -v soildepth \
+         ../lonlat_av_startfi/startfi0.nc startphy_nc3.nc
+
+nccopy -k 'netCDF-4' startphy_nc3.nc startphy_new_rearrange.nc
+
+#Temporary Time variable from start2archive; should be added with xios
+ncks -A -v Time \
+         start_archive_nc4.nc startphy_new_rearrange.nc
+
+rearrange_startphy > rearrange_startphy.out 2>&1
+
+# make a proper start.nc file based on start_icosa.nc
+cp start_icosa.nc start.nc
+# add u,u_lat,u_lon,bounds_lat_u and bound_lon_u from start_icosa_ref.nc
+ncks -A -v u,lon_u,lat_u,bounds_lon_u,bounds_lat_u \
+     start_icosa_ref.nc start.nc
+#ncrename -v lon,lon_mesh -v lat,lat_mesh -v bounds_lon,bounds_lon_mesh -v bounds_lat,bounds_lat_mesh \
+#         start.nc
+ncrename -d cell,cell_mesh \
+         start.nc
+ncrename -d nvertex,nvertex_mesh \
+         start.nc
+ncks -A -v xcell,ycell,zcell \
+     start_icosa_ref.nc start.nc
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/iodef.xml
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/iodef.xml	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/iodef.xml	(revision 2363)
@@ -0,0 +1,226 @@
+<?xml version="1.0"?>
+<simulation>
+
+ <context id="test">
+   <calendar type="Gregorian"
+             start_date="2012-03-01 15:00:00"
+             time_origin="2012-02-29 15:00:00" />
+
+   <field_definition level="1" >    
+    <field_group  id="fields_2D_dst" domain_ref="src_domain_regular_clean" >
+      <!-- 2D fields -->
+      <field id="ps_clean" />
+      <field id="tsurf_clean" />
+      <field id="co2ice_clean" />
+      <field id="emis_clean" />
+      <field id="q2surf_clean" />
+    </field_group>
+    <!-- -->
+    <field_group  id="fields_dst">
+      <!-- from topography file -->
+      <field id="topo"
+             domain_ref="src_topo_domain_regular" />
+      <!-- 3D fields -->
+      <field id="temp_clean"
+             grid_ref="src_grid_regular_clean" />
+      <field id="theta_rhodz_clean"
+             grid_ref="src_grid_regular_clean" />
+      <field id="u_clean" grid_ref="src_grid_regular_clean" />
+      <field id="v_clean" grid_ref="src_grid_regular_clean" />
+      <!-- tracers -->
+      <field id="tracers_clean"
+             grid_ref="src_nq_grid_regular_clean" />
+      <field id="co2_clean" grid_ref="src_grid_regular_clean" />
+      <!-- soil -->
+      <field id="tsoil_clean" grid_ref="src_grid_soil_clean" />
+      <field id="inertiedat_clean" grid_ref="src_grid_soil_clean" />
+    </field_group>
+    
+   </field_definition>
+
+   <file_definition type="one_file" par_access="collective"
+                    output_freq="1ts" output_level="10"  enabled=".TRUE.">
+      <!-- input files -->
+      <file_group id="read_files" >
+       <file id="start_archive_nc4" name="start_archive_nc4" mode="read" >
+         <!-- 2D fields -->
+         <field_group freq_offset="1ts"
+                      domain_ref="src_domain_regular"
+                      operation="instant" >
+          <field id="src_ps" name="ps" />
+          <!--field id="src_phis" name="phisinit" /-->
+          <field id="src_tsurf" name="tsurf" />
+          <field id="src_co2ice" name="co2ice" />
+          <field id="src_emis" name="emis" />
+          <field id="src_q2surf" name="q2surf" />
+          <!--field id="src_sollw" name="sollw" /-->
+          <!--field id="src_ZMEA" name="zmea" /-->
+         </field_group>
+         <!-- 3D fields -->
+          <field id="src_temp" name="temp" freq_offset="1ts"
+                 grid_ref="src_grid_regular"
+                 operation="instant" />
+          <field id="src_u" name="u" freq_offset="1ts"
+                 grid_ref="src_grid_regular"
+                 operation="instant" />
+          <field id="src_v" name="v" freq_offset="1ts"
+                 grid_ref="src_grid_regular"
+                 operation="instant" />
+          <field id="src_trac1" name="co2" freq_offset="1ts"
+                 grid_ref="src_grid_regular"
+                 operation="instant" />
+         <!-- soil fields -->
+          <field id="src_tsoil" name="tsoil" freq_offset="1ts"
+                 grid_ref="src_grid_soil"
+                 operation="instant" />
+          <field id="src_inertiedat" name="inertiedat" freq_offset="1ts"
+                 grid_ref="src_grid_soil"
+                 operation="instant" />
+       </file>
+       <!-- <file id="vt1x1inv_nc4" name="vt1x1inv_nc4" mode="read" > -->
+       <file id="surface_nc4" name="surface_nc4" mode="read" >
+         <field id="zMOL" name="zMOL" 
+                domain_ref="src_topo_domain_regular"
+                freq_offset="1ts"
+                operation="instant" />
+       </file>
+      </file_group>
+      
+      <!-- output files -->
+      <file_group id="write_files" >
+        <file id="start_icosa" name="start_icosa" >
+         <!-- 2D fields -->
+         <field_group domain_ref="dst_domain_unstructured"
+                      operation="once" >
+          <field field_ref="ps_clean" name="ps" />
+          <!-- <field field_ref="phis_clean" name="phis" /> -->
+          <field field_ref="topo" name="phis" />
+         </field_group>
+         <!-- 3D fields -->
+          <field field_ref="theta_rhodz_clean" name="theta_rhodz"
+                 grid_ref="dst_grid_unstructured"
+                 operation="once" />
+          <field field_ref="u_clean" name="ulon"
+                 grid_ref="dst_grid_unstructured"
+                 operation="once" />
+          <field field_ref="v_clean" name="ulat"
+                 grid_ref="dst_grid_unstructured"
+                 operation="once" />
+          <!-- tracers -->
+          <field field_ref="tracers_clean" name="q"
+                 grid_ref="dst_nq_grid_unstructured"
+                 operation="once" />
+        </file>
+        <file id="startphy_icosa" name="startphy_icosa" >
+         <!-- 2D fields -->
+         <field_group domain_ref="dst_domain_unstructured"
+                      operation="once" >
+          <field field_ref="tsurf_clean" name="tsurf" />
+          <field field_ref="co2ice_clean" name="co2ice" />
+          <field field_ref="emis_clean" name="emis" />
+          <field field_ref="q2surf_clean" name="q2" />
+          <field field_ref="topo" name="phisfi" />
+         </field_group>
+         <!-- soil fields -->
+          <field field_ref="tsoil_clean" name="tsoil"
+                 grid_ref="dst_grid_soil_unstructured"
+                 operation="once" />
+          <field field_ref="inertiedat_clean" name="inertiedat"
+                 grid_ref="dst_grid_soil_unstructured"
+                 operation="once" />
+          <field field_ref="co2_clean" name="co2"
+                 grid_ref="dst_grid_unstructured"
+                 operation="once" />
+        </file>
+      </file_group>
+   </file_definition>
+
+   <axis_definition>
+     <axis id="lev" positive="down"
+           standard_name="atmosphere_hybrid_sigma_pressure_coordinate"
+           long_name="hybrid level at midpoints" />
+     <axis id="nq" name="nq" />
+     <axis id="soil_layers" name="subsurface_layers" />
+   </axis_definition>
+
+   <domain_definition>
+    <domain_group id="read_domains">
+      <domain id="src_domain_regular" type="rectilinear">
+        <!-- <generate_rectilinear_domain /> -->
+      </domain>
+      <domain id="src_domain_regular_clean" type="rectilinear">
+        <!-- <generate_rectilinear_domain /> -->
+      </domain>
+      <domain id="src_topo_domain_regular" type="rectilinear">
+        <!-- <generate_rectilinear_domain /> -->
+      </domain>
+    </domain_group>
+
+    <domain_group id="domain_dst">
+     <domain id="dst_domain_unstructured" type="unstructured">
+       <interpolate_domain order="1" renormalize="true" />
+     </domain>
+    </domain_group>
+   </domain_definition>
+
+   <grid_definition>
+    <grid_group id="read_grids">
+      <grid id="src_grid_regular">
+        <domain domain_ref="src_domain_regular" />
+        <axis axis_ref="lev" />
+      </grid>
+      <grid id="src_grid_regular_clean">
+        <domain domain_ref="src_domain_regular_clean" />
+        <axis axis_ref="lev" />
+      </grid>
+      <grid id="src_nq_grid_regular_clean">
+        <domain domain_ref="src_domain_regular_clean" />
+        <axis axis_ref="lev" />
+        <axis axis_ref="nq" />
+      </grid>
+      <grid id="src_grid_soil">
+        <domain domain_ref="src_domain_regular" />
+        <axis axis_ref="soil_layers" />
+      </grid>
+      <grid id="src_grid_soil_clean">
+        <domain domain_ref="src_domain_regular_clean" />
+        <axis axis_ref="soil_layers" />
+      </grid>
+    </grid_group>
+    
+    <grid_group id="grid_dst">
+      <grid id="dst_grid_unstructured">
+        <domain domain_ref="dst_domain_unstructured" />
+        <axis axis_ref="lev" />
+      </grid>
+      <grid id="dst_nq_grid_unstructured">
+        <domain domain_ref="dst_domain_unstructured" />
+        <axis axis_ref="lev" />
+        <axis axis_ref="nq" />
+      </grid>
+      <grid id="dst_grid_soil_unstructured">
+        <domain domain_ref="dst_domain_unstructured" />
+        <axis axis_ref="soil_layers" />
+      </grid>
+    </grid_group>
+
+   </grid_definition>
+
+  </context>
+
+  <context id="xios">
+      <variable_definition>
+        <variable_group id="buffer">
+            <variable id="optimal_buffer_size" type="string">performance</variable>
+            <variable id="buffer_size_factor" type="double">30.0</variable>
+            <variable id="min_buffer_size" type="double">800000</variable>
+         </variable_group>
+
+        <variable_group id="parameters" >
+          <variable id="using_server" type="bool">false</variable>
+          <variable id="info_level" type="int">100</variable>
+          <variable id="print_file" type="bool">true</variable>
+        </variable_group>
+      </variable_definition>
+  </context>
+</simulation>
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/job_mpi
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/job_mpi	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/job_mpi	(revision 2363)
@@ -0,0 +1,21 @@
+#!/bin/bash
+#SBATCH --nodes=2
+#SBATCH --ntasks-per-node=14
+### set --threads-per-core=2 for hyperthreading
+#SBATCH --threads-per-core=1
+#SBATCH -J remap_XIOS
+#SBATCH --time=00:20:00
+#SBATCH --output job_mpi.%j.out
+#SBATCH --constraint=BDW28
+##SBATCH --constraint=HSW24
+##SBATCH --mem=8G
+#SBATCH --exclusive
+
+set -vx
+
+source /scratch/cnt0027/lmd1167/adelavois/dynamico-giant/code/XIOS/arch.env
+
+#printenv | sort
+
+srun --resv-ports --kill-on-bad-exit=1 --mpi=pmi2 --label -n $SLURM_NTASKS  start_archive2icosa > start_archive2icosa.out 2>&1
+
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/rearrange_startphy.f90
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/rearrange_startphy.f90	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/rearrange_startphy.f90	(revision 2363)
@@ -0,0 +1,207 @@
+PROGRAM rearrange_startphy
+
+USE netcdf
+
+IMPLICIT NONE
+
+INTEGER :: ncid
+INTEGER :: dimids(4)
+INTEGER :: varid
+INTEGER :: ierr
+INTEGER :: nvar
+INTEGER :: ndim
+CHARACTER(LEN=100) :: varname
+INTEGER :: varname_dimids(4)
+
+INTEGER :: i,j,k,nb_cells,nlev,nsoil,nvertex
+INTEGER,ALLOCATABLE :: cell_index(:)
+REAL,ALLOCATABLE :: ref_lon(:)
+REAL,ALLOCATABLE :: ref_lat(:)
+REAL,ALLOCATABLE :: lon(:)
+REAL,ALLOCATABLE :: lat(:)
+REAL,ALLOCATABLE :: ref_field(:),field(:)
+REAL,ALLOCATABLE :: ref_field_3D(:,:),field_3D(:,:)
+REAL,ALLOCATABLE :: ref_field_soil(:,:),field_soil(:,:)
+REAL,ALLOCATABLE :: ref_field_vertex(:,:),field_vertex(:,:)
+REAL :: lati,loni
+REAL :: diff_lat,diff_lon
+CHARACTER(LEN=*),PARAMETER :: ref_file="startphy_icosa_ref.nc"
+CHARACTER(LEN=*),PARAMETER :: file="startphy_new_rearrange.nc"
+
+DOUBLE PRECISION,PARAMETER :: pi=acos(-1.d0)
+REAL :: reflatj,reflonj
+
+! load coordinates from files
+  ierr=NF90_OPEN(ref_file, NF90_NOWRITE, ncid)
+  ierr=NF90_INQ_DIMID(ncid,"physical_points",dimids(1))
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(1), len=nb_cells)
+  write(*,*) "nb_cells=",nb_cells,"dimids(1)=",dimids(1)
+  allocate(ref_lat(nb_cells),ref_lon(nb_cells))
+  
+  ierr=NF90_INQ_VARID(ncid,"latitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid,ref_lat)
+  if (ierr /= nf90_noerr) then
+    write(*,*) "cannot load latitude from ",trim(ref_file)
+  else
+    write(*,*) "ref_lat(1:5)=",ref_lat(1:5)
+  endif
+  ierr=NF90_INQ_VARID(ncid,"longitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid,ref_lon)
+  if (ierr /= nf90_noerr) then
+    write(*,*) "cannot load longitude from ",trim(ref_file)
+  else
+    write(*,*) "ref_lon(1:5)=",ref_lon(1:5)
+  endif
+  ierr=NF90_CLOSE(ncid)
+  
+  ierr=NF90_OPEN(file, NF90_WRITE, ncid)
+  allocate(lat(nb_cells),lon(nb_cells))
+  ierr=NF90_INQ_VARID(ncid,"latitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid,lat)
+  if (ierr /= nf90_noerr) then
+    write(*,*) "cannot load lat from ",trim(file)
+  else
+    write(*,*) "lat(1:5)=",lat(1:5)
+  endif
+  ierr=NF90_INQ_VARID(ncid,"longitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid,lon)
+  if (ierr /= nf90_noerr) then
+    write(*,*) "cannot load lat from ",trim(file)
+  else
+    write(*,*) "lon(1:5)=",lon(1:5)
+  endif
+  
+  ! find correspondances between lon/ref_lon & lat/ref_lat
+  allocate(cell_index(nb_cells))
+  cell_index(:)=0
+  do i=1,nb_cells !in case lon and lat are expressed in rad in startphy_icosa_ref /180*pi
+    lat(i)=lat(i) /(180.d0/pi) ; lon(i)=lon(i)/(180.d0/pi)
+    do j=1,nb_cells
+     reflatj=ref_lat(j)
+     reflonj=ref_lon(j)
+
+     if (lat(i) ==0.) then
+       diff_lat=abs((lat(i)-ref_lat(j))/1.e-8)
+     else
+       diff_lat=abs((lat(i)-ref_lat(j))/lat(i))
+     endif
+
+     if (lon(i) ==0.) then
+      diff_lon=abs((lon(i)-ref_lon(j))/1.e-8)
+     else
+      diff_lon=abs((lon(i)-ref_lon(j))/lon(i))
+     endif
+
+     if ((diff_lat <= 1.e-5).and.(diff_lon <= 1.e-5)) then
+      cell_index(i)=j
+      write(*,*)"j=",j," lat(i)=",lat(i)," ref_lat(j)=",reflatj
+      write(*,*)"j=",j," lon(i)=",lon(i)," ref_lon(j)=",reflonj
+     endif
+    enddo ! of do j=1,nb_cells
+    write(*,*) ">> i=",i,"cell_index(i)=",cell_index(i)
+    ! sanity check:
+    if (cell_index(i)==0) then
+      write(*,*) "Error, could not find lon-lat match for i=",i , lat(i), lon(i)
+      stop
+    endif
+    ! writing lat and lon in rad rather than deg
+    ierr=NF90_INQ_VARID(ncid,"longitude",varid)
+    ierr=NF90_PUT_VAR(ncid,varid,lon)
+
+    ierr=NF90_INQ_VARID(ncid,"latitude",varid)
+    ierr=NF90_PUT_VAR(ncid,varid,lat)
+  enddo ! of do i=1,nb_cells
+  
+!  do i=1,nb_cells
+!    write(*,*) "i=",i," cell_index(i)=",cell_index(i)
+!    write(*,*) "  lat(i)=",lat(i),"ref_lat(cell_index(i))=",ref_lat(cell_index(i))
+!  enddo
+  
+  ! load, rearrange and write variables
+  ierr=NF90_INQ_DIMID(ncid,"physical_points",dimids(1))
+  ierr=NF90_INQ_DIMID(ncid,"lev",dimids(2))
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(2), len=nlev)
+  ierr=NF90_INQ_DIMID(ncid,"subsurface_layers",dimids(3))
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(3), len=nsoil)
+  ierr=NF90_INQ_DIMID(ncid,"nvertex",dimids(4))
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(4), len=nvertex)
+  ierr=NF90_INQUIRE(ncid,nVariables=nvar)
+  write(*,*) "nvar=",nvar
+  allocate(ref_field(nb_cells),field(nb_cells))
+  allocate(ref_field_3D(nb_cells,nlev),field_3D(nb_cells,nlev))
+  allocate(ref_field_soil(nb_cells,nsoil),field_soil(nb_cells,nsoil))
+  allocate(ref_field_vertex(nvertex,nb_cells),field_vertex(nvertex,nb_cells))
+  write(*,*) "dimids :", dimids
+  ! loop over variables:
+  do k=1,nvar
+    varname_dimids(:)=0
+    ierr=NF90_INQUIRE_VARIABLE(ncid,k,name=varname,ndims=ndim,dimids=varname_dimids)
+      write(*,*) "name: ",trim(varname),"; dimensions:",varname_dimids
+    if (ierr /= nf90_noerr) then
+      write(*,*) "error for variable k=",k
+      write(*,*) nf90_strerror(ierr)
+    endif
+    ! Processing 2D variables
+    if ((ndim==1).and.(varname_dimids(1)==dimids(1))) then
+      write(*,*) "processing ",trim(varname)
+      ! load field
+      ierr=NF90_INQ_VARID(ncid,varname,varid)
+      ierr=NF90_GET_VAR(ncid,varid,ref_field)
+      ! rearrange field
+      do i=1,nb_cells
+        field(cell_index(i))=ref_field(i)
+      enddo
+      ! write field
+      ierr=NF90_PUT_VAR(ncid,varid,field)
+
+    ! Processing 3D variables : bounds_lon and bounds_lat
+    else if ((ndim==2).and.(varname_dimids(1)==dimids(4))) then
+      write(*,*) "processing ",trim(varname)
+      ! load field_3D
+      ierr=NF90_INQ_VARID(ncid,varname,varid)
+      if (ierr /= nf90_noerr) print*,"error inqvarid ",trim(varname)
+      ierr=NF90_GET_VAR(ncid,varid,ref_field_vertex)
+      if (ierr /= nf90_noerr) print*,"error get_var ",trim(varname)
+      ! rearrange field_3D
+      do i=1,nb_cells
+        field_vertex(:,cell_index(i))=ref_field_vertex(:,i)
+      enddo
+      ! write field_3D
+      ierr=NF90_PUT_VAR(ncid,varid,field_vertex)
+      if (ierr /= nf90_noerr) print*,"error putvar ",trim(varname)
+ 
+    ! Processing 3D variables : altitude
+    else if ((ndim==2).and.(varname_dimids(2)==dimids(2))) then
+      write(*,*) "processing ",trim(varname)
+      ! load field_3D
+      ierr=NF90_INQ_VARID(ncid,varname,varid)
+      if (ierr /= nf90_noerr) print*,"error inqvarid ",trim(varname)
+      ierr=NF90_GET_VAR(ncid,varid,ref_field_3D)
+      if (ierr /= nf90_noerr) print*,"error get_var ",trim(varname)
+      ! rearrange field_3D
+      do i=1,nb_cells
+        field_3D(cell_index(i),:)=ref_field_3D(i,:)
+      enddo
+      ! write field_3D
+      ierr=NF90_PUT_VAR(ncid,varid,field_3D)
+      if (ierr /= nf90_noerr) print*,"error putvar ",trim(varname)
+
+    ! Processing 3D variables : soil
+    else if ((ndim==2).and.(varname_dimids(2)==dimids(3))) then
+      write(*,*) "processing ",trim(varname)
+      ! load field_soil
+      ierr=NF90_INQ_VARID(ncid,varname,varid)
+      if (ierr /= nf90_noerr) print*,"error inqvarid ",trim(varname)
+      ierr=NF90_GET_VAR(ncid,varid,ref_field_soil)
+      if (ierr /= nf90_noerr) print*,"error get_var ",trim(varname)
+      ! rearrange field_soil
+      do i=1,nb_cells
+        field_soil(cell_index(i),:)=ref_field_soil(i,:)
+      enddo
+      ! write field_soil
+      ierr=NF90_PUT_VAR(ncid,varid,field_soil)
+      if (ierr /= nf90_noerr) print*,"error putvar ",trim(varname)
+    endif
+  enddo
+  
+END PROGRAM rearrange_startphy
Index: trunk/LMDZ.MARS/util/newstart_startarchive2icosa/start_archive2icosa.f90
===================================================================
--- trunk/LMDZ.MARS/util/newstart_startarchive2icosa/start_archive2icosa.f90	(revision 2363)
+++ trunk/LMDZ.MARS/util/newstart_startarchive2icosa/start_archive2icosa.f90	(revision 2363)
@@ -0,0 +1,463 @@
+PROGRAM start_archive2icosa
+
+  USE xios
+  USE mod_wait
+  USE netcdf
+  
+  IMPLICIT NONE
+  INCLUDE "mpif.h"
+  INTEGER :: rank
+  INTEGER :: size
+  INTEGER :: ierr
+
+  CHARACTER(len=*),PARAMETER :: id="client"
+  INTEGER :: comm
+  TYPE(xios_duration) :: dtime
+  CHARACTER(len=15) :: calendar_type
+  TYPE(xios_context) :: ctx_hdl
+
+  INTEGER :: n,l
+  INTEGER :: src_ibegin, src_iend, src_topo_ibegin, src_topo_iend
+  INTEGER :: src_jbegin, src_jend, src_topo_jbegin, src_topo_jend
+  INTEGER :: src_ni, src_ni_glo, src_topo_ni, src_topo_ni_glo
+  INTEGER :: src_nj, src_nj_glo, src_topo_nj, src_topo_nj_glo
+  INTEGER :: src_nlev ! number of vertical layers
+  INTEGER :: src_nq=1 ! number of tracers
+  INTEGER :: src_nt=1 ! number of time steps
+  INTEGER :: src_nsoil=18 ! number of soil layers
+  DOUBLE PRECISION,ALLOCATABLE :: lev_values(:) ! vertical axis 
+  DOUBLE PRECISION,ALLOCATABLE :: nq_values(:) ! tracer # axis 
+  DOUBLE PRECISION,ALLOCATABLE :: soil_layers_values(:) ! soil axis
+  DOUBLE PRECISION,ALLOCATABLE :: src_lon(:) ! mesh center coordinate
+  DOUBLE PRECISION,ALLOCATABLE :: src_lat(:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_ap(:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_bp(:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_controle(:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_field_2D(:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_pk(:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_field_3D(:,:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_pressure(:,:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_theta_rhodz(:,:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_tracers(:,:,:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_topo_lon(:) ! mesh center coordinate
+  DOUBLE PRECISION,ALLOCATABLE :: src_topo_lat(:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_topo(:,:)
+  DOUBLE PRECISION,ALLOCATABLE :: src_field_3D_soil(:,:,:) ! 3D grid in soil, for tsoil et inertia
+  DOUBLE PRECISION,ALLOCATABLE :: src_soil_layers(:) ! soil_depth
+  
+  CHARACTER(LEN=*),PARAMETER :: src_file="start_archive_nc4.nc"
+  CHARACTER(LEN=*),PARAMETER :: src_topo_file="surface_nc4.nc"
+  CHARACTER(LEN=*),PARAMETER :: dst_coord_file="start_icosa_ref.nc"
+  CHARACTER(LEN=*),PARAMETER :: src_controle_file="startphy_icosa_ref.nc"
+  DOUBLE PRECISION,ALLOCATABLE :: dst_lon(:),dst_lat(:)
+  DOUBLE PRECISION,ALLOCATABLE :: dst_boundslon(:,:) ! mesh corner coordinates
+  DOUBLE PRECISION,ALLOCATABLE :: dst_boundslat(:,:)
+  INTEGER :: dst_ibegin !, dst_iend
+  INTEGER :: dst_ni, dst_ni_glo
+  INTEGER :: dst_nvertex
+  INTEGER :: ncid
+  INTEGER :: dimids(4)
+  INTEGER :: varid
+  
+  INTEGER :: div, remain
+  INTEGER :: ts ! time step #
+  DOUBLE PRECISION,PARAMETER :: pi=acos(-1.d0)
+  DOUBLE PRECISION :: gravity,kappa,preff
+
+!!! MPI Initialization
+  CALL MPI_INIT(ierr)
+  CALL init_wait
+
+!!! XIOS Initialization (get the local communicator)
+  CALL xios_initialize(id,return_comm=comm)
+! get local rank of MPI process
+  CALL MPI_COMM_RANK(comm,rank,ierr)
+! get total number of MPI processes
+  CALL MPI_COMM_SIZE(comm,size,ierr)
+
+!!! Open files and load sizes and coordinates
+  ierr=NF90_OPEN(src_topo_file, NF90_NOWRITE, ncid)
+  ierr=NF90_INQ_VARID(ncid,"zMOL",varid)
+  ierr=NF90_INQUIRE_VARIABLE(ncid, varid,dimids=dimids)
+  write(*,*) "rank=",rank,"dimids=",dimids
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(1), len=src_topo_ni_glo)
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(2), len=src_topo_nj_glo)
+  write(*,*) "rank=",rank," src_topo_ni_glo=",src_topo_ni_glo ! longitude
+  write(*,*) "rank=",rank," src_topo_nj_glo=",src_topo_nj_glo ! latitude
+
+! assume domain splitup with MPI only along latitudes
+  src_topo_ni=src_topo_ni_glo
+  src_topo_ibegin=0
+  src_topo_iend=src_topo_ibegin+src_ni-1
+  write(*,*) "rank=",rank," src_topo_ni=",src_topo_ni
+  
+  src_topo_jbegin=0
+  DO n=0,size-1
+    src_topo_nj=src_topo_nj_glo/size
+    IF (n<MOD(src_topo_nj_glo,size)) src_topo_nj=src_topo_nj+1
+    IF (n==rank) exit
+    src_topo_jbegin=src_topo_jbegin+src_topo_nj
+  ENDDO
+  src_topo_jend=src_topo_jbegin+src_topo_nj-1
+  write(*,*) "rank=",rank," src_topo_nj=",src_topo_nj, &
+             " src_topo_jbegin=",src_topo_jbegin
+
+  ALLOCATE(src_topo_lon(src_topo_ni))
+  ALLOCATE(src_topo_lat(src_topo_nj))
+  ALLOCATE(src_topo(src_topo_ni,src_topo_nj))
+
+! load src_topo_lon and src_topo_lat
+  ierr=NF90_INQ_VARID(ncid,"longitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid, src_topo_lon, &
+                    start=(/src_topo_ibegin+1/),count=(/src_topo_ni/))
+  WRITE(*,*) rank,":src_topo_lon(1:2)=",src_topo_lon(1:2)
+  ierr=NF90_INQ_VARID(ncid,"latitude",varid)
+  ierr=NF90_GET_VAR(ncid,varid, src_topo_lat, &
+                    start=(/src_topo_jbegin+1/),count=(/src_topo_nj/))
+  WRITE(*,*) rank,":src_topo_lat(1:2)=",src_topo_lat(1:2)
+
+! from start_archive.nc file
+  ierr=NF90_OPEN(src_file, NF90_NOWRITE, ncid)
+  ierr=NF90_INQ_VARID(ncid,"temp",varid)
+  ierr=NF90_INQUIRE_VARIABLE(ncid, varid,dimids=dimids)
+  write(*,*) "rank=",rank,"dimids=",dimids
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(1), len=src_ni_glo)
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(2), len=src_nj_glo)
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(3), len=src_nlev)
+  write(*,*) "rank=",rank," src_ni_glo=",src_ni_glo ! longitude
+  write(*,*) "rank=",rank," src_nj_glo=",src_nj_glo ! latitude
+  write(*,*) "rank=",rank," src_nlev=",src_nlev ! number of vertical layers
+  write(*,*) "rank=",rank," src_nq=",src_nq ! number of tracers
+! soil_depth with tsoil variable
+  ierr=NF90_INQ_VARID(ncid,"tsoil",varid)
+  ierr=NF90_INQUIRE_VARIABLE(ncid, varid,dimids=dimids)
+  write(*,*) "rank=",rank,"dimids tsoil =",dimids
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(3), len=src_nsoil)
+  write(*,*) "rank=",rank," src_nsoil=",src_nsoil ! number of vertical layers
+
+! assume domain splitup with MPI only along latitudes
+  src_ni=src_ni_glo
+  src_ibegin=0
+  src_iend=src_ibegin+src_ni-1
+  write(*,*) "rank=",rank," src_ni=",src_ni
+  
+  src_jbegin=0
+  DO n=0,size-1
+    src_nj=src_nj_glo/size
+    IF (n<MOD(src_nj_glo,size)) src_nj=src_nj+1
+    IF (n==rank) exit
+    src_jbegin=src_jbegin+src_nj
+  ENDDO
+  src_jend=src_jbegin+src_nj-1
+  write(*,*) "rank=",rank," src_nj=",src_nj," src_jbegin=",src_jbegin
+
+  ALLOCATE(src_lon(src_ni))
+  ALLOCATE(src_lat(src_nj))
+  ALLOCATE(src_field_2D(src_ni,src_nj))
+  ALLOCATE(src_pk(src_ni,src_nj))
+  ALLOCATE(src_field_3D(src_ni,src_nj,src_nlev))
+  ALLOCATE(src_field_3D_soil(src_ni,src_nj,src_nsoil))
+!  ALLOCATE(src_field_soil_layers(src_nsoil))
+  ALLOCATE(src_pressure(src_ni,src_nj,src_nlev+1))
+  ALLOCATE(src_theta_rhodz(src_ni,src_nj,src_nlev))
+  ALLOCATE(src_tracers(src_ni,src_nj,src_nlev,src_nq))
+
+! load src_lon and src_lat
+  ierr=NF90_INQ_VARID(ncid,"rlonv",varid)
+  ierr=NF90_GET_VAR(ncid,varid, src_lon, &
+                    start=(/src_ibegin+1/),count=(/src_ni/))
+! convert rad to deg
+  src_lon(1:src_ni)=src_lon(1:src_ni)*(180.d0/pi)
+  WRITE(*,*) rank,":src_lon=",src_lon
+  ierr=NF90_INQ_VARID(ncid,"rlatu",varid)
+  ierr=NF90_GET_VAR(ncid,varid, src_lat, &
+                    start=(/src_jbegin+1/),count=(/src_nj/))
+! convert rad to deg
+  src_lat(1:src_nj)=src_lat(1:src_nj)*(180.d0/pi)
+  WRITE(*,*) rank,":src_lat=",src_lat
+
+! load ap, bp and controle
+  ALLOCATE(src_ap(src_nlev+1),src_bp(src_nlev+1))
+  ierr=NF90_INQ_VARID(ncid,"ap",varid)
+  ierr=NF90_GET_VAR(ncid,varid,src_ap)
+  WRITE(*,*) rank,":src_ap(1:5)=",src_ap(1:5)
+  ierr=NF90_INQ_VARID(ncid,"bp",varid)
+  ierr=NF90_GET_VAR(ncid,varid,src_bp)
+  WRITE(*,*) rank,":src_bp(1:5)=",src_bp(1:5)
+  
+! controle is taken in startphy_icosa_ref as start_archive_nc4 is too old
+  ierr=NF90_OPEN(src_controle_file, NF90_NOWRITE, ncid)
+  ALLOCATE(src_controle(100))
+  ierr=NF90_INQ_VARID(ncid,"controle",varid)
+  ierr=NF90_GET_VAR(ncid,varid,src_controle)
+  ! day_ini set to 0 as startphy_icosa_ref is a restart
+  src_controle(3)=0
+  WRITE(*,*) rank,":src_controle(1:5)=",src_controle(1:5)
+  gravity=src_controle(7)
+  WRITE(*,*) rank,":gravity=",gravity
+  kappa=src_controle(9)
+  WRITE(*,*) rank,":kappa=",kappa
+!  preff=src_controle(18)
+! AD: warning preff is hardcoded because not in controle of restartfi
+  preff=610.
+  WRITE(*,*) rank,":preff=",preff
+
+! destination coordinates
+  ierr=NF90_OPEN(dst_coord_file, NF90_NOWRITE, ncid)
+  ierr=NF90_INQ_VARID(ncid,"bounds_lon_mesh",varid)
+  ierr=NF90_INQUIRE_VARIABLE(ncid, varid,dimids=dimids)
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(1), len=dst_nvertex)
+  ierr=NF90_INQUIRE_DIMENSION(ncid, dimids(2), len=dst_ni_glo)
+  write(*,*) "rank=",rank," dst_nvertex=",dst_nvertex ! vertex
+  write(*,*) "rank=",rank," dst_ni_glo=",dst_ni_glo ! vertex boundaries
+
+! evenly split into MPI domains
+  div    = dst_ni_glo/size
+  remain = MOD( dst_ni_glo, size )
+  IF (rank < remain) THEN
+    dst_ni=div+1 ;
+    dst_ibegin=rank*(div+1) ;
+  ELSE
+    dst_ni=div ;
+    dst_ibegin= remain * (div+1) + (rank-remain) * div ;
+  ENDIF
+  write(*,*) "rank=",rank," dst_ni=",dst_ni
+
+  ALLOCATE(dst_lon(dst_ni))
+  ALLOCATE(dst_lat(dst_ni))
+  ALLOCATE(dst_boundslon(dst_nvertex,dst_ni))
+  ALLOCATE(dst_boundslat(dst_nvertex,dst_ni))
+
+! load dst_lon, dst_lat, dst_boundslon and dst_boundslat
+  ierr=NF90_INQ_VARID(ncid,"lon_mesh",varid)
+  ierr=NF90_GET_VAR(ncid,varid, dst_lon, &
+                    start=(/dst_ibegin+1/), count=(/dst_ni/))
+  WRITE(*,*) rank,":dst_lon(1:5)=",dst_lon(1:5)
+  ierr=NF90_INQ_VARID(ncid,"lat_mesh",varid)
+  ierr=NF90_GET_VAR(ncid,varid, dst_lat, &
+                    start=(/dst_ibegin+1/), count=(/dst_ni/))
+  WRITE(*,*) rank,":dst_lat(1:5)=",dst_lat(1:5)
+  ierr=NF90_INQ_VARID(ncid,"bounds_lon_mesh",varid)
+  ierr=NF90_GET_VAR(ncid,varid,dst_boundslon, &
+                    start=(/1,dst_ibegin+1/), count=(/dst_nvertex,dst_ni/))
+  WRITE(*,*) rank,":dst_boundslon(:,1:2)=",dst_boundslon(:,1:2)
+  ierr=NF90_INQ_VARID(ncid,"bounds_lat_mesh",varid)
+  ierr=NF90_GET_VAR(ncid,varid, dst_boundslat, &
+                    start=(/1,dst_ibegin+1/), count=(/dst_nvertex,dst_ni/))
+  WRITE(*,*) rank,":dst_boundslat(:,1:2)=",dst_boundslat(:,1:2)
+
+
+! Initialize XIOS context
+  WRITE(*,*) rank,":CALL xios_context_initialize()"
+  CALL xios_context_initialize("test",comm)
+  CALL xios_get_handle("test",ctx_hdl)
+  CALL xios_set_current_context(ctx_hdl)
+
+! Set XIOS calendar and timestep
+  CALL xios_get_calendar_type(calendar_type)
+  WRITE(*,*) rank,":calendar_type = ", calendar_type
+  dtime%second = 3600
+  CALL xios_set_timestep(dtime)
+
+! Set axes
+  ALLOCATE(lev_values(src_nlev))
+  lev_values=(/ (l,l=1,src_nlev) /)
+  write(*,*) rank,":lev_values()=",lev_values
+  CALL xios_set_axis_attr("lev",n_glo=src_nlev,value=lev_values)
+  ALLOCATE(nq_values(src_nq))
+  nq_values=(/(l,l=1,src_nq)/)
+  CALL xios_set_axis_attr("nq",n_glo=src_nq,value=nq_values)
+  ALLOCATE(soil_layers_values(src_nsoil))
+  soil_layers_values=(/ (l,l=1,src_nsoil) /)
+  CALL xios_set_axis_attr("soil_layers",n_glo=src_nsoil,value=soil_layers_values)
+
+! Set domains
+  CALL xios_set_domain_attr("src_domain_regular", &
+                            ni_glo=src_ni_glo, nj_glo=src_nj_glo, &
+                            ibegin=src_ibegin, ni=src_ni, &
+                            jbegin=src_jbegin, nj=src_nj, &
+                            type='rectilinear')
+  CALL xios_set_domain_attr("src_domain_regular", &
+                             data_dim=2, &
+                             data_ibegin=0, data_ni=src_ni, &
+                             data_jbegin=0, data_nj=src_nj)
+  CALL xios_set_domain_attr("src_domain_regular", &
+                            lonvalue_1D=src_lon, &
+                            latvalue_1D=src_lat)
+
+  CALL xios_set_domain_attr("src_topo_domain_regular", &
+                            ni_glo=src_topo_ni_glo, nj_glo=src_topo_nj_glo, &
+                            ibegin=src_topo_ibegin, ni=src_topo_ni, &
+                            jbegin=src_topo_jbegin, nj=src_topo_nj, &
+                            type='rectilinear')
+  CALL xios_set_domain_attr("src_topo_domain_regular", &
+                             data_dim=2, &
+                             data_ibegin=0, data_ni=src_topo_ni, &
+                             data_jbegin=0, data_nj=src_topo_nj)
+  CALL xios_set_domain_attr("src_topo_domain_regular", &
+                            lonvalue_1D=src_topo_lon, &
+                            latvalue_1D=src_topo_lat)
+
+  CALL xios_set_domain_attr("src_domain_regular_clean", &
+                            ni_glo=src_ni_glo-1, nj_glo=src_nj_glo, &
+                            ibegin=src_ibegin, ni=src_ni-1, &
+                            jbegin=src_jbegin, nj=src_nj, &
+                            type='rectilinear')
+  CALL xios_set_domain_attr("src_domain_regular_clean", &
+                             data_dim=2, &
+                             data_ibegin=0, data_ni=src_ni-1, &
+                             data_jbegin=0, data_nj=src_nj)
+  CALL xios_set_domain_attr("src_domain_regular_clean", &
+                            lonvalue_1D=src_lon(1:src_ni-1), &
+                            latvalue_1D=src_lat)
+
+  CALL xios_set_domain_attr("dst_domain_unstructured", &
+                            ni_glo=dst_ni_glo, &
+                            ibegin=dst_ibegin, &
+                            ni=dst_ni, &
+                            type="unstructured")
+  CALL xios_set_domain_attr("dst_domain_unstructured", &
+                            lonvalue_1D=dst_lon, &
+                            latvalue_1D=dst_lat, &
+                            bounds_lon_1D=dst_boundslon, &
+                            bounds_lat_1D=dst_boundslat, &
+                            nvertex=dst_nvertex)
+
+! Finalize XIOS context definition
+  WRITE(*,*) rank,":CALL xios_close_context_definition()"
+  CALL xios_close_context_definition()
+  CALL xios_get_handle("test",ctx_hdl)
+  CALL xios_set_current_context(ctx_hdl)
+
+! Temporal loop
+  DO ts=1,src_nt
+    WRITE(*,*) rank,":ts=",ts
+    ! Update calendar
+    CALL xios_update_calendar(ts)
+
+    ! Topography
+    CALL xios_recv_field("zMOL",src_topo)
+    WRITE(*,*) rank,":topo(1:2,1:3)=",src_topo(1:2,1:3)
+    ! Send surface geopotential
+    CALL xios_send_field("topo",src_topo(:,:)*gravity*1000.)
+
+    ! Surface pressure:
+    !! get data using XIOS:
+    CALL xios_recv_field("src_ps",src_field_2D)
+    WRITE(*,*) rank,":src_ps(1:2,1:3)=",src_field_2D(1:2,1:3)
+    !! write data using XIOS
+    CALL xios_send_field("ps_clean",src_field_2D(1:src_ni-1,1:src_nj))
+
+    ! compute inter-layer pressures
+    DO l=1,src_nlev+1
+      src_pressure(:,:,l) = src_ap(l)+src_bp(l)*src_field_2D(:,:)
+    ENDDO
+    
+    ! surface temperature:
+    CALL xios_recv_field("src_tsurf",src_field_2D)
+    WRITE(*,*) rank,":src_tsurf(1:2,1:3)=",src_field_2D(1:2,1:3)
+    CALL xios_send_field("tsurf_clean",src_field_2D(1:src_ni-1,1:src_nj))
+
+    ! co2 ice coverage:
+    CALL xios_recv_field("src_co2ice",src_field_2D)
+    WRITE(*,*) rank,":src_co2ice(1:2,1:3)=",src_field_2D(1:2,1:3)
+    CALL xios_send_field("co2ice_clean",src_field_2D(1:src_ni-1,1:src_nj))
+
+    ! emissivity:
+    CALL xios_recv_field("src_emis",src_field_2D)
+    WRITE(*,*) rank,":src_emis(1:2,1:3)=",src_field_2D(1:2,1:3)
+    CALL xios_send_field("emis_clean",src_field_2D(1:src_ni-1,1:src_nj))
+
+    ! q2 surf :
+    CALL xios_recv_field("src_q2surf",src_field_2D)
+    WRITE(*,*) rank,":src_q2(1:2,1:3)=",src_field_2D(1:2,1:3)
+    CALL xios_send_field("q2surf_clean",src_field_2D(1:src_ni-1,1:src_nj))
+    
+    ! Temperature:
+    CALL xios_recv_field("src_temp",src_field_3D)
+    ! compute theta_rhodz
+    DO l=1,src_nlev
+      src_pk(:,:)=((.5/preff)*(src_pressure(:,:,l)+src_pressure(:,:,l+1)))**kappa
+      src_theta_rhodz(:,:,l) = src_field_3D(:,:,l) * &
+      ((src_pressure(:,:,l)-src_pressure(:,:,l+1))/gravity)/src_pk(:,:)
+    ENDDO
+    CALL xios_send_field("theta_rhodz_clean", &
+                         src_theta_rhodz(1:src_ni-1,1:src_nj,1:src_nlev))
+
+    ! zonal wind
+    CALL xios_recv_field("src_u",src_field_3D)
+    CALL xios_send_field("u_clean", &
+                          src_field_3D(1:src_ni-1,1:src_nj,1:src_nlev))
+    
+    ! meridional wind
+    CALL xios_recv_field("src_v",src_field_3D)
+    CALL xios_send_field("v_clean", &
+                          src_field_3D(1:src_ni-1,1:src_nj,1:src_nlev))
+    ! tracers
+    ! AD:Warning, src_trac1 is for the moment only equal to c02 but is 4D array
+    CALL xios_recv_field("src_trac1",src_field_3D(:,:,:))
+    src_tracers(:,:,:,1)=src_field_3D(:,:,:)
+    CALL xios_send_field("tracers_clean", &
+                          src_tracers(1:src_ni-1,1:src_nj,1:src_nlev,1:src_nq))
+    CALL xios_send_field("co2_clean", &
+                          src_field_3D(1:src_ni-1,1:src_nj,1:src_nlev))
+
+    WRITE(*,*) rank,"AD: tracers and wind ok with xios"
+
+    ! soil temperature
+    CALL xios_recv_field("src_tsoil",src_field_3D_soil)
+    WRITE(*,*) rank,"AD: tsoil receive field ok with xios, "
+    CALL xios_send_field("tsoil_clean", &
+                          src_field_3D_soil(1:src_ni-1,1:src_nj,1:src_nsoil))
+    WRITE(*,*) rank,"AD: tsoil ok with xios"
+    ! soil thermal intertia
+    CALL xios_recv_field("src_inertiedat",src_field_3D_soil)
+    CALL xios_send_field("inertiedat_clean", &
+                          src_field_3D_soil(1:src_ni-1,1:src_nj,1:src_nsoil))
+    WRITE(*,*) rank,"AD: thermal inertia ok with xios"
+
+  ENDDO ! of DO ts=1,src_nt
+  
+!! Finalize
+  write(*,*) rank,":Finalize: call xios_context_finalize"
+  CALL xios_context_finalize()
+
+  write(*,*) rank,":Finalize: call MPI_COMM_FREE"
+  CALL MPI_COMM_FREE(comm, ierr)
+  write(*,*) rank,":Finalize: call xios_finalize"
+  CALL xios_finalize()
+
+  if (rank==0) then
+    ! add a couple of things in the "startphy_icosa.nc" file
+    write(*,*) rank,"Write controle() to startphy_icosa.nc"
+    ierr=NF90_OPEN("startphy_icosa.nc",NF90_WRITE,ncid)
+    ierr=NF90_REDEF(ncid) ! switch to define mode
+    ierr=NF90_DEF_DIM(ncid,"index",100,dimids(1))
+    ierr=NF90_DEF_VAR(ncid,"controle",NF90_DOUBLE,dimids(1),varid)
+    ierr=NF90_ENDDEF(ncid) ! switch out of define mode
+    ierr=NF90_PUT_VAR(ncid,varid,src_controle(1:100))
+    if (ierr.ne.NF90_NOERR) then
+      write(*,*) "NetCDF Error:",NF90_STRERROR(ierr)
+    endif
+    ierr=NF90_CLOSE(ncid)
+    
+    ! add a couple of things in the "start_icosa.nc" file
+    ierr=NF90_OPEN("start_icosa.nc",NF90_WRITE,ncid)
+    ierr=NF90_REDEF(ncid)
+    ierr=NF90_DEF_DIM(ncid,"nvertex_u",2,dimids(1))
+    ierr=NF90_DEF_VAR(ncid,"iteration",NF90_FLOAT,varid)
+    ierr=NF90_ENDDEF(ncid)
+    if (ierr.ne.NF90_NOERR) then
+      write(*,*) "NetCDF Error:",NF90_STRERROR(ierr)
+    endif
+    ierr=NF90_PUT_VAR(ncid,varid,0) ! set "iteration" value to 0
+    ierr=NF90_CLOSE(ncid)
+    
+  endif ! of if (rank==0)
+
+  write(*,*) rank,":Finalize: call MPI_FINALIZE"
+  CALL MPI_FINALIZE(ierr)
+
+  write(*,*) rank,":my_remap: all is well that ends well!"
+
+END PROGRAM start_archive2icosa
