Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to get delta_T from USNO.
#! /usr/local/bin/python3.6
"""
USNO(アメリカ海軍天文台) から delta T(ΔT) を取得
: 確定データは
[Delta T determinations](http://maia.usno.navy.mil/ser7/deltat.data)
から、推定データは
[Delta T predictions](http://maia.usno.navy.mil/ser7/deltat.preds)
から取得する。
date name version
2018.06.20 mk-mode.com 1.00 新規作成
Copyright(C) 2018 mk-mode.com All Rights Reserved.
---
引数 : UTC (YYYYMMDD 形式)
* 無指定ならシステム日時を UTC とみなす
"""
from datetime import datetime
import re
import requests
import sys
import traceback
class GetDeltaTUsno:
USAGE = "[USAGE] ./get_delta_t_usno.py [yyyymmdd]"
URL_D = "http://maia.usno.navy.mil/ser7/deltat.data"
URL_P = "http://maia.usno.navy.mil/ser7/deltat.preds"
UA = (
"<BOT_NAME> (by Python/{}.{}.{}, "
"Administrator: <MAIL_ADDRESS>)"
).format(
sys.version_info.major,
sys.version_info.minor,
sys.version_info.micro
)
PTN_0 = re.compile(r'[\r\n]+')
PTN_1 = re.compile(r'^\s+')
PTN_2 = re.compile(r'\s+')
PTN_3 = re.compile(r'[\d\.]+')
PTN_4 = re.compile(r'\.')
def __init__(self):
self.__get_arg()
self.data = [] # delta t 一覧
self.delta_t = 0 # delta t 初期値
self.dp = "" # 確定/推定値区分(推定値なら "*")
def exec(self):
try:
self.__get_determination() # 確定データ取得
self.__get_prediction() # 推定データ取得
self.__get_delta_t() # delta_t 取得
self.__display() # 結果出力
except Exception as e:
raise
def __get_arg(self):
""" argument getting """
try:
if len(sys.argv) > 1:
ymd = sys.argv[1]
else:
ymd = datetime.now().strftime("%Y%m%d")
if not(re.search(r"^\d{8}$", ymd)):
print(self.USAGE)
sys.exit(0)
self.date = "{}-{}-{}".format(ymd[0:4], ymd[4:6], ymd[6:8])
except Exception as e:
raise
def __get_determination(self):
""" 確定データ取得 """
try:
headers = {'user-agent': self.UA}
res = requests.get(self.URL_D, headers=headers)
html = res.text.strip()
for line in re.split(self.PTN_0, html):
line = re.sub(self.PTN_1, "", line)
items = re.split(self.PTN_2, re.sub(self.PTN_1, "", line))
date = "{:04d}-{:02d}-{:02d}".format(
int(items[0]), int(items[1]), int(items[2])
)
self.date_max = date
self.data.append([date, float(items[-1]), "D"])
except Exception as e:
raise
def __get_prediction(self):
""" 推定データ取得 """
try:
headers = {'user-agent': self.UA}
res = requests.get(self.URL_P, headers=headers)
html = res.text.strip()
for line in re.split(self.PTN_0, html):
line = re.sub(self.PTN_1, "", line)
items = re.split(self.PTN_2, re.sub(self.PTN_1, "", line))
if not(re.match(self.PTN_3, items[0])):
continue
year, year_frac = re.split(self.PTN_4, items[0])
year = int(year)
if year_frac == "00":
month = 1
elif year_frac == "25":
month = 4
elif year_frac == "50":
month = 7
elif year_frac == "75":
month = 10
date = "{:04d}-{:02d}-01".format(year, month)
if date <= self.date_max:
continue
self.data.append([date, float(items[1]), "P"])
except Exception as e:
raise
def __get_delta_t(self):
""" delta t 取得 """
try:
for i, d in enumerate(reversed(self.data)):
if d[0] <= self.date:
if i == 0:
break
self.delta_t = d[1]
if d[2] == "P":
self.dp = "*"
break
except Exception as e:
raise
def __display(self):
""" 結果出力 """
try:
print("[{}] delta_T = {} sec. {}".format(
self.date, self.delta_t, self.dp)
)
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = GetDeltaTUsno()
obj.exec()
except Exception as e:
traceback.print_exc()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.