astro/lite/sun/sun.go
starainrt 3ffdbe0034
feat: 扩展天文计算能力
- 新增日食、月食、本地可见性、中心线、半影区域、SVG 图示与沙罗周期信息
- 新增行星冲合、留、方照、物理星历、视直径、相位、亮肢角、轨道节点等计算
- 新增木星伽利略卫星位置、现象与接触事件计算
- 新增恒星星表、星座判定、自行修正与观测辅助能力
- 新增 coord、formula、orbit、sundial、lite/sun、lite/moon 等扩展包
- 完善农历年号、月相英文别名、视差角、大气质量、折射、日晷与双星计算
- 增加 NASA、JPL Horizons、IMCCE 等回归测试数据与基线测试
- 重构基础算法文件组织,补充大量公开 API 注释和语义回归测试
- 更新中文和英文 README,补充示例、精度说明、SVG 配图
2026-05-01 22:38:44 +08:00

129 lines
4.2 KiB
Go

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
}