Index: trunk/LMDZ.GENERIC/README
===================================================================
--- trunk/LMDZ.GENERIC/README	(revision 1621)
+++ trunk/LMDZ.GENERIC/README	(revision 1622)
@@ -1275,2 +1275,9 @@
 - added scalheight (atmospheric scale height) in comvert_mod.
 
+== 31/10/2016 == EM
+Introducing a first prototype of XIOS outputs: requires compiling with
+"-io xios" flag and having an XIOS library at hand. Corresponding
+companion xml files iodef.xml and context_lmdz_physics.xml in the "deftank"
+subdirectory. Still some work to do concerning the XIOS calendar (definition
+of an initial date and month number and lenght).
+
Index: trunk/LMDZ.GENERIC/deftank/context_lmdz_physics.xml
===================================================================
--- trunk/LMDZ.GENERIC/deftank/context_lmdz_physics.xml	(revision 1622)
+++ trunk/LMDZ.GENERIC/deftank/context_lmdz_physics.xml	(revision 1622)
@@ -0,0 +1,111 @@
+<context id="LMDZ">
+
+    <!-- Definition of a calendar is done in code via wxios_set_cal -->
+    
+    <!-- Definition of domains and groups of domains -->
+    <domain_definition>
+        <domain id="dom_glo" data_dim="2" />
+    </domain_definition>
+        
+    <!-- Definition of vertical axes 
+         NB: these must be defined and set in code via
+         wxios_add_vaxis -->
+    <axis_definition>
+        <axis id="presnivs" 
+              standard_name="Pseudo-pressure of model vertical levels" 
+              unit="Pa">
+        </axis>
+        <axis id="altitude" 
+              standard_name="Pseudo-altitude of model vertical levels" 
+              unit="km">
+        </axis>
+    </axis_definition>
+    
+    <!-- toggle axis id below to change output vertical axis -->
+    <grid id="grid_3d">
+        <domain id="dom_glo" />
+        <!-- <axis id="presnivs" /> -->
+        <axis id="altitude" />
+    </grid>
+    
+    <!-- Definition of model variables -->
+    <field_definition prec="4" 
+                      freq_op="1ts"
+                      enabled=".TRUE.">
+            
+        <!-- 2D variables -->
+        <field_group id="fields_2D" domain_ref="dom_glo">
+            <field id="ps"
+                   long_name="Surface Pressure" 
+                   unit="Pa" />
+            <field id="area"
+                   long_name="Mesh area"
+                   unit="m2" />
+        </field_group>
+
+        <!-- 3D variables -->
+        <field_group id="fields_3D" domain_ref="dom_glo" axis_ref="altitude">
+        <!-- <field_group id="fields_3D" grid_ref="grid_3d"> --> <!-- doesn't work... why? -->
+            <field id="temperature"
+                   long_name="Atmospheric temperature"
+                   unit="K" />
+            <field id="u"
+                   long_name="Eastward Zonal Wind"
+                   unit="m/s" />
+            <field id="v" 
+                   long_name="Northward Meridional Wind" 
+                   unit="m/s" />
+        </field_group>
+
+    </field_definition>
+    
+    <!-- Definition of output files -->
+    <file_definition>
+        <!-- Instantaneous outputs; Here output_freq in physics time steps -->
+        <file id="histins"
+              name="Xhistins"
+              output_freq="1ts" 
+              type="one_file"
+              enabled=".true.">
+                    
+            <!-- VARS 2D -->
+            <field_group operation="instant"
+                         freq_op="1ts">
+                <field field_ref="area" operation="once" />
+                <field field_ref="ps" />
+            </field_group>
+
+            <!-- VARS 3D -->
+            <field_group operation="instant"
+                         freq_op="1ts">
+                <field field_ref="temperature" />
+                <field field_ref="u" />
+                <field field_ref="v" />
+            </field_group>
+        </file>
+
+        <!-- diurnal averages outputs; output_freq is every day -->
+        <file id="diurnalave"
+              name="Xdiurnalave"
+              output_freq="1d" 
+              type="one_file"
+              enabled=".true.">
+                    
+            <!-- VARS 2D -->
+            <field_group operation="average"
+                         freq_op="1ts">
+                <field field_ref="area" operation="once" />
+                <field field_ref="ps" />
+            </field_group>
+
+            <!-- VARS 3D -->
+            <field_group operation="average"
+                         freq_op="1ts">
+                <field field_ref="temperature" />
+                <field field_ref="u" />
+                <field field_ref="v" />
+            </field_group>
+        </file>
+    </file_definition>
+
+</context>
Index: trunk/LMDZ.GENERIC/deftank/iodef.xml
===================================================================
--- trunk/LMDZ.GENERIC/deftank/iodef.xml	(revision 1622)
+++ trunk/LMDZ.GENERIC/deftank/iodef.xml	(revision 1622)
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<simulation> 
+        
+        <context  id="xios">
+            <variable_definition>
+                <variable_group id="buffer">
+                buffer_size = 85000000
+                buffer_server_factor_size = 2 
+                </variable_group>
+
+                <variable_group id="parameters" >
+                    <variable id="using_server" type="bool">false</variable>
+                    <variable id="info_level" type="int">100</variable>
+                </variable_group>
+            </variable_definition>
+        </context>
+        
+
+        <context id="LMDZ" src="./context_lmdz_physics.xml"/>
+
+
+</simulation>
Index: trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90	(revision 1621)
+++ trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90	(revision 1622)
@@ -46,4 +46,8 @@
       use time_phylmdz_mod, only: daysec
       use callkeys_mod
