from scipy.optimize import curve_fit
from scipy.interpolate import splrep, splev
import csv
import argparse
import numpy
import matplotlib.pyplot as plt
from eisgenerator import EisSpectra
import io
import tarfile
from tqdm import tqdm
from spectrafile import SpectraFile


def add_soc_estimate(spectras: list[SpectraFile]):
	data = [list(), list()]

	for spectra in spectras:
		if not spectra.meta.soc <= 0:
			data[0].append(spectra.ocv)
			data[1].append(spectra.meta.soc)

	ndata = numpy.asarray(data)
	ndata.sort(1)

	knots = 9
	qs = numpy.linspace(0, 1, knots)[1:-1]
	knots = numpy.quantile(ndata[0], qs)
	tck = splrep(ndata[0], ndata[1], t=knots, k=3)
	estimates = splev(ndata[0], tck)

	for spectra in spectras:
		spectra.meta.soc_estimate = splev(spectra.ocv, tck)
