Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Aug 5, 2024
1 parent f1e76d0 commit fe864ef
Show file tree
Hide file tree
Showing 23 changed files with 852 additions and 248 deletions.
724 changes: 682 additions & 42 deletions dev/Untitled1.ipynb

Large diffs are not rendered by default.

97 changes: 70 additions & 27 deletions premise/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def _update_battery(scenario, version, system_model):

battery.adjust_battery_mass()

if battery.iam_data.battery_scenarios is not None:
if (
battery.iam_data.battery_mobile_scenarios is not None
or battery.iam_data.battery_stationary_scenarios is not None
):
battery.adjust_battery_market_shares()

scenario["database"] = battery.database
Expand Down Expand Up @@ -98,14 +101,19 @@ def adjust_battery_market_shares(self) -> None:
- market for battery capacity (PLiB scenario)
"""

market_datasets = {
market_datasets_mobile = {
"market for battery capacity (MIX scenario)": "MIX",
"market for battery capacity (LFP scenario)": "LFP",
"market for battery capacity (NCx scenario)": "NCX",
"market for battery capacity (PLiB scenario)": "PLIB",
}

datasets_mapping = {
market_datasets_stationary = {
"market for battery capacity, stationary (CONT scenario)": "cont",
"market for battery capacity, stationary (TC scenario)": "tc",
}

datasets_mapping_mobile = {
v: k
for k, v in {
"LAB": "market for battery capacity, Li-ion, Li-O2",
Expand All @@ -121,28 +129,54 @@ def adjust_battery_market_shares(self) -> None:
}.items()
}

datasets_mapping_stationary = {
v: k
for k, v in {
"LFP": "market for battery capacity, Li-ion, LFP, stationary",
"NMC111": "market for battery capacity, Li-ion, NMC111, stationary",
"NMC622": "market for battery capacity, Li-ion, NMC622, stationary",
"NMC811": "market for battery capacity, Li-ion, NMC811, stationary",
"VRFB": "market for battery capacity, redox-flow, Vanadium, stationary",
"LEAD-ACID": "market for battery capacity, lead acid, rechargeable, stationary",
}.items()
}

self._adjust_shares(
market_datasets_stationary, datasets_mapping_stationary, "stationary"
)
self._adjust_shares(market_datasets_mobile, datasets_mapping_mobile, "mobile")

def _adjust_shares(self, market_datasets, datasets_mapping, market_type):
"""
Adjust the shares within the datasets.
"""
if market_type == "mobile":
battery_scenarios = self.iam_data.battery_mobile_scenarios
else:
battery_scenarios = self.iam_data.battery_stationary_scenarios

for ds in ws.get_many(
self.database,
ws.either(*[ws.equals("name", name) for name in market_datasets]),
):

if self.year in self.iam_data.battery_scenarios.year:
shares = self.iam_data.battery_scenarios.sel(
if self.year in battery_scenarios.year:
shares = battery_scenarios.sel(
scenario=market_datasets[ds["name"]],
year=self.year,
)
elif self.year < min(self.iam_data.battery_scenarios.year):
shares = self.iam_data.battery_scenarios.sel(
elif self.year < min(battery_scenarios.year):
shares = battery_scenarios.sel(
scenario=market_datasets[ds["name"]],
year=min(self.iam_data.battery_scenarios.year),
year=min(battery_scenarios.year),
)
elif self.year > max(self.iam_data.battery_scenarios.year):
shares = self.iam_data.battery_scenarios.sel(
elif self.year > max(battery_scenarios.year):
shares = battery_scenarios.sel(
scenario=market_datasets[ds["name"]],
year=max(self.iam_data.battery_scenarios.year),
year=max(battery_scenarios.year),
)
else:
shares = self.iam_data.battery_scenarios.sel(
shares = battery_scenarios.sel(
scenario=market_datasets[ds["name"]],
).interp(year=self.year)

Expand All @@ -162,7 +196,7 @@ def adjust_battery_market_shares(self) -> None:
f"{datasets_mapping[exc['name']]} market share"
] = exc["amount"]

self.write_log(ds, status="modified")
self.write_log(ds, status=f"modified ({market_type})")

def adjust_battery_mass(self) -> None:
"""
Expand Down Expand Up @@ -258,25 +292,34 @@ def adjust_battery_mass(self) -> None:

self.write_log(ds, status="modified")

def write_log(self, dataset, status="created"):
def write_log(self, dataset, status):
"""
Write log file.
"""

