Index: LMDZ5/trunk/libf/cosp/cosp_output_mod.F90
===================================================================
--- LMDZ5/trunk/libf/cosp/cosp_output_mod.F90	(revision 2080)
+++ LMDZ5/trunk/libf/cosp/cosp_output_mod.F90	(revision 2137)
@@ -17,5 +17,5 @@
       INTEGER, DIMENSION(3), SAVE  :: nhoricosp, nvert, nvertmcosp, nvertcol, nvertisccp, nvertp
       REAL, DIMENSION(3), SAVE                :: zoutm_cosp
-!$OMP THREADPRIVATE(nhori, nvert, nvertmcosp, nvertcol, nvertisccp, nvertp, zoutm_cosp)
+!$OMP THREADPRIVATE(nhoricosp, nvert, nvertmcosp, nvertcol, nvertisccp, nvertp, zoutm_cosp)
       REAL, SAVE                   :: zdtimemoy_cosp
 !$OMP THREADPRIVATE(zdtimemoy_cosp) 
@@ -78,6 +78,5 @@
            by the ISCCP Simulator","K", (/ ('', i=1, 3) /))
 
-!   LOGICAL, SAVE :: cosp_varsdefined = .FALSE. ! ug PAS THREADPRIVATE ET C'EST NORMAL
-  LOGICAL, SAVE        :: cosp_varsdefined
+   LOGICAL, SAVE :: cosp_varsdefined = .FALSE. ! ug PAS THREADPRIVATE ET C'EST NORMAL
 
 CONTAINS
@@ -89,5 +88,5 @@
 
   SUBROUTINE cosp_output_open(Nlevlmdz, Ncolumns, presnivs, dtime, freq_cosp, &
-                              ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, &
+                              ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, ok_all_xml,  &
                               ecrit_mth, ecrit_day, ecrit_hf, use_vgrid, vgrid)
 
@@ -108,11 +107,12 @@
   real,dimension(Nlevlmdz) :: presnivs
   real                     :: dtime, freq_cosp, ecrit_day, ecrit_hf, ecrit_mth 
-  logical                  :: ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, use_vgrid                    
+  logical                  :: ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, use_vgrid, ok_all_xml                    
   type(cosp_vgrid)   :: vgrid   ! Information on vertical grid of stats
 
 !!! Variables locales
   integer                  :: idayref, iff, ii
-  real                     :: zjulian
+  real                     :: zjulian,zjulian_start
   real,dimension(Ncolumns) :: column_ax
+  CHARACTER(LEN=20), DIMENSION(3)  :: chfreq = (/ '1day', '1d', '3h' /)            
 
 !!! Variables d'entree
@@ -124,7 +124,10 @@
     INTEGER :: x_an, x_mois, x_jour
     REAL :: x_heure
+    INTEGER :: ini_an, ini_mois, ini_jour
+    REAL :: ini_heure
 #endif
 
     WRITE(lunout,*) 'Debut cosp_output_mod.F90'
+    print*,'cosp_varsdefined',cosp_varsdefined
     ! Initialisations (Valeurs par defaut)
 
@@ -154,18 +157,38 @@
     CALL getin('cosp_outfilenames',cosp_outfilenames)
     CALL getin('cosp_outfilekeys',cosp_outfilekeys)
-    CALL getin('cosp_outfiletimesteps',cosp_ecritfiles)
+    CALL getin('cosp_ecritfiles',cosp_ecritfiles)
     CALL getin('cosp_outfiletypes',cosp_outfiletypes)
 
     WRITE(lunout,*)'cosp_outfilenames=',cosp_outfilenames
     WRITE(lunout,*)'cosp_outfilekeys=',cosp_outfilekeys
-    WRITE(lunout,*)'cosp_outfiletimesteps=',cosp_ecritfiles
+    WRITE(lunout,*)'cosp_ecritfiles=',cosp_ecritfiles
     WRITE(lunout,*)'cosp_outfiletypes=',cosp_outfiletypes
