source: trunk/libIGCM/libIGCM_date/libIGCM_date.ksh @ 23

Last change on this file since 23 was 23, checked in by sdipsl, 16 years ago

PB + MM : - great optimisation for libIGCM_date

  • add IGCM_card_WriteArrayOption. Write an array option for a given file.card and section
  • Property licence set to
    The following licence information concerns ONLY the libIGCM tools
    ==================================================================

    Copyright © Centre National de la Recherche Scientifique CNRS
    Commissariat à l'Énergie Atomique CEA

    libIGCM : Library for Portable Models Computation of IGCM Group.

    IGCM Group is the french IPSL Global Climate Model Group.

    This library is a set of shell scripts and functions whose purpose is
    the management of the initialization, the launch, the transfer of
    output files, the post-processing and the monitoring of datas produce
    by any numerical program on any plateforme.

    This software is governed by the CeCILL license under French law and
    abiding by the rules of distribution of free software. You can use,
    modify and/ or redistribute the software under the terms of the CeCILL
    license as circulated by CEA, CNRS and INRIA at the following URL
    "http://www.cecill.info".

    As a counterpart to the access to the source code and rights to copy,
    modify and redistribute granted by the license, users are provided only
    with a limited warranty and the software's author, the holder of the
    economic rights, and the successive licensors have only limited
    liability.

    In this respect, the user's attention is drawn to the risks associated
    with loading, using, modifying and/or developing or reproducing the
    software by the user in light of its specific status of free software,
    that may mean that it is complicated to manipulate, and that also
    therefore means that it is reserved for developers and experienced
    professionals having in-depth computer knowledge. Users are therefore
    encouraged to load and test the software's suitability as regards their
    requirements in conditions enabling the security of their systems and/or
    data to be ensured and, more generally, to use and operate it in the
    same conditions as regards security.

    The fact that you are presently reading this means that you have had
    knowledge of the CeCILL license and that you accept its terms.
  • Property svn:keywords set to Date Author Revision
