Iau2006XysData.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import buildModuleUrl from "./buildModuleUrl.js";
  2. import defaultValue from "./defaultValue.js";
  3. import defer from "./defer.js";
  4. import defined from "./defined.js";
  5. import Iau2006XysSample from "./Iau2006XysSample.js";
  6. import JulianDate from "./JulianDate.js";
  7. import Resource from "./Resource.js";
  8. import TimeStandard from "./TimeStandard.js";
  9. /**
  10. * A set of IAU2006 XYS data that is used to evaluate the transformation between the International
  11. * Celestial Reference Frame (ICRF) and the International Terrestrial Reference Frame (ITRF).
  12. *
  13. * @alias Iau2006XysData
  14. * @constructor
  15. *
  16. * @param {Object} [options] Object with the following properties:
  17. * @param {Resource|String} [options.xysFileUrlTemplate='Assets/IAU2006_XYS/IAU2006_XYS_{0}.json'] A template URL for obtaining the XYS data. In the template,
  18. * `{0}` will be replaced with the file index.
  19. * @param {Number} [options.interpolationOrder=9] The order of interpolation to perform on the XYS data.
  20. * @param {Number} [options.sampleZeroJulianEphemerisDate=2442396.5] The Julian ephemeris date (JED) of the
  21. * first XYS sample.
  22. * @param {Number} [options.stepSizeDays=1.0] The step size, in days, between successive XYS samples.
  23. * @param {Number} [options.samplesPerXysFile=1000] The number of samples in each XYS file.
  24. * @param {Number} [options.totalSamples=27426] The total number of samples in all XYS files.
  25. *
  26. * @private
  27. */
  28. function Iau2006XysData(options) {
  29. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  30. this._xysFileUrlTemplate = Resource.createIfNeeded(
  31. options.xysFileUrlTemplate
  32. );
  33. this._interpolationOrder = defaultValue(options.interpolationOrder, 9);
  34. this._sampleZeroJulianEphemerisDate = defaultValue(
  35. options.sampleZeroJulianEphemerisDate,
  36. 2442396.5
  37. );
  38. this._sampleZeroDateTT = new JulianDate(
  39. this._sampleZeroJulianEphemerisDate,
  40. 0.0,
  41. TimeStandard.TAI
  42. );
  43. this._stepSizeDays = defaultValue(options.stepSizeDays, 1.0);
  44. this._samplesPerXysFile = defaultValue(options.samplesPerXysFile, 1000);
  45. this._totalSamples = defaultValue(options.totalSamples, 27426);
  46. this._samples = new Array(this._totalSamples * 3);
  47. this._chunkDownloadsInProgress = [];
  48. const order = this._interpolationOrder;
  49. // Compute denominators and X values for interpolation.
  50. const denom = (this._denominators = new Array(order + 1));
  51. const xTable = (this._xTable = new Array(order + 1));
  52. const stepN = Math.pow(this._stepSizeDays, order);
  53. for (let i = 0; i <= order; ++i) {
  54. denom[i] = stepN;
  55. xTable[i] = i * this._stepSizeDays;
  56. for (let j = 0; j <= order; ++j) {
  57. if (j !== i) {
  58. denom[i] *= i - j;
  59. }
  60. }
  61. denom[i] = 1.0 / denom[i];
  62. }
  63. // Allocate scratch arrays for interpolation.
  64. this._work = new Array(order + 1);
  65. this._coef = new Array(order + 1);
  66. }
  67. const julianDateScratch = new JulianDate(0, 0.0, TimeStandard.TAI);
  68. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  69. const dateTT = julianDateScratch;
  70. dateTT.dayNumber = dayTT;
  71. dateTT.secondsOfDay = secondTT;
  72. return JulianDate.daysDifference(dateTT, xys._sampleZeroDateTT);
  73. }
  74. /**
  75. * Preloads XYS data for a specified date range.
  76. *
  77. * @param {Number} startDayTT The Julian day number of the beginning of the interval to preload, expressed in
  78. * the Terrestrial Time (TT) time standard.
  79. * @param {Number} startSecondTT The seconds past noon of the beginning of the interval to preload, expressed in
  80. * the Terrestrial Time (TT) time standard.
  81. * @param {Number} stopDayTT The Julian day number of the end of the interval to preload, expressed in
  82. * the Terrestrial Time (TT) time standard.
  83. * @param {Number} stopSecondTT The seconds past noon of the end of the interval to preload, expressed in
  84. * the Terrestrial Time (TT) time standard.
  85. * @returns {Promise<void>} A promise that, when resolved, indicates that the requested interval has been
  86. * preloaded.
  87. */
  88. Iau2006XysData.prototype.preload = function (
  89. startDayTT,
  90. startSecondTT,
  91. stopDayTT,
  92. stopSecondTT
  93. ) {
  94. const startDaysSinceEpoch = getDaysSinceEpoch(
  95. this,
  96. startDayTT,
  97. startSecondTT
  98. );
  99. const stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  100. let startIndex =
  101. (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  102. 0;
  103. if (startIndex < 0) {
  104. startIndex = 0;
  105. }
  106. let stopIndex =
  107. (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  108. (0 + this._interpolationOrder);
  109. if (stopIndex >= this._totalSamples) {
  110. stopIndex = this._totalSamples - 1;
  111. }
  112. const startChunk = (startIndex / this._samplesPerXysFile) | 0;
  113. const stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  114. const promises = [];
  115. for (let i = startChunk; i <= stopChunk; ++i) {
  116. promises.push(requestXysChunk(this, i));
  117. }
  118. return Promise.all(promises);
  119. };
  120. /**
  121. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  122. * this method will return undefined.
  123. *
  124. * @param {Number} dayTT The Julian day number for which to compute the XYS value, expressed in
  125. * the Terrestrial Time (TT) time standard.
  126. * @param {Number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  127. * the Terrestrial Time (TT) time standard.
  128. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  129. * is undefined, a new instance is allocated and returned.
  130. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  131. * computation has not yet been downloaded.
  132. *
  133. * @see Iau2006XysData#preload
  134. */
  135. Iau2006XysData.prototype.computeXysRadians = function (
  136. dayTT,
  137. secondTT,
  138. result
  139. ) {
  140. const daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  141. if (daysSinceEpoch < 0.0) {
  142. // Can't evaluate prior to the epoch of the data.
  143. return undefined;
  144. }
  145. const centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  146. if (centerIndex >= this._totalSamples) {
  147. // Can't evaluate after the last sample in the data.
  148. return undefined;
  149. }
  150. const degree = this._interpolationOrder;
  151. let firstIndex = centerIndex - ((degree / 2) | 0);
  152. if (firstIndex < 0) {
  153. firstIndex = 0;
  154. }
  155. let lastIndex = firstIndex + degree;
  156. if (lastIndex >= this._totalSamples) {
  157. lastIndex = this._totalSamples - 1;
  158. firstIndex = lastIndex - degree;
  159. if (firstIndex < 0) {
  160. firstIndex = 0;
  161. }
  162. }
  163. // Are all the samples we need present?
  164. // We can assume so if the first and last are present
  165. let isDataMissing = false;
  166. const samples = this._samples;
  167. if (!defined(samples[firstIndex * 3])) {
  168. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  169. isDataMissing = true;
  170. }
  171. if (!defined(samples[lastIndex * 3])) {
  172. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  173. isDataMissing = true;
  174. }
  175. if (isDataMissing) {
  176. return undefined;
  177. }
  178. if (!defined(result)) {
  179. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  180. } else {
  181. result.x = 0.0;
  182. result.y = 0.0;
  183. result.s = 0.0;
  184. }
  185. const x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  186. const work = this._work;
  187. const denom = this._denominators;
  188. const coef = this._coef;
  189. const xTable = this._xTable;
  190. let i, j;
  191. for (i = 0; i <= degree; ++i) {
  192. work[i] = x - xTable[i];
  193. }
  194. for (i = 0; i <= degree; ++i) {
  195. coef[i] = 1.0;
  196. for (j = 0; j <= degree; ++j) {
  197. if (j !== i) {
  198. coef[i] *= work[j];
  199. }
  200. }
  201. coef[i] *= denom[i];
  202. let sampleIndex = (firstIndex + i) * 3;
  203. result.x += coef[i] * samples[sampleIndex++];
  204. result.y += coef[i] * samples[sampleIndex++];
  205. result.s += coef[i] * samples[sampleIndex];
  206. }
  207. return result;
  208. };
  209. function requestXysChunk(xysData, chunkIndex) {
  210. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  211. // Chunk has already been requested.
  212. return xysData._chunkDownloadsInProgress[chunkIndex];
  213. }
  214. const deferred = defer();
  215. xysData._chunkDownloadsInProgress[chunkIndex] = deferred;
  216. let chunkUrl;
  217. const xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  218. if (defined(xysFileUrlTemplate)) {
  219. chunkUrl = xysFileUrlTemplate.getDerivedResource({
  220. templateValues: {
  221. 0: chunkIndex,
  222. },
  223. });
  224. } else {
  225. chunkUrl = new Resource({
  226. url: buildModuleUrl(`Assets/IAU2006_XYS/IAU2006_XYS_${chunkIndex}.json`),
  227. });
  228. }
  229. chunkUrl.fetchJson().then(function (chunk) {
  230. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  231. const samples = xysData._samples;
  232. const newSamples = chunk.samples;
  233. const startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  234. for (let i = 0, len = newSamples.length; i < len; ++i) {
  235. samples[startIndex + i] = newSamples[i];
  236. }
  237. deferred.resolve();
  238. });
  239. return deferred.promise;
  240. }
  241. export default Iau2006XysData;