Refine climate metrics and data pipeline
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
SCRIPTS_DIR = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||
|
||||
from apply_nsrdb_cloud_metric_to_climate_data import ( # noqa: E402
|
||||
POLYGON_SOURCE_TAG,
|
||||
REPRESENTATIVE_POINT_SOURCE_TAG,
|
||||
merge_metric,
|
||||
)
|
||||
|
||||
|
||||
def write_csv(path: Path, fieldnames: list[str], rows: list[dict[str, str]]) -> None:
|
||||
with path.open("w", encoding="utf-8", newline="") as handle:
|
||||
writer = csv.DictWriter(handle, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
|
||||
|
||||
class ApplyNsrdbCloudMetricTests(unittest.TestCase):
|
||||
def test_polygon_area_weighted_value_wins_with_representative_fallback(self) -> None:
|
||||
with TemporaryDirectory() as temp_dir:
|
||||
base = Path(temp_dir)
|
||||
climate_data = base / "climate-data.csv"
|
||||
polygon_summary = base / "polygon-cloud.csv"
|
||||
representative_summary = base / "representative-cloud.csv"
|
||||
|
||||
write_csv(
|
||||
climate_data,
|
||||
["countyFips", "meanDailyGlobalHorizontalRadiationKwhM2Day", "clearSkyGhiReductionIndex", "source"],
|
||||
[
|
||||
{
|
||||
"countyFips": "01001",
|
||||
"meanDailyGlobalHorizontalRadiationKwhM2Day": "4.8",
|
||||
"clearSkyGhiReductionIndex": "0.9999",
|
||||
"source": f"base + {REPRESENTATIVE_POINT_SOURCE_TAG}",
|
||||
},
|
||||
{
|
||||
"countyFips": "01003",
|
||||
"meanDailyGlobalHorizontalRadiationKwhM2Day": "4.9",
|
||||
"clearSkyGhiReductionIndex": "",
|
||||
"source": "base",
|
||||
},
|
||||
{
|
||||
"countyFips": "01005",
|
||||
"meanDailyGlobalHorizontalRadiationKwhM2Day": "5.0",
|
||||
"clearSkyGhiReductionIndex": "",
|
||||
"source": "base",
|
||||
},
|
||||
],
|
||||
)
|
||||
write_csv(
|
||||
polygon_summary,
|
||||
[
|
||||
"county_fips",
|
||||
"clearSkyGhiReductionIndex",
|
||||
"areaWeightedClearSkyGhiReductionIndex",
|
||||
],
|
||||
[
|
||||
{
|
||||
"county_fips": "01001",
|
||||
"clearSkyGhiReductionIndex": "0.1111",
|
||||
"areaWeightedClearSkyGhiReductionIndex": "0.2222",
|
||||
},
|
||||
],
|
||||
)
|
||||
write_csv(
|
||||
representative_summary,
|
||||
["county_fips", "clearSkyGhiReductionIndex"],
|
||||
[
|
||||
{"county_fips": "01001", "clearSkyGhiReductionIndex": "0.3333"},
|
||||
{"county_fips": "01003", "clearSkyGhiReductionIndex": "0.4444"},
|
||||
],
|
||||
)
|
||||
|
||||
result = merge_metric(climate_data, polygon_summary, representative_summary)
|
||||
|
||||
self.assertEqual(result, (3, 1, 1, 1))
|
||||
with climate_data.open("r", encoding="utf-8", newline="") as handle:
|
||||
rows = {row["countyFips"]: row for row in csv.DictReader(handle)}
|
||||
|
||||
self.assertEqual(rows["01001"]["clearSkyGhiReductionIndex"], "0.2222")
|
||||
self.assertIn(POLYGON_SOURCE_TAG, rows["01001"]["source"])
|
||||
self.assertNotIn(REPRESENTATIVE_POINT_SOURCE_TAG, rows["01001"]["source"])
|
||||
self.assertEqual(rows["01003"]["clearSkyGhiReductionIndex"], "0.4444")
|
||||
self.assertIn(REPRESENTATIVE_POINT_SOURCE_TAG, rows["01003"]["source"])
|
||||
self.assertEqual(rows["01005"]["clearSkyGhiReductionIndex"], "")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,55 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
SCRIPTS_DIR = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||
|
||||
from summarize_county_gridmet_humidity import ( # noqa: E402
|
||||
DEFAULT_HEAT_INDEX_THRESHOLD_F,
|
||||
_heat_index_f,
|
||||
)
|
||||
|
||||
|
||||
class HeatIndexTests(unittest.TestCase):
|
||||
def test_matches_nws_example(self) -> None:
|
||||
result = _heat_index_f(np.array([100.0]), np.array([55.0]))
|
||||
|
||||
self.assertAlmostEqual(result[0], 124.0, delta=0.5)
|
||||
|
||||
def test_uses_simple_formula_below_regression_range(self) -> None:
|
||||
result = _heat_index_f(np.array([70.0]), np.array([50.0]))
|
||||
|
||||
self.assertAlmostEqual(result[0], 69.525, places=3)
|
||||
|
||||
def test_default_threshold_is_extreme_caution_boundary(self) -> None:
|
||||
heat_index = _heat_index_f(
|
||||
np.array([85.0, 90.0]),
|
||||
np.array([40.0, 55.0]),
|
||||
)
|
||||
|
||||
self.assertEqual(DEFAULT_HEAT_INDEX_THRESHOLD_F, 90.0)
|
||||
self.assertEqual(
|
||||
(heat_index >= DEFAULT_HEAT_INDEX_THRESHOLD_F).tolist(),
|
||||
[False, True],
|
||||
)
|
||||
|
||||
def test_relative_humidity_is_clamped_to_physical_range(self) -> None:
|
||||
result = _heat_index_f(
|
||||
np.array([90.0, 90.0]),
|
||||
np.array([-5.0, 105.0]),
|
||||
)
|
||||
expected = _heat_index_f(
|
||||
np.array([90.0, 90.0]),
|
||||
np.array([0.0, 100.0]),
|
||||
)
|
||||
|
||||
np.testing.assert_allclose(result, expected)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -7,7 +7,6 @@ from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
SCRIPTS_DIR = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
SCRIPTS_DIR = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||
|
||||
|
||||
@@ -1,28 +1,27 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import sys
|
||||
import unittest
|
||||
import csv
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import call, patch
|
||||
|
||||
|
||||
SCRIPTS_DIR = Path(__file__).resolve().parents[1] / "scripts"
|
||||
sys.path.insert(0, str(SCRIPTS_DIR))
|
||||
|
||||
from request_nsrdb_county_polygon_archives import ( # noqa: E402
|
||||
EXCEPTIONAL_POLYGON_WAIT,
|
||||
LARGE_POLYGON_WAIT,
|
||||
MEDIUM_POLYGON_WAIT,
|
||||
SMALL_POLYGON_WAIT,
|
||||
ArchiveQueueMonitor,
|
||||
CountyRequestEvent,
|
||||
CountyRequestState,
|
||||
CountyRequestStateMachine,
|
||||
EXCEPTIONAL_POLYGON_WAIT,
|
||||
InvalidCountyRequestTransition,
|
||||
LARGE_POLYGON_WAIT,
|
||||
LocalQueueCapacityError,
|
||||
MEDIUM_POLYGON_WAIT,
|
||||
SMALL_POLYGON_WAIT,
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user