Index: LMDZ6/trunk/libf/phylmd/phyaqua_mod.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/phyaqua_mod.F90	(revision 3401)
+++ LMDZ6/trunk/libf/phylmd/phyaqua_mod.F90	(revision 3435)
@@ -29,5 +29,9 @@
     USE indice_sol_mod
     USE nrtype, ONLY: pi
-    USE ioipsl
+!    USE ioipsl
+    USE mod_phys_lmdz_para, ONLY: is_master
+    USE mod_phys_lmdz_transfert_para, ONLY: bcast
+    USE mod_grid_phy_lmdz
+    USE ioipsl_getin_p_mod, ONLY : getin_p
     IMPLICIT NONE
 
@@ -57,6 +61,4 @@
     INTEGER it, unit, i, k, itap
 
-    REAL airefi, zcufi, zcvfi
-
     REAL rugos, albedo
     REAL tsurf
@@ -64,9 +66,5 @@
     REAL qsol_f
     REAL rugsrel(nlon)
-    ! real zmea(nlon),zstd(nlon),zsig(nlon)
-    ! real zgam(nlon),zthe(nlon),zpic(nlon),zval(nlon)
-    ! real rlon(nlon),rlat(nlon)
     LOGICAL alb_ocean
-    ! integer demih_pas
 
     CHARACTER *80 ans, file_forctl, file_fordat, file_start
@@ -86,11 +84,6 @@
 
     INTEGER, SAVE :: read_climoz = 0 ! read ozone climatology
-
-    ! intermediate variables to use getin (need to be "save" to be shared by
-    ! all threads)
-    INTEGER, SAVE :: nbapp_rad_omp
-    REAL, SAVE :: co2_ppm_omp, solaire_omp
-    LOGICAL, SAVE :: alb_ocean_omp
-    REAL, SAVE :: rugos_omp
+!$OMP THREADPRIVATE(read_climoz)
+
     ! -------------------------------------------------------------------------
     ! declaration pour l'appel a phyredem
@@ -117,8 +110,6 @@
     INTEGER l, ierr, aslun
 
-!    REAL longitude, latitude
     REAL paire
 
-!    DATA latitude, longitude/48., 0./
 
     ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -148,10 +139,4 @@
     time = 0.
 
-    ! IM ajout latfi, lonfi
-!    rlatd = latfi
-!    rlond = lonfi
-!    rlat = rlatd*180./pi
-!    rlon = rlond*180./pi
-
     ! -----------------------------------------------------------------------
     ! initialisations de la physique
@@ -160,13 +145,7 @@
     day_ini = day_ref
     day_end = day_ini + ndays
-!    airefi = 1.
-!    zcufi = 1.
-!    zcvfi = 1.
-    !$OMP MASTER
-    nbapp_rad_omp = 24
-    CALL getin('nbapp_rad', nbapp_rad_omp)
-    !$OMP END MASTER
-    !$OMP BARRIER
-    nbapp_rad = nbapp_rad_omp
+
+    nbapp_rad = 24
+    CALL getin_p('nbapp_rad', nbapp_rad)
 
     ! ---------------------------------------------------------------------
@@ -175,20 +154,17 @@
     ! Initialisations des constantes
     ! Ajouter les manquants dans planete.def... (albedo etc)
-    !$OMP MASTER
-    co2_ppm_omp = 348.
-    CALL getin('co2_ppm', co2_ppm_omp)
-    solaire_omp = 1365.
-    CALL getin('solaire', solaire_omp)
+    co2_ppm = 348.
+    CALL getin_p('co2_ppm', co2_ppm)
+
+    solaire = 1365.
+    CALL getin_p('solaire', solaire)
+ 
     ! CALL getin('albedo',albedo) ! albedo is set below, depending on
     ! type_aqua
-    alb_ocean_omp = .TRUE.
-    CALL getin('alb_ocean', alb_ocean_omp)
-    !$OMP END MASTER
-    !$OMP BARRIER
-    co2_ppm = co2_ppm_omp
+    alb_ocean = .TRUE.
+    CALL getin_p('alb_ocean', alb_ocean)
+
     WRITE (*, *) 'iniaqua: co2_ppm=', co2_ppm
