Skip to content

Commit

Permalink
Add NORAD as a TLE source.
Browse files Browse the repository at this point in the history
This new source allows to use a file with multiple entries as an input
to orbit_predictor.
  • Loading branch information
inakimalerba committed Nov 28, 2017
1 parent dd7ff7b commit 5d410cc
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions orbit_predictor/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,36 @@ def _fetch_tle(self, path, sate_id, date=None):
return lines
else:
raise ValueError("Error requesting TLE: %s", response.text)

class NoradTLESource(TLESource):
'''
This source is intended to be used with norad-like multi-line files
eg. https://www.celestrak.com/NORAD/elements/resource.txt
'''
def __init__(self, content):
self.content = content

@classmethod
def from_url(cls, url):
headers = {'user-agent': 'orbit-predictor', 'Accept': 'text/plain'}
try:
response = requests.get(url, headers=headers)
except requests.exceptions.RequestException as error:
logger.error("Exception requesting TLE: %s", error)
raise
lines = response.content.decode("UTF-8").splitlines()
return cls(lines)

@classmethod
def from_file(cls, filename):
with open(filename, 'r') as f:
lines = f.read().splitlines()
return cls(lines)

def _get_tle(self, sate_id, date):
content = iter(self.content)
for sate, line_1, line_2 in zip(content, content, content):
if sate_id in sate:
return tuple([line_1, line_2])

raise LookupError("Couldn't find it. Wrong file?")

0 comments on commit 5d410cc

Please # to comment.