2020-06-09 15:56:53 +00:00
|
|
|
# NanoVNASaver
|
2020-06-25 17:48:03 +00:00
|
|
|
#
|
2020-06-09 15:56:53 +00:00
|
|
|
# A python program to view and export Touchstone data from a NanoVNA
|
2020-06-25 17:52:30 +00:00
|
|
|
# Copyright (C) 2019, 2020 Rune B. Broberg
|
2021-06-30 05:21:14 +00:00
|
|
|
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
|
2019-08-29 13:10:35 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-09-16 13:47:37 +00:00
|
|
|
import logging
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2023-03-12 07:02:58 +00:00
|
|
|
from PyQt6 import QtWidgets, QtCore, QtGui
|
2021-06-22 14:22:35 +00:00
|
|
|
from NanoVNASaver.Touchstone import Touchstone
|
|
|
|
from NanoVNASaver.RFTools import Datapoint
|
2023-02-27 20:01:35 +00:00
|
|
|
from NanoVNASaver.Windows.Defaults import make_scrollable
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2019-09-16 13:47:37 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2022-05-27 07:03:37 +00:00
|
|
|
|
2021-06-22 14:07:37 +00:00
|
|
|
class FilesWindow(QtWidgets.QWidget):
|
|
|
|
def __init__(self, app: QtWidgets.QWidget):
|
2019-08-28 18:20:07 +00:00
|
|
|
super().__init__()
|
2021-06-22 14:07:37 +00:00
|
|
|
self.app = app
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2021-06-22 14:07:37 +00:00
|
|
|
self.setWindowTitle("Files")
|
|
|
|
self.setWindowIcon(self.app.icon)
|
|
|
|
self.setMinimumWidth(200)
|
2023-03-12 07:02:58 +00:00
|
|
|
QtGui.QShortcut(QtCore.Qt.Key.Key_Escape, self, self.hide)
|
2023-02-01 07:28:23 +00:00
|
|
|
|
2023-02-27 20:01:35 +00:00
|
|
|
file_window_layout = QtWidgets.QVBoxLayout()
|
|
|
|
make_scrollable(self, file_window_layout)
|
2019-09-03 14:59:36 +00:00
|
|
|
|
2019-10-09 09:30:29 +00:00
|
|
|
load_file_control_box = QtWidgets.QGroupBox("Import file")
|
|
|
|
load_file_control_box.setMaximumWidth(300)
|
|
|
|
load_file_control_layout = QtWidgets.QFormLayout(load_file_control_box)
|
2019-09-26 11:14:42 +00:00
|
|
|
|
|
|
|
btn_load_sweep = QtWidgets.QPushButton("Load as sweep")
|
2021-06-22 14:22:35 +00:00
|
|
|
btn_load_sweep.clicked.connect(self.loadSweepFile)
|
2019-10-09 09:30:29 +00:00
|
|
|
btn_load_reference = QtWidgets.QPushButton("Load reference")
|
2021-06-22 14:22:35 +00:00
|
|
|
btn_load_reference.clicked.connect(self.loadReferenceFile)
|
2019-10-09 09:30:29 +00:00
|
|
|
load_file_control_layout.addRow(btn_load_sweep)
|
|
|
|
load_file_control_layout.addRow(btn_load_reference)
|
2019-09-03 14:59:36 +00:00
|
|
|
|
2019-10-09 09:30:29 +00:00
|
|
|
file_window_layout.addWidget(load_file_control_box)
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2019-10-09 09:30:29 +00:00
|
|
|
save_file_control_box = QtWidgets.QGroupBox("Export file")
|
|
|
|
save_file_control_box.setMaximumWidth(300)
|
|
|
|
save_file_control_layout = QtWidgets.QFormLayout(save_file_control_box)
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2020-02-02 16:50:46 +00:00
|
|
|
btn_export_file = QtWidgets.QPushButton("Save 1-Port file (S1P)")
|
2021-06-22 14:22:35 +00:00
|
|
|
btn_export_file.clicked.connect(lambda: self.exportFile(1))
|
2019-11-12 10:26:42 +00:00
|
|
|
save_file_control_layout.addRow(btn_export_file)
|
2019-08-30 19:57:10 +00:00
|
|
|
|
2020-02-02 16:50:46 +00:00
|
|
|
btn_export_file = QtWidgets.QPushButton("Save 2-Port file (S2P)")
|
2021-06-22 14:22:35 +00:00
|
|
|
btn_export_file.clicked.connect(lambda: self.exportFile(4))
|
2019-11-12 10:26:42 +00:00
|
|
|
save_file_control_layout.addRow(btn_export_file)
|
2019-08-28 18:20:07 +00:00
|
|
|
|
2019-10-09 09:30:29 +00:00
|
|
|
file_window_layout.addWidget(save_file_control_box)
|
2019-09-03 14:59:36 +00:00
|
|
|
|
2019-09-26 11:14:42 +00:00
|
|
|
btn_open_file_window = QtWidgets.QPushButton("Files ...")
|
2020-06-28 16:48:41 +00:00
|
|
|
btn_open_file_window.clicked.connect(
|
2023-03-08 08:40:39 +00:00
|
|
|
lambda: self.app.display_window("file")
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
|
|
|
|
def exportFile(self, nr_params: int = 1):
|
2021-06-22 20:07:36 +00:00
|
|
|
if len(self.app.data.s11) == 0:
|
2021-06-22 14:22:35 +00:00
|
|
|
QtWidgets.QMessageBox.warning(
|
2023-03-08 08:40:39 +00:00
|
|
|
self, "No data to save", "There is no data to save."
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
return
|
2021-06-22 20:07:36 +00:00
|
|
|
if nr_params > 2 and len(self.app.data.s21) == 0:
|
2021-06-22 14:22:35 +00:00
|
|
|
QtWidgets.QMessageBox.warning(
|
2023-03-08 08:40:39 +00:00
|
|
|
self, "No S21 data to save", "There is no S21 data to save."
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
filedialog = QtWidgets.QFileDialog(self)
|
|
|
|
if nr_params == 1:
|
|
|
|
filedialog.setDefaultSuffix("s1p")
|
|
|
|
filedialog.setNameFilter(
|
2023-03-08 08:40:39 +00:00
|
|
|
"Touchstone 1-Port Files (*.s1p);;All files (*.*)"
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
else:
|
|
|
|
filedialog.setDefaultSuffix("s2p")
|
|
|
|
filedialog.setNameFilter(
|
2023-03-08 08:40:39 +00:00
|
|
|
"Touchstone 2-Port Files (*.s2p);;All files (*.*)"
|
|
|
|
)
|
2023-04-18 10:14:21 +00:00
|
|
|
filedialog.setAcceptMode(QtWidgets.QFileDialog.AcceptMode.AcceptSave)
|
2021-06-22 14:22:35 +00:00
|
|
|
selected = filedialog.exec()
|
|
|
|
if not selected:
|
|
|
|
return
|
|
|
|
filename = filedialog.selectedFiles()[0]
|
|
|
|
if filename == "":
|
|
|
|
logger.debug("No file name selected.")
|
|
|
|
return
|
|
|
|
|
|
|
|
ts = Touchstone(filename)
|
2021-06-22 20:07:36 +00:00
|
|
|
ts.sdata[0] = self.app.data.s11
|
2021-06-22 14:22:35 +00:00
|
|
|
if nr_params > 1:
|
2021-06-22 20:07:36 +00:00
|
|
|
ts.sdata[1] = self.app.data.s21
|
|
|
|
for dp in self.app.data.s11:
|
2021-06-22 14:22:35 +00:00
|
|
|
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 loadReferenceFile(self):
|
|
|
|
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
|
2023-03-08 08:40:39 +00:00
|
|
|
filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)"
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
if filename != "":
|
|
|
|
self.app.resetReference()
|
|
|
|
t = Touchstone(filename)
|
|
|
|
t.load()
|
2021-06-22 20:07:36 +00:00
|
|
|
self.app.setReference(t.s11, t.s21, filename)
|
2021-06-22 14:22:35 +00:00
|
|
|
|
|
|
|
def loadSweepFile(self):
|
|
|
|
filename, _ = QtWidgets.QFileDialog.getOpenFileName(
|
2023-03-08 08:40:39 +00:00
|
|
|
filter="Touchstone Files (*.s1p *.s2p);;All files (*.*)"
|
|
|
|
)
|
2021-06-22 14:22:35 +00:00
|
|
|
if filename != "":
|
2021-06-22 20:07:36 +00:00
|
|
|
self.app.data.s11 = []
|
|
|
|
self.app.data.s21 = []
|
2021-06-22 14:22:35 +00:00
|
|
|
t = Touchstone(filename)
|
|
|
|
t.load()
|
2021-06-22 20:07:36 +00:00
|
|
|
self.app.saveData(t.s11, t.s21, filename)
|
2021-06-22 14:22:35 +00:00
|
|
|
self.app.dataUpdated()
|