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