Index: LMDZ6/trunk/libf/phylmd/tracco2i_mod.F90
===================================================================
--- LMDZ6/trunk/libf/phylmd/tracco2i_mod.F90	(revision 3549)
+++ LMDZ6/trunk/libf/phylmd/tracco2i_mod.F90	(revision 3581)
@@ -2,4 +2,13 @@
 !
 ! This module does the work for the interactive CO2 tracers
+! Authors: Patricia Cadule and Olivier Boucher
+!
+! Purpose and description:
+!  -----------------------
+! Main routine for the interactive carbon cycle
+! Gather all carbon fluxes and emissions from ORCHIDEE, PISCES and fossil fuel
+! Compute the net flux in source field which is used in phytrac
+! Compute global CO2 mixing ratio for radiation scheme if option is activated
+! Redistribute CO2 evenly over the atmosphere if transport is desactivated
 !
 CONTAINS
@@ -12,7 +21,10 @@
     USE infotrac_phy
     USE geometry_mod, ONLY: cell_area
+    USE carbon_cycle_mod, ONLY: carbon_cycle_init
     USE carbon_cycle_mod, ONLY: id_CO2, nbcf_in, fields_in, cfname_in
     USE carbon_cycle_mod, ONLY: fco2_ocn_day, fco2_ff, fco2_bb, fco2_land, fco2_ocean
-    USE carbon_cycle_mod, ONLY: carbon_cycle_tr, carbon_cycle_rad, RCO2_glo, RCO2_tot
+    USE carbon_cycle_mod, ONLY: fco2_land_nbp, fco2_land_nep, fco2_land_fLuc
+    USE carbon_cycle_mod, ONLY: fco2_land_fwoodharvest, fco2_land_fHarvest
+    USE carbon_cycle_mod, ONLY: carbon_cycle_cpl, carbon_cycle_tr, carbon_cycle_rad, RCO2_glo, RCO2_tot
     USE mod_grid_phy_lmdz
     USE mod_phys_lmdz_mpi_data, ONLY: is_mpi_root
@@ -54,6 +66,8 @@
     REAL, DIMENSION(klon_glo,klev) :: m_air_glo ! variable temporaire sur la grille global
 
-    INTEGER, SAVE :: mth_pre=0, day_pre=0
-!$OMP THREADPRIVATE(mth_pre, day_pre)
+    LOGICAL, SAVE :: check_fCO2_nbp_in_cfname
+!$OMP THREADPRIVATE(check_fCO2_nbp_in_cfname)
+    INTEGER, SAVE :: day_pre=-1
+!$OMP THREADPRIVATE(day_pre)
 
     IF (is_mpi_root) THEN
@@ -66,13 +80,19 @@
     IF (debutphy) THEN
 
+! Initialisation de module carbon_cycle_mod
+      IF (carbon_cycle_cpl) THEN
+        CALL carbon_cycle_init()
+      ENDIF
+
+! Initialisation de tr_seri(id_CO2) si pas initialise
       IF (MAXVAL(tr_seri(:,:,id_CO2)).LT.1.e-15) THEN 
-        !!tr_seri(:,:,id_CO2)=280.e-6/RMD*RMCO2
-        tr_seri(:,:,id_CO2)=400.e-6/RMD*RMCO2 !--initialised to 400 ppm for a test
+        tr_seri(:,:,id_CO2)=co2_ppm0*1.e-6/RMD*RMCO2 !--initialised from co2_ppm0 in rdem
       ENDIF 
 
-      ALLOCATE(fco2_ff(klon))
-      ALLOCATE(fco2_bb(klon))
-      ALLOCATE(fco2_land(klon))
-      ALLOCATE(fco2_ocean(klon))
+!--check if fCO2_nbp is in 
+      check_fCO2_nbp_in_cfname=.FALSE.
+      DO nb=1, nbcf_in
+        IF (cfname_in(nb)=="fCO2_nbp") check_fCO2_nbp_in_cfname=.TRUE.
+      ENDDO
 
     ENDIF
@@ -93,24 +113,63 @@
     fco2_land(:)=0.0
     fco2_ocean(:)=0.0
+    fco2_land_nbp(:)=0.
+    fco2_land_nep(:)=0.
+    fco2_land_fLuc(:)=0.
+    fco2_land_fwoodharvest(:)=0.
+    fco2_land_fHarvest(:)=0.
+
     DO nb=1, nbcf_in
-      print *,'nb tracco2=', nb, cfname_in(nb)
-!--fCO2_nep comes in unit of kg C m-2 s-1
-!--converting to kg CO2 m-2 s-1
-      IF (cfname_in(nb) == "fCO2_nbp" )   fco2_land(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
-!--fCO2_fgco2 comes in unit of mol C02 m-2 s-1
-!--converting to kg CO2 m-2 s-1 + change sign
-      IF (cfname_in(nb) == "fCO2_fgco2" ) fco2_ocean(:)=-1.*fco2_ocn_day(:)*RMCO2/1.e3*(pctsrf(:,is_oce)+pctsrf(:,is_sic))
+
+      SELECT CASE(cfname_in(nb))
+!--dealing with the different fluxes coming from ORCHIDEE
+!--fluxes come in unit of kg C m-2 s-1 is converted into kg CO2 m-2 s-1
+      CASE("fCO2_nep")
+          fco2_land_nep(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
+      CASE("fCO2_fLuc")
+          fco2_land_fLuc(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
+      CASE("fCO2_fwoodharvest")
+          fco2_land_fwoodharvest(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
+      CASE("fCO2_fHarvest")
+          fco2_land_fHarvest(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
+      CASE("fCO2_nbp")
+          fco2_land_nbp(:)=fields_in(:,nb)*RMCO2/RMC*pctsrf(:,is_ter)
+!--fCO2_fco2_ocn comes in unit of mol C02 m-2 s-1 is converted into kg CO2 m-2 s-1 + change sign
+      CASE("fCO2_fgco2") 
+          fco2_ocean(:)=-1.*fco2_ocn_day(:)*RMCO2/1.e3*(pctsrf(:,is_oce)+pctsrf(:,is_sic))
+      END SELECT
+
     ENDDO
 
-!--preparing the net anthropogenic flux at the surface for mixing layer
-!--unit kg CO2 / m2 / s
+!--if fCO2_nbp is transferred we use it, otherwise we use the sum of what has been passed from ORCHIDEE
+    IF (check_fCO2_nbp_in_cfname)  THEN
+       fco2_land(:)=fco2_land_nbp(:)
+    ELSE
+       fco2_land(:)=fco2_land_nep(:)+fco2_land_fLuc(:)+fco2_land_fwoodharvest(:)+fco2_land_fHarvest(:)
+    ENDIF
+
+!!--preparing the net anthropogenic flux at the surface for mixing layer
+!!--unit kg CO2 / m2 / s
+!    PRINT *, 'tracco2i_mod.F90 --- MAXVAL(fco2_ff) ',MAXVAL(fco2_ff)
+!    PRINT *, 'tracco2i_mod.F90 --- MINVAL(fco2_ff) ',MINVAL(fco2_ff)
+!    PRINT *, 'tracco2i_mod.F90 --- MAXVAL(fco2_bb) ',MAXVAL(fco2_bb)
+!    PRINT *, 'tracco2i_mod.F90 --- MINVAL(fco2_bb) ',MINVAL(fco2_bb)
+!    PRINT *, 'tracco2i_mod.F90 --- MAXVAL(fco2_land) ',MAXVAL(fco2_land)
+!    PRINT *, 'tracco2i_mod.F90 --- MINVAL(fco2_land) ',MINVAL(fco2_land)
+!    PRINT *, 'tracco2i_mod.F90 --- MAXVAL(fco2_ocean) ',MAXVAL(fco2_ocean)
+!    PRINT *, 'tracco2i_mod.F90 --- MINVAL(fco2_ocean) ',MINVAL(fco2_ocean)
+!    PRINT *, 'tracco2i_mod.F90 --- MAXVAL(source(:,id_CO2)) ',MAXVAL(source(:,id_CO2))
+!    PRINT *, 'tracco2i_mod.F90 --- MINVAL(source(:,id_CO2)) ',MINVAL(source(:,id_CO2))
+!
+!--build final source term for CO2
     source(:,id_CO2)=fco2_ff(:)+fco2_bb(:)+fco2_land(:)+fco2_ocean(:)
 
 !--computing global mean CO2 for radiation
-!--every timestep for now but enough every day
-!    IF (debutphy.OR.mth_cur.NE.mth_pre) THEN
-!    IF (debutphy.OR.day_cur.NE.day_pre) THEN
+!--for every timestep comment out the IF ENDIF statements 
+!--otherwise this is updated every day
+    IF (debutphy.OR.day_cur.NE.day_pre) THEN
+
       CALL gather(tr_seri(:,:,id_CO2),co2_glo)
       CALL gather(m_air,m_air_glo)
+
 !$OMP MASTER
 
@@ -124,5 +183,4 @@
 !$OMP END MASTER
        CALL bcast(RCO2_glo)
-       mth_pre=mth_cur
        day_pre=day_cur
 !--if not carbon_cycle_tr, then we reinitialize the CO2 each day to its global mean value
@@ -130,5 +188,5 @@
          tr_seri(:,:,id_CO2)=RCO2_glo
        ENDIF
-!    ENDIF
+    ENDIF
 
   END SUBROUTINE tracco2i
@@ -168,5 +226,7 @@
 !! may be controlled via the .def later on
 !! also co2bb for now comes from ORCHIDEE
-    LOGICAL, PARAMETER :: readco2ff=.TRUE., readco2bb=.FALSE. 
+    LOGICAL, PARAMETER :: readco2ff=.TRUE.
+!! this should be left to FALSE for now
+    LOGICAL, PARAMETER :: readco2bb=.FALSE. 
 
     CHARACTER (len = 20) :: modname = 'tracco2i.co2_emissions'
@@ -217,4 +277,5 @@
 
 !--reading CO2 biomass burning emissions
+!--using it will be inconsistent with treatment in ORCHIDEE
       IF (readco2bb) THEN 
 
