Source code for mds_api.mds_io
""" I/O, as in "import-export" for MDS"""
import json
import math
import re
import time
import numpy as np
from mds_api import client
from mds_api.NetworkConfiguration import NetworkConfiguration
from mds_api.mds_utils import *
[docs]
def load_tle_from_file(filename: str, desired_spacecraft: list | None = None):
""" Load Two-Line Elements. Pre-computes orbital ephemeris with the SGP4 propagator and adds a spacecraft to
follow that trajectory. Can be a single TLE or multiple TLEs.
Args:
tle (str): Two-Line Elements string. May contain one or multiple TLEs.
desired_spacecraft (list): A list of spacecraft that you want to load from the file. In None or empty,
then all spacecraft in the file will be loaded. Otherwise, only spacecraft with names
in the list will be loaded. Default: None.
"""
with open(filename, "r") as file:
tle = file.read()
return load_tle(tle, desired_spacecraft)
def load_tle(tle: str, desired_spacecraft: list | None = None):
""" Load Two-Line Elements. Pre-computes orbital ephemeris with the SGP4 propagator and adds a spacecraft to
follow that trajectory. Can be a single TLE or multiple TLEs.
Args:
tle (str): Two-Line Elements string. May contain one or multiple TLEs.
desired_spacecraft (list): A list of spacecraft that you want to load from the file. In None or empty,
then all spacecraft in the file will be loaded. Otherwise, only spacecraft with names
in the list will be loaded. Default: None.
"""
json_dict = {
"tle":tle,
"list":desired_spacecraft
}
message = json.dumps(json_dict)
response = client.load_orbit_from_tle(message, NetworkConfiguration.MDS_HOST, NetworkConfiguration.MDS_HOST_PORT)
return response
# TODO: Include also an option of None
[docs]
def get_tle(sat_names: str | list, max_iterations:int = 35, max_days_back:int = 5) -> bytes:
""" Get Two-Line Elements. Computes a Two-Line Elements string from the Cartesian state of the spacecraft.
Args:
sat_name (str): Name of the spacecraft or a list of names.
"""
names = [sat_names] if isinstance(sat_names, str) else sat_names
json_dict = {
"list":names,
"max_iterations":max_iterations,
"max_back_days":max_days_back,
}
response = client.get_tle(json.dumps(json_dict), NetworkConfiguration.MDS_HOST, NetworkConfiguration.MDS_HOST_PORT)
return response
[docs]
def save_OMM(sat_names: str | list | None, max_iterations = 35, num_days_back = 5):
""" Retrieve OMM file of all satellites as string."""
names = [sat_names] if isinstance(sat_names, str) else sat_names
json_dict = {
"list": names,
"iterations": max_iterations,
"days_back": num_days_back
}
response = client.save_OMM(json.dumps(json_dict), NetworkConfiguration.MDS_HOST, NetworkConfiguration.MDS_HOST_PORT)
return response
[docs]
def load_OMM(xml_path: str, desired_spacecraft: list | None = None):
""" Load an OMM file from a specified path
Args:
xml_path: filename to OMM.
"""
with open(xml_path, "r") as f:
content = f.readlines()
json_dict = {
"omm":content,
"list":desired_spacecraft
}
response = client.load_OMM(NetworkConfiguration.MDS_HOST, NetworkConfiguration.MDS_HOST_PORT, json.dumps(json_dict))
return response
def load_OMM_from_data(xml_data: str, desired_spacecraft: list | None = None):
""" Load an OMM from a dirctly provided data
Args:
xml_data: xml file data.
"""
json_dict = {
"omm":xml_data,
"list":desired_spacecraft
}
response = client.load_OMM(NetworkConfiguration.MDS_HOST, NetworkConfiguration.MDS_HOST_PORT, json.dumps(json_dict))
return response