Datons Stories #15 - PNIEC Simulation | Energy Mix After Nuclear Shutdown
Simulating Spain's energy mix after nuclear plant closures under the PNIEC plan. ML models trained on real ESIOS data, three replacement scenarios, and an interactive Streamlit app.
Get weekly updates
Live coding sessions, Python libraries for energy data, and hands-on tutorials. One email per week, zero fluff.
Result

An interactive Streamlit app that simulates Spain’s energy mix year by year as nuclear plants close following the PNIEC schedule. ML models trained on real ESIOS data predict demand and generation, while users adjust installed capacity per technology to explore replacement scenarios.
The problem

- Spain’s PNIEC plans staggered closure of 7 nuclear reactors (7,399 MW) between 2027 and 2035
- Nuclear provides ~20% of electricity generation with a capacity factor of 0.89 — the highest of any technology
- Replacing 1 MW of nuclear requires ~5 MW of solar PV (factor 0.17) or ~3.7 MW of wind (factor 0.24)
- Nuclear shutdown debates lack real data-driven simulations showing the concrete impact
Capacity factors

The capacity factor measures what percentage of the time a plant generates at maximum output. It’s the key concept for understanding why replacing nuclear is not simply installing the same capacity in renewables.
| Technology | Factor | Hours equiv./year |
|---|---|---|
| Nuclear | 0.89 | 7,796 h |
| Wind | 0.24 | 2,102 h |
| Combined cycle | 0.20 | 1,752 h |
| Solar PV | 0.17 | 1,489 h |
| Hydro | 0.15 | 1,314 h |
Nuclear generates almost 90% of the time. Solar only 17%. You need roughly 5x the installed capacity to fill the gap.
The solution
We started with historical generation and demand data downloaded from the ESIOS API (Red Electrica) to build predictive models and an interactive simulation tool.
ESIOS and PNIEC data

Downloaded historical time series of generation by technology and electricity demand from the ESIOS API. The data includes hourly production from nuclear, wind, solar, combined cycle gas, hydro, and all other technologies in Spain’s energy mix.
Data preparation

The PNIEC data comes as installed capacity targets per technology at the annual level. With Pandas we interpolated these values month by month to obtain a continuous series to feed the models. We also created temporal variables (hour, day, month, weekend) as features for the model.
Machine learning models
We trained XGBoosting models to predict electricity demand and generation by technology. The train/test split was explained using an exam analogy: the model studies with one set of data (train) and takes an exam with data it has never seen (test). If it only memorized the study answers, it would fail the exam — that’s overfitting.
Model evaluation

Models were evaluated with two main metrics:
- MAE (Mean Absolute Error): average deviation in absolute terms — how many MW the prediction is off on average
- RMSE (Root Mean Squared Error): penalizes large errors more heavily — useful for catching wildly off predictions
The models showed acceptable performance for scenario simulation, capturing seasonal and hourly generation patterns.
Generation forecast

With the trained models, we projected future generation following the PNIEC schedule. Nuclear drops progressively from 7,000 MW to 2,300 MW while other technologies must compensate for the deficit.
Streamlit application

We built an interactive app with Streamlit and ChatGPT assistance. We refactored the notebooks into a clean project structure with separate modules for data, models, and visualization. The app lets users:
- Select a future year (2027-2035)
- See which nuclear plants have closed by that year
- Adjust installed capacity per technology
- Simulate hourly generation and view the resulting mix
- Compare against historical generation
Project structure
Three replacement scenarios
We simulated three scenarios to fill the gap left by nuclear:
Scenario A — Renewables only: Install enough wind and solar to compensate for lost nuclear energy. The problem: intermittency. There are hours with excess generation and hours with deficits that cannot be covered.
Scenario B — Gas as backup (Germany approach): Combine renewables with combined cycle gas turbines as backup technology. Covers the deficit but increases CO2 emissions — exactly what happened in Germany after their nuclear shutdown.
Scenario C — Optimized mix: Find the optimal combination of renewables, storage, and gas that minimizes both the energy deficit and emissions. The most realistic scenario but also the most complex to model.
Reference
Nuclear plants in Spain
| Plant | Capacity (MW) | Planned closure |
|---|---|---|
| Almaraz I | 1,049 | 2027 |
| Almaraz II | 1,006 | 2028 |
| Asco I | 1,033 | 2030 |
| Cofrentes | 1,092 | 2030 |
| Asco II | 1,027 | 2032 |
| Vandellos II | 1,087 | 2035 |
| Trillo | 1,066 | 2035 |
Total: 7,399 MW — approximately 20% of Spain’s electricity generation.
Machine learning concepts used
| Concept | Description |
|---|---|
| XGBoosting | Gradient boosting algorithm based on decision trees |
| Train/test split | Data division for training and evaluation |
| Overfitting | Model that memorizes training data but fails to generalize |
| MAE | Mean Absolute Error — average prediction deviation |
| RMSE | Root Mean Squared Error — penalizes large errors |
| Feature engineering | Creating variables (hour, day, month, temperature) from raw data |
Stack
- Python · Pandas · Plotly · XGBoost · Streamlit · ESIOS API · ChatGPT
Resources
- ESIOS - REE - Real-time generation and demand data
- PNIEC - MITECO - Spain’s National Energy and Climate Plan
- StatQuest - XGBoost - Accessible ML explainers
- Streamlit - Framework for interactive data apps