Index: trunk/libIGCM/libIGCM_date/libIGCM_date.ksh
===================================================================
--- trunk/libIGCM/libIGCM_date/libIGCM_date.ksh	(revision 673)
+++ trunk/libIGCM/libIGCM_date/libIGCM_date.ksh	(revision 722)
@@ -85,10 +85,10 @@
   IGCM_debug_PushStack "IGCM_date_ConvertFormatToGregorian" $@
 
-  # from a yyyy-mm-dd date format return 
+  # from a yyyy-mm-dd date format return
   # a yyymmdd date format
   # usage IGCM_date_ConvertFormat yyyy-mm-dd
 
-  # if there is no argument on the command line, 
-  # then assume that a y-m-d formated date is being 
+  # if there is no argument on the command line,
+  # then assume that a y-m-d formated date is being
   # piped in
   typeset ymd
@@ -110,10 +110,10 @@
   IGCM_debug_PushStack "IGCM_date_ConvertFormatToHuman" $@
 
-  # from a yyyymmdd date format return 
+  # from a yyyymmdd date format return
   # a yyyy-mm-dd date format
   # usage IGCM_date_ConvertFormat yyyymmdd
 
-  # if there is no argument on the command line, 
-  # then assume that a yyyymmdd formated date is being 
+  # if there is no argument on the command line,
+  # then assume that a yyyymmdd formated date is being
   # piped in
   typeset dt
@@ -137,10 +137,10 @@
   IGCM_debug_PushStack "IGCM_date_GetYearMonth" $@
 
-  # from a yyyymmdd date format return 
+  # from a yyyymmdd date format return
   # a yyyy year and mm month
   # usage IGCM_date_GetYearMonth yyyymmdd year_var month_var
 
-  # if there is no argument on the command line, 
-  # then assume that a yyyymmdd formated date is being 
+  # if there is no argument on the command line,
+  # then assume that a yyyymmdd formated date is being
   # piped in
   typeset dt
@@ -166,10 +166,10 @@
   IGCM_debug_PushStack "IGCM_date_GetYearMonthDay" $@
 
-  # from a yyyymmdd date format return 
+  # from a yyyymmdd date format return
   # a yyyy year, mm month and dd day
   # usage IGCM_date_GetYearMonthDay yyyymmdd year_var month_var day_var
 
-  # if there is no argument on the command line, 
-  # then assume that a yyyymmdd formated date is being 
+  # if there is no argument on the command line,
+  # then assume that a yyyymmdd formated date is being
   # piped in
   typeset dt
@@ -196,5 +196,5 @@
 #D-* Purpose: Return the number of days in a year
 #D-* Usage: IGCM_date_DaysInYear yyyy
-#D-         if there is no argument on the command line, 
+#D-         if there is no argument on the command line,
 #D-         then assume that a yyyy is being piped in
 #D-
@@ -237,5 +237,5 @@
   typeset y a
 
-  # if there is no argument on the command line, 
+  # if there is no argument on the command line,
   # then assume that a yyyy is being piped in
   if [ $# = 0 ]
@@ -277,5 +277,5 @@
     return
   fi
-  
+
   # if it is evenly divisible by 4 it must be a leap year
   a=$(( $y % 4 ))
@@ -304,8 +304,8 @@
 #D-#==================================================================
 #D-function IGCM_date_DaysInMonth
-#D-* Purpose: Calculate the number of days in a month 
+#D-* Purpose: Calculate the number of days in a month
 #D-* Usage: IGCM_date_DaysInMonth yyyy mm
 #D-         or IGCM_date_DaysInMonth yyyymmdd
-#D-         if there are no command line arguments then 
+#D-         if there are no command line arguments then
 #D-         assume that a yyyymmdd is being piped in and read the value.
 #D-         if there is only one argument assume it is a yyyymmdd on the command line
@@ -315,8 +315,8 @@
 #    IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@
 
-  # calculates the number of days in a month 
+  # calculates the number of days in a month
   # usage IGCM_date_DaysInMonth yyyy mm
   # or IGCM_date_DaysInMonth yyyymmdd
-  
+
   # What is the calendar :
   if ( [ "${config_UserChoices_CalendarType}" = "360d" ] || [ "${config_UserChoices_CalendarType}" = "360_day" ] ) ; then
@@ -340,5 +340,5 @@
   then
     read ymd
-  elif [ $# = 1 ] 
+  elif [ $# = 1 ]
   then
     ymd=$1
@@ -353,5 +353,5 @@
   # 30 days hath september etc.
   case $m in
-  1|3|5|7|8|10|12) 
+  1|3|5|7|8|10|12)
     if [ X$3 = X ] ; then
       echo 31
@@ -360,7 +360,7 @@
     fi
 
-#	    IGCM_debug_PopStack "IGCM_date_DaysInMonth"
+#           IGCM_debug_PopStack "IGCM_date_DaysInMonth"
     return ;;
-  4|6|9|11) 
+  4|6|9|11)
     if [ X$3 = X ] ; then
       echo 30
