Index: LMDZ5/trunk/libf/phylmd/iophy.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 2989)
+++ LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 3003)
@@ -921,5 +921,5 @@
 ! ug NOUVELLE VERSION DES WRITE AVEC LA BOUCLE DO RENTREE
   SUBROUTINE histwrite2d_phy(var,field, STD_iff)
-  USE dimphy, ONLY: klon
+  USE dimphy, ONLY: klon, klev
   USE mod_phys_lmdz_para, ONLY: gather_omp, grid1dto2d_mpi, &
                                 jj_nb, klon_mpi, klon_mpi_begin, &
@@ -984,11 +984,15 @@
 
     !Et sinon on.... écrit
-    IF (SIZE(field)/=klon) CALL abort_physic('iophy::histwrite2d_phy','Field first DIMENSION not equal to klon',1)
-    
+    IF (SIZE(field)/=klon .AND. SIZE(field)/=klev) CALL abort_physic('iophy::histwrite2d_phy','Field first DIMENSION not equal to klon/klev',1)    
     IF (prt_level >= 10) THEn
       WRITE (lunout,*)"histwrite2d_phy: .not.vars_defined ; time to gather and write ", trim(var%name)
     ENDIF
     
-    CALL Gather_omp(field,buffer_omp)
+    
+    IF (SIZE(field) == klon) then
+        CALL Gather_omp(field,buffer_omp)
+    ELSE
+        buffer_omp(:)=0.
+    ENDIF
 !$OMP MASTER
     CALL grid1Dto2D_mpi(buffer_omp,Field2d)
@@ -1002,5 +1006,9 @@
              write(lunout,*)'Dans iophy histwrite2D,var%name ', trim(var%name)                       
           ENDIF
-          CALL xios_send_field(var%name, Field2d)
+          IF (SIZE(field) == klon) then
+              CALL xios_send_field(var%name, Field2d)
+          ELSE
+             CALL xios_send_field(var%name, field)
+          ENDIF 
           IF (prt_level >= 10) THEN
              WRITE (lunout,*)'Dans iophy histwrite2D,var%name apres xios_send ', trim(var%name)                       
@@ -1019,5 +1027,9 @@
                      WRITE (lunout,*)"histwrite2d_phy:.NOT.clef_stations(iff)and iff==iff_beg, call xios_send_field"
                   ENDIF
-                  CALL xios_send_field(var%name, Field2d)
+                  IF (SIZE(field) == klon) then
+                     CALL xios_send_field(var%name, Field2d)
+                  ELSE
+                     CALL xios_send_field(var%name, field)
+                  ENDIF
                   firstx=.false.
                ENDIF
@@ -1129,5 +1141,7 @@
   ELSE
     !Et sinon on.... écrit
-    IF (SIZE(field,1)/=klon) CALL abort_physic('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
+
+    IF (SIZE(field,1)/=klon .AND. SIZE(field,1)/=klev) CALL abort_physic('iophy::histwrite3d_xios','Field first DIMENSION not equal to klon/klev',1)
+
     nlev=SIZE(field,2)
     IF (nlev.EQ.klev+1) THEN
@@ -1137,5 +1151,9 @@
     ENDIF
 
-    CALL Gather_omp(field,buffer_omp)
+    IF (SIZE(field,1) == klon) then
+        CALL Gather_omp(field,buffer_omp)
+    ELSE
+        buffer_omp(:,:)=0.
+    ENDIF
 !$OMP MASTER
     CALL grid1Dto2D_mpi(buffer_omp,field3d)
@@ -1151,5 +1169,9 @@
                              trim(var%name)                       
           ENDIF
-          CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
+          IF (SIZE(field,1) == klon) then
+             CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
+          ELSE
+             CALL xios_send_field(var%name, field)
+          ENDIF
 #else
         CALL abort_physic ('iophy','cannot have ok_all_xml = .T. without CPP_XIOS defined' ,1)
@@ -1169,6 +1191,10 @@
                                   nbp_lon,jj_nb,nlevx
                 ENDIF
-                CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
-                            firstx=.false.
+                IF (SIZE(field,1) == klon) then
+                    CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
+                ELSE
+                     CALL xios_send_field(var%name, field)
+                ENDIF
+                firstx=.false.
               ENDIF
 #endif
@@ -1226,5 +1252,5 @@
 #ifdef CPP_XIOS
   SUBROUTINE histwrite2d_xios(field_name,field)
-  USE dimphy, ONLY: klon
+  USE dimphy, ONLY: klon, klev
   USE mod_phys_lmdz_para, ONLY: gather_omp, grid1Dto2D_mpi, &
                                 is_sequential, klon_mpi_begin, klon_mpi_end, &
@@ -1249,9 +1275,14 @@
 
     !Et sinon on.... écrit
-    IF (SIZE(field)/=klon) CALL abort_physic('iophy::histwrite2d_xios','Field first DIMENSION not equal to klon',1)
-    
-    CALL Gather_omp(field,buffer_omp)    
+    IF (SIZE(field)/=klon .AND. SIZE(field)/=klev) CALL abort_physic('iophy::histwrite2d_xios','Field first DIMENSION not equal to klon/klev',1)
+    
+    IF (SIZE(field) == klev) then
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
+        CALL xios_send_field(field_name,field)
+!$OMP END MASTER   
+    ELSE
+        CALL Gather_omp(field,buffer_omp)    
+!$OMP MASTER
+        CALL grid1Dto2D_mpi(buffer_omp,Field2d)
     
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -1259,34 +1290,35 @@
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     !IF(.NOT.clef_stations(iff)) THEN
-    IF (.TRUE.) THEN
-        ALLOCATE(index2d(nbp_lon*jj_nb))
-        ALLOCATE(fieldok(nbp_lon*jj_nb))
-
-
-        CALL xios_send_field(field_name, Field2d)
-
-    ELSE 
-        ALLOCATE(fieldok(npstn))
-        ALLOCATE(index2d(npstn))
-
-        IF (is_sequential) THEN
-            DO ip=1, npstn
-                fieldok(ip)=buffer_omp(nptabij(ip))
-            ENDDO
-        ELSE
-            DO ip=1, npstn
-                PRINT*,'histwrite2d_xios is_sequential npstn ip namenptabij',npstn,ip,field_name,nptabij(ip)
-                IF(nptabij(ip).GE.klon_mpi_begin.AND. &
-                nptabij(ip).LE.klon_mpi_end) THEN
-                    fieldok(ip)=buffer_omp(nptabij(ip)-klon_mpi_begin+1)
-                ENDIF
-            ENDDO
+        IF (.TRUE.) THEN
+            ALLOCATE(index2d(nbp_lon*jj_nb))
+            ALLOCATE(fieldok(nbp_lon*jj_nb))
+    
+    
+            CALL xios_send_field(field_name, Field2d)
+    
+        ELSE 
+            ALLOCATE(fieldok(npstn))
+            ALLOCATE(index2d(npstn))
+    
+            IF (is_sequential) THEN
+                DO ip=1, npstn
+                    fieldok(ip)=buffer_omp(nptabij(ip))
+                ENDDO
+            ELSE
+                DO ip=1, npstn
+                    PRINT*,'histwrite2d_xios is_sequential npstn ip namenptabij',npstn,ip,field_name,nptabij(ip)
+                    IF(nptabij(ip).GE.klon_mpi_begin.AND. &
+                    nptabij(ip).LE.klon_mpi_end) THEN
+                        fieldok(ip)=buffer_omp(nptabij(ip)-klon_mpi_begin+1)
+                    ENDIF
+                ENDDO
+            ENDIF
+    
         ENDIF
-
-    ENDIF
                   
-    DEALLOCATE(index2d)
-    DEALLOCATE(fieldok)
+        DEALLOCATE(index2d)
+        DEALLOCATE(fieldok)
 !$OMP END MASTER   
+    ENDIF
 
   IF (prt_level >= 10) WRITE(lunout,*)'End histrwrite2d_xios ',field_name
@@ -1318,11 +1350,17 @@
 
     !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)
