Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/cal_jpl.rb
Last active Apr 19, 2018

Embed
What would you like to do?
Ruby script to calculate a Japan's calendar with a selfmade gem library, using JPL DE430.
#! /usr/local/bin/ruby
# coding: utf-8
#---------------------------------------------------------------------------------
#= カレンダー
# : 自作 RubyGems ライブラリ MkCalJpl を使用
# ( 高野氏のプログラムを改良した MkCalendar を、 JPL DE430 から正確な位置データを
# 取得して計算するようさらに改良したもの )
#
# date name version
# 2016.09.17 mk-mode.com 1.00 新規作成
#
# Copyright(C) 2016 mk-mode.com All Rights Reserved.
#---------------------------------------------------------------------------------
# 引数 : JST(日本標準時)
# 書式:YYYYMMDD
# 無指定なら現在(システム日時)と判断。
#---------------------------------------------------------------------------------
#++
require 'date'
require 'mk_cal_jpl'
class CalJpl
USAGE = "[USAGE] .cal_jpl.rb [YYYYMMDD]"
MSG_ERR = "[ERROR] Invalid date!"
BIN_PATH = "/path/to/JPLEPH"
def initialize
@date = ARGV.shift
@date ||= Time.now.strftime("%Y%m%d")
unless @date =~ /\d{8}/
puts USAGE
exit 0
end
unless Date.valid_date?(@date[0,4].to_i, @date[4,2].to_i, @date[6,2].to_i)
puts MSG_ERR
exit 0
end
end
def calc
@obj = MkCalJpl.new(BIN_PATH, @date)
oc = @obj.oc
str = sprintf("%04d-%02d-%02d", @obj.year, @obj.month, @obj.day)
str << " #{@obj.yobi}曜日"
str << " #{@obj.holiday}" unless @obj.holiday == ""
str << " #{@obj.jd}UTC(#{@obj.jd_jst}JST) #{@obj.kanshi} "
str << sprintf("%04d-%02d-%02d", oc[0], oc[2], oc[3])
str << "(閏)" if oc[1] == 1
str << " #{oc[4]}"
str << " #{@obj.sekki_24}" unless @obj.sekki_24 == ""
str << " #{@obj.zassetsu}" unless @obj.zassetsu == ""
str << " #{@obj.sekku}" unless @obj.sekku == ""
str << " #{@obj.lambda} #{@obj.alpha} #{@obj.moonage}"
puts str
# ついでに、黄経(月、太陽)から月相を計算
# diff = @obj.alpha - @obj.lambda
# diff += 360 if diff < 0
# moonphase = (diff / 360.0 * 28).round
# puts "\n月相: #{moonphase}"
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each { |tr| $stderr.puts "\t#{tr}"}
exit 1
end
end
CalJpl.new.calc 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.