@@ -368,6 +368,6 @@
       eval $3=30 > /dev/null 2>&1
     fi
-    
-#	    IGCM_debug_PopStack "IGCM_date_DaysInMonth"
+
+#           IGCM_debug_PopStack "IGCM_date_DaysInMonth"
     return ;;
   *) ;;
@@ -379,5 +379,5 @@
   IGCM_date_DaysInYear $y diy
   case $diy in
-  365) 
+  365)
     if [ X$3 = X ] ; then
       echo 28
@@ -388,5 +388,5 @@
 #    IGCM_debug_PopStack "IGCM_date_DaysInMonth"
     return ;;
-  366) 
+  366)
     if [ X$3 = X ] ; then
       echo 29
@@ -419,5 +419,5 @@
 
   # if there is no command line argument, then assume that the date
-  # is coming in on a pipe and use read to collect it    
+  # is coming in on a pipe and use read to collect it
   if [ $# = 0 ]
   then
@@ -456,5 +456,5 @@
 #D-* Purpose: Convert yyyyddd to yyyymmdd
 #D-* Usage: IGCM_date_ConvertJulianDateToGregorian 1998213
-#D-         if there is no command line argument, assume one is being 
+#D-         if there is no command line argument, assume one is being
 #D-         piped in and read it
 #D-
@@ -468,5 +468,5 @@
   typeset dt y m d grg
 
-  # if there is no command line argument, assume one is being 
+  # if there is no command line argument, assume one is being
   # piped in and read it
   if [ X$1 = X ]
@@ -476,9 +476,9 @@
     dt=$1
   fi
-  
+
   # break apart the year and the days
   y=$(( $dt / 1000 ))
   d=$(( $dt % 1000 ))
-  
+
   # subtract the number of days in each month starting from 1
   # from the days in the date. When the day goes below 1, you
@@ -543,5 +543,5 @@
   IGCM_date_DaysInYear $y diy
 
-  # If the calculated day exceeds the days in the year, 
+  # If the calculated day exceeds the days in the year,
   # add one year to the year and subtract the days in the year from the
   # calculated days. Extract the days in the new year and repeat
@@ -604,5 +604,5 @@
   tmp=$( IGCM_date_AddDaysToJulianDate $tmp $dif )
   res=$( IGCM_date_ConvertJulianDateToGregorian $tmp )
-  
+
   echo $res
 
@@ -612,6 +612,6 @@
 #D-#==================================================================
 #D-function IGCM_date_DaysBetweenJulianDate
-#D-* Purpose: Calculate the days difference between two dates and reports 
-#D-           the number days as jul1 - jul2 
+#D-* Purpose: Calculate the days difference between two dates and reports
+#D-           the number days as jul1 - jul2
 #D-* Usage: IGCM_date_DaysBetweenJulianDate jul1 jul2
 #D-         where julian date is in the form yyyyddd
@@ -621,9 +621,9 @@
   IGCM_debug_PushStack "IGCM_date_DaysBetweenJulianDate" $@
 
-  # calculates the days difference between two dates and reports 
-  # the number days as jul1 - jul2 
+  # calculates the days difference between two dates and reports
+  # the number days as jul1 - jul2
   # usage IGCM_date_DaysBetweenJulianDate jul1 jul2
   # where julian date is in the form yyyyddd
-  
+
   usage () {
     echo "Usage:"
@@ -634,10 +634,10 @@
     echo " where a julian date is in the form of yyyyddd."
   }
-  
+
   if [ $# -lt 2 ]; then
     usage
     IGCM_debug_Exit "IGCM_date_DaysBetweenJulianDate"
   fi
-  
+
   # This process subtracts arg2 from arg1. If arg2 is larger
   # then reverse the arguments. The calculations are done, and
@@ -668,5 +668,5 @@
   # Then add days in year until year2 matches year1
   case ${config_UserChoices_CalendarType} in
-  360d|360_day) 
+  360d|360_day)
     res=$(( ( ( $yyyy1 - $yyyy2 ) * 360 ) + $res )) ;;
   noleap|365_day)