+    IF (SIZE(field,1)/=klon .AND. SIZE(field,1)/=klev) CALL abort_physic('iophy::histwrite3d_xios','Field first DIMENSION not equal to klon/klev',1)
+    
+    IF (SIZE(field,1) == klev) then
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,field3d)
+        CALL xios_send_field(field_name,field)
+!$OMP END MASTER   
+    ELSE
+        nlev=SIZE(field,2)
+
+
+        CALL Gather_omp(field,buffer_omp)
+!$OMP MASTER
+        CALL grid1Dto2D_mpi(buffer_omp,field3d)
 
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -1330,34 +1368,35 @@
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     !IF (.NOT.clef_stations(iff)) THEN
-    IF(.TRUE.)THEN
-        ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
-        ALLOCATE(fieldok(nbp_lon*jj_nb,nlev))
-        CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
-                        
-    ELSE 
-        nlev=size(field,2)
-        ALLOCATE(index3d(npstn*nlev))
-        ALLOCATE(fieldok(npstn,nlev))
-
-        IF (is_sequential) THEN
-            DO n=1, nlev
-                DO ip=1, npstn
-                    fieldok(ip,n)=buffer_omp(nptabij(ip),n)
+        IF(.TRUE.)THEN
+            ALLOCATE(index3d(nbp_lon*jj_nb*nlev))
+            ALLOCATE(fieldok(nbp_lon*jj_nb,nlev))
+            CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
+                            
+        ELSE 
+            nlev=size(field,2)
+            ALLOCATE(index3d(npstn*nlev))
+            ALLOCATE(fieldok(npstn,nlev))
+    
+            IF (is_sequential) THEN
+                DO n=1, nlev
+                    DO ip=1, npstn
+                        fieldok(ip,n)=buffer_omp(nptabij(ip),n)
+                    ENDDO
                 ENDDO
-            ENDDO
-        ELSE
-            DO n=1, nlev
-                DO ip=1, npstn
-                    IF(nptabij(ip).GE.klon_mpi_begin.AND. &
-                    nptabij(ip).LE.klon_mpi_end) THEN
-                        fieldok(ip,n)=buffer_omp(nptabij(ip)-klon_mpi_begin+1,n)
-                    ENDIF
+            ELSE
+                DO n=1, nlev
+                    DO ip=1, npstn
+                        IF(nptabij(ip).GE.klon_mpi_begin.AND. &
+                        nptabij(ip).LE.klon_mpi_end) THEN
+                            fieldok(ip,n)=buffer_omp(nptabij(ip)-klon_mpi_begin+1,n)
+                        ENDIF
+                    ENDDO
                 ENDDO
-            ENDDO
-        ENDIF
-    ENDIF 
-    DEALLOCATE(index3d)
-    DEALLOCATE(fieldok)
+            ENDIF
+        ENDIF 
+        DEALLOCATE(index3d)
+        DEALLOCATE(fieldok)
 !$OMP END MASTER   
+    ENDIF
 
   IF (prt_level >= 10) write(lunout,*)'End histrwrite3d_xios ',field_name
