diff --git a/src/RsWaveform/meta/defaults.py b/src/RsWaveform/meta/defaults.py index 543700b..4c1b4aa 100644 --- a/src/RsWaveform/meta/defaults.py +++ b/src/RsWaveform/meta/defaults.py @@ -12,6 +12,7 @@ "peak": None, "samples": None, "reflevel": None, + "date": None, } META_IQTAR_DEFAULTS = { diff --git a/src/RsWaveform/meta/meta_wv.py b/src/RsWaveform/meta/meta_wv.py index 7f86d5a..9183bb4 100644 --- a/src/RsWaveform/meta/meta_wv.py +++ b/src/RsWaveform/meta/meta_wv.py @@ -1,5 +1,6 @@ """WV meta data for Storage container.""" +import datetime import typing import numpy as np @@ -115,3 +116,12 @@ def reflevel(self) -> typing.Optional[float]: @reflevel.setter def reflevel(self, value: float) -> None: self._items["reflevel"] = value + + @property + def date(self) -> typing.Optional[datetime.datetime]: + """The timestamp of the loaded file.""" + return self._items.get("date") + + @date.setter + def date(self, value: float) -> None: + self._items["date"] = value diff --git a/src/RsWaveform/wv/load.py b/src/RsWaveform/wv/load.py index 572ccbd..61f4048 100644 --- a/src/RsWaveform/wv/load.py +++ b/src/RsWaveform/wv/load.py @@ -260,6 +260,9 @@ def _extract_meta(tags: dict, index: int = 0) -> dict: value = int(value) elif key == "reflevel": value = float(value) + elif key == "frequency": + meta.update({"center_frequency": float(value)}) + value = None elif key == "mwv_segment_level_offs": possible_values = value.split(",") offset = possible_values[index * 2] @@ -417,6 +420,7 @@ def _get_regex_tokens() -> list: ("DATE", r"", r"[\d\-;\:]+"), # Timestamp of waveform ("SAMPLES", r"", r"[\de\-\+]+"), # Sample count ("CLOCK", r"", r"[\d\.e\-\+]+"), # Waveform sampling frequency + ("FREQUENCY", r"", r"[\d\.e\-\+]+"), # center frequency of the signal ("REFLEVEL", r"", r"[\d\.e\-\+]+"), # Reference level for playback ("VECTOR MAX", r"", r"[\d\.\-e\+]+"), # tbd ("LEVEL OFFS", r"", r"([\d\.\-e\+]+),([\d\.\-e\+]+)"), diff --git a/src/RsWaveform/wv/save.py b/src/RsWaveform/wv/save.py index bbf7f43..f566e08 100644 --- a/src/RsWaveform/wv/save.py +++ b/src/RsWaveform/wv/save.py @@ -50,6 +50,7 @@ def _write( self._write_date(fp, data) self._write_clock(fp, data.storages[0]) self._write_samples(fp, data.storages[0]) + self._write_frequency(fp, data.storages[0]) self._write_reflevel(fp, data.storages[0]) self._write_control_length(fp, data.storages[0]) @@ -80,6 +81,7 @@ def _write_mwv( self._write_copyright(fp, tmp_storage) self._write_date(fp, datas) self._write_samples(fp, tmp_storage) + self._write_frequency(fp, tmp_storage) self._write_reflevel(fp, tmp_storage) self._write_mwv_segment_count(fp, datas) @@ -177,6 +179,13 @@ def _write_reflevel(file: typing.IO, data: Storage): line = f"{{REFLEVEL:{reflevel:.6f}}}" file.write(line.encode("utf-8")) + @staticmethod + def _write_frequency(file: typing.IO, data: Storage): + frequency = data.meta.get("center_frequency") + if frequency: + line = f"{{FREQUENCY:{frequency:.6f}}}" + file.write(line.encode("utf-8")) + @staticmethod def _write_marker(file: typing.IO, data: Storage): marker_keys = [ diff --git a/tests/data/dummy.wv b/tests/data/dummy.wv index 2893b2a..85eeefd 100644 --- a/tests/data/dummy.wv +++ b/tests/data/dummy.wv @@ -1 +1 @@ -{TYPE:SMU-WV}{COPYRIGHT:Rohde & Schwarz}{COMMENT:Test waveform file}{LEVEL OFFS:2.220459,0.000000}{DATE:2023-01-05;10:03:52}{CLOCK:100000000.0}{SAMPLES:2}{REFLEVEL:-20.000000}{CONTROL LENGTH:2}{CONTROL LIST WIDTH4-2:#i}{MARKER LIST 1: 0:1;32:0;63:0}{EMPTYTAG-223:# }{WAVEFORM-9:#š33ÍLff} \ No newline at end of file +{TYPE:SMU-WV}{COPYRIGHT:Rohde & Schwarz}{COMMENT:Test waveform file}{LEVEL OFFS:2.220459,0.000000}{DATE:2023-01-05;10:03:52}{CLOCK:100000000.0}{SAMPLES:2}{FREQUENCY:1234000000.000000}{REFLEVEL:-20.000000}{CONTROL LENGTH:2}{CONTROL LIST WIDTH4-2:#i}{MARKER LIST 1: 0:1;32:0;63:0}{EMPTYTAG-223:# }{WAVEFORM-9:#š33ÍLff} \ No newline at end of file diff --git a/tests/data/dummy_2.wv b/tests/data/dummy_2.wv index 2893b2a..85eeefd 100644 --- a/tests/data/dummy_2.wv +++ b/tests/data/dummy_2.wv @@ -1 +1 @@ -{TYPE:SMU-WV}{COPYRIGHT:Rohde & Schwarz}{COMMENT:Test waveform file}{LEVEL OFFS:2.220459,0.000000}{DATE:2023-01-05;10:03:52}{CLOCK:100000000.0}{SAMPLES:2}{REFLEVEL:-20.000000}{CONTROL LENGTH:2}{CONTROL LIST WIDTH4-2:#i}{MARKER LIST 1: 0:1;32:0;63:0}{EMPTYTAG-223:# }{WAVEFORM-9:#š33ÍLff} \ No newline at end of file +{TYPE:SMU-WV}{COPYRIGHT:Rohde & Schwarz}{COMMENT:Test waveform file}{LEVEL OFFS:2.220459,0.000000}{DATE:2023-01-05;10:03:52}{CLOCK:100000000.0}{SAMPLES:2}{FREQUENCY:1234000000.000000}{REFLEVEL:-20.000000}{CONTROL LENGTH:2}{CONTROL LIST WIDTH4-2:#i}{MARKER LIST 1: 0:1;32:0;63:0}{EMPTYTAG-223:# }{WAVEFORM-9:#š33ÍLff} \ No newline at end of file diff --git a/tests/data/dummy_mwv.wv b/tests/data/dummy_mwv.wv index b228f42..e72f43b 100644 Binary files a/tests/data/dummy_mwv.wv and b/tests/data/dummy_mwv.wv differ diff --git a/tests/test_wv.py b/tests/test_wv.py index e172f98..ccc10a1 100644 --- a/tests/test_wv.py +++ b/tests/test_wv.py @@ -46,6 +46,7 @@ def meta() -> Meta: "peak": 0.0, "samples": 2, "reflevel": -20.0, + "center_frequency": 1234000000.0, } ) @@ -64,6 +65,7 @@ def meta_mwv() -> Meta: "peak": 200.0, "samples": 2000, "reflevel": -10.0, + "center_frequency": 5678000000.0, } ) @@ -135,6 +137,13 @@ def test_loader_meta_only(reference_waveform_file_name: str, meta): assert obtained_meta == ref_meta +def test_meta_provides_date_field(reference_waveform_file_name: str, meta): + loader = Load() + parent_storage = loader.load_meta(reference_waveform_file_name) + obtained_meta = parent_storage.storages[0].meta + assert obtained_meta.date == meta["date"] + + def test_loader_mwv( reference_waveform_mwv_file_name: str, meta_mwv: dict, reference_mwv: np.ndarray ):