Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a SiteDetails dataclass when creating a location #1169

Merged
merged 3 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/panoptes/pocs/mount/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def create_mount_from_config(mount_info=None,

# Get details from config.
site_details = create_location_from_config()
earth_location = site_details['earth_location']
earth_location = site_details.earth_location

brand = mount_info.get('brand')
driver = mount_info.get('driver')
Expand Down Expand Up @@ -122,7 +122,7 @@ def create_mount_simulator(mount_info=None,
# Set mount device info to simulator
set_config('mount', mount_config)

earth_location = earth_location or create_location_from_config()['earth_location']
earth_location = earth_location or create_location_from_config().earth_location

logger.debug(f"Loading mount driver: {mount_config['driver']}")
try:
Expand Down
6 changes: 3 additions & 3 deletions src/panoptes/pocs/observatory.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def __init__(self, cameras=None, scheduler=None, dome=None, mount=None, *args, *
# Setup information about site location
self.logger.info('Setting up location')
site_details = create_location_from_config()
self.location = site_details['location']
self.earth_location = site_details['earth_location']
self.observer = site_details['observer']
self.location = site_details.location
self.earth_location = site_details.earth_location
self.observer = site_details.observer

# Do some one-time calculations
now = current_time()
Expand Down
2 changes: 1 addition & 1 deletion src/panoptes/pocs/scheduler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg
if not observer:
logger.debug(f'No Observer provided, creating location from config.')
site_details = create_location_from_config()
observer = site_details['observer']
observer = site_details.observer

# Read the targets from the file
fields_file = scheduler_config.get('fields_file', 'simple.yaml')
Expand Down
5 changes: 3 additions & 2 deletions src/panoptes/pocs/state/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def next_state(self, value):
# Methods
################################################################################################

def run(self, exit_when_done=False, run_once=False, park_when_done=True, initial_next_state='ready'):
def run(self, exit_when_done=False, run_once=False, park_when_done=True,
initial_next_state='ready'):
"""Runs the state machine loop.

This runs the state machine in a loop. Setting the machine property
Expand Down Expand Up @@ -188,7 +189,7 @@ def run(self, exit_when_done=False, run_once=False, park_when_done=True, initial
else:
if park_when_done:
self.logger.info(f'Run loop ended, parking mount')
self.observatory.mount.park()
self.observatory.mount.park()

def goto_next_state(self):
"""Make a transition to the next state.
Expand Down
2 changes: 1 addition & 1 deletion src/panoptes/pocs/utils/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ class NotSafeError(PanError):
""" Error for when safety fails. """

def __init__(self, msg='Not safe', **kwargs):
super().__init__(msg, **kwargs)
super().__init__(msg, **kwargs)
20 changes: 14 additions & 6 deletions src/panoptes/pocs/utils/location.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from dataclasses import dataclass
from astroplan import Observer
from astropy import units as u
from astropy.coordinates import EarthLocation
Expand All @@ -9,7 +10,14 @@
logger = get_logger()


def create_location_from_config():
@dataclass
class SiteDetails:
observer: Observer
earth_location: EarthLocation
location: dict


def create_location_from_config() -> SiteDetails:
"""
Sets up the site and location details.

Expand Down Expand Up @@ -63,11 +71,11 @@ def create_location_from_config():
earth_location = EarthLocation(lat=latitude, lon=longitude, height=elevation)
observer = Observer(location=earth_location, name=name, timezone=timezone)

site_details = {
"location": location,
"earth_location": earth_location,
"observer": observer
}
site_details = SiteDetails(
location=location,
earth_location=earth_location,
observer=observer
)

return site_details

Expand Down
10 changes: 5 additions & 5 deletions tests/scheduler/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def test_bad_scheduler_namespace(config_host, config_port):
set_config('scheduler.type', 'dispatch')
site_details = create_location_from_config()
with pytest.raises(error.NotFound):
create_scheduler_from_config(observer=site_details['observer'])
create_scheduler_from_config(observer=site_details.observer)

set_config('scheduler.type', 'panoptes.pocs.scheduler.dispatch')
scheduler = create_scheduler_from_config(observer=site_details['observer'])
scheduler = create_scheduler_from_config(observer=site_details.observer)

assert isinstance(scheduler, BaseScheduler)

Expand All @@ -36,7 +36,7 @@ def test_bad_scheduler_type(config_host, config_port):
set_config('scheduler.type', 'foobar')
site_details = create_location_from_config()
with pytest.raises(error.NotFound):
create_scheduler_from_config(observer=site_details['observer'])
create_scheduler_from_config(observer=site_details.observer)

reset_conf(config_host, config_port)

Expand All @@ -45,7 +45,7 @@ def test_bad_scheduler_fields_file(config_host, config_port):
set_config('scheduler.fields_file', 'foobar')
site_details = create_location_from_config()
with pytest.raises(error.NotFound):
create_scheduler_from_config(observer=site_details['observer'])
create_scheduler_from_config(observer=site_details.observer)

reset_conf(config_host, config_port)

Expand All @@ -58,5 +58,5 @@ def test_no_scheduler_in_config(config_host, config_port):
set_config('scheduler', None)
site_details = create_location_from_config()
assert create_scheduler_from_config(
observer=site_details['observer']) is None
observer=site_details.observer) is None
reset_conf(config_host, config_port)
4 changes: 1 addition & 3 deletions tests/test_ioptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ def setup(self):
with pytest.raises(AssertionError):
mount = Mount(location)

earth_location = location['earth_location']

mount = Mount(earth_location)
mount = Mount(location.earth_location)
assert mount is not None

self.mount = mount
Expand Down
2 changes: 1 addition & 1 deletion tests/test_mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_create_mount_with_earth_location(config_host, config_port):
# Set config to not have a location.
set_config('location', None)
set_config('simulator', hardware.get_all_names())
assert isinstance(create_mount_from_config(earth_location=loc['earth_location']),
assert isinstance(create_mount_from_config(earth_location=loc.earth_location),
AbstractMount) is True

reset_conf(config_host, config_port)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_observatory.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def observatory(mount, cameras, images_dir):
"""Return a valid Observatory instance with a specific config."""

site_details = create_location_from_config()
scheduler = create_scheduler_from_config(observer=site_details['observer'])
scheduler = create_scheduler_from_config(observer=site_details.observer)

obs = Observatory(scheduler=scheduler)
obs.set_mount(mount)
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_cannot_observe(caplog):
time.sleep(0.5) # log sink time
log_record = caplog.records[-1]
assert log_record.message.endswith("not present") and log_record.levelname == "WARNING"
obs.scheduler = create_scheduler_from_config(observer=site_details['observer'])
obs.scheduler = create_scheduler_from_config(observer=site_details.observer)

assert obs.can_observe is False
time.sleep(0.5) # log sink time
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_primary_camera_no_primary_camera(observatory):

def test_set_scheduler(observatory, caplog):
site_details = create_location_from_config()
scheduler = create_scheduler_from_config(observer=site_details['observer'])
scheduler = create_scheduler_from_config(observer=site_details.observer)

assert observatory.current_observation is None

Expand Down
2 changes: 1 addition & 1 deletion tests/test_pocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def site_details():

@pytest.fixture(scope='function')
def scheduler(site_details):
return create_scheduler_from_config(observer=site_details['observer'])
return create_scheduler_from_config(observer=site_details.observer)


@pytest.fixture(scope='function')
Expand Down