Index: branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.card
===================================================================
--- branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.card	(revision 5804)
+++ branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.card	(revision 7073)
@@ -83,9 +83,20 @@
 # This last parameter must be non-zero.
 
-# level of the history files for each iteration of the SPINUP
-level_hist=0
-# level of the history files for the final iteration of the SPINUP
+# level of the history files for each iteration of the SPINUP init stage
+level_hist_init=0
+# level of the history files for each iteration of the SPINUP transient stage
+level_hist_trans=10
+# level of the history files for the final iteration of the SPINUP final stage
 level_hist_final=10
 
+# frequency of the history files for each iteration of the SPINUP init stage
+freq_hist_init=
+# frequency of the history files for each iteration of the SPINUP transient stage
+freq_hist_trans=
+# frequency of the history files for the final iteration of the SPINUP final stage
+freq_hist_final=
+
+
+co2_init=
 
 RESOL=360x180
Index: branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.driver
===================================================================
--- branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.driver	(revision 5804)
+++ branches/ORCHIDEE_2_2/ORCHIDEE_OL/SPINUP/COMP/spinup.driver	(revision 7073)
@@ -252,4 +252,5 @@
     IGCM_card_WriteOption ${New_SUBMIT_DIR}/COMP/sechiba.card UserChoices LAND_USE ${spinup_UserChoices_land_use}
     IGCM_card_WriteOption ${New_SUBMIT_DIR}/COMP/sechiba.card UserChoices OKCO2 ${spinup_UserChoices_ok_co2}
+
 
     if [ X${spinup_UserChoices_BoundaryFilesListNonDel} = Xy ] ; then 
@@ -579,5 +580,5 @@
 	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/OOL_SEC ${ExtName}
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -601,5 +602,5 @@
 	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/OOL_SEC_STO ${ExtName}
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -631,5 +632,5 @@
 	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/TESTSTOMATE ${ExtName}
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -688,5 +689,5 @@
 	    fi
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -766,5 +767,5 @@
 	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/TESTSTOMATE ${ExtName}
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -811,12 +812,9 @@
 	    This_Job_Length_In_Years=${spinup_UserChoices_duree_carbonsol}
 	    
-	    # One execution only
-	    config_SubJob_PeriodLength="${This_Job_Length_In_Years}Y"
-
 	    # Copy the standard card set 
 	    # !!! must not have been modified !!!
-	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/FORCESOIL ${ExtName}
-
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    SPIN_Cp_Job ${spinup_UserChoices_SubJobPath}/OOL_SEC_STO ${ExtName}
+
+	    Level_Hist=${spinup_UserChoices_level_hist_init}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -824,23 +822,67 @@
 	    fi
 
-	    # Always restart for forcesoil
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card Restarts OverRule n
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG Restart y
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartDate ${output_PreviousStage_LastRestartDate}
-
-            # Define restart simulation name
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartJobName ${LastJobName}
-	    config_SRF_RestartPath=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}
-	    config_SBG_RestartPath=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartPath ${config_SBG_RestartPath}
-
-	    # We Get the forcing file from the previous run of ORCHIDEE
-	    IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/output.card PreviousStage LastRestartDate
-
-	    IGCM_card_WriteOption ${New_SUBMIT_DIR}/COMP/stomate.card BoundaryFiles ListNonDel \
-                    "(${config_SBG_RestartPath}/${LastJobName}/SBG/Restart/${LastJobName}_${output_PreviousStage_LastRestartDate}_stomate_Cforcing.nc, stomate_Cforcing.nc), \\"
-
-	    # Nb years for forcing FORCESOIL
-	    ORCHIDEE_def FORCESOIL_STEP_PER_YEAR 365
+	    ORCHIDEE_def SPINUP_ANALYTIC "y"
+
+	    SPIN_prepare
+
+	    if [ X${output_PreviousStage_LastStageName} != X"start" ] ; then
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF Restart y
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartDate ${output_PreviousStage_LastRestartDate}
+                # Define restart simulation name
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartJobName ${LastJobName}
+		config_SRF_RestartPath=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartPath ${config_SRF_RestartPath}
+
+		if [ X"${spinup_UserChoices_ok_stomate}" = "Xy" ] ; then
+		    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG Restart y
+		    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartDate ${output_PreviousStage_LastRestartDate}
+                    # Define restart simulation name
+		    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartJobName ${LastJobName}
+		    config_SBG_RestartPath=${R_OUT}/${config_UserChoices_TagName}/${config_UserChoices_SpaceName}/${config_UserChoices_ExperimentName}
+		    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartPath ${config_SBG_RestartPath}
+		fi
+	    else
+		# If we begin directly with the loop.
+		IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF Restart
+		IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF RestartDate
+		IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF RestartJobName
+		IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF RestartPath
+		IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SRF OldName
+		echo "Run in iteration loop with restarts : "
+		echo ${config_SRF_Restart}, ${config_SRF_RestartDate}, ${config_SRF_RestartJobName}, ${config_SRF_RestartPath}, ${config_SRF_OldName}
+ 
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF Restart ${config_SRF_Restart}
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartDate ${config_SRF_RestartDate}
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartJobName ${config_SRF_RestartJobName}
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF RestartPath ${config_SRF_RestartPath}
+		if [ X${config_SRF_OldName} != X"" ] ; then
+			IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF OldName ${config_SRF_OldName}
+		fi
+		if [ X"${spinup_UserChoices_ok_stomate}" = "Xy" ] ; then
+			IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG Restart
+			IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG RestartDate
+			IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG RestartJobName
+			IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG RestartPath
+			IGCM_card_DefineVariableFromOption ${SUBMIT_DIR}/config.card SBG OldName
+			echo "Run in iteration loop with STOMATE restarts : "
+			echo ${config_SBG_Restart}, ${config_SBG_RestartDate}, ${config_SBG_RestartJobName}, ${config_SBG_RestartPath}, ${config_SBG_OldName}
+
+			IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG Restart ${config_SBG_Restart}
+			IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartDate ${config_SBG_RestartDate}
+			IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartJobName ${config_SBG_RestartJobName}
+			IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG RestartPath ${config_SBG_RestartPath}
+			if [ X${config_SBG_OldName} != X"" ] ; then
+			    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG OldName ${config_SBG_OldName}
+			fi
+		fi
+	    fi
+
+	    SPIN_OptionsSechiba
+	    if [ X"${spinup_UserChoices_ok_stomate}" = "Xy" ] ; then
+		SPIN_OptionsStomate
+	    fi
+
+
+
 
 	    ;;
