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

save simbev metadata #978

Merged
merged 13 commits into from
Oct 31, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ Added
`#907 <https://github.com/openego/eGon-data/issues/907>`_
* Add wind off shore power plants for eGon100RE
`#868 <https://github.com/openego/eGon-data/issues/868>`_
* Write simBEV metadata to DB table
`PR #978 <https://github.com/openego/eGon-data/pull/978>`_

.. _PR #159: https://github.com/openego/eGon-data/pull/159
.. _PR #703: https://github.com/openego/eGon-data/pull/703
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,16 @@

from egon.data import db, subprocess
from egon.data.datasets import Dataset
from egon.data.datasets.emobility.motorized_individual_travel.db_classes import (
from egon.data.datasets.emobility.motorized_individual_travel.db_classes import ( # noqa: E501
khelfen marked this conversation as resolved.
Show resolved Hide resolved
EgonEvCountMunicipality,
EgonEvCountMvGridDistrict,
EgonEvCountRegistrationDistrict,
EgonEvMetadata,
EgonEvMvGridDistrict,
EgonEvPool,
EgonEvTrip,
)
from egon.data.datasets.emobility.motorized_individual_travel.ev_allocation import (
from egon.data.datasets.emobility.motorized_individual_travel.ev_allocation import ( # noqa: E501
allocate_evs_numbers,
allocate_evs_to_grid_districts,
)
Expand All @@ -105,11 +106,12 @@
TRIP_COLUMN_MAPPING,
WORKING_DIR,
)
from egon.data.datasets.emobility.motorized_individual_travel.model_timeseries import (
from egon.data.datasets.emobility.motorized_individual_travel.model_timeseries import ( # noqa: E501
delete_model_data_from_db,
generate_model_data_bunch,
generate_model_data_eGon100RE_remaining,
generate_model_data_eGon2035_remaining,
read_simbev_metadata_file,
)


Expand Down Expand Up @@ -152,6 +154,8 @@ def create_tables():
EgonEvTrip.__table__.create(bind=engine, checkfirst=True)
EgonEvMvGridDistrict.__table__.drop(bind=engine, checkfirst=True)
EgonEvMvGridDistrict.__table__.create(bind=engine, checkfirst=True)
EgonEvMetadata.__table__.drop(bind=engine, checkfirst=True)
EgonEvMetadata.__table__.create(bind=engine, checkfirst=True)

# Create dir for results, if it does not exist
result_dir = WORKING_DIR / Path("results")
Expand Down Expand Up @@ -357,6 +361,41 @@ def import_csv(f):
os.remove(trip_file)


def write_metadata_to_db():
"""
Write used SimBEV metadata per scenario to database.
"""
dtypes = {
"scenario": str,
"eta_cp": float,
"stepsize": int,
"start_date": np.datetime64,
"end_date": np.datetime64,
"soc_min": float,
"grid_timeseries": bool,
"grid_timeseries_by_usecase": bool,
}

for scenario_name in ["eGon2035", "eGon100RE"]:
meta_run_config = read_simbev_metadata_file(
scenario_name, "config"
).loc["basic"]

meta_run_config = (
meta_run_config.to_frame()
.T.assign(scenario=scenario_name)[dtypes.keys()]
.astype(dtypes)
)

meta_run_config.to_sql(
name=EgonEvMetadata.__table__.name,
schema=EgonEvMetadata.__table__.schema,
con=db.engine(),
if_exists="append",
index=False,
)


class MotorizedIndividualTravel(Dataset):
def __init__(self, dependencies):
def generate_model_data_tasks(scenario_name):
Expand Down Expand Up @@ -414,13 +453,20 @@ def generate_model_data_tasks(scenario_name):

super().__init__(
name="MotorizedIndividualTravel",
version="0.0.4",
version="0.0.6",
dependencies=dependencies,
tasks=(
create_tables,
{
(download_and_preprocess, allocate_evs_numbers),
(extract_trip_file, write_evs_trips_to_db),
(
download_and_preprocess,
allocate_evs_numbers,
),
(
extract_trip_file,
write_metadata_to_db,
write_evs_trips_to_db,
),
},
allocate_evs_to_grid_districts,
delete_model_data_from_db,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
DB tables / SQLAlchemy ORM classes for motorized individual travel
"""

from sqlalchemy import ( # ForeignKeyConstraint,
from sqlalchemy import (
Boolean,
Column,
DateTime,
Float,
ForeignKey,
Integer,
SmallInteger,
Expand Down Expand Up @@ -232,3 +235,19 @@ class EgonEvMvGridDistrict(Base):
egon_ev_pool_ev_id = Column(Integer, nullable=False)

# ev = relationship("EgonEvPool", back_populates="mvgds")


class EgonEvMetadata(Base):
"""List of EV Pool Metadata"""

__tablename__ = "egon_ev_metadata"
__table_args__ = {"schema": "demand"}

scenario = Column(String, primary_key=True)
eta_cp = Column(Float)
stepsize = Column(Integer)
start_date = Column(DateTime)
end_date = Column(DateTime)
soc_min = Column(Float)
grid_timeseries = Column(Boolean)
grid_timeseries_by_usecase = Column(Boolean)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import pandas as pd

from egon.data import db
from egon.data.datasets.emobility.motorized_individual_travel.db_classes import (
from egon.data.datasets.emobility.motorized_individual_travel.db_classes import ( # noqa: E501
EgonEvMvGridDistrict,
EgonEvPool,
EgonEvTrip,
Expand All @@ -60,8 +60,6 @@
)
from egon.data.datasets.mv_grid_districts import MvGridDistricts

# from egon.data.datasets.scenario_parameters import get_sector_parameters


def data_preprocessing(
scenario_data: pd.DataFrame, ev_data_df: pd.DataFrame
Expand Down Expand Up @@ -93,7 +91,7 @@ def data_preprocessing(
# charging capacity in MVA
ev_data_df = ev_data_df.assign(
charging_capacity_grid_MW=(
ev_data_df.charging_capacity_grid / 10 ** 3
ev_data_df.charging_capacity_grid / 10**3
),
minimum_charging_time=(
ev_data_df.charging_demand
Expand Down Expand Up @@ -619,7 +617,7 @@ def write_link(scenario_name: str) -> None:
carrier="BEV charger",
efficiency=float(run_config.eta_cp),
p_nom=(
load_time_series_df.simultaneous_plugged_in_charging_capacity.max()
load_time_series_df.simultaneous_plugged_in_charging_capacity.max() # noqa: E501
),
p_nom_extendable=False,
p_nom_min=0,
Expand All @@ -641,7 +639,7 @@ def write_link(scenario_name: str) -> None:
temp_id=1,
p_min_pu=None,
p_max_pu=(
hourly_load_time_series_df.ev_availability.to_list()
hourly_load_time_series_df.ev_availability.to_list() # noqa: E501
),
)
)
Expand Down Expand Up @@ -744,7 +742,7 @@ def write_load(
scenario_name=scenario_name,
connection_bus_id=emob_bus_id,
load_ts=(
hourly_load_time_series_df.driving_load_time_series.to_list()
hourly_load_time_series_df.driving_load_time_series.to_list() # noqa: E501
),
)
else:
Expand Down