-
Notifications
You must be signed in to change notification settings - Fork 3
Mapping driven metadata (for solar and wind) #54
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
Changes from all commits
fc75bf6
3994db5
8ecde29
fb9f3bf
752386c
283570d
2425264
6ea0a84
5267290
0425fc0
3f14175
6e83dbc
3968f7b
9d0cf28
ea18300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| from pathlib import Path | ||
|
|
||
| from isp_trace_parser import mappings | ||
|
|
||
| # Temporrary mapping that translates the YAML's resource_type vocabulary to the legacy | ||
| # short codes still used downstream for now (filters, parquet columns, output filenames). | ||
|
|
||
| _RESOURCE_TYPE_CODES: dict[str, str] = { | ||
| "solar_sat": "SAT", | ||
| "solar_ffp": "FFP", | ||
| "solar_cst": "CST", | ||
| "wind": "WIND", | ||
| "wind_high": "WH", | ||
| "wind_medium": "WM", | ||
| "wind_offshore_fixed": "WFX", | ||
| "wind_offshore_floating": "WFL", | ||
| } | ||
|
|
||
|
|
||
| def build( | ||
| files: list[Path], | ||
| version: str, | ||
| ) -> dict[Path, dict[str, str | int]]: | ||
| """Build metadata for resource files by lookup in the resource mapping. | ||
|
|
||
| The mapping key is the trace stem (the filename with `_RefYear<year>.csv` | ||
| stripped) so `<stem>_RefYear<year>.csv` decomposes back to (stem, year). | ||
| """ | ||
|
|
||
| resource_mapping = mappings.load("resources", version=version) | ||
|
|
||
| file_metadata: dict[Path, dict[str, str]] = {} | ||
| for path in files: | ||
| stem, sep, ref = path.stem.rpartition("_RefYear") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth adding error handling for either file name not matching expected format with _RefYear, or filename missing from resource_mapping. I guess these are errors which should only be found by devs at parser update time, so maybe it's not worth it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm yes. I think you're right that it should be found at update time (at least with respect to resource_mapping). That said maybe someone might tinker with filenames or something on their computer (for some reason?) - will add in a little error handling ("Unexpected trace filename") or similar. |
||
| if not sep or not ref.isdigit() or stem not in resource_mapping: | ||
| raise ValueError(f"Unexpected trace filename: {path.name}") | ||
| entry = resource_mapping[stem] | ||
| file_metadata[path] = { | ||
| "name": entry["location"], | ||
| "reference_year": int(ref), | ||
| "resource_type": _RESOURCE_TYPE_CODES[entry["resource_type"]], | ||
| "file_type": entry["location_type"], | ||
| } | ||
| return file_metadata | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from isp_trace_parser import resource_trace_metadata | ||
|
|
||
|
|
||
| def test_build(): | ||
| """One test covers function logic compared with regex approach | ||
|
|
||
| Solar zones / wind zones / extra reference years add no new code-path | ||
| coverage (they're different YAML rows, not different code) | ||
| """ | ||
| files = [ | ||
| Path("Adelaide_Desal_FFP_RefYear2011.csv"), | ||
| Path("BLUFF1_RefYear2011.csv"), | ||
| ] | ||
| metadata = resource_trace_metadata.build(files, version="2024") | ||
|
|
||
| assert metadata[files[0]] == { | ||
| "name": "Adelaide_Desal", | ||
| "reference_year": 2011, | ||
| "resource_type": "FFP", | ||
| "file_type": "project", | ||
| } | ||
| assert metadata[files[1]]["resource_type"] == "WIND" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "filename", | ||
| [ | ||
| "Adelaide_Desal.csv", # missing _RefYear separator | ||
| "Adelaide_Desal_RefYear.csv", # missing year | ||
| "Adelaide_Desal_RefYear2011a.csv", # non-digit year | ||
| "Mystery_Plant_RefYear2011.csv", # stem not in mapping | ||
| ], | ||
| ) | ||
| def test_build_rejects_unexpected_filename(filename): | ||
| with pytest.raises(ValueError, match="Unexpected trace filename"): | ||
| resource_trace_metadata.build([Path(filename)], version="2024") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generator_to_trace_draft_mapper.py imported extract_solar_trace_metadata and extract_wind_trace_metadata. So maybe we should just delete generator_to_trace_draft_mapper.py along with these functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah good point - deleted.