Skip to content
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

Add csv-demand parser #995

Merged
merged 52 commits into from
Jun 17, 2024
Merged
Changes from 9 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
0f5f09a
Account for the file extension
ekatef Mar 20, 2024
849c092
Add import
ekatef Mar 21, 2024
cbe0dce
Fix format of the message
ekatef Mar 22, 2024
a3b2f8c
Add csv loading
ekatef Mar 22, 2024
6470034
Refactor path generation
ekatef Mar 22, 2024
0ff2282
Merge branch 'main' into add_demand_parser
ekatef Mar 22, 2024
7eb36b4
Fix the check of demand availability
ekatef Mar 23, 2024
1d9a250
Merge branch 'add_demand_parser' of https://github.com/ekatef/pypsa-e…
ekatef Mar 23, 2024
5504b79
Comment-out the load path check
ekatef Mar 23, 2024
e12853e
Merge branch 'pypsa-meets-earth:main' into add_demand_parser
ekatef Apr 11, 2024
c37bf1b
Implement Davide's suggestion
ekatef Apr 11, 2024
397a6d9
Add a TODO comment
ekatef Apr 11, 2024
061c985
Put loading csv-s into a function
ekatef Apr 11, 2024
9464d55
Use na-safe csv loading
ekatef Apr 11, 2024
8c27cd5
Fix the function output
ekatef Apr 11, 2024
59fe870
Add an option to combine nc and csv inputs
ekatef Apr 11, 2024
a83693c
Merge branch 'main' into add_demand_parser
ekatef Apr 29, 2024
94378e5
Fix breaking the loop
ekatef Apr 29, 2024
bc66e5d
Merge branch 'main' into add_demand_parser
ekatef May 7, 2024
87fb099
Put the path check back
ekatef May 7, 2024
37c62ce
Add a string conversion
ekatef May 8, 2024
8650b7c
Add a test for paths checked
ekatef May 8, 2024
827deb9
Replace an error with a warning
ekatef May 8, 2024
8b031fe
Add a diagnostic info
ekatef May 8, 2024
0aabc66
Define load_paths to snakemake params
ekatef May 8, 2024
065a9cd
Fix structure
ekatef May 13, 2024
8c2c2aa
Fix the info message
ekatef May 13, 2024
cfb5432
Update the error message
ekatef May 13, 2024
5f383dd
Revert "Define load_paths to snakemake params"
ekatef May 25, 2024
4c5974d
Add an existence check
ekatef May 25, 2024
e63f6b3
Return default nc-path definition
ekatef May 25, 2024
7622818
Apply changes to the path definition
ekatef May 25, 2024
5b488b2
Remove a redundant definition
ekatef May 28, 2024
083a1ac
Add type conversion
ekatef May 28, 2024
b71eeea
Add a (temporary) warning
ekatef May 28, 2024
eaf8552
Merge branch 'main' into add_demand_parser
ekatef May 28, 2024
31eec33
Add a diagnostic logger info
ekatef May 28, 2024
9482e28
Revert "Remove a redundant definition"
ekatef May 29, 2024
18df6d3
Account for different states of the demand inputs
ekatef May 29, 2024
049aaa1
Modify handling an empty demand case
ekatef May 29, 2024
8e34213
Add release note
ekatef May 29, 2024
511140d
Remove temporary comments
ekatef May 29, 2024
25451d7
Add documentation
ekatef May 29, 2024
c1c80b4
Merge branch 'main' into add_demand_parser
ekatef Jun 4, 2024
0584f17
Move existence check into the demand parser
ekatef Jun 4, 2024
00b8d60
Clean-up implementation
ekatef Jun 4, 2024
5c8ce7f
Remove an outdate commit
ekatef Jun 4, 2024
4fadf74
Implement Davide's suggestion
ekatef Jun 6, 2024
d21bbe6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 6, 2024
c6aa7f1
Fix formatting
ekatef Jun 6, 2024
14f4bca
Remove an outdated check
ekatef Jun 6, 2024
ef4dc69
Remove a not-used variable
ekatef Jun 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 60 additions & 11 deletions scripts/build_demand_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"""
import logging
import os
import os.path
from itertools import product

import geopandas as gpd
Expand Down Expand Up @@ -109,15 +110,29 @@ def get_load_paths_gegis(ssp_parentfolder, config):
ssp = config.get("load_options")["ssp"]

load_paths = []
for continent in region_load:
load_path = os.path.join(
ssp_parentfolder,
str(ssp),
str(prediction_year),
"era5_" + str(weather_year),
str(continent) + ".nc",
)
load_paths.append(load_path)
load_dir = os.path.join(
ssp_parentfolder,
str(ssp),
str(prediction_year),
"era5_" + str(weather_year),
)

for ext in [".nc", ".csv"]:
avail_regions = [
cnt
for cnt in region_load
if os.path.exists(os.path.join(load_dir, cnt + ext))
]
if len(avail_regions) == len(region_load):
for continent in region_load:
load_path = os.path.join(load_dir, continent + ext)
load_paths.append(load_path)
break
ekatef marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, move it inside the if, otherwise the .csv never gets executed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thanks :)


# if len(load_paths) == 0:
davide-f marked this conversation as resolved.
Show resolved Hide resolved
# raise AssertionError(
# f"No demand data file for {set(region_load).difference(avail_regions)}"
# )

return load_paths

Expand Down Expand Up @@ -176,8 +191,42 @@ def build_demand_profiles(
substation_lv_i = n.buses.index[n.buses["substation_lv"]]
regions = gpd.read_file(regions).set_index("name").reindex(substation_lv_i)
load_paths = load_paths
# Merge load .nc files: https://stackoverflow.com/questions/47226429/join-merge-multiple-netcdf-files-using-xarray
gegis_load = xr.open_mfdataset(load_paths, combine="nested")

if str(load_paths[0]).endswith(".csv"):
davide-f marked this conversation as resolved.
Show resolved Hide resolved
df = pd.concat(
[
pd.read_csv(
pth,
sep=";",
dtype={
"region_code": str,
"time": str,
"region_name": str,
"Electricity demand": np.float64,
},
keep_default_na=False,
na_values=["_", ""],
)
for pth in load_paths
],
axis=0,
)
df.time = pd.to_datetime(df.time, format="%Y-%m-%d %H:%M:%S")
load_regions = {c: n for c, n in zip(df.region_code, df.region_name)}

gegis_load = df.set_index(["region_code", "time"]).to_xarray()
gegis_load = gegis_load.assign_coords(
{
"region_name": (
"region_code",
[name for (code, name) in load_regions.items()],
)
}
)
else:
# Merge load .nc files: https://stackoverflow.com/questions/47226429/join-merge-multiple-netcdf-files-using-xarray
gegis_load = xr.open_mfdataset(load_paths, combine="nested")

gegis_load = gegis_load.to_dataframe().reset_index().set_index("time")
davide-f marked this conversation as resolved.
Show resolved Hide resolved
# filter load for analysed countries
gegis_load = gegis_load.loc[gegis_load.region_code.isin(countries)]
Expand Down
Loading