- 
-#ifdef CPP_XIOS
-    ! ug Réglage du calendrier xios
+    
+    idayref = day_ref
+    CALL ymds2ju(annee_ref, 1, idayref, 0.0, zjulian)
+    CALL ymds2ju(annee_ref, 1, day_ini, start_time, zjulian_start)
+
+#ifdef CPP_XIOS
+    ! ug R\'eglage du calendrier xios
     !Temps julian => an, mois, jour, heure
-    CALL ymds2ju(annee_ref, 1, day_ref, 0.0, zjulian)
     CALL ju2ymds(zjulian, x_an, x_mois, x_jour, x_heure)
-    CALL wxios_set_cal(dtime, calend, x_an, x_mois, x_jour, x_heure)
+    CALL ju2ymds(zjulian_start, ini_an, ini_mois, ini_jour, ini_heure)
+    CALL wxios_set_cal(dtime, calend, x_an, x_mois, x_jour, x_heure, ini_an, &
+                       ini_mois, ini_jour, ini_heure )
+       ! ug dï¿½claration des axes verticaux de chaque fichier:
+    if (use_vgrid) then
+        CALL wxios_add_vaxis("height", vgrid%Nlvgrid, vgrid%z)
+    else
+         WRITE(lunout,*) 'wxios_add_vaxis "presnivs", vgrid%Nlvgrid ',vgrid%Nlvgrid
+        CALL wxios_add_vaxis("presnivs", vgrid%Nlvgrid, presnivs)
+    endif
+    WRITE(lunout,*) 'wxios_add_vaxis height_mlev, Nlevlmdz ',Nlevlmdz
+    CALL wxios_add_vaxis("height_mlev", Nlevlmdz, vgrid%mz)
+    WRITE(lunout,*) 'wxios_add_vaxis sza, PARASOL_NREFL ',PARASOL_NREFL
+    CALL wxios_add_vaxis("sza", PARASOL_NREFL, PARASOL_SZA)
+    WRITE(lunout,*) 'wxios_add_vaxis pressure2 ',7
+    CALL wxios_add_vaxis("pressure2", 7, ISCCP_PC)
+    WRITE(lunout,*) 'wxios_add_vaxis column ',Ncolumns
+    CALL wxios_add_vaxis("column", Ncolumns, column_ax)
 #endif
    
@@ -176,9 +199,17 @@
 
        IF (cosp_outfilekeys(iff)) THEN
-           idayref = day_ref
-           CALL ymds2ju(annee_ref, 1, idayref, 0., zjulian) 
-           CALL histbeg_phy(cosp_outfilenames(iff),itau_phy,zjulian,&
+           CALL histbeg_phy_all(cosp_outfilenames(iff),itau_phy,zjulian,&
              dtime,nhoricosp(iff),cosp_nidfiles(iff))
-
+           print*,'histbeg_phy nhoricosp(iff),cosp_nidfiles(iff)', &
+                    nhoricosp(iff),cosp_nidfiles(iff)
+
+#ifdef CPP_XIOS
+        IF (.not. ok_all_xml) then
+         WRITE(lunout,*) 'wxios_add_file ',cosp_outfilenames(iff)
+         CALL wxios_add_file(cosp_outfilenames(iff),chfreq(iff),10)
+        ENDIF
+#endif
+
+#ifndef CPP_IOIPSL_NO_OUTPUT 
 ! Definition de l'axe vertical
        if (use_vgrid) then
@@ -197,16 +228,4 @@
 
       CALL histvert(cosp_nidfiles(iff),"column","column","count",Ncolumns,column_ax(1:Ncolumns),nvertcol(iff))
-
-#ifdef CPP_XIOS
-    ! ug déclaration des axes verticaux de chaque fichier:
-    if (use_vgrid) then
-      CALL wxios_add_vaxis("height", cosp_outfilenames(iff), vgrid%Nlvgrid, vgrid%z)
-    else
-      CALL wxios_add_vaxis("presnivs", cosp_outfilenames(iff), vgrid%Nlvgrid, presnivs)
-    endif
-    CALL wxios_add_vaxis("height_mlev", cosp_outfilenames(iff), Nlevlmdz, vgrid%mz)
-    CALL wxios_add_vaxis("sza", cosp_outfilenames(iff), PARASOL_NREFL, PARASOL_SZA)
-    CALL wxios_add_vaxis("pressure2", cosp_outfilenames(iff), 7, ISCCP_PC)
-    CALL wxios_add_vaxis("column", cosp_outfilenames(iff), Ncolumns, column_ax)
 #endif
 
Index: LMDZ5/trunk/libf/cosp/cosp_output_write_mod.F90
===================================================================
--- LMDZ5/trunk/libf/cosp/cosp_output_write_mod.F90	(revision 2080)
+++ LMDZ5/trunk/libf/cosp/cosp_output_write_mod.F90	(revision 2137)
@@ -17,5 +17,6 @@
    CONTAINS
 
-  SUBROUTINE cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, cfg, gbx, vgrid, sglidar, stlidar, isccp)
+  SUBROUTINE cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, &
+                               cfg, gbx, vgrid, sglidar, stlidar, isccp)
 
     USE ioipsl
