Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(events): add support for opposition events
- Loading branch information
Jérôme Deuchnord
committed
Dec 5, 2019
1 parent
0786a42
commit fa2da9e
Showing
8 changed files
with
204 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Kosmorro - Compute The Next Ephemerides | ||
# Copyright (C) 2019 Jérôme Deuchnord <jerome@deuchnord.fr> | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
from datetime import date as date_type | ||
|
||
from skyfield.timelib import Time | ||
from skyfield.almanac import find_discrete | ||
|
||
from .data import Event, Planet | ||
from .core import get_timescale, get_skf_objects, ASTERS | ||
|
||
|
||
def _search_oppositions(start_time: Time, end_time: Time) -> [Event]: | ||
earth = get_skf_objects()['earth'] | ||
sun = get_skf_objects()['sun'] | ||
aster = None | ||
|
||
def is_oppositing(time: Time) -> [bool]: | ||
earth_pos = earth.at(time) | ||
sun_pos = earth_pos.observe(sun).apparent() # Never do this without eyes protection! | ||
aster_pos = earth_pos.observe(get_skf_objects()[aster.skyfield_name]).apparent() | ||
_, lon1, _ = sun_pos.ecliptic_latlon() | ||
_, lon2, _ = aster_pos.ecliptic_latlon() | ||
return (lon1.degrees - lon2.degrees) > 180 | ||
|
||
is_oppositing.rough_period = 1.0 | ||
events = [] | ||
|
||
for aster in ASTERS: | ||
if not isinstance(aster, Planet) or aster.name in ['Mercury', 'Venus']: | ||
continue | ||
|
||
times, _ = find_discrete(start_time, end_time, is_oppositing) | ||
for time in times: | ||
events.append(Event('OPPOSITION', aster, time)) | ||
|
||
return events | ||
|
||
|
||
def search_events(date: date_type) -> [Event]: | ||
start_time = get_timescale().utc(date.year, date.month, date.day) | ||
end_time = get_timescale().utc(date.year, date.month, date.day + 1) | ||
|
||
return [ | ||
opposition for opposition in _search_oppositions(start_time, end_time) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .dumper import * | ||
from .ephemerides import * | ||
from .events import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import unittest | ||
|
||
from datetime import date | ||
|
||
from kosmorrolib import events | ||
from kosmorrolib.data import Event | ||
from kosmorrolib.core import get_timescale | ||
|
||
|
||
class MyTestCase(unittest.TestCase): | ||
def test_event_only_accepts_valid_values(self): | ||
with self.assertRaises(ValueError): | ||
Event('SUPERNOVA', None, get_timescale().now()) | ||
|
||
def test_find_oppositions(self): | ||
# Test case: Mars opposition | ||
# Source of the information: https://promenade.imcce.fr/en/pages6/887.html#mar | ||
o1 = (events.search_events(date(2020, 10, 13)), '^2020-10-13T23:25') | ||
o2 = (events.search_events(date(2022, 12, 8)), '^2022-12-08T05:41') | ||
o3 = (events.search_events(date(2025, 1, 16)), '^2025-01-16T02:38') | ||
o4 = (events.search_events(date(2027, 2, 19)), '^2027-02-19T15:50') | ||
|
||
for (o, expected_date) in [o1, o2, o3, o4]: | ||
self.assertEqual(1, len(o), 'Expected 1 event for %s, got %d' % (expected_date, len(o))) | ||
self.assertEqual('OPPOSITION', o[0].event_type) | ||
self.assertEqual('MARS', o[0].object.skyfield_name) | ||
self.assertRegex(o[0].start_time.utc_iso(), expected_date) | ||
self.assertIsNone(o[0].end_time) | ||
self.assertEqual('Mars is in opposition', o[0].get_description()) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |