Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libIGCM/libIGCM_date/libIGCM_date.ksh

    r23 r7  
    153153function IGCM_date_DaysInYear 
    154154{ 
    155 #    IGCM_debug_PushStack "IGCM_date_DaysInYear" $@ 
     155    IGCM_debug_PushStack "IGCM_date_DaysInYear" $@ 
    156156    # return the number of days in a year 
    157157    # usage IGCM_date_DaysInYear yyyy 
     
    159159    # What is the calendar : 
    160160    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then 
    161         if [ X$2 = X ] ; then 
    162             echo 360 
    163         else 
    164             eval $2=360 > /dev/null 2>&1 
    165         fi 
    166 #       IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     161        echo 360 
     162        IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    167163        return 
    168164    else 
    169165        if [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then 
    170             if [ X$2 = X ] ; then 
    171                 echo 365 
    172             else 
    173                 eval $2=365 > /dev/null 2>&1 
    174             fi 
    175  
    176 #           IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     166            echo 365 
     167            IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    177168            return 
    178169        fi 
    179170    fi 
    180  
    181     typeset y a 
    182171 
    183172    # if there is no argument on the command line,  
     
    195184 
    196185    # if it is evenly divisible by 400 it must be a leap year 
    197     a=$(( $y % 400 )) 
     186    a=$( expr $y \% 400 ) 
    198187    if [ $a = 0 ] 
    199188        then 
    200         if [ X$2 = X ] ; then 
    201             echo 366 
    202         else 
    203             eval $2=366 > /dev/null 2>&1 
    204         fi 
    205  
    206 #       IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     189        echo 366 
     190        IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    207191        return 
    208192    fi 
    209193 
    210194    #if it is evenly divisible by 100 it must not be a leap year 
    211     a=$(( $y % 100 )) 
     195    a=$( expr $y \% 100 ) 
    212196    if [ $a = 0 ] 
    213197        then 
    214         if [ X$2 = X ] ; then 
    215             echo 365 
    216         else 
    217             eval $2=365 > /dev/null 2>&1 
    218         fi 
    219  
    220 #       IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     198        echo 365 
     199        IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    221200        return 
    222201    fi 
    223202         
    224203    # if it is evenly divisible by 4 it must be a leap year 
    225     a=$(( $y % 4 )) 
     204    a=$( expr $y \% 4 ) 
    226205    if [ $a = 0 ] 
    227206        then 
    228         if [ X$2 = X ] ; then 
    229             echo 366 
    230         else 
    231             eval $2=366 > /dev/null 2>&1 
    232         fi 
    233  
    234 #       IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     207        echo 366 
     208        IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    235209        return 
    236210    fi 
    237211 
    238212    # otherwise it is not a leap year 
    239     if [ X$2 = X ] ; then 
    240         echo 365 
    241     else 
    242         eval $2=365 > /dev/null 2>&1 
    243     fi 
    244  
    245 #    IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     213    echo 365 
     214 
     215    IGCM_debug_PopStack "IGCM_date_DaysInYear" 
    246216} 
    247217 
     
    257227function IGCM_date_DaysInMonth 
    258228{ 
    259 #    IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@ 
     229    IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@ 
    260230 
    261231    # calculates the number of days in a month  
    262232    # usage IGCM_date_DaysInMonth yyyy mm 
    263233    # or IGCM_date_DaysInMonth yyyymmdd 
    264      
     234 
    265235    # What is the calendar : 
    266236    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then 
    267         if [ X$3 = X ] ; then 
    268             echo 30 
    269         else 
    270             eval $3=30 > /dev/null 2>&1 
    271         fi 
    272  
    273 #       IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     237        echo 30 
     238        IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    274239        return 
    275240    fi 
    276  
    277     typeset ymd y m diy 
    278241 
    279242    # if there are no command line arguments then assume that a yyyymmdd is being 
     
    288251        ymd=$1 
    289252    else 
    290         ymd=$(( ( $1 * 10000 ) + ( $2 * 100 ) + 1 )) 
     253        ymd=$( expr \( \( $1 \* 10000 \) \+ \( $2 \* 100 \) \+ 1 \) ) 
    291254    fi 
    292255 
    293256    # extract the year and the month 
    294     y=$(( $ymd / 10000 )) ; 
    295     m=$(( ( $ymd % 10000 ) / 100 )) ; 
     257    y=$( expr $ymd / 10000 ) ; 
     258    m=$( expr \( $ymd \% 10000 \) / 100 ) ; 
    296259 
    297260    # 30 days hath september etc. 
    298261    case $m in 
    299262        1|3|5|7|8|10|12)  
    300             if [ X$3 = X ] ; then 
    301                 echo 31 
    302             else 
    303                 eval $3=31 > /dev/null 2>&1 
    304             fi 
    305  
    306 #           IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     263            echo 31 
     264            IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    307265            return ;; 
    308266        4|6|9|11)  
    309             if [ X$3 = X ] ; then 
    310                 echo 30 
    311             else 
    312                 eval $3=30 > /dev/null 2>&1 
    313             fi 
    314          
    315 #           IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     267            echo 30      
     268            IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    316269            return ;; 
    317270        *) ;; 
     
    321274    # Use IGCM_date_DaysInYear to get the number of days in the year and return a value 
    322275    # accordingly. 
    323     IGCM_date_DaysInYear $y diy 
     276    diy=$( IGCM_date_DaysInYear $y ) 
    324277    case $diy in 
    325278        365)  
    326             if [ X$3 = X ] ; then 
    327                 echo 28 
    328             else 
    329                 eval $3=28 > /dev/null 2>&1 
    330             fi 
    331  
    332 #           IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     279            echo 28 
     280            IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    333281            return ;; 
    334282        366)  
    335             if [ X$3 = X ] ; then 
    336                 echo 29 
    337             else 
    338                 eval $3=29 > /dev/null 2>&1 
    339             fi 
    340  
    341 #           IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     283            echo 29 
     284            IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    342285            return ;; 
    343286    esac 
    344287 
    345 #    IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     288    IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    346289} 
    347290 
     
    360303    # usage IGCM_date_ConvertGregorianDateToJulian 19980429 
    361304 
    362     typeset dt y m d x jul 
    363  
    364305    # if there is no command line argument, then assume that the date 
    365306    # is coming in on a pipe and use read to collect it     
     
    372313 
    373314    # break the yyyymmdd into separate parts for year, month and day 
    374     y=$(( $dt / 10000 )) 
    375     m=$(( ( $dt % 10000 ) / 100 )) 
    376     d=$(( ( $dt % 100 ) )) 
     315    y=$( expr $dt / 10000 ) 
     316    m=$( expr \( $dt \% 10000 \) / 100 ) 
     317    d=$( expr \( $dt \% 100 \) ) 
    377318 
    378319    # add the days in each month up to (but not including the month itself) 
     
    382323    # and add them to 14. 
    383324    x=1 
    384     while [ $x -lt $m ] 
     325    while [ $( expr $x \< $m ) = 1 ] 
    385326      do 
    386       IGCM_date_DaysInMonth $y $x md 
    387       d=$(( $d + $md )) 
    388       x=$(( $x + 1 )) 
     327      md=$( IGCM_date_DaysInMonth $y $x ) 
     328      d=$( expr \( $d \+ $md \) ) 
     329      x=$( expr \( $x \+ 1 \) ) 
    389330    done 
    390331 
    391332    # combine the year and day back together again and you have the julian date. 
    392     jul=$(( ( $y * 1000 ) + $d )) 
     333    jul=$( expr \( $y \* 1000 \) + $d ) 
    393334    echo $jul 
    394335 
     
    410351    # usage IGCM_date_ConvertJulianDateToGregorian 1998213 
    411352 
    412     typeset dt y m d md grg 
    413  
    414353    # if there is no command line argument, assume one is being  
    415354    # piped in and read it 
     
    422361         
    423362    # break apart the year and the days 
    424     y=$(( $dt / 1000 )) 
    425     d=$(( $dt % 1000 )) 
     363    y=$( expr $dt / 1000 ) 
     364    d=$( expr $dt \% 1000 ) 
    426365         
    427366    # subtract the number of days in each month starting from 1 
     
    430369    # month to get the correct day of the month 
    431370    m=1 
    432     while [ $d -gt 0 ] 
     371    while [ $( expr $d \> 0 ) = 1 ] 
    433372      do 
    434       IGCM_date_DaysInMonth $y $m md 
    435       d=$(( $d - $md )) 
    436       m=$(( $m + 1 )) 
     373      md=$( IGCM_date_DaysInMonth $y $m ) 
     374      d=$( expr $d \- $md ) 
     375      m=$( expr $m \+ 1 ) 
    437376    done 
    438377 
    439     d=$(( $d + $md )) 
     378    d=$( expr $d \+ $md ) 
    440379 
    441380    # the loop steps one past the correct month, so back up the month 
    442     m=$(( $m - 1 )) 
     381    m=$( expr $m \- 1 ) 
    443382 
    444383    # assemble the results into a gregorian date 
    445     grg=$(( ( $y * 10000 ) + ( $m * 100 ) + $d )) 
     384    grg=$( expr \( $y \* 10000 \) \+ \( $m \* 100 \) \+ $d ) 
    446385    echo $( IGCM_date_GregorianDigit $grg ) 
    447386 
     
    463402    # usage IGCM_date_AddDaysToJulianDate 1998312 { ,-}14 
    464403 
    465     typeset dif yd d y diy 
    466  
    467404    # Read the difference from the command lines 
    468405    # and the date from the command line, or standard input 
     
    477414 
    478415    # Break it into pieces 
    479     d=$(( $yd % 1000 )) 
    480     y=$(( $yd / 1000 )) 
     416    d=$( expr $yd \% 1000 ) 
     417    y=$( expr $yd / 1000 ) 
    481418 
    482419    # Add the number of days (if days is negative this results is 
    483420    # a subtraction) 
    484     d=$(( $d + $dif )) 
     421    d=$( expr $d \+ $dif ) 
    485422 
    486423    # Extract the days in the year 
    487     IGCM_date_DaysInYear $y diy 
     424    diy=$( IGCM_date_DaysInYear $y ) 
    488425 
    489426    # If the calculated day exceeds the days in the year,  
     
    492429    # test until you end up with a day number that falls within the 
    493430    # days of the year 
    494     while [ $d -gt $diy ] 
     431    while [ $( expr $d \> $diy ) = 1 ] 
    495432      do 
    496       d=$(( $d - $diy )) 
    497       y=$(( $y + 1 )) 
    498       IGCM_date_DaysInYear $y diy 
     433      d=$( expr $d - $diy ) 
     434      y=$( expr $y \+ 1 ) 
     435      diy=$( IGCM_date_DaysInYear $y ) 
    499436    done 
    500437 
     
    504441    # loop on this test until you end up with a number that 
    505442    # falls within the days of the year 
    506     while [ $d -lt 1 ] 
     443    while [ $( expr $d \< 1 ) = 1 ] 
    507444      do 
    508       y=$(( $y - 1 )) 
    509       IGCM_date_DaysInYear $y diy 
    510       d=$(( $d + $diy )) 
     445      y=$( expr $y - 1 ) 
     446      diy=$( IGCM_date_DaysInYear $y ) 
     447      d=$( expr $d \+ $diy ) 
    511448    done 
    512449 
    513450    # put the year and day back together and echo the result 
    514     yd=$(( ( $y * 1000 ) + $d )) 
     451    yd=$( expr \( $y \* 1000 \) + $d ) 
    515452 
    516453    echo $yd 
     
    602539 
    603540    # Break the dates in to year and day portions 
    604     yyyy1=$(( $jul1 / 1000 )) 
    605     yyyy2=$(( $jul2 / 1000 )) 
    606     ddd1=$(( $jul1 % 1000 )) 
    607     ddd2=$(( $jul2 % 1000 )) 
     541    yyyy1=$( expr $jul1 / 1000 ) 
     542    yyyy2=$( expr $jul2 / 1000 ) 
     543    ddd1=$( expr $jul1 \% 1000 ) 
     544    ddd2=$( expr $jul2 \% 1000 ) 
    608545 
    609546    # Subtract days 
    610     res=$(( $ddd1 - $ddd2 )) 
     547    res=$( expr $ddd1 - $ddd2 ) 
    611548 
    612549    # Then add days in year until year2 matches year1 
    613550 
    614551    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then 
    615         res=$(( ( ( $yyyy1 - $yyyy2 ) * 360 ) + $res )) 
     552        res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 360 \) \+ $res ) 
    616553    elif [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then 
    617         res=$(( ( ( $yyyy1 - $yyyy2 ) * 365 ) + $res )) 
     554        res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 365 \) \+ $res ) 
    618555    elif ( [ "${config_UserChoices_CalendarType}" = "leap" ] || [ "${config_UserChoices_CalendarType}" = "gregorian" ] ) ; then 
    619556        while [ $yyyy2 -lt $yyyy1 ] 
    620557          do 
    621           IGCM_date_DaysInYear $yyyy2 diy 
    622           res=$(( $res + $diy )) 
    623           yyyy2=$(( $yyyy2 + 1 )) 
     558          diy=$( IGCM_date_DaysInYear $yyyy2 ) 
     559          res=$( expr $res + $diy ) 
     560          yyyy2=$( expr $yyyy2 + 1 ) 
    624561        done 
    625562    fi 
     
    630567    if [ $1 -lt $2 ] 
    631568        then 
    632         res=$(( $res * -1 )) 
     569        res=$( expr $res \* -1 ) 
    633570    fi 
    634571  
     
    753690        num=19000101 
    754691    fi 
    755     echo $(( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) + $aux )) 
     692    echo $( expr \( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) \+ $aux \) ) 
    756693 
    757694    IGCM_debug_PopStack "IGCM_date_DaysSinceJC" 
Note: See TracChangeset for help on using the changeset viewer.