@@ -23,6 +24,6 @@
 
 #ifdef CPP_XIOS
-    ! ug Pour les sorties XIOS
-    USE wxios
+    USE wxios, only: wxios_closedef
+    USE xios, only: xios_update_calendar
 #endif
 
@@ -38,5 +39,5 @@
 
 !!! Variables locales
-  integer               :: icl, iinitend=1
+  integer               :: icl
   logical               :: ok_sync
   integer               :: itau_wcosp
@@ -44,35 +45,46 @@
 
   include "temps.h"
+  include "iniprint.h"
 
   Nlevout = vgrid%Nlvgrid
   Ncolout = Ncolumns
 
-  IF (MOD(itap,NINT(freq_COSP/dtime)).EQ.0) THEN
-       
 ! A refaire
        itau_wcosp = itau_phy + itap + start_time * day_step / iphysiq
+        if (prt_level >= 10) then
+             WRITE(lunout,*)'itau_wcosp, itap, start_time, day_step, iphysiq =', & 
+                             itau_wcosp, itap, start_time, day_step, iphysiq
+        endif
 
 ! On le donne a  cosp_output_write_mod pour que les histwrite y aient acces:
        CALL set_itau_iocosp(itau_wcosp)
+        if (prt_level >= 10) then
+              WRITE(lunout,*)'itau_iocosp =',itau_iocosp
+        endif
 
     ok_sync = .TRUE.
     
-    IF(.NOT.cosp_varsdefined) THEN
-        iinitend = 2
-    ELSE
-        iinitend = 1
-    ENDIF
-
-! ug la boucle qui suit ne sert qu'une fois, pour l'initialisation, sinon il n'y a toujours qu'un seul passage:
-DO iinit=1, iinitend
-#ifdef CPP_XIOS
+!DO iinit=1, iinitend
+! AI sept 2014 cette boucle supprimee
+! On n'ecrit pas quand itap=1 (cosp)
+
+   if (prt_level >= 10) then
+         WRITE(lunout,*)'DO iinit=1, iinitend ',iinitend
+   endif
+
+#ifdef CPP_XIOS
+ !$OMP MASTER
 IF (cosp_varsdefined) THEN
-     CALL wxios_update_calendar(itau_iocosp)
-END IF
+   if (prt_level >= 10) then
+         WRITE(lunout,*)'Apell xios_update_calendar cosp_varsdefined iinitend ', &
+                         cosp_varsdefined,iinitend
+   endif 
+    CALL xios_update_calendar(itau_wcosp)
+ENDIF
+  !$OMP END MASTER
+  !$OMP BARRIER
 #endif
 
  if (cfg%Llidar_sim) then
-! print*,'cfg%Llidar_sim dans output_write',cfg%Llidar_sim
-! print*,'Nlevout Npoints dans output_write, R_UNDEF =',Nlevout,Npoints,R_UNDEF
 ! Pb des valeurs indefinies, on les met a 0
 ! A refaire proprement
@@ -116,4 +128,5 @@
    enddo
 
