Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #! /usr/local/bin/python3.6
  2. """
  3. グリニジ平均恒星時 GMST(= Greenwich Mean Sidereal Time)の計算
  4. : IAU1982 版
  5.  
  6. Date Author Version
  7. 2016.06.15 mk-mode.com 1.00 新規作成
  8.  
  9. Copyright(C) 2018 mk-mode.com All Rights Reserved.
  10. ---
  11. 引数 : 日時(UT1(世界時1))
  12. 書式:YYYYMMDD or YYYYMMDDHHMMSS
  13. 無指定なら現在(システム日時)を UT1 とみなす。
  14. """
  15. from datetime import datetime
  16. import math
  17. import re
  18. import sys
  19. import traceback
  20.  
  21.  
  22. class GmstIau82:
  23. PI2 = math.pi * 2 # => 6.283185307179586
  24. D2R = math.pi / 180 # => 0.017453292519943295
  25.  
  26. def __init__(self):
  27. self.__get_arg()
  28.  
  29. def exec(self):
  30. try:
  31. print(self.ut1.strftime("%Y-%m-%d %H:%M:%S UT1"))
  32. jd_ut1 = self.__gc2jd(self.ut1)
  33. print("JD(UT1):", jd_ut1)
  34. gmst = self.__calc_gmst(jd_ut1)
  35. gmst_d = gmst * 180 / math.pi
  36. gmst_h = self.__deg2hms(gmst_d)
  37. print((
  38. "GMST = {} rad.\n"
  39. " = {} deg.\n"
  40. " = {}"
  41. ).format(
  42. gmst, gmst_d, gmst_h
  43. ))
  44. except Exception as e:
  45. raise
  46.  
  47. def __get_arg(self):
  48. """ コマンドライン引数の取得
  49. * コマンドライン引数で指定した日時を self.ut1 に設定
  50. * コマンドライン引数が存在しなければ、現在時刻を self.ut1 に設定
  51. """
  52. try:
  53. if len(sys.argv) < 2:
  54. self.ut1 = datetime.now()
  55. return
  56. if re.search(r"^(\d{8}|\d{14})$", sys.argv[1]):
  57. dt = sys.argv[1].ljust(14, "0")
  58. else:
  59. sys.exit(0)
  60. try:
  61. self.ut1 = datetime.strptime(dt, "%Y%m%d%H%M%S")
  62. except ValueError as e:
  63. print("Invalid date!")
  64. sys.exit(0)
  65. except Exception as e:
  66. raise
  67.  
  68. def __gc2jd(self, ut1):
  69. """ 年月日(グレゴリオ暦) -> ユリウス日(JD) 変換
  70. : フリーゲルの公式を使用
  71. JD = int(365.25 * year)
  72. + int(year / 400)
  73. - int(year / 100)
  74. + int(30.59 * (month - 2))
  75. + day
  76. + 1721088
  77. ※上記の int(x) は厳密には、x を超えない最大の整数
  78. (ちなみに、準JDを求めるなら `+ 1721088` が `- 678912` となる)
  79.  
  80. :param datetime ut1: UT1(世界時1)
  81. :return float jd: Julian Day
  82. """
  83. try:
  84. year, month, day = ut1.year, ut1.month, ut1.day
  85. hour, minute, second = ut1.hour, ut1.minute, ut1.second
  86. # 1月,2月は前年の13月,14月とする
  87. if month < 3:
  88. year -= 1
  89. month += 12
  90. # 日付(整数)部分計算
  91. jd = int(365.25 * year) \
  92. + year // 400 \
  93. - year // 100 \
  94. + int(30.59 * (month - 2)) \
  95. + day \
  96. + 1721088.5
  97. # 時間(小数)部分計算
  98. t = (second / 3600 \
  99. + minute / 60 \
  100. + hour) / 24.0
  101. return jd + t
  102. except Exception as e:
  103. raise
  104.  
  105. def __calc_gmst(self, jd_ut1):
  106. """ GMST(グリニッジ平均恒星時)計算
  107. : IAU1982理論(by David Vallado)によるもの
  108. GMST = 18h 41m 50.54841s
  109. + 8640184.812866s T + 0.093104s T^2 - 0.0000062s T^3
  110. (但し、 T = 2000年1月1日12時(UT1)からのユリウス世紀単位)
  111.  
  112. :param float jd_ut1: UT1 に対するユリウス日
  113. :return datetime gmst: グリニッジ平均恒星時(単位:radian)
  114. """
  115. try:
  116. t_ut1= (jd_ut1 - 2451545.0) / 36525
  117. gmst = 67310.54841 \
  118. + (876600.0 * 3600.0 + 8640184.812866 \
  119. + (0.093104 \
  120. - 6.2e-6 * t_ut1) * t_ut1) * t_ut1
  121. gmst = (gmst * self.D2R / 240.0) % self.PI2
  122. if gmst < 0.0:
  123. gmst += self.PI2
  124. return gmst
  125. except Exception as e:
  126. raise
  127.  
  128. def __deg2hms(self, deg):
  129. """ 99.999° -> 99h99m99s 変換
  130.  
  131. :param float deg: degree
  132. :return string: 99 h 99 m 99.999 s
  133. """
  134. sign = ""
  135. try:
  136. h = int(deg / 15)
  137. _m = (deg - h * 15.0) * 4.0
  138. m = int(_m)
  139. s = (_m - m) * 60.0
  140. if s < 0:
  141. s *= -1
  142. sign = "-"
  143. return "{}{:2d} h {:02d} m {:06.3f} s".format(sign, h, m, s)
  144. except Exception as e:
  145. raise
  146.  
  147.  
  148. if __name__ == '__main__':
  149. try:
  150. GmstIau82().exec()
  151. except Exception as e:
  152. traceback.print_exc()
  153. sys.exit(1)
Add Comment
Please, Sign In to add comment