+      use vertical_layers_mod, only: presnivs, pseudoalt
+      use xios_output_mod, only: initialize_xios_output, &
+                                 update_xios_timestep, &
+                                 send_xios_field
       implicit none
 
@@ -198,6 +202,8 @@
 ! ----------------------
 
-      integer day_ini                                              ! Initial date of the run (sol since Ls=0).
-      integer icount                                               ! Counter of calls to physiq during the run.
+      integer,save :: day_ini                                      ! Initial date of the run (sol since Ls=0).
+      integer,save :: icount                                       ! Counter of calls to physiq during the run.
+!$OMP THREADPRIVATE(day_ini,icount)
+
       real, dimension(:),allocatable,save ::  tsurf                ! Surface temperature (K).
       real, dimension(:,:),allocatable,save ::  tsoil              ! Sub-surface temperatures (K).
@@ -225,7 +231,4 @@
 !$OMP THREADPRIVATE(emis,dtrad,fluxrad_sky,fluxrad,capcal,fluxgrd,qsurf,q2)
 
-      save day_ini, icount
-      
-!$OMP THREADPRIVATE(day_ini,icount)
 
 ! Local variables :
@@ -706,5 +709,11 @@
          endif
          
-               
+         ! XIOS outputs
+#ifdef CPP_XIOS
+
+         write(*,*) "physiq: call initialize_xios_output"
+         call initialize_xios_output(pday,ptime,ptimestep,daysec, &
+                                     presnivs,pseudoalt)
+#endif
       endif ! end of 'firstcall'
 
@@ -712,4 +721,9 @@
 ! I.2   Initializations done at every physical timestep:
 ! ------------------------------------------------------
+
+#ifdef CPP_XIOS      
+      ! update XIOS time/calendar
+      call update_xios_timestep
+#endif      
 
       ! Initialize various variables
@@ -2132,4 +2146,17 @@
       endif
 
