Changeset 23


Ignore:
Timestamp:
10/28/08 18:42:58 (15 years ago)
Author:
sdipsl
Message:

PB + MM : - great optimisation for libIGCM_date

  • add IGCM_card_WriteArrayOption. Write an array option for a given file.card and section
Location:
trunk/libIGCM
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/libIGCM/libIGCM_card/IGCM_card_Test.ksh

    r5 r23  
    8282 
    8383diff ${libIGCM}/libIGCM_card/TestFile.card NewTestFile.card 
    84 rm -f NewTestFile.card 
     84rm -f NewTestFile.card NewTestFile.card.bak 
    8585 
  • trunk/libIGCM/libIGCM_card/IGCM_card_WriteOption.awk

    r5 r23  
    102102END { 
    103103 
    104 if (! exit_value ) { 
     104    if (! exit_value ) { 
    105105 
    106 myprint("###############################") 
    107 myprint("section      ====> " section) 
    108 myprint("option       ====> " option) 
    109 myprint("value        ====> " value) 
     106        myprint("###############################") 
     107        myprint("section      ====> " section) 
     108        myprint("option       ====> " option) 
     109        myprint("value        ====> " value) 
     110         
     111        if (section_found == 0) { 
     112            print "Error: Section not found", section, option 
     113        } else if (option_found == 0) { 
     114            print "Error: Option not found", section, option 
     115        }        
     116         
     117    } else { 
    110118 
    111 if (section_found == 0) { 
    112         print "Error: Section not found" 
    113 } else if (option_found == 0) { 
    114         print "Error: Option not found" 
    115 }        
     119        print "Error in with IGCM_WriteOption : ", section, option 
    116120 
    117 } 
     121    } 
    118122 
    119123} 
  • trunk/libIGCM/libIGCM_card/libIGCM_card.ksh

    r5 r23  
    167167#D-#================================================================== 
    168168#D-function IGCM_card_WriteOption 
    169 #D-* Purpose: Write an option from a given file.card and section 
     169#D-* Purpose: Write an option in a given file.card and section 
    170170#D-* Usage: IGCM_card_WriteOption file.card section newvalue 
    171171#D-* Examples: IGCM_card_WriteOption file.card Recipes Red 150 
     
    181181        typeset tmpfile=tmpfile_$$ 
    182182        ( awk -f ${libIGCM}/libIGCM_card/IGCM_card_WriteOption.awk -- "$@" 2> /dev/null ) > ${tmpfile} 
     183        cp $1 $1.bak 
    183184        mv ${tmpfile} $1 
    184185    else 
     
    188189    fi 
    189190    IGCM_debug_PopStack "IGCM_card_WriteOption" 
     191} 
     192 
     193#D-#================================================================== 
     194#D-function IGCM_card_WriteArrayOption 
     195#D-* Purpose: Write an array option a given file.card and section 
     196#D-* Usage: IGCM_card_WriteArrayOption file.card section option newarray 
     197#D-* Examples: set -A MyArray -- 1 2 3 
     198#D-            IGCM_card_WriteArrayOption file.card Recipes List MyArray 
     199#D- 
     200function IGCM_card_WriteArrayOption 
     201{ 
     202    IGCM_debug_PushStack "IGCM_card_WriteArrayOption" $@ 
     203 
     204    if [ -r "$1" ] && [ -w "$1" ] ; then 
     205        typeset tmpfile=tmpfile_$$ 
     206        if [ X"${4}" != X"" ]; then 
     207            tab=$4 
     208            IGCM_card_WriteOption $1 $2 $3 '('$( eval echo \${${tab}[@]} | sed -e 's/ /,/g' )')' 
     209        else 
     210            IGCM_card_WriteOption $1 $2 $3 '()' 
     211        fi 
     212    else 
     213        echo "--Error--> IGCM_card_WriteArrayOption $@" 
     214        echo "           $1 is not readable or not writable" 
     215        IGCM_debug_Exit "IGCM_card_WriteArrayOption" 
     216    fi 
     217    IGCM_debug_PopStack "IGCM_card_WriteArrayOption" 
    190218} 
    191219 
  • trunk/libIGCM/libIGCM_date/libIGCM_date.ksh

    r7 r23  
    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         echo 360 
    162         IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    163167        return 
    164168    else 
    165169        if [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then 
    166             echo 365 
    167             IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    168177            return 
    169178        fi 
    170179    fi 
     180 
     181    typeset y a 
    171182 
    172183    # if there is no argument on the command line,  
     
    184195 
    185196    # if it is evenly divisible by 400 it must be a leap year 
    186     a=$( expr $y \% 400 ) 
     197    a=$(( $y % 400 )) 
    187198    if [ $a = 0 ] 
    188199        then 
    189         echo 366 
    190         IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    191207        return 
    192208    fi 
    193209 
    194210    #if it is evenly divisible by 100 it must not be a leap year 
    195     a=$( expr $y \% 100 ) 
     211    a=$(( $y % 100 )) 
    196212    if [ $a = 0 ] 
    197213        then 
    198         echo 365 
    199         IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    200221        return 
    201222    fi 
    202223         
    203224    # if it is evenly divisible by 4 it must be a leap year 
    204     a=$( expr $y \% 4 ) 
     225    a=$(( $y % 4 )) 
    205226    if [ $a = 0 ] 
    206227        then 
    207         echo 366 
    208         IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    209235        return 
    210236    fi 
    211237 
    212238    # otherwise it is not a leap year 
    213     echo 365 
    214  
    215     IGCM_debug_PopStack "IGCM_date_DaysInYear" 
     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" 
    216246} 
    217247 
     
    227257function IGCM_date_DaysInMonth 
    228258{ 
    229     IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@ 
     259#    IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@ 
    230260 
    231261    # calculates the number of days in a month  
    232262    # usage IGCM_date_DaysInMonth yyyy mm 
    233263    # or IGCM_date_DaysInMonth yyyymmdd 
    234  
     264     
    235265    # What is the calendar : 
    236266    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then 
    237         echo 30 
    238         IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     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" 
    239274        return 
    240275    fi 
     276 
     277    typeset ymd y m diy 
    241278 
    242279    # if there are no command line arguments then assume that a yyyymmdd is being 
     
    251288        ymd=$1 
    252289    else 
    253         ymd=$( expr \( \( $1 \* 10000 \) \+ \( $2 \* 100 \) \+ 1 \) ) 
     290        ymd=$(( ( $1 * 10000 ) + ( $2 * 100 ) + 1 )) 
    254291    fi 
    255292 
    256293    # extract the year and the month 
    257     y=$( expr $ymd / 10000 ) ; 
    258     m=$( expr \( $ymd \% 10000 \) / 100 ) ; 
     294    y=$(( $ymd / 10000 )) ; 
     295    m=$(( ( $ymd % 10000 ) / 100 )) ; 
    259296 
    260297    # 30 days hath september etc. 
    261298    case $m in 
    262299        1|3|5|7|8|10|12)  
    263             echo 31 
    264             IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     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" 
    265307            return ;; 
    266308        4|6|9|11)  
    267             echo 30      
    268             IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     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" 
    269316            return ;; 
    270317        *) ;; 
     
    274321    # Use IGCM_date_DaysInYear to get the number of days in the year and return a value 
    275322    # accordingly. 
    276     diy=$( IGCM_date_DaysInYear $y ) 
     323    IGCM_date_DaysInYear $y diy 
    277324    case $diy in 
    278325        365)  
    279             echo 28 
    280             IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     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" 
    281333            return ;; 
    282334        366)  
    283             echo 29 
    284             IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     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" 
    285342            return ;; 
    286343    esac 
    287344 
    288     IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
     345#    IGCM_debug_PopStack "IGCM_date_DaysInMonth" 
    289346} 
    290347 
     
    303360    # usage IGCM_date_ConvertGregorianDateToJulian 19980429 
    304361 
     362    typeset dt y m d x jul 
     363 
    305364    # if there is no command line argument, then assume that the date 
    306365    # is coming in on a pipe and use read to collect it     
     
    313372 
    314373    # break the yyyymmdd into separate parts for year, month and day 
    315     y=$( expr $dt / 10000 ) 
    316     m=$( expr \( $dt \% 10000 \) / 100 ) 
    317     d=$( expr \( $dt \% 100 \) ) 
     374    y=$(( $dt / 10000 )) 
     375    m=$(( ( $dt % 10000 ) / 100 )) 
     376    d=$(( ( $dt % 100 ) )) 
    318377 
    319378    # add the days in each month up to (but not including the month itself) 
     
    323382    # and add them to 14. 
    324383    x=1 
    325     while [ $( expr $x \< $m ) = 1 ] 
     384    while [ $x -lt $m ] 
    326385      do 
    327       md=$( IGCM_date_DaysInMonth $y $x ) 
    328       d=$( expr \( $d \+ $md \) ) 
    329       x=$( expr \( $x \+ 1 \) ) 
     386      IGCM_date_DaysInMonth $y $x md 
     387      d=$(( $d + $md )) 
     388      x=$(( $x + 1 )) 
    330389    done 
    331390 
    332391    # combine the year and day back together again and you have the julian date. 
    333     jul=$( expr \( $y \* 1000 \) + $d ) 
     392    jul=$(( ( $y * 1000 ) + $d )) 
    334393    echo $jul 
    335394 
     
    351410    # usage IGCM_date_ConvertJulianDateToGregorian 1998213 
    352411 
     412    typeset dt y m d md grg 
     413 
    353414    # if there is no command line argument, assume one is being  
    354415    # piped in and read it 
     
    361422         
    362423    # break apart the year and the days 
    363     y=$( expr $dt / 1000 ) 
    364     d=$( expr $dt \% 1000 ) 
     424    y=$(( $dt / 1000 )) 
     425    d=$(( $dt % 1000 )) 
    365426         
    366427    # subtract the number of days in each month starting from 1 
     
    369430    # month to get the correct day of the month 
    370431    m=1 
    371     while [ $( expr $d \> 0 ) = 1 ] 
     432    while [ $d -gt 0 ] 
    372433      do 
    373       md=$( IGCM_date_DaysInMonth $y $m ) 
    374       d=$( expr $d \- $md ) 
    375       m=$( expr $m \+ 1 ) 
     434      IGCM_date_DaysInMonth $y $m md 
     435      d=$(( $d - $md )) 
     436      m=$(( $m + 1 )) 
    376437    done 
    377438 
    378     d=$( expr $d \+ $md ) 
     439    d=$(( $d + $md )) 
    379440 
    380441    # the loop steps one past the correct month, so back up the month 
    381     m=$( expr $m \- 1 ) 
     442    m=$(( $m - 1 )) 
    382443 
    383444    # assemble the results into a gregorian date 
    384     grg=$( expr \( $y \* 10000 \) \+ \( $m \* 100 \) \+ $d ) 
     445    grg=$(( ( $y * 10000 ) + ( $m * 100 ) + $d )) 
    385446    echo $( IGCM_date_GregorianDigit $grg ) 
    386447 
     
    402463    # usage IGCM_date_AddDaysToJulianDate 1998312 { ,-}14 
    403464 
     465    typeset dif yd d y diy 
     466 
    404467    # Read the difference from the command lines 
    405468    # and the date from the command line, or standard input 
     
    414477 
    415478    # Break it into pieces 
    416     d=$( expr $yd \% 1000 ) 
    417     y=$( expr $yd / 1000 ) 
     479    d=$(( $yd % 1000 )) 
     480    y=$(( $yd / 1000 )) 
    418481 
    419482    # Add the number of days (if days is negative this results is 
    420483    # a subtraction) 
    421     d=$( expr $d \+ $dif ) 
     484    d=$(( $d + $dif )) 
    422485 
    423486    # Extract the days in the year 
    424     diy=$( IGCM_date_DaysInYear $y ) 
     487    IGCM_date_DaysInYear $y diy 
    425488 
    426489    # If the calculated day exceeds the days in the year,  
     
    429492    # test until you end up with a day number that falls within the 
    430493    # days of the year 
    431     while [ $( expr $d \> $diy ) = 1 ] 
     494    while [ $d -gt $diy ] 
    432495      do 
    433       d=$( expr $d - $diy ) 
    434       y=$( expr $y \+ 1 ) 
    435       diy=$( IGCM_date_DaysInYear $y ) 
     496      d=$(( $d - $diy )) 
     497      y=$(( $y + 1 )) 
     498      IGCM_date_DaysInYear $y diy 
    436499    done 
    437500 
     
    441504    # loop on this test until you end up with a number that 
    442505    # falls within the days of the year 
    443     while [ $( expr $d \< 1 ) = 1 ] 
     506    while [ $d -lt 1 ] 
    444507      do 
    445       y=$( expr $y - 1 ) 
    446       diy=$( IGCM_date_DaysInYear $y ) 
    447       d=$( expr $d \+ $diy ) 
     508      y=$(( $y - 1 )) 
     509      IGCM_date_DaysInYear $y diy 
     510      d=$(( $d + $diy )) 
    448511    done 
    449512 
    450513    # put the year and day back together and echo the result 
    451     yd=$( expr \( $y \* 1000 \) + $d ) 
     514    yd=$(( ( $y * 1000 ) + $d )) 
    452515 
    453516    echo $yd 
     
    539602 
    540603    # Break the dates in to year and day portions 
    541     yyyy1=$( expr $jul1 / 1000 ) 
    542     yyyy2=$( expr $jul2 / 1000 ) 
    543     ddd1=$( expr $jul1 \% 1000 ) 
    544     ddd2=$( expr $jul2 \% 1000 ) 
     604    yyyy1=$(( $jul1 / 1000 )) 
     605    yyyy2=$(( $jul2 / 1000 )) 
     606    ddd1=$(( $jul1 % 1000 )) 
     607    ddd2=$(( $jul2 % 1000 )) 
    545608 
    546609    # Subtract days 
    547     res=$( expr $ddd1 - $ddd2 ) 
     610    res=$(( $ddd1 - $ddd2 )) 
    548611 
    549612    # Then add days in year until year2 matches year1 
    550613 
    551614    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then 
    552         res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 360 \) \+ $res ) 
     615        res=$(( ( ( $yyyy1 - $yyyy2 ) * 360 ) + $res )) 
    553616    elif [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then 
    554         res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 365 \) \+ $res ) 
     617        res=$(( ( ( $yyyy1 - $yyyy2 ) * 365 ) + $res )) 
    555618    elif ( [ "${config_UserChoices_CalendarType}" = "leap" ] || [ "${config_UserChoices_CalendarType}" = "gregorian" ] ) ; then 
    556619        while [ $yyyy2 -lt $yyyy1 ] 
    557620          do 
    558           diy=$( IGCM_date_DaysInYear $yyyy2 ) 
    559           res=$( expr $res + $diy ) 
    560           yyyy2=$( expr $yyyy2 + 1 ) 
     621          IGCM_date_DaysInYear $yyyy2 diy 
     622          res=$(( $res + $diy )) 
     623          yyyy2=$(( $yyyy2 + 1 )) 
    561624        done 
    562625    fi 
     
    567630    if [ $1 -lt $2 ] 
    568631        then 
    569         res=$( expr $res \* -1 ) 
     632        res=$(( $res * -1 )) 
    570633    fi 
    571634  
     
    690753        num=19000101 
    691754    fi 
    692     echo $( expr \( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) \+ $aux \) ) 
     755    echo $(( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) + $aux )) 
    693756 
    694757    IGCM_debug_PopStack "IGCM_date_DaysSinceJC" 
Note: See TracChangeset for help on using the changeset viewer.