Index: LMDZ5/trunk/libf/phylmd/iophy.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 1828)
+++ LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 1852)
@@ -5,4 +5,7 @@
 
   USE phys_output_var_mod
+#ifdef CPP_XIOS
+  USE wxios
+#endif
 
 #ifdef CPP_XIOS
@@ -20,8 +23,14 @@
 
 !$OMP THREADPRIVATE(itau_iophy)
-  
+
+#ifdef CPP_XIOS
+  INTERFACE histwrite_phy
+    MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old,histwrite2d_xios,histwrite3d_xios
+  END INTERFACE
+#else
   INTERFACE histwrite_phy
     MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old
   END INTERFACE
+#endif
 
   INTERFACE histbeg_phy_all
@@ -44,5 +53,5 @@
   USE mod_grid_phy_lmdz
   USE ioipsl
-  
+
   IMPLICIT NONE
   INCLUDE 'dimensions.h'   
@@ -61,4 +70,5 @@
     INTEGER,DIMENSION(2) :: dhe 
     INTEGER :: i    
+    INTEGER :: data_ibegin, data_iend
 
     CALL gather(rlat,rlat_glo)
@@ -79,4 +89,18 @@
     ALLOCATE(io_lon(iim))
     io_lon(:)=rlon_glo(2-1/(iim*jjm):iim+1-1/(iim*jjm))
+!! (I) dtnb   : total number of domains
+!! (I) dnb    : domain number
+!! (I) did(:) : distributed dimensions identifiers
+!!              (up to 5 dimensions are supported)
+!! (I) dsg(:) : total number of points for each dimension
+!! (I) dsl(:) : local number of points for each dimension
+!! (I) dpf(:) : position of first local point for each dimension
+!! (I) dpl(:) : position of last local point for each dimension
+!! (I) dhs(:) : start halo size for each dimension
+!! (I) dhe(:) : end halo size for each dimension
+!! (C) cdnm   : Model domain definition name.
+!!              The names actually supported are :
+!!              "BOX", "APPLE", "ORANGE".
+!!              These names are case insensitive.
 
     ddid=(/ 1,2 /)
@@ -91,10 +115,34 @@
       dhe=(/ iim-ii_end,0 /)  
     ENDIF
-    
+
+#ifndef CPP_NO_IOIPSL    
     CALL flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
                       'APPLE',phys_domain_id)
+#endif
 #ifdef CPP_XIOS
+    !Pour els soucis en MPI, réglage du masque:
+    IF (mpi_rank == 0) THEN
+        data_ibegin = 0
+    ELSE 
+        data_ibegin = ii_begin - 1
+    END IF
+
+    IF (mpi_rank == mpi_size-1) THEN
+        data_iend = nbp_lon
+    ELSE
+        data_iend = ii_end + 1
+    END IF
+
+    WRITE(*,*) "TOTO mpirank=",mpi_rank,"iibeg=",ii_begin , "jjbeg=",jj_begin,"jjnb=",jj_nb,"jjend=",jj_end
+
     !On initialise le domaine xios, maintenant que tout est connu:
-    CALL wxios_domain_param("dom_glo", is_sequential, iim, jjm+1, io_lat, io_lon)
+    !SUBROUTINE wxios_domain_param(dom_id, is_sequential, ni, nj, ni_glo, nj_glo,        &
+    !                                ibegin, iend, jbegin, jend,                         &
+    !                                data_ni, data_ibegin,                               &
+    !                                io_lat, io_lon)
+    CALL wxios_domain_param("dom_glo", is_sequential, nbp_lon, jj_nb, nbp_lon, nbp_lat, &
+                            1, nbp_lon, ii_begin, ii_end, jj_begin, jj_end,             &
+                            klon_mpi+2*(nbp_lon-1), data_ibegin, data_iend,             &
+                            io_lat, io_lon)
 #endif
 !$OMP END MASTER
@@ -137,7 +185,8 @@
     endif
     
+#ifndef CPP_NO_IOIPSL 
     call flio_dom_set(mpi_size,mpi_rank,ddid,dsg,dsl,dpf,dpl,dhs,dhe, &
                       'APPLE',phys_domain_id)
-
+#endif
 !$OMP END MASTER
       
@@ -197,4 +246,5 @@
 
 !$OMP MASTER    
