Index: LMDZ6/trunk/libf/phylmd/iophy.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/iophy.F90	(revision 3266)
+++ LMDZ6/trunk/libf/phylmd/iophy.F90	(revision 3435)
@@ -18,10 +18,5 @@
 #ifdef CPP_XIOS
   INTERFACE histwrite_phy
-!#ifdef CPP_XIOSnew
     MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old,histwrite2d_xios,histwrite3d_xios,histwrite0d_xios
-!#else
-!    MODULE PROCEDURE histwrite2d_phy,histwrite3d_phy,histwrite2d_phy_old,histwrite3d_phy_old,histwrite2d_xios,histwrite3d_xios
-!#endif
-
   END INTERFACE
 #else
@@ -52,11 +47,11 @@
                                   mpi_size, mpi_rank, klon_mpi, &
                                 is_sequential, is_south_pole_dyn
-    USE mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat, klon_glo
-    USE print_control_mod, ONLY: prt_level,lunout 
+  USE mod_grid_phy_lmdz, ONLY: nbp_lon, nbp_lat, klon_glo, grid_type, unstructured
+  USE print_control_mod, ONLY: prt_level,lunout 
 #ifdef CPP_IOIPSL
     USE ioipsl, ONLY: flio_dom_set
 #endif
 #ifdef CPP_XIOS
-    USE wxios, ONLY: wxios_domain_param
+  use wxios, ONLY: wxios_domain_param, wxios_domain_param_unstructured, wxios_context_init
 #endif
     IMPLICIT NONE
@@ -77,8 +72,21 @@
     INTEGER :: data_ibegin, data_iend
 
-    CALL gather(rlat,rlat_glo)
-    CALL bcast(rlat_glo)
-    CALL gather(rlon,rlon_glo)
-    CALL bcast(rlon_glo)
+#ifdef CPP_XIOS
+      CALL wxios_context_init
+#endif
+    
+
+    IF (grid_type==unstructured) THEN
+    
+#ifdef CPP_XIOS
+      CALL wxios_domain_param_unstructured("dom_glo")
+#endif
+
+    ELSE
+
+      CALL gather(rlat,rlat_glo)
+      CALL bcast(rlat_glo)
+      CALL gather(rlon,rlon_glo)
+      CALL bcast(rlon_glo)
     
 !$OMP MASTER  
@@ -133,32 +141,34 @@
 #endif
 #ifdef CPP_XIOS
-    ! Set values for the mask:
-    IF (mpi_rank == 0) THEN
-        data_ibegin = 0
-    ELSE 
-        data_ibegin = ii_begin - 1
-    ENDIF
-
-    IF (mpi_rank == mpi_size-1) THEN
-        data_iend = nbp_lon
-    ELSE
-        data_iend = ii_end + 1
-    ENDIF
-
-    IF (prt_level>=10) THEN
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
-      write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," is_south_pole=",is_south_pole_dyn
-    ENDIF
-
-    ! Initialize the XIOS domain coreesponding to this process:
-    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,is_south_pole_dyn,mpi_rank)
+      ! Set values for the mask:
+      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
+
+      IF (prt_level>=10) THEN
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," iibegin=",ii_begin , " ii_end=",ii_end," jjbegin=",jj_begin," jj_nb=",jj_nb," jj_end=",jj_end
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," nbp_lon=",nbp_lon," nbp_lat=",nbp_lat
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," data_ibegin=",data_ibegin," data_iend=",data_iend
+        write(lunout,*) "init_iophy_new: mpirank=",mpi_rank," is_south_pole_dyn=",is_south_pole_dyn
+      ENDIF
+
+      ! Initialize the XIOS domain coreesponding to this process:
 #endif
 !$OMP END MASTER
+
+#ifdef CPP_XIOS    
+        CALL wxios_domain_param("dom_glo")
+#endif
+      
+    ENDIF
       
   END SUBROUTINE init_iophy_new
@@ -291,5 +301,5 @@
                                 is_sequential, klon_mpi_begin, klon_mpi_end, &
                                 mpi_rank
-  USE mod_grid_phy_lmdz, ONLY: klon_glo, nbp_lon, nbp_lat
+  USE mod_grid_phy_lmdz, ONLY: klon_glo, nbp_lon, nbp_lat, grid1dTo2d_glo
   USE ioipsl, ONLY: histbeg
 
@@ -366,5 +376,5 @@
      ENDDO
 
