""""
Scenario-based Inventory Modelling for Prospective Life Cycle Assessment of Mineral Raw Materials (SIMPL-Minerals)

Jonas Klimt1,*, Robert Istrate1, Valerio Barbarossa1, Antoine Beylot2, Frederic Lai2, Bernhard Steubing1

1 Institute of Environmental Sciences (CML), Leiden University, 2333 CC Leiden, The Netherlands
2 BRGM, F-45060 Orléans, France
* Correspondance: j.b.j.klimt@cml.leidenuniv.nl 

Supplementary Information 6

""""


# If needed:
# pip install numpy pandas scipy

import numpy as np
import pandas as pd
from scipy.interpolate import CubicSpline

# ----------------------------
# Input data (knots)
# ----------------------------
years = np.array([2005, 2010, 2015, 2020, 2025, 2030, 2035, 2040, 2045, 2050], dtype=float)

ndc_vals = np.array([
    83.9365841031684, 97.7106967220592, 148.193183787913, 382.956844732012,
    2506.71580193297, 7703.9755912948, 14851.3904782584, 21950.9671618419,
    27757.2390250574, 31683.756692456
], dtype=float)

two_vals = np.array([
    83.9365841031684, 97.7106967220592, 148.193183787913, 382.956844732012,
    2506.71580193297, 15215.6647823779, 27818.1745349379, 38198.3431296875,
    45590.3155340745, 50505.8569441801
], dtype=float)

onefive_vals = np.array([
    83.9365841031684, 97.7106967220592, 148.193183787913, 382.956844732012,
    2506.71580193297, 13750.4292711817, 29398.1409669582, 41142.0466026586,
    49097.6192430417, 54546.7764936666
], dtype=float)

# ----------------------------
# Build "normal" cubic splines (SciPy default is not-a-knot)
# ----------------------------
cs_ndc = CubicSpline(years, ndc_vals, bc_type='not-a-knot')
cs_two = CubicSpline(years, two_vals, bc_type='not-a-knot')
cs_15  = CubicSpline(years, onefive_vals, bc_type='not-a-knot')

# Annual grid
years_full = np.arange(2005, 2051, 1, dtype=float)

# Evaluate splines over the whole range
ndc_interp = cs_ndc(years_full)
two_interp = cs_two(years_full)
onefive_interp = cs_15(years_full)

# ----------------------------
# Replace 2005–2025 (inclusive) for ALL scenarios with the NDC interpolation
# ----------------------------
mask_2005_2025 = (years_full >= 2005) & (years_full <= 2025)
ndc_early = ndc_interp[mask_2005_2025]

# Overwrite the early segment
two_interp[mask_2005_2025] = ndc_early
onefive_interp[mask_2005_2025] = ndc_early
# (ndc already equals ndc_early there, but we can reassign for symmetry)
ndc_interp[mask_2005_2025] = ndc_early

# ----------------------------
# Assemble and (optionally) round for display
# ----------------------------
df = pd.DataFrame({
    "Year": years_full.astype(int),
    "NDC": ndc_interp,
    "2.0": two_interp,
    "1.5": onefive_interp
})

# Display a rounded view (2 decimals)
df_rounded = df.copy()
df_rounded[["NDC", "2.0", "1.5"]] = df_rounded[["NDC", "2.0", "1.5"]].round(2)
print(df_rounded.to_string(index=False))

# Optional: save to CSV
# df.to_csv("cubic_spline_with_ndc_early_override_full_precision.csv", index=False)
# df_rounded.to_csv("cubic_spline_with_ndc_early_override_rounded.csv", index=False

df_rounded.to_csv("piecewise_cubic_interpolation.csv", index=False)