Satellite tracker and pass predictor for Android, inspired by Gpredict
Kotlin amsatcelestrakgpsham-radiohamlibhamradiokotlinnasaradiosatellitesatellite-datasatellite-positionsatellite-predictionsatellitessatnogssdrspacespacexstarlink
Corrected CelestialComputer moon calculations error #266
Arty Bishop2f3adb4949a3
diff --git a/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputer.kt b/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputer.ktindex 45b4db01..04efff34 100644--- a/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputer.kt+++ b/core/domain/src/main/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputer.kt@@ -62,7 +62,7 @@ object CelestialComputer {/** Moon position as seen from a ground observer. */data class MoonPosition(val azimuth: Double, // degrees, 0=N, 90=E- val elevation: Double, // degrees+ val elevation: Double, // degrees, topocentric (parallax corrected)val rightAscension: Double, // degreesval declination: Double, // degreesval gha: Double, // Greenwich Hour Angle, degrees@@ -232,21 +232,27 @@ object CelestialComputer {val lm = l * DEG2RADval moonDx = 3.0 / (PI * p)- // Ecliptic → equatorial+ // Ecliptic → equatorial. atan2 keeps RA in the right quadrant for any ecliptic longitude:+ // the periodic terms above can push l slightly outside [0, 360), and an acos based+ // conversion then mirrors RA around 0h and throws the Moon off by up to 16° in azimuth.val z = (jd - 2415020.5) / 365.2422val ob = (23.452294 - (0.46845 * z + 5.9e-07 * z * z) / 3600.0).toRadians()val dec = asin(sin(bRad) * cos(ob) + cos(bRad) * sin(ob) * sin(lm))- var ra = acos(cos(bRad) * cos(lm) / cos(dec)); if (lm > PI) ra = TWO_PI - ra+ val ra = mod2PI(atan2(sin(lm) * cos(ob) - tan(bRad) * sin(ob), cos(lm)))val n = observer.latitude * DEG2RADt = (jd - 2451545.0) / 36525.0- var teg = 280.46061837 + 360.98564736629 * (jd - 2451545.0) + (0.000387933 * t - t * t / 38710000.0) * t- while (teg > 360.0) teg -= 360.0+ val teg = modulus(+ 280.46061837 + 360.98564736629 * (jd - 2451545.0) + (0.000387933 * t - t * t / 38710000.0) * t, 360.0+ )// LST = GMST + east longitude (positive east convention)val th = mod2PI((teg + observer.longitude) * DEG2RAD)val h = th - raval azVal = atan2(sin(h), cos(h) * sin(n) - tan(dec) * cos(n)) + PIval el = asin(sin(n) * sin(dec) + cos(n) * cos(dec) * cos(h))+ // Geocentric → topocentric. Parallax is up to ~1°, two full Moon diameters, and shifts+ // the Moon along the vertical circle only, so just the elevation needs correcting.+ val elTopo = el / DEG2RAD - p * cos(el)// Moon radial velocity approximation (from "Amateur Radio Software", GM4ANB, RSGB 1985)val mm = fixAngle(1.319238 + daynum * 0.228027135)@@ -266,7 +272,7 @@ object CelestialComputer {return MoonPosition(azimuth = azVal / DEG2RAD,- elevation = el / DEG2RAD,+ elevation = elTopo,rightAscension = moonRa,declination = dec / DEG2RAD,gha = moonGha,diff --git a/core/domain/src/test/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputerTest.kt b/core/domain/src/test/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputerTest.ktindex 6685e85c..43550557 100644--- a/core/domain/src/test/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputerTest.kt+++ b/core/domain/src/test/java/com/rtbishop/look4sat/core/domain/predict/CelestialComputerTest.kt@@ -71,6 +71,41 @@ class CelestialComputerTest {private fun String.toMillis(): Long = Instant.parse(this).toEpochMilli()+ private data class MoonCase(+ val latitude: Double,+ val longitude: Double,+ val iso: String,+ val azimuth: Double,+ val elevation: Double+ )++ @Test+ fun `getMoonPosition matches reference topocentric az-el across the ecliptic longitude wrap`() {+ // Reference values are topocentric, refraction excluded. The first three cases sit on or+ // near the ecliptic longitude 0h crossing, where an acos based RA conversion mirrors the+ // Moon around 0h and skews azimuth by up to 16 degrees.+ val cases = listOf(+ MoonCase(52.52, 13.405, "2026-01-23T12:12:00Z", 133.317, 28.418),+ MoonCase(52.52, 13.405, "2026-09-26T02:00:00Z", 239.701, 20.508),+ MoonCase(52.52, 13.405, "2026-09-26T05:00:00Z", 276.290, -4.496),+ MoonCase(52.52, 13.405, "2026-10-05T19:00:00Z", 358.283, -19.612),+ MoonCase(-33.8688, 151.2093, "2026-03-15T11:00:00Z", 184.964, -36.659),+ MoonCase(40.7128, -74.0060, "2026-07-04T02:00:00Z", 98.385, -10.437)+ )++ cases.forEach { testCase ->+ val observer = GeoPos(testCase.latitude, testCase.longitude)+ val moon = CelestialComputer.getMoonPosition(observer, testCase.iso.toMillis())+ val azimuthDelta = abs((moon.azimuth - testCase.azimuth + 540.0) % 360.0 - 180.0)++ assertTrue(+ "${testCase.iso}: azimuth ${moon.azimuth} should be within 0.5 of ${testCase.azimuth}",+ azimuthDelta < 0.5+ )+ assertEquals("${testCase.iso}: elevation", testCase.elevation, moon.elevation, 0.5)+ }+ }+private companion object {private const val SUNRISE_SET_THRESHOLD = -0.8333private const val HOUR_MILLIS = 60L * 60L * 1000Ldiff --git a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.ktindex 7761d7b0..96309002 100644--- a/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt+++ b/feature/radar/src/main/java/com/rtbishop/look4sat/feature/radar/RadarViewModel.kt@@ -17,6 +17,7 @@*/package com.rtbishop.look4sat.feature.radar+import android.util.Logimport androidx.lifecycle.ViewModelimport androidx.lifecycle.viewModelScopeimport androidx.lifecycle.viewmodel.initializer@@ -50,6 +51,10 @@ import kotlinx.coroutines.flow.collectLatestimport kotlinx.coroutines.flow.updateimport kotlinx.coroutines.isActiveimport kotlinx.coroutines.launch+import java.text.SimpleDateFormat+import java.util.Date+import java.util.Locale+import java.util.TimeZoneimport kotlin.time.Duration.Companion.millisecondsclass RadarViewModel(@@ -206,9 +211,11 @@ class RadarViewModel(// Recompute celestial positions at most once per minute (they move very slowly)if (timeNow - lastCelestialUpdateMs >= 60_000L) {+ val isFirstUpdate = lastCelestialUpdateMs == 0LcachedSunPos = CelestialComputer.getSunPosition(stationPos, timeNow)cachedMoonPos = CelestialComputer.getMoonPosition(stationPos, timeNow)lastCelestialUpdateMs = timeNow+ if (isFirstUpdate) logCelestialPositions(timeNow)}val (time, isAos) = computeTimer(pass.isDeepSpace, pass.aosTime, pass.losTime, timeNow)@@ -222,6 +229,23 @@ class RadarViewModel(sendPassData(pos)}+ // Logs the computed celestial positions once when the screen opens, and again whenever the+ // station position changes, so they can be checked against an external ephemeris.+ // Values are geometric: no atmospheric refraction is applied, unlike most online tools.+ private fun logCelestialPositions(timeMillis: Long) {+ val sun = cachedSunPos ?: return+ val moon = cachedMoonPos ?: return+ val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT)+ .apply { timeZone = TimeZone.getTimeZone("UTC") }+ val tag = "RadarCelestial"+ Log.i(tag, "Celestial check at ${formatter.format(Date(timeMillis))} UTC")+ Log.i(tag, "Observer: ${stationPos.latitude.fmt()}, ${stationPos.longitude.fmt()}, ${stationPos.altitude.fmt()}m")+ Log.i(tag, "Sun: azimuth=${sun.azimuth.fmt()}° elevation=${sun.elevation.fmt()}°")+ Log.i(tag, "Moon: azimuth=${moon.azimuth.fmt()}° elevation=${moon.elevation.fmt()}°")+ }++ private fun Double.fmt(): String = String.format(Locale.ROOT, "%.3f", this)+private fun collectRadioTrackingState() {viewModelScope.launch {trackingService.state.collect { svc ->