Skip to content

Commit 1a8a4d0

Browse files
committed
Get outputs for first year
1 parent 2831b7a commit 1a8a4d0

4 files changed

Lines changed: 59 additions & 38 deletions

File tree

src/muse/mca.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,9 @@ def run(self) -> None:
306306
# Save sector outputs
307307
for sector in self.sectors:
308308
if type(sector) is Sector:
309-
sector.save_outputs()
309+
if year_idx == 0:
310+
sector.save_outputs(years[0])
311+
sector.save_outputs(years[1])
310312

311313
# If we need to account for the carbon budget, we might need to change
312314
# the budget for the future, too.
@@ -324,6 +326,10 @@ def run(self) -> None:
324326
self.market.prices.sel(dims), new_market.prices.sel(year=years[1])
325327
)
326328

329+
if year_idx == 0:
330+
# Necessary to get global outputs for the first year of the framework
331+
self.outputs(self.market, self.sectors, year=years[0])
332+
327333
# Global outputs
328334
investment_year = years[1]
329335
self.outputs(self.market, self.sectors, year=investment_year)

src/muse/outputs/mca.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
@register_output_quantity
99
def quantity(
1010
sectors: List[AbstractSector],
11-
market: xr.Dataset, **kwargs
11+
market: xr.Dataset,
12+
year: int,
13+
**kwargs
1214
) -> Union[pd.DataFrame, xr.DataArray]:
1315
pass
1416
@@ -76,9 +78,13 @@ def round_values(function: Callable) -> OUTPUT_QUANTITY_SIGNATURE:
7678

7779
@wraps(function)
7880
def rounded(
79-
market: xr.Dataset, sectors: list[AbstractSector], rounding: int = 4, **kwargs
81+
market: xr.Dataset,
82+
sectors: list[AbstractSector],
83+
year: int,
84+
rounding: int = 4,
85+
**kwargs,
8086
) -> xr.DataArray:
81-
result = function(market, sectors, **kwargs)
87+
result = function(market=market, sectors=sectors, year=year, **kwargs)
8288

8389
if hasattr(result, "to_dataframe"):
8490
result = result.to_dataframe()
@@ -150,15 +156,17 @@ def reformat_finite_resources(params):
150156
@register_output_quantity
151157
@round_values
152158
def consumption(
153-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
159+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
154160
) -> pd.DataFrame:
155161
"""Current consumption."""
156162
return market_quantity(market.consumption, **kwargs).to_dataframe().reset_index()
157163

158164

159165
@register_output_quantity
160166
@round_values
161-
def supply(market: xr.Dataset, sectors: list[AbstractSector], **kwargs) -> pd.DataFrame:
167+
def supply(
168+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
169+
) -> pd.DataFrame:
162170
"""Current supply."""
163171
return market_quantity(market.supply, **kwargs).to_dataframe().reset_index()
164172