log_params = dataset.get("log parameters", {})
battery_input = log_params.get("battery input", "")
old_battery_mass = log_params.get("old battery mass", "")
new_battery_mass = log_params.get("new battery mass", "")

shares = [
log_params.get("NMC111 market share", ""),
log_params.get("NMC532 market share", ""),
log_params.get("NMC622 market share", ""),
log_params.get("NMC811 market share", ""),
log_params.get("NMC900-Si market share", ""),
log_params.get("LFP market share", ""),
log_params.get("NCA market share", ""),
log_params.get("LAB market share", ""),
log_params.get("LSB market share", ""),
log_params.get("SIB market share", ""),
log_params.get("VRFB market share", ""),
log_params.get("LEAD-ACID market share", ""),
]

logger.info(
f"{status}|{self.model}|{self.scenario}|{self.year}|"
f"{dataset['name']}|{dataset['location']}|"
f"{dataset.get('log parameters', {}).get('battery input', '')}|"
f"{dataset.get('log parameters', {}).get('old battery mass', '')}|"
f"{dataset.get('log parameters', {}).get('new battery mass', '')}|"
f"{dataset.get('log parameters', {}).get('NMC111 market share', '')}|"
f"{dataset.get('log parameters', {}).get('NMC532 market share', '')}|"
f"{dataset.get('log parameters', {}).get('NMC622 market share', '')}|"
f"{dataset.get('log parameters', {}).get('NMC811 market share', '')}|"
f"{dataset.get('log parameters', {}).get('NMC900-Si market share', '')}|"
f"{dataset.get('log parameters', {}).get('LFP market share', '')}|"
f"{dataset.get('log parameters', {}).get('NCA market share', '')}|"
f"{dataset.get('log parameters', {}).get('LAB market share', '')}|"
f"{dataset.get('log parameters', {}).get('LSB market share', '')}|"
f"{dataset.get('log parameters', {}).get('SIB market share', '')}"
f"{battery_input}|{old_battery_mass}|{new_battery_mass}|"
f"{'|'.join(map(str, shares))}"
)
Binary file modified premise/data/additional_inventories/lci-PV-GaAs.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-PV-perovskite.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-battery-capacity.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-buses.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-pass_cars.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-trucks.xlsx
Binary file not shown.
Binary file modified premise/data/additional_inventories/lci-two_wheelers.xlsx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scenario,chemistry,year,value
scenario,chemistry,year,value
MIX,NCA,2010,0.1
MIX,NMC111,2010,0.3
MIX,NMC532,2010,0.38
Expand Down
2 changes: 1 addition & 1 deletion premise/data/consequential/leadtimes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Wind Onshore: 2
Wind Offshore: 3
Wave: 3
Storage, Hydrogen: 3
Storage, Flow Battery: 3
Storage, Battery: 3
diesel: 3
gasoline: 3
diesel, synthetic, from wood: 3
Expand Down
2 changes: 1 addition & 1 deletion premise/data/consequential/lifetimes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ daccs_sorbent: 20
biomass - residual: 35
biomass crops - purpose grown: 35
Storage, Hydrogen: 20
Storage, Flow Battery: 20
Storage, Battery: 20
8 changes: 8 additions & 0 deletions premise/data/utils/logging/reporting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,14 @@ premise_battery:
name: SIB market share
description: Market share of SIB
unit: 0-1
VRFB market share:
name: VRFB market share
description: Market share of VRFB
unit: 0-1
LEAD-ACID market share:
name: LEAD-ACID market share
description: Market share of LEAD-ACID
unit: 0-1
tab: Battery

premise_transport:
Expand Down
69 changes: 61 additions & 8 deletions premise/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
IAM_TRANS_BUS_VARS = VARIABLES_DIR / "transport_bus_variables.yaml"
IAM_TRANS_TWO_WHEELERS_VARS = VARIABLES_DIR / "transport_two_wheelers_variables.yaml"

# TODO: use IAM_TRANS_VAR also for road transport. Delete _FLEET_COMP files afterwards.
VEHICLES_MAP = DATA_DIR / "transport" / "vehicles_map.yaml"
IAM_CARBON_CAPTURE_VARS = VARIABLES_DIR / "carbon_capture_variables.yaml"
CROPS_PROPERTIES = VARIABLES_DIR / "crops_variables.yaml"
GAINS_GEO_MAP = VARIABLES_DIR / "gains_regions_mapping.yaml"
COAL_POWER_PLANTS_DATA = DATA_DIR / "electricity" / "coal_power_emissions_2012_v1.csv"
BATTERY_SCENARIO_DATA = DATA_DIR / "battery" / "scenarios.csv"
BATTERY_MOBILE_SCENARIO_DATA = DATA_DIR / "battery" / "mobile_scenarios.csv"
BATTERY_STATIONARY_SCENARIO_DATA = DATA_DIR / "battery" / "stationary_scenarios.csv"


