Index: trunk/libIGCM/AA_create_ts
===================================================================
--- trunk/libIGCM/AA_create_ts	(revision 18)
+++ trunk/libIGCM/AA_create_ts	(revision 29)
@@ -241,9 +241,9 @@
 	FlagDir=$( echo ${FILE} | awk -F "_" '{print $1}' )
 	case ${FlagDir} in
-	    1Y)    TS_Dir=TS_YE  ;;
-	    1M)    TS_Dir=TS_MO  ;;
-	    5D|1D) TS_Dir=TS_DA  ;;
-	    HF)    TS_Dir=TS_HF  ;;
-	    INS)   TS_Dir=TS_INS ;;
+	    *Y)  TS_Dir=TS_YE  ;; 
+	    *M)  TS_Dir=TS_MO  ;; 
+	    *D)  TS_Dir=TS_DA  ;; 
+	    HF)  TS_Dir=TS_HF  ;;
+	    INS) TS_Dir=TS_INS ;;
 	esac
 
@@ -297,19 +297,41 @@
     IGCM_debug_PushStack "create_ts_next_date" $@
     typeset year month Length
-    case ${1} in
-	1Y|1y)
-	    IGCM_date_GetYearMonth ${2} year month
-	    Length=$( IGCM_date_DaysInYear $(( year + 1 )) ) ;;
-	1M|1m)
-	    IGCM_date_GetYearMonth ${2} year month
-	    if [ $month -lt 12 ] ; then
-		Length=$( IGCM_date_DaysInMonth $year $(( month + 1 )) )
+    case ${config_UserChoices_PeriodLength} in
+	*Y|*y) 
+	    PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
+	    IGCM_date_GetYearMonth ${1} year month
+	    (( Length=0 ))
+	    (( i=0 ))
+	    until [ $i -ge $PeriodLengthInYears ] ; do
+		(( Length = Length + $( IGCM_date_DaysInYear $(( year + i + 1 )) ) )) 
+	        (( i=i+1 ))
+	    done
+	    ;;
+	*M|*m) 
+	    PeriodLengthInMonths=$( echo ${config_UserChoices_PeriodLength} | awk -F '[mM]' '{print $1}' )
+	    IGCM_date_GetYearMonth ${1} year month
+	    (( year0=year ))
+	    if [ $(( month + 1 )) -lt 13 ] ; then
+		month0=$(( month + 1 ))
 	    else
-		Length=$( IGCM_date_DaysInMonth $(( year + 1 )) 1 )
-	    fi ;;
+		month0=$(( month + 1 - 12 ))
+		(( year = year0 + 1 ))
+	    fi
+	    (( Length=0 ))
+	    (( i=0 ))
+	    until [ $i -ge $PeriodLengthInMonths ] ; do
+		if [ $(( month0 + i )) -lt 13 ] ; then
+		    (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i )) ) ))
+		else
+		    (( year = year0 + 1 ))
+		    (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i - 12 )) ) ))
+		fi
+	        (( i=i+1 ))
+	    done
+	    ;;
 	*D|*d)
-	    Length=$( echo ${1} | sed -e "s/[dD]//" ) ;;
+	    Length=$( echo ${config_UserChoices_PeriodLength} | sed -e "s/[dD]//" ) ;;
 	*)
-	    IGCM_debug_Exit "create_ts " ${1} " invalid PeriodLength : choose in 1Y, 1M, 5D, 1D."
+	    IGCM_debug_Exit "create_ts " ${config_UserChoices_PeriodLength} " invalid PeriodLength : choose in 1Y, *M, *D."
 	    IGCM_debug_Verif_Exit_Post ;;
     esac
@@ -323,15 +345,46 @@
     IGCM_debug_PushStack "create_ts_begin_date" $@
     typeset year month Length
