diff options
| author | Yuval Adam <_@yuv.al> | 2020-09-24 21:39:17 +0300 |
|---|---|---|
| committer | Yuval Adam <_@yuv.al> | 2020-09-24 21:39:17 +0300 |
| commit | df526f8cf7fa4233e97b925b40daf51d4924cb18 (patch) | |
| tree | 1a5b4db9d01e22ced02f1b3c9c9aa5f45e04ddb8 | |
| parent | 95fe0bb114dd02d62f83556bcccaff70c9b605b2 (diff) | |
Implement get_predictions()
| -rw-r--r-- | iss/predictions.py | 28 | ||||
| -rw-r--r-- | iss/tests/test_predictions.py | 28 | ||||
| -rw-r--r-- | iss/utils.py | 3 |
3 files changed, 48 insertions, 11 deletions
diff --git a/iss/predictions.py b/iss/predictions.py index 7febe62..3c05ef2 100644 --- a/iss/predictions.py +++ b/iss/predictions.py @@ -1,24 +1,30 @@ from skyfield.api import Topos, load +from .utils import chunks + class Predictions(object): ISS = "ISS (ZARYA)" - def __init__(self, lat, lng, altitude=30.0, tz="UTC", satellite=ISS): + def __init__( + self, lat, lng, altitude=30.0, tz="UTC", satellite=ISS, start=None, days=10 + ): self.lat = lat self.lng = lng self.altitude = altitude self.tz = tz self.satellite = satellite + self.start = start + self.days = days def init_stations(self): stations_url = "http://celestrak.com/NORAD/elements/stations.txt" return load.tle_file(stations_url) - def get_next_days(self, days=10): + def get_next_days(self): ts = load.timescale() - t0 = ts.now() - t1 = ts.ut1_jd(t0.ut1 + days) + t0 = ts.now() if not self.start else ts.ut1_jd(self.start) + t1 = ts.ut1_jd(t0.ut1 + self.days) return t0, t1 def get_location(self): @@ -31,13 +37,17 @@ class Predictions(object): def get_predictions(self): satellite = self.get_satellite() - t0, t1 = self.get_next_days(days=10) + t0, t1 = self.get_next_days() location = self.get_location() - t, events = satellite.find_events( + ts, _events = satellite.find_events( location, t0, t1, altitude_degrees=self.altitude ) - for ti, event in zip(t, events): - name = ("rise above 30°", "culminate", "set below 30°")[event] - print(ti.utc_strftime("%Y %b %d %H:%M:%S"), name) + # events are returned as 3-tuples of (rise, culminate, set) + # where rise/set are relative to given altitude + # docs mention the possibilibity of several culminations + # https://rhodesmill.org/skyfield/earth-satellites.html#finding-when-a-satellite-rises-and-sets + # but this doesn't seem to happen in our case + preds = chunks(ts, 3) + return [[t.utc_iso() for t in p] for p in preds] diff --git a/iss/tests/test_predictions.py b/iss/tests/test_predictions.py index b809885..27fc667 100644 --- a/iss/tests/test_predictions.py +++ b/iss/tests/test_predictions.py @@ -4,16 +4,40 @@ from ..predictions import Predictions def test_get_timescales(): - p = Predictions(lat=32.0853, lng=34.7817, tz="Asia/Jerusalem") days = 5 - t0, t1 = p.get_next_days(days=days) + p = Predictions(lat=32.0853, lng=34.7817, days=days) + t0, t1 = p.get_next_days() delta = t1.ut1 - t0.ut1 assert delta == days +def test_custom_start(): + start = 2459117.245895914 + p = Predictions(lat=32.0853, lng=34.7817, start=start) + t0, _t1 = p.get_next_days() + assert t0.ut1 == start + + def test_get_location(): p = Predictions(lat=32.0853, lng=34.7817) tlv = Topos("32.0853 N", "34.7817 E") topos = p.get_location() assert topos.latitude.degrees == tlv.latitude.degrees assert topos.longitude.degrees == tlv.longitude.degrees + + +def test_get_predictions(): + start = 2459117.245895914 + + p = Predictions(lat=32.0853, lng=34.7817, tz="Asia/Jerusalem", start=start) + preds = p.get_predictions() + assert len(preds) == 17 + assert preds[0] == [ + "2020-09-24T21:31:09Z", + "2020-09-24T21:32:35Z", + "2020-09-24T21:34:02Z", + ] + + p = Predictions(lat=32.0853, lng=34.7817, tz="Asia/Jerusalem", start=start, days=5) + preds = p.get_predictions() + assert len(preds) == 8 diff --git a/iss/utils.py b/iss/utils.py new file mode 100644 index 0000000..f1a75c0 --- /dev/null +++ b/iss/utils.py @@ -0,0 +1,3 @@ +def chunks(l, n): + for i in range(0, len(l), n): + yield l[i : i + n] |
