Skip to content

Commit

Permalink
Fix conda_build_config.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Dec 19, 2024
1 parent 58d35be commit 99d4e81
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 51 deletions.
182 changes: 133 additions & 49 deletions premise/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from functools import lru_cache
from pathlib import Path
from typing import List, Union
from pprint import pprint

import numpy as np
import wurst
from wurst import copy_to_new_location
import xarray as xr
import yaml
from datapackage import Package
Expand Down Expand Up @@ -443,9 +445,11 @@ def __init__(

for data in self.external_scenarios_data.values():
self.regionalize_inventories(ds_names, external_scenario_regions, data)

self.dict_bio_flows = get_biosphere_flow_uuid(self.version)
self.outdated_flows = get_correspondence_bio_flows()


def regionalize_inventories(self, ds_names, regions, data: dict) -> None:
"""
Produce IAM region-specific version of the dataset.
Expand Down Expand Up @@ -789,40 +793,24 @@ def check_existence_of_market_suppliers(self):

# we loop through the possible locations
# by order of preference
try:
while not suppliers:
# suppliers = list(
# ws.get_many(
# self.database,
# ws.equals("name", name),
# ws.equals(
# "reference product",
# ref_prod,
# ),
# ws.equals(
# "location", possible_locations[counter]
# ),
# )
# )

for possible_location in possible_locations:
if not suppliers:
suppliers = [
s
for s in self.database
if s["name"].lower() == name.lower()
and s["reference product"].lower()
== ref_prod.lower()
and s["location"] == possible_locations[counter]
and s["reference product"].lower() == ref_prod.lower()
and s["location"] == possible_location
]

counter += 1

except IndexError as err:
if not suppliers:
raise ValueError(
f"Regionalized datasets for pathway {pathway_to_include} "
f"with `name` {name} and `reference product` {ref_prod} "
f"cannot be found in "
f"locations {possible_locations}."
) from err
)

if not exists_in_database or regionalize_dataset:
for ds in suppliers:
Expand All @@ -840,25 +828,70 @@ def fetch_supply_share(
:return: np.ndarray
"""

return np.clip(
(
self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=var,
)
.interp(year=self.year)
/ self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=variables,
)
.interp(year=self.year)
.sum(dim="variables")
).values.item(0),
0,
1,
)
if self.year < min(
self.external_scenarios_data[i]["production volume"].coords["year"].values
):
return np.clip(
(
self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=var,
)
.interp(year=min(self.external_scenarios_data[i]["production volume"].coords["year"].values))
/ self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=variables,
)
.interp(year=min(self.external_scenarios_data[i]["production volume"].coords["year"].values))
.sum(dim="variables")
).values.item(0),
0,
1,
)
elif self.year > max(
self.external_scenarios_data[i]["production volume"].coords["year"].values
):
return np.clip(
(
self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=var,
)
.interp(year=max(self.external_scenarios_data[i]["production volume"].coords["year"].values))
/ self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=variables,
)
.interp(year=max(self.external_scenarios_data[i]["production volume"].coords["year"].values))
.sum(dim="variables")
).values.item(0),
0,
1,
)
else:
return np.clip(
(
self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=var,
)
.interp(year=self.year)
/ self.external_scenarios_data[i]["production volume"]
.sel(
region=region,
variables=variables,
)
.interp(year=self.year)
.sum(dim="variables")
).values.item(0),
0,
1,
)

def fetch_potential_suppliers(
self, possible_locations: list, name: str, ref_prod: str
Expand All @@ -884,7 +917,26 @@ def fetch_potential_suppliers(
]

if not act:
print("Cannot find -> ", name, ref_prod, possible_locations)

act = [
a
for a in self.database
if a["name"].lower() == name.lower()
and a["reference product"].lower() == ref_prod.lower()
]

if act:
# create a copy of the dataset
# and change its location to the first possible location
candidate = copy_to_new_location(act[0], possible_locations[0])
candidate = self.relink_technosphere_exchanges(candidate)
self.database.append(candidate)
act = [candidate]

else:
raise ValueError(
f"Cannot find dataset with name {name} and reference product {ref_prod}."
)

return act

Expand Down Expand Up @@ -1035,6 +1087,13 @@ def adjust_efficiency_of_new_markets(
return datatset

def get_region_for_non_null_production_volume(self, i, variables):

if not any(
x in self.external_scenarios_data[i]["production volume"].coords["variables"].values
for x in variables
):
return []

nz = np.argwhere(
(
self.external_scenarios_data[i]["production volume"]
Expand Down Expand Up @@ -1114,13 +1173,38 @@ def create_markets(self) -> None:
.values.item(0)
)
else:
production_volume = (
self.external_scenarios_data[i]["production volume"]
.sel(variables=production_variables, region=region)
.sum(dim="variables")
.interp(year=self.year)
.values.item(0)
)
if self.year < min(
self.external_scenarios_data[i]["production volume"].coords[
"year"
].values
):
production_volume = (
self.external_scenarios_data[i]["production volume"]
.sel(variables=production_variables, region=region)
.sum(dim="variables")
.interp(year=min(self.external_scenarios_data[i]["production volume"].coords["year"].values))
.values.item(0)
)
elif self.year > max(
self.external_scenarios_data[i]["production volume"].coords[
"year"
].values
):
production_volume = (
self.external_scenarios_data[i]["production volume"]
.sel(variables=production_variables, region=region)
.sum(dim="variables")
.interp(year=max(self.external_scenarios_data[i]["production volume"].coords["year"].values))
.values.item(0)
)
else:
production_volume = (
self.external_scenarios_data[i]["production volume"]
.sel(variables=production_variables, region=region)
.sum(dim="variables")
.interp(year=self.year)
.values.item(0)
)

# Update production volume of the market
for e in ws.production(new_market):
Expand Down
1 change: 0 additions & 1 deletion premise/external_data_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ def adjust_candidates_or_raise_error(
for key, val in d_datasets.items():
if val.get("exists in original database"):
mask = val.get("mask")

duplicate_name = None
if val.get("duplicate") is True:
duplicate_name = key[0]
Expand Down
4 changes: 3 additions & 1 deletion premise/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,9 @@ def build_datapackage(self, name: str, contributors: list = None):

# also, remove "pathways/" from the path of each resource
for resource in data["resources"]:
resource["path"] = resource["path"].replace("pathways/", "")
resource["path"] = resource["path"].replace("pathways/", "").replace(
"pathways\\", ""
)

# save it back as a json file
with open(Path.cwd() / "pathways" / "datapackage.json", "w") as fp:
Expand Down

0 comments on commit 99d4e81

Please sign in to comment.