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

Cut new version #30

Merged
merged 18 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
277 changes: 0 additions & 277 deletions examples/collect_hrrr_precip.py

This file was deleted.

3 changes: 1 addition & 2 deletions tests/RME_test_case.py → tests/RME/RME_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class RMETestCase(unittest.TestCase):
"""
Base class for all unit tests using the RME data
"""
test_dir = Path(__file__).parent
basin_dir = test_dir.joinpath('RME')
basin_dir = Path(__file__).parent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain moving this file? It seems to go against how SMRF/AWSM are setup to test with basins. Just trying to think of how the format fits with the other tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this in here because I see this as the next improvement over how SMRF/AWSM does it and would also like to see this change propagate to there. If you look at the class, it basically describes the structure of the basin dir test data and is all you need in order to use the data in tests.

It also clearly separates test data from test logic, moving it in there.

Last argument I see is that the test_dir property was removed and this was never used anywhere else, showing that moving the class into the folder is the better place to keep it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does seem to be cleaner if we move that route. Then in SMRF, the RME/Lakes would be under their presepective directories.

The one major downside I see is discoverability for that class. You pretty much have to know that it's there, as in the SMRF case, it'll be buried a few directories down.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see this downside addressed with documentation. Currently we have a need for that as a whole, on how they are set up, what parts tests what, or when it is appropriate to updated the gold files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes to documentation. But it's way easier to just code.

gold_dir = basin_dir.joinpath('gold', 'hrrr')
hrrr_dir = basin_dir.joinpath('gridded/hrrr_test')
output_path = basin_dir.joinpath('output')
Expand Down
5 changes: 5 additions & 0 deletions tests/RME/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .RME_test_case import RMETestCase

__all__ = [
RMETestCase
]
10 changes: 5 additions & 5 deletions tests/data/hrrr/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mock
import xarray

from tests.RME_test_case import RMETestCase
from tests.RME import RMETestCase
from weather_forecast_retrieval.data.hrrr.file_loader import FileLoader
from weather_forecast_retrieval.data.hrrr.grib_file import GribFile
from weather_forecast_retrieval.data.hrrr.netcdf_file import NetCdfFile
Expand Down Expand Up @@ -52,12 +52,12 @@ def setUpClass(cls):
cls.subject = FileLoader()

def test_parameters(self, _data_patch, _df_patch):
self.subject.get_saved_data(*self.METHOD_ARGS, output_dir='path')
self.subject.get_saved_data(*self.METHOD_ARGS, file_dir='path')

self.assertEqual(self.START_DATE, self.subject.start_date)
self.assertEqual(self.END_DATE, self.subject.end_date)
self.assertEqual(self.BBOX, self.subject.file_loader.bbox)
self.assertEqual('path', self.subject.output_dir)
self.assertEqual('path', self.subject.file_dir)

def test_defaults_to_grib(self, _data_patch, _df_patch):
self.subject.get_saved_data(*self.METHOD_ARGS)
Expand Down Expand Up @@ -105,7 +105,7 @@ def setUp(self):
subject = FileLoader()
subject.start_date = RMETestCase.START_DATE
subject.end_date = RMETestCase.END_DATE
subject.output_dir = RMETestCase.hrrr_dir.as_posix()
subject.file_dir = RMETestCase.hrrr_dir.as_posix()
subject.file_loader = file_loader
self.subject = subject

Expand Down Expand Up @@ -145,7 +145,7 @@ def test_tries_six_forecast_hours(self):
)

def test_file_not_found(self):
self.subject.output_dir = None
self.subject.file_dir = None

with self.assertRaises(IOError):
self.subject.get_data({})
Expand Down
55 changes: 55 additions & 0 deletions tests/data/hrrr/test_hrrr_gold.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
import pandas as pd

from tests.RME import RMETestCase
from weather_forecast_retrieval.data.hrrr import FileLoader


def compare_gold(v_name, gold_dir, test_df):
"""
Compare against gold HRRR data stored as csv in basin directory.

Args:
v_name: Variable to compare that also serves as the file name to load.
gold_dir: Directory containing gold standard results
test_df: Data frame containing test results to be compared
"""

df_gold = pd.read_csv(
gold_dir.joinpath(v_name+'_data.csv').as_posix(),
header=0,
delimiter=',',
parse_dates=['date_time'],
index_col='date_time',
dtype=np.float32,
)

np.testing.assert_allclose(test_df.values, df_gold.values, rtol=1e-4)


class TestHRRR(RMETestCase):
def testHRRRGribLoad(self):
"""
Load HRRR data from multiple grib files
"""
metadata, data = FileLoader().get_saved_data(
self.START_DATE,
self.END_DATE,
self.BBOX,
file_type='grib2',
file_dir=self.hrrr_dir.as_posix(),
force_zone_number=self.UTM_ZONE_NUMBER
)

df = pd.read_csv(
self.gold_dir.joinpath('metadata.csv').as_posix(),
header=0,
index_col='grid'
)

self.assertIsNotNone(metadata)
np.testing.assert_allclose(
df.values, metadata[df.columns].values, rtol=1e-4
)

[compare_gold(k, self.gold_dir, df) for k, df in data.items()]