-    case ${1} in
-	1Y|1y)
-	    IGCM_date_GetYearMonth ${2} year month
-	    Length=$( IGCM_date_DaysInYear $year ) ;;
-	1M|1m)
-	    IGCM_date_GetYearMonth ${2} year month
-	    Length=$( IGCM_date_DaysInMonth $year $month ) ;;
+    case ${config_UserChoices_PeriodLength} in
+	*Y|*y) 
+	    PeriodLengthInYears=$( echo ${config_UserChoices_PeriodLength} | awk -F '[yY]' '{print $1}' )
+	    IGCM_date_GetYearMonth ${1} year month
+	    if [ X${2} = Xend ] ; then
+		(( year = year - PeriodLengthInYears + 1))
+	    fi
+	    (( Length=0 ))
+	    (( i=0 ))
+	    until [ $i -ge $PeriodLengthInYears ] ; do
+		(( Length = Length + $( IGCM_date_DaysInYear $(( year + i )) ) )) 
+	        (( i=i+1 ))
+	    done
+	    ;;
+	*M|*m) 
+	    PeriodLengthInMonths=$( echo ${config_UserChoices_PeriodLength} | awk -F '[mM]' '{print $1}' )
+	    IGCM_date_GetYearMonth ${1} year month
+	    if [ X${2} = Xend ] ; then
+		(( month = $month - $PeriodLengthInMonths + 1 ))
+	    fi	    
+	    (( year0=$year ))
+	    if [ $month -le 0 ] ; then
+		(( month = $month + 12 ))
+		year=$( printf "%04i\n" $(( year - 1 )) )
+	    fi
+	    month=$( printf "%02i\n" ${month} )
+	    (( Length=0 ))
+	    (( i=0 ))
+	    until [ $i -ge $PeriodLengthInMonths ] ; do
+		if [ $(( month + i )) -lt 13 ] ; then
+		    (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
+		else
+		    (( year = year0 + 1 ))
+		    (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i - 12 )) ) ))
+		fi
+	        (( i=i+1 ))
+	    done
+	    ;;
 	*D|*d)
-	    Length=$( echo ${1} | sed -e "s/[dD]//" ) ;;
+	    Length=$( echo ${config_UserChoices_PeriodLength} | sed -e "s/[dD]//" ) ;;
 	*)
-	    IGCM_debug_Exit "create_ts " ${1} " invalid PeriodLength : choose in 1Y, 1M, 5D, 1D."
+	    IGCM_debug_Exit "create_ts " ${config_UserChoices_PeriodLength} " invalid PeriodLength : choose in 1Y, *M, *D."
 	    IGCM_debug_Verif_Exit_Post ;;
     esac
@@ -375,5 +428,5 @@
     # First Time Series Submission
     FIRST_PASS=TRUE
-    Length=$( create_ts_begin_date ${config_UserChoices_PeriodLength} ${DateBegin} )
+    Length=$( create_ts_begin_date ${DateBegin} begin)
     DATE_FIN_JOB_B=$( IGCM_date_AddDaysToGregorianDate ${DateBegin} $(( Length - 1 )) )
 else
@@ -402,9 +455,9 @@
 	    FlagDir=$( echo ${file} | awk -F "_" '{print $1}' )
 	    case ${FlagDir} in
-		1Y)    TS_Dir=TS_YE  ;;
-		1M)    TS_Dir=TS_MO  ;;
-		5D|1D) TS_Dir=TS_DA  ;;
-		HF)    TS_Dir=TS_HF  ;;
-		INS)   TS_Dir=TS_INS ;;
+	    	*Y)  TS_Dir=TS_YE  ;;
+	    	*M)  TS_Dir=TS_MO  ;;
+	    	*D)  TS_Dir=TS_DA  ;;
+		HF)  TS_Dir=TS_HF  ;;
+		INS) TS_Dir=TS_INS ;;
 	    esac
 	    for var in $( eval echo \${LISTE_VARS_${file}[*]} ) ; do
@@ -438,5 +491,5 @@
     while [ ${DATE_COUNT} -lt ${PeriodDateEnd} ] ; do
 	(( NBRE_FILE_TOT = NBRE_FILE_TOT + 1 ))
-	Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_COUNT} )
+	Length=$( create_ts_next_date ${DATE_COUNT} )
 	DATE_COUNT=$( IGCM_date_AddDaysToGregorianDate ${DATE_COUNT} ${Length} )
     done
@@ -469,5 +522,5 @@
 	    DATE_COURANTE=${DATE_FIN_JOB_B_LOOP}
 	else
