Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for frequency tag #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/RsWaveform/meta/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"peak": None,
"samples": None,
"reflevel": None,
"date": None,
}

META_IQTAR_DEFAULTS = {
Expand Down
10 changes: 10 additions & 0 deletions src/RsWaveform/meta/meta_wv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""WV meta data for Storage container."""

import datetime
import typing

import numpy as np
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions src/RsWaveform/wv/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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\+]+)"),
Expand Down
9 changes: 9 additions & 0 deletions src/RsWaveform/wv/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion tests/data/dummy.wv
Original file line number Diff line number Diff line change
@@ -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}
{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}
2 changes: 1 addition & 1 deletion tests/data/dummy_2.wv
Original file line number Diff line number Diff line change
@@ -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}
{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}
Binary file modified tests/data/dummy_mwv.wv
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/test_wv.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def meta() -> Meta:
"peak": 0.0,
"samples": 2,
"reflevel": -20.0,
"center_frequency": 1234000000.0,
}
)

Expand All @@ -64,6 +65,7 @@ def meta_mwv() -> Meta:
"peak": 200.0,
"samples": 2000,
"reflevel": -10.0,
"center_frequency": 5678000000.0,
}
)

Expand Down Expand Up @@ -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
):
Expand Down
Loading