def loadReferenceFile(self): filename, _ = QtWidgets.QFileDialog.getOpenFileName( filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)") if filename != "": self.app.resetReference() t = Touchstone(filename) t.load() self.app.setReference(t.s11, t.s21, filename)
def loadSweepFile(self): filename, _ = QtWidgets.QFileDialog.getOpenFileName( filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)") if filename != "": self.app.data.s11 = [] self.app.data.s21 = [] t = Touchstone(filename) t.load() self.app.saveData(t.s11, t.s21, filename) self.app.dataUpdated()
def test_load_scikit(self): ts = Touchstone("./test/data/scikit_unordered.s2p") with self.assertLogs(level=logging.WARNING) as cm: ts.load() self.assertEqual(cm.output, [ 'WARNING:NanoVNASaver.Touchstone:Non integer resistance value: 50.0', 'WARNING:NanoVNASaver.Touchstone:Comment after header: !freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22', 'WARNING:NanoVNASaver.Touchstone:Frequency not ascending: 15000000.0 0.849810063 -0.4147357 -0.000306106 0.0041482 0.0 0.0 0.0 0.0', 'WARNING:NanoVNASaver.Touchstone:Reordering data', ]) self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 101) self.assertIn("!freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22", ts.comments)
def test_swap(self): ts = Touchstone("./test/data/valid.s2p") ts.load() s11, s21, s12, s22 = ts.sdata ts.swap() s11_, s21_, s12_, s22_ = ts.sdata self.assertEqual([s11_, s21_, s12_, s22_] ,[s22, s12, s21, s11])
def test_load(self): ts = Touchstone("./test/data/valid.s1p") ts.load() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1010) self.assertEqual(len(ts.s21data), 0) self.assertEqual(ts.r, 50) ts = Touchstone("./test/data/valid.s2p") ts.load() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1020) self.assertEqual(len(ts.s21data), 1020) self.assertEqual(len(ts.s12data), 1020) self.assertEqual(len(ts.s22data), 1020) self.assertIn("! Vector Network Analyzer VNA R2", ts.comments)
def exportFile(self, nr_params: int = 1): if len(self.app.data.s11) == 0: QtWidgets.QMessageBox.warning(self, "No data to save", "There is no data to save.") return if nr_params > 2 and len(self.app.data.s21) == 0: QtWidgets.QMessageBox.warning(self, "No S21 data to save", "There is no S21 data to save.") return filedialog = QtWidgets.QFileDialog(self) if nr_params == 1: filedialog.setDefaultSuffix("s1p") filedialog.setNameFilter( "Touchstone 1-Port Files (*.s1p);;All files (*.*)") else: filedialog.setDefaultSuffix("s2p") filedialog.setNameFilter( "Touchstone 2-Port Files (*.s2p);;All files (*.*)") filedialog.setAcceptMode(QtWidgets.QFileDialog.AcceptSave) selected = filedialog.exec() if not selected: return filename = filedialog.selectedFiles()[0] if filename == "": logger.debug("No file name selected.") return ts = Touchstone(filename) ts.sdata[0] = self.app.data.s11 if nr_params > 1: ts.sdata[1] = self.app.data.s21 for dp in self.app.data.s11: ts.sdata[2].append(Datapoint(dp.freq, 0, 0)) ts.sdata[3].append(Datapoint(dp.freq, 0, 0)) try: ts.save(nr_params) except IOError as e: logger.exception("Error during file export: %s", e) return
def test_load(self): ts = Touchstone("./test/data/valid.s1p") ts.load() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1010) self.assertEqual(len(ts.s21data), 0) self.assertEqual(ts.r, 50) ts = Touchstone("./test/data/valid.s2p") ts.load() ts.gen_interpolation() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1020) self.assertEqual(len(ts.s21data), 1020) self.assertEqual(len(ts.s12data), 1020) self.assertEqual(len(ts.s22data), 1020) self.assertIn("! Vector Network Analyzer VNA R2", ts.comments) self.assertEqual(ts.min_freq(), 500000) self.assertEqual(ts.max_freq(), 900000000) self.assertEqual(ts.s_freq("11", 1), Datapoint(1, -3.33238E-001, 1.80018E-004)) self.assertEqual( ts.s_freq("11", 750000), Datapoint(750000, -0.3331754099382822, 0.00032433255669243524)) ts = Touchstone("./test/data/ma.s2p") ts.load() self.assertEqual(str(ts.opts), "# MHZ S MA R 50") ts = Touchstone("./test/data/db.s2p") ts.load() self.assertEqual(str(ts.opts), "# HZ S DB R 50") ts = Touchstone("./test/data/broken_pair.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "Data values aren't pairs") ts = Touchstone("./test/data/missing_pair.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "Inconsistent number") ts = Touchstone("./test/data/nonexistent.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "No such file or directory")
def test_save(self): ts = Touchstone("./test/data/valid.s2p") self.assertEqual(ts.saves(), "# HZ S RI R 50\n") ts.load() lines = ts.saves().splitlines() self.assertEqual(len(lines), 1021) self.assertEqual(lines[0], "# HZ S RI R 50") self.assertEqual(lines[1], '500000 -0.333238 0.000180018') self.assertEqual(lines[-1], '900000000 -0.127646 0.31969') lines = ts.saves(4).splitlines() self.assertEqual(len(lines), 1021) self.assertEqual(lines[0], "# HZ S RI R 50") self.assertEqual( lines[1], '500000 -0.333238 0.000180018 0.67478 -8.1951e-07' ' 0.67529 -8.20129e-07 -0.333238 0.000308078') self.assertEqual( lines[-1], '900000000 -0.127646 0.31969 0.596287 -0.503453' ' 0.599076 -0.50197 -0.122713 0.326965') ts.filename = "./test/data/output.s2p" ts.save(4) os.remove(ts.filename) ts.filename = "" self.assertRaises(FileNotFoundError, ts.save) ts.s11data[0] = Datapoint(100, 0.1, 0.1) self.assertRaisesRegex(LookupError, "Frequencies of sdata not correlated", ts.saves, 4)
def test_setter(self): ts = Touchstone("") dp_list = [Datapoint(1, 0.0, 0.0), Datapoint(3, 1.0, 1.0)] ts.s11data = dp_list[:] ts.s21data = dp_list[:] ts.s12data = dp_list[:] ts.s22data = dp_list[:] self.assertEqual(ts.s11data, dp_list) self.assertEqual(ts.s21data, dp_list) self.assertEqual(ts.s12data, dp_list) self.assertEqual(ts.s22data, dp_list) self.assertEqual(ts.min_freq(), 1) self.assertEqual(ts.max_freq(), 3) ts.gen_interpolation() self.assertEqual(ts.s_freq("11", 2), Datapoint(2, 0.5, 0.5))
def test_db_conversation(self): ts_db = Touchstone("./test/data/attenuator-0643_DB.s2p") ts_db.load() ts_ri = Touchstone("./test/data/attenuator-0643_RI.s2p") ts_ri.load() ts_ma = Touchstone("./test/data/attenuator-0643_MA.s2p") ts_ma.load() self.assertEqual(len(ts_db.s11data), len(ts_ri.s11data)) for dps_db, dps_ri in zip(ts_db.s11data, ts_ri.s11data): self.assertAlmostEqual(dps_db.z, dps_ri.z, places=5) self.assertEqual(len(ts_db.s11data), len(ts_ma.s11data)) for dps_db, dps_ma in zip(ts_db.s11data, ts_ma.s11data): self.assertAlmostEqual(dps_db.z, dps_ma.z, places=5)
def test_load(self): ts = Touchstone("./test/data/valid.s1p") ts.load() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1010) self.assertEqual(len(ts.s21data), 0) self.assertEqual(ts.r, 50) ts = Touchstone("./test/data/valid.s2p") ts.load() self.assertEqual(str(ts.opts), "# HZ S RI R 50") self.assertEqual(len(ts.s11data), 1020) self.assertEqual(len(ts.s21data), 1020) self.assertEqual(len(ts.s12data), 1020) self.assertEqual(len(ts.s22data), 1020) self.assertIn("! Vector Network Analyzer VNA R2", ts.comments) ts = Touchstone("./test/data/ma.s2p") ts.load() self.assertEqual(str(ts.opts), "# MHZ S MA R 50") ts = Touchstone("./test/data/db.s2p") ts.load() self.assertEqual(str(ts.opts), "# HZ S DB R 50") ts = Touchstone("./test/data/broken_pair.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "Data values aren't pairs") ts = Touchstone("./test/data/missing_pair.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "Inconsistent number") ts = Touchstone("./test/data/nonexistent.s2p") with self.assertLogs(level=logging.ERROR) as cm: ts.load() self.assertRegex(cm.output[0], "No such file or directory")
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("-d", "--debug", action="store_true", help="Set loglevel to debug") parser.add_argument("-D", "--debug-file", help="File to write debug logging output to") parser.add_argument( "-f", "--file", help="Touchstone file to load as sweep for off device usage") parser.add_argument( "-r", "--ref-file", help="Touchstone file to load as reference for off device usage") parser.add_argument("--version", action="version", version=f"NanoVNASaver {VERSION}") args = parser.parse_args() console_log_level = logging.WARNING file_log_level = logging.DEBUG print(INFO) if args.debug: console_log_level = logging.DEBUG logger = logging.getLogger("NanoVNASaver") logger.setLevel(logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(console_log_level) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) if args.debug_file: fh = logging.FileHandler(args.debug_file) fh.setLevel(file_log_level) fh.setFormatter(formatter) logger.addHandler(fh) logger.info("Startup...") QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) app = QtWidgets.QApplication(sys.argv) window = NanoVNASaver() window.show() if args.file: t = Touchstone(args.file) t.load() window.saveData(t.s11, t.s21, args.file) window.dataUpdated() if args.ref_file: t = Touchstone(args.ref_file) t.load() window.setReference(t.s11, t.s21, args.ref_file) window.dataUpdated() try: app.exec_() except BaseException as exc: logger.exception("%s", exc) raise exc