Index: /LMDZ5/trunk/libf/phylmd/clesphys.h
===================================================================
--- /LMDZ5/trunk/libf/phylmd/clesphys.h	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/clesphys.h	(revision 2989)
@@ -20,5 +20,4 @@
        REAL(kind=8) CH4_ppb, N2O_ppb, CFC11_ppt, CFC12_ppt
 !IM ajout CFMIP2/CMIP5
-       LOGICAL ok_4xCO2atm
        REAL(kind=8) RCO2_per,RCH4_per,RN2O_per,RCFC11_per,RCFC12_per
        REAL(kind=8) CH4_ppb_per,N2O_ppb_per,CFC11_ppt_per,CFC12_ppt_per
@@ -124,5 +123,4 @@
      &     , iflag_con, nbapp_cv, nbapp_wk                              &
      &     , iflag_ener_conserv                                         &
-     &     , ok_4xCO2atm                                                & 
      &     , ok_suntime_rrtm                                            & 
      &     , overlap                                                    &
Index: /LMDZ5/trunk/libf/phylmd/conf_phys_m.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/conf_phys_m.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/conf_phys_m.F90	(revision 2989)
@@ -157,5 +157,4 @@
     REAL,SAVE :: CFC11_ppt_omp,RCFC11_omp,CFC11_ppt_per_omp,RCFC11_per_omp
     REAL,SAVE :: CFC12_ppt_omp,RCFC12_omp,CFC12_ppt_per_omp,RCFC12_per_omp
-    LOGICAL,SAVE :: ok_4xCO2atm_omp
     REAL,SAVE :: epmax_omp
     REAL,SAVE :: coef_epmax_cape_omp
@@ -664,7 +663,4 @@
     !Config Help = 
 
-    ok_4xCO2atm_omp = .FALSE.
-    CALL getin('ok_4xCO2atm',ok_4xCO2atm_omp)
-
     !Config Key  = RCH4_per
     !Config Desc = Concentration du CH4_per
@@ -2152,5 +2148,4 @@
     RCFC11_act = RCFC11
     RCFC12_act = RCFC12
-    ok_4xCO2atm = ok_4xCO2atm_omp
     RCO2_per = RCO2_per_omp
     RCH4_per = RCH4_per_omp
@@ -2501,5 +2496,4 @@
     write(lunout,*)' CFC11_ppt=',CFC11_ppt,' RCFC11_act=  ',RCFC11_act
     write(lunout,*)' CFC12_ppt=',CFC12_ppt,' RCFC12_act=  ',RCFC12_act
-    write(lunout,*)' ok_4xCO2atm=',ok_4xCO2atm
     write(lunout,*)' RCO2_per = ',RCO2_per,' RCH4_per = ', RCH4_per
     write(lunout,*)' RN2O_per = ',RN2O_per,' RCFC11_per = ', RCFC11_per
Index: /LMDZ5/trunk/libf/phylmd/iophy.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/iophy.F90	(revision 2989)
@@ -459,5 +459,6 @@
     USE mod_phys_lmdz_para, ONLY: jj_nb
     USE phys_output_var_mod, ONLY: type_ecri, zoutm, zdtime_moy, lev_files, &
-                                   nid_files, nhorim, swaero_diag, dryaod_diag, nfiles
+                                   nid_files, nhorim, swaero_diag, dryaod_diag, nfiles, &
+                                   ok_4xCO2atm
     USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
     USE aero_mod, ONLY : naero_tot, name_aero_tau
@@ -517,4 +518,13 @@
     ENDDO
 
