From 7e9fb508a9fedc2c19ae0d96a7d063750cea56df Mon Sep 17 00:00:00 2001 From: Richard Date: Sat, 20 Jan 2024 16:45:15 -0800 Subject: [PATCH] Delay planet load until GPS lock --- python/PiFinder/calc_utils.py | 5 +++-- python/PiFinder/catalogs.py | 19 +++++++++++-------- python/PiFinder/main.py | 1 + python/PiFinder/ui/catalog.py | 21 ++++++++++++++++++++- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/python/PiFinder/calc_utils.py b/python/PiFinder/calc_utils.py index c6d8c48a..d769b412 100644 --- a/python/PiFinder/calc_utils.py +++ b/python/PiFinder/calc_utils.py @@ -10,6 +10,7 @@ load_constellation_map, ) from skyfield.magnitudelib import planetary_magnitude +from skyfield import almanac import PiFinder.utils as utils import json import hashlib @@ -200,14 +201,14 @@ def radec_to_constellation(self, ra, dec): sky_pos = position_of_radec(Angle(degrees=ra)._hours, dec) return self.constellation_map(sky_pos) - def calc_planets(self): + def calc_planets(self, dt): """Returns dictionary with all planet positions: {'SUN': {'radec': (279.05819685702846, -23.176809282384962), 'radec_pretty': ((18.0, 36.0, 14), (-23, 10, 36.51)), 'altaz': (1.667930045300066, 228.61434416619613)}, } """ - t = self.ts.now() + t = self.ts.from_datetime(dt) observer = self.observer_loc.at(t) planet_dict = {} for name, planet in zip(self.planet_names, self.planets): diff --git a/python/PiFinder/catalogs.py b/python/PiFinder/catalogs.py index 38e8d2a5..1b1d1eb8 100644 --- a/python/PiFinder/catalogs.py +++ b/python/PiFinder/catalogs.py @@ -1,5 +1,6 @@ import logging import time +import datetime from typing import List, Dict, DefaultDict, Optional import numpy as np import pandas as pd @@ -301,11 +302,14 @@ def __iter__(self): class PlanetCatalog(Catalog): """Creates a catalog of planets""" - def __init__(self): - super().__init__("PL", 11, "The planets") - planet_dict = sf_utils.calc_planets() - for sequence, name in enumerate(sf_utils.planet_names): - self.add_planet(sequence, name, planet_dict[name]) + def __init__(self, dt: datetime.datetime): + super().__init__("PL", 10, "The planets") + planet_dict = sf_utils.calc_planets(dt) + sequence = 0 + for name in sf_utils.planet_names: + if name.lower() != "sun": + self.add_planet(sequence, name, planet_dict[name]) + sequence += 1 def add_planet(self, sequence: int, name: str, planet: Dict[str, Dict[str, float]]): ra, dec = planet["radec"] @@ -320,9 +324,10 @@ def add_planet(self, sequence: int, name: str, planet: Dict[str, Dict[str, float "const": constellation, "size": "", "mag": planet["mag"], + "names": [name.capitalize()], "catalog_code": "PL", "sequence": sequence + 1, - "description": f"{name.capitalize()}, alt={planet['altaz'][0]:.1f}°", + "description": f"", } ) self.add_object(obj) @@ -351,8 +356,6 @@ def build(self) -> Catalogs: self.catalog_dicts = {} logging.debug(f"Loaded {len(composite_objects)} objects from database") all_catalogs: Catalogs = self._get_catalogs(composite_objects, catalogs_info) - planet_catalog: Catalog = PlanetCatalog() - all_catalogs.add(planet_catalog) return all_catalogs def _build_composite( diff --git a/python/PiFinder/main.py b/python/PiFinder/main.py index 795087ce..03bc6493 100644 --- a/python/PiFinder/main.py +++ b/python/PiFinder/main.py @@ -443,6 +443,7 @@ def main(script_name=None, show_fps=False): f'GPS: Location {location["lat"]} {location["lon"]} {location["altitude"]}' ) location["gps_lock"] = True + shared_state.set_location(location) if gps_msg == "time": logging.debug(f"GPS time msg: {gps_content}") diff --git a/python/PiFinder/ui/catalog.py b/python/PiFinder/ui/catalog.py index 02af6f5d..53b1d6cf 100644 --- a/python/PiFinder/ui/catalog.py +++ b/python/PiFinder/ui/catalog.py @@ -25,7 +25,7 @@ import logging from PiFinder.db.observations_db import ObservationsDatabase -from PiFinder.catalogs import CompositeObject, CatalogBuilder, Catalogs +from PiFinder.catalogs import CompositeObject, CatalogBuilder, Catalogs, PlanetCatalog # Constants for display modes @@ -130,6 +130,18 @@ def __init__(self, *args): self.catalog_tracker.filter() self.update_object_info() + self._planets_loaded = False + + def add_planets(self, dt): + """ + Since we can't calc planet positions until we know the date/time + this is called once we have a GPS lock to add on the planets catalog + """ + self.catalogs.add(PlanetCatalog(dt)) + self.catalog_tracker = CatalogTracker( + self.catalogs, self.shared_state, self._config_options + ) + self._planets_loaded = True def _layout_designator(self): """ @@ -329,6 +341,13 @@ def update_object_info(self): def active(self): # trigger refilter super().active() + + # check for planet add + if not self._planets_loaded: + dt = self.shared_state.datetime() + if dt: + self.add_planets(dt) + self.catalog_tracker.filter() target = self.ui_state.target() if target: