File: C:\NOAA\NEMS_11731\src\chem\gocart\src\GMAO_Shared\GMAO_gfio\diffdate.f

1     !-------------------------------------------------------------------------
2     !         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
3     !-------------------------------------------------------------------------
4     !BOI
5     !
6     !  !TITLE: Calculate difference in seconds between two times.
7     !
8     !  !AUTHORS: Rob Lucchesi
9     !
10     !  !AFFILIATION: Data Assimilation Office, NASA/GSFC, Greenbelt, MD 20771
11     !
12     !  !DATE: October 17, 1997
13     !
14     !EOI
15     !-------------------------------------------------------------------------
16     !-------------------------------------------------------------------------
17     !         NASA/GSFC, Data Assimilation Office, Code 910.3, GEOS/DAS      !
18     !-------------------------------------------------------------------------
19     !BOP
20     !
21     ! !ROUTINE: DiffDate --- Calculates the number of seconds between two times.
22     !
23     ! !INTERFACE:
24     !
25     
26            integer function DiffDate (yyyymmhh_1,hhmmss_1,yyyymmhh_2,
27          .                             hhmmss_2)
28     
29     ! 
30     ! !USES:
31     !
32     
33            implicit none
34     
35     !
36     ! !INPUT PARAMETERS:
37     !
38            
39            integer yyyymmhh_1               ! First date in YYYYYMMDD format
40            integer hhmmss_1                 ! First time in HHMMSS format
41            integer yyyymmhh_2               ! Second date in YYYYMMDD format
42            integer hhmmss_2                 ! Second time in HHMMSS format
43     
44     !
45     ! !OUTPUT PARAMETERS:
46     !
47     !                 Integer function returns number of seconds between the
48     !                 the times given as input.  -1 is returned in the event 
49     !                 of an error.  
50     !
51     ! !DESCRIPTION:   This function returns the number of seconds between two
52     !                 times.  Each time is specified with two integers, one 
53     !                 representing a date in the format YYYYMMDD and one 
54     !                 representing a time in the format HHMMSS.  This function 
55     !                 determines the Julian day of each date using the "julday"
56     !                 function from the book "Numerical Recipes in FORTRAN, the 
57     !                 art of scientific computing (2nd Ed.), by William H. Press, 
58     !                 Saul A. Teukolsky, William T. Vetterling, and Brian P. 
59     !                 Flannery (Cambridge University Press, 1992).  This julian
60     !                 day is reduced by a constant and converted to seconds.  The
61     !                 reduction is required to allow the conversion to seconds to
62     !                 fit in a 32 bit integer.  The difference between the two 
63     !                 times is then calculated and returned.  The times need not
64     !                 be in chronological order as the function returns the abs
65     !                 value.  -1 is returned in the event of an error.
66     !
67     ! !REVISION HISTORY:
68     !
69     !  17Oct97   Lucchesi    Initial version.
70     !
71     !EOP
72     !-------------------------------------------------------------------------
73     
74            integer StartDate, julday
75            parameter (StartDate = 2439321)   ! Use birthday of author as base date
76     
77            integer year1,mon1,day1,hour1,min1,sec1
78            integer year2,mon2,day2,hour2,min2,sec2
79            integer julian1, julian2, julsec1, julsec2
80     
81            character*8 dateString
82     
83     ! Error checking.
84     
85            if (yyyymmhh_1 .lt. 19000000 .or. yyyymmhh_1 .gt. 21000000 ) then
86              DiffDate=-1
87              return
88            endif
89            if (yyyymmhh_2 .lt. 19000000 .or. yyyymmhh_2 .gt. 21000000 ) then
90              DiffDate=-1
91              return
92            endif
93            if (hhmmss_1 .lt. 0 .or. hhmmss_1 .ge. 240000 ) then
94              DiffDate=-1
95              return
96            endif
97            if (hhmmss_2 .lt. 0 .or. hhmmss_2 .ge. 240000 ) then
98              DiffDate=-1
99              return
100            endif
101     
102     ! Convert Date/Time strings to integer variables.
103     
104            write (dateString, 200) yyyymmhh_1
105     200    format (I8)
106            read (dateString, 201) year1, mon1, day1
107     201    format (I4,2I2)
108            write (dateString, 200) yyyymmhh_2
109            read (dateString, 201) year2, mon2, day2
110            write (dateString, 202) hhmmss_1
111     202    format (I6)
112            read (dateString, 203) hour1, min1, sec1
113     203    format (3I2)
114            write (dateString, 202) hhmmss_2
115            read (dateString, 203) hour2, min2, sec2
116     
117     ! Get Julian Days and subtract off a constant (Julian days since 7/14/66)
118      
119            julian1 = julday (mon1, day1, year1)
120            julian1 = julian1 - StartDate
121            julian2 = julday (mon2, day2, year2)
122            julian2 = julian2 - StartDate
123           
124     ! Calculcate Julian seconds
125     
126            julsec1 = (julian1-1)*86400 + hour1*3600 + min1*60 + sec1
127            julsec2 = (julian2-1)*86400 + hour2*3600 + min2*60 + sec2
128            
129     !!!       DiffDate = iabs (julsec2 - julsec1)
130            DiffDate = julsec2 - julsec1
131     
132            return
133            end
134