-    solaire = solaire_omp
     WRITE (*, *) 'iniaqua: solaire=', solaire
-    alb_ocean = alb_ocean_omp
     WRITE (*, *) 'iniaqua: alb_ocean=', alb_ocean
 
@@ -226,10 +202,6 @@
     END IF
 
-    !$OMP MASTER
-    rugos_omp = rugos
-    CALL getin('rugos', rugos_omp)
-    !$OMP END MASTER
-    !$OMP BARRIER
-    rugos = rugos_omp
+    CALL getin_p('rugos', rugos)
+
     WRITE (*, *) 'iniaqua: rugos=', rugos
     zmasq(:) = pctsrf(:, is_ter)
@@ -262,7 +234,12 @@
     CALL profil_sst(nlon, latitude, type_profil, phy_sst)
 
-    CALL writelim(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, phy_ice, &
-      phy_fter, phy_foce, phy_flic, phy_fsic)
-
+    IF (grid_type==unstructured) THEN
+      CALL writelim_unstruct(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, phy_ice, &
+                             phy_fter, phy_foce, phy_flic, phy_fsic)
+    ELSE
+      
+       CALL writelim(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, phy_ice, &
+                     phy_fter, phy_foce, phy_flic, phy_fsic)
+    ENDIF
 
     ! ---------------------------------------------------------------------
@@ -339,5 +316,5 @@
     PRINT *, 'iniaqua: before phyredem'
 
-    pbl_tke(:,:,:)=1.e-8
+    pbl_tke(:,:,:) = 1.e-8 
     falb1 = albedo
     falb2 = albedo
@@ -349,5 +326,5 @@
     wake_deltaq = 0.
     wake_s = 0.
-    wake_dens = 0. 
+    wake_dens = 0.
     wake_cstar = 0.
     wake_pe = 0.
@@ -360,4 +337,14 @@
     alp_bl =0.
     treedrg(:,:,:)=0.
+
+!ym error : the sub surface dimension is the third not second : forgotten for iniaqua
+!    falb_dir(:,is_ter,:)=0.08; falb_dir(:,is_lic,:)=0.6
+!    falb_dir(:,is_oce,:)=0.5;  falb_dir(:,is_sic,:)=0.6
+    falb_dir(:,:,is_ter)=0.08; falb_dir(:,:,is_lic)=0.6
+    falb_dir(:,:,is_oce)=0.5;  falb_dir(:,:,is_sic)=0.6
+
+!ym falb_dif has been forgotten, initialize with defaukt value found in phyetat0 or 0 ?
+!ym probably the uninitialized value was 0 for standard (regular grid) case
+    falb_dif(:,:,:)=0
 
 
@@ -488,11 +475,14 @@
   ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
