forked from PyPSA/technology-data
-
Notifications
You must be signed in to change notification settings - Fork 1
Prototest manual input USA #68
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
Merged
Merged
Changes from 25 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
a0ea75b
data: add manual_input_usa.csv
finozzifa 658a937
code: create parser file
finozzifa 5d787d0
code: improve manual_input_usa.py
finozzifa 423b730
Merge branch 'prototype-2' of https://github.com/open-energy-transiti…
finozzifa 2db7119
Merge branch 'prototype-2' of https://github.com/open-energy-transiti…
finozzifa 57c13da
code: update manual_input_usa.py
finozzifa 514bc8b
code: update manual_input_usa.py
finozzifa 404cb07
code: update carrier and unit
finozzifa f43a2d6
code: pre-commit
finozzifa 367c745
code: new update
finozzifa fa869ca
pre-commit
finozzifa f35276b
code: modify manual_input_usa.py
finozzifa 2bfb02f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a5cbf34
code: include pre-commit hooks
finozzifa f1b876d
code: solve merge conflicts
finozzifa 85d2197
code: re-set parameter dictionary
finozzifa bd2f6d5
Merge branch 'prototype-2' of https://github.com/open-energy-transiti…
finozzifa fa24584
Merge branch 'prototype-2' of https://github.com/open-energy-transiti…
finozzifa b7cb057
code: solve merge conflicts
finozzifa 2bc2903
code: move parse_input_arguments to Commons
finozzifa 37326c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 843cc85
code: move parse_input_arguments to Commons
finozzifa 5a6bbdd
Merge branch 'issue_59_prototest_manual_input' of https://github.com/…
finozzifa a8a06d5
code: add unit tests
finozzifa 9a3f7e1
Merge branch 'prototype-2' of https://github.com/open-energy-transiti…
finozzifa 7297df9
Update src/technologydata/utils/carriers.txt
finozzifa 1ac28ec
Update src/technologydata/package_data/manual_input_usa/manual_input_…
finozzifa 5a48edb
Update src/technologydata/package_data/manual_input_usa/manual_input_…
finozzifa ef205e4
Update src/technologydata/package_data/manual_input_usa/manual_input_…
finozzifa bbd3cc1
merge from prototype-2
finozzifa 191f891
merge from origin
finozzifa f4aa965
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 386ed7d
Update src/technologydata/parsers/manual_input_usa/manual_input_usa.py
finozzifa 3623d17
Update src/technologydata/parsers/manual_input_usa/manual_input_usa.py
finozzifa 7e39994
further changes
finozzifa 4c277ad
Merge remote-tracking branch 'origin/issue_59_prototest_manual_input'…
finozzifa cdb0e31
code: update manual_input_usa technologies.json
finozzifa 544fac6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5bfc391
code: include changes to parser
finozzifa ecf0393
include pre-commit
finozzifa 0d2c03d
Merge branch 'issue_59_prototest_manual_input' of https://github.com/…
finozzifa 3e1a503
doc: update documentation'
finozzifa 744e6fc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f91f666
include pre-commit
finozzifa 724e3f1
Merge branch 'issue_59_prototest_manual_input' of https://github.com/…
finozzifa 5de286d
modify MANIFEST.in
finozzifa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 245 additions & 0 deletions
245
src/technologydata/package_data/manual_input_usa/manual_input_usa.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| # SPDX-FileCopyrightText: The technology-data authors | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| """ | ||
| Data parser for the manual_input_usa.csv data set. | ||
|
|
||
| How to run: | ||
| From the repository root, execute: | ||
| python src/technologydata/package_data/manual_input_usa/manual_input_usa.py | ||
|
|
||
|
finozzifa marked this conversation as resolved.
|
||
| Configuration options (command-line arguments): | ||
| --num_digits <int> Number of significant digits to round the values. Default: 4 | ||
| --store_source Store the source object on the Wayback Machine. Default: False | ||
|
|
||
| Example: | ||
| python src/technologydata/package_data/manual_input_usa/manual_input_usa.py --num_digits 3 --store_source | ||
|
|
||
| """ | ||
|
|
||
| import logging | ||
| import pathlib | ||
|
|
||
| import pandas | ||
|
|
||
| from technologydata import ( | ||
| Commons, | ||
| Parameter, | ||
| Source, | ||
| SourceCollection, | ||
| Technology, | ||
| TechnologyCollection, | ||
| ) | ||
|
|
||
| path_cwd = pathlib.Path.cwd() | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def extract_units_carriers_heating_value( | ||
| input_unit: str, | ||
| ) -> tuple[str, str | None, str | None]: | ||
| """ | ||
| Extract standardized units and carriers from an input unit string. Add also heating_value. | ||
|
|
||
| This function maps complex unit representations to simplified unit and carrier | ||
| combinations using a predefined dictionary of special patterns. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| input_unit : str | ||
| A specialized unit string to be converted. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple[str, str | None, str | None] | ||
| A tuple containing two elements: | ||
| - The first element is the standardized unit | ||
| - The second element is the corresponding carrier (or None if not found) | ||
| - The third element is the corresponding heating value (or None if not found) | ||
|
|
||
| """ | ||
| # Define conversion dictionary | ||
| special_patterns = { | ||
| "USD_2022/MW_FT": ("USD_2022/MW", "1/FT", "LHV"), | ||
| "MWh_H2/MWh_FT": ("MWh/MWh", "H2/FT", "LHV"), | ||
| "MWh_el/MWh_FT": ("MWh/MWh", "el/FT", "LHV"), | ||
| "t_CO2/MWh_FT": ("t/MWh", "CO2/FT", "LHV"), | ||
| "USD_2022/kWh_H2": ("USD_2022/kWh", "1/H2", "LHV"), | ||
| "MWh_el/MWh_H2": ("MWh/MWh", "el/H2", "LHV"), | ||
| "USD_2023/t_CO2/h": ("USD_2023/t/h", "1/CO2", "LHV"), | ||
|
finozzifa marked this conversation as resolved.
Outdated
|
||
| "MWh_el/t_CO2": ("MWh/t", "el/CO2", "LHV"), | ||
| "MWh_th/t_CO2": ("MWh/t", "thermal/CO2", "LHV"), | ||
| } | ||
|
|
||
| if isinstance(input_unit, str) and input_unit in special_patterns.keys(): | ||
| return special_patterns[input_unit] | ||
| else: | ||
| return input_unit, None, None | ||
|
|
||
|
|
||
| def build_technology_collection( | ||
| dataframe: pandas.DataFrame, | ||
| sources_path: pathlib.Path, | ||
| store_source: bool = False, | ||
| ) -> TechnologyCollection: | ||
| """ | ||
| Compute a collection of technologies from a grouped DataFrame. | ||
|
|
||
| Processes input DataFrame by grouping technologies and extracting their parameters, | ||
| creating Technology instances for each unique group. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| dataframe : pandas.DataFrame | ||
| Input DataFrame containing technology parameters. | ||
| Expected columns include: | ||
| - 'scenario': Estimation or case identifier | ||
| - 'year': Year of the technology | ||
| - 'technology': Detailed technology name | ||
| - 'parameter': Parameter name | ||
| - 'value': Parameter value | ||
| - 'unit': Parameter units | ||
| - 'further_description': Extra information about the technology | ||
| - 'financial_case': Technology financial case | ||
| sources_path: pathlib.Path | ||
| Output path for storing the SourceCollection object | ||
| store_source: Optional[bool] | ||
| Flag to decide whether to store the source object on the Wayback Machine. Default False. | ||
|
|
||
| Returns | ||
| ------- | ||
| TechnologyCollection | ||
| A collection of Technology instances, each representing a unique | ||
| technology group with its associated parameters. | ||
|
|
||
| Notes | ||
| ----- | ||
| - The function groups the DataFrame by ["scenario", "year", "technology"] | ||
| - For each group, it creates a dictionary of Parameters | ||
| - Each Technology is instantiated with group-specific attributes | ||
|
|
||
| """ | ||
| list_techs = [] | ||
|
|
||
| if store_source: | ||
| source = Source( | ||
| title="Energy system technology data for the US", | ||
| authors="Contributors to technology-data. Data source: manual_input_usa.csv", | ||
| url="https://github.com/PyPSA/technology-data/blob/master/inputs/US/manual_input_usa.csv", | ||
| ) | ||
| source.ensure_in_wayback() | ||
| sources = SourceCollection(sources=[source]) | ||
| sources.to_json(sources_path) | ||
| else: | ||
| sources = SourceCollection.from_json(sources_path) | ||
|
|
||
| for (scenario, year, technology), group in dataframe.groupby( | ||
| ["scenario", "year", "technology"] | ||
| ): | ||
| parameters = {} | ||
| for _, row in group.iterrows(): | ||
| unit, carrier, heating_value = extract_units_carriers_heating_value( | ||
| row["unit"] | ||
| ) | ||
| param_kwargs = { | ||
| "magnitude": row["value"], | ||
| "sources": sources, | ||
| } | ||
| if carrier is not None: | ||
| param_kwargs["carrier"] = carrier | ||
| if heating_value is not None: | ||
| param_kwargs["heating_value"] = heating_value | ||
| if unit is not None: | ||
| param_kwargs["units"] = unit | ||
| if row["further_description"] is not None and isinstance( | ||
| row["further_description"], str | ||
| ): | ||
| param_kwargs["note"] = row["further_description"] | ||
| if row["financial_case"] is not None and isinstance( | ||
| row["financial_case"], str | ||
| ): | ||
| param_kwargs["provenance"] = str(row["financial_case"]) | ||
| parameters[row["parameter"]] = Parameter(**param_kwargs) | ||
| list_techs.append( | ||
| Technology( | ||
| name=technology, | ||
| region="US", | ||
| year=year, | ||
| parameters=parameters, | ||
| case=str(scenario), | ||
| detailed_technology=technology, | ||
| ) | ||
| ) | ||
|
|
||
| return TechnologyCollection(technologies=list_techs) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # Parse input arguments | ||
| input_args = Commons.parse_input_arguments( | ||
| description="Parse the technology_data manual_input_usa.csv dataset" | ||
| ) | ||
| logger.info("Command line arguments parsed.") | ||
|
|
||
| manual_input_usa_input_path = pathlib.Path( | ||
| path_cwd, | ||
| "src", | ||
| "technologydata", | ||
| "package_data", | ||
| "raw", | ||
| "manual_input_usa.csv", | ||
| ) | ||
|
|
||
| manual_input_usa_df = pandas.read_csv( | ||
| manual_input_usa_input_path, dtype=str, na_values="None" | ||
| ) | ||
| manual_input_usa_df["value"] = manual_input_usa_df["value"].astype(float) | ||
| manual_input_usa_df["scenario"] = manual_input_usa_df["scenario"].fillna( | ||
| "not_available" | ||
| ) | ||
|
|
||
| # Replace "per unit" with "%" and multiply val by 100 | ||
| mask_per_unit = manual_input_usa_df["unit"].str.contains("per unit") | ||
| manual_input_usa_df.loc[mask_per_unit, "unit"] = manual_input_usa_df.loc[ | ||
| mask_per_unit, "unit" | ||
| ].str.replace("per unit", "%") | ||
| manual_input_usa_df.loc[mask_per_unit, "value"] = ( | ||
| manual_input_usa_df.loc[mask_per_unit, "value"] * 100.0 | ||
| ).round(input_args.num_digits) | ||
| logger.info("`per unit` replaced by `%`. Corresponding value multiplied by 100.") | ||
|
|
||
| # Include currency_year in unit if applicable | ||
| manual_input_usa_df["unit"] = manual_input_usa_df.apply( | ||
| lambda row: Commons.update_unit_with_currency_year( | ||
| row["unit"], row["currency_year"] | ||
| ), | ||
| axis=1, | ||
| ) | ||
| logger.info("`currency_year` included in `unit` column.") | ||
|
|
||
| # Build TechnologyCollection | ||
| manual_input_usa_base_path = pathlib.Path( | ||
| path_cwd, | ||
| "src", | ||
| "technologydata", | ||
| "package_data", | ||
| "manual_input_usa", | ||
| ) | ||
| output_technologies_path = pathlib.Path( | ||
| manual_input_usa_base_path, | ||
| "technologies.json", | ||
| ) | ||
| output_sources_path = pathlib.Path( | ||
| manual_input_usa_base_path, | ||
| "sources.json", | ||
| ) | ||
|
|
||
| tech_col = build_technology_collection( | ||
| manual_input_usa_df, output_sources_path, store_source=input_args.store_source | ||
| ) | ||
|
|
||
| logger.info("TechnologyCollection object instantiated.") | ||
| tech_col.to_json(output_technologies_path) | ||
| logger.info("TechnologyCollection object exported to json.") | ||
12 changes: 12 additions & 0 deletions
12
src/technologydata/package_data/manual_input_usa/sources.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "sources": [ | ||
| { | ||
| "title": "Energy system technology data for the US", | ||
| "authors": "Contributors to technology-data. Data source: manual_input_usa.csv", | ||
| "url": "https://github.com/PyPSA/technology-data/blob/master/inputs/US/manual_input_usa.csv", | ||
|
finozzifa marked this conversation as resolved.
Outdated
|
||
| "url_archive": null, | ||
| "url_date": null, | ||
| "url_date_archive": null | ||
| } | ||
| ] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.