Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby script to get delta_T from USNO.
#! /usr/local/bin/ruby
# coding: utf-8
#---------------------------------------------------------------------------------
#= 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 とみなす
#---------------------------------------------------------------------------------
#++
require "open-uri"
class GetDeltaTUsno
USAGE = "[USAGE] ./get_delta_t_usno.rb [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 Ruby/#{RUBY_VERSION}, Administrator: <MAIL_ADDRESS>)"
def initialize
ymd = ARGV.shift
ymd ||= Time.now.strftime("%Y%m%d")
if ymd !~ /^\d{8}$/
puts USAGE
exit 0
end
@date = "#{ymd[0, 4]}-#{ymd[4, 2]}-#{ymd[6, 2]}"
@delta_t = 0 # delta T 初期値
@dp = "" # 確定/推定値区分(推定値なら "*")
end
def exec
get_determination # 確定データ取得
get_prediction # 推定データ取得
get_delta_t # delta_T 取得
display # 結果出力
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each { |tr| $stderr.puts "\t#{tr}"}
exit 1
end
private
# ------------------------------------
# 確定データ取得
# ------------------------------------
def get_determination
html = open(URL_D, "r:utf-8", {"User-Agent" => UA}) { |f| f.read }
@data = html.split(/[\r\n]+/).map do |line|
items = line.sub(/^\s+/, "").split(/\s+/)
date = "%04d-%02d-%02d" % [*items[0, 3]]
@date_max = date
[date, items[-1].to_f, "D"]
end
rescue => e
raise
end
# ------------------------------------
# 推定データ取得
# ------------------------------------
def get_prediction
html = open(URL_P, "r:utf-8", {"User-Agent" => UA}) { |f| f.read }
html.split(/[\r\n]+/).each do |line|
items = line.sub(/^\s+/, "").split(/\s+/)
next if /[\d\.]+/ !~items[0]
year, year_frac = items[0].split(/\./)
year = year.to_i
case year_frac
when "00"; month = 1
when "25"; month = 4
when "50"; month = 7
when "75"; month = 10
end
date = "%04d-%02d-01" % [year, month]
next if date <= @date_max
@data << [date, items[1].to_f, "P"]
end
rescue => e
raise
end
# ------------------------------------
# delta T 取得
# ------------------------------------
def get_delta_t
@data.reverse.each_with_index do |d, i|
if d[0] <= @date
break if i == 0
@delta_t = d[1]
@dp = "*" if d[2] == "P"
break
end
end
rescue => e
raise
end
# ------------------------------------
# 結果出力
# ------------------------------------
def display
puts "[#{@date}] delta_T = #{@delta_t} sec. #{@dp}"
rescue => e
raise
end
end
GetDeltaTUsno.new.exec if __FILE__ == $0
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.