实例介绍
【实例简介】太阳月亮位置计算ruby代码
【实例截图】
【核心代码】class SunCalc
ONE_RADIAN = Math::PI / 180
ONE_DAY_IN_SECONDS = 60 * 60 * 24
J0 = 0.0009
J1970 = 2_440_588
J2000 = 2_451_545
OBLIQUITY_OF_THE_EARTH = ONE_RADIAN * 23.4397
end
class SunCalc
# :nodoc:
module Helpers
def to_julian(date)
date.to_f / ONE_DAY_IN_SECONDS - 0.5 J1970
end
def from_julian(j)
Time.at((j 0.5 - J1970) * ONE_DAY_IN_SECONDS).utc
end
def to_days(date)
to_julian(date) - J2000
end
def right_ascension(l, b)
Math.atan2(
Math.sin(l) * Math.cos(OBLIQUITY_OF_THE_EARTH) -
Math.tan(b) * Math.sin(OBLIQUITY_OF_THE_EARTH),
Math.cos(l)
)
end
def declination(l, b)
Math.asin(Math.sin(b) * Math.cos(OBLIQUITY_OF_THE_EARTH)
Math.cos(b) * Math.sin(OBLIQUITY_OF_THE_EARTH) * Math.sin(l))
end
def azimuth(h, phi, dec)
Math.atan2(Math.sin(h),
Math.cos(h) * Math.sin(phi) - Math.tan(dec) * Math.cos(phi))
end
def altitude(h, phi, dec)
Math.asin(Math.sin(phi) * Math.sin(dec)
Math.cos(phi) * Math.cos(dec) * Math.cos(h))
end
def sidereal_time(d, lw)
ONE_RADIAN * (280.16 360.9856235 * d) - lw
end
def astro_refraction(h)
# The following formula works for positive altitudes only.
h = 0 if h < 0
# Based on forumla 16.4 of "Astronomical Algorithms" 2nd edition by Jean
# Meeus (Willmann-Bell, Richmond) 1998.
0.0002967 / Math.tan(h 0.00312536 / (h 0.08901179))
end
def solar_mean_anomaly(d)
ONE_RADIAN * (357.5291 0.98560028 * d)
end
def ecliptic_longitude(m)
# Equation of center.
c = ONE_RADIAN * (1.9148 * Math.sin(m)
0.02 * Math.sin(2 * m) 0.0003 * Math.sin(3 * m))
# Perihelion of Earth.
p = ONE_RADIAN * 102.9372
m c p Math::PI
end
def sun_coords(d)
m = solar_mean_anomaly(d)
l = ecliptic_longitude(m)
{ dec: declination(l, 0),
ra: right_ascension(l, 0) }
end
def julian_cycle(d, lw)
(d - J0 - lw / (2 * Math::PI)).round
end
def approx_transit(ht, lw, n)
J0 (ht lw) / (2 * Math::PI) n
end
def solar_transit_j(ds, m, l)
J2000 ds 0.0053 * Math.sin(m) - 0.0069 * Math.sin(2 * l)
end
def hour_angle(h0, phi, dec)
Math.acos(
(Math.sin(h0) - Math.sin(phi) * Math.sin(dec)) /
(Math.cos(phi) * Math.cos(dec))
)
end
def get_set_j(h0, lw, phi, dec, n, m, l)
w = hour_angle(h0, phi, dec)
a = approx_transit(w, lw, n)
solar_transit_j(a, m, l)
end
def moon_coords(d)
# Geocentric ecliptic coordinates of the moon
l = ONE_RADIAN * (218.316 13.176396 * d) # ecliptic longitude
m = ONE_RADIAN * (134.963 13.064993 * d) # mean anomaly
f = ONE_RADIAN * (93.272 13.229350 * d) # mean distance
l = ONE_RADIAN * 6.289 * Math.sin(m) # longitude
b = ONE_RADIAN * 5.128 * Math.sin(f) # latitude
dt = 385_001 - 20_905 * Math.cos(m) # distance to the moon in km
{ ra: right_ascension(l, b),
dec: declination(l, b),
dist: dt }
end
def hours_later(date, h)
Time.at(date.to_f h * ONE_DAY_IN_SECONDS / 24).utc
end
end
end
【实例截图】
【核心代码】class SunCalc
ONE_RADIAN = Math::PI / 180
ONE_DAY_IN_SECONDS = 60 * 60 * 24
J0 = 0.0009
J1970 = 2_440_588
J2000 = 2_451_545
OBLIQUITY_OF_THE_EARTH = ONE_RADIAN * 23.4397
end
class SunCalc
# :nodoc:
module Helpers
def to_julian(date)
date.to_f / ONE_DAY_IN_SECONDS - 0.5 J1970
end
def from_julian(j)
Time.at((j 0.5 - J1970) * ONE_DAY_IN_SECONDS).utc
end
def to_days(date)
to_julian(date) - J2000
end
def right_ascension(l, b)
Math.atan2(
Math.sin(l) * Math.cos(OBLIQUITY_OF_THE_EARTH) -
Math.tan(b) * Math.sin(OBLIQUITY_OF_THE_EARTH),
Math.cos(l)
)
end
def declination(l, b)
Math.asin(Math.sin(b) * Math.cos(OBLIQUITY_OF_THE_EARTH)
Math.cos(b) * Math.sin(OBLIQUITY_OF_THE_EARTH) * Math.sin(l))
end
def azimuth(h, phi, dec)
Math.atan2(Math.sin(h),
Math.cos(h) * Math.sin(phi) - Math.tan(dec) * Math.cos(phi))
end
def altitude(h, phi, dec)
Math.asin(Math.sin(phi) * Math.sin(dec)
Math.cos(phi) * Math.cos(dec) * Math.cos(h))
end
def sidereal_time(d, lw)
ONE_RADIAN * (280.16 360.9856235 * d) - lw
end
def astro_refraction(h)
# The following formula works for positive altitudes only.
h = 0 if h < 0
# Based on forumla 16.4 of "Astronomical Algorithms" 2nd edition by Jean
# Meeus (Willmann-Bell, Richmond) 1998.
0.0002967 / Math.tan(h 0.00312536 / (h 0.08901179))
end
def solar_mean_anomaly(d)
ONE_RADIAN * (357.5291 0.98560028 * d)
end
def ecliptic_longitude(m)
# Equation of center.
c = ONE_RADIAN * (1.9148 * Math.sin(m)
0.02 * Math.sin(2 * m) 0.0003 * Math.sin(3 * m))
# Perihelion of Earth.
p = ONE_RADIAN * 102.9372
m c p Math::PI
end
def sun_coords(d)
m = solar_mean_anomaly(d)
l = ecliptic_longitude(m)
{ dec: declination(l, 0),
ra: right_ascension(l, 0) }
end
def julian_cycle(d, lw)
(d - J0 - lw / (2 * Math::PI)).round
end
def approx_transit(ht, lw, n)
J0 (ht lw) / (2 * Math::PI) n
end
def solar_transit_j(ds, m, l)
J2000 ds 0.0053 * Math.sin(m) - 0.0069 * Math.sin(2 * l)
end
def hour_angle(h0, phi, dec)
Math.acos(
(Math.sin(h0) - Math.sin(phi) * Math.sin(dec)) /
(Math.cos(phi) * Math.cos(dec))
)
end
def get_set_j(h0, lw, phi, dec, n, m, l)
w = hour_angle(h0, phi, dec)
a = approx_transit(w, lw, n)
solar_transit_j(a, m, l)
end
def moon_coords(d)
# Geocentric ecliptic coordinates of the moon
l = ONE_RADIAN * (218.316 13.176396 * d) # ecliptic longitude
m = ONE_RADIAN * (134.963 13.064993 * d) # mean anomaly
f = ONE_RADIAN * (93.272 13.229350 * d) # mean distance
l = ONE_RADIAN * 6.289 * Math.sin(m) # longitude
b = ONE_RADIAN * 5.128 * Math.sin(f) # latitude
dt = 385_001 - 20_905 * Math.cos(m) # distance to the moon in km
{ ra: right_ascension(l, b),
dec: declination(l, b),
dist: dt }
end
def hours_later(date, h)
Time.at(date.to_f h * ONE_DAY_IN_SECONDS / 24).utc
end
end
end
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论