+    ! Set ok_4xCO2atm=true if at least one of the concerned variables are
+    ! defined
+    IF (nomvar=='rsut4co2'.OR.nomvar=='rlut4co2'.OR.nomvar=='rsutcs4co2' & 
+        .OR. nomvar=='rlutcs4co2'.OR.nomvar=='rsu4co2'.OR.nomvar=='rsucs4co2' &
+        .OR.nomvar=='rsu4co2'.OR.nomvar=='rsucs4co2'.OR.nomvar=='rsd4co2'.OR. &
+        nomvar=='rsdcs4co2'.OR.nomvar=='rlu4co2'.OR.nomvar=='rlucs4co2'.OR.&
+        nomvar=='rld4co2'.OR.nomvar=='rldcs4co2') THEN
+        IF ( flag_var(iff)<=lev_files(iff) ) ok_4xCO2atm=.TRUE.
+    ENDIF 
   END SUBROUTINE histdef2d_old
 
@@ -573,5 +583,6 @@
     USE phys_output_var_mod, ONLY: ctrl_out, type_ecri_files, zoutm, zdtime_moy, &
                                    clef_stations, phys_out_filenames, lev_files, &
-                                   nid_files, nhorim, swaero_diag, dryaod_diag
+                                   nid_files, nhorim, swaero_diag, dryaod_diag,&
+                                   ok_4xCO2atm 
     USE print_control_mod, ONLY: prt_level,lunout 
     USE mod_grid_phy_lmdz, ONLY : nbp_lon, nbp_lat
@@ -663,4 +674,13 @@
       ENDIF
     ENDDO
+    ! Set ok_4xCO2atm=true if at least one of the concerned variables are
+    ! defined
+    IF (var%name=='rsut4co2'.OR.var%name=='rlut4co2'.OR.var%name=='rsutcs4co2' & 
+        .OR. var%name=='rlutcs4co2'.OR.var%name=='rsu4co2'.OR.var%name=='rsucs4co2' &
+        .OR.var%name=='rsu4co2'.OR.var%name=='rsucs4co2'.OR.var%name=='rsd4co2'.OR. &
+        var%name=='rsdcs4co2'.OR.var%name=='rlu4co2'.OR.var%name=='rlucs4co2'.OR.&
+        var%name=='rld4co2'.OR.var%name=='rldcs4co2') THEN
+        IF ( var%flag(iff)<=lev_files(iff) ) ok_4xCO2atm=.TRUE.
+    ENDIF 
   END SUBROUTINE histdef2d
 
Index: /LMDZ5/trunk/libf/phylmd/phys_output_mod.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/phys_output_mod.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/phys_output_mod.F90	(revision 2989)
@@ -520,4 +520,5 @@
       WRITE(lunout,*)'swaero_diag=',swaero_diag
       WRITE(lunout,*)'dryaod_diag=',dryaod_diag
+      WRITE(lunout,*)'ok_4xCO2atm=',ok_4xCO2atm
       WRITE(lunout,*)'phys_output_open: ends here'
     ENDIF
Index: /LMDZ5/trunk/libf/phylmd/phys_output_var_mod.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/phys_output_var_mod.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/phys_output_var_mod.F90	(revision 2989)
@@ -92,4 +92,12 @@
   LOGICAL, SAVE                                :: dryaod_diag=.TRUE.
   !$OMP THREADPRIVATE(swaero_diag, dryaod_diag)
+  ! ok_4xCO2atm : flag indicates if it is necessary to do a second call of
+  ! radiation code with a 4xCO2 or another different GES to assess SW/LW
+  ! in this case
+  !--IM: as for swaero_diag or dryaod_diag this needs to be set to TRUE by default and
+  !--    changed back to FALSE after first radiation call and corrected back to TRUE 
+  !--    based on output requests
+  LOGICAL, SAVE                                :: ok_4xCO2atm=.FALSE.
+  !$OMP THREADPRIVATE(ok_4xCO2atm)
 
   INTEGER, SAVE:: levmin(nfiles) = 1
Index: /LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 2989)
@@ -320,5 +320,7 @@
          map_emis_ThCi,map_pcld_ThCi,map_tcld_ThCi,&
          map_emis_Anv,map_pcld_Anv,map_tcld_Anv, &
-         alt_tropo
+         alt_tropo, &
+!Ionela
+         ok_4xCO2atm
 
     USE ocean_slab_mod, ONLY: nslay, tslab, slab_bils, slab_bilg, tice, &