+#ifndef CPP_NO_IOIPSL 
     if (is_sequential) then
       call histbeg(name,iim,io_lon, jj_nb,io_lat(jj_begin:jj_end), &
@@ -204,5 +254,5 @@
                    1,iim,1,jj_nb,itau0, zjulian, dtime, nhori, nid_day,phys_domain_id)
     endif
-
+#endif
 !$OMP END MASTER
   
@@ -334,7 +384,9 @@
     ENDDO
 !    print*,'iophy is_sequential nname, nnhori, nnid_day=',trim(nname),nnhori,nnid_day
+#ifndef CPP_NO_IOIPSL 
      call histbeg(nname,pim,plon,plon_bounds, & 
                            plat,plat_bounds, &
                            itau0, zjulian, dtime, nnhori, nnid_day)
+#endif
     else
      npproc=0
@@ -373,7 +425,9 @@
       ENDIF
      ENDDO
+#ifndef CPP_NO_IOIPSL 
      call histbeg(nname,npstn,npplon,npplon_bounds, &
                             npplat,npplat_bounds, &
                             itau0,zjulian,dtime,nnhori,nnid_day,phys_domain_id)
+#endif
     endif
 !$OMP END MASTER
@@ -534,8 +588,10 @@
 
     IF(.NOT.clef_stations(iff)) THEN  
+
 #ifdef CPP_XIOS
-        CALL wxios_add_field_to_file(var%name, 2, nid_files(iff), phys_out_filenames(iff), &
+        CALL wxios_add_field_to_file(var%name, 2, iff, phys_out_filenames(iff), &
         var%description, var%unit, var%flag(iff), typeecrit)
 #endif
+#ifndef CPP_NO_IOIPSL 
 
        IF ( var%flag(iff)<=lev_files(iff) ) THEN
@@ -550,4 +606,5 @@
                typeecrit, zstophym,zoutm(iff))                
        ENDIF
+#endif
     ENDIF
 
@@ -602,8 +659,10 @@
 
     IF(.NOT.clef_stations(iff)) THEN
+
 #ifdef CPP_XIOS
-        CALL wxios_add_field_to_file(var%name, 3, nid_files(iff), phys_out_filenames(iff), &
+        CALL wxios_add_field_to_file(var%name, 3, iff, phys_out_filenames(iff), &
         var%description, var%unit, var%flag(iff), typeecrit)
 #endif
+#ifndef CPP_NO_IOIPSL 
 
        IF ( var%flag(iff)<=lev_files(iff) ) THEN
@@ -620,4 +679,5 @@
                typeecrit, zstophym,zoutm(iff))
        ENDIF
+#endif
     ENDIF
   END SUBROUTINE histdef3d
@@ -849,6 +909,7 @@
                         ALLOCATE(index2d(iim*jj_nb))
                         ALLOCATE(fieldok(iim*jj_nb))
-     
+#ifndef CPP_NO_IOIPSL 
                         CALL histwrite(nid_files(iff),var%name,itau_iophy,Field2d,iim*jj_nb,index2d)
+#endif
 #ifdef CPP_XIOS
                         IF (iff == iff_beg) THEN
@@ -873,6 +934,7 @@
                               ENDDO
                        ENDIF
-     
+#ifndef CPP_NO_IOIPSL 
                        CALL histwrite(nid_files(iff),var%name,itau_iophy,fieldok,npstn,index2d)
+#endif
                   ENDIF
                   
@@ -953,5 +1015,8 @@
                         ALLOCATE(index3d(iim*jj_nb*nlev))
                         ALLOCATE(fieldok(iim*jj_nb,nlev))
+
+#ifndef CPP_NO_IOIPSL 
                         CALL histwrite(nid_files(iff),var%name,itau_iophy,Field3d,iim*jj_nb*nlev,index3d)
+#endif
 
 #ifdef CPP_XIOS
@@ -982,5 +1047,7 @@
                               ENDDO
                         ENDIF
+#ifndef CPP_NO_IOIPSL 
                         CALL histwrite(nid_files(iff),var%name,itau_iophy,fieldok,npstn*nlev,index3d)
+#endif
                   ENDIF 
                   deallocate(index3d)
@@ -993,3 +1060,143 @@
   END SUBROUTINE histwrite3d_phy
   
