Skip to content

Commit

Permalink
Add target.from_tid, visibility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emirkmo committed Feb 9, 2023
1 parent d5fcb37 commit f3bc6e8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
9 changes: 8 additions & 1 deletion flows/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@ def from_fid(cls, fid: int, datafile: Optional[Dict] = None) -> 'Target':
"""
Create target from fileid.
"""

datafile = datafile or api.get_datafile(fid)
if datafile is None:
raise ValueError(f'No datafile found for fid={fid}')
d = api.get_target(datafile['target_name']) | datafile
return cls.from_dict(d)

@classmethod
def from_tid(cls, target_id: int) -> 'Target':
"""
Create target from target id.
"""
return cls.from_dict(api.get_target(target_id))
4 changes: 3 additions & 1 deletion flows/visibility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Target visibility plotting.
Target visibility plotting.
@TODO: Move to flows-tools.
.. codeauthor:: Rasmus Handberg <rasmush@phys.au.dk>
Expand All @@ -20,6 +20,7 @@
from tendrils import api
from .target import Target
from typing import Optional
import warnings


# --------------------------------------------------------------------------------------------------
Expand All @@ -41,6 +42,7 @@ def visibility(target: Target, siteid: Optional[int] = None, date=None, output=N
"""

logger = logging.getLogger(__name__)
warnings.warn(DeprecationWarning("This module is moved to SNFLOWS/flows-tools."))

if date is None:
date = datetime.utcnow()
Expand Down
32 changes: 31 additions & 1 deletion tests/test_sites.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from typing import Optional
from typing import Optional, List, Dict, Any

import pytest
from matplotlib import pyplot as plt
import tempfile
from tendrils import api

from flows.instruments import INSTRUMENTS, Instrument, Site
from flows.visibility import visibility
from flows.target import Target


@pytest.fixture(scope='session')
def flows_sites() -> List[Dict[str, Any]]:
return api.get_all_sites() # type: ignore


def test_flows_sites(siteid: int = 9):
Expand Down Expand Up @@ -30,6 +40,7 @@ def test_site_from_astropy_vs_flows(sitename: str = "paranal", siteid: int = 2):
assert int(site.latitude) == int(flows_site.latitude)
assert int(site.longitude) == int(flows_site.longitude)


def test_user_site(monkeypatch):

# provided inputs
Expand All @@ -55,5 +66,24 @@ def test_user_site(monkeypatch):
assert int(site.earth_location.lat.value) == int(lat)


# Very basic due to being moved to flows tools
def test_site_visibility(flows_sites):
target = Target.from_tid(8)
with pytest.deprecated_call():
with plt.ioff():
ax = visibility(target, siteid=2)
assert isinstance(ax, plt.Axes)

with tempfile.TemporaryDirectory() as tempdir:
with pytest.deprecated_call():
with plt.ioff():
plotpaths = visibility(
target, date="2023-01-01", output=tempdir
)

assert not isinstance(plotpaths, plt.Axes)
assert len(flows_sites) == len(plotpaths)


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

0 comments on commit f3bc6e8

Please sign in to comment.