| 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:  2016 December 31   : | 
|---|
| 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 TAI-UTC 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 TAI-UTC 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-2017, 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 = 2016}; | 
|---|
| 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 | { 2017,  1, 37.0       } | 
|---|
| 193 | }; | 
|---|
| 194 |  | 
|---|
| 195 | /* Number of Delta(AT) changes */ | 
|---|
| 196 | enum { NDAT = (int) (sizeof changes / sizeof changes[0]) }; | 
|---|
| 197 |  | 
|---|
| 198 | /* Miscellaneous local variables */ | 
|---|
| 199 | int j, i, m; | 
|---|
| 200 | double da, djm0, djm; | 
|---|
| 201 |  | 
|---|
| 202 |  | 
|---|
| 203 | /* Initialize the result to zero. */ | 
|---|
| 204 | *deltat = da = 0.0; | 
|---|
| 205 |  | 
|---|
| 206 | /* If invalid fraction of a day, set error status and give up. */ | 
|---|
| 207 | if (fd < 0.0 || fd > 1.0) return -4; | 
|---|
| 208 |  | 
|---|
| 209 | /* Convert the date into an MJD. */ | 
|---|
| 210 | j = eraCal2jd(iy, im, id, &djm0, &djm); | 
|---|
| 211 |  | 
|---|
| 212 | /* If invalid year, month, or day, give up. */ | 
|---|
| 213 | if (j < 0) return j; | 
|---|
| 214 |  | 
|---|
| 215 | /* If pre-UTC year, set warning status and give up. */ | 
|---|
| 216 | if (iy < changes[0].iyear) return 1; | 
|---|
| 217 |  | 
|---|
| 218 | /* If suspiciously late year, set warning status but proceed. */ | 
|---|
| 219 | if (iy > IYV + 5) j = 1; | 
|---|
| 220 |  | 
|---|
| 221 | /* Combine year and month to form a date-ordered integer... */ | 
|---|
| 222 | m = 12*iy + im; | 
|---|
| 223 |  | 
|---|
| 224 | /* ...and use it to find the preceding table entry. */ | 
|---|
| 225 | for (i = NDAT-1; i >=0; i--) { | 
|---|
| 226 | if (m >= (12 * changes[i].iyear + changes[i].month)) break; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | /* Prevent underflow warnings. */ | 
|---|
| 230 | if (i < 0) return -5; | 
|---|
| 231 |  | 
|---|
| 232 | /* Get the Delta(AT). */ | 
|---|
| 233 | da = changes[i].delat; | 
|---|
| 234 |  | 
|---|
| 235 | /* If pre-1972, adjust for drift. */ | 
|---|
| 236 | if (i < NERA1) da += (djm + fd - drift[i][0]) * drift[i][1]; | 
|---|
| 237 |  | 
|---|
| 238 | /* Return the Delta(AT) value. */ | 
|---|
| 239 | *deltat = da; | 
|---|
| 240 |  | 
|---|
| 241 | /* Return the status. */ | 
|---|
| 242 | return j; | 
|---|
| 243 |  | 
|---|
| 244 | } | 
|---|
| 245 | /*---------------------------------------------------------------------- | 
|---|
| 246 | ** | 
|---|
| 247 | ** | 
|---|
| 248 | **  Copyright (C) 2013-2017, NumFOCUS Foundation. | 
|---|
| 249 | **  All rights reserved. | 
|---|
| 250 | ** | 
|---|
| 251 | **  This library is derived, with permission, from the International | 
|---|
| 252 | **  Astronomical Union's "Standards of Fundamental Astronomy" library, | 
|---|
| 253 | **  available from http://www.iausofa.org. | 
|---|
| 254 | ** | 
|---|
| 255 | **  The ERFA version is intended to retain identical functionality to | 
|---|
| 256 | **  the SOFA library, but made distinct through different function and | 
|---|
| 257 | **  file names, as set out in the SOFA license conditions.  The SOFA | 
|---|
| 258 | **  original has a role as a reference standard for the IAU and IERS, | 
|---|
| 259 | **  and consequently redistribution is permitted only in its unaltered | 
|---|
| 260 | **  state.  The ERFA version is not subject to this restriction and | 
|---|
| 261 | **  therefore can be included in distributions which do not support the | 
|---|
| 262 | **  concept of "read only" software. | 
|---|
| 263 | ** | 
|---|
| 264 | **  Although the intent is to replicate the SOFA API (other than | 
|---|
| 265 | **  replacement of prefix names) and results (with the exception of | 
|---|
| 266 | **  bugs;  any that are discovered will be fixed), SOFA is not | 
|---|
| 267 | **  responsible for any errors found in this version of the library. | 
|---|
| 268 | ** | 
|---|
| 269 | **  If you wish to acknowledge the SOFA heritage, please acknowledge | 
|---|
| 270 | **  that you are using a library derived from SOFA, rather than SOFA | 
|---|
| 271 | **  itself. | 
|---|
| 272 | ** | 
|---|
| 273 | ** | 
|---|
| 274 | **  TERMS AND CONDITIONS | 
|---|
| 275 | ** | 
|---|
| 276 | **  Redistribution and use in source and binary forms, with or without | 
|---|
| 277 | **  modification, are permitted provided that the following conditions | 
|---|
| 278 | **  are met: | 
|---|
| 279 | ** | 
|---|
| 280 | **  1 Redistributions of source code must retain the above copyright | 
|---|
| 281 | **    notice, this list of conditions and the following disclaimer. | 
|---|
| 282 | ** | 
|---|
| 283 | **  2 Redistributions in binary form must reproduce the above copyright | 
|---|
| 284 | **    notice, this list of conditions and the following disclaimer in | 
|---|
| 285 | **    the documentation and/or other materials provided with the | 
|---|
| 286 | **    distribution. | 
|---|
| 287 | ** | 
|---|
| 288 | **  3 Neither the name of the Standards Of Fundamental Astronomy Board, | 
|---|
| 289 | **    the International Astronomical Union nor the names of its | 
|---|
| 290 | **    contributors may be used to endorse or promote products derived | 
|---|
| 291 | **    from this software without specific prior written permission. | 
|---|
| 292 | ** | 
|---|
| 293 | **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|---|
| 294 | **  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|---|
| 295 | **  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|---|
| 296 | **  FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE | 
|---|
| 297 | **  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 298 | **  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|---|
| 299 | **  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
|---|
| 300 | **  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
|---|
| 301 | **  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 302 | **  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | 
|---|
| 303 | **  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
|---|
| 304 | **  POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 305 | ** | 
|---|
| 306 | */ | 
|---|