def print_missing_variables(missing_vars):
Expand Down Expand Up @@ -820,18 +820,23 @@ def __init__(

self.coal_power_plants = self.fetch_external_data_coal_power_plants()

self.battery_scenarios = self.fetch_external_data_battery_scenarios()
self.battery_mobile_scenarios = (
self.fetch_external_data_battery_mobile_scenarios()
)
self.battery_stationary_scenarios = (
self.fetch_external_data_battery_stationary_scenarios()
)

def fetch_external_data_battery_scenarios(self):
def fetch_external_data_battery_mobile_scenarios(self):
"""
Fetch external data on battery scenarios.
Fetch external data on mobile battery scenarios.
"""
if not BATTERY_SCENARIO_DATA.is_file():
if not BATTERY_MOBILE_SCENARIO_DATA.is_file():
return None

data = pd.read_csv(
BATTERY_SCENARIO_DATA,
delimiter=get_delimiter(filepath=BATTERY_SCENARIO_DATA),
BATTERY_MOBILE_SCENARIO_DATA,
delimiter=get_delimiter(filepath=BATTERY_MOBILE_SCENARIO_DATA),
low_memory=False,
na_filter=False,
)
Expand Down Expand Up @@ -863,6 +868,54 @@ def fetch_external_data_battery_scenarios(self):
.to_xarray()
)

def fetch_external_data_battery_stationary_scenarios(
self, exclude_chemistries=["NAS"]
):
"""
Fetch external data on stationary battery scenarios.
"""

if not BATTERY_STATIONARY_SCENARIO_DATA.is_file():
return None

data = pd.read_csv(
BATTERY_STATIONARY_SCENARIO_DATA,
delimiter=get_delimiter(filepath=BATTERY_STATIONARY_SCENARIO_DATA),
low_memory=False,
na_filter=False,
)

if exclude_chemistries is not None:
data = data[~data["chemistry"].isin(exclude_chemistries)]

grouped_data = (
data.groupby(
[
"scenario",
"chemistry",
"year",
]
)["value"]
.sum()
.reset_index()
)

total_shares = (
grouped_data.groupby(["scenario", "year"])["value"].sum().reset_index()
)
total_shares = total_shares.rename(columns={"value": "total_share"})

merged_data = pd.merge(grouped_data, total_shares, on=["scenario", "year"])

# Scale the remaining shares so that they sum up to one
merged_data["scaled_value"] = merged_data["value"] / merged_data["total_share"]

xarray_data = merged_data.set_index(["scenario", "chemistry", "year"])[
"scaled_value"
].to_xarray()

return xarray_data

def __get_iam_variable_labels(
self, filepath: Path, variable: str
) -> Dict[str, Union[str, List[str]]]:
Expand Down
4 changes: 2 additions & 2 deletions premise/electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ def create_region_specific_power_plants(self):
# "Oil CC",
"Coal CF 80-20",
"Coal CF 50-50",
"Storage, Flow Battery",
"Storage, Battery",
"Storage, Hydrogen",
]

Expand Down Expand Up @@ -1550,7 +1550,7 @@ def create_region_specific_power_plants(self):
ws.either(
*[ws.contains("name", name) for name in list_datasets_to_duplicate]
),
ws.exclude(ws.contains("name", "market")),
#ws.exclude(ws.contains("name", "market")),
ws.exclude(ws.contains("name", ", oxy, ")),
ws.exclude(ws.contains("name", ", pre, ")),
):
Expand Down
2 changes: 1 addition & 1 deletion premise/iam_variables_mapping/electricity_variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ Solar PV Residential:
Storage, Battery:
ecoinvent_aliases:
fltr:
- market for battery capacity, stationary (CONT scenario)
- market for battery capacity, stationary (CONT scenario)
iam_aliases:
image: Secondary Energy|Electricity|Storage
message: Secondary Energy|Electricity|Storage
Expand Down
Loading

0 comments on commit fe864ef

Please sign in to comment.