pypsa-eur icon indicating copy to clipboard operation
pypsa-eur copied to clipboard

Refactor `prepare_costs`

Open euronion opened this issue 1 month ago • 3 comments

prepare_costs can cause confusion.

Suggestion is to refactor to make the logic more clear and maintainable.

euronion avatar Nov 05 '25 12:11 euronion

Spun from #1879

euronion avatar Nov 05 '25 12:11 euronion

The prepare_costs function do the following:

  1. Enforce unit convention > this could be delegated to technology-data
  2. Fill missing values
  3. Overwrite values if applicable (raw values) > removing the deprecated code will make the code leaner
  4. Compute capital and marginal costs
  5. Assign OCGT and CCGT fuel cost and CO2 intensity using the gas values
  6. Define the solar capital cost using solar-utility assumption
  7. Rename
    • solar-utility single-axis tracking to solar-hsat > this could be delegated to technology-data
    • standing losses to standing_losses > this could be delegated to technology-data
  8. Calculate storage costs for H2 and batteries
  9. Overwrite values if applicable (processed values, i.e. marginal and capital costs)

We could make things clearer with

  def prepare_costs(...):
      # Stage 1: Apply custom costs to raw attributes (before computation)
      costs = fill_missing_values(costs, config)
      costs = apply_custom_costs(costs, custom_raw)

      # Compute derived costs from raw attributes
      costs = compute_derived_costs(costs, config, nyears)
      costs = calculate_storage_costs(costs, max_hours)

      # Stage 2: Apply custom costs to computed attributes (after computation)
      costs = apply_custom_costs(costs, custom_prepared)

      return costs

However, if we want to maintain the same set of attributes, I think that the two-stage overwrite must be kept. Better logging could help as well.

tgilon avatar Nov 05 '25 13:11 tgilon

Sounds good the way @tgilon describes it.

fneum avatar Nov 05 '25 15:11 fneum