EarthOrientationParameters.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import binarySearch from "./binarySearch.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defined from "./defined.js";
  4. import EarthOrientationParametersSample from "./EarthOrientationParametersSample.js";
  5. import JulianDate from "./JulianDate.js";
  6. import LeapSecond from "./LeapSecond.js";
  7. import Resource from "./Resource.js";
  8. import RuntimeError from "./RuntimeError.js";
  9. import TimeConstants from "./TimeConstants.js";
  10. import TimeStandard from "./TimeStandard.js";
  11. /**
  12. * Specifies Earth polar motion coordinates and the difference between UT1 and UTC.
  13. * These Earth Orientation Parameters (EOP) are primarily used in the transformation from
  14. * the International Celestial Reference Frame (ICRF) to the International Terrestrial
  15. * Reference Frame (ITRF).
  16. *
  17. * @alias EarthOrientationParameters
  18. * @constructor
  19. *
  20. * @param {Object} [options] Object with the following properties:
  21. * @param {Resource|String} [options.url] The URL from which to obtain EOP data. If neither this
  22. * parameter nor options.data is specified, all EOP values are assumed
  23. * to be 0.0. If options.data is specified, this parameter is
  24. * ignored.
  25. * @param {Object} [options.data] The actual EOP data. If neither this
  26. * parameter nor options.data is specified, all EOP values are assumed
  27. * to be 0.0.
  28. * @param {Boolean} [options.addNewLeapSeconds=true] True if leap seconds that
  29. * are specified in the EOP data but not in {@link JulianDate.leapSeconds}
  30. * should be added to {@link JulianDate.leapSeconds}. False if
  31. * new leap seconds should be handled correctly in the context
  32. * of the EOP data but otherwise ignored.
  33. *
  34. * @example
  35. * // An example EOP data file, EOP.json:
  36. * {
  37. * "columnNames" : ["dateIso8601","modifiedJulianDateUtc","xPoleWanderRadians","yPoleWanderRadians","ut1MinusUtcSeconds","lengthOfDayCorrectionSeconds","xCelestialPoleOffsetRadians","yCelestialPoleOffsetRadians","taiMinusUtcSeconds"],
  38. * "samples" : [
  39. * "2011-07-01T00:00:00Z",55743.0,2.117957047295119e-7,2.111518721609984e-6,-0.2908948,-2.956e-4,3.393695767766752e-11,3.3452143996557983e-10,34.0,
  40. * "2011-07-02T00:00:00Z",55744.0,2.193297093339541e-7,2.115460256837405e-6,-0.29065,-1.824e-4,-8.241832578862112e-11,5.623838700870617e-10,34.0,
  41. * "2011-07-03T00:00:00Z",55745.0,2.262286080161428e-7,2.1191157519929706e-6,-0.2905572,1.9e-6,-3.490658503988659e-10,6.981317007977318e-10,34.0
  42. * ]
  43. * }
  44. *
  45. * @example
  46. * // Loading the EOP data
  47. * const eop = new Cesium.EarthOrientationParameters({ url : 'Data/EOP.json' });
  48. * Cesium.Transforms.earthOrientationParameters = eop;
  49. *
  50. * @private
  51. */
  52. function EarthOrientationParameters(options) {
  53. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  54. this._dates = undefined;
  55. this._samples = undefined;
  56. this._dateColumn = -1;
  57. this._xPoleWanderRadiansColumn = -1;
  58. this._yPoleWanderRadiansColumn = -1;
  59. this._ut1MinusUtcSecondsColumn = -1;
  60. this._xCelestialPoleOffsetRadiansColumn = -1;
  61. this._yCelestialPoleOffsetRadiansColumn = -1;
  62. this._taiMinusUtcSecondsColumn = -1;
  63. this._columnCount = 0;
  64. this._lastIndex = -1;
  65. this._downloadPromise = undefined;
  66. this._dataError = undefined;
  67. this._addNewLeapSeconds = defaultValue(options.addNewLeapSeconds, true);
  68. if (defined(options.data)) {
  69. // Use supplied EOP data.
  70. onDataReady(this, options.data);
  71. } else if (defined(options.url)) {
  72. const resource = Resource.createIfNeeded(options.url);
  73. // Download EOP data.
  74. const that = this;
  75. this._downloadPromise = resource
  76. .fetchJson()
  77. .then(function (eopData) {
  78. onDataReady(that, eopData);
  79. })
  80. .catch(function () {
  81. that._dataError = `An error occurred while retrieving the EOP data from the URL ${resource.url}.`;
  82. });
  83. } else {
  84. // Use all zeros for EOP data.
  85. onDataReady(this, {
  86. columnNames: [
  87. "dateIso8601",
  88. "modifiedJulianDateUtc",
  89. "xPoleWanderRadians",
  90. "yPoleWanderRadians",
  91. "ut1MinusUtcSeconds",
  92. "lengthOfDayCorrectionSeconds",
  93. "xCelestialPoleOffsetRadians",
  94. "yCelestialPoleOffsetRadians",
  95. "taiMinusUtcSeconds",
  96. ],
  97. samples: [],
  98. });
  99. }
  100. }
  101. /**
  102. * A default {@link EarthOrientationParameters} instance that returns zero for all EOP values.
  103. */
  104. EarthOrientationParameters.NONE = Object.freeze({
  105. getPromiseToLoad: function () {
  106. return Promise.resolve();
  107. },
  108. compute: function (date, result) {
  109. if (!defined(result)) {
  110. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  111. } else {
  112. result.xPoleWander = 0.0;
  113. result.yPoleWander = 0.0;
  114. result.xPoleOffset = 0.0;
  115. result.yPoleOffset = 0.0;
  116. result.ut1MinusUtc = 0.0;
  117. }
  118. return result;
  119. },
  120. });
  121. /**
  122. * Gets a promise that, when resolved, indicates that the EOP data has been loaded and is
  123. * ready to use.
  124. *
  125. * @returns {Promise<void>} The promise.
  126. */
  127. EarthOrientationParameters.prototype.getPromiseToLoad = function () {
  128. return Promise.resolve(this._downloadPromise);
  129. };
  130. /**
  131. * Computes the Earth Orientation Parameters (EOP) for a given date by interpolating.
  132. * If the EOP data has not yet been download, this method returns undefined.
  133. *
  134. * @param {JulianDate} date The date for each to evaluate the EOP.
  135. * @param {EarthOrientationParametersSample} [result] The instance to which to copy the result.
  136. * If this parameter is undefined, a new instance is created and returned.
  137. * @returns {EarthOrientationParametersSample} The EOP evaluated at the given date, or
  138. * undefined if the data necessary to evaluate EOP at the date has not yet been
  139. * downloaded.
  140. *
  141. * @exception {RuntimeError} The loaded EOP data has an error and cannot be used.
  142. *
  143. * @see EarthOrientationParameters#getPromiseToLoad
  144. */
  145. EarthOrientationParameters.prototype.compute = function (date, result) {
  146. // We cannot compute until the samples are available.
  147. if (!defined(this._samples)) {
  148. if (defined(this._dataError)) {
  149. throw new RuntimeError(this._dataError);
  150. }
  151. return undefined;
  152. }
  153. if (!defined(result)) {
  154. result = new EarthOrientationParametersSample(0.0, 0.0, 0.0, 0.0, 0.0);
  155. }
  156. if (this._samples.length === 0) {
  157. result.xPoleWander = 0.0;
  158. result.yPoleWander = 0.0;
  159. result.xPoleOffset = 0.0;
  160. result.yPoleOffset = 0.0;
  161. result.ut1MinusUtc = 0.0;
  162. return result;
  163. }
  164. const dates = this._dates;
  165. const lastIndex = this._lastIndex;
  166. let before = 0;
  167. let after = 0;
  168. if (defined(lastIndex)) {
  169. const previousIndexDate = dates[lastIndex];
  170. const nextIndexDate = dates[lastIndex + 1];
  171. const isAfterPrevious = JulianDate.lessThanOrEquals(
  172. previousIndexDate,
  173. date
  174. );
  175. const isAfterLastSample = !defined(nextIndexDate);
  176. const isBeforeNext =
  177. isAfterLastSample || JulianDate.greaterThanOrEquals(nextIndexDate, date);
  178. if (isAfterPrevious && isBeforeNext) {
  179. before = lastIndex;
  180. if (!isAfterLastSample && nextIndexDate.equals(date)) {
  181. ++before;
  182. }
  183. after = before + 1;
  184. interpolate(this, dates, this._samples, date, before, after, result);
  185. return result;
  186. }
  187. }
  188. let index = binarySearch(dates, date, JulianDate.compare, this._dateColumn);
  189. if (index >= 0) {
  190. // If the next entry is the same date, use the later entry. This way, if two entries
  191. // describe the same moment, one before a leap second and the other after, then we will use
  192. // the post-leap second data.
  193. if (index < dates.length - 1 && dates[index + 1].equals(date)) {
  194. ++index;
  195. }
  196. before = index;
  197. after = index;
  198. } else {
  199. after = ~index;
  200. before = after - 1;
  201. // Use the first entry if the date requested is before the beginning of the data.
  202. if (before < 0) {
  203. before = 0;
  204. }
  205. }
  206. this._lastIndex = before;
  207. interpolate(this, dates, this._samples, date, before, after, result);
  208. return result;
  209. };
  210. function compareLeapSecondDates(leapSecond, dateToFind) {
  211. return JulianDate.compare(leapSecond.julianDate, dateToFind);
  212. }
  213. function onDataReady(eop, eopData) {
  214. if (!defined(eopData.columnNames)) {
  215. eop._dataError =
  216. "Error in loaded EOP data: The columnNames property is required.";
  217. return;
  218. }
  219. if (!defined(eopData.samples)) {
  220. eop._dataError =
  221. "Error in loaded EOP data: The samples property is required.";
  222. return;
  223. }
  224. const dateColumn = eopData.columnNames.indexOf("modifiedJulianDateUtc");
  225. const xPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  226. "xPoleWanderRadians"
  227. );
  228. const yPoleWanderRadiansColumn = eopData.columnNames.indexOf(
  229. "yPoleWanderRadians"
  230. );
  231. const ut1MinusUtcSecondsColumn = eopData.columnNames.indexOf(
  232. "ut1MinusUtcSeconds"
  233. );
  234. const xCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  235. "xCelestialPoleOffsetRadians"
  236. );
  237. const yCelestialPoleOffsetRadiansColumn = eopData.columnNames.indexOf(
  238. "yCelestialPoleOffsetRadians"
  239. );
  240. const taiMinusUtcSecondsColumn = eopData.columnNames.indexOf(
  241. "taiMinusUtcSeconds"
  242. );
  243. if (
  244. dateColumn < 0 ||
  245. xPoleWanderRadiansColumn < 0 ||
  246. yPoleWanderRadiansColumn < 0 ||
  247. ut1MinusUtcSecondsColumn < 0 ||
  248. xCelestialPoleOffsetRadiansColumn < 0 ||
  249. yCelestialPoleOffsetRadiansColumn < 0 ||
  250. taiMinusUtcSecondsColumn < 0
  251. ) {
  252. eop._dataError =
  253. "Error in loaded EOP data: The columnNames property must include modifiedJulianDateUtc, xPoleWanderRadians, yPoleWanderRadians, ut1MinusUtcSeconds, xCelestialPoleOffsetRadians, yCelestialPoleOffsetRadians, and taiMinusUtcSeconds columns";
  254. return;
  255. }
  256. const samples = (eop._samples = eopData.samples);
  257. const dates = (eop._dates = []);
  258. eop._dateColumn = dateColumn;
  259. eop._xPoleWanderRadiansColumn = xPoleWanderRadiansColumn;
  260. eop._yPoleWanderRadiansColumn = yPoleWanderRadiansColumn;
  261. eop._ut1MinusUtcSecondsColumn = ut1MinusUtcSecondsColumn;
  262. eop._xCelestialPoleOffsetRadiansColumn = xCelestialPoleOffsetRadiansColumn;
  263. eop._yCelestialPoleOffsetRadiansColumn = yCelestialPoleOffsetRadiansColumn;
  264. eop._taiMinusUtcSecondsColumn = taiMinusUtcSecondsColumn;
  265. eop._columnCount = eopData.columnNames.length;
  266. eop._lastIndex = undefined;
  267. let lastTaiMinusUtc;
  268. const addNewLeapSeconds = eop._addNewLeapSeconds;
  269. // Convert the ISO8601 dates to JulianDates.
  270. for (let i = 0, len = samples.length; i < len; i += eop._columnCount) {
  271. const mjd = samples[i + dateColumn];
  272. const taiMinusUtc = samples[i + taiMinusUtcSecondsColumn];
  273. const day = mjd + TimeConstants.MODIFIED_JULIAN_DATE_DIFFERENCE;
  274. const date = new JulianDate(day, taiMinusUtc, TimeStandard.TAI);
  275. dates.push(date);
  276. if (addNewLeapSeconds) {
  277. if (taiMinusUtc !== lastTaiMinusUtc && defined(lastTaiMinusUtc)) {
  278. // We crossed a leap second boundary, so add the leap second
  279. // if it does not already exist.
  280. const leapSeconds = JulianDate.leapSeconds;
  281. const leapSecondIndex = binarySearch(
  282. leapSeconds,
  283. date,
  284. compareLeapSecondDates
  285. );
  286. if (leapSecondIndex < 0) {
  287. const leapSecond = new LeapSecond(date, taiMinusUtc);
  288. leapSeconds.splice(~leapSecondIndex, 0, leapSecond);
  289. }
  290. }
  291. lastTaiMinusUtc = taiMinusUtc;
  292. }
  293. }
  294. }
  295. function fillResultFromIndex(eop, samples, index, columnCount, result) {
  296. const start = index * columnCount;
  297. result.xPoleWander = samples[start + eop._xPoleWanderRadiansColumn];
  298. result.yPoleWander = samples[start + eop._yPoleWanderRadiansColumn];
  299. result.xPoleOffset = samples[start + eop._xCelestialPoleOffsetRadiansColumn];
  300. result.yPoleOffset = samples[start + eop._yCelestialPoleOffsetRadiansColumn];
  301. result.ut1MinusUtc = samples[start + eop._ut1MinusUtcSecondsColumn];
  302. }
  303. function linearInterp(dx, y1, y2) {
  304. return y1 + dx * (y2 - y1);
  305. }
  306. function interpolate(eop, dates, samples, date, before, after, result) {
  307. const columnCount = eop._columnCount;
  308. // First check the bounds on the EOP data
  309. // If we are after the bounds of the data, return zeros.
  310. // The 'before' index should never be less than zero.
  311. if (after > dates.length - 1) {
  312. result.xPoleWander = 0;
  313. result.yPoleWander = 0;
  314. result.xPoleOffset = 0;
  315. result.yPoleOffset = 0;
  316. result.ut1MinusUtc = 0;
  317. return result;
  318. }
  319. const beforeDate = dates[before];
  320. const afterDate = dates[after];
  321. if (beforeDate.equals(afterDate) || date.equals(beforeDate)) {
  322. fillResultFromIndex(eop, samples, before, columnCount, result);
  323. return result;
  324. } else if (date.equals(afterDate)) {
  325. fillResultFromIndex(eop, samples, after, columnCount, result);
  326. return result;
  327. }
  328. const factor =
  329. JulianDate.secondsDifference(date, beforeDate) /
  330. JulianDate.secondsDifference(afterDate, beforeDate);
  331. const startBefore = before * columnCount;
  332. const startAfter = after * columnCount;
  333. // Handle UT1 leap second edge case
  334. let beforeUt1MinusUtc = samples[startBefore + eop._ut1MinusUtcSecondsColumn];
  335. let afterUt1MinusUtc = samples[startAfter + eop._ut1MinusUtcSecondsColumn];
  336. const offsetDifference = afterUt1MinusUtc - beforeUt1MinusUtc;
  337. if (offsetDifference > 0.5 || offsetDifference < -0.5) {
  338. // The absolute difference between the values is more than 0.5, so we may have
  339. // crossed a leap second. Check if this is the case and, if so, adjust the
  340. // afterValue to account for the leap second. This way, our interpolation will
  341. // produce reasonable results.
  342. const beforeTaiMinusUtc =
  343. samples[startBefore + eop._taiMinusUtcSecondsColumn];
  344. const afterTaiMinusUtc =
  345. samples[startAfter + eop._taiMinusUtcSecondsColumn];
  346. if (beforeTaiMinusUtc !== afterTaiMinusUtc) {
  347. if (afterDate.equals(date)) {
  348. // If we are at the end of the leap second interval, take the second value
  349. // Otherwise, the interpolation below will yield the wrong side of the
  350. // discontinuity
  351. // At the end of the leap second, we need to start accounting for the jump
  352. beforeUt1MinusUtc = afterUt1MinusUtc;
  353. } else {
  354. // Otherwise, remove the leap second so that the interpolation is correct
  355. afterUt1MinusUtc -= afterTaiMinusUtc - beforeTaiMinusUtc;
  356. }
  357. }
  358. }
  359. result.xPoleWander = linearInterp(
  360. factor,
  361. samples[startBefore + eop._xPoleWanderRadiansColumn],
  362. samples[startAfter + eop._xPoleWanderRadiansColumn]
  363. );
  364. result.yPoleWander = linearInterp(
  365. factor,
  366. samples[startBefore + eop._yPoleWanderRadiansColumn],
  367. samples[startAfter + eop._yPoleWanderRadiansColumn]
  368. );
  369. result.xPoleOffset = linearInterp(
  370. factor,
  371. samples[startBefore + eop._xCelestialPoleOffsetRadiansColumn],
  372. samples[startAfter + eop._xCelestialPoleOffsetRadiansColumn]
  373. );
  374. result.yPoleOffset = linearInterp(
  375. factor,
  376. samples[startBefore + eop._yCelestialPoleOffsetRadiansColumn],
  377. samples[startAfter + eop._yCelestialPoleOffsetRadiansColumn]
  378. );
  379. result.ut1MinusUtc = linearInterp(
  380. factor,
  381. beforeUt1MinusUtc,
  382. afterUt1MinusUtc
  383. );
  384. return result;
  385. }
  386. export default EarthOrientationParameters;