-       CALL gr_fi_ecrit(1,klon,nbp_lon,nbp_lat,rlon_glo,zx_lon)
+       CALL grid1dTo2d_glo(rlon_glo,zx_lon)
        IF ((nbp_lon*nbp_lat).GT.1) THEN
        DO i = 1, nbp_lon
@@ -373,5 +383,5 @@
        ENDDO
        ENDIF
-       CALL gr_fi_ecrit(1,klon,nbp_lon,nbp_lat,rlat_glo,zx_lat)
+       CALL grid1dTo2d_glo(rlat_glo,zx_lat)
 
     DO i=1,pim
@@ -963,8 +973,9 @@
                                  nid_files, swaerofree_diag, swaero_diag, dryaod_diag, ok_4xCO2atm
   USE print_control_mod, ONLY: prt_level,lunout
-  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
+  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat, grid_type, unstructured, regular_lonlat
 #ifdef CPP_XIOS
   USE xios, ONLY: xios_send_field
 #endif
+  USE print_control_mod, ONLY: lunout, prt_level
 
   IMPLICIT NONE
@@ -1007,5 +1018,6 @@
       IF (.not. ok_all_xml) THEN
       IF (prt_level >= 10) THEN
-      WRITE (lunout,*)"histwrite2d_phy: .not.vars_defined ; time to define ", trim(var%name)
+      write(lunout,*)"histwrite2d_phy: .not.vars_defined ; time to define ", &
+                     trim(var%name)
       ENDIF
       DO iff=iff_beg, iff_end
@@ -1037,5 +1049,5 @@
     ENDIF
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,Field2d)
+    IF (grid_type==regular_lonlat) CALL grid1Dto2D_mpi(buffer_omp,Field2d)
 
 ! La boucle sur les fichiers:
@@ -1047,11 +1059,17 @@
              write(lunout,*)'Dans iophy histwrite2D,var%name ', trim(var%name)                       
           ENDIF
-          IF (SIZE(field) == klon) then
+          
+          IF (grid_type==regular_lonlat) THEN
+            IF (SIZE(field) == klon) then
               CALL xios_send_field(var%name, Field2d)
-          ELSE
-             CALL xios_send_field(var%name, field)
-          ENDIF 
+            ELSE
+               CALL xios_send_field(var%name, field)
+            ENDIF 
+          ELSE IF (grid_type==unstructured) THEN
+            CALL xios_send_field(var%name, buffer_omp)
+          ENDIF
           IF (prt_level >= 10) THEN
-             WRITE (lunout,*)'Dans iophy histwrite2D,var%name apres xios_send ', trim(var%name)                       
+             write(lunout,*)'Dans iophy histwrite2D,var%name apres xios_send ',&
+                             trim(var%name)                       
           ENDIF
 #else
@@ -1065,12 +1083,18 @@
                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 xios_send_field"
+                     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 xios_send_field"
                   ENDIF
-                  IF (SIZE(field) == klon) then
-                     CALL xios_send_field(var%name, Field2d)
-                  ELSE
-                     CALL xios_send_field(var%name, field)
+                  IF (grid_type==regular_lonlat) THEN
+                    IF (SIZE(field) == klon) then
+                       CALL xios_send_field(var%name, Field2d)
+                    ELSE
+                       CALL xios_send_field(var%name, field)
+                    ENDIF
+                  ELSE IF (grid_type==unstructured) THEN
+                    CALL xios_send_field(var%name, buffer_omp)
                   ENDIF
+
                   firstx=.false.
                ENDIF
@@ -1085,7 +1109,7 @@
 !#ifdef CPP_XIOS
 !                        IF (iff == iff_beg) THEN
-!                          if (prt_level >= 10) then
+!                          IF (prt_level >= 10) THEN
 !                            write(lunout,*)"histwrite2d_phy: .NOT.clef_stations(iff) and iff==iff_beg, call xios_send_field"
-!                          endif
+!                          ENDIF
 !                          CALL xios_send_field(var%name, Field2d)
 !                        ENDIF
@@ -1109,5 +1133,5 @@
                        ENDIF ! of IF (is_sequential)
 #ifndef CPP_IOIPSL_NO_OUTPUT 
-                       IF (prt_level >= 10) THEn
+                       IF (prt_level >= 10) THEN
                          write(lunout,*)"histwrite2d_phy: clef_stations(iff) and iff==iff_beg, call wxios_write_2D"
                        ENDIF
@@ -1141,5 +1165,5 @@
                                  nfiles, vars_defined, clef_stations, &
                                  nid_files, swaerofree_diag
-  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
+  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat, grid_type, regular_lonlat, unstructured
 #ifdef CPP_XIOS
   USE xios, ONLY: xios_send_field
