Skip to content

Commit

Permalink
add test for new site generation, flows instrument sites.
Browse files Browse the repository at this point in the history
  • Loading branch information
emirkmo committed Feb 8, 2023
1 parent ba4f4c8 commit 4527e97
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 12 deletions.
3 changes: 3 additions & 0 deletions flows/instruments/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .instruments import INSTRUMENTS, Instrument, verify_coordinates
from .sites import Site

__all__ = ["INSTRUMENTS", "Instrument", "verify_coordinates", "Site"]
27 changes: 15 additions & 12 deletions flows/instruments/sites.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from dataclasses import dataclass
from astropy.coordinates import EarthLocation
import astropy.units as u
from typing import Optional

import astropy.units as u
from astropy.coordinates import EarthLocation
from tendrils import api


@dataclass
class Site:
siteid: int
Expand All @@ -13,9 +15,9 @@ class Site:
elevation: float
earth_location: Optional[EarthLocation] = None
site_keyword: Optional[str] = None

def __post_init__(self):
if self.earth_location is None:
if self.earth_location is not None:
return
self.earth_location = EarthLocation(
lat=self.latitude*u.deg,
Expand All @@ -25,23 +27,24 @@ def __post_init__(self):

@classmethod
def from_flows(cls, siteid: int) -> 'Site':
return cls(**api.get_site(siteid))

site_dict = api.get_site(siteid)
site_dict['earth_location'] = site_dict.pop('EarthLocation')
return cls(**site_dict)

@classmethod
def from_astropy(cls, sitename: str) -> 'Site':
loc = EarthLocation.of_site(sitename)
return cls(siteid=999, sitename=sitename,
longitude=loc.long.value, latitude=loc.lat.value,
return cls(siteid=999, sitename=sitename,
longitude=loc.lon.value, latitude=loc.lat.value,
elevation=loc.height.value, earth_location=loc)

@classmethod
def from_query(cls) -> 'Site':
sitename = input('Enter a site name for logging: ')
longitude = float(input('Enter longitude in degrees: '))
lat = float(input('Enter latitude in degrees: '))
elevation = float(input('Enter elevation in meters: '))
siteid = 1000 # hardcoded for user defined site
return cls(siteid=999, sitename=sitename,
siteid = 999 # hardcoded for user defined site
return cls(siteid=siteid, sitename=sitename,
longitude=longitude, latitude=lat,
elevation=elevation)

60 changes: 60 additions & 0 deletions tests/test_sites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from typing import Optional

import pytest
from tendrils import api

from flows.instruments import INSTRUMENTS, Instrument, Site


def test_flows_sites(siteid: int = 9):

site = Site.from_flows(siteid)

instrument: Optional[Instrument] = None
for instrument_name, instrument_class in INSTRUMENTS:
if instrument_class.siteid == siteid:
instrument = instrument_class()
if instrument is None:
raise ValueError(f"Expected to find site and instrument with siteid {siteid} but found None in INSTRUMENTS")

assert instrument.telescope == 'CA 3.5m'
assert site.siteid == siteid
assert site.sitename == site.sitename
assert site.site_keyword == site.site_keyword


def test_site_from_astropy_vs_flows(sitename: str = "paranal", siteid: int = 2):
site = Site.from_astropy(sitename)
assert site.sitename == sitename

flows_site = Site.from_flows(siteid)
assert int(site.latitude) == int(flows_site.latitude)
assert int(site.longitude) == int(flows_site.longitude)

def test_user_site(monkeypatch):

# provided inputs
sitename = 'test'
longitude = 11.5
lat = 12.5
elevation = 1200

# creating iterator object
answers = iter([sitename, str(longitude), str(lat), str(elevation)])

monkeypatch.setattr('builtins.input', lambda name: next(answers))
site = Site.from_query()

assert site.sitename == 'test'
assert site.siteid == 999
assert int(site.longitude) == int(longitude)
assert int(site.latitude) == int(lat)
assert int(elevation) == int(elevation)

if site.earth_location is None:
raise ValueError(f"Expected to find site with earth location!")
assert int(site.earth_location.lat.value) == int(lat)


if __name__ == '__main__':
pytest.main([__file__])

0 comments on commit 4527e97

Please sign in to comment.