+   print*,'Appel histwrite2d_cosp'
    CALL histwrite2d_cosp(o_cllcalipso,stlidar%cldlayer(:,1))
    CALL histwrite2d_cosp(o_clhcalipso,stlidar%cldlayer(:,3))
@@ -208,4 +221,5 @@
  IF(.NOT.cosp_varsdefined) THEN
 !$OMP MASTER
+#ifndef CPP_IOIPSL_NO_OUTPUT
             DO iff=1,3
                 IF (cosp_outfilekeys(iff)) THEN
@@ -213,25 +227,28 @@
                 ENDIF ! cosp_outfilekeys
             ENDDO !  iff
-#ifdef CPP_XIOS
+#endif
+! Fermeture dans phys_output_write
+!#ifdef CPP_XIOS
             !On finalise l'initialisation:
-            CALL wxios_closedef()
-#endif
+            !CALL wxios_closedef()
+!#endif
+
 !$OMP END MASTER
 !$OMP BARRIER
             cosp_varsdefined = .TRUE.
  END IF
-END DO  !! iinit
-
-!    IF(cosp_varsdefined) THEN
+
+    IF(cosp_varsdefined) THEN
 ! On synchronise les fichiers pour IOIPSL
+#ifndef CPP_IOIPSL_NO_OUTPUT 
 !$OMP MASTER
-   DO iff=1,3
+     DO iff=1,3
          IF (ok_sync .AND. cosp_outfilekeys(iff)) THEN
              CALL histsync(cosp_nidfiles(iff))
          ENDIF
-   END DO
+     END DO
 !$OMP END MASTER
-
-   ENDIF ! if freq_COSP
+#endif
+    ENDIF  !cosp_varsdefined
 
     END SUBROUTINE cosp_output_write
@@ -258,4 +275,6 @@
     INCLUDE "dimensions.h"
     INCLUDE "temps.h"
+    INCLUDE "clesphys.h"
+    include "iniprint.h"
 
     INTEGER                          :: iff
@@ -286,7 +305,16 @@
 
 #ifdef CPP_XIOS
+     IF (.not. ok_all_xml) then
+       IF ( var%cles(iff) ) THEN
+         if (prt_level >= 10) then
+              WRITE(lunout,*)'Appel wxios_add_field_to_file var%name =',var%name 
+         endif
         CALL wxios_add_field_to_file(var%name, 2, cosp_nidfiles(iff), cosp_outfilenames(iff), &
-        var%description, var%unit, 1, typeecrit)
-#endif
+                                     var%description, var%unit, 1, typeecrit)
+       ENDIF
+     ENDIF
+#endif
+
+#ifndef CPP_IOIPSL_NO_OUTPUT 
        IF ( var%cles(iff) ) THEN
           CALL histdef (cosp_nidfiles(iff), var%name, var%description, var%unit, &
@@ -294,4 +322,5 @@
                typeecrit, zstophym,zoutm_cosp(iff))
        ENDIF
+#endif
 
   END SUBROUTINE histdef2d_cosp
@@ -312,4 +341,6 @@
     INCLUDE "dimensions.h"
     INCLUDE "temps.h"
+    INCLUDE "clesphys.h"
+    include "iniprint.h"
 
     INTEGER                        :: iff, klevs
@@ -322,17 +353,21 @@
     CHARACTER(LEN=20) :: nom
     character(len=2) :: str2
+    CHARACTER(len=20) :: nam_axvert
 
 ! Axe vertical
       IF (nvertsave.eq.nvertp(iff)) THEN
           klevs=PARASOL_NREFL
+          nam_axvert="sza"
       ELSE IF (nvertsave.eq.nvertisccp(iff)) THEN
           klevs=7
+          nam_axvert="pressure2"
       ELSE IF (nvertsave.eq.nvertcol(iff)) THEN
           klevs=Ncolout
+          nam_axvert="column"
       ELSE
-          klevs=Nlevout
+           klevs=Nlevout
+           nam_axvert="presnivs"
       ENDIF
-      
-         
+
 ! ug RUSTINE POUR LES Champs 4D
       IF (PRESENT(ncols)) THEN
@@ -365,7 +400,16 @@
 
 #ifdef CPP_XIOS