-	    Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_FIN_JOB_B_LOOP} )
+	    Length=$( create_ts_next_date ${DATE_FIN_JOB_B_LOOP} )
 	    DATE_COURANTE=$( IGCM_date_AddDaysToGregorianDate ${DATE_FIN_JOB_B_LOOP} ${Length} )
 	fi
@@ -486,10 +539,10 @@
 
         if [ ! ${FIRST_PASS} = TRUE ] && [ ${CURRENT_LOOP} -eq 1 ] ; then
-	    Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_FIN_JOB_B_LOOP} )
+	    Length=$( create_ts_next_date ${DATE_FIN_JOB_B_LOOP} )
 	    DATE_COURANTE=$( IGCM_date_AddDaysToGregorianDate ${DATE_FIN_JOB_B_LOOP} ${Length} )
 	elif [ -z "${DATE_COURANTE}" ] ; then
 	    DATE_COURANTE=${DATE_FIN_JOB_B}
 	else
-	    Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_FIN} )
+	    Length=$( create_ts_next_date ${DATE_FIN} )
 	    DATE_COURANTE=$( IGCM_date_AddDaysToGregorianDate ${DATE_FIN} ${Length} )
 	fi
@@ -501,5 +554,5 @@
 	while [ ${COMPTEUR} -lt ${NBRE_FILE_LOOP} ] ; do
 	    #
-	    Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_LOOP} )
+	    Length=$( create_ts_next_date ${DATE_LOOP} )
 	    DATE_LOOP=$( IGCM_date_AddDaysToGregorianDate ${DATE_LOOP} ${Length} )
 	    (( TotLength = TotLength + Length ))
@@ -518,5 +571,5 @@
 	[ ${NBRE_FILE_LOOP} -eq 0 ] && NBRE_FILE_LOOP=${UNIX_MAX_LIMIT}
 
-	Length=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_FIN} )
+	Length=$( create_ts_next_date ${DATE_FIN} )
 	DATE_COURANTE=$(IGCM_date_AddDaysToGregorianDate ${DATE_FIN} ${Length} )
 	DATE_FIN=${PeriodDateEnd}
@@ -546,6 +599,6 @@
 	    FlagDir=$( echo ${file} | awk -F "_" '{print $1}' )
 	    case ${FlagDir} in
-		1Y)    TS_Dir=TS_YE  ; FreqDir=AN  ;;
-		1M)    TS_Dir=TS_MO  ; FreqDir=MO  ;;
+		*Y)    TS_Dir=TS_YE  ; FreqDir=YE  ;;
+		*M)    TS_Dir=TS_MO  ; FreqDir=MO  ;;
 		*D)    TS_Dir=TS_DA  ; FreqDir=DA  ;;
 		HF)    TS_Dir=TS_HF  ; FreqDir=HF  ;;
@@ -567,8 +620,8 @@
 	    while [ ${COMPTEUR} -le ${NBRE_FILE_LOOP} ] ; do
 		#
-		Length1=$( create_ts_begin_date ${config_UserChoices_PeriodLength} ${DATE_LOOP} )
+		Length1=$( create_ts_begin_date ${DATE_LOOP} end)
 	        DATE_TAB=$(  IGCM_date_AddDaysToGregorianDate ${DATE_LOOP} $(( 1 - Length1 )) )_${DATE_LOOP}
 
-		Length2=$( create_ts_next_date ${config_UserChoices_PeriodLength} ${DATE_LOOP} )
+		Length2=$( create_ts_next_date ${DATE_LOOP} end)
 		DATE_LOOP=$( IGCM_date_AddDaysToGregorianDate ${DATE_LOOP} ${Length2} )
 
@@ -596,5 +649,5 @@
 	    # WE CAN CONCATENATE FILES AT THIS POINT
 	    [ "${FLAG_B}" = "TRUE" ] && DATE_BUILD_B=${DateBegin}_${DATE_FIN_JOB_B_LOOP_PREC}
-	    Length=$( create_ts_begin_date ${config_UserChoices_PeriodLength} ${DATE_COURANTE} )
+	    Length=$( create_ts_begin_date ${DATE_COURANTE} end)
 	    DATE_BUILD1=$( IGCM_date_AddDaysToGregorianDate ${DATE_COURANTE} -$(( Length - 1 )) )
 	    DATE_BUILD=${DATE_BUILD1}_${DATE_FIN}
