Index: LMDZ6/trunk/libf/phylmd/output_physiqex_mod.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/output_physiqex_mod.F90	(revision 4539)
+++ LMDZ6/trunk/libf/phylmd/output_physiqex_mod.F90	(revision 4539)
@@ -0,0 +1,143 @@
+MODULE output_physiqex_mod
+
+
+
+CONTAINS 
+
+SUBROUTINE output_physiqex(debut,zjulian,pdtphys,presnivs,paprs,u,v,t)
+
+      USE dimphy, only : klon,klev
+      USE iophy, only : histbeg_phy,histwrite_phy
+      USE ioipsl, only : histvert,histdef,histend,histsync
+      USE mod_phys_lmdz_para, only : jj_nb
+      USE ioipsl_getin_p_mod, ONLY : getin_p
+      USE mod_grid_phy_lmdz, ONLY: nbp_lon,nbp_lat
+      USE iophy, ONLY : init_iophy_new
+      USE geometry_mod, ONLY: latitude_deg, longitude_deg
+
+
+
+implicit none
+logical, intent(in) :: debut
+real, intent(in) :: pdtphys,zjulian
+real,intent(in) :: presnivs(klev) ! pseudo-pressure (Pa) of mid-layers
+real,intent(in) :: u(klon,klev) ! eastward zonal wind (m/s)
+real,intent(in) :: v(klon,klev) ! northward meridional wind (m/s)
+real,intent(in) :: t(klon,klev) ! temperature (K)
+real,intent(in) :: paprs(klon,klev+1) ! interlayer pressure (Pa)
+
+real :: t_ops ! frequency of the IOIPSL operations (eg average over...)
+real :: t_wrt ! frequency of the IOIPSL outputs
+integer :: zvertid ! vertical coordinate ID
+real :: dtime
+
+integer,save :: iwrite_phys=1 ! output every iwrite_phys physics step
+!$OMP THREADPRIVATE(iwrite_phys)
+integer :: nhori ! horizontal coordinate ID
+integer,save :: nid_hist ! output file ID
+!$OMP THREADPRIVATE(nid_hist)
+integer, save :: itau=0
+!$OMP THREADPRIVATE(itau)
+
+
+
+print*,'nnnnnnn ',nid_hist,debut,itau
+
+if(debut)then
+
+   CALL init_iophy_new(latitude_deg,longitude_deg)
+
+   dtime=pdtphys
+   itau=0
+   call histbeg_phy("histins.nc",itau,zjulian,dtime,nhori,nid_hist)
+   print*,'NNNNNNN ',nid_hist,debut
+
+   call getin_p("iwrite_phys",iwrite_phys)
+   print*,'NNNNNNN OK0'
+   t_ops=pdtphys*iwrite_phys ! frequency of the IOIPSL operation
+   t_wrt=pdtphys*iwrite_phys ! frequency of the outputs in the file
+   print*,'NNNNNNN OK1'
+
+   !$OMP MASTER
+
+#ifndef CPP_IOIPSL_NO_OUTPUT 
+     ! IOIPSL
+     ! define vertical coordinate
+     call histvert(nid_hist,"presnivs","Vertical levels","Pa",klev, &
+                   presnivs,zvertid,'down')
+     ! define variables which will be written in "histins.nc" file
+     call histdef(nid_hist,'Temp','Atmospheric temperature','K', &
+                  nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
+                  'inst(X)',t_ops,t_wrt)
+     print*,'NNNNNNN OK2a',nid_hist,t_ops,t_wrt
+     call histdef(nid_hist,'u','Eastward Zonal Wind','m/s', &
+                  nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
+                  'inst(X)',t_ops,t_wrt)
+     print*,'NNNNNNN OK2b',nid_hist,t_ops,t_wrt
+     call histdef(nid_hist,'v','Northward Meridional Wind','m/s', &
+                  nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
+                  'inst(X)',t_ops,t_wrt)
+     print*,'NNNNNNN OK2c',nid_hist,t_ops,t_wrt
+     call histdef(nid_hist,'ps','Surface Pressure','Pa', &
+                  nbp_lon,jj_nb,nhori,1,1,1,zvertid,32, &
+                  'inst(X)',t_ops,t_wrt)
+     ! end definition sequence
+     print*,'NNNNNNN OK2',nid_hist,t_ops,t_wrt
+     call histend(nid_hist)
+     print*,'NNNNNNN OK3'
+#endif
+
+#ifdef CPP_XIOS
+   !XIOS
+       ! Declare available vertical axes to be used in output files:    
+       CALL wxios_add_vaxis("presnivs", klev, presnivs)
+   
+       ! Declare calendar and time step
+       CALL wxios_set_cal(dtime,"earth_360d",1,1,1,0.0,1,1,1,0.0)
+       
+       !Finalize the context:
+       CALL wxios_closedef()
+#endif
+
+   !$OMP END MASTER
+   !$OMP BARRIER
+
+endif
+
+
+itau=itau+1
+
+! write some outputs:
+! IOIPSL
+#ifndef CPP_IOIPSL_NO_OUTPUT 
+if (modulo(itau,iwrite_phys)==0) then
+  call histwrite_phy(nid_hist,.false.,"Temp",itau,t)
+  call histwrite_phy(nid_hist,.false.,"u",itau,u)
+  call histwrite_phy(nid_hist,.false.,"v",itau,v)
+  call histwrite_phy(nid_hist,.false.,"ps",itau,paprs(:,1))
+!$OMP MASTER
+     CALL histsync(nid_hist)
+!$OMP END MASTER
+endif
+#endif
+
+!XIOS
+#ifdef CPP_XIOS
+   !$OMP MASTER
+       !Increment XIOS time
+       CALL xios_update_calendar(itau)
+   !$OMP END MASTER
+   !$OMP BARRIER
+   
+       !Send fields to XIOS: (NB these fields must also be defined as
+       ! <field id="..." /> in iodef.xml to be correctly used
+       CALL histwrite_phy("Temp",t)
+       CALL histwrite_phy("temp_newton",temp_newton)
+       CALL histwrite_phy("u",u)
+       CALL histwrite_phy("v",v)
+       CALL histwrite_phy("ps",paprs(:,1))
+#endif
+
+
+END SUBROUTINE output_physiqex
+END MODULE output_physiqex_mod
Index: LMDZ6/trunk/libf/phylmd/physiqex_mod.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/physiqex_mod.F90	(revision 4538)
+++ LMDZ6/trunk/libf/phylmd/physiqex_mod.F90	(revision 4539)
@@ -17,19 +17,8 @@
       USE geometry_mod, only : latitude
 !      USE comcstphy, only : rg
-      USE iophy, only : histbeg_phy,histwrite_phy
-      USE ioipsl, only : getin,histvert,histdef,histend,ymds2ju
-      USE mod_phys_lmdz_para, only : jj_nb
+      USE ioipsl, only : ymds2ju
       USE phys_state_var_mod, only : phys_state_var_init
-      USE mod_grid_phy_lmdz, ONLY: nbp_lon,nbp_lat
       USE phyetat0_mod, only: phyetat0
-      USE iophy, ONLY : init_iophy_new
-      USE geometry_mod, ONLY: cell_area, latitude_deg, longitude_deg
-
-
-#ifdef CPP_XIOS
-      USE xios, ONLY: xios_update_calendar
-      USE wxios, only: wxios_add_vaxis, wxios_set_cal, wxios_closedef
-      USE iophy, ONLY: histwrite_phy
-#endif
+      USE output_physiqex_mod, ONLY: output_physiqex
 
       IMPLICIT none
@@ -69,6 +58,4 @@
 
 
-integer,save :: itau=0 ! counter to count number of calls to physics
-!$OMP THREADPRIVATE(itau)
 real :: temp_newton(klon,klev)
 integer :: k
@@ -82,16 +69,4 @@
 integer :: itau0
 real :: zjulian
-real :: dtime
-integer :: nhori ! horizontal coordinate ID
-integer,save :: nid_hist ! output file ID
-!$OMP THREADPRIVATE(nid_hist)
-integer :: zvertid ! vertical coordinate ID
-integer,save :: iwrite_phys ! output every iwrite_phys physics step
-!$OMP THREADPRIVATE(iwrite_phys)
-integer,save :: iwrite_phys_omp ! intermediate variable to read iwrite_phys
-                                ! (must be shared by all threads)
-real :: t_ops ! frequency of the IOIPSL operations (eg average over...)
-real :: t_wrt ! frequency of the IOIPSL outputs
-
 
 
@@ -108,25 +83,13 @@
   call phys_state_var_init(1) ! some initializations, required before calling phyetat0
   call phyetat0("startphy.nc", clesphy0, tabcntr0)
-  CALL init_iophy_new(latitude_deg,longitude_deg)
 
 ! Initialize outputs:
   itau0=0
-!$OMP MASTER
-  iwrite_phys_omp=1 !default: output every physics timestep
-  ! NB: getin() is not threadsafe; only one thread should call it.
-  call getin("iwrite_phys",iwrite_phys_omp)
-!$OMP END MASTER
-!$OMP BARRIER
-  iwrite_phys=iwrite_phys_omp
-  t_ops=pdtphys*iwrite_phys ! frequency of the IOIPSL operation
-  t_wrt=pdtphys*iwrite_phys ! frequency of the outputs in the file
   ! compute zjulian for annee0=1979 and month=1 dayref=1 and hour=0.0
   !CALL ymds2ju(annee0, month, dayref, hour, zjulian)
   call ymds2ju(1979, 1, 1, 0.0, zjulian)
-  dtime=pdtphys
 
 #ifndef CPP_IOIPSL_NO_OUTPUT
   ! Initialize IOIPSL output file
-  call histbeg_phy("histins.nc",itau0,zjulian,dtime,nhori,nid_hist)
 #endif
 
@@ -137,6 +100,4 @@
 !------------------------------------------------------------
 
-! increment local time counter itau
-itau=itau+1
 
 ! set all tendencies to zero
@@ -166,72 +127,6 @@
 !------------------------------------------------------------
 
-print*,'PHYDEV: itau=',itau
 
-
-if(debut)then
-!$OMP MASTER
-#ifndef CPP_IOIPSL_NO_OUTPUT 
-! IOIPSL
-  ! define vertical coordinate
-  call histvert(nid_hist,"presnivs","Vertical levels","Pa",klev, &
-                presnivs,zvertid,'down')
-  ! define variables which will be written in "histins.nc" file
-  call histdef(nid_hist,'temperature','Atmospheric temperature','K', &
-               nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
-               'inst(X)',t_ops,t_wrt)
-  call histdef(nid_hist,'u','Eastward Zonal Wind','m/s', &
-               nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
-               'inst(X)',t_ops,t_wrt)
-  call histdef(nid_hist,'v','Northward Meridional Wind','m/s', &
-               nbp_lon,jj_nb,nhori,klev,1,klev,zvertid,32, &
-               'inst(X)',t_ops,t_wrt)
-  call histdef(nid_hist,'ps','Surface Pressure','Pa', &
-               nbp_lon,jj_nb,nhori,1,1,1,zvertid,32, &
-               'inst(X)',t_ops,t_wrt)
-  ! end definition sequence
-  call histend(nid_hist)
-#endif
-
-#ifdef CPP_XIOS
-!XIOS
-    ! Declare available vertical axes to be used in output files:    
-    CALL wxios_add_vaxis("presnivs", klev, presnivs)
-
-    ! Declare calendar and time step
-    CALL wxios_set_cal(dtime,"earth_360d",1,1,1,0.0,1,1,1,0.0)
-    
-    !Finalize the context:
-    CALL wxios_closedef()
-#endif
-!$OMP END MASTER
-!$OMP BARRIER
-endif
-! write some outputs:
-! IOIPSL
-#ifndef CPP_IOIPSL_NO_OUTPUT 
-if (modulo(itau,iwrite_phys)==0) then
-  call histwrite_phy(nid_hist,.false.,"temperature",itau,t)
-  call histwrite_phy(nid_hist,.false.,"u",itau,u)
-  call histwrite_phy(nid_hist,.false.,"v",itau,v)
-  call histwrite_phy(nid_hist,.false.,"ps",itau,paprs(:,1))
-endif
-#endif
-
-!XIOS
-#ifdef CPP_XIOS
-!$OMP MASTER
-    !Increment XIOS time
-    CALL xios_update_calendar(itau)
-!$OMP END MASTER
-!$OMP BARRIER
-
-    !Send fields to XIOS: (NB these fields must also be defined as
-    ! <field id="..." /> in iodef.xml to be correctly used
-    CALL histwrite_phy("temperature",t)
-    CALL histwrite_phy("temp_newton",temp_newton)
-    CALL histwrite_phy("u",u)
-    CALL histwrite_phy("v",v)
-    CALL histwrite_phy("ps",paprs(:,1))
-#endif
+call output_physiqex(debut,zjulian,pdtphys,presnivs,paprs,u,v,t)
 
 ! if lastcall, then it is time to write "restartphy.nc" file
@@ -240,4 +135,5 @@
 endif
 
+
 end subroutine physiqex
 
