feat(moon): 新增行星合月查询并修正月球地心赤经赤纬接口

- 修正月球地心真/视赤经赤纬接口口径
- 新增月球与七大行星合月时刻查询
This commit is contained in:
2026-05-23 19:00:53 +08:00
parent 34ff6a36ae
commit be3af3884c
13 changed files with 1302 additions and 3 deletions
+72
View File
@@ -0,0 +1,72 @@
package moon
import (
"time"
"b612.me/astro/basic"
)
// ConjunctionPlanet 月球合月目标行星 / target planet for Moon-planet conjunction.
type ConjunctionPlanet string
const (
ConjunctionMercury ConjunctionPlanet = "mercury"
ConjunctionVenus ConjunctionPlanet = "venus"
ConjunctionMars ConjunctionPlanet = "mars"
ConjunctionJupiter ConjunctionPlanet = "jupiter"
ConjunctionSaturn ConjunctionPlanet = "saturn"
ConjunctionUranus ConjunctionPlanet = "uranus"
ConjunctionNeptune ConjunctionPlanet = "neptune"
)
func conjunctionPlanetToBasic(planet ConjunctionPlanet) basic.MoonPlanetConjunctionPlanet {
switch planet {
case ConjunctionMercury:
return basic.MoonPlanetConjunctionMercury
case ConjunctionVenus:
return basic.MoonPlanetConjunctionVenus
case ConjunctionMars:
return basic.MoonPlanetConjunctionMars
case ConjunctionJupiter:
return basic.MoonPlanetConjunctionJupiter
case ConjunctionSaturn:
return basic.MoonPlanetConjunctionSaturn
case ConjunctionUranus:
return basic.MoonPlanetConjunctionUranus
case ConjunctionNeptune:
return basic.MoonPlanetConjunctionNeptune
default:
return 0
}
}
func validConjunctionPlanet(planet ConjunctionPlanet) bool {
return conjunctionPlanetToBasic(planet) != 0
}
// LastConjunctionWithPlanet 上一次行星合月(赤经合) / previous Moon-planet conjunction.
func LastConjunctionWithPlanet(date time.Time, planet ConjunctionPlanet) time.Time {
if !validConjunctionPlanet(planet) {
return time.Time{}
}
jde := basic.TD2UT(basic.Date2JDE(date.UTC()), true)
return basic.JDE2DateByZone(basic.LastMoonPlanetConjunction(jde, conjunctionPlanetToBasic(planet)), date.Location(), false)
}
// NextConjunctionWithPlanet 下一次行星合月(赤经合) / next Moon-planet conjunction.
func NextConjunctionWithPlanet(date time.Time, planet ConjunctionPlanet) time.Time {
if !validConjunctionPlanet(planet) {
return time.Time{}
}
jde := basic.TD2UT(basic.Date2JDE(date.UTC()), true)
return basic.JDE2DateByZone(basic.NextMoonPlanetConjunction(jde, conjunctionPlanetToBasic(planet)), date.Location(), false)
}
// ClosestConjunctionWithPlanet 最近一次行星合月(赤经合) / closest Moon-planet conjunction.
func ClosestConjunctionWithPlanet(date time.Time, planet ConjunctionPlanet) time.Time {
if !validConjunctionPlanet(planet) {
return time.Time{}
}
jde := basic.TD2UT(basic.Date2JDE(date.UTC()), true)
return basic.JDE2DateByZone(basic.ClosestMoonPlanetConjunction(jde, conjunctionPlanetToBasic(planet)), date.Location(), false)
}
+82
View File
@@ -0,0 +1,82 @@
package moon
import (
"math"
"testing"
"time"
"b612.me/astro/basic"
)
func TestConjunctionPlanetWrappersMatchBasic(t *testing.T) {
loc := time.FixedZone("CST", 8*3600)
query := time.Date(2026, 1, 15, 20, 0, 0, 0, loc)
queryTT := basic.TD2UT(basic.Date2JDE(query.UTC()), true)
cases := []struct {
name string
planet ConjunctionPlanet
basic basic.MoonPlanetConjunctionPlanet
}{
{name: "Mercury", planet: ConjunctionMercury, basic: basic.MoonPlanetConjunctionMercury},
{name: "Venus", planet: ConjunctionVenus, basic: basic.MoonPlanetConjunctionVenus},
{name: "Mars", planet: ConjunctionMars, basic: basic.MoonPlanetConjunctionMars},
{name: "Jupiter", planet: ConjunctionJupiter, basic: basic.MoonPlanetConjunctionJupiter},
{name: "Saturn", planet: ConjunctionSaturn, basic: basic.MoonPlanetConjunctionSaturn},
{name: "Uranus", planet: ConjunctionUranus, basic: basic.MoonPlanetConjunctionUranus},
{name: "Neptune", planet: ConjunctionNeptune, basic: basic.MoonPlanetConjunctionNeptune},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assertSameConjunctionTime(t, "last", LastConjunctionWithPlanet(query, tc.planet), basic.LastMoonPlanetConjunction(queryTT, tc.basic), loc)
assertSameConjunctionTime(t, "next", NextConjunctionWithPlanet(query, tc.planet), basic.NextMoonPlanetConjunction(queryTT, tc.basic), loc)
assertSameConjunctionTime(t, "closest", ClosestConjunctionWithPlanet(query, tc.planet), basic.ClosestMoonPlanetConjunction(queryTT, tc.basic), loc)
})
}
}
func assertSameConjunctionTime(t *testing.T, name string, got time.Time, wantJDE float64, loc *time.Location) {
t.Helper()
want := basic.JDE2DateByZone(wantJDE, loc, false)
if got.Location() != loc {
t.Fatalf("%s location mismatch: got %q want %q", name, got.Location().String(), loc.String())
}
if !got.Equal(want) {
t.Fatalf("%s time mismatch: got %s want %s", name, got.Format(time.RFC3339Nano), want.Format(time.RFC3339Nano))
}
}
func TestClosestConjunctionReturnsNearestCandidate(t *testing.T) {
query := time.Date(2026, 1, 15, 12, 0, 0, 0, time.UTC)
last := LastConjunctionWithPlanet(query, ConjunctionMercury)
next := NextConjunctionWithPlanet(query, ConjunctionMercury)
got := ClosestConjunctionWithPlanet(query, ConjunctionMercury)
lastDiff := math.Abs(query.Sub(last).Seconds())
nextDiff := math.Abs(next.Sub(query).Seconds())
if lastDiff <= nextDiff {
if !got.Equal(last) {
t.Fatalf("closest should match last: got %s want %s", got.Format(time.RFC3339Nano), last.Format(time.RFC3339Nano))
}
return
}
if !got.Equal(next) {
t.Fatalf("closest should match next: got %s want %s", got.Format(time.RFC3339Nano), next.Format(time.RFC3339Nano))
}
}
func TestInvalidConjunctionPlanetReturnsZeroTime(t *testing.T) {
query := time.Date(2026, 1, 15, 12, 0, 0, 0, time.FixedZone("CST", 8*3600))
invalid := ConjunctionPlanet("pluto")
for name, fn := range map[string]func(time.Time, ConjunctionPlanet) time.Time{
"last": LastConjunctionWithPlanet,
"next": NextConjunctionWithPlanet,
"closest": ClosestConjunctionWithPlanet,
} {
if got := fn(query, invalid); !got.IsZero() {
t.Fatalf("%s should return zero time for invalid planet, got %s", name, got.Format(time.RFC3339Nano))
}
}
}
+43
View File
@@ -0,0 +1,43 @@
package moon
import (
"math"
"testing"
"time"
"b612.me/astro/basic"
)
func TestGeocentricApparentRaDecComponentsMatch(t *testing.T) {
date := time.Date(2026, 1, 1, 6, 0, 0, 0, time.UTC)
ra, dec := GeocentricApparentRaDec(date)
if diff := math.Abs(ra - GeocentricApparentRa(date)); diff > 1e-12 {
t.Fatalf("RA pair mismatch: got %.15f want %.15f", ra, GeocentricApparentRa(date))
}
if diff := math.Abs(dec - GeocentricApparentDec(date)); diff > 1e-12 {
t.Fatalf("Dec pair mismatch: got %.15f want %.15f", dec, GeocentricApparentDec(date))
}
}
func TestGeocentricApparentRaDecDiffersFromTopocentricAtSite(t *testing.T) {
date := time.Date(2026, 1, 1, 6, 0, 0, 0, time.FixedZone("CST", 8*3600))
geoRA, geoDec := GeocentricApparentRaDec(date)
topoRA, topoDec := ApparentRaDec(date, 121.4737, 31.2304)
if math.Abs(geoRA-topoRA) < 1e-6 && math.Abs(geoDec-topoDec) < 1e-6 {
t.Fatalf("geocentric apparent RA/Dec unexpectedly matches topocentric values: geo=(%.12f, %.12f) topo=(%.12f, %.12f)",
geoRA, geoDec, topoRA, topoDec)
}
}
func TestTrueRaDecUsesBasicGeocentricTrue(t *testing.T) {
date := time.Date(2026, 1, 1, 6, 0, 0, 0, time.UTC)
wantRA, wantDec := basic.HMoonGeocentricTrueRaDec(basic.TD2UT(basic.Date2JDE(date.UTC()), true))
gotRA, gotDec := TrueRaDec(date)
if math.Abs(gotRA-wantRA) > 1e-12 || math.Abs(gotDec-wantDec) > 1e-12 {
t.Fatalf("TrueRaDec mismatch: got (%.15f, %.15f) want (%.15f, %.15f)", gotRA, gotDec, wantRA, wantDec)
}
}
+30 -3
View File
@@ -84,7 +84,7 @@ func ApparentLo(date time.Time) float64 {
// Returns the Moon's geocentric true right ascension at the instant represented by date, in degrees.
func TrueRa(date time.Time) float64 {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonTrueRa(basic.TD2UT(jde, true))
return basic.HMoonGeocentricTrueRa(basic.TD2UT(jde, true))
}
// TrueDec 月亮地心真赤纬 / true geocentric declination.
@@ -93,7 +93,7 @@ func TrueRa(date time.Time) float64 {
// Returns the Moon's geocentric true declination at the instant represented by date, in degrees.
func TrueDec(date time.Time) float64 {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonTrueDec(basic.TD2UT(jde, true))
return basic.HMoonGeocentricTrueDec(basic.TD2UT(jde, true))
}
// TrueRaDec 月亮地心真赤经、真赤纬 / true geocentric right ascension and declination.
@@ -102,7 +102,34 @@ func TrueDec(date time.Time) float64 {
// Returns the Moon's geocentric true right ascension and declination at the instant represented by date, in degrees.
func TrueRaDec(date time.Time) (float64, float64) {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonTrueRaDec(basic.TD2UT(jde, true))
return basic.HMoonGeocentricTrueRaDec(basic.TD2UT(jde, true))
}
// GeocentricApparentRa 月亮地心视赤经 / apparent geocentric right ascension.
//
// 返回月亮在 date 对应绝对时刻的地心视赤经,单位度。
// Returns the Moon's apparent geocentric right ascension at the instant represented by date, in degrees.
func GeocentricApparentRa(date time.Time) float64 {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonGeocentricApparentRa(basic.TD2UT(jde, true))
}
// GeocentricApparentDec 月亮地心视赤纬 / apparent geocentric declination.
//
// 返回月亮在 date 对应绝对时刻的地心视赤纬,单位度。
// Returns the Moon's apparent geocentric declination at the instant represented by date, in degrees.
func GeocentricApparentDec(date time.Time) float64 {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonGeocentricApparentDec(basic.TD2UT(jde, true))
}
// GeocentricApparentRaDec 月亮地心视赤经、视赤纬 / apparent geocentric right ascension and declination.
//
// 返回月亮在 date 对应绝对时刻的地心视赤经与视赤纬,单位度。
// Returns the Moon's apparent geocentric right ascension and declination at the instant represented by date, in degrees.
func GeocentricApparentRaDec(date time.Time) (float64, float64) {
jde := basic.Date2JDE(date.UTC())
return basic.HMoonGeocentricApparentRaDec(basic.TD2UT(jde, true))
}
// ApparentRa 月亮站心视赤经 / apparent topocentric right ascension.