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()