Skip to content

Commit

Permalink
cache tmpdir + test to_netcdf
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Jun 2, 2022
1 parent 78b9704 commit 14fe23d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 deletions.
23 changes: 18 additions & 5 deletions sfy-processing/sfy/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tqdm import tqdm
import json
import math
import tempfile

logger = logging.getLogger(__name__)

Expand All @@ -33,21 +34,33 @@ class Hub:
key: str
cache: Path

def __init__(self, endpoint, key, cache):
tmpdir = None

def __init__(self, endpoint, key, cache = None):
"""
Set up a Hub client.
endpoint: URL to sfy hub.
endpoint: URL to sfy hub including buoys, e.g.:
https://wavebug.met.no/buoys/
key: Read token.
cache: A directory used to cache the data files.
"""
self.endpoint = endpoint

if self.endpoint[-1] != '/':
self.endpoint += '/'

self.key = key
self.cache = Path(cache)

if cache is not None:
self.cache = Path(cache)
else:
logger.error("No cache dir specified, will use temporary directory. This will cause data to be re-downloaded every time.")
self.tmpdir = tempfile.TemporaryDirectory()
self.cache = Path(self.tmpdir.name)

if not self.cache.exists():
os.makedirs(self.cache, exist_ok=True)
Expand All @@ -63,8 +76,8 @@ def from_env():
KEY = os.getenv('SFY_READ_TOKEN')
CACHE = os.getenv('SFY_DATA_CACHE')

if API is None or KEY is None or CACHE is None:
raise Exception("No API, KEY or CACHE")
if API is None or KEY is None:
raise Exception("Missing API or KEY")

API = urljoin(API, 'buoys')
return Hub(urljoin(API, 'buoys'), KEY, CACHE)
Expand Down
9 changes: 9 additions & 0 deletions sfy-processing/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from sfy import hub


@pytest.fixture
def sfyhub(tmpdir):
h = hub.Hub.from_env()
h.cache = tmpdir
return h
35 changes: 20 additions & 15 deletions sfy-processing/tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@

from sfy import hub
from sfy.axl import AxlCollection
from . import sfyhub


@pytest.fixture
def sfy(tmpdir):
h = hub.Hub.from_env()
h.cache = tmpdir
return h


def test_collect(sfy):
b = sfy.buoy("dev864475044204278")
pcks = b.axl_packages_range(datetime(2022, 4, 26, 11, 34 , tzinfo=timezone.utc),
datetime(2022, 4, 26, 11, 35, tzinfo=timezone.utc))
def test_collect(sfyhub):
b = sfyhub.buoy("dev864475044204278")
pcks = b.axl_packages_range(
datetime(2022, 4, 26, 11, 34, tzinfo=timezone.utc),
datetime(2022, 4, 26, 11, 35, tzinfo=timezone.utc))
assert len(pcks) > 2

c = AxlCollection(pcks)
Expand All @@ -25,13 +20,23 @@ def test_collect(sfy):

np.testing.assert_almost_equal(c.duration, len(pcks) * 1024 / 52.)

def test_segment(sfy):
b = sfy.buoy("dev864475044204278")
pcks = b.axl_packages_range(datetime(2022, 4, 26, 11, 34 , tzinfo=timezone.utc),
datetime(2022, 4, 26, 11, 35, tzinfo=timezone.utc))

def test_segment(sfyhub):
b = sfyhub.buoy("dev864475044204278")
pcks = b.axl_packages_range(
datetime(2022, 4, 26, 11, 34, tzinfo=timezone.utc),
datetime(2022, 4, 26, 11, 35, tzinfo=timezone.utc))
c = AxlCollection(pcks)

segments = list(c.segments())
assert len(segments) == 2
assert sum((len(s) for s in segments)) == len(c)


def test_to_nc(sfyhub, tmpdir):
b = sfyhub.buoy("dev864475044204278")
pcks = b.axl_packages_range(
datetime(2022, 4, 26, 11, 34, tzinfo=timezone.utc),
datetime(2022, 4, 26, 11, 35, tzinfo=timezone.utc))
c = AxlCollection(pcks)
c.to_netcdf(tmpdir / "test.nc")
41 changes: 18 additions & 23 deletions sfy-processing/tests/test_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,51 @@
from datetime import datetime, timezone

from sfy import hub
from . import sfyhub


@pytest.fixture
def sfy(tmpdir):
h = hub.Hub.from_env()
h.cache = tmpdir
return h
def test_list_buoys(sfyhub):
print(sfyhub.buoys())


def test_list_buoys(sfy):
print(sfy.buoys())


def test_get_buoy(sfy):
b = sfy.buoy("867730051260788")
def test_get_buoy(sfyhub):
b = sfyhub.buoy("867730051260788")
assert b.dev == "dev867730051260788"


def test_list_packages(sfy):
b = sfy.buoy("867730051260788")
def test_list_packages(sfyhub):
b = sfyhub.buoy("867730051260788")
print(b.packages())


def test_get_raw_package(sfy):
b = sfy.buoy("dev864475044204278")
def test_get_raw_package(sfyhub):
b = sfyhub.buoy("dev864475044204278")
pck = b.raw_package(
'1650973616744-42e2549d-868b-4c46-a7ef-723c7a1e6418_axl.qo.json')


def test_get_package(sfy):
b = sfy.buoy("dev864475044204278")
def test_get_package(sfyhub):
b = sfyhub.buoy("dev864475044204278")
pck = b.package(
'1650973616744-42e2549d-868b-4c46-a7ef-723c7a1e6418_axl.qo.json')
print(pck)


def test_get_last(sfy, benchmark):
b = sfy.buoy("867730051260788")
def test_get_last(sfyhub, benchmark):
b = sfyhub.buoy("867730051260788")
pck = benchmark(b.last)
print(pck)


def test_list_packages_range(sfy):
b = sfy.buoy("867730051260788")
def test_list_packages_range(sfyhub):
b = sfyhub.buoy("867730051260788")
start = datetime(2022, 1, 21, tzinfo=timezone.utc)
pcks = b.packages_range(start=start)
assert all((pck[0] > start for pck in pcks))

def test_fetch_raw_range(sfy):
b = sfy.buoy("867730051260788")

def test_fetch_raw_range(sfyhub):
b = sfyhub.buoy("867730051260788")
start = datetime(2022, 1, 21, tzinfo=timezone.utc)
pcks = b.packages_range(start=start)
assert all((pck[0] > start for pck in pcks))

0 comments on commit 14fe23d

Please sign in to comment.