-        CALL wxios_add_field_to_file(nom, 3, cosp_nidfiles(iff), cosp_outfilenames(iff), &
-        var%description, var%unit, 1, typeecrit)
-#endif
+      IF (.not. ok_all_xml) then
+        IF ( var%cles(iff) ) THEN
+          if (prt_level >= 10) then
+              WRITE(lunout,*)'Appel wxios_add_field_to_file 3d nom variable nam_axvert = ',nom, nam_axvert 
+          endif
+          CALL wxios_add_field_to_file(nom, 3, cosp_nidfiles(iff), cosp_outfilenames(iff), &
+                                       var%description, var%unit, 1, typeecrit, nam_axvert)
+        ENDIF
+      ENDIF
+#endif
+
+#ifndef CPP_IOIPSL_NO_OUTPUT
        IF ( var%cles(iff) ) THEN
           CALL histdef (cosp_nidfiles(iff), nom, var%description, var%unit, &
@@ -374,4 +418,6 @@
                zstophym, zoutm_cosp(iff))
        ENDIF
+#endif
+
   END SUBROUTINE histdef3d_cosp
 
@@ -383,5 +429,5 @@
 
 #ifdef CPP_XIOS
-  USE wxios
+  USE xios, only: xios_send_field
 #endif
 
@@ -389,4 +435,5 @@
   INCLUDE 'dimensions.h'
   INCLUDE 'iniprint.h'
+  INCLUDE 'clesphys.h'
 
     TYPE(ctrl_outcosp), INTENT(IN) :: var
@@ -400,4 +447,6 @@
     CHARACTER(LEN=20) ::  nomi, nom
     character(len=2) :: str2
+    LOGICAL, SAVE  :: firstx
+!$OMP THREADPRIVATE(firstx)
 
     IF (prt_level >= 9) WRITE(lunout,*)'Begin histrwrite2d ',var%name
@@ -424,20 +473,38 @@
 
 ! La boucle sur les fichiers:
+      firstx=.true.
       DO iff=1, 3
            IF (var%cles(iff) .AND. cosp_outfilekeys(iff)) THEN
                 ALLOCATE(index2d(iim*jj_nb))
+#ifndef CPP_IOIPSL_NO_OUTPUT
         CALL histwrite(cosp_nidfiles(iff),var%name,itau_iocosp,Field2d,iim*jj_nb,index2d) 
-#ifdef CPP_XIOS
-                IF (iff == 1) THEN
-                   CALL wxios_write_2D(var%name, Field2d)
-                ENDIF
-#endif
-
+#endif
                 deallocate(index2d)
-           ENDIF !levfiles
-      ENDDO
+#ifdef CPP_XIOS
+              IF (.not. ok_all_xml) then
+                 if (firstx) then
+                  if (prt_level >= 10) then
+                    WRITE(lunout,*)'xios_send_field variable ',var%name
+                  endif
+                  CALL xios_send_field(var%name, Field2d)
+                   firstx=.false.
+                 endif
+              ENDIF
+#endif
+           ENDIF
+      ENDDO 
+
+#ifdef CPP_XIOS
+      IF (ok_all_xml) THEN
+        if (prt_level >= 10) then
+              WRITE(lunout,*)'xios_send_field variable ',var%name
+        endif
+       CALL xios_send_field(var%name, Field2d)
+      ENDIF
+#endif
+
 !$OMP END MASTER   
   ENDIF ! vars_defined
-  IF (prt_level >= 9) WRITE(lunout,*)'End histrwrite2d_cosp ',nom
+  IF (prt_level >= 9) WRITE(lunout,*)'End histrwrite2d_cosp ',var%name
   END SUBROUTINE histwrite2d_cosp
 
@@ -451,5 +518,5 @@
 
 #ifdef CPP_XIOS
- USE WXIOS
+  USE xios, only: xios_send_field
 #endif
 
@@ -458,4 +525,5 @@
   INCLUDE 'dimensions.h'
   INCLUDE 'iniprint.h'
+  INCLUDE 'clesphys.h'
 
     TYPE(ctrl_outcosp), INTENT(IN)    :: var
