diff --git a/README.md b/README.md index 53d5ab1..a372d26 100644 --- a/README.md +++ b/README.md @@ -204,7 +204,7 @@ To run the tests, run the following command: ```sh $ pip install tox -$ tox -e pytest +$ tox -e test ``` This will run all the tests for the package and report any issues or failures. Make sure that you have installed the necessary dependencies before running the tests. diff --git a/src/RsWaveform/wv/Load.py b/src/RsWaveform/wv/Load.py index 3efe592..f15a9fe 100644 --- a/src/RsWaveform/wv/Load.py +++ b/src/RsWaveform/wv/Load.py @@ -33,7 +33,7 @@ def load(self, file: typing.Union[str, typing.IO, Path]) -> ParentStorage: content = fp.read() tags = self._split_data_via_tags_meta(content) - samples = int(tags.pop("samples")) + samples = int(tags["samples"]) mwv_segment_count = int(tags.pop("mwv_segment_count", 1)) tags.update(self._split_data_via_tags_waveform(content)) @@ -135,7 +135,7 @@ def load_meta(self, file: typing.Union[str, typing.IO, Path]) -> ParentStorage: header_content, _ = self._read_chunks(fp, separators) tags = self._split_data_via_tags_meta(header_content) - samples = int(tags.pop("samples")) + samples = int(tags["samples"]) tags.update( self._split_data_via_tags_control_list_width4(header_content, samples) ) @@ -257,7 +257,9 @@ def _extract_meta(tags: dict, index: int = 0) -> dict: marker.update({key: marker_list}) continue elif key == "samples": - continue + value = int(value) + elif key == "reflevel": + value = float(value) elif key == "mwv_segment_level_offs": possible_values = value.split(",") offset = possible_values[index * 2] @@ -415,6 +417,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 + ("REFLEVEL", r"", r"[\d\.e\-\+]+"), # Reference level for playback ("VECTOR MAX", r"", r"[\d\.\-e\+]+"), # tbd ("LEVEL OFFS", r"", r"([\d\.\-e\+]+),([\d\.\-e\+]+)"), # Level offset to dBFS diff --git a/src/RsWaveform/wv/Save.py b/src/RsWaveform/wv/Save.py index c7766c3..e0046cc 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_reflevel(fp, data.storages[0]) self._write_control_length(fp, data.storages[0]) self._write_control_list(fp, data.storages[0]) @@ -79,6 +80,7 @@ def _write_mwv( self._write_copyright(fp, tmp_storage) self._write_date(fp, datas) self._write_samples(fp, tmp_storage) + self._write_reflevel(fp, tmp_storage) self._write_mwv_segment_count(fp, datas) self._write_mwv_segment_length(fp, datas.storages) @@ -168,6 +170,13 @@ def _write_samples(file: typing.IO, data: Storage): line = f"{{SAMPLES:{len(data.data)}}}" file.write(line.encode("utf-8")) + @staticmethod + def _write_reflevel(file: typing.IO, data: Storage): + reflevel = data.meta.get("reflevel") + if reflevel: + line = f"{{REFLEVEL:{reflevel:.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 19925fb..2893b2a 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}{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}{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 19925fb..2893b2a 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}{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}{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 fe3e4bd..b228f42 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 a51d5c1..d914072 100644 --- a/tests/test_wv.py +++ b/tests/test_wv.py @@ -44,6 +44,8 @@ def meta() -> Meta: "encryption_flag": False, "rms": 2.220459, "peak": 0.0, + "samples": 2, + "reflevel": -20.0, } ) @@ -60,6 +62,8 @@ def meta_mwv() -> Meta: "encryption_flag": False, "rms": 200.0, "peak": 200.0, + "samples": 2000, + "reflevel": -10.0, } )