diff --git a/sgp4/omm.py b/sgp4/omm.py index 88699ac..7380772 100644 --- a/sgp4/omm.py +++ b/sgp4/omm.py @@ -1,6 +1,7 @@ """Support for the new Orbit Mean-Elements Message format for TLE data.""" import csv +import json import xml.etree.ElementTree as ET from datetime import datetime from math import pi @@ -21,6 +22,13 @@ def parse_xml(file): fields.update((field.tag, field.text) for field in element) yield fields +def parse_json(file): + json_data = json.load(file) + fields = {} + for item in json_data: + fields.update((k, v) for k, v in item.items()) + yield fields + _epoch0 = datetime(1949, 12, 31) _to_radians = pi / 180.0 _ndot_units = 1036800.0 / pi # See SGP4.cpp for details. diff --git a/sgp4/tests.py b/sgp4/tests.py index 66e306a..0d5aadf 100644 --- a/sgp4/tests.py +++ b/sgp4/tests.py @@ -792,6 +792,30 @@ def format_long_line(satrec, tsince, mu, r, v): MARIO,1998-067UQ,2023-04-25T10:45:30.642912,15.99081912,.0014649,51.6242,216.2930,331.8976,28.1241,0,U,55123,999,1839,.1568E-2,.787702E-2,.29408E-3 """ +MARIO_JSON = """\ +[ + { + "OBJECT_NAME": "MARIO", + "OBJECT_ID": "1998-067UQ", + "EPOCH": "2023-04-25T10:45:30.642912", + "MEAN_MOTION": 15.99081912, + "ECCENTRICITY": 0.0014649, + "INCLINATION": 51.6242, + "RA_OF_ASC_NODE": 216.2930, + "ARG_OF_PERICENTER": 331.8976, + "MEAN_ANOMALY": 28.1241, + "EPHEMERIS_TYPE": 0, + "CLASSIFICATION_TYPE": "U", + "NORAD_CAT_ID": 55123, + "ELEMENT_SET_NO": 999, + "REV_AT_EPOCH": 1839, + "BSTAR": 0.001568, + "MEAN_MOTION_DOT": 0.00787702, + "MEAN_MOTION_DDOT": 0.00029408 + } +] +""" + def test_omm_xml_matches_old_tle(): line0, line1, line2 = MARIO_TLE.splitlines() sat1 = Satrec.twoline2rv(line1, line2) @@ -812,6 +836,16 @@ def test_omm_csv_matches_old_tle(): assert_satellites_match(sat1, sat2) +def test_omm_json_matches_old_tle(): + line0, line1, line2 = MARIO_TLE.splitlines() + sat1 = Satrec.twoline2rv(line1, line2) + + fields = next(omm.parse_json(StringIO(MARIO_JSON))) + sat2 = Satrec() + omm.initialize(sat2, fields) + + assert_satellites_match(sat1, sat2) + def assert_satellites_match(sat1, sat2): for attr in dir(sat1): if attr.startswith('_'):