Index: LMDZ5/trunk/libf/phylmd/iophy.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 2000)
+++ LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 2001)
@@ -568,4 +568,5 @@
     INCLUDE "temps.h"
     INCLUDE "clesphys.h"
+    INCLUDE "iniprint.h"
 
     INTEGER                          :: iff
@@ -602,6 +603,12 @@
 
 #ifdef CPP_XIOS
+      IF ( var%flag(iff)<=lev_files(iff) ) THEN
         CALL wxios_add_field_to_file(var%name, 2, iff, phys_out_filenames(iff), &
         var%description, var%unit, var%flag(iff), typeecrit)
+        IF (prt_level >= 10) THEN
+          WRITE(lunout,*) 'histdef2d: call wxios_add_field_to_file var%name iff: ', &
+                          trim(var%name),iff
+        ENDIF
+      ENDIF
 #endif
 #ifndef CPP_NO_IOIPSL 
@@ -628,4 +635,5 @@
     END IF
   END SUBROUTINE histdef2d
+
   SUBROUTINE histdef3d (iff,var)
 
@@ -645,4 +653,5 @@
     INCLUDE "temps.h"
     INCLUDE "clesphys.h"
+    INCLUDE "iniprint.h"
 
     INTEGER                          :: iff
@@ -679,6 +688,12 @@
 
 #ifdef CPP_XIOS
+      IF ( var%flag(iff)<=lev_files(iff) ) THEN
         CALL wxios_add_field_to_file(var%name, 3, iff, phys_out_filenames(iff), &
         var%description, var%unit, var%flag(iff), typeecrit)
+        IF (prt_level >= 10) THEN
+          WRITE(lunout,*) 'histdef3d: call wxios_add_field_to_file var%name iff: ', &
+                          trim(var%name),iff
+        ENDIF
+      ENDIF
 #endif
 #ifndef CPP_NO_IOIPSL 
@@ -881,5 +896,7 @@
       
     INTEGER :: iff, iff_beg, iff_end
-      
+    LOGICAL, SAVE  :: firstx
+!$OMP THREADPRIVATE(firstx)
+
     REAL,DIMENSION(klon_mpi) :: buffer_omp
     INTEGER, allocatable, DIMENSION(:) :: index2d
@@ -889,6 +906,7 @@
     REAL, ALLOCATABLE, DIMENSION(:) :: fieldok
 
-    IF (prt_level >= 10) WRITE(lunout,*)'Begin histwrite2d_phy ',trim(var%name)
-
+    IF (prt_level >= 10) THEN
+      WRITE(lunout,*)'Begin histwrite2d_phy for ',trim(var%name)
+    ENDIF
 ! ug RUSTINE POUR LES STD LEVS.....
       IF (PRESENT(STD_iff)) THEN
@@ -925,7 +943,20 @@
 
 ! La boucle sur les fichiers:
+      firstx=.true.
       DO iff=iff_beg, iff_end
             IF (var%flag(iff) <= lev_files(iff) .AND. clef_files(iff)) THEN 
-    
+
+#ifdef CPP_XIOS
+               IF (firstx) THEN
+                  if (prt_level >= 10) then
+                     write(lunout,*)'Dans iophy histwrite2D,iff,var%name ',&
+                                    iff,trim(var%name)                       
+                     write(lunout,*)"histwrite2d_phy:.NOT.clef_stations(iff)and iff==iff_beg, call wxios_write_2D"
+                  endif
+                  CALL wxios_write_2D(var%name, Field2d)
+                  firstx=.false.
+               ENDIF
+#endif
+
                   IF(.NOT.clef_stations(iff)) THEN
                         ALLOCATE(index2d(iim*jj_nb))
@@ -934,12 +965,12 @@
                         CALL histwrite(nid_files(iff),var%name,itau_iophy,Field2d,iim*jj_nb,index2d)
 #endif
-#ifdef CPP_XIOS
-                        IF (iff == iff_beg) THEN
-                          if (prt_level >= 10) then
-                            write(lunout,*)"histwrite2d_phy: .NOT.clef_stations(iff) and iff==iff_beg, call wxios_write_2D"
-                          endif
-                          CALL wxios_write_2D(var%name, Field2d)
-                        ENDIF
-#endif
+!#ifdef CPP_XIOS
+!                        IF (iff == iff_beg) THEN
+!                          if (prt_level >= 10) then
+!                            write(lunout,*)"histwrite2d_phy: .NOT.clef_stations(iff) and iff==iff_beg, call wxios_write_2D"
+!                          endif
+!                          CALL wxios_write_2D(var%name, Field2d)
+!                        ENDIF
+!#endif
                   ELSE 
                         ALLOCATE(fieldok(npstn))
@@ -1001,8 +1032,9 @@
       
     INTEGER :: iff, iff_beg, iff_end
-
+    LOGICAL, SAVE  :: firstx
+!$OMP THREADPRIVATE(firstx)
     REAL,DIMENSION(klon_mpi,SIZE(field,2)) :: buffer_omp
     REAL :: Field3d(iim,jj_nb,SIZE(field,2))
-    INTEGER :: ip, n, nlev
+    INTEGER :: ip, n, nlev, nlevx
     INTEGER, ALLOCATABLE, DIMENSION(:) :: index3d
     REAL,ALLOCATABLE, DIMENSION(:,:) :: fieldok
