Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/get_eop_csv.py
Last active Jun 29, 2018

Embed
What would you like to do?
Python script to generate csv data from IERS's EOP.
#! /usr/local/bin/python3.6
"""
IERS の Buttelin A テキストデータから EOP(Polar Motion etc.) CSV ファイルを生成
* 予め [こちら](ftp://ftp.iers.org/products/eop/rapid/) からダウンロードして
おいたものを使用する。
(IAU 2000A 章動理論によるデータ finals2000A.all", "finals2000A.daily")
* 1日のデータに速報値(区分"I")と確定値がある場合は、確定値を優先。
* 2ファイルで重複する日付のデータは "finals2000A.daily" を優先。
* 取得する項目:
date, mjd, flag_pm, pm_x, pm_x_e, pm_y, pm_y_e,
flag_dut, dut1, dut1_e, lod, lod_e,
flag_nut, nut_x, nut_x_e, nut_y, nut_y_e
date name version
2018.06.25 mk-mode.com 1.00 新規作成
Copyright(C) 2018 mk-mode.com All Rights Reserved.
"""
import re
import requests
import sys
import traceback
class GetPolarMotionCsv:
USAGE = "[USAGE] ./get_eop_csv.py"
FILE_A = "./file/finals2000A.all"
FILE_D = "./file/finals2000A.daily"
FILE_CSV = "data/eop.csv"
RE_0 = re.compile(
r'^(.{6}) (.{8}) ([IP ]) (.{9})(.{9}) (.{9})(.{9}) '
'([IP ])(.{10})(.{10}) (.{7})(.{7}) '
'([IP ]) (.{9})(.{9}) (.{9})(.{9})'
'(.{10})(.{10})(.{11})(.{10})(.{10})\s*$'
)
RE_1 = re.compile(r'^\s+$')
def __init__(self):
self.data = {}
def exec(self):
try:
self.__get_data("A") # 過去全データ取得
self.__get_data("D") # 日次データ取得
self.__write_csv() # CSV 保存
except Exception as e:
raise
def __get_data(self, div):
""" データ取得
:param string div: "A": ALLデータ, "D": DAILYデータ
"""
try:
f_read = self.FILE_A if div == "A" else self.FILE_D
with open(f_read) as f:
for l in f.readlines():
m = re.findall(self.RE_0, l)[0]
flag_pm, flag_dut, flag_nut = m[2], m[7], m[12]
flag_pm = re.sub(self.RE_1, "", flag_pm)
flag_dut = re.sub(self.RE_1, "", flag_dut)
flag_nut = re.sub(self.RE_1, "", flag_nut)
if div == "A" and \
flag_pm == "P" and \
flag_dut == "P" and \
flag_nut == "P":
break
year = int(m[0][0:2])
month = int(m[0][2:4])
day = int(m[0][4:6])
year = year + 1900 if year > 72 else year + 2000
date = "{:04d}-{:02d}-{:02d}".format(year, month, day)
mjd = float(m[1])
# Polar Motion
pm_x, pm_x_e, pm_y, pm_y_e = map(
lambda x: None if re.match(self.RE_1, x) else float(x),
m[3:7]
)
pm_x_f, pm_y_f = map(
lambda x: None if re.match(self.RE_1, x)
else float(x), m[17:19]
)
if pm_x_f is not(None) and pm_y_f is not(None):
flag_pm = "F"
if pm_x_f is not(None):
pm_x, pm_x_e = pm_x_f, 0.0
if pm_y_f is not(None):
pm_y, pm_y_e = pm_y_f, 0.0
# UT1 - UTC, LOD
dut1, dut1_e, lod, lod_e = map(
lambda x: None if re.match(self.RE_1, x) else float(x),
m[8:12]
)
dut1_f = None if re.match(self.RE_1, m[19]) \
else float(m[19])
if dut1_f is not(None):
flag_dut, dut1, dut1_e = "F", dut1_f, 0.0
# Nutation
nut_x, nut_x_e, nut_y, nut_y_e = map(
lambda x: None if re.match(self.RE_1, x) else float(x),
m[13:17]
)
nut_x_f, nut_y_f = map(
lambda x: None if re.match(self.RE_1, x) else float(x),
m[20:22]
)
if nut_x_f is not(None) and nut_y_f is not(None):
flag_nut = "F"
if nut_x_f is not(None):
nut_x, nut_x_e = nut_x_f, 0.0
if nut_y_f is not(None):
nut_y, nut_y_e = nut_y_f, 0.0
self.data[mjd] = [
date, flag_pm, pm_x, pm_x_e, pm_y, pm_y_e,
flag_dut, dut1, dut1_e, lod, lod_e,
flag_nut, nut_x, nut_x_e, nut_y, nut_y_e
]
except Exception as e:
raise
def __write_csv(self):
""" CSV 保存 """
try:
with open(self.FILE_CSV, "w") as f:
for k, v in self.data.items():
l = "{},{},".format(v[0], k)
l += ",".join(map(
lambda x: "" if x is None else str(x), v[1:])
) + "\n"
f.write(l)
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = GetPolarMotionCsv()
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.