Index: trunk/LMDZ.COMMON/libf/bibio/wxios.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/bibio/wxios.F90	(revision 1107)
+++ trunk/LMDZ.COMMON/libf/bibio/wxios.F90	(revision 1300)
@@ -18,4 +18,8 @@
     TYPE(xios_context), SAVE :: g_ctx
 !$OMP THREADPRIVATE(g_comm,g_cts_name,g_ctx)
+    LOGICAL, SAVE :: g_flag_xml = .FALSE.
+    CHARACTER(len=100) :: g_field_name = "nofield"
+!$OMP THREADPRIVATE(g_flag_xml,g_field_name)
+
 
     CONTAINS
@@ -25,15 +29,11 @@
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
-    SUBROUTINE concat(str, i, str_i) !MAX i = 99
-        CHARACTER(len=*), INTENT(IN) :: str
-        INTEGER, INTENT(IN) :: i
-        CHARACTER(len=100), INTENT(OUT) :: str_i
-        
-        
-        !INT -> CHAR:
-        CHARACTER(len=10) :: num
-        WRITE(num, "(I5)") i
-        str_i = TRIM(ADJUSTL(str//"_"//TRIM(ADJUSTL(num))))
-        
+    SUBROUTINE concat(str, str2, str_str2)
+        CHARACTER(len=*), INTENT(IN) :: str, str2
+        CHARACTER(len=20), INTENT(OUT) :: str_str2
+        
+        
+        str_str2 = TRIM(ADJUSTL(str//"_"//TRIM(ADJUSTL(str2))))
+        !IF (prt_level >= 10) WRITE(lunout,*) "Xios. ",str,"+",str2,"=",str_str2
     END SUBROUTINE concat
     
@@ -47,18 +47,30 @@
         
         INTEGER :: i = 0
-        
+         !!!!!!!!!!!!!!!!!!
+         ! Pour XIOS:
+         !  year : y
+         !  month : mo
+         !  day : d
+         !  hour : h
+         !  minute : mi
+         !  second : s
+         !!!!!!!!!!!!!!!!!!
+
         i = INDEX(odate, "day")
         IF (i > 0) THEN
             ndate = odate(1:i-1)//"d"
-        ELSE
-            i = INDEX(odate, "hr")
-            IF (i > 0) THEN
-                ndate = odate(1:i-1)//"h"
-            ELSE
-                ndate = odate
-            END IF
-        END IF
-        
-        !WRITE(*,*) "Xios. ", odate, " => ", ndate
+        END IF
+
+        i = INDEX(odate, "hr")
+        IF (i > 0) THEN
+            ndate = odate(1:i-1)//"h"
+        END IF
+
+        i = INDEX(odate, "mth")
+        IF (i > 0) THEN
+            ndate = odate(1:i-1)//"mo"
+        END IF
+        
+        !IF (prt_level >= 10) WRITE(lunout,*) "Xios. ", odate, " => ", ndate
     END SUBROUTINE reformadate
     
@@ -89,5 +101,5 @@
         END IF
         
-        !WRITE(*,*) "Xios. ", op, " => ", reformaop
+        !IF (prt_level >= 10) WRITE(lunout,*) "Xios. ", op, " => ", reformaop
     END FUNCTION reformaop
 
@@ -97,27 +109,58 @@
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
-    SUBROUTINE wxios_init(xios_ctx_name)
+    SUBROUTINE wxios_init(xios_ctx_name, locom, outcom)
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
       CHARACTER(len=*), INTENT(IN) :: xios_ctx_name
-        
-      INTEGER :: xios_comm
+      INTEGER, INTENT(IN), OPTIONAL :: locom
+      INTEGER, INTENT(OUT), OPTIONAL :: outcom
+
+    
         TYPE(xios_context) :: xios_ctx
-      
-        WRITE(*,*) "Xios. Initialization"
-
-        !Lancement de xios:
-        CALL xios_initialize(xios_ctx_name, return_comm = xios_comm )
-        
-        !Initialisation du contexte:
-        CALL xios_context_initialize(xios_ctx_name, xios_comm)
-        CALL xios_get_handle(xios_ctx_name, xios_ctx)    !RÃ©cupÃ©ration
-        CALL xios_set_current_context(xios_ctx)            !Activation
+        INTEGER :: xios_comm
+
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_init: Initialization"
+
+
+
+        IF (PRESENT(locom)) THEN
+          CALL xios_initialize(xios_ctx_name, local_comm = locom, return_comm = xios_comm )
+          IF (prt_level >= 10) WRITE(lunout,*) "wxios_init: ctx=",xios_ctx_name," local_comm=",locom,", return_comm=",xios_comm
+        ELSE
+          CALL xios_initialize(xios_ctx_name, return_comm = xios_comm )
+          IF (prt_level >= 10) WRITE(lunout,*) "wxios_init: ctx=",xios_ctx_name," return_comm=",xios_comm
+        END IF
+        
+        IF (PRESENT(outcom)) THEN
+          outcom = xios_comm
+          IF (prt_level >= 10) WRITE(lunout,*) "wxios_init: ctx=",xios_ctx_name," outcom=",outcom
+        END IF
         
         !Enregistrement des variables globales:
         g_comm = xios_comm
         g_ctx_name = xios_ctx_name
+        
+        CALL wxios_context_init()
+        
+    END SUBROUTINE wxios_init
+
+    SUBROUTINE wxios_context_init()
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
+        TYPE(xios_context) :: xios_ctx
+
+        !Initialisation du contexte:
+        CALL xios_context_initialize(g_ctx_name, g_comm)
+        CALL xios_get_handle(g_ctx_name, xios_ctx)    !RÃ©cupÃ©ration
+        CALL xios_set_current_context(xios_ctx)            !Activation
         g_ctx = xios_ctx
-        
-        WRITE(*,*) "Xios. Current context is ", xios_ctx_name
-    END SUBROUTINE wxios_init
+
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_context_init: Current context is ",trim(g_ctx_name)
+
+        !Une premiÃ¨re analyse des hÃ©ritages:
+        CALL xios_solve_inheritance()
+    END SUBROUTINE wxios_context_init
 
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -126,4 +169,7 @@
 
     SUBROUTINE wxios_set_cal(pasdetemps, calendrier, annee, mois, jour, heure)
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
      !ParamÃ¨tres:
      CHARACTER(len=*), INTENT(IN) :: calendrier
@@ -146,13 +192,13 @@
             CASE('earth_360d')
                 CALL xios_set_context_attr_hdl(g_ctx, calendar_type= "D360")
-                WRITE(*,*) 'Xios. Calendrier terrestre a 360 jours/an'
+                IF (prt_level >= 10) WRITE(lunout,*) 'wxios_set_cal: Calendrier terrestre a 360 jours/an'
             CASE('earth_365d')
                 CALL xios_set_context_attr_hdl(g_ctx, calendar_type= "NoLeap")
-                WRITE(*,*) 'Xios. Calendrier terrestre a 365 jours/an'
+                IF (prt_level >= 10) WRITE(lunout,*) 'wxios_set_cal: Calendrier terrestre a 365 jours/an'
             CASE('earth_366d')
                 CALL xios_set_context_attr_hdl(g_ctx, calendar_type= "Gregorian")
-                WRITE(*,*) 'Xios. Calendrier gregorien'
+                IF (prt_level >= 10) WRITE(lunout,*) 'wxios_set_cal: Calendrier gregorien'
             CASE DEFAULT
-                abort_message = 'Xios. Mauvais choix de calendrier'
+                abort_message = 'wxios_set_cal: Mauvais choix de calendrier'
                 CALL abort_gcm('Gcm:Xios',abort_message,1)
         END SELECT
@@ -161,5 +207,5 @@
         WRITE(date, "(i4.4,'-',i2.2,'-',i2.2,' 00:00:00')") annee, mois, jour
         
-        WRITE(*,*) "Xios. Initial time: ", date
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_set_cal: Initial time: ", date
         
         CALL xios_set_context_attr_hdl(g_ctx, start_date= date)
@@ -167,49 +213,88 @@
         !Et enfin,le pas de temps:
         CALL xios_set_timestep(mdtime)
-        WRITE(*,*) "Xios. ts=",mdtime
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_set_cal: ts=",mdtime
     END SUBROUTINE wxios_set_cal
+
+    SUBROUTINE wxios_set_timestep(ts)
+        REAL, INTENT(IN) :: ts
+        TYPE(xios_time) :: mdtime     
+
+        mdtime = xios_time(0, 0, 0, 0, 0, ts)
+
+        CALL xios_set_timestep(mdtime)
+    END SUBROUTINE wxios_set_timestep
 
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     ! Pour initialiser un domaine !!!!!!!!!!!!!!!!!!!!
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-    SUBROUTINE wxios_domain_param(dom_id, is_sequential, iim, jjm, io_lat, io_lon)
+    SUBROUTINE wxios_domain_param(dom_id, is_sequential, ni, nj, ni_glo, nj_glo,        &
+                                    ibegin, iend, ii_begin, ii_end, jbegin, jend,       &
+                                    data_ni, data_ibegin, data_iend,                    &
+                                    io_lat, io_lon,is_south_pole,mpi_rank)
          
-        CHARACTER (len=*), INTENT(IN) :: dom_id
-        LOGICAL, INTENT(IN) :: is_sequential
-        INTEGER, INTENT(IN) :: iim, jjm
-        REAL, DIMENSION(:) :: io_lat, io_lon
-        
+
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
+        CHARACTER(len=*),INTENT(IN) :: dom_id ! domain identifier
+        LOGICAL,INTENT(IN) :: is_sequential ! flag
+        INTEGER,INTENT(IN) :: ni ! local MPI domain number of longitudes
+        INTEGER,INTENT(IN) :: nj ! local MPI domain number of latitudes
+        INTEGER,INTENT(IN) :: ni_glo ! global grid number of longitudes
+        INTEGER,INTENT(IN) :: nj_glo ! global grid number of latitudes
+        INTEGER,INTENT(IN) :: ibegin ! start index, on global grid, of local MPI domain 
+        INTEGER,INTENT(IN) :: iend ! end index, on global grid, of local MPI domain
+        INTEGER,INTENT(IN) :: ii_begin ! i index at which local data starts (first row)
+        INTEGER,INTENT(IN) :: ii_end ! i index at which local data ends (last row)
+        INTEGER,INTENT(IN) :: jbegin ! start index, on global grid, of local MPI domain 
+        INTEGER,INTENT(IN) :: jend ! end index, on global grid, of local MPI domain
+        INTEGER,INTENT(IN) :: data_ni
+        INTEGER,INTENT(IN) :: data_ibegin
+        INTEGER,INTENT(IN) :: data_iend
+        REAL,INTENT(IN) :: io_lat(:) ! latitudes (of global grid)
+        REAL,INTENT(IN) :: io_lon(:) ! longitudes (of global grid)
+        logical,intent(in) :: is_south_pole ! does this process include the south pole?
+        integer,intent(in) :: mpi_rank ! rank of process
         
         TYPE(xios_domain) :: dom
-        INTEGER :: ni, nj, ni_glo, nj_glo, ibegin, iend, jbegin, jend
-        LOGICAl :: boool
-        
-        ni_glo = iim
-        nj_glo = jjm
-        ni = iim
-        nj = jjm
-        ibegin = 1
-        jbegin = 1
-        iend = ibegin + ni - 1
-        jend = jbegin + nj - 1
+        LOGICAL :: boool
+        
+        !Masque pour les problÃ¨mes de recouvrement MPI:
+        LOGICAL :: mask(ni,nj)
         
         !On rÃ©cupÃ¨re le handle:
         CALL xios_get_domain_handle(dom_id, dom)
         
-        WRITE(*,*) "Xios. ni:",iim," ni_glo:", iim, " nj:", jjm, " nj_glo:", jjm
-        WRITE(*,*) "Xios. Size lon:", SIZE(io_lon), " lat:", SIZE(io_lat)
+        IF (prt_level >= 10) THEN
+          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ni:",ni," ni_glo:", ni_glo, " nj:", nj, " nj_glo:", nj_glo
+          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ibegin:",ibegin," iend:", iend, " jbegin:", jbegin, " jend:", jend
+          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," ii_begin:",ii_begin," ii_end:", ii_end
+          WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," Size io_lon:", SIZE(io_lon(ibegin:iend)), " io_lat:", SIZE(io_lat(jbegin:jend))
+        ENDIF
         
         !On parametrise le domaine:
-        !IF (is_sequential) THEN
-            CALL xios_set_domain_attr_hdl(dom, ni_glo=iim, ibegin=1, ni=iim,&
-            & nj_glo=jjm, jbegin=1,nj=jjm,&
-            & lonvalue=io_lon(ibegin:iend), latvalue=io_lat(jbegin:jend))
-        !END IF
+        CALL xios_set_domain_attr_hdl(dom, ni_glo=ni_glo, ibegin=ibegin, ni=ni)
+        CALL xios_set_domain_attr_hdl(dom, nj_glo=nj_glo, jbegin=jbegin, nj=nj, data_dim=2)
+        CALL xios_set_domain_attr_hdl(dom, lonvalue=io_lon(ibegin:iend), latvalue=io_lat(jbegin:jend))
+
+        IF (.NOT.is_sequential) THEN
+            mask(:,:)=.TRUE.
+            if (ii_begin>1) mask(1:ii_begin-1,1) = .FALSE.
+            if (ii_end<ni) mask(ii_end+1:ni,nj) = .FALSE.
+            ! special case for south pole
+            if ((ii_end.eq.1).and.(is_south_pole)) mask(1:ni,nj)=.true.
+            IF (prt_level >= 10) THEN
+              WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," mask(:,1)=",mask(:,1)
+              WRITE(lunout,*) "wxios_domain_param: mpirank=",mpi_rank," mask(:,nj)=",mask(:,nj)
+            ENDIF
+            CALL xios_set_domain_attr_hdl(dom, mask=mask)
+        END IF
+
          CALL xios_is_defined_domain_attr_hdl(dom,ni_glo=boool)
         !VÃ©rification:
         IF (xios_is_valid_domain(dom_id)) THEN
-            WRITE(*,*) "Xios. Domain initialized: ", dom_id, boool
-        ELSE
-            WRITE(*,*) "Xios. Invalid domain: ", dom_id
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_domain_param: Domain initialized: ", trim(dom_id), boool
+        ELSE
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_domain_param: Invalid domain: ", trim(dom_id)
         END IF
     END SUBROUTINE wxios_domain_param
@@ -218,16 +303,19 @@
     ! Pour dÃ©clarer un axe vertical !!!!!!!!!!!!!!!
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-    SUBROUTINE wxios_add_vaxis(axisgroup_id, axis_file_id, axis_size, axis_value)
-        CHARACTER (len=*), INTENT(IN) :: axisgroup_id
-        INTEGER, INTENT(IN) :: axis_file_id, axis_size
+    SUBROUTINE wxios_add_vaxis(axisgroup_id, axis_file, axis_size, axis_value)
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
+        CHARACTER (len=*), INTENT(IN) :: axisgroup_id, axis_file
+        INTEGER, INTENT(IN) :: axis_size
         REAL, DIMENSION(axis_size), INTENT(IN) :: axis_value
         
         TYPE(xios_axisgroup) :: axgroup
         TYPE(xios_axis) :: ax
-        CHARACTER(len=100) :: axis_id
+        CHARACTER(len=20) :: axis_id
         
         
         !PrÃ©paration du nom de l'axe:
-        CALL concat(axisgroup_id, axis_file_id, axis_id)
+        CALL concat(axisgroup_id, axis_file, axis_id)
         
         !On rÃ©cupÃ¨re le groupe d'axes qui va bien:
@@ -235,5 +323,5 @@
         
         !On ajoute l'axe correspondant Ã  ce fichier:
-        CALL xios_add_axis(axgroup, ax, axis_id)
+        CALL xios_add_axis(axgroup, ax, TRIM(ADJUSTL(axis_id)))
         
         !Et on le parametrise:
@@ -241,8 +329,8 @@
         
         !VÃ©rification:
-        IF (xios_is_valid_axis(axis_id)) THEN
-            WRITE(*,*) "Xios. Axis created: ", axis_id
-        ELSE
-            WRITE(*,*) "Xios. Invalid axis: ", axis_id
+        IF (xios_is_valid_axis(TRIM(ADJUSTL(axis_id)))) THEN
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_vaxis: Axis created: ", TRIM(ADJUSTL(axis_id))
+        ELSE
+            WRITE(*,*) "wxios_add_vaxis: Invalid axis: ", TRIM(ADJUSTL(axis_id))
         END IF
 
@@ -254,4 +342,7 @@
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     SUBROUTINE wxios_add_file(fname, ffreq, flvl)
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
         CHARACTER(len=*), INTENT(IN) :: fname
         CHARACTER(len=*), INTENT(IN) :: ffreq
@@ -262,21 +353,27 @@
         CHARACTER(len=100) :: nffreq
         
-        !On crÃ©Ã© le noeud:
-        CALL xios_get_filegroup_handle("defile", x_fg)
-        CALL xios_add_file(x_fg, x_file, "X"//fname)
-        
-        !On reformate la frÃ©quence:
-        CALL reformadate(ffreq, nffreq)
-        
-        !On configure:
-        CALL xios_set_file_attr_hdl(x_file, name="X"//fname,&
+        !On regarde si le fichier n'est pas dÃ©fini par XML:
+        IF (.NOT.xios_is_valid_file(fname)) THEN
+            !On crÃ©Ã© le noeud:
+            CALL xios_get_filegroup_handle("defile", x_fg)
+            CALL xios_add_file(x_fg, x_file, fname)
+        
+            !On reformate la frÃ©quence:
+            CALL reformadate(ffreq, nffreq)
+        
+            !On configure:
+            CALL xios_set_file_attr_hdl(x_file, name="X"//fname,&
                 output_freq=TRIM(ADJUSTL(nffreq)), output_level=flvl, enabled=.TRUE.)
         
-        IF (xios_is_valid_file("X"//fname)) THEN
-            WRITE(*,*) "Xios. New file: ", "X"//fname
-            WRITE(*,*) "Xios. output_freq=",TRIM(ADJUSTL(nffreq)),"; output_lvl=",flvl
-        ELSE
-            WRITE(*,*) "Xios. Error, invalid file: ", "X"//fname
-            WRITE(*,*) "Xios. output_freq=",TRIM(ADJUSTL(nffreq)),"; output_lvl=",flvl
+            IF (xios_is_valid_file("X"//fname)) THEN
+                IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_file: New file: ", "X"//fname
+                IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_file: output_freq=",TRIM(ADJUSTL(nffreq)),"; output_lvl=",flvl
+            ELSE
+                WRITE(*,*) "wxios_add_file: Error, invalid file: ", "X"//trim(fname)
+                WRITE(*,*) "wxios_add_file: output_freq=",TRIM(ADJUSTL(nffreq)),"; output_lvl=",flvl
+            END IF
+        ELSE
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_file: File ",trim(fname), " dÃ©fined using XML."
+                CALL xios_set_file_attr(fname, enabled=.TRUE.)
         END IF
     END SUBROUTINE wxios_add_file
@@ -286,5 +383,8 @@
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     SUBROUTINE wxios_add_field(fieldname, fieldgroup, fieldlongname, fieldunit)
-        USE netcdf
+        USE netcdf, only: nf90_fill_real
+
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
         
         CHARACTER(len=*), INTENT(IN) :: fieldname
@@ -308,10 +408,10 @@
         !On ajoute le champ:
         CALL xios_add_field(fieldgroup, field, fieldname)
-        !WRITE(*,*) "Xios. ",fieldname,fieldgroup, fieldlongname, fieldunit
+        !IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field: ",fieldname,fieldgroup, fieldlongname, fieldunit
         
         !On rentre ses paramÃ¨tres:
         CALL xios_set_field_attr_hdl(field, standard_name=fieldlongname, unit=newunit, default_value=def)
-        WRITE(*,*) "Xios. Champ ", fieldname, "cree:"
-        WRITE(*,*) "Xios. long_name=",fieldlongname,"; unit=",newunit,";  default_value=",nf90_fill_real
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field: Field ",trim(fieldname), "cree:"
+        IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field: long_name=",trim(fieldlongname),"; unit=",trim(newunit),";  default_value=",nf90_fill_real
 
     END SUBROUTINE wxios_add_field
@@ -321,4 +421,7 @@
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     SUBROUTINE wxios_add_field_to_file(fieldname, fdim, fid, fname, fieldlongname, fieldunit, field_level, op)
+        IMPLICIT NONE
+        INCLUDE 'iniprint.h'
+
         CHARACTER(len=*), INTENT(IN) :: fieldname
         INTEGER, INTENT(IN)          :: fdim, fid
@@ -329,13 +432,15 @@
         CHARACTER(len=*), INTENT(IN) :: op
         
-        CHARACTER(len=100) :: axis_id
+        CHARACTER(len=20) :: axis_id
         CHARACTER(len=100) :: operation
         TYPE(xios_file) :: f
         TYPE(xios_field) :: field
         TYPE(xios_fieldgroup) :: fieldgroup
+        LOGICAL :: bool=.FALSE.
+        INTEGER :: lvl =0
         
         
         !PrÃ©paration du nom de l'axe:
-        CALL concat("presnivs", fid, axis_id)
+        CALL concat("presnivs", fname, axis_id)
         
         !on prÃ©pare le nom de l'opÃ©ration:
@@ -352,42 +457,63 @@
         
         !On regarde si le champ Ã  dÃ©jÃ  Ã©tÃ© crÃ©Ã© ou non:
-        IF (xios_is_valid_field(fieldname)) THEN
-            WRITE(*,*) "Xios. Champ ", fieldname, "existe"
-        ELSE
-            WRITE(*,*) "Xios. Champ ", fieldname, "nexiste pas"
+        IF (xios_is_valid_field(fieldname) .AND. .NOT. g_field_name == fieldname) THEN
+            !Si ce champ existe via XML (ie, dÃ¨s le premier passage, ie g_field_name != fieldname) alors rien d'autre Ã  faire
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field_to_file: Field ", trim(fieldname), "exists via XML"
+            g_flag_xml = .TRUE.
+            g_field_name = fieldname
+
+        ELSE IF (.NOT. g_field_name == fieldname) THEN
+            !Si premier pssage et champ indÃ©fini, alors on le crÃ©Ã©
+
+            IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field_to_file: Field ", trim(fieldname), "does not exist"
             
             !On le crÃ©Ã©:
             CALL wxios_add_field(fieldname,  fieldgroup, fieldlongname, fieldunit)
             IF (xios_is_valid_field(fieldname)) THEN
-                WRITE(*,*) "Xios. Champ ", fieldname, "cree"
+                IF (prt_level >= 10) WRITE(lunout,*) "wxios_add_field_to_file: Field ", trim(fieldname), "created"
             ENDIF
-        ENDIF
-        
-        !On ajoute le champ:
-        CALL xios_get_file_handle("X"//fname, f)
-        CALL xios_add_fieldtofile(f, field)
-        
-        
-        !L'operation, sa frequence:
-        CALL xios_set_field_attr_hdl(field, field_ref=fieldname, operation=TRIM(ADJUSTL(operation)), freq_op="1ts", prec=4)
-
-        
-        !On rentre ses paramÃ¨tres:
-        CALL xios_set_field_attr_hdl(field, level=field_level, enabled=.TRUE.)
-        
-        IF (fdim.EQ.2) THEN
-            !Si c'est un champ 2D:
-            WRITE(*,*) "Xios. Champ 2D ", fieldname, " de ", "X"//fname ," configure:"
-            WRITE (*,*) "Xios. op=", TRIM(ADJUSTL(operation))
-            WRITE(*,*) "Xios. freq_op=1ts","; lvl=",field_level
-        ELSE
-            !Si 3D :
-            !On ajoute l'axe vertical qui va bien:
-            CALL xios_set_field_attr_hdl(field, axis_ref=axis_id)
+
+            g_flag_xml = .FALSE.
+            g_field_name = fieldname
+
+        END IF
+
+        IF (.NOT. g_flag_xml) THEN
+            !Champ existe dÃ©jÃ , mais pas XML, alors on l'ajoute
+            !On ajoute le champ:
+            CALL xios_get_file_handle(fname, f)
+            CALL xios_add_fieldtofile(f, field)
             
-            WRITE(*,*) "Xios. Champ 3D ", fieldname, " de ", "X"//fname, "configure:"
-            WRITE(*,*) "Xios. freq_op=1ts","; lvl=",field_level
-            WRITE(*,*) "Xios. axe=",axis_id
-        END IF
+            
+            !L'operation, sa frequence:
+            CALL xios_set_field_attr_hdl(field, field_ref=fieldname, operation=TRIM(ADJUSTL(operation)), freq_op="1ts", prec=4)
+
+            
+            !On rentre ses paramÃ¨tres:
+            CALL xios_set_field_attr_hdl(field, level=field_level, enabled=.TRUE.)
+            
+            IF (fdim.EQ.2) THEN
+                !Si c'est un champ 2D:
+                IF (prt_level >= 10) THEN
+                  WRITE(lunout,*) "wxios_add_field_to_file: 2D Field ", trim(fieldname), " in ", "X"//trim(fname) ," configured with:"
+                  WRITE(lunout,*) "wxios_add_field_to_file: op=", TRIM(ADJUSTL(operation))
+                  WRITE(lunout,*) "wxios_add_field_to_file: freq_op=1ts","; lvl=",field_level
+                ENDIF
+            ELSE
+                !Si 3D :
+                !On ajoute l'axe vertical qui va bien:
+                CALL xios_set_field_attr_hdl(field, axis_ref=TRIM(ADJUSTL(axis_id)))
+                
+                IF (prt_level >= 10) THEN
+                  WRITE(lunout,*) "wxios_add_field_to_file: 3D Field",trim(fieldname), " in ", "X"//trim(fname), "configured with:"
+                  WRITE(lunout,*) "wxios_add_field_to_file: freq_op=1ts","; lvl=",field_level
+                  WRITE(lunout,*) "wxios_add_field_to_file: axis=",TRIM(ADJUSTL(axis_id))
+                ENDIF
+            END IF
+        
+        ELSE
+            !Sinon on se contente de l'activer:
+            CALL xios_set_field_attr(fieldname, enabled=.TRUE.)
+        ENDIF       
         
     END SUBROUTINE wxios_add_field_to_file