@@ -472,4 +540,6 @@
     CHARACTER(LEN=20) ::  nomi, nom
     character(len=2) :: str2
+    LOGICAL, SAVE  :: firstx
+!$OMP THREADPRIVATE(firstx)
 
   IF (prt_level >= 9) write(lunout,*)'Begin histrwrite3d ',var%name
@@ -505,19 +575,32 @@
     CALL grid1Dto2D_mpi(buffer_omp,field3d)
 
-
 ! BOUCLE SUR LES FICHIERS
+     firstx=.true.
      DO iff=1, 3
         IF (var%cles(iff) .AND. cosp_outfilekeys(iff)) THEN
            ALLOCATE(index3d(iim*jj_nb*nlev))
+#ifndef CPP_IOIPSL_NO_OUTPUT
     CALL histwrite(cosp_nidfiles(iff),nom,itau_iocosp,Field3d,iim*jj_nb*nlev,index3d) 
-
-#ifdef CPP_XIOS
-           IF (iff == 1) THEN
-               CALL wxios_write_3D(nom, Field3d(:,:,1:klev))
+#endif
+
+#ifdef CPP_XIOS
+          IF (.not. ok_all_xml) then
+           IF (firstx) THEN
+               CALL xios_send_field(nom, Field3d(:,:,1:nlev))
+               IF (prt_level >= 9) WRITE(lunout,*)'xios_send_field ',var%name
+               firstx=.FALSE.
            ENDIF
+          ENDIF
 #endif
          deallocate(index3d)
         ENDIF
       ENDDO
+#ifdef CPP_XIOS
+    IF (ok_all_xml) THEN
+     CALL xios_send_field(nom, Field3d(:,:,1:nlev))
+     IF (prt_level >= 9) WRITE(lunout,*)'xios_send_field ',var%name
+    ENDIF
+#endif
+
 !$OMP END MASTER   
   ENDIF ! vars_defined
Index: LMDZ5/trunk/libf/cosp/phys_cosp.F90
===================================================================
--- LMDZ5/trunk/libf/cosp/phys_cosp.F90	(revision 2080)
+++ LMDZ5/trunk/libf/cosp/phys_cosp.F90	(revision 2137)
@@ -6,5 +6,5 @@
   subroutine phys_cosp( itap,dtime,freq_cosp, &
                         ok_mensuelCOSP,ok_journeCOSP,ok_hfCOSP, &
-                        ecrit_mth,ecrit_day,ecrit_hf, &
+                        ecrit_mth,ecrit_day,ecrit_hf, ok_all_xml, &
                         Nptslmdz,Nlevlmdz,lon,lat, presnivs,overlaplmdz,sunlit, &
                         ref_liq,ref_ice,fracTerLic,u_wind,v_wind,phis,phi,ph,p,skt,t, &
@@ -119,5 +119,5 @@
   integer :: ii
   real    :: ecrit_day,ecrit_hf,ecrit_mth
-  logical :: ok_mensuelCOSP,ok_journeCOSP,ok_hfCOSP
+  logical :: ok_mensuelCOSP,ok_journeCOSP,ok_hfCOSP, ok_all_xml
 
   logical, save :: debut_cosp=.true.
@@ -298,5 +298,5 @@
         print *, ' Open outpts files and define axis'
         call cosp_output_open(Nlevlmdz, Ncolumns, presnivs, dtime, freq_cosp, &
-                              ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, &
+                              ok_mensuelCOSP, ok_journeCOSP, ok_hfCOSP, ok_all_xml, &
                               ecrit_mth, ecrit_day, ecrit_hf, use_vgrid, vgrid)
       !$OMP END MASTER
@@ -313,5 +313,6 @@
 !!!!!!!!!!!!!!!!!! Ecreture des sorties Cosp !!!!!!!!!!!!!!r!!!!!!:!!!!!
        print *, 'Calling write output'
-        call cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, cfg, gbx, vgrid, sglidar, stlidar, isccp)
+        call cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, &
+                               cfg, gbx, vgrid, sglidar, stlidar, isccp)
 
 !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