@@ -1733,7 +1735,6 @@
        CALL histwrite_phy(o_ref_liq, ref_liq)
        CALL histwrite_phy(o_ref_ice, ref_ice)
-       IF (RCO2_per.NE.RCO2_act.OR.RCH4_per.NE.RCH4_act.OR. &
-            RN2O_per.NE.RN2O_act.OR.RCFC11_per.NE.RCFC11_act.OR. &
-            RCFC12_per.NE.RCFC12_act) THEN
+!
+       IF (ok_4xCO2atm) THEN
           IF (vars_defined) zx_tmp_fi2d(:) = swupp(:,klevp1)*swradcorr(:)
           CALL histwrite_phy(o_rsut4co2, zx_tmp_fi2d)
@@ -1764,5 +1765,5 @@
           CALL histwrite_phy(o_rld4co2, lwdnp)
           CALL histwrite_phy(o_rldcs4co2, lwdn0p)
-       ENDIF
+       ENDIF !ok_4xCO2atm
 !!!!!!!!!!!! Sorties niveaux de pression NMC !!!!!!!!!!!!!!!!!!!!
 #ifdef CPP_IOIPSL
Index: /LMDZ5/trunk/libf/phylmd/physiq_mod.F90
===================================================================
--- /LMDZ5/trunk/libf/phylmd/physiq_mod.F90	(revision 2988)
+++ /LMDZ5/trunk/libf/phylmd/physiq_mod.F90	(revision 2989)
@@ -1610,4 +1610,13 @@
          IF (xios_field_is_active("dryod550_"//name_aero_tau(naero))) dryaod_diag=.TRUE. 
        ENDDO
+       !
+       !--setting up ok_4xCO2atm to TRUE in XIOS case 
+       IF (xios_field_is_active("rsut4co2").OR.xios_field_is_active("rlut4co2").OR. & 
+           xios_field_is_active("rsutcs4co2").OR.xios_field_is_active("rlutcs4co2").OR. &
+           xios_field_is_active("rsu4co2").OR.xios_field_is_active("rsucs4co2").OR. &
+           xios_field_is_active("rsd4co2").OR.xios_field_is_active("rsdcs4co2").OR. &
+           xios_field_is_active("rlu4co2").OR.xios_field_is_active("rlucs4co2").OR. &
+           xios_field_is_active("rld4co2").OR.xios_field_is_active("rldcs4co2")) &
+           ok_4xCO2atm=.TRUE. 
 #endif 
 
@@ -3725,4 +3734,7 @@
           IF (debut) swaero_diag = .FALSE.
           IF (debut) dryaod_diag = .FALSE.
+          !--IM 15/09/2017 here we return ok_4xCO2atm to FALSE
+          !--as for swaero_diag, see above
+          IF (debut) ok_4xCO2atm = .FALSE.
 #endif
           !
@@ -3731,8 +3743,11 @@
           !IM Par defaut on a les taux perturbes egaux aux taux actuels
           !
+          IF (RCO2_per.NE.RCO2_act.OR. &
+              RCH4_per.NE.RCH4_act.OR. &
+              RN2O_per.NE.RN2O_act.OR. &
+              RCFC11_per.NE.RCFC11_act.OR. &
+              RCFC12_per.NE.RCFC12_act) ok_4xCO2atm =.TRUE. 
+   ! 
           IF (ok_4xCO2atm) THEN
-             IF (RCO2_per.NE.RCO2_act.OR.RCH4_per.NE.RCH4_act.OR.     &
-                 RN2O_per.NE.RN2O_act.OR.RCFC11_per.NE.RCFC11_act.OR. &
-                 RCFC12_per.NE.RCFC12_act) THEN
                 !
                 RCO2 = RCO2_per
@@ -3782,5 +3797,4 @@
                      ZLWFT0_i, ZFLDN0, ZFLUP0, &
                      ZSWFT0_i, ZFSDN0, ZFSUP0)
-             endif
           endif
           !
