feat: 扩展天文计算能力

- 新增日食、月食、本地可见性、中心线、半影区域、SVG 图示与沙罗周期信息
- 新增行星冲合、留、方照、物理星历、视直径、相位、亮肢角、轨道节点等计算
- 新增木星伽利略卫星位置、现象与接触事件计算
- 新增恒星星表、星座判定、自行修正与观测辅助能力
- 新增 coord、formula、orbit、sundial、lite/sun、lite/moon 等扩展包
- 完善农历年号、月相英文别名、视差角、大气质量、折射、日晷与双星计算
- 增加 NASA、JPL Horizons、IMCCE 等回归测试数据与基线测试
- 重构基础算法文件组织,补充大量公开 API 注释和语义回归测试
- 更新中文和英文 README,补充示例、精度说明、SVG 配图
This commit is contained in:
2026-05-01 22:38:44 +08:00
parent 98ff574495
commit 3ffdbe0034
365 changed files with 63589 additions and 17508 deletions
+147
View File
@@ -0,0 +1,147 @@
package internal
import (
"errors"
"math"
. "b612.me/astro/tools"
)
const (
SynodicMonthDays = 29.530588853
)
var (
ErrNeverRise = errors.New("rise event does not occur on this date")
ErrNeverSet = errors.New("set event does not occur on this date")
ErrNotOnThisDate = errors.New("rise/set event occurs on adjacent date")
)
func MeanObliquity(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
return 23.4392911111 - (46.8150*t+0.00059*t*t-0.001813*t*t*t)/3600.0
}
func MeanSiderealTime(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
return Limit360(280.46061837 + 360.98564736629*(jd-2451545.0) + 0.000387933*t*t - t*t*t/38710000.0)
}
func EclipticToEquatorial(jd, lo, bo float64) (float64, float64) {
eps := MeanObliquity(jd)
ra := math.Atan2(Sin(lo)*Cos(eps)-Tan(bo)*Sin(eps), Cos(lo)) * 180.0 / math.Pi
if ra < 0 {
ra += 360
}
dec := ArcSin(Sin(bo)*Cos(eps) + Cos(bo)*Sin(eps)*Sin(lo))
return ra, dec
}
func HorizontalCoordinates(ra, dec, jd, lon, lat float64) (float64, float64, float64) {
lst := Limit360(MeanSiderealTime(jd) + lon)
hourAngle := Limit360(lst - ra)
altitude := ArcSin(clampUnit(Sin(lat)*Sin(dec) + Cos(lat)*Cos(dec)*Cos(hourAngle)))
y := Sin(hourAngle)
x := Cos(hourAngle)*Sin(lat) - Tan(dec)*Cos(lat)
azimuth := math.Atan2(y, x) * 180.0 / math.Pi
if azimuth < 0 {
if hourAngle < 180 {
azimuth += 360
} else {
azimuth += 180
}
} else if hourAngle < 180 {
azimuth += 180
}
return altitude, Limit360(azimuth), hourAngle
}
func TopocentricRaDec(ra, dec, observerLat, observerLon, jd, distanceEarthRadii, heightMeters float64) (float64, float64) {
u := math.Atan(0.99664719 * Tan(observerLat))
rhoSin := 0.99664719*math.Sin(u) + heightMeters/6378140.0*Sin(observerLat)
rhoCos := math.Cos(u) + heightMeters/6378140.0*Cos(observerLat)
parallax := math.Asin(1.0 / distanceEarthRadii)
hourAngle := (Limit360(MeanSiderealTime(jd) + observerLon - ra)) * math.Pi / 180.0
decRad := dec * math.Pi / 180.0
numerator := -rhoCos * math.Sin(parallax) * math.Sin(hourAngle)
denominator := math.Cos(decRad) - rhoCos*math.Sin(parallax)*math.Cos(hourAngle)
deltaRA := math.Atan2(numerator, denominator)
topRA := Limit360(ra + deltaRA*180.0/math.Pi)
topDec := math.Atan2((math.Sin(decRad)-rhoSin*math.Sin(parallax))*math.Cos(deltaRA), denominator) * 180.0 / math.Pi
return topRA, topDec
}
func SearchRiseSet(startJD, targetAltitude, stepMinutes float64, isRise bool, altitudeFn func(float64) float64) (float64, error) {
step := stepMinutes / 1440.0
prevJD := startJD
prevAlt := altitudeFn(prevJD) - targetAltitude
minAlt := prevAlt
maxAlt := prevAlt
for i := 1; i <= int(math.Round(1440.0/stepMinutes)); i++ {
currentJD := startJD + float64(i)*step
currentAlt := altitudeFn(currentJD) - targetAltitude
if currentAlt < minAlt {
minAlt = currentAlt
}
if currentAlt > maxAlt {
maxAlt = currentAlt
}
if crosses(prevAlt, currentAlt, isRise) {
return bisectEvent(prevJD, currentJD, targetAltitude, altitudeFn), nil
}
prevJD = currentJD
prevAlt = currentAlt
}
if maxAlt < 0 {
return 0, ErrNeverRise
}
if minAlt > 0 {
return 0, ErrNeverSet
}
return 0, ErrNotOnThisDate
}
func crosses(prevAlt, currentAlt float64, isRise bool) bool {
if isRise {
return prevAlt < 0 && currentAlt >= 0
}
return prevAlt > 0 && currentAlt <= 0
}
func bisectEvent(lo, hi, targetAltitude float64, altitudeFn func(float64) float64) float64 {
loAlt := altitudeFn(lo) - targetAltitude
for i := 0; i < 40; i++ {
mid := (lo + hi) / 2.0
midAlt := altitudeFn(mid) - targetAltitude
if midAlt == 0 {
return mid
}
if sameSign(loAlt, midAlt) {
lo = mid
loAlt = midAlt
} else {
hi = mid
}
}
return (lo + hi) / 2.0
}
func sameSign(a, b float64) bool {
return (a >= 0 && b >= 0) || (a <= 0 && b <= 0)
}
func clampUnit(v float64) float64 {
if v > 1 {
return 1
}
if v < -1 {
return -1
}
return v
}
+125
View File
@@ -0,0 +1,125 @@
package internal
import (
"math"
. "b612.me/astro/tools"
)
type MoonState struct {
Longitude float64
Latitude float64
DistanceEarthRadii float64
RightAscension float64
Declination float64
}
func MoonGeocentric(jd float64) MoonState {
d := jd - 2451543.5
node := Limit360(125.1228 - 0.0529538083*d)
inclination := 5.1454
perigee := Limit360(318.0634 + 0.1643573223*d)
semiMajorAxis := 60.2666
eccentricity := 0.054900
meanAnomaly := Limit360(115.3654 + 13.0649929509*d)
meanLongitude := Limit360(node + perigee + meanAnomaly)
argumentLatitude := Limit360(meanLongitude - node)
sunPerigee := Limit360(282.9404 + 0.0000470935*d)
sunEccentricity := 0.016709 - 0.000000001151*d
sunMeanAnomaly := Limit360(356.0470 + 0.9856002585*d)
sunTrueLongitude, _ := orbitalLongitudeDistance(sunPerigee, sunEccentricity, 1, sunMeanAnomaly)
longitude, latitude, distance := orbitalLongitudeLatitudeDistance(node, inclination, perigee, semiMajorAxis, eccentricity, meanAnomaly)
elongation := Limit360(meanLongitude - sunTrueLongitude)
longitude += -1.274 * Sin(meanAnomaly-2*elongation)
longitude += 0.658 * Sin(2*elongation)
longitude += -0.186 * Sin(sunMeanAnomaly)
longitude += -0.059 * Sin(2*meanAnomaly-2*elongation)
longitude += -0.057 * Sin(meanAnomaly-2*elongation+sunMeanAnomaly)
longitude += 0.053 * Sin(meanAnomaly+2*elongation)
longitude += 0.046 * Sin(2*elongation-sunMeanAnomaly)
longitude += 0.041 * Sin(meanAnomaly-sunMeanAnomaly)
longitude += -0.035 * Sin(elongation)
longitude += -0.031 * Sin(meanAnomaly+sunMeanAnomaly)
longitude += -0.015 * Sin(2*argumentLatitude-2*elongation)
longitude += 0.011 * Sin(meanAnomaly-4*elongation)
latitude += -0.173 * Sin(argumentLatitude-2*elongation)
latitude += -0.055 * Sin(meanAnomaly-argumentLatitude-2*elongation)
latitude += -0.046 * Sin(meanAnomaly+argumentLatitude-2*elongation)
latitude += 0.033 * Sin(argumentLatitude+2*elongation)
latitude += 0.017 * Sin(2*meanAnomaly+argumentLatitude)
distance += -0.58 * Cos(meanAnomaly-2*elongation)
distance += -0.46 * Cos(2*elongation)
longitude = Limit360(longitude)
ra, dec := EclipticToEquatorial(jd, longitude, latitude)
return MoonState{
Longitude: longitude,
Latitude: latitude,
DistanceEarthRadii: distance,
RightAscension: ra,
Declination: dec,
}
}
func MoonTopocentric(jd, observerLon, observerLat, heightMeters float64) MoonState {
geo := MoonGeocentric(jd)
ra, dec := TopocentricRaDec(geo.RightAscension, geoDeclinationClamp(geo.Declination), observerLat, observerLon, jd, geo.DistanceEarthRadii, heightMeters)
geo.RightAscension = ra
geo.Declination = dec
return geo
}
func orbitalLongitudeLatitudeDistance(node, inclination, perigee, axis, eccentricity, meanAnomaly float64) (float64, float64, float64) {
meanAnomalyRad := meanAnomaly * math.Pi / 180.0
eccentricAnomaly := meanAnomalyRad + eccentricity*math.Sin(meanAnomalyRad)*(1+eccentricity*math.Cos(meanAnomalyRad))
for i := 0; i < 5; i++ {
eccentricAnomaly -= (eccentricAnomaly - eccentricity*math.Sin(eccentricAnomaly) - meanAnomalyRad) / (1 - eccentricity*math.Cos(eccentricAnomaly))
}
xv := axis * (math.Cos(eccentricAnomaly) - eccentricity)
yv := axis * math.Sqrt(1-eccentricity*eccentricity) * math.Sin(eccentricAnomaly)
trueAnomaly := math.Atan2(yv, xv)
radius := math.Hypot(xv, yv)
nodeRad := node * math.Pi / 180.0
inclinationRad := inclination * math.Pi / 180.0
perigeeRad := perigee * math.Pi / 180.0
xh := radius * (math.Cos(nodeRad)*math.Cos(trueAnomaly+perigeeRad) - math.Sin(nodeRad)*math.Sin(trueAnomaly+perigeeRad)*math.Cos(inclinationRad))
yh := radius * (math.Sin(nodeRad)*math.Cos(trueAnomaly+perigeeRad) + math.Cos(nodeRad)*math.Sin(trueAnomaly+perigeeRad)*math.Cos(inclinationRad))
zh := radius * math.Sin(trueAnomaly+perigeeRad) * math.Sin(inclinationRad)
longitude := math.Atan2(yh, xh) * 180.0 / math.Pi
latitude := math.Atan2(zh, math.Hypot(xh, yh)) * 180.0 / math.Pi
return Limit360(longitude), latitude, radius
}
func orbitalLongitudeDistance(perigee, eccentricity, axis, meanAnomaly float64) (float64, float64) {
meanAnomalyRad := meanAnomaly * math.Pi / 180.0
eccentricAnomaly := meanAnomalyRad + eccentricity*math.Sin(meanAnomalyRad)*(1+eccentricity*math.Cos(meanAnomalyRad))
for i := 0; i < 5; i++ {
eccentricAnomaly -= (eccentricAnomaly - eccentricity*math.Sin(eccentricAnomaly) - meanAnomalyRad) / (1 - eccentricity*math.Cos(eccentricAnomaly))
}
xv := axis * (math.Cos(eccentricAnomaly) - eccentricity)
yv := axis * math.Sqrt(1-eccentricity*eccentricity) * math.Sin(eccentricAnomaly)
trueAnomaly := math.Atan2(yv, xv) * 180.0 / math.Pi
radius := math.Hypot(xv, yv)
return Limit360(trueAnomaly + perigee), radius
}
func geoDeclinationClamp(dec float64) float64 {
if dec > 90 {
return 90
}
if dec < -90 {
return -90
}
return dec
}
+48
View File
@@ -0,0 +1,48 @@
package internal
import . "b612.me/astro/tools"
func SunLo(jd float64) float64 {
t := (jd - 2451545.0) / 365250.0
return Limit360(280.4664567 + 360007.6982779*t + 0.03032028*t*t + t*t*t/49931.0 - t*t*t*t/15299.0 - t*t*t*t*t/1988000.0)
}
func SunMeanAnomaly(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
return Limit360(357.5291092 + 35999.0502909*t - 0.0001559*t*t - 0.00000048*t*t*t)
}
func EarthEccentricity(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
return 0.016708617 - 0.000042037*t - 0.0000001236*t*t
}
func SunCenter(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
m := SunMeanAnomaly(jd)
return (1.9146-0.004817*t-0.000014*t*t)*Sin(m) + (0.019993-0.000101*t)*Sin(2*m) + 0.00029*Sin(3*m)
}
func SunTrueLo(jd float64) float64 {
return Limit360(SunLo(jd) + SunCenter(jd))
}
func SunApparentLo(jd float64) float64 {
t := (jd - 2451545.0) / 36525.0
return Limit360(SunTrueLo(jd) - 0.00569 - 0.00478*Sin(125.04-1934.136*t))
}
func SunDistanceAU(jd float64) float64 {
c := SunCenter(jd)
m := SunMeanAnomaly(jd)
e := EarthEccentricity(jd)
return 1.000001018 * (1 - e*e) / (1 + e*Cos(m+c))
}
func SunTrueRaDec(jd float64) (float64, float64) {
return EclipticToEquatorial(jd, SunTrueLo(jd), 0)
}
func SunApparentRaDec(jd float64) (float64, float64) {
return EclipticToEquatorial(jd, SunApparentLo(jd), 0)
}
+143
View File
@@ -0,0 +1,143 @@
package moon
import (
"errors"
"time"
"b612.me/astro/basic"
lite "b612.me/astro/lite/internal"
. "b612.me/astro/tools"
)
var (
ERR_MOON_NEVER_RISE = errors.New("ERROR:极夜,月亮在今日永远在地平线下!")
ERR_MOON_NEVER_SET = errors.New("ERROR:极昼,月亮在今日永远在地平线上!")
ERR_NOT_TODAY = errors.New("ERROR:月亮已在(昨日/明日)(升起/降下)")
)
// TrueLo 轻量真黄经 / lightweight true ecliptic longitude.
func TrueLo(date time.Time) float64 {
return lite.MoonGeocentric(basic.Date2JDE(date.UTC())).Longitude
}
// TrueBo 轻量真黄纬 / lightweight true ecliptic latitude.
func TrueBo(date time.Time) float64 {
return lite.MoonGeocentric(basic.Date2JDE(date.UTC())).Latitude
}
// TrueRa 轻量真赤经 / lightweight true right ascension.
func TrueRa(date time.Time) float64 {
return lite.MoonGeocentric(basic.Date2JDE(date.UTC())).RightAscension
}
// TrueDec 轻量真赤纬 / lightweight true declination.
func TrueDec(date time.Time) float64 {
return lite.MoonGeocentric(basic.Date2JDE(date.UTC())).Declination
}
// TrueRaDec 轻量真赤经、真赤纬 / lightweight true right ascension and declination.
func TrueRaDec(date time.Time) (float64, float64) {
state := lite.MoonGeocentric(basic.Date2JDE(date.UTC()))
return state.RightAscension, state.Declination
}
// ApparentRa 轻量站心视赤经 / lightweight topocentric apparent right ascension.
func ApparentRa(date time.Time, lon, lat float64) float64 {
state := lite.MoonTopocentric(basic.Date2JDE(date.UTC()), lon, lat, 0)
return state.RightAscension
}
// ApparentDec 轻量站心视赤纬 / lightweight topocentric apparent declination.
func ApparentDec(date time.Time, lon, lat float64) float64 {
state := lite.MoonTopocentric(basic.Date2JDE(date.UTC()), lon, lat, 0)
return state.Declination
}
// ApparentRaDec 轻量站心视赤经、视赤纬 / lightweight topocentric apparent right ascension and declination.
func ApparentRaDec(date time.Time, lon, lat float64) (float64, float64) {
state := lite.MoonTopocentric(basic.Date2JDE(date.UTC()), lon, lat, 0)
return state.RightAscension, state.Declination
}
// HourAngle 轻量时角 / lightweight hour angle.
func HourAngle(date time.Time, lon, lat float64) float64 {
_, _, hourAngle := lite.HorizontalCoordinates(ApparentRa(date, lon, lat), ApparentDec(date, lon, lat), basic.Date2JDE(date.UTC()), lon, lat)
return hourAngle
}
// Azimuth 轻量方位角 / lightweight azimuth.
func Azimuth(date time.Time, lon, lat float64) float64 {
_, azimuth, _ := lite.HorizontalCoordinates(ApparentRa(date, lon, lat), ApparentDec(date, lon, lat), basic.Date2JDE(date.UTC()), lon, lat)
return azimuth
}
// Altitude 轻量高度角 / lightweight altitude.
func Altitude(date time.Time, lon, lat float64) float64 {
altitude, _, _ := lite.HorizontalCoordinates(ApparentRa(date, lon, lat), ApparentDec(date, lon, lat), basic.Date2JDE(date.UTC()), lon, lat)
return altitude
}
// Zenith 轻量天顶距 / lightweight zenith distance.
func Zenith(date time.Time, lon, lat float64) float64 {
return 90 - Altitude(date, lon, lat)
}
// SunMoonLoDiff 轻量日月黄经差 / lightweight Moon-Sun ecliptic-longitude difference.
func SunMoonLoDiff(date time.Time) float64 {
jd := basic.Date2JDE(date.UTC())
return Limit360(lite.MoonGeocentric(jd).Longitude - lite.SunApparentLo(jd))
}
// PhaseAge 轻量月龄 / lightweight lunar age in days.
func PhaseAge(date time.Time) float64 {
return lite.SynodicMonthDays * SunMoonLoDiff(date) / 360.0
}
// Phase 轻量受照比例 / lightweight illuminated fraction.
func Phase(date time.Time) float64 {
return 0.5 * (1 - Cos(SunMoonLoDiff(date)))
}
// RiseTime 轻量月出时刻 / lightweight moonrise time.
func RiseTime(date time.Time, lon, lat, height float64, aero bool) (time.Time, error) {
return riseSetTime(date, lon, lat, height, aero, true)
}
// SetTime 轻量月落时刻 / lightweight moonset time.
func SetTime(date time.Time, lon, lat, height float64, aero bool) (time.Time, error) {
return riseSetTime(date, lon, lat, height, aero, false)
}
func riseSetTime(date time.Time, lon, lat, height float64, aero, isRise bool) (time.Time, error) {
localMidnight := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
localJD := basic.Date2JDE(localMidnight)
_, offset := localMidnight.Zone()
timezone := float64(offset) / 3600.0
targetAltitude := -basic.HeightDegreeByLat(height, lat)
if aero {
targetAltitude -= 0.83333
}
altitudeFn := func(localJD float64) float64 {
utJD := localJD - timezone/24.0
state := lite.MoonTopocentric(utJD, lon, lat, height)
altitude, _, _ := lite.HorizontalCoordinates(state.RightAscension, state.Declination, utJD, lon, lat)
return altitude
}
eventJD, err := lite.SearchRiseSet(localJD, targetAltitude, 15, isRise, altitudeFn)
if err != nil {
switch {
case errors.Is(err, lite.ErrNeverRise):
return time.Time{}, ERR_MOON_NEVER_RISE
case errors.Is(err, lite.ErrNeverSet):
return time.Time{}, ERR_MOON_NEVER_SET
case errors.Is(err, lite.ErrNotOnThisDate):
return time.Time{}, ERR_NOT_TODAY
default:
return time.Time{}, err
}
}
return basic.JDE2DateByZone(eventJD, date.Location(), true), nil
}
+94
View File
@@ -0,0 +1,94 @@
package moon
import (
"math"
"testing"
"time"
fullmoon "b612.me/astro/moon"
)
func TestLiteMoonGeocentricAgainstFullPrecision(t *testing.T) {
samples := []time.Time{
time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC),
time.Date(2026, 2, 14, 12, 0, 0, 0, time.UTC),
time.Date(2026, 4, 1, 6, 0, 0, 0, time.UTC),
time.Date(2026, 6, 21, 18, 0, 0, 0, time.UTC),
time.Date(2026, 8, 9, 0, 0, 0, 0, time.UTC),
time.Date(2026, 10, 5, 6, 0, 0, 0, time.UTC),
time.Date(2026, 11, 27, 0, 0, 0, 0, time.UTC),
}
for _, sample := range samples {
if got, want := TrueLo(sample), fullmoon.TrueLo(sample); math.Abs(angleDiff(got, want)) > 0.20 {
t.Fatalf("TrueLo(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), got, want)
}
if got, want := TrueBo(sample), fullmoon.TrueBo(sample); math.Abs(got-want) > 0.06 {
t.Fatalf("TrueBo(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), got, want)
}
}
}
func TestLiteMoonPhaseAgainstFullPrecision(t *testing.T) {
samples := []time.Time{
time.Date(2026, 1, 10, 0, 0, 0, 0, time.UTC),
time.Date(2026, 1, 18, 0, 0, 0, 0, time.UTC),
time.Date(2026, 1, 25, 0, 0, 0, 0, time.UTC),
time.Date(2026, 2, 2, 0, 0, 0, 0, time.UTC),
}
for _, sample := range samples {
if got, want := Phase(sample), fullmoon.Phase(sample); math.Abs(got-want) > 0.03 {
t.Fatalf("Phase(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), got, want)
}
}
}
func TestLiteMoonRiseSetAgainstFullPrecision(t *testing.T) {
cases := []struct {
name string
date time.Time
lon float64
lat float64
}{
{"Shanghai", time.Date(2026, 1, 1, 0, 0, 0, 0, time.FixedZone("CST", 8*3600)), 121.4737, 31.2304},
{"London", time.Date(2026, 3, 17, 0, 0, 0, 0, time.UTC), -0.1278, 51.5074},
{"NewYork", time.Date(2026, 4, 16, 0, 0, 0, 0, time.FixedZone("EST", -5*3600)), -74.0060, 40.7128},
{"Sydney", time.Date(2026, 8, 14, 0, 0, 0, 0, time.FixedZone("AEST", 10*3600)), 151.2093, -33.8688},
{"Reykjavik", time.Date(2026, 11, 27, 0, 0, 0, 0, time.UTC), -21.8174, 64.1265},
}
for _, tc := range cases {
gotRise, gotErr := RiseTime(tc.date, tc.lon, tc.lat, 0, true)
wantRise, wantErr := fullmoon.RiseTime(tc.date, tc.lon, tc.lat, 0, true)
if gotErr != nil || wantErr != nil {
t.Fatalf("%s rise unexpected error: got=%v want=%v", tc.name, gotErr, wantErr)
}
assertTimeWithinMinutes(t, tc.name+" rise", gotRise, wantRise, 3.0)
gotSet, gotSetErr := SetTime(tc.date, tc.lon, tc.lat, 0, true)
wantSet, wantSetErr := fullmoon.SetTime(tc.date, tc.lon, tc.lat, 0, true)
if gotSetErr != nil || wantSetErr != nil {
t.Fatalf("%s set unexpected error: got=%v want=%v", tc.name, gotSetErr, wantSetErr)
}
assertTimeWithinMinutes(t, tc.name+" set", gotSet, wantSet, 3.0)
}
}
func angleDiff(a, b float64) float64 {
diff := math.Mod(a-b, 360)
if diff > 180 {
diff -= 360
}
if diff < -180 {
diff += 360
}
return diff
}
func assertTimeWithinMinutes(t *testing.T, name string, got, want time.Time, limitMinutes float64) {
t.Helper()
if math.Abs(got.Sub(want).Minutes()) > limitMinutes {
t.Fatalf("%s = %s, want %s", name, got, want)
}
}
+128
View File
@@ -0,0 +1,128 @@
package sun
import (
"errors"
"time"
"b612.me/astro/basic"
lite "b612.me/astro/lite/internal"
)
var (
ERR_SUN_NEVER_RISE = errors.New("ERROR:极夜,太阳在今日永远在地平线下!")
ERR_SUN_NEVER_SET = errors.New("ERROR:极昼,太阳在今日永远在地平线上!")
)
// TrueLo 轻量真黄经 / lightweight true ecliptic longitude.
func TrueLo(date time.Time) float64 {
return lite.SunTrueLo(basic.Date2JDE(date.UTC()))
}
// ApparentLo 轻量视黄经 / lightweight apparent ecliptic longitude.
func ApparentLo(date time.Time) float64 {
return lite.SunApparentLo(basic.Date2JDE(date.UTC()))
}
// Distance 轻量日地距离 / lightweight Sun-Earth distance in AU.
func Distance(date time.Time) float64 {
return lite.SunDistanceAU(basic.Date2JDE(date.UTC()))
}
// TrueRa 轻量真赤经 / lightweight true right ascension.
func TrueRa(date time.Time) float64 {
ra, _ := lite.SunTrueRaDec(basic.Date2JDE(date.UTC()))
return ra
}
// TrueDec 轻量真赤纬 / lightweight true declination.
func TrueDec(date time.Time) float64 {
_, dec := lite.SunTrueRaDec(basic.Date2JDE(date.UTC()))
return dec
}
// TrueRaDec 轻量真赤经、真赤纬 / lightweight true right ascension and declination.
func TrueRaDec(date time.Time) (float64, float64) {
return lite.SunTrueRaDec(basic.Date2JDE(date.UTC()))
}
// ApparentRa 轻量视赤经 / lightweight apparent right ascension.
func ApparentRa(date time.Time) float64 {
ra, _ := lite.SunApparentRaDec(basic.Date2JDE(date.UTC()))
return ra
}
// ApparentDec 轻量视赤纬 / lightweight apparent declination.
func ApparentDec(date time.Time) float64 {
_, dec := lite.SunApparentRaDec(basic.Date2JDE(date.UTC()))
return dec
}
// ApparentRaDec 轻量视赤经、视赤纬 / lightweight apparent right ascension and declination.
func ApparentRaDec(date time.Time) (float64, float64) {
return lite.SunApparentRaDec(basic.Date2JDE(date.UTC()))
}
// HourAngle 轻量时角 / lightweight hour angle.
func HourAngle(date time.Time, lon, lat float64) float64 {
_, _, hourAngle := lite.HorizontalCoordinates(ApparentRa(date), ApparentDec(date), basic.Date2JDE(date.UTC()), lon, lat)
return hourAngle
}
// Azimuth 轻量方位角 / lightweight azimuth.
func Azimuth(date time.Time, lon, lat float64) float64 {
_, azimuth, _ := lite.HorizontalCoordinates(ApparentRa(date), ApparentDec(date), basic.Date2JDE(date.UTC()), lon, lat)
return azimuth
}
// Altitude 轻量高度角 / lightweight altitude.
func Altitude(date time.Time, lon, lat float64) float64 {
altitude, _, _ := lite.HorizontalCoordinates(ApparentRa(date), ApparentDec(date), basic.Date2JDE(date.UTC()), lon, lat)
return altitude
}
// Zenith 轻量天顶距 / lightweight zenith distance.
func Zenith(date time.Time, lon, lat float64) float64 {
return 90 - Altitude(date, lon, lat)
}
// RiseTime 轻量日出时刻 / lightweight sunrise time.
func RiseTime(date time.Time, lon, lat, height float64, aero bool) (time.Time, error) {
return riseSetTime(date, lon, lat, height, aero, true)
}
// SetTime 轻量日落时刻 / lightweight sunset time.
func SetTime(date time.Time, lon, lat, height float64, aero bool) (time.Time, error) {
return riseSetTime(date, lon, lat, height, aero, false)
}
func riseSetTime(date time.Time, lon, lat, height float64, aero, isRise bool) (time.Time, error) {
localMidnight := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, date.Location())
localJD := basic.Date2JDE(localMidnight)
_, offset := localMidnight.Zone()
timezone := float64(offset) / 3600.0
targetAltitude := -basic.HeightDegreeByLat(height, lat)
if aero {
targetAltitude -= 0.8333
}
altitudeFn := func(localJD float64) float64 {
utJD := localJD - timezone/24.0
ra, dec := lite.SunApparentRaDec(utJD)
altitude, _, _ := lite.HorizontalCoordinates(ra, dec, utJD, lon, lat)
return altitude
}
eventJD, err := lite.SearchRiseSet(localJD, targetAltitude, 30, isRise, altitudeFn)
if err != nil {
switch {
case errors.Is(err, lite.ErrNeverRise):
return time.Time{}, ERR_SUN_NEVER_RISE
case errors.Is(err, lite.ErrNeverSet):
return time.Time{}, ERR_SUN_NEVER_SET
default:
return time.Time{}, err
}
}
return basic.JDE2DateByZone(eventJD, date.Location(), true), nil
}
+81
View File
@@ -0,0 +1,81 @@
package sun
import (
"math"
"testing"
"time"
fullsun "b612.me/astro/sun"
)
func TestLiteSunPositionAgainstFullPrecision(t *testing.T) {
samples := []time.Time{
time.Date(2026, 1, 5, 12, 0, 0, 0, time.UTC),
time.Date(2026, 3, 20, 6, 0, 0, 0, time.UTC),
time.Date(2026, 6, 21, 0, 0, 0, 0, time.UTC),
time.Date(2026, 9, 22, 18, 0, 0, 0, time.UTC),
time.Date(2026, 12, 21, 12, 0, 0, 0, time.UTC),
}
for _, sample := range samples {
if got, want := ApparentLo(sample), fullsun.ApparentLo(sample); math.Abs(angleDiff(got, want)) > 0.02 {
t.Fatalf("ApparentLo(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), got, want)
}
gotRA, gotDec := ApparentRaDec(sample)
wantRA, wantDec := fullsun.ApparentRaDec(sample)
if math.Abs(angleDiff(gotRA, wantRA)) > 0.03 {
t.Fatalf("ApparentRa(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), gotRA, wantRA)
}
if math.Abs(gotDec-wantDec) > 0.03 {
t.Fatalf("ApparentDec(%s) = %.6f, want %.6f", sample.Format(time.RFC3339), gotDec, wantDec)
}
}
}
func TestLiteSunRiseSetAgainstFullPrecision(t *testing.T) {
cases := []struct {
name string
date time.Time
lon float64
lat float64
}{
{"Shanghai", time.Date(2026, 1, 1, 0, 0, 0, 0, time.FixedZone("CST", 8*3600)), 121.4737, 31.2304},
{"London", time.Date(2026, 3, 20, 0, 0, 0, 0, time.UTC), -0.1278, 51.5074},
{"NewYork", time.Date(2026, 6, 21, 0, 0, 0, 0, time.FixedZone("EST", -5*3600)), -74.0060, 40.7128},
{"Sydney", time.Date(2026, 9, 23, 0, 0, 0, 0, time.FixedZone("AEST", 10*3600)), 151.2093, -33.8688},
}
for _, tc := range cases {
gotRise, gotErr := RiseTime(tc.date, tc.lon, tc.lat, 0, true)
wantRise, wantErr := fullsun.RiseTime(tc.date, tc.lon, tc.lat, 0, true)
if gotErr != nil || wantErr != nil {
t.Fatalf("%s rise unexpected error: got=%v want=%v", tc.name, gotErr, wantErr)
}
assertTimeWithinMinutes(t, tc.name+" rise", gotRise, wantRise, 2.0)
gotSet, gotSetErr := SetTime(tc.date, tc.lon, tc.lat, 0, true)
wantSet, wantSetErr := fullsun.SetTime(tc.date, tc.lon, tc.lat, 0, true)
if gotSetErr != nil || wantSetErr != nil {
t.Fatalf("%s set unexpected error: got=%v want=%v", tc.name, gotSetErr, wantSetErr)
}
assertTimeWithinMinutes(t, tc.name+" set", gotSet, wantSet, 2.0)
}
}
func angleDiff(a, b float64) float64 {
diff := math.Mod(a-b, 360)
if diff > 180 {
diff -= 360
}
if diff < -180 {
diff += 360
}
return diff
}
func assertTimeWithinMinutes(t *testing.T, name string, got, want time.Time, limitMinutes float64) {
t.Helper()
if math.Abs(got.Sub(want).Minutes()) > limitMinutes {
t.Fatalf("%s = %s, want %s", name, got, want)
}
}