@@ -685,5 +685,5 @@
 
 
-  # if argument 2 was larger than argument 1 then 
+  # if argument 2 was larger than argument 1 then
   # the arguments were reversed before calculating
   # adjust by reversing the sign
@@ -701,6 +701,6 @@
 #D-#==================================================================
 #D-function IGCM_date_DaysBetweenGregorianDate ()
-#D-* Purpose: Calculate the days difference between two dates and reports 
-#D-           the number days as grg1 - grg2 
+#D-* Purpose: Calculate the days difference between two dates and reports
+#D-           the number days as grg1 - grg2
 #D-* Usage: IGCM_date_DaysBetweenGregorianDate grg1 grg2
 #D-         where gregorian date is in the form yyyymmdd
@@ -710,6 +710,6 @@
   IGCM_debug_PushStack "IGCM_date_DaysBetweenGregorianDate" $@
 
-  # calculates the days difference between two dates and reports 
-  # the number days as grg1 - grg2 
+  # calculates the days difference between two dates and reports
+  # the number days as grg1 - grg2
   # usage IGCM_date_DaysBetweenGregorianDate grg1 grg2
   # where gregorian date is in the form yyyymmdd
@@ -775,14 +775,14 @@
     IGCM_debug_Exit "IGCM_date_DaysSinceJC"
   fi
-  
+
   typeset aux num
 
   if   [ ${1} -lt  5000000 ]; then
     case ${config_UserChoices_CalendarType} in
-    360d|360_day) 
+    360d|360_day)
       aux=-360;;
     noleap|365_day)
       aux=-365;;
-    all_leap|366_day) 
+    all_leap|366_day)
       aux=-366;;
     leap|gregorian|standard)
@@ -794,9 +794,9 @@
     # which is number of days since JC and 10000101
     case ${config_UserChoices_CalendarType} in
-    360d|360_day) 
+    360d|360_day)
       aux=359640;;
     noleap|365_day)
       aux=364635;;
-    all_leap|366_day) 
+    all_leap|366_day)
       aux=365634;;
     leap|gregorian|standard)
@@ -808,9 +808,9 @@
     # which is number of days since JC and 19000101
     case ${config_UserChoices_CalendarType} in
-    360d|360_day) 
+    360d|360_day)
       aux=683640;;
     noleap|365_day)
       aux=693135;;
-    all_leap|366_day) 
+    all_leap|366_day)
       aux=695034;;
     leap|gregorian|standard)
@@ -844,9 +844,9 @@
       (( year = year - PeriodLengthInYears + 1))
     fi
-    (( Length=0 ))
-    (( i=0 ))
+    Length=0
+    i=0
     until [ $i -ge $PeriodLengthInYears ] ; do
-      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i )) ) )) 
-      (( i=i+1 ))
+      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i )) ) ))
+      (( i = i + 1 ))
     done
     ;;
@@ -857,6 +857,6 @@
     if [ X${3} = Xend ] ; then
       (( month = month - PeriodLengthInMonths + 1 ))
-    fi	    
-    (( year0 = year ))
+    fi
+    year0=year
     if [ $month -le 0 ] ; then
       (( month = month + 12 ))
@@ -864,14 +864,14 @@
     fi
     month=$( printf "%02i\n" ${month} )
-    (( Length=0 ))
-    (( i=0 ))
+    Length=0
+    i=0
     until [ $i -ge $PeriodLengthInMonths ] ; do
       if [ $(( month + i )) -lt 13 ] ; then
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
       else
-	(( year = year0 + 1 ))
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i - 12 )) ) ))
+        (( year = year0 + 1 ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i - 12 )) ) ))
       fi
-      (( i=i+1 ))
+      (( i = i + 1 ))
     done
     ;;
@@ -899,20 +899,20 @@
   Period=${2}
   case ${Period} in
-  *Y|*y) 
+  *Y|*y)
     PeriodLengthInYears=$( echo ${Period} | awk -F '[yY]' '{print $1}' )
     IGCM_date_GetYearMonth ${1} year month
     year=$( IGCM_date_SupressZeros ${year} )