@@ -1033,5 +1065,9 @@
     IF (SIZE(field,1)/=klon) CALL abort_gcm('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
     nlev=SIZE(field,2)
-
+    if (nlev.eq.klev+1) then
+        nlevx=klev
+    else
+        nlevx=nlev
+    endif
 
     CALL Gather_omp(field,buffer_omp)
@@ -1041,6 +1077,20 @@
 
 ! BOUCLE SUR LES FICHIERS
+     firstx=.true.
      DO iff=iff_beg, iff_end
             IF (var%flag(iff) <= lev_files(iff) .AND. clef_files(iff)) THEN
+#ifdef CPP_XIOS
+              IF (firstx) THEN
+                if (prt_level >= 10) then
+                  write(lunout,*)'Dans iophy, histwrite3D iff nlev klev firstx', &
+                                  iff,nlev,klev, firstx                       
+                  write(lunout,*)'histwrite3d_phy: call wxios_write_3D for ', &
+                                  trim(var%name), ' with iim jjm nlevx = ', &
+                                  iim,jj_nb,nlevx
+                endif
+                CALL wxios_write_3D(var%name, Field3d(:,:,1:nlevx))
+                            firstx=.false.
+              ENDIF
+#endif
                 IF (.NOT.clef_stations(iff)) THEN
                         ALLOCATE(index3d(iim*jj_nb*nlev))
@@ -1051,10 +1101,10 @@
 #endif
 
-#ifdef CPP_XIOS
-                        IF (iff == 1) THEN
-                              CALL wxios_write_3D(var%name, Field3d(:,:,1:klev))
-                        ENDIF
-#endif
-                        
+!#ifdef CPP_XIOS
+!                        IF (iff == 1) THEN
+!                              CALL wxios_write_3D(var%name, Field3d(:,:,1:klev))
+!                        ENDIF
+!#endif
+!                        
                 ELSE 
                         nlev=size(field,2)
@@ -1204,5 +1254,5 @@
         ALLOCATE(index3d(iim*jj_nb*nlev))
         ALLOCATE(fieldok(iim*jj_nb,nlev))
-        CALL wxios_write_3D(field_name, Field3d(:,:,1:klev))
+        CALL wxios_write_3D(field_name, Field3d(:,:,1:nlev))
                         
     ELSE 
Index: LMDZ5/trunk/libf/phylmd/phys_output_mod.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/phys_output_mod.F90	(revision 2000)
+++ LMDZ5/trunk/libf/phylmd/phys_output_mod.F90	(revision 2001)
@@ -306,9 +306,13 @@
 #ifdef CPP_XIOS
 !!! Ouverture de chaque fichier XIOS !!!!!!!!!!!
+    if (prt_level >= 10) then
+      print*,'phys_output_open: call wxios_add_file with phys_out_filenames(iff)=',trim(phys_out_filenames(iff))                                                                        
+    endif
     CALL wxios_add_file(phys_out_filenames(iff),chtimestep(iff),lev_files(iff))  
-    print*,'wxios_add_file phys_out_filenames(iff)',phys_out_filenames(iff)                                                                        
 
 !!! Declaration des axes verticaux de chaque fichier:
-    print*,'Declaration des axes verticaux de chaque fichier '
+    if (prt_level >= 10) then
+      print*,'phys_output_open: Declare vertical axes for each file'
+    endif
    if (iff.le.6) then
     CALL wxios_add_vaxis("presnivs", phys_out_filenames(iff), &
@@ -318,7 +322,8 @@
     CALL wxios_add_vaxis("Bhyb", phys_out_filenames(iff), &
             levmax(iff) - levmin(iff) + 1, Bhyb)
-    CALL wxios_add_vaxis("Ahyb", phys_out_filenames(iff), &
+    CALL wxios_add_vaxis("Alt", phys_out_filenames(iff), &
             levmax(iff) - levmin(iff) + 1, Alt)
    else
+    ! NMC files
     CALL wxios_add_vaxis("plev", phys_out_filenames(iff), &
             levmax(iff) - levmin(iff) + 1, rlevSTD(levmin(iff):levmax(iff)))
@@ -362,6 +367,8 @@
           else IF (clef_stations(iff)) THEN
 
-             WRITE(lunout,*)'phys_output_mod phys_out_filenames=',phys_out_filenames(iff)
-
+             if (prt_level >= 10) then
+             WRITE(lunout,*)'phys_output_open: iff=',iff,'  phys_out_filenames(iff)=',phys_out_filenames(iff)
+             endif
+             
              CALL histbeg_phy_all(rlon,rlat,pim,tabij,ipt,jpt,plon,plat,plon_bounds,plat_bounds, &
                   phys_out_filenames(iff), &
@@ -491,6 +498,8 @@
     ecrit_ins = ecrit_files(6)
 
-    WRITE(lunout,*)'swaero_diag=',swaero_diag
-    WRITE(lunout,*)'Fin phys_output_mod.F90'
+    if (prt_level >= 10) then
+      WRITE(lunout,*)'swaero_diag=',swaero_diag
+      WRITE(lunout,*)'phys_output_open: ends here'
+    endif
 
   end SUBROUTINE phys_output_open
