Skip to content

Instantly share code, notes, and snippets.

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

Embed
What would you like to do?
Ruby script to calculate a Japan's Sun/Moon phenomenons with a selfmade gem library.
#! /usr/local/bin/ruby
# coding: utf-8
# 日の出・日の入の時刻・方位、日の南中の時刻・高度、
# 月の出・月の入の時刻・方位、月の南中の時刻・高度 の算出
#
# date name version
# 2016.06.13 mk-mode 1.00 新規作成
#
# Copyright(C) 2016 mk-mode.com All Rights Reserved.
#---------------------------------------------------------------------------------
# [引数] 第1: 日付 (Format: 99999999, Default: システム日付)
# 計算対象の日付(グレゴリオ暦)(JST)を半角8桁数字で指定。
# 第2: 緯度 (Format: [+-]999.99999999, Default: 35.47222222)
# (度で指定。北緯は +, 南緯は -, 小数点以下は省略可)
# 第3: 経度 (Format: [+-]999.99999999, Default: 133.05055556)
# (度で指定。東経は +, 西経は - ,小数点以下は省略可)
# 第4: 標高 (Format: [+]9999.99999999, Default: 0.0)
# (メートルで指定。小数点以下は省略可)
#---------------------------------------------------------------------------------
#+
require 'mk_sunmoon'
class SunMoon
USAGE =<<-EOS
[USAGE] ./sun_moon.rb [date[, latitude[, longitude[, altitude]]]]
引数
第1: 日付 (Format: 99999999, Default: システム日付)
計算対象の日付(グレゴリオ暦)(JST)を半角8桁数字で指定。
第2: 緯度 (Format: [+-]999.99999999, Default: 35.47222222)
(度で指定。北緯は +, 南緯は -, 小数点以下は省略可)
第3: 経度 (Format: [+-]999.99999999, Default: 133.05055556)
(度で指定。東経は +, 西経は - ,小数点以下は省略可)
第4: 標高 (Format: [+]9999.99999999, Default: 0.0)
(メートルで指定。小数点以下は省略可)
EOS
def initialize
check_args(ARGV)
@obj = MkSunmoon.new(*ARGV)
end
def exec
begin
sign_lat = @obj.lat < 0 ? "S" : "N"
sign_lon = @obj.lon < 0 ? "W" : "E"
sr = @obj.sunrise
ss = @obj.sunset
sm = @obj.sun_mp
mr = @obj.moonrise
ms = @obj.moonset
mm = @obj.moon_mp
str = sprintf(" %04d-%02d-%02d", @obj.year, @obj.month, @obj.day)
str << " [#{@obj.lat}#{sign_lat}, #{@obj.lon}#{sign_lon}, #{@obj.alt}m]\n"
str << " 日の出: #{sr[0]} (方位角: "
str << (sr[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", sr[1]))
str << " 日南中: #{sm[0]} ( 高度: "
str << (sm[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", sm[1]))
str << " 日の入: #{ss[0]} (方位角: "
str << (ss[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", ss[1]))
str << " 月の出: #{mr[0]} (方位角: "
str << (mr[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", mr[1]))
str << " 月南中: #{mm[0]} ( 高度: "
str << (mm[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", mm[1]))
str << " 月の入: #{ms[0]} (方位角: "
str << (ms[1] == "---" ? "--------°)\n" : sprintf("%8.4f°)\n", ms[1]))
puts str
rescue => e
$stderr.puts "[#{e.class}] #{e.message}"
e.backtrace.each { |tr| $stderr.puts "\t#{tr}" }
exit 1
end
end
private
def check_args(args)
date, lat, lon, alt = args
begin
if (date && date !~ /^\d{8}$/) ||
(lat && lat !~ /^[+-]?\d{,2}(\.\d*)?$/) ||
(lon && lon !~ /^[+-]?\d{,3}(\.\d*)?$/) ||
(alt && alt !~ /^+?\d{,4}(\.\d*)?$/)
puts USAGE
exit 0
end
rescue => e
raise
end
end
end
SunMoon.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.