-    (( Length=0 ))
-    (( i=0 ))
+    Length=0
+    i=0
     until [ $i -ge $PeriodLengthInYears ] ; do
-      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i + 1 )) ) )) 
-      (( i=i+1 ))
+      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i + 1 )) ) ))
+      (( i = i + 1 ))
     done
     ;;
-  *M|*m) 
+  *M|*m)
     PeriodLengthInMonths=$( echo ${Period} | awk -F '[mM]' '{print $1}' )
     IGCM_date_GetYearMonth ${1} year month
     year=$( IGCM_date_SupressZeros ${year} )
-    (( year0 = year ))
+    year0=year
     if [ $(( month + 1 )) -lt 13 ] ; then
       month0=$(( month + 1 ))
@@ -921,14 +921,14 @@
       (( year = year0 + 1 ))
     fi
-    (( Length=0 ))
-    (( i=0 ))
+    Length=0
+    i=0
     until [ $i -ge $PeriodLengthInMonths ] ; do
       if [ $(( month0 + i )) -lt 13 ] ; then
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i )) ) ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i )) ) ))
       else
-	(( year = year0 + 1 ))
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i - 12 )) ) ))
+        (( year = year0 + 1 ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month0 + i - 12 )) ) ))
       fi
-      (( i=i+1 ))
+      (( i = i + 1 ))
     done
     ;;
@@ -946,5 +946,5 @@
 #D-#==================================================================
 #D-function IGCM_date_DaysInCurrentPeriod ()
-#D-* Purpose: Give the numbers of days during the Current prd1 period from grg1 date 
+#D-* Purpose: Give the numbers of days during the Current prd1 period from grg1 date
 #D-* Usage: IGCM_date_DaysInCurrentPeriod grg1 prd1
 #D-         where grg1 gregorian date is in the form yyyymmdd
@@ -953,33 +953,39 @@
 function IGCM_date_DaysInCurrentPeriod {
   IGCM_debug_PushStack "IGCM_date_DaysInCurrentPeriod" $@
-  typeset Length Period
+  typeset Length Period 
   Period=${2}
   case ${Period} in
-  *Y|*y) 
+  *Y|*y)
     PeriodLengthInYears=$( echo ${Period} | awk -F '[yY]' '{print $1}' )
     IGCM_date_GetYearMonth ${1} year month
     year=$( IGCM_date_SupressZeros ${year} )
-    (( Length=0 ))
-    (( i=0 ))
+    month=$( IGCM_date_SupressZeros ${month} )
+    Length=0
+    # if starting after february and using leap calendar
+    # we need to add the number of days of the next year (potentially a leap year)
+    if [ ${month} -gt 2 ] ; then
+      (( year = year + 1 ))
+    fi
+    i=0
     until [ $i -ge $PeriodLengthInYears ] ; do
-      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i )) ) )) 
-      (( i=i+1 ))
+      (( Length = Length + $( IGCM_date_DaysInYear $(( year + i )) ) ))
+      (( i = i + 1 ))
     done
     ;;
-  *M|*m) 
+  *M|*m)
     PeriodLengthInMonths=$( echo ${Period} | awk -F '[mM]' '{print $1}' )
     IGCM_date_GetYearMonth ${1} year month
     year=$( IGCM_date_SupressZeros ${year} )
-    (( year0 = year ))
-    (( Length=0 ))
-    (( i=0 ))
+    year0=year
+    Length=0
+    i=0
     until [ $i -ge $PeriodLengthInMonths ] ; do
       if [ $(( month + i )) -lt 13 ] ; then
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i )) ) ))
       else
-	(( year = year0 + 1 ))
-	(( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i - 12 )) ) ))
+        (( year = year0 + 1 ))
+        (( Length = Length + $( IGCM_date_DaysInMonth $year $(( month + i - 12 )) ) ))
       fi
-      (( i=i+1 ))
+      (( i = i + 1 ))
     done
     ;;
@@ -1019,9 +1025,9 @@
 #---------------------
   ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh > IGCM_date_Test.ref.failed 2>&1
-  
-  if diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref > /dev/null 2>&1 ; then 
+
+  if diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref > /dev/null 2>&1 ; then
     echo "Check libIGCM_date ...............................................[ OK ]"
     rm -f IGCM_date_Test.ref.failed
-  else 
+  else
     echo "Check libIGCM_date ...........................................[ FAILED ]"
     echo "--Error--> Execution of ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh"
