Browse Source

feat(ephemerides): compute ephemerides if the position is set only

BREAKING CHANGE: invoking kosmorro command without --latitude and
--longitude arguments will now only output the Moon phase and,
eventually, the events for the given date (or today if date not given).
tags/v0.4.0
Jérôme Deuchnord 4 years ago
parent
commit
467c8227df
No known key found for this signature in database GPG Key ID: BC6F3C345B7D33B0
4 changed files with 42 additions and 16 deletions
  1. +13
    -7
      kosmorro
  2. +7
    -2
      kosmorrolib/dumper.py
  3. +5
    -3
      kosmorrolib/ephemerides.py
  4. +17
    -4
      test/dumper.py

+ 13
- 7
kosmorro View File

@@ -43,7 +43,12 @@ def main():
if day is not None and month is None:
month = date.today().month

ephemeris = EphemeridesComputer(Position(args.latitude, args.longitude))
if args.latitude is None or args.longitude is None:
position = None
else:
position = Position(args.latitude, args.longitude)

ephemeris = EphemeridesComputer(position)
ephemerides = ephemeris.compute_ephemerides(year, month, day)

events_list = events.search_events(compute_date)
@@ -86,10 +91,11 @@ def clear_cache() -> bool:
def get_args(output_formats: [str]):
today = date.today()

parser = argparse.ArgumentParser(description='Compute the ephemerides for a given date, at a given position'
' on Earth.',
epilog='By default, the ephemerides will be computed for today (%s) for an'
' observer positioned at coordinates (0,0).'
parser = argparse.ArgumentParser(description='Compute the ephemerides and the events for a given date,'
' at a given position on Earth.',
epilog='By default, only the events will be computed for today (%s).\n'
'To compute also the ephemerides, latitude and longitude arguments'
' are needed.'
% today.strftime('%a %b %d, %Y'))

parser.add_argument('--version', '-v', dest='special_action', action='store_const', const=output_version,
@@ -98,9 +104,9 @@ def get_args(output_formats: [str]):
help='Delete all the files Kosmorro stored in the cache.')
parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
help='The format under which the information have to be output')
parser.add_argument('--latitude', '-lat', type=float, default=0.,
parser.add_argument('--latitude', '-lat', type=float, default=None,
help="The observer's latitude on Earth")
parser.add_argument('--longitude', '-lon', type=float, default=0.,
parser.add_argument('--longitude', '-lon', type=float, default=None,
help="The observer's longitude on Earth")
parser.add_argument('--day', '-d', type=int, default=today.day,
help='A number between 1 and 28, 29, 30 or 31 (depending on the month). The day you want to '


+ 7
- 2
kosmorrolib/dumper.py View File

@@ -72,9 +72,14 @@ class JsonDumper(Dumper):

class TextDumper(Dumper):
def to_string(self):
text = 'Ephemerides of %s' % self.date.strftime('%A %B %d, %Y')
text = self.date.strftime('%A %B %d, %Y')

if len(self.ephemeris['details']) > 0:
text = '\n\n'.join([text,
self.get_asters(self.ephemeris['details'])
])

text = '\n\n'.join([text,
self.get_asters(self.ephemeris['details']),
self.get_moon(self.ephemeris['moon_phase'])
])



+ 5
- 3
kosmorrolib/ephemerides.py View File

@@ -17,6 +17,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import datetime
from typing import Union

from skyfield import almanac
from skyfield.timelib import Time
@@ -29,8 +30,9 @@ RISEN_ANGLE = -0.8333


class EphemeridesComputer:
def __init__(self, position: Position):
position.observation_planet = get_skf_objects()['earth']
def __init__(self, position: Union[Position, None]):
if position is not None:
position.observation_planet = get_skf_objects()['earth']
self.position = position

def get_sun(self, start_time, end_time) -> dict:
@@ -107,7 +109,7 @@ class EphemeridesComputer:
def compute_ephemerides_for_day(self, year: int, month: int, day: int) -> dict:
return {'moon_phase': self.get_moon_phase(year, month, day),
'details': [self.get_asters_ephemerides_for_aster(aster, datetime.date(year, month, day), self.position)
for aster in ASTERS]}
for aster in ASTERS] if self.position is not None else []}

def compute_ephemerides_for_month(self, year: int, month: int) -> [dict]:
if month == 2:


+ 17
- 4
test/dumper.py View File

@@ -43,7 +43,7 @@ class DumperTestCase(unittest.TestCase):

def test_text_dumper_without_events(self):
ephemerides = self._get_data()
self.assertEqual('Ephemerides of Monday October 14, 2019\n\n'
self.assertEqual('Monday October 14, 2019\n\n'
'Object Rise time Culmination time Set time\n'
'-------- ----------- ------------------ ----------\n'
'Mars - - -\n\n'
@@ -54,7 +54,7 @@ class DumperTestCase(unittest.TestCase):

def test_text_dumper_with_events(self):
ephemerides = self._get_data()
self.assertEqual('Ephemerides of Monday October 14, 2019\n\n'
self.assertEqual('Monday October 14, 2019\n\n'
'Object Rise time Culmination time Set time\n'
'-------- ----------- ------------------ ----------\n'
'Mars - - -\n\n'
@@ -68,11 +68,24 @@ class DumperTestCase(unittest.TestCase):
get_timescale().utc(2018, 7, 27, 5, 12))
], date=date(2019, 10, 14)).to_string())

def test_text_dumper_without_ephemerides_and_with_events(self):
ephemerides = self._get_data(False)
self.assertEqual('Monday October 14, 2019\n\n'
'Moon phase: Full Moon\n'
'Last Quarter on Mon Oct 21, 2019 00:00\n\n'
'Expected events:\n\n'
'05:12 Mars is in opposition\n\n'
'Note: All the hours are given in UTC.',
TextDumper(ephemerides, [Event('OPPOSITION',
Planet('Mars', 'MARS'),
get_timescale().utc(2018, 7, 27, 5, 12))
], date=date(2019, 10, 14)).to_string())

@staticmethod
def _get_data():
def _get_data(has_ephemerides: bool = True):
return {
'moon_phase': MoonPhase('FULL_MOON', get_timescale().utc(2019, 10, 14), get_timescale().utc(2019, 10, 21)),
'details': [Planet('Mars', 'MARS', AsterEphemerides(None, None, None))]
'details': [Planet('Mars', 'MARS', AsterEphemerides(None, None, None))] if has_ephemerides else []
}




Loading…
Cancel
Save