Skip to content

Satellite tracker and pass predictor for Android, inspired by Gpredict

Moved TLE import and Satellite creation to TLE companion object

Arty Bishop60a67f86b670
diff --git a/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt b/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt
index c510a02a..e7a190ca 100644
--- a/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt
+++ b/app/src/main/java/com/rtbishop/look4sat/framework/db/RoomConverters.kt
@@ -20,6 +20,7 @@ package com.rtbishop.look4sat.framework.db
import androidx.room.TypeConverter
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
import com.rtbishop.look4sat.domain.predict4kotlin.TLE
+import com.rtbishop.look4sat.domain.predict4kotlin.createSat
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
@@ -46,6 +47,6 @@ object RoomConverters {
@JvmStatic
@TypeConverter
fun satFromString(string: String): Satellite? {
- return Satellite.createSat(tleAdapter.fromJson(string))
+ return tleAdapter.fromJson(string)?.createSat()
}
}
diff --git a/core/src/main/java/com/rtbishop/look4sat/data/SatDataRepository.kt b/core/src/main/java/com/rtbishop/look4sat/data/SatDataRepository.kt
index 49105290..fd3b9892 100644
--- a/core/src/main/java/com/rtbishop/look4sat/data/SatDataRepository.kt
+++ b/core/src/main/java/com/rtbishop/look4sat/data/SatDataRepository.kt
@@ -21,6 +21,7 @@ import com.rtbishop.look4sat.domain.model.SatEntry
import com.rtbishop.look4sat.domain.model.SatItem
import com.rtbishop.look4sat.domain.model.SatTrans
import com.rtbishop.look4sat.domain.predict4kotlin.Satellite
+import com.rtbishop.look4sat.domain.predict4kotlin.TLE
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
@@ -90,6 +91,6 @@ class SatDataRepository(
}
private fun importSatEntries(stream: InputStream): List<SatEntry> {
- return Satellite.importElements(stream).map { tle -> SatEntry(tle) }
+ return TLE.importElements(stream).map { tle -> SatEntry(tle) }
}
}
diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatPredictor.kt b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatPredictor.kt
index 63920432..933d13a0 100644
--- a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatPredictor.kt
+++ b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatPredictor.kt
@@ -2,200 +2,200 @@ package com.rtbishop.look4sat.domain.predict4kotlin
import java.util.*
import kotlin.math.*
-
-const val speedOfLight = 2.99792458E8
-const val earthRadiusKm = 6378.16
-
-fun Satellite.getQuarterOrbitMin(): Int {
- return (24.0 * 60.0 / this.tle.meanmo / 4.0).toInt()
-}
-
-fun SatPos.getRangeCircle(): List<Position> {
- val positions = mutableListOf<Position>()
- val lat = this.latitude
- val lon = this.longitude
- // rangeCircleRadiusKm
- // earthRadiusKm * acos(earthRadiusKm / (earthRadiusKm + satPos.altitude))
- val beta = acos(earthRadiusKm / (earthRadiusKm + this.altitude))
- var tempAzimuth = 0
- while (tempAzimuth < 360) {
- val azimuth = tempAzimuth / 360.0 * 2.0 * Math.PI
- var rangelat = asin(sin(lat) * cos(beta) + cos(azimuth) * sin(beta) * cos(lat))
- val num = (cos(beta) - (sin(lat) * sin(rangelat)))
- val den = cos(lat) * cos(rangelat)
- var rangelon = if (tempAzimuth == 0 && (beta > ((Math.PI / 2.0) - lat))) {
- lon + Math.PI
- } else if (tempAzimuth == 180 && (beta > ((Math.PI / 2.0) - lat))) {
- lon + Math.PI
- } else if (abs(num / den) > 1.0) {
- lon
- } else {
- if ((180 - tempAzimuth) >= 0) {
- lon - acos(num / den)
- } else {
- lon + acos(num / den)
- }
- }
- while (rangelon < 0.0) rangelon += Math.PI * 2.0
- while (rangelon > Math.PI * 2.0) rangelon -= Math.PI * 2.0
- rangelat = Math.toDegrees(rangelat)
- rangelon = Math.toDegrees(rangelon)
- positions.add(Position(rangelat, rangelon))
- tempAzimuth += 1
- }
- return positions
-}
-
-fun SatPos.getDownlinkFreq(freq: Long): Long {
- return (freq.toDouble() * (speedOfLight - this.rangeRate * 1000.0) / speedOfLight).toLong()
-}
-
-fun SatPos.getUplinkFreq(freq: Long): Long {
- return (freq.toDouble() * (speedOfLight + this.rangeRate * 1000.0) / speedOfLight).toLong()
-}
-
-fun Satellite.getSatPos(date: Date): SatPos {
- return this.getPosition(stationPos, date)
-}
-
-fun Satellite.getPositions(date: Date, stepSec: Int, minBefore: Int, orbits: Double): List<SatPos> {
- val positions = mutableListOf<SatPos>()
- val orbitalPeriod = 24 * 60 / this.tle.meanmo
- val endDate = Date(date.time + (orbitalPeriod * orbits * 60L * 1000L).toLong())
- val startDate = Date(date.time - minBefore * 60L * 1000L)
- var currentDate = startDate
- while (currentDate.before(endDate)) {
- positions.add(getSatPos(currentDate))
- currentDate = Date(currentDate.time + stepSec * 1000)
- }
- return positions
-}
-
-fun Satellite.getPasses(refDate: Date, hoursAhead: Int, windBack: Boolean): List<SatPass> {
- val passes = mutableListOf<SatPass>()
- val oneQuarterOrbitMin = this.getQuarterOrbitMin()
- val endDate = Date(refDate.time + hoursAhead * 60L * 60L * 1000L)
- var startDate = refDate
- var shouldWindBack = windBack
- var lastAosDate: Date
- var count = 0
- if (this.willBeSeen(stationPos)) {
- if (this.tle.isDeepspace) {
- passes.add(nextDeepSpacePass(refDate))
- } else {
- do {
- if (count > 0) shouldWindBack = false
- val pass = nextNearEarthPass(startDate, shouldWindBack)
- lastAosDate = pass.aosDate
- passes.add(pass)
- startDate =
- Date(pass.losDate.time + (oneQuarterOrbitMin * 3) * 60L * 1000L)
- count++
- } while (lastAosDate < endDate)
- }
- }
- return passes
-}
-
-private fun Satellite.nextDeepSpacePass(refDate: Date): SatPass {
- val satPos = getSatPos(refDate)
- val id = this.tle.catnum
- val name = this.tle.name
- val isDeep = this.tle.isDeepspace
- val aos = Date(refDate.time - 24 * 60L * 60L * 1000L).time
- val los = Date(refDate.time + 24 * 60L * 60L * 1000L).time
- val tca = Date((aos + los) / 2).time
- val az = Math.toDegrees(satPos.azimuth)
- val elev = Math.toDegrees(satPos.elevation)
- val alt = satPos.altitude
- return SatPass(id, name, isDeep, aos, az, los, az, tca, az, alt, elev, this)
-}
-
-private fun Satellite.nextNearEarthPass(refDate: Date, windBack: Boolean = false): SatPass {
- val oneQuarterOrbitMin = this.getQuarterOrbitMin()
- val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply {
- clear()
- timeInMillis = refDate.time
- }
- val id = this.tle.catnum
- val name = this.tle.name
- val isDeep = this.tle.isDeepspace
-
- var elevation: Double
- var maxElevation = 0.0
- var alt = 0.0
- var tcaAz = 0.0
-
- // wind back time 1/4 of an orbit
- if (windBack) calendar.add(Calendar.MINUTE, -oneQuarterOrbitMin)
- var satPos = getSatPos(calendar.time)
-
- if (satPos.elevation > 0.0) {
- // move forward in 30 second intervals until the sat goes below the horizon
- do {
- calendar.add(Calendar.SECOND, 30)
- satPos = getSatPos(calendar.time)
- } while (satPos.elevation > 0.0)
- // move forward 3/4 of an orbit
- calendar.add(Calendar.MINUTE, oneQuarterOrbitMin * 3)
- }
-
- // find the next time sat comes above the horizon
- do {
- calendar.add(Calendar.SECOND, 60)
- satPos = getSatPos(calendar.time)
- elevation = satPos.elevation
- if (elevation > maxElevation) {
- maxElevation = elevation
- alt = satPos.altitude
- tcaAz = Math.toDegrees(satPos.azimuth)
- }
- } while (satPos.elevation < 0.0)
-
- // refine to 3 seconds
- calendar.add(Calendar.SECOND, -60)
- do {
- calendar.add(Calendar.SECOND, 3)
- satPos = getSatPos(calendar.time)
- elevation = satPos.elevation
- if (elevation > maxElevation) {
- maxElevation = elevation
- alt = satPos.altitude
- tcaAz = Math.toDegrees(satPos.azimuth)
- }
- } while (satPos.elevation < 0.0)
-
- val aos = satPos.time.time
- val aosAz = Math.toDegrees(satPos.azimuth)
-
- // find when sat goes below
- do {
- calendar.add(Calendar.SECOND, 30)
- satPos = getSatPos(calendar.time)
- elevation = satPos.elevation
- if (elevation > maxElevation) {
- maxElevation = elevation
- alt = satPos.altitude
- tcaAz = Math.toDegrees(satPos.azimuth)
- }
- } while (satPos.elevation > 0.0)
-
- // refine to 3 seconds
- calendar.add(Calendar.SECOND, -30)
- do {
- calendar.add(Calendar.SECOND, 3)
- satPos = getSatPos(calendar.time)
- elevation = satPos.elevation
- if (elevation > maxElevation) {
- maxElevation = elevation
- alt = satPos.altitude
- tcaAz = Math.toDegrees(satPos.azimuth)
- }
- } while (satPos.elevation > 0.0)
-
- val los = satPos.time.time
- val losAz = Math.toDegrees(satPos.azimuth)
- val tca = Date((aos + los) / 2).time
- val elev = Math.toDegrees(maxElevation)
- return SatPass(id, name, isDeep, aos, aosAz, los, losAz, tca, tcaAz, alt, elev, this)
-}
+//
+//const val speedOfLight = 2.99792458E8
+//const val earthRadiusKm = 6378.16
+//
+//fun Satellite.getQuarterOrbitMin(): Int {
+// return (24.0 * 60.0 / this.tle.meanmo / 4.0).toInt()
+//}
+//
+//fun SatPos.getRangeCircle(): List<Position> {
+// val positions = mutableListOf<Position>()
+// val lat = this.latitude
+// val lon = this.longitude
+// // rangeCircleRadiusKm
+// // earthRadiusKm * acos(earthRadiusKm / (earthRadiusKm + satPos.altitude))
+// val beta = acos(earthRadiusKm / (earthRadiusKm + this.altitude))
+// var tempAzimuth = 0
+// while (tempAzimuth < 360) {
+// val azimuth = tempAzimuth / 360.0 * 2.0 * Math.PI
+// var rangelat = asin(sin(lat) * cos(beta) + cos(azimuth) * sin(beta) * cos(lat))
+// val num = (cos(beta) - (sin(lat) * sin(rangelat)))
+// val den = cos(lat) * cos(rangelat)
+// var rangelon = if (tempAzimuth == 0 && (beta > ((Math.PI / 2.0) - lat))) {
+// lon + Math.PI
+// } else if (tempAzimuth == 180 && (beta > ((Math.PI / 2.0) - lat))) {
+// lon + Math.PI
+// } else if (abs(num / den) > 1.0) {
+// lon
+// } else {
+// if ((180 - tempAzimuth) >= 0) {
+// lon - acos(num / den)
+// } else {
+// lon + acos(num / den)
+// }
+// }
+// while (rangelon < 0.0) rangelon += Math.PI * 2.0
+// while (rangelon > Math.PI * 2.0) rangelon -= Math.PI * 2.0
+// rangelat = Math.toDegrees(rangelat)
+// rangelon = Math.toDegrees(rangelon)
+// positions.add(Position(rangelat, rangelon))
+// tempAzimuth += 1
+// }
+// return positions
+//}
+//
+//fun SatPos.getDownlinkFreq(freq: Long): Long {
+// return (freq.toDouble() * (speedOfLight - this.rangeRate * 1000.0) / speedOfLight).toLong()
+//}
+//
+//fun SatPos.getUplinkFreq(freq: Long): Long {
+// return (freq.toDouble() * (speedOfLight + this.rangeRate * 1000.0) / speedOfLight).toLong()
+//}
+//
+//fun Satellite.getSatPos(date: Date): SatPos {
+// return this.getPosition(stationPos, date)
+//}
+//
+//fun Satellite.getPositions(date: Date, stepSec: Int, minBefore: Int, orbits: Double): List<SatPos> {
+// val positions = mutableListOf<SatPos>()
+// val orbitalPeriod = 24 * 60 / this.tle.meanmo
+// val endDate = Date(date.time + (orbitalPeriod * orbits * 60L * 1000L).toLong())
+// val startDate = Date(date.time - minBefore * 60L * 1000L)
+// var currentDate = startDate
+// while (currentDate.before(endDate)) {
+// positions.add(getSatPos(currentDate))
+// currentDate = Date(currentDate.time + stepSec * 1000)
+// }
+// return positions
+//}
+//
+//fun Satellite.getPasses(refDate: Date, hoursAhead: Int, windBack: Boolean): List<SatPass> {
+// val passes = mutableListOf<SatPass>()
+// val oneQuarterOrbitMin = this.getQuarterOrbitMin()
+// val endDate = Date(refDate.time + hoursAhead * 60L * 60L * 1000L)
+// var startDate = refDate
+// var shouldWindBack = windBack
+// var lastAosDate: Date
+// var count = 0
+// if (this.willBeSeen(stationPos)) {
+// if (this.tle.isDeepspace) {
+// passes.add(nextDeepSpacePass(refDate))
+// } else {
+// do {
+// if (count > 0) shouldWindBack = false
+// val pass = nextNearEarthPass(startDate, shouldWindBack)
+// lastAosDate = pass.aosDate
+// passes.add(pass)
+// startDate =
+// Date(pass.losDate.time + (oneQuarterOrbitMin * 3) * 60L * 1000L)
+// count++
+// } while (lastAosDate < endDate)
+// }
+// }
+// return passes
+//}
+//
+//private fun Satellite.nextDeepSpacePass(refDate: Date): SatPass {
+// val satPos = getSatPos(refDate)
+// val id = this.tle.catnum
+// val name = this.tle.name
+// val isDeep = this.tle.isDeepspace
+// val aos = Date(refDate.time - 24 * 60L * 60L * 1000L).time
+// val los = Date(refDate.time + 24 * 60L * 60L * 1000L).time
+// val tca = Date((aos + los) / 2).time
+// val az = Math.toDegrees(satPos.azimuth)
+// val elev = Math.toDegrees(satPos.elevation)
+// val alt = satPos.altitude
+// return SatPass(id, name, isDeep, aos, az, los, az, tca, az, alt, elev, this)
+//}
+//
+//private fun Satellite.nextNearEarthPass(refDate: Date, windBack: Boolean = false): SatPass {
+// val oneQuarterOrbitMin = this.getQuarterOrbitMin()
+// val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC")).apply {
+// clear()
+// timeInMillis = refDate.time
+// }
+// val id = this.tle.catnum
+// val name = this.tle.name
+// val isDeep = this.tle.isDeepspace
+//
+// var elevation: Double
+// var maxElevation = 0.0
+// var alt = 0.0
+// var tcaAz = 0.0
+//
+// // wind back time 1/4 of an orbit
+// if (windBack) calendar.add(Calendar.MINUTE, -oneQuarterOrbitMin)
+// var satPos = getSatPos(calendar.time)
+//
+// if (satPos.elevation > 0.0) {
+// // move forward in 30 second intervals until the sat goes below the horizon
+// do {
+// calendar.add(Calendar.SECOND, 30)
+// satPos = getSatPos(calendar.time)
+// } while (satPos.elevation > 0.0)
+// // move forward 3/4 of an orbit
+// calendar.add(Calendar.MINUTE, oneQuarterOrbitMin * 3)
+// }
+//
+// // find the next time sat comes above the horizon
+// do {
+// calendar.add(Calendar.SECOND, 60)
+// satPos = getSatPos(calendar.time)
+// elevation = satPos.elevation
+// if (elevation > maxElevation) {
+// maxElevation = elevation
+// alt = satPos.altitude
+// tcaAz = Math.toDegrees(satPos.azimuth)
+// }
+// } while (satPos.elevation < 0.0)
+//
+// // refine to 3 seconds
+// calendar.add(Calendar.SECOND, -60)
+// do {
+// calendar.add(Calendar.SECOND, 3)
+// satPos = getSatPos(calendar.time)
+// elevation = satPos.elevation
+// if (elevation > maxElevation) {
+// maxElevation = elevation
+// alt = satPos.altitude
+// tcaAz = Math.toDegrees(satPos.azimuth)
+// }
+// } while (satPos.elevation < 0.0)
+//
+// val aos = satPos.time.time
+// val aosAz = Math.toDegrees(satPos.azimuth)
+//
+// // find when sat goes below
+// do {
+// calendar.add(Calendar.SECOND, 30)
+// satPos = getSatPos(calendar.time)
+// elevation = satPos.elevation
+// if (elevation > maxElevation) {
+// maxElevation = elevation
+// alt = satPos.altitude
+// tcaAz = Math.toDegrees(satPos.azimuth)
+// }
+// } while (satPos.elevation > 0.0)
+//
+// // refine to 3 seconds
+// calendar.add(Calendar.SECOND, -30)
+// do {
+// calendar.add(Calendar.SECOND, 3)
+// satPos = getSatPos(calendar.time)
+// elevation = satPos.elevation
+// if (elevation > maxElevation) {
+// maxElevation = elevation
+// alt = satPos.altitude
+// tcaAz = Math.toDegrees(satPos.azimuth)
+// }
+// } while (satPos.elevation > 0.0)
+//
+// val los = satPos.time.time
+// val losAz = Math.toDegrees(satPos.azimuth)
+// val tca = Date((aos + los) / 2).time
+// val elev = Math.toDegrees(maxElevation)
+// return SatPass(id, name, isDeep, aos, aosAz, los, losAz, tca, tcaAz, alt, elev, this)
+//}
diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/Satellite.kt b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/Satellite.kt
index f35931f1..afbecb80 100644
--- a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/Satellite.kt
+++ b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/Satellite.kt
@@ -17,7 +17,6 @@
*/
package com.rtbishop.look4sat.domain.predict4kotlin
-import java.io.InputStream
import java.util.*
import java.util.concurrent.atomic.AtomicReference
import kotlin.math.*
@@ -346,52 +345,4 @@ abstract class Satellite(val tle: TLE) {
gmst = modulus(gmst + secPerDay * earthRotPerSidDay * ut)
return twoPi * gmst / secPerDay
}
-
- companion object {
-
- fun createSat(tle: TLE?): Satellite? {
- return when {
- tle == null -> null
- tle.isDeepspace -> DeepSpaceSat(tle)
- else -> NearEarthSat(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) {
- currentTLE[line] = it
- line++
- } else {
- currentTLE[line] = it
- parseElement(currentTLE)?.let { tle -> importedTles.add(tle) }
- line = 0
- }
- }
- return importedTles
- }
-
- 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
- }
- }
- }
}
diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatelliteFactory.kt b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatelliteFactory.kt
deleted file mode 100644
index 09096c1b..00000000
--- a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/SatelliteFactory.kt
+++ /dev/null
@@ -1,66 +0,0 @@
-package com.rtbishop.look4sat.domain.predict4kotlin
-
-import java.io.InputStream
-import kotlin.math.pow
-
-object SatelliteFactory {
-
- fun createSat(tle: TLE?): Satellite? {
- return when {
- tle == null -> null
- tle.isDeepspace -> DeepSpaceSat(tle)
- else -> NearEarthSat(tle)
- }
- }
-
- fun createSat(array: Array<String>): Satellite? {
- val importedElement = importElement(array)
- return createSat(importedElement)
- }
-
- fun createDummySat(): Satellite? {
- val elementArray = arrayOf(
- "ISS (ZARYA)",
- "1 25544U 98067A 21242.56000419 .00070558 00000-0 12956-2 0 9996",
- "2 25544 51.6433 334.9559 0003020 334.9496 106.9882 15.48593918300128"
- )
- return createSat(importElement(elementArray))
- }
-
- fun importElement(array: Array<String>): TLE? {
- if (array.size != 3) return null
- try {
- val name: String = array[0].trim()
- val epoch: Double = array[1].substring(18, 32).toDouble()
- val meanmo: Double = array[2].substring(52, 63).toDouble()
- val eccn: Double = 1.0e-07 * array[2].substring(26, 33).toDouble()
- val incl: Double = array[2].substring(8, 16).toDouble()
- val raan: Double = array[2].substring(17, 25).toDouble()
- val argper: Double = array[2].substring(34, 42).toDouble()
- val meanan: Double = array[2].substring(43, 51).toDouble()
- val catnum: Int = array[1].substring(2, 7).trim().toInt()
- val bstar: Double = 1.0e-5 * array[1].substring(53, 59).toDouble() /
- 10.0.pow(array[1].substring(60, 61).toDouble())
- return TLE(name, epoch, meanmo, eccn, incl, raan, argper, meanan, catnum, bstar)
- } catch (exception: Exception) {
- return null
- }
- }
-
- fun importElements(stream: InputStream): List<TLE> {
- val elementArray = arrayOf(String(), String(), String())
- val importedElements = mutableListOf<TLE>()
- var line = 0
- stream.bufferedReader().forEachLine {
- if (line != 2) {
- elementArray[line] = it
- line++
- } else {
- elementArray[line] = it
- importElement(elementArray)?.let { tle -> importedElements.add(tle) }
- line = 0
- }
- }
- return importedElements
- }
-}
\ No newline at end of file
diff --git a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/TLE.kt b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/TLE.kt
index a3c5e5a8..cf480d7a 100644
--- a/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/TLE.kt
+++ b/core/src/main/java/com/rtbishop/look4sat/domain/predict4kotlin/TLE.kt
@@ -17,6 +17,9 @@
*/
package com.rtbishop.look4sat.domain.predict4kotlin
+import java.io.InputStream
+import kotlin.math.pow
+
data class TLE(
val name: String,
val epoch: Double,
@@ -35,4 +38,55 @@ data class TLE(
val xmo: Double = Math.toRadians(meanan)
val xno: Double = meanmo * Math.PI * 2.0 / 1440
val isDeepspace: Boolean = meanmo < 6.4
+
+ companion object {
+
+ fun createSat(array: Array<String>): Satellite? {
+ return importElement(array)?.createSat()
+ }
+
+ fun importElement(array: Array<String>): TLE? {
+ if (array.size != 3) return null
+ try {
+ val name: String = array[0].trim()
+ val epoch: Double = array[1].substring(18, 32).toDouble()
+ val meanmo: Double = array[2].substring(52, 63).toDouble()
+ val eccn: Double = 1.0e-07 * array[2].substring(26, 33).toDouble()
+ val incl: Double = array[2].substring(8, 16).toDouble()
+ val raan: Double = array[2].substring(17, 25).toDouble()
+ val argper: Double = array[2].substring(34, 42).toDouble()
+ val meanan: Double = array[2].substring(43, 51).toDouble()
+ val catnum: Int = array[1].substring(2, 7).trim().toInt()
+ val bstar: Double = 1.0e-5 * array[1].substring(53, 59).toDouble() /
+ 10.0.pow(array[1].substring(60, 61).toDouble())
+ return TLE(name, epoch, meanmo, eccn, incl, raan, argper, meanan, catnum, bstar)
+ } catch (exception: Exception) {
+ return null
+ }
+ }
+
+ fun importElements(stream: InputStream): List<TLE> {
+ val elementArray = arrayOf(String(), String(), String())
+ val importedElements = mutableListOf<TLE>()
+ var line = 0
+ stream.bufferedReader().forEachLine {
+ if (line != 2) {
+ elementArray[line] = it
+ line++
+ } else {
+ elementArray[line] = it
+ importElement(elementArray)?.let { tle -> importedElements.add(tle) }
+ line = 0
+ }
+ }
+ return importedElements
+ }
+ }
+}
+
+fun TLE.createSat(): Satellite {
+ return when {
+ this.isDeepspace -> DeepSpaceSat(this)
+ else -> NearEarthSat(this)
+ }
}
diff --git a/core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt b/core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt
new file mode 100644
index 00000000..1b73a052
--- /dev/null
+++ b/core/src/test/java/com/rtbishop/look4sat/Look4SatTest.kt
@@ -0,0 +1,26 @@
+package com.rtbishop.look4sat
+
+import com.rtbishop.look4sat.domain.predict4kotlin.TLE
+import org.junit.Test
+
+class Look4SatTest {
+
+ @Test
+ fun `Given correct TLE array returns Satellite`() {
+ val elementArray = arrayOf(
+ "ISS (ZARYA)",
+ "1 25544U 98067A 21242.56000419 .00070558 00000-0 12956-2 0 9996",
+ "2 25544 51.6433 334.9559 0003020 334.9496 106.9882 15.48593918300128"
+ )
+ assert(TLE.createSat(elementArray) != null)
+ }
+
+ @Test
+ fun `Given incorrect TLE array returns null`() {
+ val elementArray = arrayOf(
+ "1 25544U 98067A 21242.56000419 .00070558 00000-0 12956-2 0 9996",
+ "2 25544 51.6433 334.9559 0003020 334.9496 106.9882 15.48593918300128"
+ )
+ assert(TLE.createSat(elementArray) == null)
+ }
+}