Skip to content

Commit

Permalink
Create scheduler updates (#1174)
Browse files Browse the repository at this point in the history
* Create scheduler updates

A few updates to the helper script that creates a scheduler.

* Better checking of `iers_auto` conf for downloading IERS file when problems.
* Add some typing to the file. Note that this requires python 3.10.
* Use `Path`s.

* Don't require python 3.10 syntax.
  • Loading branch information
wtgee authored Aug 20, 2022
1 parent 9a99059 commit ed2fa61
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/panoptes/pocs/scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import os
from pathlib import Path
from typing import List, Optional

from astropy.utils.iers import Conf as iers_conf
from panoptes.utils import error
from panoptes.utils.config.client import get_config
from panoptes.utils.library import load_module

from panoptes.pocs.scheduler.constraint import Altitude
from panoptes.pocs.scheduler.constraint import Altitude, BaseConstraint
from panoptes.pocs.scheduler.constraint import Duration
from panoptes.pocs.scheduler.constraint import MoonAvoidance

from panoptes.pocs.scheduler.scheduler import BaseScheduler # noqa; needed for import
from panoptes.utils import error
from panoptes.utils.library import load_module
from panoptes.pocs.utils.logger import get_logger
from panoptes.utils.config.client import get_config

from panoptes.pocs.utils.location import create_location_from_config
from panoptes.pocs.utils.logger import get_logger

logger = get_logger()


def create_scheduler_from_config(config=None, observer=None, iers_url=None, *args, **kwargs):
def create_scheduler_from_config(config=None, observer=None, iers_url=None, *args,
**kwargs) -> Optional[BaseScheduler]:
""" Sets up the scheduler that will be used by the observatory """

scheduler_config = config or get_config('scheduler', default=None)
Expand All @@ -29,21 +29,22 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg

iers_url = iers_url or scheduler_config.get('iers_url')
if iers_url is not None:
logger.debug(f'Getting IERS data at {iers_url=}')
logger.info(f'Getting IERS data at {iers_url=}')
iers_conf.iers_auto_url.set(iers_url)
iers_conf.auto_download.set(scheduler_config.get('iers_auto', False))

if not observer:
logger.debug(f'No Observer provided, creating location from config.')
site_details = create_location_from_config()
observer = site_details.observer

# Read the targets from the file
fields_file = scheduler_config.get('fields_file', 'simple.yaml')
fields_dir = get_config('directories.fields', './conf_files/fields')
fields_path = os.path.join(fields_dir, fields_file)
fields_file = Path(scheduler_config.get('fields_file', 'simple.yaml'))
fields_dir = Path(get_config('directories.fields', default='./conf_files/fields'))
fields_path = fields_dir / fields_file
logger.debug(f'Creating scheduler: {fields_path}')

if os.path.exists(fields_path):
if fields_path.exists():
scheduler_type = scheduler_config.get('type', 'panoptes.pocs.scheduler.dispatch')

try:
Expand All @@ -53,20 +54,20 @@ def create_scheduler_from_config(config=None, observer=None, iers_url=None, *arg
constraints = create_constraints_from_config(config=scheduler_config)

# Create the Scheduler instance
scheduler = module.Scheduler(observer,
fields_file=fields_path,
constraints=constraints,
*args, **kwargs)
pocs_scheduler = module.Scheduler(observer,
fields_file=str(fields_path),
constraints=constraints,
*args, **kwargs)
logger.debug("Scheduler created")
except error.NotFound as e:
raise error.NotFound(msg=e)
else:
raise error.NotFound(msg=f"Fields file does not exist: {fields_path=!r}")

return scheduler
return pocs_scheduler


def create_constraints_from_config(config=None):
def create_constraints_from_config(config=None) -> List[BaseConstraint]:
scheduler_config = config or get_config('scheduler', default=dict())
constraints = list()
for constraint_config in scheduler_config.get('constraints', list()):
Expand Down

0 comments on commit ed2fa61

Please sign in to comment.