Iau2006XysData.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import when from "../ThirdParty/when.js";
  2. import buildModuleUrl from "./buildModuleUrl.js";
  3. import defaultValue from "./defaultValue.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. var order = this._interpolationOrder;
  49. // Compute denominators and X values for interpolation.
  50. var denom = (this._denominators = new Array(order + 1));
  51. var xTable = (this._xTable = new Array(order + 1));
  52. var stepN = Math.pow(this._stepSizeDays, order);
  53. for (var i = 0; i <= order; ++i) {
  54. denom[i] = stepN;
  55. xTable[i] = i * this._stepSizeDays;
  56. for (var 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. var julianDateScratch = new JulianDate(0, 0.0, TimeStandard.TAI);
  68. function getDaysSinceEpoch(xys, dayTT, secondTT) {
  69. var 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. var startDaysSinceEpoch = getDaysSinceEpoch(this, startDayTT, startSecondTT);
  95. var stopDaysSinceEpoch = getDaysSinceEpoch(this, stopDayTT, stopSecondTT);
  96. var startIndex =
  97. (startDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  98. 0;
  99. if (startIndex < 0) {
  100. startIndex = 0;
  101. }
  102. var stopIndex =
  103. (stopDaysSinceEpoch / this._stepSizeDays - this._interpolationOrder / 2) |
  104. (0 + this._interpolationOrder);
  105. if (stopIndex >= this._totalSamples) {
  106. stopIndex = this._totalSamples - 1;
  107. }
  108. var startChunk = (startIndex / this._samplesPerXysFile) | 0;
  109. var stopChunk = (stopIndex / this._samplesPerXysFile) | 0;
  110. var promises = [];
  111. for (var i = startChunk; i <= stopChunk; ++i) {
  112. promises.push(requestXysChunk(this, i));
  113. }
  114. return when.all(promises);
  115. };
  116. /**
  117. * Computes the XYS values for a given date by interpolating. If the required data is not yet downloaded,
  118. * this method will return undefined.
  119. *
  120. * @param {Number} dayTT The Julian day number for which to compute the XYS value, expressed in
  121. * the Terrestrial Time (TT) time standard.
  122. * @param {Number} secondTT The seconds past noon of the date for which to compute the XYS value, expressed in
  123. * the Terrestrial Time (TT) time standard.
  124. * @param {Iau2006XysSample} [result] The instance to which to copy the interpolated result. If this parameter
  125. * is undefined, a new instance is allocated and returned.
  126. * @returns {Iau2006XysSample} The interpolated XYS values, or undefined if the required data for this
  127. * computation has not yet been downloaded.
  128. *
  129. * @see Iau2006XysData#preload
  130. */
  131. Iau2006XysData.prototype.computeXysRadians = function (
  132. dayTT,
  133. secondTT,
  134. result
  135. ) {
  136. var daysSinceEpoch = getDaysSinceEpoch(this, dayTT, secondTT);
  137. if (daysSinceEpoch < 0.0) {
  138. // Can't evaluate prior to the epoch of the data.
  139. return undefined;
  140. }
  141. var centerIndex = (daysSinceEpoch / this._stepSizeDays) | 0;
  142. if (centerIndex >= this._totalSamples) {
  143. // Can't evaluate after the last sample in the data.
  144. return undefined;
  145. }
  146. var degree = this._interpolationOrder;
  147. var firstIndex = centerIndex - ((degree / 2) | 0);
  148. if (firstIndex < 0) {
  149. firstIndex = 0;
  150. }
  151. var lastIndex = firstIndex + degree;
  152. if (lastIndex >= this._totalSamples) {
  153. lastIndex = this._totalSamples - 1;
  154. firstIndex = lastIndex - degree;
  155. if (firstIndex < 0) {
  156. firstIndex = 0;
  157. }
  158. }
  159. // Are all the samples we need present?
  160. // We can assume so if the first and last are present
  161. var isDataMissing = false;
  162. var samples = this._samples;
  163. if (!defined(samples[firstIndex * 3])) {
  164. requestXysChunk(this, (firstIndex / this._samplesPerXysFile) | 0);
  165. isDataMissing = true;
  166. }
  167. if (!defined(samples[lastIndex * 3])) {
  168. requestXysChunk(this, (lastIndex / this._samplesPerXysFile) | 0);
  169. isDataMissing = true;
  170. }
  171. if (isDataMissing) {
  172. return undefined;
  173. }
  174. if (!defined(result)) {
  175. result = new Iau2006XysSample(0.0, 0.0, 0.0);
  176. } else {
  177. result.x = 0.0;
  178. result.y = 0.0;
  179. result.s = 0.0;
  180. }
  181. var x = daysSinceEpoch - firstIndex * this._stepSizeDays;
  182. var work = this._work;
  183. var denom = this._denominators;
  184. var coef = this._coef;
  185. var xTable = this._xTable;
  186. var i, j;
  187. for (i = 0; i <= degree; ++i) {
  188. work[i] = x - xTable[i];
  189. }
  190. for (i = 0; i <= degree; ++i) {
  191. coef[i] = 1.0;
  192. for (j = 0; j <= degree; ++j) {
  193. if (j !== i) {
  194. coef[i] *= work[j];
  195. }
  196. }
  197. coef[i] *= denom[i];
  198. var sampleIndex = (firstIndex + i) * 3;
  199. result.x += coef[i] * samples[sampleIndex++];
  200. result.y += coef[i] * samples[sampleIndex++];
  201. result.s += coef[i] * samples[sampleIndex];
  202. }
  203. return result;
  204. };
  205. function requestXysChunk(xysData, chunkIndex) {
  206. if (xysData._chunkDownloadsInProgress[chunkIndex]) {
  207. // Chunk has already been requested.
  208. return xysData._chunkDownloadsInProgress[chunkIndex];
  209. }
  210. var deferred = when.defer();
  211. xysData._chunkDownloadsInProgress[chunkIndex] = deferred;
  212. var chunkUrl;
  213. var xysFileUrlTemplate = xysData._xysFileUrlTemplate;
  214. if (defined(xysFileUrlTemplate)) {
  215. chunkUrl = xysFileUrlTemplate.getDerivedResource({
  216. templateValues: {
  217. "0": chunkIndex,
  218. },
  219. });
  220. } else {
  221. chunkUrl = new Resource({
  222. url: buildModuleUrl(
  223. "Assets/IAU2006_XYS/IAU2006_XYS_" + chunkIndex + ".json"
  224. ),
  225. });
  226. }
  227. when(chunkUrl.fetchJson(), function (chunk) {
  228. xysData._chunkDownloadsInProgress[chunkIndex] = false;
  229. var samples = xysData._samples;
  230. var newSamples = chunk.samples;
  231. var startIndex = chunkIndex * xysData._samplesPerXysFile * 3;
  232. for (var i = 0, len = newSamples.length; i < len; ++i) {
  233. samples[startIndex + i] = newSamples[i];
  234. }
  235. deferred.resolve();
  236. });
  237. return deferred.promise;
  238. }
  239. export default Iau2006XysData;