-  SUBROUTINE writelim(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, &
+  SUBROUTINE writelim_unstruct(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, &
       phy_ice, phy_fter, phy_foce, phy_flic, phy_fsic)
 
-    USE mod_phys_lmdz_para, ONLY: is_mpi_root, is_omp_root
-    USE mod_grid_phy_lmdz, ONLY: klon_glo
-    USE mod_phys_lmdz_transfert_para, ONLY: gather
+    USE mod_phys_lmdz_para, ONLY: is_omp_master, klon_mpi
+    USE mod_phys_lmdz_transfert_para, ONLY: gather_omp
+#ifdef CPP_XIOS
+    USE xios
+#endif
     IMPLICIT NONE
+
     include "netcdf.inc"
 
@@ -509,4 +499,59 @@
     REAL, INTENT (IN) :: phy_fsic(klon, 360)
 
+    REAL :: phy_mpi(klon_mpi, 360) ! temporary variable, to store phy_***(:)
+      ! on the whole physics grid
+ 
+#ifdef CPP_XIOS
+    PRINT *, 'writelim: Ecriture du fichier limit'
+
+    CALL gather_omp(phy_foce, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('foce_limout',phy_mpi)
+
+    CALL gather_omp(phy_fsic, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('fsic_limout',phy_mpi)
+      
+    CALL gather_omp(phy_fter, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('fter_limout',phy_mpi)
+      
+    CALL gather_omp(phy_flic, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('flic_limout',phy_mpi)
+
+    CALL gather_omp(phy_sst, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('sst_limout',phy_mpi)
+
+    CALL gather_omp(phy_bil, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('bils_limout',phy_mpi)
+
+    CALL gather_omp(phy_alb, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('alb_limout',phy_mpi)
+
+    CALL gather_omp(phy_rug, phy_mpi)
+    IF (is_omp_master) CALL xios_send_field('rug_limout',phy_mpi)
+#endif
+  END SUBROUTINE writelim_unstruct
+
+
+
+  SUBROUTINE writelim(klon, phy_nat, phy_alb, phy_sst, phy_bil, phy_rug, &
+      phy_ice, phy_fter, phy_foce, phy_flic, phy_fsic)
+
+    USE mod_phys_lmdz_para, ONLY: is_master
+    USE mod_grid_phy_lmdz, ONLY: klon_glo
+    USE mod_phys_lmdz_transfert_para, ONLY: gather
+    IMPLICIT NONE
+    include "netcdf.inc"
+
+    INTEGER, INTENT (IN) :: klon
+    REAL, INTENT (IN) :: phy_nat(klon, 360)
+    REAL, INTENT (IN) :: phy_alb(klon, 360)
+    REAL, INTENT (IN) :: phy_sst(klon, 360)
+    REAL, INTENT (IN) :: phy_bil(klon, 360)
+    REAL, INTENT (IN) :: phy_rug(klon, 360)
+    REAL, INTENT (IN) :: phy_ice(klon, 360)
+    REAL, INTENT (IN) :: phy_fter(klon, 360)
+    REAL, INTENT (IN) :: phy_foce(klon, 360)
+    REAL, INTENT (IN) :: phy_flic(klon, 360)
+    REAL, INTENT (IN) :: phy_fsic(klon, 360)
+
     REAL :: phy_glo(klon_glo, 360) ! temporary variable, to store phy_***(:)
       ! on the whole physics grid
@@ -522,5 +567,5 @@
     INTEGER id_fter, id_foce, id_fsic, id_flic
 
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 
       PRINT *, 'writelim: Ecriture du fichier limit'
@@ -627,10 +672,10 @@
       END DO
 
-    END IF ! of if (is_mpi_root.and.is_omp_root)
+    END IF ! of if (is_master)
 
     ! write the fields, after having collected them on master
 
     CALL gather(phy_nat, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_nat, phy_glo)
@@ -645,5 +690,5 @@
 
     CALL gather(phy_sst, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_sst, phy_glo)
@@ -658,5 +703,5 @@
 
     CALL gather(phy_bil, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_bils, phy_glo)
@@ -671,5 +716,5 @@
 
     CALL gather(phy_alb, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_alb, phy_glo)
@@ -684,5 +729,5 @@
 
     CALL gather(phy_rug, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_rug, phy_glo)
@@ -697,5 +742,5 @@
 
     CALL gather(phy_fter, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_fter, phy_glo)
@@ -710,5 +755,5 @@
 
     CALL gather(phy_foce, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_foce, phy_glo)
@@ -723,5 +768,5 @@
 
     CALL gather(phy_fsic, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_fsic, phy_glo)
@@ -736,5 +781,5 @@
 
     CALL gather(phy_flic, phy_glo)
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
 #ifdef NC_DOUBLE
       ierr = nf_put_var_double(nid, id_flic, phy_glo)
@@ -749,5 +794,5 @@
 
     ! close file:
-    IF (is_mpi_root .AND. is_omp_root) THEN
+    IF (is_master) THEN
       ierr = nf_close(nid)
     END IF
