[2] | 1 | #!/bin/ksh |
---|
| 2 | |
---|
| 3 | #************************************************************** |
---|
| 4 | # Author: Sebastien Denvil |
---|
| 5 | # Contact: Sebastien.Denvil@ipsl.jussieu.fr |
---|
[4] | 6 | # $Date$ |
---|
[5] | 7 | # $Author$ |
---|
| 8 | # $Revision$ |
---|
[2] | 9 | # IPSL (2006) |
---|
| 10 | # This software is governed by the CeCILL licence see libIGCM/libIGCM_CeCILL.LIC |
---|
| 11 | # History: |
---|
| 12 | # Modification: |
---|
| 13 | # |
---|
| 14 | #************************************************************** |
---|
| 15 | |
---|
| 16 | #================================================== |
---|
| 17 | # The documentation of this file can be automatically generated |
---|
| 18 | # if you use the prefix #D- for comments to be extracted. |
---|
| 19 | # Extract with command: cat lib* | grep "^#D-" | cut -c "4-" |
---|
| 20 | #================================================== |
---|
| 21 | |
---|
| 22 | #D-#================================================================== |
---|
| 23 | #D-libIGCM_date |
---|
| 24 | #D-This ksh library handles date calculs and convertions in different calendars. |
---|
| 25 | |
---|
| 26 | # Number of digit in the year |
---|
| 27 | typeset -r dY=${dY:=4} |
---|
| 28 | #typeset -r MaxpY=$( echo "10^"$((dY+1)) | bc -l ) |
---|
| 29 | # Number of digit in non-human date representation |
---|
| 30 | typeset -r pY=$(( dY+4 )) |
---|
| 31 | |
---|
| 32 | #================================================================== |
---|
| 33 | function IGCM_date_YearDigit |
---|
| 34 | { |
---|
| 35 | IGCM_debug_PushStack "IGCM_date_YearDigit" $@ |
---|
| 36 | |
---|
| 37 | NUM=$(( 10#${1} )) |
---|
| 38 | echo $( awk "BEGIN { printf \"%0${dY}d\",${NUM} }" ) |
---|
| 39 | |
---|
| 40 | IGCM_debug_PopStack "IGCM_date_YearDigit" |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | #================================================================== |
---|
| 44 | function IGCM_date_GregorianDigit |
---|
| 45 | { |
---|
| 46 | IGCM_debug_PushStack "IGCM_date_GregorianDigit" $@ |
---|
| 47 | |
---|
| 48 | NUM=$(( 10#${1} )) |
---|
| 49 | echo $( awk "BEGIN { printf \"%0${pY}d\",${NUM} }" ) |
---|
| 50 | |
---|
| 51 | IGCM_debug_PopStack "IGCM_date_GregorianDigit" |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | #================================================================== |
---|
| 55 | function IGCM_date_HumanDigit |
---|
| 56 | { |
---|
| 57 | IGCM_debug_PushStack "IGCM_date_HumanDigit" $@ |
---|
| 58 | |
---|
| 59 | echo $( IGCM_date_GregorianDigit $( print ${1} | sed 's/-//g' ) ) \ |
---|
| 60 | | sed -e "s/\([0-9]\{${dY}\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1-\2-\3/" |
---|
| 61 | |
---|
| 62 | IGCM_debug_PopStack "IGCM_date_HumanDigit" |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | #================================================================== |
---|
| 66 | function IGCM_date_ConvertFormatToGregorian |
---|
| 67 | { |
---|
| 68 | IGCM_debug_PushStack "IGCM_date_ConvertFormatToGregorian" $@ |
---|
| 69 | |
---|
| 70 | # from a yyyy-mm-dd date format return |
---|
| 71 | # a yyymmdd date format |
---|
| 72 | # usage IGCM_date_ConvertFormat yyyy-mm-dd |
---|
| 73 | |
---|
| 74 | # if there is no argument on the command line, |
---|
| 75 | # then assume that a y-m-d formated date is being |
---|
| 76 | # piped in |
---|
| 77 | typeset ymd |
---|
| 78 | if [ $# = 0 ] |
---|
| 79 | then |
---|
| 80 | read ymd |
---|
| 81 | else |
---|
| 82 | ymd=$1 |
---|
| 83 | fi |
---|
| 84 | |
---|
| 85 | IGCM_date_GregorianDigit $( print ${ymd} | sed 's/-//g' ) |
---|
| 86 | |
---|
| 87 | IGCM_debug_PopStack "IGCM_date_ConvertFormatToGregorian" |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | #================================================================== |
---|
| 91 | function IGCM_date_ConvertFormatToHuman |
---|
| 92 | { |
---|
| 93 | IGCM_debug_PushStack "IGCM_date_ConvertFormatToHuman" $@ |
---|
| 94 | |
---|
| 95 | # from a yyyymmdd date format return |
---|
| 96 | # a yyyy-mm-dd date format |
---|
| 97 | # usage IGCM_date_ConvertFormat yyyymmdd |
---|
| 98 | |
---|
| 99 | # if there is no argument on the command line, |
---|
| 100 | # then assume that a yyyymmdd formated date is being |
---|
| 101 | # piped in |
---|
| 102 | typeset dt |
---|
| 103 | if [ $# = 0 ] |
---|
| 104 | then |
---|
| 105 | read dt |
---|
| 106 | else |
---|
| 107 | dt=$1 |
---|
| 108 | fi |
---|
| 109 | |
---|
| 110 | # break the yyyymmdd into separate parts for year, month and day |
---|
| 111 | echo $( IGCM_date_GregorianDigit ${dt} ) \ |
---|
| 112 | | sed -e "s/\([0-9]\{${dY}\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1-\2-\3/" |
---|
| 113 | |
---|
| 114 | IGCM_debug_PopStack "IGCM_date_ConvertFormatToHuman" |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | #================================================================== |
---|
| 118 | function IGCM_date_GetYearMonth |
---|
| 119 | { |
---|
| 120 | IGCM_debug_PushStack "IGCM_date_GetYearMonth" $@ |
---|
| 121 | |
---|
| 122 | # from a yyyymmdd date format return |
---|
| 123 | # a yyyy year and mm month |
---|
| 124 | # usage IGCM_date_GetYearMonth yyyymmdd year_var month_var |
---|
| 125 | |
---|
| 126 | # if there is no argument on the command line, |
---|
| 127 | # then assume that a yyyymmdd formated date is being |
---|
| 128 | # piped in |
---|
| 129 | typeset dt |
---|
| 130 | if [ $# = 0 ] |
---|
| 131 | then |
---|
| 132 | read dt |
---|
| 133 | else |
---|
| 134 | dt=$1 |
---|
| 135 | fi |
---|
| 136 | |
---|
| 137 | # break the yyyymmdd into separate parts for year, month and day |
---|
| 138 | eval $2=$( echo $( IGCM_date_GregorianDigit ${dt} ) \ |
---|
| 139 | | sed -e "s/\([0-9]\{${dY}\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\1/" ) |
---|
| 140 | eval $3=$( echo $( IGCM_date_GregorianDigit ${dt} ) \ |
---|
| 141 | | sed -e "s/\([0-9]\{${dY}\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)/\2/" ) |
---|
| 142 | |
---|
| 143 | IGCM_debug_PopStack "IGCM_date_GetYearMonth" |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | #D-#================================================================== |
---|
| 147 | #D-function IGCM_date_DaysInYear |
---|
| 148 | #D-* Purpose: Return the number of days in a year |
---|
| 149 | #D-* Usage: IGCM_date_DaysInYear yyyy |
---|
| 150 | #D- if there is no argument on the command line, |
---|
| 151 | #D- then assume that a yyyy is being piped in |
---|
| 152 | #D- |
---|
| 153 | function IGCM_date_DaysInYear |
---|
| 154 | { |
---|
| 155 | IGCM_debug_PushStack "IGCM_date_DaysInYear" $@ |
---|
| 156 | # return the number of days in a year |
---|
| 157 | # usage IGCM_date_DaysInYear yyyy |
---|
| 158 | |
---|
| 159 | # What is the calendar : |
---|
| 160 | if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then |
---|
| 161 | echo 360 |
---|
| 162 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 163 | return |
---|
| 164 | else |
---|
| 165 | if [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then |
---|
| 166 | echo 365 |
---|
| 167 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 168 | return |
---|
| 169 | fi |
---|
| 170 | fi |
---|
| 171 | |
---|
| 172 | # if there is no argument on the command line, |
---|
| 173 | # then assume that a yyyy is being piped in |
---|
| 174 | if [ $# = 0 ] |
---|
| 175 | then |
---|
| 176 | read y |
---|
| 177 | else |
---|
| 178 | y=$(( 10#${1} )) |
---|
| 179 | fi |
---|
| 180 | |
---|
| 181 | # a year is a leap year if it is even divisible by 4 |
---|
| 182 | # but not evenly divisible by 100 |
---|
| 183 | # unless it is evenly divisible by 400 |
---|
| 184 | |
---|
| 185 | # if it is evenly divisible by 400 it must be a leap year |
---|
| 186 | a=$( expr $y \% 400 ) |
---|
| 187 | if [ $a = 0 ] |
---|
| 188 | then |
---|
| 189 | echo 366 |
---|
| 190 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 191 | return |
---|
| 192 | fi |
---|
| 193 | |
---|
| 194 | #if it is evenly divisible by 100 it must not be a leap year |
---|
| 195 | a=$( expr $y \% 100 ) |
---|
| 196 | if [ $a = 0 ] |
---|
| 197 | then |
---|
| 198 | echo 365 |
---|
| 199 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 200 | return |
---|
| 201 | fi |
---|
| 202 | |
---|
| 203 | # if it is evenly divisible by 4 it must be a leap year |
---|
| 204 | a=$( expr $y \% 4 ) |
---|
| 205 | if [ $a = 0 ] |
---|
| 206 | then |
---|
| 207 | echo 366 |
---|
| 208 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 209 | return |
---|
| 210 | fi |
---|
| 211 | |
---|
| 212 | # otherwise it is not a leap year |
---|
| 213 | echo 365 |
---|
| 214 | |
---|
| 215 | IGCM_debug_PopStack "IGCM_date_DaysInYear" |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | #D-#================================================================== |
---|
| 219 | #D-function IGCM_date_DaysInMonth |
---|
[8] | 220 | #D-* Purpose: Calculate the number of days in a month |
---|
[2] | 221 | #D-* Usage: IGCM_date_DaysInMonth yyyy mm |
---|
| 222 | #D- or IGCM_date_DaysInMonth yyyymmdd |
---|
| 223 | #D- if there are no command line arguments then |
---|
| 224 | #D- assume that a yyyymmdd is being piped in and read the value. |
---|
| 225 | #D- if there is only one argument assume it is a yyyymmdd on the command line |
---|
| 226 | #D- other wise it is a yyyy and mm on the command line |
---|
| 227 | function IGCM_date_DaysInMonth |
---|
| 228 | { |
---|
| 229 | IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@ |
---|
| 230 | |
---|
| 231 | # calculates the number of days in a month |
---|
| 232 | # usage IGCM_date_DaysInMonth yyyy mm |
---|
| 233 | # or IGCM_date_DaysInMonth yyyymmdd |
---|
| 234 | |
---|
| 235 | # What is the calendar : |
---|
| 236 | if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then |
---|
| 237 | echo 30 |
---|
| 238 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 239 | return |
---|
| 240 | fi |
---|
| 241 | |
---|
| 242 | # if there are no command line arguments then assume that a yyyymmdd is being |
---|
| 243 | # piped in and read the value. |
---|
| 244 | # if there is only one argument assume it is a yyyymmdd on the command line |
---|
| 245 | # other wise it is a yyyy and mm on the command line |
---|
| 246 | if [ $# = 0 ] |
---|
| 247 | then |
---|
| 248 | read ymd |
---|
| 249 | elif [ $# = 1 ] |
---|
| 250 | then |
---|
| 251 | ymd=$1 |
---|
| 252 | else |
---|
| 253 | ymd=$( expr \( \( $1 \* 10000 \) \+ \( $2 \* 100 \) \+ 1 \) ) |
---|
| 254 | fi |
---|
| 255 | |
---|
| 256 | # extract the year and the month |
---|
| 257 | y=$( expr $ymd / 10000 ) ; |
---|
| 258 | m=$( expr \( $ymd \% 10000 \) / 100 ) ; |
---|
| 259 | |
---|
| 260 | # 30 days hath september etc. |
---|
| 261 | case $m in |
---|
| 262 | 1|3|5|7|8|10|12) |
---|
| 263 | echo 31 |
---|
| 264 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 265 | return ;; |
---|
| 266 | 4|6|9|11) |
---|
| 267 | echo 30 |
---|
| 268 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 269 | return ;; |
---|
| 270 | *) ;; |
---|
| 271 | esac |
---|
| 272 | |
---|
| 273 | # except for month 2 which depends on whether the year is a leap year |
---|
| 274 | # Use IGCM_date_DaysInYear to get the number of days in the year and return a value |
---|
| 275 | # accordingly. |
---|
| 276 | diy=$( IGCM_date_DaysInYear $y ) |
---|
| 277 | case $diy in |
---|
| 278 | 365) |
---|
| 279 | echo 28 |
---|
| 280 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 281 | return ;; |
---|
| 282 | 366) |
---|
| 283 | echo 29 |
---|
| 284 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 285 | return ;; |
---|
| 286 | esac |
---|
| 287 | |
---|
| 288 | IGCM_debug_PopStack "IGCM_date_DaysInMonth" |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | #D-#================================================================== |
---|
| 292 | #D-function IGCM_date_ConvertGregorianDateToJulian |
---|
| 293 | #D-* Purpose: Convert yyyymmdd to yyyyddd |
---|
| 294 | #D-* Usage: IGCM_date_ConvertGregorianDateToJulian 19980429 |
---|
| 295 | #D- if there is no command line argument, then assume that the date |
---|
| 296 | #D- is coming in on a pipe and use read to collect it |
---|
| 297 | #D- |
---|
| 298 | function IGCM_date_ConvertGregorianDateToJulian |
---|
| 299 | { |
---|
| 300 | IGCM_debug_PushStack "IGCM_date_ConvertGregorianDateToJulian" $@ |
---|
| 301 | |
---|
| 302 | # IGCM_date_ConvertGregorianDateToJulian converts yyyymmdd to yyyyddd |
---|
| 303 | # usage IGCM_date_ConvertGregorianDateToJulian 19980429 |
---|
| 304 | |
---|
| 305 | # if there is no command line argument, then assume that the date |
---|
| 306 | # is coming in on a pipe and use read to collect it |
---|
| 307 | if [ $# = 0 ] |
---|
| 308 | then |
---|
| 309 | read dt |
---|
| 310 | else |
---|
| 311 | dt=$1 |
---|
| 312 | fi |
---|
| 313 | |
---|
| 314 | # 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 \) ) |
---|
| 318 | |
---|
| 319 | # add the days in each month up to (but not including the month itself) |
---|
| 320 | # into the days. For example if the date is 19980203 then extract the |
---|
| 321 | # number of days in January and add it to 03. If the date is June 14, 1998 |
---|
| 322 | # then extract the number of days in January, February, March, April and May |
---|
| 323 | # and add them to 14. |
---|
| 324 | x=1 |
---|
| 325 | while [ $( expr $x \< $m ) = 1 ] |
---|
| 326 | do |
---|
| 327 | md=$( IGCM_date_DaysInMonth $y $x ) |
---|
| 328 | d=$( expr \( $d \+ $md \) ) |
---|
| 329 | x=$( expr \( $x \+ 1 \) ) |
---|
| 330 | done |
---|
| 331 | |
---|
| 332 | # combine the year and day back together again and you have the julian date. |
---|
| 333 | jul=$( expr \( $y \* 1000 \) + $d ) |
---|
| 334 | echo $jul |
---|
| 335 | |
---|
| 336 | IGCM_debug_PopStack "IGCM_date_ConvertGregorianDateToJulian" |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | #D-#================================================================== |
---|
| 340 | #D-function IGCM_date_ConvertJulianDateToGregorian() |
---|
| 341 | #D-* Purpose: Convert yyyyddd to yyyymmdd |
---|
| 342 | #D-* Usage: IGCM_date_ConvertJulianDateToGregorian 1998213 |
---|
| 343 | #D- if there is no command line argument, assume one is being |
---|
| 344 | #D- piped in and read it |
---|
| 345 | #D- |
---|
| 346 | function IGCM_date_ConvertJulianDateToGregorian |
---|
| 347 | { |
---|
| 348 | IGCM_debug_PushStack "IGCM_date_ConvertJulianDateToGregorian" $@ |
---|
| 349 | |
---|
| 350 | # IGCM_date_ConvertJulianDateToGregorian converts yyyyddd to yyyymmdd |
---|
| 351 | # usage IGCM_date_ConvertJulianDateToGregorian 1998213 |
---|
| 352 | |
---|
| 353 | # if there is no command line argument, assume one is being |
---|
| 354 | # piped in and read it |
---|
| 355 | if [ X$1 = X ] |
---|
| 356 | then |
---|
| 357 | read dt |
---|
| 358 | else |
---|
| 359 | dt=$1 |
---|
| 360 | fi |
---|
| 361 | |
---|
| 362 | # break apart the year and the days |
---|
| 363 | y=$( expr $dt / 1000 ) |
---|
| 364 | d=$( expr $dt \% 1000 ) |
---|
| 365 | |
---|
| 366 | # subtract the number of days in each month starting from 1 |
---|
| 367 | # from the days in the date. When the day goes below 1, you |
---|
| 368 | # have the current month. Add back the number of days in the |
---|
| 369 | # month to get the correct day of the month |
---|
| 370 | m=1 |
---|
| 371 | while [ $( expr $d \> 0 ) = 1 ] |
---|
| 372 | do |
---|
| 373 | md=$( IGCM_date_DaysInMonth $y $m ) |
---|
| 374 | d=$( expr $d \- $md ) |
---|
| 375 | m=$( expr $m \+ 1 ) |
---|
| 376 | done |
---|
| 377 | |
---|
| 378 | d=$( expr $d \+ $md ) |
---|
| 379 | |
---|
| 380 | # the loop steps one past the correct month, so back up the month |
---|
| 381 | m=$( expr $m \- 1 ) |
---|
| 382 | |
---|
| 383 | # assemble the results into a gregorian date |
---|
| 384 | grg=$( expr \( $y \* 10000 \) \+ \( $m \* 100 \) \+ $d ) |
---|
| 385 | echo $( IGCM_date_GregorianDigit $grg ) |
---|
| 386 | |
---|
| 387 | IGCM_debug_PopStack "IGCM_date_ConvertJulianDateToGregorian" |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | #D-#================================================================== |
---|
| 391 | #D-function IGCM_date_AddDaysToJulianDate |
---|
| 392 | #D-* Purpose: Add days to a yyyyddd formatted date |
---|
| 393 | #D-* Usage: IGCM_date_AddDaysToJulianDate 1998312 { ,-}14 |
---|
| 394 | #D- Read from the difference from the command lines |
---|
| 395 | #D- and the date from the command line, or standard input |
---|
| 396 | #D- |
---|
| 397 | function IGCM_date_AddDaysToJulianDate |
---|
| 398 | { |
---|
| 399 | IGCM_debug_PushStack "IGCM_date_AddDaysToJulianDate" $@ |
---|
| 400 | |
---|
| 401 | # IGCM_date_AddDaysToJulianDate adds days to a yyyyddd formatted date |
---|
| 402 | # usage IGCM_date_AddDaysToJulianDate 1998312 { ,-}14 |
---|
| 403 | |
---|
| 404 | # Read the difference from the command lines |
---|
| 405 | # and the date from the command line, or standard input |
---|
| 406 | if [ X$2 = X ] |
---|
| 407 | then |
---|
| 408 | dif=$1 |
---|
| 409 | read yd |
---|
| 410 | else |
---|
| 411 | yd=$1 |
---|
| 412 | dif=$2 |
---|
| 413 | fi |
---|
| 414 | |
---|
| 415 | # Break it into pieces |
---|
| 416 | d=$( expr $yd \% 1000 ) |
---|
| 417 | y=$( expr $yd / 1000 ) |
---|
| 418 | |
---|
| 419 | # Add the number of days (if days is negative this results is |
---|
| 420 | # a subtraction) |
---|
| 421 | d=$( expr $d \+ $dif ) |
---|
| 422 | |
---|
| 423 | # Extract the days in the year |
---|
| 424 | diy=$( IGCM_date_DaysInYear $y ) |
---|
| 425 | |
---|
| 426 | # If the calculated day exceeds the days in the year, |
---|
| 427 | # add one year to the year and subtract the days in the year from the |
---|
| 428 | # calculated days. Extract the days in the new year and repeat |
---|
| 429 | # test until you end up with a day number that falls within the |
---|
| 430 | # days of the year |
---|
| 431 | while [ $( expr $d \> $diy ) = 1 ] |
---|
| 432 | do |
---|
| 433 | d=$( expr $d - $diy ) |
---|
| 434 | y=$( expr $y \+ 1 ) |
---|
| 435 | diy=$( IGCM_date_DaysInYear $y ) |
---|
| 436 | done |
---|
| 437 | |
---|
| 438 | # This is the reverse process. If the calculated number of days |
---|
| 439 | # is less than 1, move back one year. Extract |
---|
| 440 | # the days in this year and add the days in the year |
---|
| 441 | # loop on this test until you end up with a number that |
---|
| 442 | # falls within the days of the year |
---|
| 443 | while [ $( expr $d \< 1 ) = 1 ] |
---|
| 444 | do |
---|
| 445 | y=$( expr $y - 1 ) |
---|
| 446 | diy=$( IGCM_date_DaysInYear $y ) |
---|
| 447 | d=$( expr $d \+ $diy ) |
---|
| 448 | done |
---|
| 449 | |
---|
| 450 | # put the year and day back together and echo the result |
---|
| 451 | yd=$( expr \( $y \* 1000 \) + $d ) |
---|
| 452 | |
---|
| 453 | echo $yd |
---|
| 454 | |
---|
| 455 | IGCM_debug_PopStack "IGCM_date_AddDaysToJulianDate" |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | #D-#================================================================== |
---|
| 459 | #D-function IGCM_date_AddDaysToGregorianDate |
---|
| 460 | #D-* Purpose: Add days to a yyyymmdd formatted date |
---|
| 461 | #D-* Usage: IGCM_date_AddDaysToGregorianDate 19980312 { ,-}14 |
---|
| 462 | #D- Read from the difference from the command lines |
---|
| 463 | #D- and the date from the command line, or standard input |
---|
| 464 | #D- |
---|
| 465 | function IGCM_date_AddDaysToGregorianDate |
---|
| 466 | { |
---|
| 467 | IGCM_debug_PushStack "IGCM_date_AddDaysToGregorianDate" $@ |
---|
| 468 | |
---|
| 469 | # IGCM_date_AddDaysToGregorianDate adds days to a yyyymmdd formatted date |
---|
| 470 | # usage IGCM_date_AddDaysToGregorianDate 19980312 { ,-}14 |
---|
| 471 | |
---|
| 472 | # Read the difference from the command lines |
---|
| 473 | # and the date from the command line, or standard input |
---|
| 474 | typeset dif yd tmp res |
---|
| 475 | if [ X$2 = X ] |
---|
| 476 | then |
---|
| 477 | dif=$1 |
---|
| 478 | read yd |
---|
| 479 | else |
---|
| 480 | yd=$1 |
---|
| 481 | dif=$2 |
---|
| 482 | fi |
---|
| 483 | |
---|
| 484 | tmp=$( IGCM_date_ConvertGregorianDateToJulian $yd ) |
---|
| 485 | tmp=$( IGCM_date_AddDaysToJulianDate $tmp $dif ) |
---|
| 486 | res=$( IGCM_date_ConvertJulianDateToGregorian $tmp ) |
---|
| 487 | |
---|
| 488 | echo $res |
---|
| 489 | |
---|
| 490 | IGCM_debug_PopStack "IGCM_date_AddDaysToGregorianDate" |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | #D-#================================================================== |
---|
| 494 | #D-function IGCM_date_DaysBetweenJulianDate |
---|
[8] | 495 | #D-* Purpose: Calculate the days difference between two dates and reports |
---|
[2] | 496 | #D- the number days as jul1 - jul2 |
---|
| 497 | #D-* Usage: IGCM_date_DaysBetweenJulianDate jul1 jul2 |
---|
| 498 | #D- where julian date is in the form yyyyddd |
---|
| 499 | #D- |
---|
| 500 | function IGCM_date_DaysBetweenJulianDate |
---|
| 501 | { |
---|
| 502 | IGCM_debug_PushStack "IGCM_date_DaysBetweenJulianDate" $@ |
---|
| 503 | |
---|
| 504 | # calculates the days difference between two dates and reports |
---|
| 505 | # the number days as jul1 - jul2 |
---|
| 506 | # usage IGCM_date_DaysBetweenJulianDate jul1 jul2 |
---|
| 507 | # where julian date is in the form yyyyddd |
---|
| 508 | |
---|
| 509 | usage () { |
---|
| 510 | echo "Usage:" |
---|
| 511 | echo " IGCM_date_DaysBetweenJulianDate jul1 jul2" |
---|
| 512 | echo "" |
---|
| 513 | echo " Calculates the day difference between" |
---|
| 514 | echo " two julian dates (jul1 -jul2)" |
---|
| 515 | echo " where a julian date is in the form of yyyyddd." |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | if [ $# -lt 2 ]; then |
---|
| 519 | usage |
---|
| 520 | IGCM_debug_Exit "IGCM_date_DaysBetweenJulianDate" |
---|
| 521 | fi |
---|
| 522 | |
---|
| 523 | # This process subtracts arg2 from arg1. If arg2 is larger |
---|
| 524 | # then reverse the arguments. The calculations are done, and |
---|
| 525 | # then the sign is reversed |
---|
| 526 | if [ $1 -lt $2 ] |
---|
| 527 | then |
---|
| 528 | jul1=$2 |
---|
| 529 | jul2=$1 |
---|
| 530 | elif [ $1 -gt $2 ] |
---|
| 531 | then |
---|
| 532 | jul1=$1 |
---|
| 533 | jul2=$2 |
---|
| 534 | else |
---|
| 535 | echo 0 |
---|
| 536 | IGCM_debug_PopStack "IGCM_date_DaysBetweenJulianDate" |
---|
| 537 | return |
---|
| 538 | fi |
---|
| 539 | |
---|
| 540 | # 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 ) |
---|
| 545 | |
---|
| 546 | # Subtract days |
---|
| 547 | res=$( expr $ddd1 - $ddd2 ) |
---|
| 548 | |
---|
| 549 | # Then add days in year until year2 matches year1 |
---|
| 550 | |
---|
| 551 | if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then |
---|
| 552 | res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 360 \) \+ $res ) |
---|
| 553 | elif [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then |
---|
| 554 | res=$( expr \( \( $yyyy1 \- $yyyy2 \) \* 365 \) \+ $res ) |
---|
| 555 | elif ( [ "${config_UserChoices_CalendarType}" = "leap" ] || [ "${config_UserChoices_CalendarType}" = "gregorian" ] ) ; then |
---|
| 556 | while [ $yyyy2 -lt $yyyy1 ] |
---|
| 557 | do |
---|
| 558 | diy=$( IGCM_date_DaysInYear $yyyy2 ) |
---|
| 559 | res=$( expr $res + $diy ) |
---|
| 560 | yyyy2=$( expr $yyyy2 + 1 ) |
---|
| 561 | done |
---|
| 562 | fi |
---|
| 563 | |
---|
| 564 | # if argument 2 was larger than argument 1 then |
---|
| 565 | # the arguments were reversed before calculating |
---|
| 566 | # adjust by reversing the sign |
---|
| 567 | if [ $1 -lt $2 ] |
---|
| 568 | then |
---|
| 569 | res=$( expr $res \* -1 ) |
---|
| 570 | fi |
---|
| 571 | |
---|
| 572 | # and output the results |
---|
| 573 | echo $res |
---|
| 574 | |
---|
| 575 | IGCM_debug_PopStack "IGCM_date_DaysBetweenJulianDate" |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | #D-#================================================================== |
---|
| 579 | #D-function IGCM_date_DaysBetweenGregorianDate () |
---|
[8] | 580 | #D-* Purpose: Calculate the days difference between two dates and reports |
---|
[2] | 581 | #D- the number days as grg1 - grg2 |
---|
| 582 | #D-* Usage: IGCM_date_DaysBetweenGregorianDate grg1 grg2 |
---|
| 583 | #D- where gregorian date is in the form yyyymmdd |
---|
| 584 | #D- |
---|
| 585 | function IGCM_date_DaysBetweenGregorianDate |
---|
| 586 | { |
---|
| 587 | IGCM_debug_PushStack "IGCM_date_DaysBetweenGregorianDate" $@ |
---|
| 588 | |
---|
| 589 | # calculates the days difference between two dates and reports |
---|
| 590 | # the number days as grg1 - grg2 |
---|
| 591 | # usage IGCM_date_DaysBetweenGregorianDate grg1 grg2 |
---|
| 592 | # where gregorian date is in the form yyyymmdd |
---|
| 593 | |
---|
| 594 | usage () { |
---|
| 595 | echo "Usage:" |
---|
| 596 | echo " IGCM_date_DaysBetweenGregorianDate grg1 grg2" |
---|
| 597 | echo "" |
---|
| 598 | echo " Calculate day difference between" |
---|
| 599 | echo " two gregorian dates (grg1 - grg2)" |
---|
| 600 | echo " where a gregorian date is in the form of yyyymmdd." |
---|
| 601 | } |
---|
| 602 | |
---|
| 603 | if [ $# -lt 2 ]; then |
---|
| 604 | usage |
---|
| 605 | IGCM_debug_Exit "IGCM_date_DaysBetweenGregorianDate" |
---|
| 606 | fi |
---|
| 607 | |
---|
| 608 | # convert each date to julian |
---|
| 609 | grg1=$1 |
---|
| 610 | grg2=$2 |
---|
| 611 | |
---|
| 612 | jul1=$( IGCM_date_ConvertGregorianDateToJulian $grg1 ) |
---|
| 613 | jul2=$( IGCM_date_ConvertGregorianDateToJulian $grg2 ) |
---|
| 614 | |
---|
| 615 | if [ $jul1 -ne $jul2 ]; then |
---|
| 616 | # calculate the answer using IGCM_date_DaysBetweenJulianDate |
---|
| 617 | res=$( IGCM_date_DaysBetweenJulianDate $jul1 $jul2 ) |
---|
| 618 | # and output the results |
---|
| 619 | echo $res |
---|
| 620 | else |
---|
| 621 | echo 0 |
---|
| 622 | fi |
---|
| 623 | |
---|
| 624 | IGCM_debug_PopStack "IGCM_date_DaysBetweenGregorianDate" |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | #D-#================================================================== |
---|
| 628 | #D-function IGCM_date_DaysSinceJC () |
---|
| 629 | #D-* Purpose: Calculate the days difference between a date and 00010101 |
---|
| 630 | #D-* Usage: IGCM_date_DaysSinceJC grg1 |
---|
| 631 | #D- where gregorian date is in the form yyyymmdd |
---|
| 632 | #D- |
---|
| 633 | function IGCM_date_DaysSinceJC |
---|
| 634 | { |
---|
| 635 | IGCM_debug_PushStack "IGCM_date_DaysSinceJC" $@ |
---|
| 636 | |
---|
| 637 | # calculates the days difference between a date and 00010101 |
---|
| 638 | # usage IGCM_date_DaysSinceJC grg1 |
---|
| 639 | # where gregorian date is in the form yyyymmdd |
---|
| 640 | |
---|
| 641 | usage () { |
---|
| 642 | echo "Usage:" |
---|
| 643 | echo " IGCM_date_DaysSinceJC grg1" |
---|
| 644 | echo "" |
---|
| 645 | echo " Calculate day difference between" |
---|
| 646 | echo " a gregorian date and 00010101" |
---|
| 647 | echo " where a gregorian date is in the form of yyyymmdd." |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | if [ $# -lt 1 ]; then |
---|
| 651 | usage |
---|
| 652 | IGCM_debug_Exit "IGCM_date_DaysSinceJC" |
---|
| 653 | fi |
---|
| 654 | |
---|
| 655 | typeset aux num |
---|
| 656 | |
---|
| 657 | if [ ${1} -lt 5000000 ]; then |
---|
| 658 | case ${config_UserChoices_CalendarType} in |
---|
| 659 | 360d) |
---|
| 660 | aux=-360;; |
---|
| 661 | noleap) |
---|
| 662 | aux=-365;; |
---|
| 663 | leap) |
---|
| 664 | aux=-366;; |
---|
| 665 | esac |
---|
| 666 | num=101 |
---|
| 667 | elif [ ${1} -lt 15000000 ]; then |
---|
| 668 | # To save CPU type we use auxiliary value |
---|
| 669 | # which is number of days since JC and 10000101 |
---|
| 670 | case ${config_UserChoices_CalendarType} in |
---|
| 671 | 360d) |
---|
| 672 | aux=359640;; |
---|
| 673 | noleap) |
---|
| 674 | aux=364635;; |
---|
| 675 | leap) |
---|
| 676 | aux=364877;; |
---|
| 677 | esac |
---|
| 678 | num=10000101 |
---|
| 679 | else |
---|
| 680 | # To save CPU type we use auxiliary value |
---|
| 681 | # which is number of days since JC and 19000101 |
---|
| 682 | case ${config_UserChoices_CalendarType} in |
---|
| 683 | 360d) |
---|
| 684 | aux=683640;; |
---|
| 685 | noleap) |
---|
| 686 | aux=693135;; |
---|
| 687 | leap) |
---|
| 688 | aux=693595;; |
---|
| 689 | esac |
---|
| 690 | num=19000101 |
---|
| 691 | fi |
---|
| 692 | echo $( expr \( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) \+ $aux \) ) |
---|
| 693 | |
---|
| 694 | IGCM_debug_PopStack "IGCM_date_DaysSinceJC" |
---|
| 695 | } |
---|
| 696 | |
---|
| 697 | #D-#================================================================== |
---|
| 698 | #D-function IGCM_date_Check |
---|
| 699 | #D- * Purpose: Check the present file by comparison with a reference file |
---|
| 700 | function IGCM_date_Check |
---|
| 701 | { |
---|
| 702 | IGCM_debug_PushStack "IGCM_date_Check" |
---|
| 703 | |
---|
| 704 | #--------------------- |
---|
| 705 | if [ ! -n "${libIGCM}" ] ; then |
---|
| 706 | echo "Check libIGCM_date ...........................................[ FAILED ]" |
---|
| 707 | echo "--Error--> libIGCM variable is not defined" |
---|
| 708 | IGCM_debug_Exit "IGCM_date_Check" |
---|
| 709 | fi |
---|
| 710 | |
---|
| 711 | #--------------------- |
---|
| 712 | whence -v awk > /dev/null 2>&1 |
---|
| 713 | if [ ! $? -eq 0 ] ; then |
---|
| 714 | echo "Check libIGCM_date ...........................................[ FAILED ]" |
---|
| 715 | echo "--Error--> awk command is not defined" |
---|
| 716 | IGCM_debug_Exit "IGCM_date_Check" |
---|
| 717 | fi |
---|
| 718 | |
---|
| 719 | #--------------------- |
---|
| 720 | ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh > IGCM_date_Test.ref.failed 2>&1 |
---|
| 721 | |
---|
| 722 | if diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref > /dev/null 2>&1 ; then |
---|
| 723 | echo "Check libIGCM_date ...............................................[ OK ]" |
---|
| 724 | rm -f IGCM_date_Test.ref.failed |
---|
| 725 | else |
---|
| 726 | echo "Check libIGCM_date ...........................................[ FAILED ]" |
---|
| 727 | echo "--Error--> Execution of ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh" |
---|
| 728 | echo " has produced the file IGCM_date_Test.ref.failed" |
---|
| 729 | echo " Please analyse differences with the reference file by typing:" |
---|
| 730 | echo " diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref" |
---|
| 731 | echo " Report errors to the author: Sebastien.Denvil@ipsl.jussieu.fr" |
---|
| 732 | IGCM_debug_Exit "IGCM_date_Check" |
---|
| 733 | fi |
---|
| 734 | |
---|
| 735 | #--------------------- |
---|
| 736 | IGCM_debug_PopStack "IGCM_date_Check" |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | #================================================================== |
---|