Minor refactoring and classes move

This commit is contained in:
Arty Bishop committed 2021-04-24 16:47:42 +01:00
1 parent e9a6471b6d
commit 9ffb793dfd
16 files changed
+158 -173

No files matched your search

@@ -32,14 +32,14 @@ class DefaultSatelliteRepo(
}
override suspend fun importDataFromFile(stream: InputStream) = withContext(ioDispatcher) {
val entries = Satellite.importTLE(stream).map { tle -> SatEntry(tle) }
localSource.updateEntries(entries)
val importedEntries = Satellite.importElements(stream).map { tle -> SatEntry(tle) }
localSource.updateEntries(importedEntries)
}
override suspend fun importDataFromWeb(sources: List<String>) {
coroutineScope {
launch(ioDispatcher) {
val entries = mutableListOf<SatEntry>()
val importedEntries = mutableListOf<SatEntry>()
val streams = mutableListOf<InputStream>()
sources.forEach { source ->
remoteSource.fetchFileStream(source)?.let { stream ->
@@ -52,10 +52,10 @@ class DefaultSatelliteRepo(
}
}
streams.forEach { stream ->
val importedEntries = Satellite.importTLE(stream).map { tle -> SatEntry(tle) }
entries.addAll(importedEntries)
val entries = Satellite.importElements(stream).map { tle -> SatEntry(tle) }
importedEntries.addAll(entries)
}
localSource.updateEntries(entries)
localSource.updateEntries(importedEntries)
}
launch(ioDispatcher) {
val transmitters = remoteSource.fetchTransmitters().filter { it.isAlive }
@@ -19,7 +19,7 @@ package com.rtbishop.look4sat.domain.predict4kotlin
import java.util.*
class PassPredictor(private val satellite: Satellite, private val qth: GroundPos) {
class PassPredictor(private val satellite: Satellite, private val gsp: StationPosition) {
private val oneQuarterOrbitMin = (24.0 * 60.0 / satellite.tle.meanmo / 4.0).toInt()
private val speedOfLight = 2.99792458E8
@@ -35,7 +35,7 @@ class PassPredictor(private val satellite: Satellite, private val qth: GroundPos
}
fun getSatPos(date: Date): SatPos {
return satellite.getPosition(qth, date)
return satellite.getPosition(gsp, date)
}
fun getPositions(refDate: Date, stepSec: Int, minBefore: Int, orbits: Double): List<SatPos> {
@@ -58,7 +58,7 @@ class PassPredictor(private val satellite: Satellite, private val qth: GroundPos
var shouldWindBack = windBack
var lastAosDate: Date
var count = 0
if (satellite.willBeSeen(qth)) {
if (satellite.willBeSeen(gsp)) {
if (satellite.tle.isDeepspace) {
passes.add(nextDeepSpacePass(refDate))
} else {
@@ -17,4 +17,4 @@
*/
package com.rtbishop.look4sat.domain.predict4kotlin
data class Position(val lat: Double, val lon: Double)
data class Position(val latitude: Double, val longitude: Double)
@@ -0,0 +1,68 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.rtbishop.look4sat.domain.predict4kotlin
import kotlin.math.round
class QthConverter {
fun qthToPosition(qthString: String): Position? {
val trimmedQth = qthString.take(6)
if (!isValidQTH(trimmedQth)) return null
val lonFirst = (trimmedQth[0].toUpperCase().toInt() - 65) * 20
val latFirst = (trimmedQth[1].toUpperCase().toInt() - 65) * 10
val lonSecond = trimmedQth[2].toString().toInt() * 2
val latSecond = trimmedQth[3].toString().toInt()
val lonThird = (((trimmedQth[4].toLowerCase().toInt() - 97) / 12.0) + (1.0 / 24.0)) - 180
val latThird = (((trimmedQth[5].toLowerCase().toInt() - 97) / 24.0) + (1.0 / 48.0)) - 90
val longitude = (lonFirst + lonSecond + lonThird).roundToDecimals(4)
val latitude = (latFirst + latSecond + latThird).roundToDecimals(4)
return Position(latitude, longitude)
}
fun positionToQTH(lat: Double, lon: Double): String? {
if (!isValidPosition(lat, lon)) return null
val tempLon = if (lon > 180.0) lon - 360 else lon
val upper = "ABCDEFGHIJKLMNOPQRSTUVWX"
val lower = "abcdefghijklmnopqrstuvwx"
val longitude = tempLon + 180
val latitude = lat + 90
val lonFirst = upper[(longitude / 20).toInt()]
val latFirst = upper[(latitude / 10).toInt()]
val lonSecond = ((longitude / 2) % 10).toInt().toString()
val latSecond = (latitude % 10).toInt().toString()
val lonThird = lower[((longitude % 2) * 12).toInt()]
val latThird = lower[((latitude % 1) * 24).toInt()]
return "$lonFirst$latFirst$lonSecond$latSecond$lonThird$latThird"
}
private fun isValidQTH(qthString: String): Boolean {
val qthPattern = "[a-xA-X][a-xA-X][0-9][0-9][a-xA-X][a-xA-X]".toRegex()
return qthString.matches(qthPattern)
}
private fun isValidPosition(lat: Double, lon: Double): Boolean {
return (lat > -90.0 && lat < 90.0) && (lon > -180.0 && lon < 360.0)
}
private fun Double.roundToDecimals(decimals: Int): Double {
var multiplier = 1.0
repeat(decimals) { multiplier *= 10 }
return round(this * multiplier) / multiplier
}
}
@@ -22,39 +22,28 @@ import kotlin.math.*
class SatPos {
private val earthRadiusKm = 6.378137E3
private val r0 = 6378.16
private val earthRadiusKm = 6378.16
// Radians
var azimuth = 0.0
var elevation = 0.0
var latitude = 0.0
var longitude = 0.0
var time = Date()
var altitude = 0.0
var range = 0.0
var rangeRate = 0.0
var phase = 0.0
var altitude = 0.0
var theta = 0.0
var eclipseDepth = 0.0
var eclipsed = false
var aboveHorizon = false
fun getDate(): Date {
return Date(time.time)
}
var time = Date()
fun getRangeCircleRadiusKm(): Double {
return 0.5 * (12756.33 * acos(earthRadiusKm / (earthRadiusKm + altitude)))
return earthRadiusKm * acos(earthRadiusKm / (earthRadiusKm + altitude))
}
fun getRangeCircle(incrementDegrees: Double = 1.0): List<Position> {
val positions = mutableListOf<Position>()
val radiusKm = this.getRangeCircleRadiusKm()
val lat = this.latitude
val lon = this.longitude
val beta = radiusKm / r0
val beta = getRangeCircleRadiusKm() / earthRadiusKm
var tempAzimuth = 0
while (tempAzimuth < 360) {
val azimuth = tempAzimuth / 360.0 * 2.0 * Math.PI
@@ -76,22 +65,8 @@ class SatPos {
}
while (rangelon < 0.0) rangelon += Math.PI * 2.0
while (rangelon > Math.PI * 2.0) rangelon -= Math.PI * 2.0
rangelat = (rangelat / (2.0 * Math.PI)) * 360.0
rangelon = (rangelon / (2.0 * Math.PI)) * 360.0
// if (rangelong < 180.0) {
// rangelong = -rangelong;
// }
// else if (rangelong > 180.0) {
// rangelong = 360.0 - rangelong;
// }
//
// if (rangelat < 90.0) {
// rangelat = -rangelat;
// }
// else if (rangelat > 90.0) {
// rangelat = 180.0 - rangelat;
// }
rangelat = Math.toDegrees(rangelat)
rangelon = Math.toDegrees(rangelon)
positions.add(Position(rangelat, rangelon))
tempAzimuth += incrementDegrees.toInt()
}
@@ -42,21 +42,21 @@ abstract class Satellite(val tle: TLE) {
var s4 = 0.0
val xke = 7.43669161E-2
fun willBeSeen(pos: GroundPos): Boolean {
fun willBeSeen(pos: StationPosition): Boolean {
return if (tle.meanmo < 1e-8) false else {
var lin = tle.incl
if (lin >= 90.0) lin = 180.0 - lin
val sma = 331.25 * exp(ln(1440.0 / tle.meanmo) * (2.0 / 3.0))
val apogee = sma * (1.0 + tle.eccn) - earthRadius
acos(earthRadius / (apogee + earthRadius)) + lin * deg2Rad > abs(pos.lat * deg2Rad)
acos(earthRadius / (apogee + earthRadius)) + lin * deg2Rad > abs(pos.latitude * deg2Rad)
}
}
fun getPredictor(pos: GroundPos): PassPredictor {
fun getPredictor(pos: StationPosition): PassPredictor {
return PassPredictor(this, pos)
}
fun getPosition(pos: GroundPos, time: Date): SatPos {
fun getPosition(pos: StationPosition, time: Date): SatPos {
val satPos = SatPos()
// Date/time at which the position and velocity were calculated
val julUTC = calcCurrentDaynum(time) + 2444238.5
@@ -121,7 +121,7 @@ abstract class Satellite(val tle: TLE) {
julianUTC: Double,
positionVector: Vector4,
velocityVector: Vector4,
gsPos: GroundPos,
gsPos: StationPosition,
squintVector: Vector4,
satPos: SatPos
) {
@@ -144,8 +144,8 @@ abstract class Satellite(val tle: TLE) {
velocityVector.z - obsVel.z
)
magnitude(range)
val sinLat = sin(deg2Rad * gsPos.lat)
val cosLat = cos(deg2Rad * gsPos.lat)
val sinLat = sin(deg2Rad * gsPos.latitude)
val cosLat = cos(deg2Rad * gsPos.latitude)
val sinTheta = sin(gsPosTheta.get())
val cosTheta = cos(gsPosTheta.get())
val topS = sinLat * cosTheta * range.x + sinLat * sinTheta * range.y - cosLat * range.z
@@ -163,20 +163,20 @@ abstract class Satellite(val tle: TLE) {
// Returns the ECI position and velocity of the observer
private fun calculateUserPosVel(
time: Double,
gsPos: GroundPos,
gsPos: StationPosition,
gsPosTheta: AtomicReference<Double>,
obsPos: Vector4,
obsVel: Vector4
) {
val mFactor = 7.292115E-5
gsPosTheta.set(mod2PI(thetaGJD(time) + deg2Rad * gsPos.lon))
gsPosTheta.set(mod2PI(thetaGJD(time) + deg2Rad * gsPos.longitude))
val c =
invert(sqrt(1.0 + flatFactor * (flatFactor - 2) * sqr(sin(deg2Rad * gsPos.lat))))
invert(sqrt(1.0 + flatFactor * (flatFactor - 2) * sqr(sin(deg2Rad * gsPos.latitude))))
val sq = sqr(1.0 - flatFactor) * c
val achcp = (earthRadius * c + gsPos.alt / 1000.0) * cos(deg2Rad * gsPos.lat)
val achcp = (earthRadius * c + gsPos.altitude / 1000.0) * cos(deg2Rad * gsPos.latitude)
obsPos.setXYZ(
achcp * cos(gsPosTheta.get()), achcp * sin(gsPosTheta.get()),
(earthRadius * sq + gsPos.alt / 1000.0) * sin(deg2Rad * gsPos.lat)
(earthRadius * sq + gsPos.altitude / 1000.0) * sin(deg2Rad * gsPos.latitude)
)
obsVel.setXYZ(-mFactor * obsPos.y, mFactor * obsPos.x, 0.0)
magnitude(obsPos)
@@ -357,9 +357,9 @@ abstract class Satellite(val tle: TLE) {
}
}
fun importTLE(tleStream: InputStream): List<TLE> {
val importedTles = mutableListOf<TLE>()
fun importElements(tleStream: InputStream): List<TLE> {
val currentTLE = arrayOf(String(), String(), String())
val importedTles = mutableListOf<TLE>()
var line = 0
tleStream.bufferedReader().forEachLine {
if (line != 2) {
@@ -367,26 +367,31 @@ abstract class Satellite(val tle: TLE) {
line++
} else {
currentTLE[line] = it
importedTles.add(parseTLE(currentTLE))
parseElement(currentTLE)?.let { tle -> importedTles.add(tle) }
line = 0
}
}
return importedTles
}
private fun parseTLE(tle: Array<String>): TLE {
val name: String = tle[0].trim()
val epoch: Double = tle[1].substring(18, 32).toDouble()
val meanmo: Double = tle[2].substring(52, 63).toDouble()
val eccn: Double = 1.0e-07 * tle[2].substring(26, 33).toDouble()
val incl: Double = tle[2].substring(8, 16).toDouble()
val raan: Double = tle[2].substring(17, 25).toDouble()
val argper: Double = tle[2].substring(34, 42).toDouble()
val meanan: Double = tle[2].substring(43, 51).toDouble()
val catnum: Int = tle[1].substring(2, 7).trim().toInt()
val bstar: Double = 1.0e-5 * tle[1].substring(53, 59).toDouble() /
10.0.pow(tle[1].substring(60, 61).toDouble())
return TLE(name, epoch, meanmo, eccn, incl, raan, argper, meanan, catnum, bstar)
private fun parseElement(tle: Array<String>): TLE? {
if (tle.size != 3) return null
try {
val name: String = tle[0].trim()
val epoch: Double = tle[1].substring(18, 32).toDouble()
val meanmo: Double = tle[2].substring(52, 63).toDouble()
val eccn: Double = 1.0e-07 * tle[2].substring(26, 33).toDouble()
val incl: Double = tle[2].substring(8, 16).toDouble()
val raan: Double = tle[2].substring(17, 25).toDouble()
val argper: Double = tle[2].substring(34, 42).toDouble()
val meanan: Double = tle[2].substring(43, 51).toDouble()
val catnum: Int = tle[1].substring(2, 7).trim().toInt()
val bstar: Double = 1.0e-5 * tle[1].substring(53, 59).toDouble() /
10.0.pow(tle[1].substring(60, 61).toDouble())
return TLE(name, epoch, meanmo, eccn, incl, raan, argper, meanan, catnum, bstar)
} catch (exception: Exception) {
return null
}
}
}
}
@@ -17,4 +17,9 @@
*/
package com.rtbishop.look4sat.domain.predict4kotlin
data class GroundPos(val lat: Double, val lon: Double, val alt: Double, val name: String = "base")
data class StationPosition(
val latitude: Double,
val longitude: Double,
val altitude: Double,
val name: String = "base"
)
@@ -0,0 +1,60 @@
/*
* Look4Sat. Amateur radio satellite tracker and pass predictor.
* Copyright (C) 2019-2021 Arty Bishop (bishop.arty@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.rtbishop.look4sat
import com.rtbishop.look4sat.domain.predict4kotlin.QthConverter
import org.junit.Test
class QthConverterTest {
private val converter = QthConverter()
@Test
fun `Given valid QTH returns correct location`() {
var result = converter.qthToPosition("io91VL39FX")
assert(result?.latitude == 51.4792 && result.longitude == -0.2083)
result = converter.qthToPosition("JN58TD")
assert(result?.latitude == 48.1458 && result.longitude == 11.6250)
result = converter.qthToPosition("gf15vc")
assert(result?.latitude == -34.8958 && result.longitude == -56.2083)
result = converter.qthToPosition("fm18LW")
assert(result?.latitude == 38.9375 && result.longitude == -77.0417)
}
@Test
fun `Given invalid QTH returns null`() {
var result = converter.qthToPosition("ZZ00zz")
assert(result == null)
result = converter.qthToPosition("JN58tz")
assert(result == null)
}
@Test
fun `Given valid location returns correct QTH`() {
assert(converter.positionToQTH(51.4878, -0.2146) == "IO91vl")
assert(converter.positionToQTH(48.1466, 11.6083) == "JN58td")
assert(converter.positionToQTH(-34.91, -56.2116) == "GF15vc")
assert(converter.positionToQTH(38.92, -77.065) == "FM18lw")
}
@Test
fun `Given invalid location returns null`() {
assert(converter.positionToQTH(91.0542, -170.1142) == null)
assert(converter.positionToQTH(89.0542, -240.1142) == null)
}
}