From aa090555b6dc4f85d5f1f0bb24dd0aea18c9c215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Deuchnord?= Date: Wed, 22 Apr 2020 14:02:14 +0200 Subject: [PATCH] feat: add solar eclipse --- kosmorrolib/data.py | 3 ++- kosmorrolib/events.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/kosmorrolib/data.py b/kosmorrolib/data.py index 6a66157..3c6c164 100644 --- a/kosmorrolib/data.py +++ b/kosmorrolib/data.py @@ -43,7 +43,8 @@ EVENTS = { 'OPPOSITION': {'message': _('%s is in opposition')}, 'CONJUNCTION': {'message': _('%s and %s are in conjunction')}, 'OCCULTATION': {'message': _('%s occults %s')}, - 'MAXIMAL_ELONGATION': {'message': _("%s's largest elongation")} + 'MAXIMAL_ELONGATION': {'message': _("%s's largest elongation")}, + 'PARTIAL_SOLAR_ECLIPSE': {'message': _('Partial solar eclipse')}, } diff --git a/kosmorrolib/events.py b/kosmorrolib/events.py index 791fef5..c1f1165 100644 --- a/kosmorrolib/events.py +++ b/kosmorrolib/events.py @@ -22,7 +22,7 @@ from skyfield.timelib import Time from skyfield.searchlib import find_discrete, find_maxima from numpy import pi -from .data import Event, Star, Planet, ASTERS +from .data import Event, Object, Star, Planet, ASTERS from .core import get_timescale, get_skf_objects, flatten_list @@ -74,6 +74,37 @@ def _search_conjunction(start_time: Time, end_time: Time) -> [Event]: return conjunctions +def _search_solar_eclipse(start_time: Time, end_time: Time): + earth = get_skf_objects()['earth'] + sun = ASTERS[0] + moon = ASTERS[1] + + def is_eclipsing(time: Time): + aster1_pos = (moon.get_skyfield_object() - earth).at(time) + aster2_pos = (sun.get_skyfield_object() - earth).at(time) + distance = aster1_pos.separation_from(aster2_pos).degrees + + return distance - moon.get_apparent_radius(time, earth) < sun.get_apparent_radius(time, earth) + + is_eclipsing.rough_period = 60.0 + + times, val = find_discrete(start_time, end_time, is_eclipsing) + # moon_pos = (moon.get_skyfield_object() - earth).at(time) + # sun_pos = (sun.get_skyfield_object() - earth).at(time) + # distance = moon_pos.separation_from(sun_pos).degrees + + print(times) + print(val) + + start = times[0] if val[0] else val[1] + end = times[1] if not val[1] else val[0] + + + # if distance != 0: + return Event('PARTIAL_SOLAR_ECLIPSE', [moon, sun], + start.utc_datetime(), end.utc_datetime()) + + def _search_oppositions(start_time: Time, end_time: Time) -> [Event]: earth = get_skf_objects()['earth'] sun = get_skf_objects()['sun'] @@ -137,5 +168,6 @@ def search_events(date: date_type) -> [Event]: return sorted(flatten_list([ _search_oppositions(start_time, end_time), _search_conjunction(start_time, end_time), - _search_maximal_elongations(start_time, end_time) + _search_maximal_elongations(start_time, end_time), + _search_solar_eclipse(start_time, end_time) ]), key=lambda event: event.start_time)