From 23fe8024f779b74cce8919ba137de561d8792a36 Mon Sep 17 00:00:00 2001 From: "Rune B. Broberg" Date: Tue, 17 Sep 2019 11:13:42 +0200 Subject: [PATCH] - Further logging - Preparing for Magnitude-Angle Touchstone files - Minimum width for frequency input fields --- NanoVNASaver/NanoVNASaver.py | 2 ++ NanoVNASaver/Touchstone.py | 66 ++++++++++++++++++++++-------------- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/NanoVNASaver/NanoVNASaver.py b/NanoVNASaver/NanoVNASaver.py index c172522..7342b52 100644 --- a/NanoVNASaver/NanoVNASaver.py +++ b/NanoVNASaver/NanoVNASaver.py @@ -159,6 +159,7 @@ class NanoVNASaver(QtWidgets.QWidget): sweep_control_layout.addRow(sweep_input_layout) self.sweepStartInput = QtWidgets.QLineEdit("") + self.sweepStartInput.setMinimumWidth(60) self.sweepStartInput.setAlignment(QtCore.Qt.AlignRight) self.sweepStartInput.textEdited.connect(self.updateCenterSpan) @@ -171,6 +172,7 @@ class NanoVNASaver(QtWidgets.QWidget): sweep_input_left_layout.addRow(QtWidgets.QLabel("Stop"), self.sweepEndInput) self.sweepCenterInput = QtWidgets.QLineEdit("") + self.sweepCenterInput.setMinimumWidth(60) self.sweepCenterInput.setAlignment(QtCore.Qt.AlignRight) self.sweepCenterInput.textEdited.connect(self.updateStartEnd) diff --git a/NanoVNASaver/Touchstone.py b/NanoVNASaver/Touchstone.py index bc24397..1c85e5d 100644 --- a/NanoVNASaver/Touchstone.py +++ b/NanoVNASaver/Touchstone.py @@ -14,10 +14,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . import collections +import logging from typing import List Datapoint = collections.namedtuple('Datapoint', 'freq re im') +logger = logging.getLogger(__name__) + class Touchstone: s11data: List[Datapoint] = [] @@ -31,62 +34,75 @@ class Touchstone: def load(self): self.s11data = [] self.s21data = [] + + realimaginary = False + magnitudeangle = False + factor = 1 try: + logger.info("Attempting to open file %s", self.filename) file = open(self.filename, "r") lines = file.readlines() parsed_header = False - for l in lines: - l = l.strip() - if l.startswith("!"): + for line in lines: + line = line.strip() + if line.startswith("!"): continue - if l.startswith("#") and not parsed_header: + if line.startswith("#") and not parsed_header: # Check that this is a valid header - if l == "# Hz S RI R 50": + if line == "# Hz S RI R 50": parsed_header = True + realimaginary = True factor = 1 continue - elif l == "# kHz S RI R 50": + elif line == "# kHz S RI R 50": parsed_header = True + realimaginary = True factor = 10**3 continue - elif l == "# MHz S RI R 50": + elif line == "# MHz S RI R 50": parsed_header = True + realimaginary = True factor = 10**6 continue - elif l == "# GHz S RI R 50": + elif line == "# GHz S RI R 50": parsed_header = True + realimaginary = True factor = 10**9 continue else: # This is some other comment line continue if not parsed_header: - print("Warning: Read line without having read header: " + l) + logger.warning("Read line without having read header: %s", line) continue try: - values = l.split(maxsplit=5) - freq = values[0] - re11 = values[1] - im11 = values[2] - freq = int(float(freq) * factor) - re11 = float(re11) - im11 = float(im11) - self.s11data.append(Datapoint(freq, re11, im11)) - if len(values) > 3: - re21 = values[3] - im21 = values[4] - re21 = float(re21) - im21 = float(im21) - self.s21data.append(Datapoint(freq, re21, im21)) + if realimaginary: + values = line.split(maxsplit=5) + freq = values[0] + re11 = values[1] + im11 = values[2] + freq = int(float(freq) * factor) + re11 = float(re11) + im11 = float(im11) + self.s11data.append(Datapoint(freq, re11, im11)) + if len(values) > 3: + re21 = values[3] + im21 = values[4] + re21 = float(re21) + im21 = float(im21) + self.s21data.append(Datapoint(freq, re21, im21)) + elif magnitudeangle: + # TODO: Implement parsing code for Magnitude-Angle + continue except ValueError as e: - print("Error parsing line " + l + " : " + str(e)) + logger.exception("Failed to parse line: %s (%s)", line, e) file.close() except IOError as e: - print("Failed to open " + self.filename) + logger.exception("Failed to open %s: %s", self.filename, e) return def setFilename(self, filename):