from pathlib import Path
import pandas as pd
import numpy as np
from interpn import MulticubicRectilinear
import matplotlib.pyplot as plt
here = Path(__file__).parent
df = pd.read_csv(here / "type_k_its90_trunc.csv", header=0, index_col=0)
hjtemps_C = df.index.values
cjtemps_C = df.columns.values
for hjtemp_C in hjtemps_C:
if int(hjtemp_C) < 0:
for cjtemp_C in cjtemps_C:
if int(cjtemp_C) < 0:
cjerr = df.loc[hjtemp_C, cjtemp_C] - df.loc[hjtemp_C, "0"]
df.loc[hjtemp_C, str(-int(cjtemp_C))] = df.loc[hjtemp_C, "0"] - cjerr
elif int(hjtemp_C) == 0:
pass elif int(hjtemp_C) > 0:
for cjtemp_C in cjtemps_C:
if int(cjtemp_C) > 0:
cjerr = df.loc[hjtemp_C, cjtemp_C] - df.loc[hjtemp_C, "0"]
df.loc[hjtemp_C, str(-int(cjtemp_C))] = df.loc[hjtemp_C, "0"] - cjerr
else:
raise NotImplementedError
df.to_csv(here / "type_k_its90_mod.csv")
c_offs = 273.15 cjtemps = np.array(
[(-float(x)) + c_offs for x in df.columns.values]
)[::-1] hjtemps = np.array(
[float(x) + c_offs for x in df.index.values]
) voltages = df.values[:, ::-1] / 1e3
ngrid = 200
vmin = np.min(voltages)
vmax = np.max(voltages)
vgrid = np.linspace(
vmin, vmax, ngrid
) hjtemps_interpolated = np.zeros((ngrid, len(cjtemps)))
for i, cjtemp in enumerate(cjtemps):
v = voltages[:, i]
assert len(v) == 163
hjinterpolator = MulticubicRectilinear.new(
[v], hjtemps, linearize_extrapolation=True
)
hjtemps_interpolated[:, i] = hjinterpolator.eval([vgrid])
with open(here / "its90_cold_junc_tables_rust.txt", "w") as f:
step = cjtemps[1] - cjtemps[0]
start = cjtemps[0]
n = len(cjtemps)
f.write('// [K] temperature at the connector "cold junction", regular grid\n')
f.write(f"const COLD_JUNCTION_TEMP_START_K: f64 = {start}; // [K]\n")
f.write(f"const COLD_JUNCTION_TEMP_STEP_K: f64 = {step}; // [K]\n")
f.write(f"const COLD_JUNCTION_N: usize = {n};\n\n")
step = vgrid[1] - vgrid[0] start = vgrid[0]
n = ngrid
f.write(
"// [V] sensed voltage at (or, hopefully, close to) the connector, regular grid\n"
)
f.write(f"const SENSED_VOLTAGE_START_V: f64 = {start}; // [V]\n")
f.write(f"const SENSED_VOLTAGE_STEP_V: f64 = {step}; // [V]\n")
f.write(f"const SENSED_VOLTAGE_N: usize = {n};\n\n")
f.write(
'// [K] Temperature at the probe ("hot junction"), 2d table mapping (sensed_voltage, cold_junction_temp) => hot_junction_temp.\n'
)
f.write("#[rustfmt::skip]\n")
f.write(
f"const HOT_JUNCTION_TEMP_K: [f64; {hjtemps_interpolated.size}] = "
+ str([float(x) for x in hjtemps_interpolated.flatten()])
+ "; // [K]\n"
)
plt.imshow(hjtemps_interpolated.T, aspect="equal", extent=[0, 1, 0, 1])
cs = plt.contour(hjtemps_interpolated.T, colors="k", extent=[0, 1, 0, 1])
plt.show()