From 332b6f666c48a4ac2c902cacfe374dd9168ac151 Mon Sep 17 00:00:00 2001 From: Arty Bishop Date: Sun, 21 Mar 2021 19:48:20 +0000 Subject: [PATCH] Added several extension functions --- .../rtbishop/look4sat/utility/Extensions.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/src/main/java/com/rtbishop/look4sat/utility/Extensions.kt b/app/src/main/java/com/rtbishop/look4sat/utility/Extensions.kt index 4bbdbaf6..cdd2c025 100644 --- a/app/src/main/java/com/rtbishop/look4sat/utility/Extensions.kt +++ b/app/src/main/java/com/rtbishop/look4sat/utility/Extensions.kt @@ -38,6 +38,10 @@ import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import java.util.concurrent.TimeUnit +import kotlin.math.max +import kotlin.math.min +import kotlin.math.pow +import kotlin.math.sqrt fun Long.formatForTimer(): String { val format = "%02d:%02d:%02d" @@ -47,12 +51,37 @@ fun Long.formatForTimer(): String { return String.format(format, hours, minutes, seconds) } +fun Long.getDopplerFreq(rangeRate: Double, isDownlink: Boolean): Long { + val speedOfLight = 2.99792458E8 + return if (isDownlink) { + (this.toDouble() * (speedOfLight - rangeRate * 1000.0) / speedOfLight).toLong() + } else { + (this.toDouble() * (speedOfLight + rangeRate * 1000.0) / speedOfLight).toLong() + } +} + fun Double.round(decimals: Int): Double { var multiplier = 1.0 repeat(decimals) { multiplier *= 10 } return kotlin.math.round(this * multiplier) / multiplier } +fun Double.toOsmLat(): Double { + return min(max(this, -85.0), 85.0) +} + +fun Double.toOsmLon(): Double { + val newLon = if (this < -180.0) this + 360.0 else if (this > 180.0) this - 360.0 else this + return min(max(newLon, -180.0), 180.0) +} + +fun Double.getOrbitalVelocity(): Double { + val earthG = 6.674 * 10.0.pow(-11) + val earthM = 5.98 * 10.0.pow(24) + val radius = 6.37 * 10.0.pow(6) + this * 10.0.pow(3) + return sqrt(earthG * earthM / radius) / 1000 +} + fun Satellite.getPredictor(stationPosition: GroundStationPosition): PassPredictor { return PassPredictor(this, stationPosition) }