File size: 22.1 KB
Line 
1#!/bin/ksh
2
3#**************************************************************
4# Author: Sebastien Denvil
5# Contact: Sebastien.Denvil@ipsl.jussieu.fr
6# $Date$
7# $Author$
8# $Revision$
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
27typeset -r dY=${dY:=4}
28#typeset -r MaxpY=$( echo "10^"$((dY+1)) | bc -l )
29# Number of digit in non-human date representation
30typeset -r pY=$(( dY+4 ))
31
32#==================================================================
33function 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#==================================================================
44function 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#==================================================================
55function 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#==================================================================
66function 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#==================================================================
91function 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#==================================================================
118function 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-
153function 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        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"
167        return
168    else
169        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"
177            return
178        fi
179    fi
180
181    typeset y a
182
183    # if there is no argument on the command line,
184    # then assume that a yyyy is being piped in
185    if [ $# = 0 ]
186        then
187        read y
188    else
189        y=$(( 10#${1} ))
190    fi
191
192    # a year is a leap year if it is even divisible by 4
193    # but not evenly divisible by 100
194    # unless it is evenly divisible by 400
195
196    # if it is evenly divisible by 400 it must be a leap year
197    a=$(( $y % 400 ))
198    if [ $a = 0 ]
199        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"
207        return
208    fi
209
210    #if it is evenly divisible by 100 it must not be a leap year
211    a=$(( $y % 100 ))
212    if [ $a = 0 ]
213        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"
221        return
222    fi
223       
224    # if it is evenly divisible by 4 it must be a leap year
225    a=$(( $y % 4 ))
226    if [ $a = 0 ]
227        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"
235        return
236    fi
237
238    # 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"
246}
247
248#D-#==================================================================
249#D-function IGCM_date_DaysInMonth
250#D-* Purpose: Calculate the number of days in a month
251#D-* Usage: IGCM_date_DaysInMonth yyyy mm
252#D-         or IGCM_date_DaysInMonth yyyymmdd
253#D-         if there are no command line arguments then
254#D-         assume that a yyyymmdd is being piped in and read the value.
255#D-         if there is only one argument assume it is a yyyymmdd on the command line
256#D-         other wise it is a yyyy and mm on the command line
257function IGCM_date_DaysInMonth
258{
259#    IGCM_debug_PushStack "IGCM_date_DaysInMonth" $@
260
261    # calculates the number of days in a month
262    # usage IGCM_date_DaysInMonth yyyy mm
263    # or IGCM_date_DaysInMonth yyyymmdd
264   
265    # What is the calendar :
266    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"
274        return
275    fi
276
277    typeset ymd y m diy
278
279    # if there are no command line arguments then assume that a yyyymmdd is being
280    # piped in and read the value.
281    # if there is only one argument assume it is a yyyymmdd on the command line
282    # other wise it is a yyyy and mm on the command line
283    if [ $# = 0 ]
284        then
285        read ymd
286    elif [ $# = 1 ] 
287        then
288        ymd=$1
289    else
290        ymd=$(( ( $1 * 10000 ) + ( $2 * 100 ) + 1 ))
291    fi
292
293    # extract the year and the month
294    y=$(( $ymd / 10000 )) ;
295    m=$(( ( $ymd % 10000 ) / 100 )) ;
296
297    # 30 days hath september etc.
298    case $m in
299        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"
307            return ;;
308        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"
316            return ;;
317        *) ;;
318    esac
319
320    # except for month 2 which depends on whether the year is a leap year
321    # Use IGCM_date_DaysInYear to get the number of days in the year and return a value
322    # accordingly.
323    IGCM_date_DaysInYear $y diy
324    case $diy in
325        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"
333            return ;;
334        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"
342            return ;;
343    esac
344
345#    IGCM_debug_PopStack "IGCM_date_DaysInMonth"
346}
347
348#D-#==================================================================
349#D-function IGCM_date_ConvertGregorianDateToJulian
350#D-* Purpose: Convert yyyymmdd to yyyyddd
351#D-* Usage: IGCM_date_ConvertGregorianDateToJulian 19980429
352#D-         if there is no command line argument, then assume that the date
353#D-         is coming in on a pipe and use read to collect it
354#D-
355function IGCM_date_ConvertGregorianDateToJulian
356{
357    IGCM_debug_PushStack "IGCM_date_ConvertGregorianDateToJulian" $@
358
359    # IGCM_date_ConvertGregorianDateToJulian converts yyyymmdd to yyyyddd
360    # usage IGCM_date_ConvertGregorianDateToJulian 19980429
361
362    typeset dt y m d x jul
363
364    # if there is no command line argument, then assume that the date
365    # is coming in on a pipe and use read to collect it   
366    if [ $# = 0 ]
367        then
368        read dt
369    else
370        dt=$1
371    fi
372
373    # break the yyyymmdd into separate parts for year, month and day
374    y=$(( $dt / 10000 ))
375    m=$(( ( $dt % 10000 ) / 100 ))
376    d=$(( ( $dt % 100 ) ))
377
378    # add the days in each month up to (but not including the month itself)
379    # into the days. For example if the date is 19980203 then extract the
380    # number of days in January and add it to 03. If the date is June 14, 1998
381    # then extract the number of days in January, February, March, April and May
382    # and add them to 14.
383    x=1
384    while [ $x -lt $m ]
385      do
386      IGCM_date_DaysInMonth $y $x md
387      d=$(( $d + $md ))
388      x=$(( $x + 1 ))
389    done
390
391    # combine the year and day back together again and you have the julian date.
392    jul=$(( ( $y * 1000 ) + $d ))
393    echo $jul
394
395    IGCM_debug_PopStack "IGCM_date_ConvertGregorianDateToJulian"
396}
397
398#D-#==================================================================
399#D-function IGCM_date_ConvertJulianDateToGregorian()
400#D-* Purpose: Convert yyyyddd to yyyymmdd
401#D-* Usage: IGCM_date_ConvertJulianDateToGregorian 1998213
402#D-         if there is no command line argument, assume one is being
403#D-         piped in and read it
404#D-
405function IGCM_date_ConvertJulianDateToGregorian
406{
407    IGCM_debug_PushStack "IGCM_date_ConvertJulianDateToGregorian" $@
408
409    # IGCM_date_ConvertJulianDateToGregorian converts yyyyddd to yyyymmdd
410    # usage IGCM_date_ConvertJulianDateToGregorian 1998213
411
412    typeset dt y m d md grg
413
414    # if there is no command line argument, assume one is being
415    # piped in and read it
416    if [ X$1 = X ]
417        then
418        read dt
419    else
420        dt=$1
421    fi
422       
423    # break apart the year and the days
424    y=$(( $dt / 1000 ))
425    d=$(( $dt % 1000 ))
426       
427    # subtract the number of days in each month starting from 1
428    # from the days in the date. When the day goes below 1, you
429    # have the current month. Add back the number of days in the
430    # month to get the correct day of the month
431    m=1
432    while [ $d -gt 0 ]
433      do
434      IGCM_date_DaysInMonth $y $m md
435      d=$(( $d - $md ))
436      m=$(( $m + 1 ))
437    done
438
439    d=$(( $d + $md ))
440
441    # the loop steps one past the correct month, so back up the month
442    m=$(( $m - 1 ))
443
444    # assemble the results into a gregorian date
445    grg=$(( ( $y * 10000 ) + ( $m * 100 ) + $d ))
446    echo $( IGCM_date_GregorianDigit $grg )
447
448    IGCM_debug_PopStack "IGCM_date_ConvertJulianDateToGregorian"
449}
450
451#D-#==================================================================
452#D-function IGCM_date_AddDaysToJulianDate
453#D-* Purpose: Add days to a yyyyddd formatted date
454#D-* Usage: IGCM_date_AddDaysToJulianDate 1998312 { ,-}14
455#D-         Read from the difference from the command lines
456#D-         and the date from the command line, or standard input
457#D-
458function IGCM_date_AddDaysToJulianDate
459{
460    IGCM_debug_PushStack "IGCM_date_AddDaysToJulianDate" $@
461
462    # IGCM_date_AddDaysToJulianDate adds days to a yyyyddd formatted date
463    # usage IGCM_date_AddDaysToJulianDate 1998312 { ,-}14
464
465    typeset dif yd d y diy
466
467    # Read the difference from the command lines
468    # and the date from the command line, or standard input
469    if [ X$2 = X ]
470        then
471        dif=$1
472        read yd
473    else
474        yd=$1
475        dif=$2
476    fi
477
478    # Break it into pieces
479    d=$(( $yd % 1000 ))
480    y=$(( $yd / 1000 ))
481
482    # Add the number of days (if days is negative this results is
483    # a subtraction)
484    d=$(( $d + $dif ))
485
486    # Extract the days in the year
487    IGCM_date_DaysInYear $y diy
488
489    # If the calculated day exceeds the days in the year,
490    # add one year to the year and subtract the days in the year from the
491    # calculated days. Extract the days in the new year and repeat
492    # test until you end up with a day number that falls within the
493    # days of the year
494    while [ $d -gt $diy ]
495      do
496      d=$(( $d - $diy ))
497      y=$(( $y + 1 ))
498      IGCM_date_DaysInYear $y diy
499    done
500
501    # This is the reverse process. If the calculated number of days
502    # is less than 1, move back one year. Extract
503    # the days in this year and add the days in the year
504    # loop on this test until you end up with a number that
505    # falls within the days of the year
506    while [ $d -lt 1 ]
507      do
508      y=$(( $y - 1 ))
509      IGCM_date_DaysInYear $y diy
510      d=$(( $d + $diy ))
511    done
512
513    # put the year and day back together and echo the result
514    yd=$(( ( $y * 1000 ) + $d ))
515
516    echo $yd
517
518    IGCM_debug_PopStack "IGCM_date_AddDaysToJulianDate"
519}
520
521#D-#==================================================================
522#D-function IGCM_date_AddDaysToGregorianDate
523#D-* Purpose: Add days to a yyyymmdd formatted date
524#D-* Usage: IGCM_date_AddDaysToGregorianDate 19980312 { ,-}14
525#D-         Read from the difference from the command lines
526#D-         and the date from the command line, or standard input
527#D-
528function IGCM_date_AddDaysToGregorianDate
529{
530    IGCM_debug_PushStack "IGCM_date_AddDaysToGregorianDate" $@
531
532    # IGCM_date_AddDaysToGregorianDate adds days to a yyyymmdd formatted date
533    # usage IGCM_date_AddDaysToGregorianDate 19980312 { ,-}14
534
535    # Read the difference from the command lines
536    # and the date from the command line, or standard input
537    typeset dif yd tmp res
538    if [ X$2 = X ]
539        then
540        dif=$1
541        read yd
542    else
543        yd=$1
544        dif=$2
545    fi
546
547    tmp=$( IGCM_date_ConvertGregorianDateToJulian $yd )
548    tmp=$( IGCM_date_AddDaysToJulianDate $tmp $dif )
549    res=$( IGCM_date_ConvertJulianDateToGregorian $tmp )
550   
551    echo $res
552
553    IGCM_debug_PopStack "IGCM_date_AddDaysToGregorianDate"
554}
555
556#D-#==================================================================
557#D-function IGCM_date_DaysBetweenJulianDate
558#D-* Purpose: Calculate the days difference between two dates and reports
559#D-           the number days as jul1 - jul2
560#D-* Usage: IGCM_date_DaysBetweenJulianDate jul1 jul2
561#D-         where julian date is in the form yyyyddd
562#D-
563function IGCM_date_DaysBetweenJulianDate
564{
565    IGCM_debug_PushStack "IGCM_date_DaysBetweenJulianDate" $@
566
567    # calculates the days difference between two dates and reports
568    # the number days as jul1 - jul2
569    # usage IGCM_date_DaysBetweenJulianDate jul1 jul2
570    # where julian date is in the form yyyyddd
571   
572    usage () {
573        echo "Usage:"
574        echo " IGCM_date_DaysBetweenJulianDate jul1 jul2"
575        echo ""
576        echo " Calculates the day difference between"
577        echo " two julian dates (jul1 -jul2)"
578        echo " where a julian date is in the form of yyyyddd."
579    }
580 
581    if [ $# -lt 2 ]; then
582        usage
583        IGCM_debug_Exit "IGCM_date_DaysBetweenJulianDate"
584    fi
585 
586    # This process subtracts arg2 from arg1. If arg2 is larger
587    # then reverse the arguments. The calculations are done, and
588    # then the sign is reversed
589    if [ $1 -lt $2 ]
590        then
591        jul1=$2
592        jul2=$1
593    elif [ $1 -gt $2 ]
594        then
595        jul1=$1
596        jul2=$2
597    else
598        echo 0
599        IGCM_debug_PopStack "IGCM_date_DaysBetweenJulianDate"
600        return
601    fi
602
603    # 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 ))
608
609    # Subtract days
610    res=$(( $ddd1 - $ddd2 ))
611
612    # Then add days in year until year2 matches year1
613
614    if [ "${config_UserChoices_CalendarType}" = "360d" ] ; then
615        res=$(( ( ( $yyyy1 - $yyyy2 ) * 360 ) + $res ))
616    elif [ "${config_UserChoices_CalendarType}" = "noleap" ] ; then
617        res=$(( ( ( $yyyy1 - $yyyy2 ) * 365 ) + $res ))
618    elif ( [ "${config_UserChoices_CalendarType}" = "leap" ] || [ "${config_UserChoices_CalendarType}" = "gregorian" ] ) ; then
619        while [ $yyyy2 -lt $yyyy1 ]
620          do
621          IGCM_date_DaysInYear $yyyy2 diy
622          res=$(( $res + $diy ))
623          yyyy2=$(( $yyyy2 + 1 ))
624        done
625    fi
626
627    # if argument 2 was larger than argument 1 then
628    # the arguments were reversed before calculating
629    # adjust by reversing the sign
630    if [ $1 -lt $2 ]
631        then
632        res=$(( $res * -1 ))
633    fi
634 
635    # and output the results
636    echo $res
637
638    IGCM_debug_PopStack "IGCM_date_DaysBetweenJulianDate"
639}
640
641#D-#==================================================================
642#D-function IGCM_date_DaysBetweenGregorianDate ()
643#D-* Purpose: Calculate the days difference between two dates and reports
644#D-           the number days as grg1 - grg2
645#D-* Usage: IGCM_date_DaysBetweenGregorianDate grg1 grg2
646#D-         where gregorian date is in the form yyyymmdd
647#D-
648function IGCM_date_DaysBetweenGregorianDate
649{
650    IGCM_debug_PushStack "IGCM_date_DaysBetweenGregorianDate" $@
651
652    # calculates the days difference between two dates and reports
653    # the number days as grg1 - grg2
654    # usage IGCM_date_DaysBetweenGregorianDate grg1 grg2
655    # where gregorian date is in the form yyyymmdd
656
657    usage () {
658        echo "Usage:"
659        echo " IGCM_date_DaysBetweenGregorianDate grg1 grg2"
660        echo ""
661        echo " Calculate day difference between"
662        echo " two gregorian dates (grg1 - grg2)"
663        echo " where a gregorian date is in the form of yyyymmdd."
664    }
665
666    if [ $# -lt 2 ]; then
667        usage
668        IGCM_debug_Exit "IGCM_date_DaysBetweenGregorianDate"
669    fi
670
671    # convert each date to julian
672    grg1=$1
673    grg2=$2
674
675    jul1=$( IGCM_date_ConvertGregorianDateToJulian $grg1 )
676    jul2=$( IGCM_date_ConvertGregorianDateToJulian $grg2 )
677
678    if [ $jul1 -ne $jul2 ]; then
679    # calculate the answer using IGCM_date_DaysBetweenJulianDate
680        res=$( IGCM_date_DaysBetweenJulianDate $jul1 $jul2 )
681    # and output the results
682        echo $res
683    else
684        echo 0
685    fi
686
687    IGCM_debug_PopStack "IGCM_date_DaysBetweenGregorianDate"
688}
689
690#D-#==================================================================
691#D-function IGCM_date_DaysSinceJC ()
692#D-* Purpose: Calculate the days difference between a date and 00010101
693#D-* Usage: IGCM_date_DaysSinceJC grg1
694#D-         where gregorian date is in the form yyyymmdd
695#D-
696function IGCM_date_DaysSinceJC
697{
698    IGCM_debug_PushStack "IGCM_date_DaysSinceJC" $@
699
700    # calculates the days difference between a date and 00010101
701    # usage IGCM_date_DaysSinceJC grg1
702    # where gregorian date is in the form yyyymmdd
703
704    usage () {
705        echo "Usage:"
706        echo " IGCM_date_DaysSinceJC grg1"
707        echo ""
708        echo " Calculate day difference between"
709        echo " a gregorian date and 00010101"
710        echo " where a gregorian date is in the form of yyyymmdd."
711    }
712
713    if [ $# -lt 1 ]; then
714        usage
715        IGCM_debug_Exit "IGCM_date_DaysSinceJC"
716    fi
717   
718    typeset aux num
719
720    if   [ ${1} -lt  5000000 ]; then
721        case ${config_UserChoices_CalendarType} in
722            360d) 
723                aux=-360;;
724            noleap)
725                aux=-365;;
726            leap) 
727                aux=-366;;
728        esac
729        num=101
730    elif [ ${1} -lt 15000000 ]; then
731        # To save CPU type we use auxiliary value
732        # which is number of days since JC and 10000101
733        case ${config_UserChoices_CalendarType} in
734            360d) 
735                aux=359640;;
736            noleap)
737                aux=364635;;
738            leap) 
739                aux=364877;;
740        esac
741        num=10000101
742    else
743        # To save CPU type we use auxiliary value
744        # which is number of days since JC and 19000101
745        case ${config_UserChoices_CalendarType} in
746            360d) 
747                aux=683640;;
748            noleap)
749                aux=693135;;
750            leap) 
751                aux=693595;;
752        esac
753        num=19000101
754    fi
755    echo $(( $( IGCM_date_DaysBetweenGregorianDate $1 ${num} ) + $aux ))
756
757    IGCM_debug_PopStack "IGCM_date_DaysSinceJC"
758}
759
760#D-#==================================================================
761#D-function IGCM_date_Check
762#D- * Purpose: Check the present file by comparison with a reference file
763function IGCM_date_Check
764{
765    IGCM_debug_PushStack "IGCM_date_Check"
766
767#---------------------
768    if [ ! -n "${libIGCM}" ] ; then
769        echo "Check libIGCM_date ...........................................[ FAILED ]"
770        echo "--Error--> libIGCM variable is not defined"
771        IGCM_debug_Exit "IGCM_date_Check"
772    fi
773
774#---------------------
775    whence -v awk > /dev/null 2>&1
776    if [ ! $? -eq 0 ] ; then
777        echo "Check libIGCM_date ...........................................[ FAILED ]"
778        echo "--Error--> awk command is not defined"
779        IGCM_debug_Exit "IGCM_date_Check"
780    fi
781
782#---------------------
783    ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh > IGCM_date_Test.ref.failed 2>&1
784   
785    if diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref > /dev/null 2>&1 ; then
786        echo "Check libIGCM_date ...............................................[ OK ]"
787        rm -f IGCM_date_Test.ref.failed
788    else
789        echo "Check libIGCM_date ...........................................[ FAILED ]"
790        echo "--Error--> Execution of ${libIGCM}/libIGCM_date/IGCM_date_Test.ksh"
791        echo "           has produced the file IGCM_date_Test.ref.failed"
792        echo "           Please analyse differences with the reference file by typing:"
793        echo "           diff IGCM_date_Test.ref.failed ${libIGCM}/libIGCM_date/IGCM_date_Test${dY}.ref"
794        echo "           Report errors to the author: Sebastien.Denvil@ipsl.jussieu.fr"
795        IGCM_debug_Exit "IGCM_date_Check"
796    fi
797
798#---------------------
799    IGCM_debug_PopStack "IGCM_date_Check"
800}
801
802#==================================================================
Note: See TracBrowser for help on using the repository browser.