kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
generate cal data file content from CalDataSet
rodzic
9231737b70
commit
5860b04ce6
|
@ -151,8 +151,25 @@ class CalElement:
|
||||||
class CalDataSet(UserDict):
|
class CalDataSet(UserDict):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.notes = ""
|
||||||
self.data: defaultdict[int, CalData] = defaultdict(CalData)
|
self.data: defaultdict[int, CalData] = defaultdict(CalData)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return (
|
||||||
|
(
|
||||||
|
"# Calibration data for NanoVNA-Saver\n"
|
||||||
|
+ "\n".join([f"! {note}" for note in self.notes.splitlines()])
|
||||||
|
+ "\n" + "# Hz ShortR ShortI OpenR OpenI LoadR LoadI"
|
||||||
|
+ (" ThroughR ThroughI ThrureflR"
|
||||||
|
" ThrureflI IsolationR IsolationI\n"
|
||||||
|
if self.complete2port() else "\n")
|
||||||
|
+ "\n".join([
|
||||||
|
f"{self.data.get(freq)}" for freq in self.frequencies()
|
||||||
|
]) + "\n"
|
||||||
|
)
|
||||||
|
if self.complete1port() else ""
|
||||||
|
)
|
||||||
|
|
||||||
def insert(self, name: str, dp: Datapoint):
|
def insert(self, name: str, dp: Datapoint):
|
||||||
if name not in {'short', 'open', 'load',
|
if name not in {'short', 'open', 'load',
|
||||||
'through', 'thrurefl', 'isolation'}:
|
'through', 'thrurefl', 'isolation'}:
|
||||||
|
@ -235,7 +252,7 @@ class Calibration:
|
||||||
(g2 * gm2 - g3 * gm3) * g1)
|
(g2 * gm2 - g3 * gm3) * g1)
|
||||||
cal.e00 = - ((g2 * gm3 - g3 * gm3) * g1 * gm2 -
|
cal.e00 = - ((g2 * gm3 - g3 * gm3) * g1 * gm2 -
|
||||||
(g2 * g3 * gm2 - g2 * g3 * gm3 -
|
(g2 * g3 * gm2 - g2 * g3 * gm3 -
|
||||||
(g3 * gm2 - g2 * gm3) * g1) * gm1
|
(g3 * gm2 - g2 * gm3) * g1) * gm1
|
||||||
) / denominator
|
) / denominator
|
||||||
cal.e11 = ((g2 - g3) * gm1 - g1 * (gm2 - gm3) +
|
cal.e11 = ((g2 - g3) * gm1 - g1 * (gm2 - gm3) +
|
||||||
g3 * gm2 - g2 * gm3) / denominator
|
g3 * gm2 - g2 * gm3) / denominator
|
||||||
|
@ -377,19 +394,11 @@ class Calibration:
|
||||||
|
|
||||||
# TODO: implement tests
|
# TODO: implement tests
|
||||||
def save(self, filename: str):
|
def save(self, filename: str):
|
||||||
# Save the calibration data to file
|
self.dataset.notes = "\n".join(self.notes)
|
||||||
if not self.isValid1Port():
|
if not self.isValid1Port():
|
||||||
raise ValueError("Not a valid calibration")
|
raise ValueError("Not a valid calibration")
|
||||||
with open(filename, mode="w", encoding='utf-8') as calfile:
|
with open(filename, mode="w", encoding='utf-8') as calfile:
|
||||||
calfile.write("# Calibration data for NanoVNA-Saver\n")
|
calfile.write(str(self.dataset))
|
||||||
for note in self.notes:
|
|
||||||
calfile.write(f"! {note}\n")
|
|
||||||
calfile.write(
|
|
||||||
"# Hz ShortR ShortI OpenR OpenI LoadR LoadI"
|
|
||||||
" ThroughR ThroughI ThrureflR ThrureflI"
|
|
||||||
" IsolationR IsolationI\n")
|
|
||||||
for freq in self.dataset.frequencies():
|
|
||||||
calfile.write(f"{self.dataset.get(freq)}\n")
|
|
||||||
|
|
||||||
# TODO: implement tests
|
# TODO: implement tests
|
||||||
def load(self, filename):
|
def load(self, filename):
|
||||||
|
@ -416,9 +425,9 @@ class Calibration:
|
||||||
continue
|
continue
|
||||||
if m := RXP_CAL_HEADER.search(line):
|
if m := RXP_CAL_HEADER.search(line):
|
||||||
header = "sol"
|
header = "sol"
|
||||||
if "through" in m.groupdict():
|
if m.group("through"):
|
||||||
header = (
|
header = (
|
||||||
"long" if "thrurefl" in m.groupdict() else "short")
|
"long" if m.group("thrurefl") else "short")
|
||||||
columns = cols[header]
|
columns = cols[header]
|
||||||
logger.debug("found %s header type", header)
|
logger.debug("found %s header type", header)
|
||||||
continue
|
continue
|
||||||
|
@ -431,7 +440,8 @@ class Calibration:
|
||||||
continue
|
continue
|
||||||
m = RXP_CAL_LINE[header].search(line)
|
m = RXP_CAL_LINE[header].search(line)
|
||||||
if not m:
|
if not m:
|
||||||
logger.warning("Illegal data in cal file. Line %i", i + 1)
|
logger.warning("Illegal data in cal file. Line %i"
|
||||||
|
" (Fix line or header)", i + 1)
|
||||||
continue
|
continue
|
||||||
if (header == "short" and not m.group(8) and
|
if (header == "short" and not m.group(8) and
|
||||||
columns != cols["sol"]):
|
columns != cols["sol"]):
|
||||||
|
|
Ładowanie…
Reference in New Issue