@@ -859,5 +901,5 @@
 	    fi
 
-	    Level_Hist=${spinup_UserChoices_level_hist}
+	    Level_Hist=${spinup_UserChoices_level_hist_trans}
 	    if [ X${Level_Hist} != X ] ; then
 		ORCHIDEE_def SECHIBA_HISTLEVEL ${Level_Hist}
@@ -955,14 +997,39 @@
     IGCM_card_WriteOption ${SUBMIT_DIR}/output.card Stage ExtName ${ExtName}
     IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card UserChoices CalendarType ${config_UserChoices_CalendarType}
-    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card UserChoices DateBegin ${config_UserChoices_DateBegin}
+
+
+    myDateBegin=$( IGCM_date_ConvertFormatToGregorian ${config_UserChoices_DateBegin} )
+    IGCM_date_GetYearMonth ${myDateBegin} UserChoices_year UserChoices_month
+    if [ ${StageName} = "HISTO" ]  ; then
+       This_Job_year=${UserChoices_year}
+    elif [ ${StageName} = "SECSTOF" ]  ; then
+       This_Job_year=$(( UserChoices_year - spinup_UserChoices_duree_final))
+    else
+       This_Job_year=$(( UserChoices_year - spinup_UserChoices_duree_final -1 ))	
+    fi
+
+    if ( [ ${StageName} != "SECSTOF" ] &&  [ ${StageName} != "HISTO" ] ) ; then
+
+	eval CO2_Init=${spinup_UserChoices_co2_init}
+	if [ X${CO2_Init} != X ] ; then
+	    if [ X${CO2_Init}=X"auto" ]; then
+		eval CO2_Init=`grep Annee_${This_Job_year} /ccc/work/cont003/igcmg/igcmg/IGCM/SRF/CO2/CO2_1860_2016_TRENDYv2017.txt | awk -F= '{print $2}'`
+	    fi
+	    ORCHIDEE_def ATM_CO2 ${CO2_Init}
+	fi
+    fi
+    
+
+    This_Job_DateBegin=$( IGCM_date_YearDigit $This_Job_year )-01-01	
+    IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card UserChoices DateBegin ${This_Job_DateBegin}
 
     # Compute DateEnd for SECHIBA initialisation step
     IGCM_date_GetYearMonth ${DateBegin} year month
     if [ X"${config_UserChoices_CalendarType}" = X"360d" ] ; then
-    	This_Job_DateEnd=$( IGCM_date_YearDigit $(( year + This_Job_Length_In_Years - 1 )) )1230
-    	This_Job_HumanDateEnd=$( IGCM_date_YearDigit $(( year + This_Job_Length_In_Years - 1 )) )-12-30
-    else
-    	This_Job_DateEnd=$( IGCM_date_YearDigit $(( year + This_Job_Length_In_Years - 1 )) )1231
-    	This_Job_HumanDateEnd=$( IGCM_date_YearDigit $(( year + This_Job_Length_In_Years - 1 )) )-12-31
+    	This_Job_DateEnd=$( IGCM_date_YearDigit $(( This_Job_year + This_Job_Length_In_Years - 1 )) )1230
+    	This_Job_HumanDateEnd=$( IGCM_date_YearDigit $(( This_Job_year + This_Job_Length_In_Years - 1 )) )-12-30
+    else
+    	This_Job_DateEnd=$( IGCM_date_YearDigit $(( This_Job_year + This_Job_Length_In_Years - 1 )) )1231
+    	This_Job_HumanDateEnd=$( IGCM_date_YearDigit $(( This_Job_year + This_Job_Length_In_Years - 1 )) )-12-31
     fi
     IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card UserChoices DateEnd ${This_Job_HumanDateEnd}
@@ -1002,4 +1069,53 @@
     fi
 
+
+    if ( [ ${StageName} = "HISTO" ] ) ; then
+	case ${spinup_UserChoices_freq_hist_final} in
+	    "low")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1Y"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1Y"
+		;;
+	    "med")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1M"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1M"
+		;;
+	    "high")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1800s"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1d"
+		;;
+	esac
+    elif ( [ ${StageName} = "SECSTOF" ] ) ; then
+	case ${spinup_UserChoices_freq_hist_trans} in
+	    "low")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1Y"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1Y"
+		;;
+	    "med")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1M"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1M"
+		;;
+	    "high")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1800s"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1d"
+		;;
+	esac
+    else
+	case ${spinup_UserChoices_freq_hist_init} in
+	    "low")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1Y"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1Y"
+		;;
+	    "med")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1M"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1M"
+		;;
+	    "high")
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SRF WriteFrequency "1800s"
+		IGCM_card_WriteOption ${New_SUBMIT_DIR}/config.card SBG WriteFrequency "1d"
+		;;
+	esac
+    fi
+
+
     # Remove the script created by the system (curie or obelix) by IGCM_sys_build_execution_scripts
     \rm ${RUN_DIR}/script_SPIN.ksh  
