Skip to content

Commit b145990

Browse files
committed
Add doctest to broadcast_techs, make more generic
1 parent cfba8b6 commit b145990

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

src/muse/utilities.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def operation(x):
180180

181181

182182
def broadcast_techs(
183-
technologies: xr.Dataset | xr.DataArray,
183+
x: xr.Dataset | xr.DataArray,
184184
template: xr.DataArray | xr.Dataset,
185185
dimension: str = "asset",
186186
) -> xr.Dataset | xr.DataArray:
@@ -198,32 +198,49 @@ def broadcast_techs(
198198
This function broadcast the first representation to the shape and coordinates
199199
of the second.
200200
201-
This function does not support technologies with a 'year' dimension. Please select
202-
technology data for a specific year before calling this function.
201+
This function does not support arrays with a 'year' dimension. Please select
202+
data for a specific year before calling this function.
203203
204204
Arguments:
205-
technologies: The dataset to broadcast
205+
x: The dataset to broadcast
206206
template: the dataset or data-array to use as a template
207-
dimension: the name of the dimensiom from `template` over which to
208-
broadcast
209-
kwargs: further arguments are used initial filters over the
210-
`technologies` dataset.
211-
"""
212-
assert "year" not in technologies.dims
213-
assert "installed" not in technologies.dims
207+
dimension: TODO
208+
209+
Example:
210+
Define the example array:
211+
>>> import xarray as xr
212+
>>> x = xr.DataArray(
213+
... data=[[1, 2, 3], [4, 5, 6]],
214+
... dims=['technology', 'region'],
215+
... coords={'technology': ['gasboiler', 'heatpump'],
216+
... 'region': ['R1', 'R2', 'R3']},
217+
... )
214218
215-
# Attributes of `dimension` (e.g. "technology", "region", "installed")
216-
names = [u for u in template.coords if template[u].dims == (dimension,)]
219+
Define the assets template:
220+
>>> template = xr.DataArray(
221+
... data=[0, 0],
222+
... dims=["asset"],
223+
... coords={
224+
... "region": (["asset"], ["R1", "R2"]),
225+
... "technology": (["asset"], ["gasboiler", "heatpump"]),
226+
... "installed": (["asset"], [2020, 2025])},
227+
... )
217228
218-
# Select technologies only present in the template
219-
first_sel = {
220-
n: technologies[n].isin(template[n]) for n in names if n in technologies.dims
221-
}
222-
techs = technologies.sel(first_sel)
229+
Reshape the data to match the template:
230+
>>> broadcast_techs(x, template)
231+
<xarray.DataArray (asset: 2)> Size: 16B
232+
array([1, 5])
233+
Coordinates:
234+
technology (asset) <U9 72B 'gasboiler' 'heatpump'
235+
region (asset) <U2 16B 'R1' 'R2'
236+
installed (asset) int64 16B 2020 2025
237+
Dimensions without coordinates: asset
238+
"""
239+
assert "year" not in x.dims
240+
assert "installed" not in x.dims
223241

224-
# Restructure the technology dataset to match the template
225-
second_sel = {n: template[n] for n in template.coords if n in techs.dims}
226-
return techs.sel(second_sel)
242+
sel = {n: template[n] for n in template.coords if n in x.dims}
243+
return x.sel(sel)
227244

228245

229246
def clean_assets(assets: xr.Dataset, years: int | Sequence[int]):

0 commit comments

Comments
 (0)