FACT++  1.0
dat.c
Go to the documentation of this file.
1 #include "erfa.h"
2 
3 int eraDat(int iy, int im, int id, double fd, double *deltat )
4 /*
5 ** - - - - - - -
6 ** e r a D a t
7 ** - - - - - - -
8 **
9 ** For a given UTC date, calculate delta(AT) = TAI-UTC.
10 **
11 ** :------------------------------------------:
12 ** : :
13 ** : IMPORTANT :
14 ** : :
15 ** : A new version of this function must be :
16 ** : produced whenever a new leap second is :
17 ** : announced. There are four items to :
18 ** : change on each such occasion: :
19 ** : :
20 ** : 1) A new line must be added to the set :
21 ** : of statements that initialize the :
22 ** : array "changes". :
23 ** : :
24 ** : 2) The constant IYV must be set to the :
25 ** : current year. :
26 ** : :
27 ** : 3) The "Latest leap second" comment :
28 ** : below must be set to the new leap :
29 ** : second date. :
30 ** : :
31 ** : 4) The "This revision" comment, later, :
32 ** : must be set to the current date. :
33 ** : :
34 ** : Change (2) must also be carried out :
35 ** : whenever the function is re-issued, :
36 ** : even if no leap seconds have been :
37 ** : added. :
38 ** : :
39 ** : Latest leap second: 2015 June 30 :
40 ** : :
41 ** :__________________________________________:
42 **
43 ** Given:
44 ** iy int UTC: year (Notes 1 and 2)
45 ** im int month (Note 2)
46 ** id int day (Notes 2 and 3)
47 ** fd double fraction of day (Note 4)
48 **
49 ** Returned:
50 ** deltat double TAI minus UTC, seconds
51 **
52 ** Returned (function value):
53 ** int status (Note 5):
54 ** 1 = dubious year (Note 1)
55 ** 0 = OK
56 ** -1 = bad year
57 ** -2 = bad month
58 ** -3 = bad day (Note 3)
59 ** -4 = bad fraction (Note 4)
60 ** -5 = internal error (Note 5)
61 **
62 ** Notes:
63 **
64 ** 1) UTC began at 1960 January 1.0 (JD 2436934.5) and it is improper
65 ** to call the function with an earlier date. If this is attempted,
66 ** zero is returned together with a warning status.
67 **
68 ** Because leap seconds cannot, in principle, be predicted in
69 ** advance, a reliable check for dates beyond the valid range is
70 ** impossible. To guard against gross errors, a year five or more
71 ** after the release year of the present function (see the constant
72 ** IYV) is considered dubious. In this case a warning status is
73 ** returned but the result is computed in the normal way.
74 **
75 ** For both too-early and too-late years, the warning status is +1.
76 ** This is distinct from the error status -1, which signifies a year
77 ** so early that JD could not be computed.
78 **
79 ** 2) If the specified date is for a day which ends with a leap second,
80 ** the UTC-TAI value returned is for the period leading up to the
81 ** leap second. If the date is for a day which begins as a leap
82 ** second ends, the UTC-TAI returned is for the period following the
83 ** leap second.
84 **
85 ** 3) The day number must be in the normal calendar range, for example
86 ** 1 through 30 for April. The "almanac" convention of allowing
87 ** such dates as January 0 and December 32 is not supported in this
88 ** function, in order to avoid confusion near leap seconds.
89 **
90 ** 4) The fraction of day is used only for dates before the
91 ** introduction of leap seconds, the first of which occurred at the
92 ** end of 1971. It is tested for validity (0 to 1 is the valid
93 ** range) even if not used; if invalid, zero is used and status -4
94 ** is returned. For many applications, setting fd to zero is
95 ** acceptable; the resulting error is always less than 3 ms (and
96 ** occurs only pre-1972).
97 **
98 ** 5) The status value returned in the case where there are multiple
99 ** errors refers to the first error detected. For example, if the
100 ** month and day are 13 and 32 respectively, status -2 (bad month)
101 ** will be returned. The "internal error" status refers to a
102 ** case that is impossible but causes some compilers to issue a
103 ** warning.
104 **
105 ** 6) In cases where a valid result is not available, zero is returned.
106 **
107 ** References:
108 **
109 ** 1) For dates from 1961 January 1 onwards, the expressions from the
110 ** file ftp://maia.usno.navy.mil/ser7/tai-utc.dat are used.
111 **
112 ** 2) The 5ms timestep at 1961 January 1 is taken from 2.58.1 (p87) of
113 ** the 1992 Explanatory Supplement.
114 **
115 ** Called:
116 ** eraCal2jd Gregorian calendar to JD
117 **
118 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
119 ** Derived, with permission, from the SOFA library. See notes at end of file.
120 */
121 {
122 /* Release year for this version of eraDat */
123  enum { IYV = 2015};
124 
125 /* Reference dates (MJD) and drift rates (s/day), pre leap seconds */
126  static const double drift[][2] = {
127  { 37300.0, 0.0012960 },
128  { 37300.0, 0.0012960 },
129  { 37300.0, 0.0012960 },
130  { 37665.0, 0.0011232 },
131  { 37665.0, 0.0011232 },
132  { 38761.0, 0.0012960 },
133  { 38761.0, 0.0012960 },
134  { 38761.0, 0.0012960 },
135  { 38761.0, 0.0012960 },
136  { 38761.0, 0.0012960 },
137  { 38761.0, 0.0012960 },
138  { 38761.0, 0.0012960 },
139  { 39126.0, 0.0025920 },
140  { 39126.0, 0.0025920 }
141  };
142 
143 /* Number of Delta(AT) expressions before leap seconds were introduced */
144  enum { NERA1 = (int) (sizeof drift / sizeof (double) / 2) };
145 
146 /* Dates and Delta(AT)s */
147  static const struct {
148  int iyear, month;
149  double delat;
150  } changes[] = {
151  { 1960, 1, 1.4178180 },
152  { 1961, 1, 1.4228180 },
153  { 1961, 8, 1.3728180 },
154  { 1962, 1, 1.8458580 },
155  { 1963, 11, 1.9458580 },
156  { 1964, 1, 3.2401300 },
157  { 1964, 4, 3.3401300 },
158  { 1964, 9, 3.4401300 },
159  { 1965, 1, 3.5401300 },
160  { 1965, 3, 3.6401300 },
161  { 1965, 7, 3.7401300 },
162  { 1965, 9, 3.8401300 },
163  { 1966, 1, 4.3131700 },
164  { 1968, 2, 4.2131700 },
165  { 1972, 1, 10.0 },
166  { 1972, 7, 11.0 },
167  { 1973, 1, 12.0 },
168  { 1974, 1, 13.0 },
169  { 1975, 1, 14.0 },
170  { 1976, 1, 15.0 },
171  { 1977, 1, 16.0 },
172  { 1978, 1, 17.0 },
173  { 1979, 1, 18.0 },
174  { 1980, 1, 19.0 },
175  { 1981, 7, 20.0 },
176  { 1982, 7, 21.0 },
177  { 1983, 7, 22.0 },
178  { 1985, 7, 23.0 },
179  { 1988, 1, 24.0 },
180  { 1990, 1, 25.0 },
181  { 1991, 1, 26.0 },
182  { 1992, 7, 27.0 },
183  { 1993, 7, 28.0 },
184  { 1994, 7, 29.0 },
185  { 1996, 1, 30.0 },
186  { 1997, 7, 31.0 },
187  { 1999, 1, 32.0 },
188  { 2006, 1, 33.0 },
189  { 2009, 1, 34.0 },
190  { 2012, 7, 35.0 },
191  { 2015, 7, 36.0 }
192  };
193 
194 /* Number of Delta(AT) changes */
195  enum { NDAT = (int) (sizeof changes / sizeof changes[0]) };
196 
197 /* Miscellaneous local variables */
198  int j, i, m;
199  double da, djm0, djm;
200 
201 
202 /* Initialize the result to zero. */
203  *deltat = da = 0.0;
204 
205 /* If invalid fraction of a day, set error status and give up. */
206  if (fd < 0.0 || fd > 1.0) return -4;
207 
208 /* Convert the date into an MJD. */
209  j = eraCal2jd(iy, im, id, &djm0, &djm);
210 
211 /* If invalid year, month, or day, give up. */
212  if (j < 0) return j;
213 
214 /* If pre-UTC year, set warning status and give up. */
215  if (iy < changes[0].iyear) return 1;
216 
217 /* If suspiciously late year, set warning status but proceed. */
218  if (iy > IYV + 5) j = 1;
219 
220 /* Combine year and month to form a date-ordered integer... */
221  m = 12*iy + im;
222 
223 /* ...and use it to find the preceding table entry. */
224  for (i = NDAT-1; i >=0; i--) {
225  if (m >= (12 * changes[i].iyear + changes[i].month)) break;
226  }
227 
228 /* Prevent underflow warnings. */
229  if (i < 0) return -5;
230 
231 /* Get the Delta(AT). */
232  da = changes[i].delat;
233 
234 /* If pre-1972, adjust for drift. */
235  if (i < NERA1) da += (djm + fd - drift[i][0]) * drift[i][1];
236 
237 /* Return the Delta(AT) value. */
238  *deltat = da;
239 
240 /* Return the status. */
241  return j;
242 
243 }
244 /*----------------------------------------------------------------------
245 **
246 **
247 ** Copyright (C) 2013-2015, NumFOCUS Foundation.
248 ** All rights reserved.
249 **
250 ** This library is derived, with permission, from the International
251 ** Astronomical Union's "Standards of Fundamental Astronomy" library,
252 ** available from http://www.iausofa.org.
253 **
254 ** The ERFA version is intended to retain identical functionality to
255 ** the SOFA library, but made distinct through different function and
256 ** file names, as set out in the SOFA license conditions. The SOFA
257 ** original has a role as a reference standard for the IAU and IERS,
258 ** and consequently redistribution is permitted only in its unaltered
259 ** state. The ERFA version is not subject to this restriction and
260 ** therefore can be included in distributions which do not support the
261 ** concept of "read only" software.
262 **
263 ** Although the intent is to replicate the SOFA API (other than
264 ** replacement of prefix names) and results (with the exception of
265 ** bugs; any that are discovered will be fixed), SOFA is not
266 ** responsible for any errors found in this version of the library.
267 **
268 ** If you wish to acknowledge the SOFA heritage, please acknowledge
269 ** that you are using a library derived from SOFA, rather than SOFA
270 ** itself.
271 **
272 **
273 ** TERMS AND CONDITIONS
274 **
275 ** Redistribution and use in source and binary forms, with or without
276 ** modification, are permitted provided that the following conditions
277 ** are met:
278 **
279 ** 1 Redistributions of source code must retain the above copyright
280 ** notice, this list of conditions and the following disclaimer.
281 **
282 ** 2 Redistributions in binary form must reproduce the above copyright
283 ** notice, this list of conditions and the following disclaimer in
284 ** the documentation and/or other materials provided with the
285 ** distribution.
286 **
287 ** 3 Neither the name of the Standards Of Fundamental Astronomy Board,
288 ** the International Astronomical Union nor the names of its
289 ** contributors may be used to endorse or promote products derived
290 ** from this software without specific prior written permission.
291 **
292 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
293 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
294 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
295 ** FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
296 ** COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
297 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
298 ** BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
299 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
300 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
302 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
303 ** POSSIBILITY OF SUCH DAMAGE.
304 **
305 */
int i
Definition: db_dim_client.c:21
int eraDat(int iy, int im, int id, double fd, double *deltat)
Definition: dat.c:3
int eraCal2jd(int iy, int im, int id, double *djm0, double *djm)
Definition: cal2jd.c:3