@@ -1206,5 +1230,5 @@
     ENDIF
 !$OMP MASTER
-    CALL grid1Dto2D_mpi(buffer_omp,field3d)
+    IF (grid_type==regular_lonlat) CALL grid1Dto2D_mpi(buffer_omp,field3d)
 
 ! BOUCLE SUR LES FICHIERS
@@ -1213,12 +1237,18 @@
     IF (ok_all_xml) THEN
 #ifdef CPP_XIOS
-        IF (prt_level >= 10) THEN
-             write(lunout,*)'Dans iophy histwrite3D,var%name ',trim(var%name)                       
+          IF (prt_level >= 10) THEN
+             write(lunout,*)'Dans iophy histwrite3D,var%name ',&
+                             trim(var%name)                       
+          ENDIF
+          IF (grid_type==regular_lonlat) THEN
+            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 IF (grid_type==unstructured) THEN
+            CALL xios_send_field(var%name, buffer_omp(:,1:nlevx))
         ENDIF
-        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)
@@ -1230,16 +1260,21 @@
 #ifdef CPP_XIOS
               IF (firstx) THEN
-                IF (prt_level >= 10) THEn
-                  WRITE (lunout,*)'Dans iophy, histwrite3D iff nlev klev firstx', &
+                IF (prt_level >= 10) THEN
+                  write(lunout,*)'Dans iophy, histwrite3D iff nlev klev firstx', &
                                   iff,nlev,klev, firstx                       
-                  WRITE (lunout,*)'histwrite3d_phy: call xios_send_field for ', &
+                  write(lunout,*)'histwrite3d_phy: call xios_send_field for ', &
                                   trim(var%name), ' with iim jjm nlevx = ', &
                                   nbp_lon,jj_nb,nlevx
                 ENDIF
-                IF (SIZE(field,1) == klon) then
-                    CALL xios_send_field(var%name, Field3d(:,:,1:nlevx))
-                ELSE
-                     CALL xios_send_field(var%name, field)
+                IF (grid_type==regular_lonlat) THEN
+                  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 IF (grid_type==unstructured) THEN
+                  CALL xios_send_field(var%name, buffer_omp(:,1:nlevx))
                 ENDIF
+
                 firstx=.false.
               ENDIF
@@ -1305,5 +1340,5 @@
                                 is_sequential, klon_mpi_begin, klon_mpi_end, &
                                 jj_nb, klon_mpi, is_master
-  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
+  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat, grid_type, unstructured
   USE xios, ONLY: xios_send_field
   USE print_control_mod, ONLY: prt_level,lunout
@@ -1335,4 +1370,11 @@
         CALL Gather_omp(field,buffer_omp)    
 !$OMP MASTER
+
+      IF (grid_type==unstructured) THEN
+  
+        CALL xios_send_field(field_name, buffer_omp)
+
+      ELSE
+
         CALL grid1Dto2D_mpi(buffer_omp,Field2d)
     
@@ -1342,7 +1384,4 @@
     !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)
@@ -1365,9 +1404,9 @@
                 ENDDO
             ENDIF
-    
-        ENDIF
-                  
-        DEALLOCATE(index2d)
-        DEALLOCATE(fieldok)
+            DEALLOCATE(index2d)
+            DEALLOCATE(fieldok)
+    
+        ENDIF                  
+      ENDIF
 !$OMP END MASTER   
   ENDIF
@@ -1385,5 +1424,5 @@
                                 jj_nb, klon_mpi, is_master
   USE xios, ONLY: xios_send_field
-  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
+  USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat, grid_type, unstructured
   USE print_control_mod, ONLY: prt_level,lunout
 
@@ -1416,4 +1455,10 @@
         CALL Gather_omp(field,buffer_omp)
 !$OMP MASTER
+
+    IF (grid_type==unstructured) THEN
+
+      CALL xios_send_field(field_name, buffer_omp(:,1:nlev))
+
+    ELSE
         CALL grid1Dto2D_mpi(buffer_omp,field3d)
 
@@ -1423,7 +1468,6 @@
     !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))
+
+           CALL xios_send_field(field_name, Field3d(:,:,1:nlev))
                             
         ELSE 
@@ -1448,7 +1492,8 @@
                 ENDDO
             ENDIF
+            DEALLOCATE(index3d)
+            DEALLOCATE(fieldok)
         ENDIF 
-        DEALLOCATE(index3d)
-        DEALLOCATE(fieldok)
+      ENDIF
 !$OMP END MASTER   
   ENDIF
