Skip to content

Commit

Permalink
Add locations
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi authored and romainsacchi committed Mar 5, 2024
1 parent 80a7c84 commit 9947ac6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pathways/pathways.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,19 @@ def fetch_indices(mapping, regions, variables, A_index, geo):
return vars_idx


def fetch_inventories_locations(A_index: Dict[str, Tuple[str, str, str]]) -> List[str]:
"""
Fetch the locations of the inventories.
:param A_index: Dictionary with the indices of the activities in the technosphere matrix.
:return: List of locations.
"""
locations = []
for act in A_index:
if act[1] not in locations:
locations.append(act[1])
return locations


def generate_A_indices(A_index, reverse_classifications, lca_results_coords):
# Generate a list of activity indices for each activity category
acts_idx = []
Expand Down Expand Up @@ -549,9 +562,8 @@ def calculate(
methods = None

# Set default values if arguments are not provided
if methods is None:
if characterization is True:
methods = get_lcia_method_names()
if methods is None and characterization is True:
methods = get_lcia_method_names()
if models is None:
models = self.scenarios.coords["model"].values
models = [m.lower() for m in models]
Expand Down Expand Up @@ -614,6 +626,7 @@ def calculate(

# Create xarray for storing LCA results if not already present
if self.lca_results is None:
locations = fetch_inventories_locations(A_index)
self.lca_results = create_lca_results_array(
methods,
B_index,
Expand Down
6 changes: 6 additions & 0 deletions pathways/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ def create_lca_results_array(
B_indices: Dict,
years: List[int],
regions: List[str],
locations: List[str],
models: List[str],
scenarios: List[str],
classifications: dict,
Expand All @@ -180,6 +181,8 @@ def create_lca_results_array(
:type years: List[int]
:param regions: List of regions to consider in the LCA results.
:type regions: List[str]
:param locations: List of locations to consider in the LCA results.
:type locations: List[str]
:param models: List of models to consider in the LCA results.
:type models: List[str]
:param scenarios: List of scenarios to consider in the LCA results.
Expand All @@ -189,12 +192,14 @@ def create_lca_results_array(
:rtype: xr.DataArray
"""


# Define the coordinates for the xarray DataArray
coords = {
"act_category": list(set(classifications.values())),
"variable": list(mapping.keys()),
"year": years,
"region": regions,
"location": locations,
"model": models,
"scenario": scenarios,
}
Expand All @@ -204,6 +209,7 @@ def create_lca_results_array(
len(coords["variable"]),
len(years),
len(regions),
len(locations),
len(models),
len(scenarios),
)
Expand Down

0 comments on commit 9947ac6

Please sign in to comment.