@@ -168,6 +176,7 @@ def supply(market: xr.Dataset, sectors: list[AbstractSector], **kwargs) -> pd.Da
168176
def prices(
169177
market: xr.Dataset,
170178
sectors: list[AbstractSector],
179+
year: int,
171180
**kwargs,
172181
) -> pd.DataFrame:
173182
"""Current MCA market prices."""
@@ -177,7 +186,7 @@ def prices(
177186
@register_output_quantity
178187
@round_values
179188
def capacity(
180-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
189+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
181190
) -> pd.DataFrame:
182191
"""Current capacity across all sectors."""
183192
return _aggregate_sectors(sectors, op=sector_capacity)
@@ -236,14 +245,14 @@ def _aggregate_sectors(
236245

237246
@register_output_quantity(name=["fuel_costs"])
238247
def metric_fuel_costs(
239-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
248+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
240249
) -> pd.DataFrame:
241250
"""Current fuel costs across all sectors."""
242-
return _aggregate_sectors(sectors, market, op=sector_fuel_costs)
251+
return _aggregate_sectors(sectors, market, year, op=sector_fuel_costs)
243252

244253

245254
def sector_fuel_costs(
246-
sector: AbstractSector, market: xr.Dataset, **kwargs
255+
sector: AbstractSector, market: xr.Dataset, year: int, **kwargs
247256
) -> pd.DataFrame:
248257
"""Sector fuel costs with agent annotations."""
249258
from muse.commodities import is_fuel
@@ -258,13 +267,13 @@ def sector_fuel_costs(
258267
if len(technologies) > 0:
259268
for a in agents:
260269
agent_market["consumption"] = (market.consumption * a.quantity).sel(
261-
year=a.year
270+
year=year
262271
)
263272
commodity = is_fuel(technologies.comm_usage)
264273

265274
capacity = a.filter_input(
266275
a.assets.capacity,
267-
year=a.year,
276+
year=year,
268277
).fillna(0.0)
269278

270279
production = supply(
@@ -273,7 +282,7 @@ def sector_fuel_costs(
273282
technologies,
274283
)
275284

276-
prices = a.filter_input(market.prices, year=a.year)
285+
prices = a.filter_input(market.prices, year=year)
277286
fcons = consumption(
278287
technologies=technologies, production=production, prices=prices
279288
)
@@ -282,7 +291,7 @@ def sector_fuel_costs(
282291
data_agent["agent"] = a.name
283292
data_agent["category"] = a.category
284293
data_agent["sector"] = getattr(sector, "name", "unnamed")
285-
data_agent["year"] = a.year
294+
data_agent["year"] = year
286295
data_agent = multiindex_to_coords(data_agent, "timeslice").to_dataframe(
287296
"fuel_consumption_costs"
288297
)
@@ -298,14 +307,14 @@ def sector_fuel_costs(
298307

299308
@register_output_quantity(name=["capital_costs"])
300309
def metric_capital_costs(
301-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
310+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
302311
) -> pd.DataFrame:
303312
"""Current capital costs across all sectors."""
304-
return _aggregate_sectors(sectors, market, op=sector_capital_costs)
313+
return _aggregate_sectors(sectors, market, year, op=sector_capital_costs)
305314

306315

307316
def sector_capital_costs(
308-
sector: AbstractSector, market: xr.Dataset, **kwargs
317+
sector: AbstractSector, market: xr.Dataset, year: int, **kwargs
309318
) -> pd.DataFrame:
310319
"""Sector capital costs with agent annotations."""
311320
data_sector: list[xr.DataArray] = []
@@ -314,17 +323,17 @@ def sector_capital_costs(
314323

315324
if len(technologies) > 0:
316325
for a in agents:
317-
capacity = a.filter_input(a.assets.capacity, year=a.year).fillna(0.0)
326+
capacity = a.filter_input(a.assets.capacity, year=year).fillna(0.0)
318327
data = a.filter_input(
319328
technologies[["cap_par", "cap_exp"]],
320-
year=a.year,
329+
year=year,
321330
technology=capacity.technology,
322331
)
323332
data_agent = distribute_timeslice(data.cap_par * (capacity**data.cap_exp))
324333
data_agent["agent"] = a.name
325334
data_agent["category"] = a.category
326335
data_agent["sector"] = getattr(sector, "name", "unnamed")
327-
data_agent["year"] = a.year
336+
data_agent["year"] = year
328337
data_agent = multiindex_to_coords(data_agent, "timeslice").to_dataframe(
329338
"capital_costs"
330339
)
@@ -340,14 +349,14 @@ def sector_capital_costs(
340349

341350
@register_output_quantity(name=["emission_costs"])
342351
def metric_emission_costs(
343-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
352+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
344353
) -> pd.DataFrame:
345354
"""Current emission costs across all sectors."""
346-
return _aggregate_sectors(sectors, market, op=sector_emission_costs)
355+
return _aggregate_sectors(sectors, market, year, op=sector_emission_costs)
347356

348357

349358
def sector_emission_costs(
350-
sector: AbstractSector, market: xr.Dataset, **kwargs
359+
sector: AbstractSector, market: xr.Dataset, year: int, **kwargs
351360
) -> pd.DataFrame:
352361
"""Sector emission costs with agent annotations."""
353362
from muse.commodities import is_enduse, is_pollutant
@@ -361,21 +370,21 @@ def sector_emission_costs(
361370
if len(technologies) > 0:
362371
for a in agents:
363372
agent_market["consumption"] = (market.consumption * a.quantity).sel(
364-
year=a.year
373+
year=year
365374
)
366375

367-
capacity = a.filter_input(a.assets.capacity, year=a.year).fillna(0.0)
376+
capacity = a.filter_input(a.assets.capacity, year=year).fillna(0.0)
368377
allemissions = a.filter_input(
369378
technologies.fixed_outputs,
370379
commodity=is_pollutant(technologies.comm_usage),
371380
technology=capacity.technology,
372-
year=a.year,
381+
year=year,
373382
)
374383
envs = is_pollutant(technologies.comm_usage)
375384
enduses = is_enduse(technologies.comm_usage)
376385
i = (np.where(envs))[0][0]
377386
red_envs = envs[i].commodity.values
378-
prices = a.filter_input(market.prices, year=a.year, commodity=red_envs)
387+
prices = a.filter_input(market.prices, year=year, commodity=red_envs)
379388
production = supply(
380389
agent_market,
381390
capacity,
@@ -387,7 +396,7 @@ def sector_emission_costs(
387396
data_agent["agent"] = a.name
388397
data_agent["category"] = a.category
389398
data_agent["sector"] = getattr(sector, "name", "unnamed")
390-
data_agent["year"] = a.year
399+
data_agent["year"] = year
391400
data_agent = multiindex_to_coords(data_agent, "timeslice").to_dataframe(
392401
"emission_costs"
393402
)
@@ -404,13 +413,15 @@ def sector_emission_costs(
404413

405414
@register_output_quantity(name=["LCOE"])
406415
def metric_lcoe(
407-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
416+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
408417
) -> pd.DataFrame:
409418
"""Current lifetime levelised cost across all sectors."""
410-
return _aggregate_sectors(sectors, market, op=sector_lcoe)
419+
return _aggregate_sectors(sectors, market, year, op=sector_lcoe)
411420

412421

413-
def sector_lcoe(sector: AbstractSector, market: xr.Dataset, **kwargs) -> pd.DataFrame:
422+
def sector_lcoe(
423+
sector: AbstractSector, market: xr.Dataset, year: int, **kwargs
424+
) -> pd.DataFrame:
414425
"""Levelized cost of energy () of technologies over their lifetime."""
415426
from muse.costs import levelized_cost_of_energy as LCOE
416427
from muse.quantities import capacity_to_service_demand, consumption
@@ -485,13 +496,15 @@ def sector_lcoe(sector: AbstractSector, market: xr.Dataset, **kwargs) -> pd.Data
485496

486497
@register_output_quantity(name=["EAC"])
487498
def metric_eac(
488-
market: xr.Dataset, sectors: list[AbstractSector], **kwargs
499+
market: xr.Dataset, sectors: list[AbstractSector], year: int, **kwargs
489500
) -> pd.DataFrame:
490501
"""Current emission costs across all sectors."""
491-
return _aggregate_sectors(sectors, market, op=sector_eac)
502+
return _aggregate_sectors(sectors, market, year, op=sector_eac)
492503

493504

494-
def sector_eac(sector: AbstractSector, market: xr.Dataset, **kwargs) -> pd.DataFrame:
505+
def sector_eac(
506+
sector: AbstractSector, market: xr.Dataset, year: int, **kwargs
507+
) -> pd.DataFrame:
495508
"""Net Present Value of technologies over their lifetime."""
496509
from muse.costs import equivalent_annual_cost as EAC
497510
from muse.quantities import capacity_to_service_demand, consumption

src/muse/outputs/sector.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def save_multiple_outputs(market, *args, year: int | None = None) -> list[Any]:
107107
year = int(market.year.max())
108108

109109
return [
110-
sink(quantity(market, *args), year=year)
110+
sink(quantity(market, *args, year=year), year=year)
111111
for quantity, sink in zip(quantities, sinks)
112112
]
113113

@@ -182,6 +182,7 @@ def consumption(
182182
sum_over: list[str] | None = None,
183183
drop: list[str] | None = None,
184184
rounding: int = 4,
185+
**kwargs,
185186
) -> xr.DataArray:
186187
"""Current consumption."""
187188
moutput = market.copy(deep=True).reset_index("timeslice")
@@ -202,6 +203,7 @@ def supply(
202203
sum_over: list[str] | None = None,
203204
drop: list[str] | None = None,
204205
rounding: int = 4,
206+
**kwargs,
205207
) -> xr.DataArray:
206208
"""Current supply."""
207209
moutput = market.copy(deep=True).reset_index("timeslice")
@@ -222,6 +224,7 @@ def costs(
222224
sum_over: list[str] | None = None,
223225
drop: list[str] | None = None,
224226
rounding: int = 4,
227+
**kwargs,
225228
) -> xr.DataArray:
226229
"""Current costs."""
227230
from muse.commodities import is_pollutant

src/muse/sectors/sector.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,9 @@ def group_assets(x: xr.DataArray) -> xr.DataArray:
275275
# Convert result to global timeslicing scheme
276276
return self.convert_to_global_timeslicing(result)
277277

278-
def save_outputs(self) -> None:
278+
def save_outputs(self, year: int) -> None:
279279
"""Calls the outputs function with the current output data."""
280-
investment_year = self.output_data.year.values[1]
281-
self.outputs(self.output_data, self.capacity, year=investment_year)
280+
self.outputs(self.output_data, self.capacity, year=year)
282281

283282
def market_variables(self, market: xr.Dataset, technologies: xr.Dataset) -> Any:
284283
"""Computes resulting market: production, consumption, and costs."""

0 commit comments

Comments
 (0)