kopia lustrzana https://github.com/NanoVNA-Saver/nanovna-saver
Touchstone class save method added
rodzic
0bf3ec69a5
commit
bfc73f8d5e
|
@ -202,3 +202,34 @@ class Touchstone:
|
|||
logger.warning("Reordering data")
|
||||
for datalist in self.sdata:
|
||||
datalist.sort(key=attrgetter("freq"))
|
||||
|
||||
def save(self, nr_params: int = 1):
|
||||
"""Save touchstone data to file.
|
||||
|
||||
Args:
|
||||
nr_params: Number of s-parameters. 2 for s1p, 4 for s2p
|
||||
"""
|
||||
|
||||
logger.info("Attempting to open file %s for writing",
|
||||
self.filename)
|
||||
with open(self.filename, "w") as outfile:
|
||||
outfile.write(self.saves(nr_params))
|
||||
|
||||
def saves(self, nr_params: int = 1 ) -> str:
|
||||
"""Returns touchstone data as string.
|
||||
|
||||
Args:
|
||||
nr_params: Number of s-parameters. 1 for s1p, 4 for s2p
|
||||
"""
|
||||
assert nr_params in (1, 4)
|
||||
|
||||
ts_str = "# HZ S RI R 50\n"
|
||||
for i, dp_s11 in enumerate(self.s11data):
|
||||
ts_str += f"{dp_s11.freq} {dp_s11.re} {dp_s11.im}"
|
||||
for j in range(1, nr_params):
|
||||
dp = self.sdata[j][i]
|
||||
if dp.freq != dp_s11.freq:
|
||||
raise LookupError("Frequencies of sdata not correlated")
|
||||
ts_str +=f" {dp.re} {dp.im}"
|
||||
ts_str += "\n"
|
||||
return ts_str
|
|
@ -1,9 +0,0 @@
|
|||
# Hz S RI R 50
|
||||
140000000 -0.720544874 -0.074467673
|
||||
140307234 -0.707615315 -0.045678697
|
||||
140614468 -0.694235622 -0.017205553
|
||||
140921702 -0.679476678 0.011064857
|
||||
141228936 0.037949264
|
||||
141536169 -0.645231842 0.06495472
|
||||
141843404 -0.625548779 0.090901531
|
||||
142150638 -0.605278372 0.116493001
|
|
@ -0,0 +1,10 @@
|
|||
! Vector Network Analyzer VNA R2
|
||||
! Tucson Amateur Packet Radio
|
||||
! Saturday, 9 November, 2019 17:48:47
|
||||
! Frequency S11 S21 S12 S22
|
||||
! ListType=Lin
|
||||
# HZ S RI R 50
|
||||
000500000 -3.33238E-001 1.80018E-004 6.74780E-001 -8.19510E-007 6.75290E-001 -8.20129E-007 -3.33238E-001
|
||||
001382728 -3.33017E-001 6.89580E-004 6.74251E-001 -3.70855E-004 6.74761E-001 -5.04361E-004 -3.33016E-001 9.45694E-004
|
||||
002265456 -3.33136E-001 1.06095E-003 6.74766E-001 -1.00228E-003 6.75276E-001 -1.00304E-003 -3.33136E-001 1.06095E-003
|
||||
003148184 -3.33120E-001 1.97467E-003 6.74773E-001 -1.65230E-003 6.74773E-001 -1.65230E-003 -3.33121E-001 1.91064E-003
|
|
@ -16,10 +16,11 @@
|
|||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
import unittest
|
||||
import logging
|
||||
import os
|
||||
|
||||
# Import targets to be tested
|
||||
from NanoVNASaver.Touchstone import Options, Touchstone
|
||||
|
||||
from NanoVNASaver.RFTools import Datapoint
|
||||
|
||||
class TestTouchstoneOptions(unittest.TestCase):
|
||||
def setUp(self):
|
||||
|
@ -81,6 +82,21 @@ class TestTouchstoneTouchstone(unittest.TestCase):
|
|||
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_db_conversation(self):
|
||||
ts_db = Touchstone("./test/data/attenuator-0643_DB.s2p")
|
||||
ts_db.load()
|
||||
|
@ -111,3 +127,26 @@ class TestTouchstoneTouchstone(unittest.TestCase):
|
|||
self.assertIn("!freq ReS11 ImS11 ReS21 ImS21 ReS12 ImS12 ReS22 ImS22",
|
||||
ts.comments)
|
||||
|
||||
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)
|
||||
|
|
Ładowanie…
Reference in New Issue