-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/update py psa reader #33
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
Open
scrappie1
wants to merge
38
commits into
main
Choose a base branch
from
feature/Update-PyPSA-Reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
5e9b9c3
Define mapping and functions for dPower_BusInfo and dPower_Network.
BenjaminStoeckl 73f92e5
Define mapping and functions for dPower_ThermalGen.
BenjaminStoeckl 1f7553f
Add mapping_config.yaml.
BenjaminStoeckl 3bcc8b2
Update mapping functions.
BenjaminStoeckl 6889f93
Add functionality of fuel cost for thermal generators.
BenjaminStoeckl 8a35990
Improve code structure.
BenjaminStoeckl bb30bab
Add transformers to line conversions.
BenjaminStoeckl 79dda95
Use predefined tables for dataframes.
BenjaminStoeckl 05abafd
Add functionality to translate whole case studies in the file PypsaRe…
BenjaminStoeckl 0e77ea6
Move main functions to standalone function.
BenjaminStoeckl 7df6bac
Add docstring comments to pypsa_helper.py.
BenjaminStoeckl be89576
Add checker for parallel lines and transformers.
BenjaminStoeckl 2b13602
Add 'DC' to bus carriers.
BenjaminStoeckl cc1e86d
Add demand of 0 for buses without specified demand.
BenjaminStoeckl e020ef4
Change k index to standard format of k0001 -> k0002.
BenjaminStoeckl a1270e1
Add PypsaReaderDocumentation.md.
BenjaminStoeckl 1440743
Add bus filter to demand consider same buses, when creating the demand.
BenjaminStoeckl 7e9daf2
Add a default_storage_capacity_cost to avoid NaN values in the optimi…
BenjaminStoeckl 70ecec2
Set MinReserve of storage in mapping_config.yaml to 0.
BenjaminStoeckl f667840
Fill NaN values in inflow profiles.
BenjaminStoeckl e8f9a2c
Fill pRampUp and pRampDown with pmax if not specified.
BenjaminStoeckl 6f0a394
Add handling of units with zero installed capacity.
BenjaminStoeckl e5d0eeb
Adapt handling of units with zero installed capacity.
BenjaminStoeckl a04f71c
Apply ruff formatting and checking.
BenjaminStoeckl a489076
Add default_storage_capacity_cost for hydro units.
BenjaminStoeckl 2aa081b
Correct name of RampDw.
BenjaminStoeckl 41e1143
Add ExisUnits validation to ThermalGen and Storage.
BenjaminStoeckl cef4f57
Add per unit conversion for AC line parameters.
BenjaminStoeckl 5e189d2
Add per unit conversion for trafos.
BenjaminStoeckl 1c94a0a
Add ror to VRES technologies category.
BenjaminStoeckl c53c6b5
Add clipping of generator name to less than 100 characters.
BenjaminStoeckl 14c2050
Define line, link & transformer names individual to ensure compatibil…
BenjaminStoeckl 1356b26
Update the documentation and rename mapping file for improved underst…
BenjaminStoeckl 0189a55
Increase character length of the index from 100 to 450.
BenjaminStoeckl 690dfe9
Add guide on how to translate from pypsa to lego, add to environment,…
scrappie1 f37e5d6
Move pypsa files into dedicated PyPSA folder, replace plain prints, m…
scrappie1 952ed66
Merge branch 'main' into feature/Update-PyPSA-Reader
scrappie1 9dd037c
Remove link to remote desktop from pypsa readme
scrappie1 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,164 @@ | ||
| # How to translate a PyPSA-Eur dataset to LEGO | ||
|
|
||
| This guide explains how to convert an existing PyPSA-Eur network file (`.nc`) to LEGO Excel files. | ||
|
|
||
| To build the PyPSA-Eur dataset first, follow | ||
| [How to create a PyPSA-Eur dataset](https://github.com/IEE-TUGraz/pypsa-eur-multivoltage/blob/fix/corine-bool-and-offshore-keyerror/README_pypsa_dataset.md) | ||
| in the `pypsa-eur-multivoltage` repository. | ||
|
|
||
| Since the conversion can require a lot of memory and may also take some time, it is recommended to use a powerful | ||
| machine. | ||
|
|
||
| ## 1. Set the mapping metadata | ||
|
|
||
| In `pypsa_lego_mapping_config.yaml`, update the dataset metadata fields `dataPackage` and `dataSource`. | ||
|
|
||
| Use the name of the PyPSA-Eur dataset for `dataSource`. | ||
|
|
||
| Example: | ||
|
|
||
| ```yaml | ||
| Metadata: | ||
| dataPackage: "PyPSA-Eur" | ||
| dataSource: "PyPSA-Eur_2025_CY2013" | ||
| ``` | ||
|
|
||
| ## 2. Translate to LEGO | ||
|
|
||
| From the `PyPSA/` folder, run the converter with: | ||
|
|
||
| ``` | ||
| python PypsaReader.py <input directory> <input file> <output directory> <output folder name> | ||
| ``` | ||
|
|
||
| Example: | ||
|
|
||
| ``` | ||
| python PypsaReader.py "../../pypsa-eur-multivoltage/resources/PyPSA-Eur_2025_CY2013/networks" "base_s_all_elec.nc" "../../" "LEGO_PyPSA-Eur_2025_CY2013" | ||
| ``` | ||
|
|
||
| This will write the LEGO Excel files to: | ||
|
|
||
| ``` | ||
| ../../LEGO_PyPSA-Eur_2025_CY2013 | ||
| ``` | ||
|
|
||
| ## About the generated files | ||
|
|
||
| The converter generates the following LEGO files: | ||
|
|
||
| - `Power_BusInfo.xlsx` | ||
| - `Power_Demand.xlsx` | ||
| - `Power_Inflows.xlsx` | ||
| - `Power_Network.xlsx` | ||
| - `Power_Storage.xlsx` | ||
| - `Power_ThermalGen.xlsx` | ||
| - `Power_VRES.xlsx` | ||
| - `Power_VRESProfiles.xlsx` | ||
|
|
||
| The following files are not generated and have to be added and adapted manually: | ||
|
|
||
| - `Data_Packages.xlsx` | ||
| - `Data_Sources.xlsx` | ||
| - `Global_Parameters.xlsx` | ||
| - `Global_Scenarios.xlsx` | ||
| - `Power_Hindex.xlsx` | ||
| - `Power_Parameters.xlsx` | ||
| - `Power_WeightsK.xlsx` | ||
| - `Power_WeightsRP.xlsx` | ||
|
|
||
| # Reader Documentation | ||
|
|
||
| The `PypsaReader` module provides a specialized pipeline for converting PyPSA (Python for Power System Analysis) | ||
| networks into the Excel-based data | ||
| format required by the LEGO model. | ||
|
|
||
| ## Purpose & limitations | ||
|
|
||
| - This reader only converts PyPSA networks to LEGO input files (EXCEL). It does not return a LEGO CaseStudy object. | ||
| - For running the converted case study in LEGO, at least the files `Global_Parameters.xlsx`, `Power_Parameters.xlsx`, | ||
| `Power_Hindex.xlsx`, | ||
| `Power_WeightsK.xlsx` and `Power_WeightsRP.xlsx` must be defined. | ||
| - Currently, only the power sector is supported by the converter. | ||
| - For importing the generated Excel files into the LEGO database, the files `Global_Scenarios.xlsx`, | ||
| `Data_Packages.xlsx` and `Data_Sources.xlsx` must | ||
| be defined. | ||
|
|
||
| ## General Workflow | ||
|
|
||
| The conversion process follows a structured workflow: | ||
|
|
||
| 1. **Network Loading**: A PyPSA network is loaded from a NetCDF (`.nc`) file. | ||
| 2. **Configuration Parsing**: The `NetworkDataExtractor` reads `pypsa_lego_mapping_config.yaml` to determine how PyPSA | ||
| components (buses, lines, | ||
| generators, | ||
| etc.) map to LEGO tables. | ||
| 3. **Data Extraction & Transformation**: | ||
| - **Source Retrieval**: Data is pulled either directly from PyPSA network attributes (e.g., `net.buses`) or via | ||
| complex processing in | ||
| `pypsa_helper.py`. | ||
| - **Filtering**: Technologies are filtered based on their `carrier` attribute using definitions in the `Metadata` | ||
| section of the config. | ||
| - **Column Mapping**: PyPSA attributes are mapped to LEGO column names. | ||
| - **Unit Conversion**: The `Conversions` class applies transformations (e.g., MW to kW, EUR to MEUR, or calculating | ||
| decommissioning years). | ||
| 4. **Normalization**: DataFrames are augmented with mandatory LEGO columns (`scenario`, `id`, `dataPackage`, | ||
| `dataSource`) and aligned with | ||
| definitions in `TableDefinitions.xml`. | ||
| 5. **Excel Export**: The `ExcelWriter` saves each processed DataFrame into individual `.xlsx` files. <br> **Warning:** | ||
| This can take up to several | ||
| hours for very | ||
| large networks! | ||
|
|
||
| ## Configuration File (`pypsa_lego_mapping_config.yaml`) | ||
|
|
||
| The YAML configuration acts as the "translation map" between the two models. | ||
|
|
||
| ### Metadata Section | ||
|
|
||
| * **Technology Filters**: Defines list of `carrier` strings that identify specific technologies (e.g., | ||
| `solar: filter: ['solar', 'Solar']`). | ||
| * **Fuel Costs**: Specifies fuel prices in EUR/MWh for thermal technologies. | ||
| * **Categories**: Groups individual technologies into broader LEGO categories like `ThermalGen` or `VRES` for batch | ||
| processing. | ||
| * **Global Settings**: Defines `dataPackage`, `dataSource`, and thresholds like `LDES_threshold`. | ||
|
|
||
| ### Table Mapping Section | ||
|
|
||
| Each entry (e.g., `dPower_ThermalGen`) defines: | ||
|
|
||
| * **source**: The data origin (`type: attribute` for direct PyPSA access or `type: helper` for function calls). | ||
| * **category**: (Optional) References a metadata category to automatically filter the source data. | ||
| * **index**: Defines the LEGO index columns (e.g., `i` for bus, `g` for generator). | ||
| * **mapping**: A dictionary where keys are LEGO columns and values are either: | ||
| - A direct PyPSA attribute name. | ||
| - A static value (int/float). | ||
| - A dictionary specifying an `attr`, a `conversion` function, or a `factor`. | ||
|
|
||
| ## Helper Functions (`pypsa_helper.py`) | ||
|
|
||
| When simple attribute mapping is insufficient, helper functions handle complex data aggregation and profile generation: | ||
|
|
||
| * **Network Topology**: | ||
| - `prepare_ac_lines_and_dc_links`: Combines PyPSA `lines`, `links` (filtered for DC), and `transformers` into a | ||
| single LEGO `Power_Network` table. | ||
| - `prepare_ac_lines` / `prepare_transformers`: Calculate missing electrical parameters ($r, x, b$) based on standard | ||
| type definitions if they are | ||
| not explicitly set. | ||
| * **Time-Series Profiles**: | ||
| - `prepare_renewable_profiles`: Extracts `p_max_pu` for VRES generators and reshapes it into a long-form LEGO | ||
| format. | ||
| - `prepare_inflow_profiles`: Aggregates inflow data from both `storage_units` (hydro) and `generators` ( | ||
| Run-of-River) into a unified time-series. | ||
| - `prepare_demand_profiles`: Sums PyPSA load data per bus and formats it for the LEGO demand table. | ||
|
|
||
| ## Conversions & Logic | ||
|
|
||
| The `Conversions` class in `PypsaReader.py` contains static methods for specialized logic: | ||
|
|
||
| * **Unit Scaling**: `EUR_to_MEUR`, `MW_to_kW`, `V_to_kV`. | ||
| * **Calculated Values**: `year_and_lifetime_to_year_decom` (derives decommissioning date) and | ||
| `total_capacity_to_number_of_units`. | ||
| * **LEGO Specifics**: `is_ldes` (Long Duration Energy Storage detection) and `line_carrier_to_tec_repr` (mapping | ||
| carriers to LEGO technical | ||
| representations like `DC-OPF` or `TP`). | ||
Oops, something went wrong.
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.
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.
Probably we should extend the description of the config file depending on the aimed use. I can do this, but maybe we should discuss first.