+
+! VERSION DES HISTWRITE DEDIEES AU TOUT-XIOS-XML DEJA UTILISEE DANS PHYDEV
+#ifdef CPP_XIOS
+  SUBROUTINE histwrite2d_xios(field_name,field)
+  USE dimphy
+  USE mod_phys_lmdz_para
+  USE wxios
+
+
+  IMPLICIT NONE
+  INCLUDE 'dimensions.h'
+  INCLUDE 'iniprint.h'
+
+    CHARACTER(LEN=*), INTENT(IN) :: field_name
+    REAL, DIMENSION(:), INTENT(IN) :: field
+      
+    REAL,DIMENSION(klon_mpi) :: buffer_omp
+    INTEGER, allocatable, DIMENSION(:) :: index2d
+    REAL :: Field2d(iim,jj_nb)
+
+    INTEGER :: ip
+    REAL, ALLOCATABLE, DIMENSION(:) :: fieldok
+
+    IF (prt_level >= 9) WRITE(lunout,*)'Begin histrwrite2d_xios ',field_name
+
+    !Et sinon on.... écrit
+    IF (SIZE(field)/=klon) CALL abort_gcm('iophy::histwrite2d_xios','Field first DIMENSION not equal to klon',1)
+    
+    CALL Gather_omp(field,buffer_omp)    
+!$OMP MASTER
+    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
+    
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+!ATTENTION, STATIONS PAS GEREES !
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+    !IF(.NOT.clef_stations(iff)) THEN
+    IF (.TRUE.) THEN
+        ALLOCATE(index2d(iim*jj_nb))
+        ALLOCATE(fieldok(iim*jj_nb))
+
+
+        CALL wxios_write_2D(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
+                  
+    deallocate(index2d)
+    deallocate(fieldok)
+!$OMP END MASTER   
+
+  IF (prt_level >= 9) WRITE(lunout,*)'End histrwrite2d_xios ',field_name
+  END SUBROUTINE histwrite2d_xios
+
+
+! ug NOUVELLE VERSION DES WRITE AVEC LA BOUCLE DO RENTREE
+  SUBROUTINE histwrite3d_xios(field_name, field)
+  USE dimphy
+  USE mod_phys_lmdz_para
+  USE wxios
+
+
+  IMPLICIT NONE
+  INCLUDE 'dimensions.h'
+  INCLUDE 'iniprint.h'
+
+    CHARACTER(LEN=*), INTENT(IN) :: field_name
+    REAL, DIMENSION(:,:), INTENT(IN) :: field ! --> field(klon,:)
+
+    REAL,DIMENSION(klon_mpi,SIZE(field,2)) :: buffer_omp
+    REAL :: Field3d(iim,jj_nb,SIZE(field,2))
+    INTEGER :: ip, n, nlev
+    INTEGER, ALLOCATABLE, DIMENSION(:) :: index3d
+    REAL,ALLOCATABLE, DIMENSION(:,:) :: fieldok
+
+  IF (prt_level >= 9) write(lunout,*)'Begin histrwrite3d_xios ',field_name
+
+    !Et on.... écrit
+    IF (SIZE(field,1)/=klon) CALL abort_gcm('iophy::histwrite3d','Field first DIMENSION not equal to klon',1)
+    nlev=SIZE(field,2)
+
+
+    CALL Gather_omp(field,buffer_omp)
+!$OMP MASTER
+    CALL grid1Dto2D_mpi(buffer_omp,field3d)
+
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+!ATTENTION, STATIONS PAS GEREES !
+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+    !IF (.NOT.clef_stations(iff)) THEN
+    IF(.TRUE.)THEN
+        ALLOCATE(index3d(iim*jj_nb*nlev))
+        ALLOCATE(fieldok(iim*jj_nb,nlev))
+        CALL wxios_write_3D(field_name, Field3d(:,:,1:klev))
+                        
+    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
+        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
+        ENDIF
+    ENDIF 
+    deallocate(index3d)
+    deallocate(fieldok)
+!$OMP END MASTER   
+
+  IF (prt_level >= 9) write(lunout,*)'End histrwrite3d_xios ',field_name
+  END SUBROUTINE histwrite3d_xios
+#endif
 end module iophy
