Skip to content

Commit 901066b

Browse files
committed
bug fix:修正时区边界导致的月球升降时间在特殊日期计算不正确的问题
1 parent eaa8e22 commit 901066b

3 files changed

Lines changed: 38 additions & 37 deletions

File tree

basic/coordinate_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55
"testing"
66
)
77

8-
func Test_LoBo(t *testing.T) {
9-
fmt.Printf("%.9f", dt_cal(2020.5))
10-
}
11-
128
func Test_LoBoRaDec(t *testing.T) {
139
jde := 2451545.0
1410
lo, bo := RaDecToLoBo(jde, 10, 50)

basic/moon.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,19 +1435,20 @@ func GetMoonRiseTime(julianDay, longitude, latitude, timeZone, zenithShift, heig
14351435
timeZone = longitude / 15
14361436
var moonAngle, timeToMeridian float64 = 0, 0
14371437
julianDayZero := math.Floor(julianDay) + 0.5
1438-
julianDay = math.Floor(julianDay) + 0.5 - originalTimeZone/24 + timeZone/24 // 求0时JDE
1439-
1438+
//julianDay = math.Floor(julianDay) + 0.5 - originalTimeZone/24 + timeZone/24 // 求0时JDE
1439+
//fix:这里时间分界线应当以传入的时区为准,不应当使用当地时区,否则在0时的判断会出错
1440+
julianDay = math.Floor(julianDay) + 0.5
14401441
estimatedTime := julianDay
1441-
moonHeight := MoonHeight(julianDay, longitude, latitude, timeZone) // 求此时月亮高度
1442+
moonHeight := MoonHeight(julianDay, longitude, latitude, originalTimeZone) // 求此时月亮高度
14421443

14431444
if zenithShift != 0 {
14441445
moonAngle = -0.83333 // 修正大气折射
14451446
}
14461447
moonAngle = moonAngle - HeightDegreeByLat(height, latitude)
14471448

1448-
moonAngleTime := MoonTimeAngle(julianDay, longitude, latitude, timeZone)
1449+
moonAngleTime := MoonTimeAngle(julianDay, longitude, latitude, originalTimeZone)
14491450

1450-
if moonHeight > 0 { // 月亮在地平线上或在落下与下中天之间
1451+
if moonHeight-moonAngle > 0 { // 月亮在地平线上或在落下与下中天之间
14511452
if moonAngleTime > 180 {
14521453
timeToMeridian = (180 + 360 - moonAngleTime) / 15
14531454
} else {
@@ -1456,10 +1457,10 @@ func GetMoonRiseTime(julianDay, longitude, latitude, timeZone, zenithShift, heig
14561457
estimatedTime += (timeToMeridian/24 + (timeToMeridian/24*12.0)/15.0/24.0)
14571458
}
14581459

1459-
if moonHeight < 0 && moonAngleTime > 180 {
1460+
if moonHeight-moonAngle < 0 && moonAngleTime > 180 {
14601461
timeToMeridian = (180 - moonAngleTime) / 15
14611462
estimatedTime += (timeToMeridian/24 + (timeToMeridian/24*12.0)/15.0/24.0)
1462-
} else if moonHeight < 0 && moonAngleTime < 180 {
1463+
} else if moonHeight-moonAngle < 0 && moonAngleTime < 180 {
14631464
timeToMeridian = (180 - moonAngleTime) / 15
14641465
estimatedTime += (timeToMeridian/24 + (timeToMeridian/24*12.0)/15.0/24.0)
14651466
}
@@ -1519,28 +1520,29 @@ func GetMoonSetTime(julianDay, longitude, latitude, timeZone, zenithShift, heigh
15191520
timeZone = longitude / 15
15201521
var moonAngle, timeToMeridian float64 = 0, 0
15211522
julianDayZero := math.Floor(julianDay) + 0.5
1522-
julianDay = math.Floor(julianDay) + 0.5 - originalTimeZone/24 + timeZone/24 // 求0时JDE
1523-
1523+
//julianDay = math.Floor(julianDay) + 0.5 - originalTimeZone/24 + timeZone/24 // 求0时JDE
1524+
//fix:这里时间分界线应当以传入的时区为准,不应当使用当地时区,否则在0时的判断会出错
1525+
julianDay = math.Floor(julianDay) + 0.5
15241526
estimatedTime := julianDay
1525-
moonHeight := MoonHeight(julianDay, longitude, latitude, timeZone) // 求此时月亮高度
1527+
moonHeight := MoonHeight(julianDay, longitude, latitude, originalTimeZone) // 求此时月亮高度
15261528

15271529
if zenithShift != 0 {
15281530
moonAngle = -0.83333 // 修正大气折射
15291531
}
15301532
moonAngle = moonAngle - HeightDegreeByLat(height, latitude)
15311533

1532-
moonAngleTime := MoonTimeAngle(julianDay, longitude, latitude, timeZone)
1534+
moonAngleTime := MoonTimeAngle(julianDay, longitude, latitude, originalTimeZone)
15331535

1534-
if moonHeight < 0 {
1536+
if moonHeight-moonAngle < 0 {
15351537
timeToMeridian = (360 - moonAngleTime) / 15
15361538
estimatedTime += (timeToMeridian/24 + (timeToMeridian/24.0*12.0)/15.0/24.0)
15371539
}
15381540

15391541
// 月亮在地平线上或在落下与下中天之间
1540-
if moonHeight > 0 && moonAngleTime < 180 {
1542+
if moonHeight-moonAngle > 0 && moonAngleTime < 180 {
15411543
timeToMeridian = (-moonAngleTime) / 15
15421544
estimatedTime += (timeToMeridian/24.0 + (timeToMeridian/24.0*12.0)/15.0/24.0)
1543-
} else if moonHeight > 0 {
1545+
} else if moonHeight-moonAngle > 0 {
15441546
timeToMeridian = (360 - moonAngleTime) / 15
15451547
estimatedTime += (timeToMeridian/24.0 + (timeToMeridian/24.0*12.0)/15.0/24.0)
15461548
}

basic/moon_test.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package basic
22

33
import (
4-
"github.com/starainrt/astro/tools"
54
"fmt"
65
"math"
76
"testing"
87
"time"
8+
9+
"github.com/starainrt/astro/tools"
910
)
1011

1112
func Benchmark_MoonRiseBench(b *testing.B) {
@@ -492,11 +493,11 @@ var moonRiseSetTestData = []MoonRiseSetTestCase{
492493
{2024, 2, 29.0, 31.2357, 30.0444, 2.0, 1, 100, 2460370.429845, 2460369.865380},
493494
{2024, 2, 29.0, 31.2357, 30.0444, 2.0, 1, 1000, 2460370.428616, 2460369.866552},
494495
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 0, 0, 2460861.065509, -3.000000},
495-
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 0, 100, 2460861.064944, -3.000000},
496-
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 0, 1000, 2460861.063725, -3.000000},
497-
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 0, 2460861.062582, -3.000000},
498-
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 100, 2460861.062019, -3.000000},
499-
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 1000, 2460861.060803, -3.000000},
496+
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 0, 100, 2460861.064944, 2460860.500441},
497+
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 0, 1000, 2460861.063725, 2460860.501606},
498+
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 0, 2460861.062582, 2460860.502699},
499+
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 100, 2460861.062019, 2460860.503237},
500+
{2025, 7, 4.0, 31.2357, 30.0444, 2.0, 1, 1000, 2460861.060803, 2460860.504400},
500501
{2023, 1, 15.0, -43.1729, -22.9068, -3.0, 0, 0, -3.000000, 2459960.019905},
501502
{2023, 1, 15.0, -43.1729, -22.9068, -3.0, 0, 100, -3.000000, 2459960.020416},
502503
{2023, 1, 15.0, -43.1729, -22.9068, -3.0, 0, 1000, -3.000000, 2459960.021522},
@@ -619,10 +620,10 @@ var moonRiseSetTestData = []MoonRiseSetTestCase{
619620
{2024, 2, 29.0, -149.9003, 61.2181, -9.0, 1, 1000, 2460369.506308, 2460369.862264},
620621
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 0, 0, 2460861.205484, 2460861.493716},
621622
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 0, 100, 2460861.204170, 2460861.495011},
622-
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 0, 1000, 2460861.201358, 2460861.497780},
623-
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 0, 2460861.198757, -3.000000},
624-
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 100, 2460861.197484, -3.000000},
625-
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 1000, 2460861.194755, -3.000000},
623+
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 0, 1000, 2460861.201358, 2460860.500309},
624+
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 0, 2460861.198757, 2460860.502500},
625+
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 100, 2460861.197484, 2460860.503576},
626+
{2025, 7, 4.0, -149.9003, 61.2181, -9.0, 1, 1000, 2460861.194755, 2460860.505890},
626627
{2023, 1, 15.0, -42.6043, 71.7069, -3.0, 0, 0, 2459959.583014, 2459959.895953},
627628
{2023, 1, 15.0, -42.6043, 71.7069, -3.0, 0, 100, 2459959.581157, 2459959.897741},
628629
{2023, 1, 15.0, -42.6043, 71.7069, -3.0, 0, 1000, 2459959.577188, 2459959.901560},
@@ -656,8 +657,8 @@ var moonRiseSetTestData = []MoonRiseSetTestCase{
656657
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 0, 0, 2460369.510257, 2460369.739050},
657658
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 0, 100, 2460369.507908, 2460369.741347},
658659
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 0, 1000, 2460369.502952, 2460369.746190},
659-
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 1, 0, -3.000000, 2460369.750583},
660-
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 1, 100, -3.000000, 2460369.752708},
660+
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 1, 0, -2.000000, 2460369.750583},
661+
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 1, 100, -2.000000, 2460369.752708},
661662
{2024, 2, 29.0, -42.6043, 71.7069, -3.0, 1, 1000, -3.000000, 2460369.757206},
662663
{2025, 7, 4.0, -42.6043, 71.7069, -3.0, 0, 0, 2460861.253176, 2460861.327087},
663664
{2025, 7, 4.0, -42.6043, 71.7069, -3.0, 0, 100, 2460861.246829, 2460861.333396},
@@ -712,39 +713,41 @@ var moonRiseSetTestData = []MoonRiseSetTestCase{
712713
// TestMoonRiseSetRegression 月出月落回归测试
713714
func TestMoonRiseSetRegression(t *testing.T) {
714715
beforeDeltaT := defDeltaTFn
715-
SetDeltaTFn(DefaultDeltaT)
716+
SetDeltaTFn(DefaultDeltaTv2)
716717
defer SetDeltaTFn(beforeDeltaT)
717-
const tolerance = 0.000001 // 容差设为1微秒,对于天文计算来说已经足够精确
718+
const tolerance = 0.00011574074
718719

719720
for i, testCase := range moonRiseSetTestData {
720721
julianDay := JDECalc(testCase.Year, testCase.Month, testCase.Day)
721722

722723
// 测试月出时间
724+
723725
actualRise := GetMoonRiseTime(julianDay, testCase.Longitude, testCase.Latitude,
724726
testCase.TimeZone, testCase.ZenithShift, testCase.Height)
725727

726728
if !floatEquals(actualRise, testCase.ExpectedRise, tolerance) {
727729
t.Errorf("测试用例 %d 月出时间不匹配:\n"+
728730
" 日期: %d-%d-%.1f, 经纬度: (%.4f, %.4f), 时区: %.1f, 天顶修正: %.0f, 海拔: %.0f\n"+
729-
" 期望月出: %.6f, 实际月出: %.6f, 差值: %.9f",
731+
" 期望月出: %v, 实际月出: %.6f, 差值: %.9f",
730732
i, testCase.Year, testCase.Month, testCase.Day,
731733
testCase.Longitude, testCase.Latitude, testCase.TimeZone,
732734
testCase.ZenithShift, testCase.Height,
733-
testCase.ExpectedRise, actualRise, math.Abs(actualRise-testCase.ExpectedRise))
735+
JDE2Date(testCase.ExpectedRise), actualRise, math.Abs(actualRise-testCase.ExpectedRise))
734736
}
735737

736738
// 测试月落时间
737739
actualSet := GetMoonSetTime(julianDay, testCase.Longitude, testCase.Latitude,
738740
testCase.TimeZone, testCase.ZenithShift, testCase.Height)
739741

740742
if !floatEquals(actualSet, testCase.ExpectedSet, tolerance) {
743+
fmt.Println(JDECalc(testCase.Year, testCase.Month, testCase.Day))
741744
t.Errorf("测试用例 %d 月落时间不匹配:\n"+
742745
" 日期: %d-%d-%.1f, 经纬度: (%.4f, %.4f), 时区: %.1f, 天顶修正: %.0f, 海拔: %.0f\n"+
743-
" 期望月落: %.6f, 实际月落: %.6f, 差值: %.9f",
746+
" 期望月落: %v,%.6f, 实际月落: %v,%.6f , 差值: %.9f",
744747
i, testCase.Year, testCase.Month, testCase.Day,
745748
testCase.Longitude, testCase.Latitude, testCase.TimeZone,
746749
testCase.ZenithShift, testCase.Height,
747-
testCase.ExpectedSet, actualSet, math.Abs(actualSet-testCase.ExpectedSet))
750+
JDE2Date(testCase.ExpectedSet), testCase.ExpectedSet, JDE2Date(actualSet), actualSet, math.Abs(actualSet-testCase.ExpectedSet))
748751
}
749752
}
750753

@@ -754,7 +757,7 @@ func TestMoonRiseSetRegression(t *testing.T) {
754757
// TestMoonRiseSetSpecialCases 测试特殊情况
755758
func TestMoonRiseSetSpecialCases(t *testing.T) {
756759
beforeDeltaT := defDeltaTFn
757-
SetDeltaTFn(DefaultDeltaT)
760+
SetDeltaTFn(DefaultDeltaTv2)
758761
defer SetDeltaTFn(beforeDeltaT)
759762
testCases := []struct {
760763
name string

0 commit comments

Comments
 (0)