Source

juliandays.ts

  1. /**
  2. @module JulianDays
  3. */
  4. import dayjs from 'dayjs'
  5. import { DAYMS, DEG2H, J1970, J2000, MJD_START } from './constants'
  6. import { ArcSecond, Degree, Hour, JulianCentury, JulianDay, JulianMillenium, Radian } from './types'
  7. import { transformUTC2TT } from '@/times'
  8. import { Earth } from '@/earth'
  9. import { fmod24, fmod360, isNumber } from './utils'
  10. /**
  11. * Computes the date corresponding to the Julian Day
  12. * @param {JulianDay} jd The julian day
  13. * @returns {Date}
  14. */
  15. export function getDate (jd: JulianDay): Date {
  16. return new Date((jd + 0.5 - J1970) * DAYMS)
  17. }
  18. /**
  19. * Computes the Julian day for a given date.
  20. * @param args
  21. * @returns {JulianDay} The julian day
  22. */
  23. export function getJulianDay (...args: any[]): JulianDay {
  24. if (args.length === 0) {
  25. return new Date().valueOf() / DAYMS - 0.5 + J1970
  26. } else if (args.length === 1) {
  27. const value = args[0]
  28. if (value instanceof Date) {
  29. // @ts-ignore
  30. return value.valueOf() / DAYMS - 0.5 + J1970
  31. } else if (value instanceof String || typeof value === 'string') {
  32. // @ts-ignore
  33. return dayjs(value).toDate().valueOf() / DAYMS - 0.5 + J1970
  34. } else if (isNumber(value)) {
  35. const year = Math.floor(value)
  36. const month = Math.floor(value - year) * 12 // the month is 0-indexed
  37. const day = Math.floor(((value - year) * 12 - month) * 365)
  38. const UTCDate = new Date(Date.UTC(year, month, day))
  39. return UTCDate.valueOf() / DAYMS - 0.5 + J1970
  40. } else {
  41. console.warn(`Invalid input argument for JulianDay : ${value} (${typeof value}). Using J2000 in place.`)
  42. return J2000
  43. }
  44. } else if (args.length === 2) {
  45. const UTCDate = new Date(Date.UTC(args[0], args[1], 0))
  46. return UTCDate.valueOf() / DAYMS - 0.5 + J1970
  47. } else {
  48. const UTCDate = new Date(Date.UTC(args[0], args[1], args[2]))
  49. return UTCDate.valueOf() / DAYMS - 0.5 + J1970
  50. }
  51. }
  52. /**
  53. * The local mean sidereal time (sun's clock time) for a given julian day
  54. * at a given longitude on Earth
  55. * @param {JulianDay} jd The julian day
  56. * @param {Degree} lng The longitude
  57. * @return {Hour}
  58. */
  59. export function getLocalSiderealTime (jd: JulianDay, lng: Degree): Hour {
  60. const t = getJulianCentury(jd)
  61. // Greenwich SiderealTime in degrees! Equ. 12.4 of AA, p. 88
  62. const gmst = 280.460_618_37
  63. + 360.985_647_366_29 * (jd - 2451545)
  64. + 0.000_387_933 * t * t
  65. - t * t * t / 38_710_000
  66. return fmod24(fmod360(gmst + lng) * DEG2H)
  67. }
  68. /**
  69. * The apparent local mean sidereal time (sun's clock time) for a given julian day, corrected for the
  70. * nutation, at a given longitude on Earth
  71. * @param {JulianDay} jd The julian day
  72. * @param {Degree} lng The longitude
  73. * @return {Hour}
  74. */
  75. export function getApparentLocalSiderealTime (jd: JulianDay, lng: Degree): Hour {
  76. const epsilon: Radian = Earth.getTrueObliquityOfEcliptic(jd)
  77. const deltaPsi: ArcSecond = Earth.getNutationInLongitude(jd)
  78. return getLocalSiderealTime(jd, lng) + deltaPsi * Math.cos(epsilon) * DEG2H
  79. }
  80. /**
  81. * Modified julian day
  82. * @param {JulianDay} jd The julian day
  83. * @return {number} The modified Julian Day
  84. */
  85. export function getModifiedJulianDay (jd: JulianDay): JulianDay {
  86. return jd - MJD_START
  87. }
  88. /**
  89. * The Julian Day of Midnight UTC for a given Julian Day.
  90. * @param {JulianDay} jd The initial julian day
  91. * @returns {JulianDay}
  92. */
  93. export function getJulianDayMidnight (jd: JulianDay): JulianDay {
  94. return Math.floor(jd - 0.5) + 0.5
  95. }
  96. /**
  97. * The Julian Day of Midnight Dynamical Time (not exactly equal to UTC) for a given Julian Day.
  98. * When computing rise, transit and set times, the input equatorial coordinates must be first
  99. * computed at these times.
  100. * @param {JulianDay} jd The initial julian day
  101. * @returns {JulianDay}
  102. */
  103. export function getJulianDayMidnightDynamicalTime (jd: JulianDay): JulianDay {
  104. return transformUTC2TT(getJulianDayMidnight(jd))
  105. }
  106. /**
  107. * The Julian Century (time interval of 36525 days)
  108. * @param {JulianDay} jd The initial julian day
  109. * @returns {JulianCentury}
  110. */
  111. export function getJulianCentury (jd: JulianDay): JulianCentury {
  112. // AA, Equ 12.1.
  113. return (jd - 2451545) / 36525
  114. }
  115. /**
  116. * The Julian Millenium (time interval of 365250 days)
  117. * @param {JulianDay} jd The initial julian day
  118. * @returns {JulianMillenium}
  119. */
  120. export function getJulianMillenium (jd: JulianDay): JulianMillenium {
  121. return (jd - 2451545) / 365250
  122. }