Index: LMDZ5/branches/testing/libf/cosp/cosp_output_write_mod.F90
===================================================================
--- LMDZ5/branches/testing/libf/cosp/cosp_output_write_mod.F90	(revision 1999)
+++ LMDZ5/branches/testing/libf/cosp/cosp_output_write_mod.F90	(revision 2160)
@@ -8,4 +8,6 @@
    INTEGER, SAVE  :: itau_iocosp
 !$OMP THREADPRIVATE(itau_iocosp)
+   INTEGER, save        :: Nlevout, Ncolout
+!$OMP THREADPRIVATE(Nlevout, Ncolout)
 
 !  INTERFACE histwrite_cosp
@@ -15,5 +17,6 @@
    CONTAINS
 
-  SUBROUTINE cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, cfg, gbx, sglidar, stlidar, isccp)
+  SUBROUTINE cosp_output_write(Nlevlmdz, Npoints, Ncolumns, itap, dtime, freq_COSP, &
+                               cfg, gbx, vgrid, sglidar, stlidar, isccp)
 
     USE ioipsl
@@ -21,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
 
@@ -33,7 +36,8 @@
   type(cosp_isccp)      :: isccp   ! Output from ISCCP simulator
   type(cosp_lidarstats) :: stlidar ! Summary statistics from lidar simulator
+  type(cosp_vgrid)      :: vgrid   ! Information on vertical grid of stats
 
 !!! Variables locales
-  integer               :: icl, iinitend=1
+  integer               :: icl
   logical               :: ok_sync
   integer               :: itau_wcosp
@@ -41,31 +45,46 @@
 
   include "temps.h"
-
-  IF (MOD(itap,NINT(freq_COSP/dtime)).EQ.0) THEN
-       
+  include "iniprint.h"
+
+  Nlevout = vgrid%Nlvgrid
+  Ncolout = Ncolumns
+
 ! 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
-
 ! Pb des valeurs indefinies, on les met a 0
 ! A refaire proprement
@@ -109,4 +128,5 @@
    enddo
 
+   print*,'Appel histwrite2d_cosp'
    CALL histwrite2d_cosp(o_cllcalipso,stlidar%cldlayer(:,1))
    CALL histwrite2d_cosp(o_clhcalipso,stlidar%cldlayer(:,3))
@@ -201,4 +221,5 @@
  IF(.NOT.cosp_varsdefined) THEN
 !$OMP MASTER
+#ifndef CPP_IOIPSL_NO_OUTPUT
             DO iff=1,3
                 IF (cosp_outfilekeys(iff)) THEN
@@ -206,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
@@ -251,4 +275,6 @@
     INCLUDE "dimensions.h"
     INCLUDE "temps.h"
+    INCLUDE "clesphys.h"
+    include "iniprint.h"
 
     INTEGER                          :: iff
@@ -279,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, &
@@ -287,4 +322,5 @@
                typeecrit, zstophym,zoutm_cosp(iff))
        ENDIF
+#endif
 
   END SUBROUTINE histdef2d_cosp
@@ -305,4 +341,6 @@
     INCLUDE "dimensions.h"
     INCLUDE "temps.h"
+    INCLUDE "clesphys.h"
+    include "iniprint.h"
 
     INTEGER                        :: iff, klevs
@@ -315,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
@@ -358,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, &
@@ -367,4 +418,6 @@
                zstophym, zoutm_cosp(iff))
        ENDIF
+#endif
+
   END SUBROUTINE histdef3d_cosp
 
@@ -376,5 +429,5 @@
 
 #ifdef CPP_XIOS
-  USE wxios
+  USE xios, only: xios_send_field
 #endif
 
@@ -382,4 +435,5 @@
   INCLUDE 'dimensions.h'
   INCLUDE 'iniprint.h'
+  INCLUDE 'clesphys.h'
 
     TYPE(ctrl_outcosp), INTENT(IN) :: var
@@ -393,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
@@ -417,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
 
@@ -444,5 +518,5 @@
 
 #ifdef CPP_XIOS
- USE WXIOS
+  USE xios, only: xios_send_field
 #endif
 
@@ -451,4 +525,5 @@
   INCLUDE 'dimensions.h'
   INCLUDE 'iniprint.h'
+  INCLUDE 'clesphys.h'
 
     TYPE(ctrl_outcosp), INTENT(IN)    :: var
@@ -465,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
@@ -498,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
