|
| 1 | +from dataclasses import dataclass |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from sgp4.api import SGP4_ERRORS, Satrec, jday |
| 5 | +from sgp4.api import WGS72 as GRAVITY_MODEL |
| 6 | +from sgp4.api import accelerated |
| 7 | +from loguru import logger |
| 8 | +from math import radians |
| 9 | + |
| 10 | +from gs.backend.positioning.tle import AFSPC_MODE_IMPROVED, TLEData |
| 11 | + |
| 12 | +#log whether accelerated mode is used |
| 13 | +if not accelerated: |
| 14 | + logger.warning( |
| 15 | + "SGP4 accelerated mode is not available. " |
| 16 | + "Falling back to pure Python implementation (slower)." |
| 17 | + ) |
| 18 | +else: |
| 19 | + logger.info("SGP4 accelerated mode is enabled.") |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class SGP4Data: |
| 24 | + """Data structure representing the satellite's position and velocity.""" |
| 25 | + |
| 26 | + position_km: tuple[float, float, float] |
| 27 | + velocity_km_sec: tuple[float, float, float] |
| 28 | + |
| 29 | + |
| 30 | +def setup_sgp4(tle: TLEData) -> Satrec: |
| 31 | + """ |
| 32 | + Initialize the SGP4 satellite model using TLE data. Formatting and SGP4 initialization pulled from link below |
| 33 | + https://pypi.org/project/sgp4/ |
| 34 | + """ |
| 35 | + |
| 36 | + |
| 37 | + sat = Satrec() |
| 38 | + |
| 39 | + |
| 40 | + sat.sgp4init( #causes error when tle.eccentricity is low while tle.drag_term is high) |
| 41 | + GRAVITY_MODEL, # gravity model |
| 42 | + AFSPC_MODE_IMPROVED, # propagation mode |
| 43 | + tle.satellite_number, # satellite number |
| 44 | + tle.convert_epoch_values_to_jd(), # epoch (Julian date) |
| 45 | + tle.drag_term, # BSTAR drag term |
| 46 | + #6.2485e-05, |
| 47 | + tle.first_derivative_mean_motion, # first time derivative of mean motion |
| 48 | + tle.second_derivative_mean_motion, # second time derivative of mean motion |
| 49 | + tle.eccentricity, # eccentricity |
| 50 | + radians(tle.argument_of_perigee), # argument of perigee (radians) |
| 51 | + radians(tle.inclination), # inclination (radians) |
| 52 | + radians(tle.mean_anomaly), # mean anomaly (radians) |
| 53 | + tle.mean_motion * (2 * 3.141592653589793 / 1440.0), # mean motion (radians/min) |
| 54 | + radians(tle.right_ascension), # RA of ascending node (radians) |
| 55 | + ) |
| 56 | + """ |
| 57 | + sat.sgp4init( |
| 58 | + GRAVITY_MODEL, # gravity model |
| 59 | + 'i', # 'a' = old AFSPC mode, 'i' = improved mode |
| 60 | + 25544, # satnum: Satellite number |
| 61 | + 25545.69339541, # epoch: days since 1949 December 31 00:00 UT |
| 62 | + 3.8792e-05, # bstar: drag coefficient (1/earth radii) |
| 63 | + 0.0, # ndot: first time derivative of mean motion (radians/min^2) |
| 64 | + 0.0, # nddot: second derivative of mean motion (radians/min^3) |
| 65 | + 0.0007417, # ecco: eccentricity (0..1) |
| 66 | + 0.3083420829620822, # argpo: argument of perigee (radians) |
| 67 | + 0.9013560935706996, # inclo: inclination (radians) |
| 68 | + 1.4946964807494398, # mo: mean anomaly (radians) |
| 69 | + 0.06763602333248933, # no_kozai: mean motion (radians/min) |
| 70 | + 3.686137125541276, # nodeo: R.A. of ascending node (radians) |
| 71 | + ) |
| 72 | + """ |
| 73 | + print("GRAVITY_MODEL:", GRAVITY_MODEL) |
| 74 | + print("Propagation mode:", AFSPC_MODE_IMPROVED) |
| 75 | + print("Satellite number:", tle.satellite_number) |
| 76 | + print("Epoch (Julian date):", tle.convert_epoch_values_to_jd()) |
| 77 | + print("BSTAR drag term:", tle.drag_term) |
| 78 | + print("First derivative of mean motion:", tle.first_derivative_mean_motion) |
| 79 | + print("Second derivative of mean motion:", tle.second_derivative_mean_motion) |
| 80 | + print("Eccentricity:", tle.eccentricity) |
| 81 | + print("Argument of perigee (rad):", radians(tle.argument_of_perigee)) |
| 82 | + print("Inclination (rad):", radians(tle.inclination)) |
| 83 | + print("Mean anomaly (rad):", radians(tle.mean_anomaly)) |
| 84 | + print("Mean motion (rad/min):", tle.mean_motion * (2 * 3.141592653589793 / 1440.0)) |
| 85 | + print("RA of ascending node (rad):", radians(tle.right_ascension)) |
| 86 | + return sat |
| 87 | + |
| 88 | + |
| 89 | +def get_sat_position(tle: TLEData, dt: datetime) -> SGP4Data: |
| 90 | + """ |
| 91 | + Compute the satellite's position and velocity at a given time. |
| 92 | +
|
| 93 | + Arguments are |
| 94 | + tle(TLEData): Two-line element set representing the satellite. |
| 95 | + dt(datetime): The timestamp for which to calculate the position. |
| 96 | +
|
| 97 | + """ |
| 98 | + sat = setup_sgp4(tle) |
| 99 | + jd, fr = jday(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second) |
| 100 | + error_code, position, velocity = sat.sgp4(jd, fr) |
| 101 | + |
| 102 | + #print("eccentricity (parsed):", tle.eccentricity) |
| 103 | + |
| 104 | + |
| 105 | + if 0.0 <= tle.eccentricity and tle.eccentricity <= 1.0: |
| 106 | + print("tle.eccentricity within expected bounds") |
| 107 | + |
| 108 | + error_message = SGP4_ERRORS.get(error_code, None) |
| 109 | + if error_message is not None: |
| 110 | + raise RuntimeError(error_message) |
| 111 | + |
| 112 | + return SGP4Data( |
| 113 | + position_km=tuple(position), |
| 114 | + velocity_km_sec=tuple(velocity), |
| 115 | + ) |
0 commit comments