+! XIOS outputs
+#ifdef CPP_XIOS      
+      ! Send fields to XIOS: (NB these fields must also be defined as
+      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
+      CALL send_xios_field("ps",ps)
+      CALL send_xios_field("area",cell_area)
+
+      CALL send_xios_field("temperature",zt)
+      CALL send_xios_field("u",zu)
+      CALL send_xios_field("v",zv)
+      
+#endif
+
       icount=icount+1
 
Index: trunk/LMDZ.GENERIC/libf/phystd/xios_output_mod.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phystd/xios_output_mod.F90	(revision 1622)
+++ trunk/LMDZ.GENERIC/libf/phystd/xios_output_mod.F90	(revision 1622)
@@ -0,0 +1,220 @@
+MODULE xios_output_mod
+
+ IMPLICIT NONE
+ 
+ INTEGER,PRIVATE,SAVE :: time_it=0 ! store number of iterations with calls to XIOS since start
+! does not need to be threadprivate; managed by omp master
+
+ CHARACTER(LEN=*), PARAMETER :: context_id= "LMDZ" ! same as in context_lmdz_physics.xml
+ 
+#ifdef CPP_XIOS
+
+ INTERFACE send_xios_field
+    MODULE PROCEDURE histwrite2d_xios,histwrite3d_xios
+ END INTERFACE
+ 
+
+CONTAINS
+
+  SUBROUTINE initialize_xios_output(day,timeofday,dtphys,daysec,&
+                                    presnivs,pseudoalt)
+!  USE mod_phys_lmdz_para, only: gather, bcast, &
+!                                jj_nb, jj_begin, jj_end, ii_begin, ii_end, &
+!                                mpi_size, mpi_rank, klon_mpi, &
+!                                is_sequential, is_south_pole_dyn
+  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: lunout, prt_level
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat
+  USE regular_lonlat_mod, ONLY: lon_reg, lat_reg
+  USE nrtype, ONLY: pi
+  USE wxios
+  IMPLICIT NONE
+  
+  REAL,INTENT(IN) :: day ! Number of elapsed sols since reference Ls=0.
+  REAL,INTENT(IN) :: timeofday ! "Universal time", given as fraction of sol (e.g.: 0.5 for noon).
+  REAL,INTENT(IN) :: dtphys ! physics time step (s)
+  REAL,INTENT(IN) :: daysec ! lengthof a standard day (s)
+  REAL,INTENT(IN) :: presnivs(:) ! vertical grid approximate pressure (Pa)
+  REAL,INTENT(IN) :: pseudoalt(:) ! vertical grid approximate altitude (km)
+  
+  
+  INTEGER :: data_ibegin, data_iend
+  TYPE(xios_duration) :: timestep
+  TYPE(xios_date) :: time_origin
+  TYPE(xios_date) :: start_date
+  
+!$OMP BARRIER
+!$OMP MASTER
+
+    ! 1. Declare available vertical axes to be used in output files:
+    IF (prt_level>=10) WRITE(lunout,*) "initialize_xios_output: call xios_set_axis_attr for presnivs"
+    CALL xios_set_axis_attr("presnivs", n_glo=size(presnivs), value=presnivs,&
+                            unit="Pa",positive="down")
+    IF (prt_level>=10) WRITE(lunout,*) "initialize_xios_output: call xios_set_axis_attr for altitude"
+    CALL xios_set_axis_attr("altitude", n_glo=size(pseudoalt), value=pseudoalt,&
+                            unit="km",positive="up")
+    
+    ! 2. Declare horizontal domain
+    ! Set values for the mask:
+    IF (mpi_rank == 0) THEN
+        data_ibegin = 0
+    ELSE 
+        data_ibegin = ii_begin - 1
+    END IF
+
+    IF (mpi_rank == mpi_size-1) THEN
+        data_iend = nbp_lon
+    ELSE
+        data_iend = ii_end + 1
+    END IF
+
+    if (prt_level>=10) then
+      write(lunout,*) "initialize_xios_output: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
+      write(lunout,*) "initialize_xios_output: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
+      write(lunout,*) "initialize_xios_output: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+      write(lunout,*) "initialize_xios_output: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+      write(lunout,*) "initialize_xios_output: mpirank=",mpi_rank," is_south_pole=",is_south_pole_dyn
+    endif
+
+    ! Initialize the XIOS domain coreesponding to this process:
+    if (prt_level>=10) write(lunout,*) "initialize_xios_output: call wxios_domain_param"
+    CALL wxios_domain_param("dom_glo", is_sequential, nbp_lon, jj_nb, nbp_lon, nbp_lat, &
+                            1, nbp_lon, ii_begin, ii_end, jj_begin, jj_end,             &
+                            klon_mpi+2*(nbp_lon-1), data_ibegin, data_iend,             &
+                            lat_reg*(180./pi), lon_reg*(180./pi),                       &
+                            is_south_pole_dyn,mpi_rank)
+
+    ! 3. Declare calendar and time step
+    if (prt_level>=10) write(lunout,*) "initialize_xios_output: build calendar"
+    timestep%second=dtphys
+    ! time origin of the simulation (default: 1st year/1st month/1st day, Ls=0)
+    time_origin=xios_date(1,1,1,0,0,0)
+    ! start date of the simulation (i.e time elapsed since last Ls=0)
+    start_date=xios_date(1,1,1,0,0,nint((day+timeofday)*daysec))
+    CALL xios_define_calendar(type="user_defined", &
+                              timestep=timestep, &
+                              day_length=nint(daysec), &
+                              start_date=start_date, &
+                              time_origin=time_origin, &
+                              month_lengths=[30,30,30,30,30,30,30,30,30,30,30,30])
+    
+    ! 4. Finalize the context:
+    if (prt_level>=10) write(*,*) "initialize_xios_output: call wxios_closedef"
+    CALL wxios_closedef()
+
+!$OMP END MASTER
+!$OMP BARRIER
+  
+  END SUBROUTINE initialize_xios_output
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  SUBROUTINE finalize_xios_output
+  USE xios
+  IMPLICIT NONE
+!$OMP BARRIER    
+!$OMP MASTER
+    CALL xios_context_finalize
+!$OMP END MASTER    
+!$OMP BARRIER    
+  
+  END SUBROUTINE finalize_xios_output
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  SUBROUTINE update_xios_timestep
+  USE xios
+  IMPLICIT NONE
+    CALL set_xios_context
+!$OMP MASTER
+    time_it=time_it+1
+    CALL xios_update_calendar(time_it)
+!$OMP END MASTER    
+  END SUBROUTINE update_xios_timestep
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  SUBROUTINE set_xios_context
+  USE XIOS
+  IMPLICIT NONE
+    TYPE(xios_context) :: ctx_hdl
+
+!$OMP MASTER
+    CALL xios_get_handle(context_id,ctx_hdl)
+    CALL xios_set_current_context(ctx_hdl)
+!$OMP END MASTER    
+  END SUBROUTINE set_xios_context
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  SUBROUTINE histwrite2d_xios(field_name,field)
+  USE dimphy, only: klon
+  USE mod_phys_lmdz_para, only: gather_omp, grid1Dto2D_mpi, &
+                                jj_nb, klon_mpi
+  USE xios, only: xios_send_field
+  USE print_control_mod, ONLY: prt_level, lunout
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+  IMPLICIT NONE
+
+    CHARACTER(LEN=*), INTENT(IN) :: field_name
+    REAL, DIMENSION(:), INTENT(IN) :: field
+      
+    REAL,DIMENSION(klon_mpi) :: buffer_omp
+    REAL :: Field2d(nbp_lon,jj_nb)
+
+    IF (prt_level >= 10) WRITE(lunout,*)'Begin histrwrite2d_xios ',trim(field_name)
+
+    IF (SIZE(field)/=klon) CALL abort_physic('iophy::histwrite2d_xios','Field first DIMENSION not equal to klon',1)
+    
+    CALL Gather_omp(field,buffer_omp)    
+!$OMP MASTER
+    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
+    
+    CALL xios_send_field(field_name, Field2d)
+!$OMP END MASTER   
+
+    IF (prt_level >= 10) WRITE(lunout,*)'End histrwrite2d_xios ',trim(field_name)
+  END SUBROUTINE histwrite2d_xios
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+  SUBROUTINE histwrite3d_xios(field_name, field)
+  USE dimphy, only: klon, klev
+  USE mod_phys_lmdz_para, only: gather_omp, grid1Dto2D_mpi, &
+                                jj_nb, klon_mpi
+  USE xios, only: xios_send_field
+  USE print_control_mod, ONLY: prt_level,lunout
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon
+
+  IMPLICIT NONE
+
+    CHARACTER(LEN=*), INTENT(IN) :: field_name
+    REAL, DIMENSION(:,:), INTENT(IN) :: field ! --> field(klon,:)
+
+    REAL,DIMENSION(klon_mpi,SIZE(field,2)) :: buffer_omp
+    REAL :: Field3d(nbp_lon,jj_nb,SIZE(field,2))
+    INTEGER :: ip, n, nlev
+
+  IF (prt_level >= 10) write(lunout,*)'Begin histrwrite3d_xios ',trim(field_name)
+
+    !Et on.... écrit
+    IF (SIZE(field,1)/=klon) CALL abort_physic('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
+    nlev=SIZE(field,2)
+
+
+    CALL Gather_omp(field,buffer_omp)
+!$OMP MASTER
+    CALL grid1Dto2D_mpi(buffer_omp,field3d)
+
+    CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
+!$OMP END MASTER   
+
+    IF (prt_level >= 10) write(lunout,*)'End histrwrite3d_xios ',trim(field_name)
+  END SUBROUTINE histwrite3d_xios
+
+#endif
+
+END MODULE xios_output_mod
