| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import TYPE_CHECKING |
| 4 | |
| 5 | if TYPE_CHECKING: |
| 6 | from collections.abc import Sequence |
| 7 | |
| 8 | import sys |
| 9 | from enum import Enum |
| 10 | |
| 11 | import numpy as np |
| 12 | |
| 13 | import skrf |
| 14 | from skrf.vi import vna |
| 15 | from skrf.vi.validators import ( |
| 16 | BooleanValidator, |
| 17 | DelimitedStrValidator, |
| 18 | EnumValidator, |
| 19 | FloatValidator, |
| 20 | FreqValidator, |
| 21 | IntValidator, |
| 22 | ) |
| 23 | from skrf.vi.vna import VNA, ValuesFormat |
| 24 | |
| 25 | |
| 26 | class SweepType(Enum): |
| 27 | LINEAR = "LIN" |
| 28 | LOG = "LOG" |
| 29 | SEGMENT = "SEGM" |
| 30 | POWER = "POW" |
| 31 | CW = "CW" |
| 32 | POINT = "POIN" |
| 33 | PULSE = "PULS" |
| 34 | IAMPLITUDE = "IAMP" |
| 35 | IPHASE = "IPH" |
| 36 | |
| 37 | |
| 38 | class SweepMode(Enum): |
| 39 | SINGLE = "0" |
| 40 | CONTINUOUS = "1" |
| 41 | |
| 42 | |
| 43 | class AveragingMode(Enum): |
| 44 | AUTO = "AUTO" |
| 45 | FLATTEN = "FLAT" |
| 46 | REDUCE = "RED" |
| 47 | MOVING = "MOV" |
| 48 | |
| 49 | |
| 50 | class RSVNA(VNA): |
| 51 | class Channel(vna.Channel): |
| 52 | def __init__(self, parent, cnum: int, cname: str): |
| 53 | super().__init__(parent, cnum, cname) |
| 54 | |
| 55 | if cnum != 1: |
| 56 | default_msmnt = f"CH{self.cnum}_S11_1" |
| 57 | self.create_measurement(default_msmnt, "S11") |
| 58 | |
| 59 | def _on_delete(self): |
| 60 | self.write(f"CONF:CHAN{self.cnum} OFF") |
| 61 | |
| 62 | freq_start = VNA.command( |
| 63 | get_cmd="SENS<self:cnum>:FREQ:STAR?", |
| 64 | set_cmd="SENS<self:cnum>:FREQ:STAR <arg>", |
| 65 | doc="""The start frequency [Hz].""", |
| 66 | validator=FreqValidator(), |
| 67 | ) |
| 68 | |
| 69 | freq_stop = VNA.command( |
| 70 | get_cmd="SENS<self:cnum>:FREQ:STOP?", |
| 71 | set_cmd="SENS<self:cnum>:FREQ:STOP <arg>", |
| 72 | doc="""The stop frequency [Hz].""", |
| 73 | validator=FreqValidator(), |
| 74 | ) |
| 75 | |
| 76 | freq_span = VNA.command( |
| 77 | get_cmd="SENS<self:cnum>:FREQ:SPAN?", |
| 78 | set_cmd="SENS<self:cnum>:FREQ:SPAN <arg>", |
| 79 | doc="""The frequency span [Hz].""", |
| 80 | validator=FreqValidator(), |
| 81 | ) |
| 82 | |
| 83 | freq_center = VNA.command( |
| 84 | get_cmd="SENS<self:cnum>:FREQ:CENT?", |
| 85 | set_cmd="SENS<self:cnum>:FREQ:CENT <arg>", |
| 86 | doc="""The frequency center [Hz].""", |
| 87 | validator=FreqValidator(), |
| 88 | ) |
| 89 | |
| 90 | freq_fixed = VNA.command( |
| 91 | get_cmd="SENS<self:cnum>:FREQ:FIX?", |
| 92 | set_cmd="SENS<self:cnum>:FREQ:FIX <arg>", |
| 93 | doc="""The fixed frequency [Hz].""", |
| 94 | validator=FreqValidator(), |
| 95 | ) |
| 96 | |
| 97 | npoints = VNA.command( |
| 98 | get_cmd="SENS<self:cnum>:SWE:POIN?", |
| 99 | set_cmd="SENS<self:cnum>:SWE:POIN <arg>", |
| 100 | doc="""The number of frequency points. Sets the frequency step as a |
| 101 | side effect. |
| 102 | """, |
| 103 | validator=IntValidator(), |
| 104 | ) |
| 105 | |
| 106 | if_bandwidth = VNA.command( |
| 107 | get_cmd="SENS<self:cnum>:BWID?", |
| 108 | set_cmd="SENS<self:cnum>:BWID <arg>", |
| 109 | doc="""The IF bandwidth [Hz]""", |
| 110 | validator=FreqValidator(), |
| 111 | ) |
| 112 | |
| 113 | freq_step = VNA.command( |
| 114 | get_cmd="SENS<self:cnum>:SWE:STEP?", |
| 115 | set_cmd="SENS<self:cnum>:SWE:STEP <arg>", |
| 116 | doc="""The frequency step size [Hz]""", |
| 117 | validator=FreqValidator(), |
| 118 | ) |
| 119 | |
| 120 | sweep_time = VNA.command( |
| 121 | get_cmd="SENS<self:cnum>:SWE:TIME?", |
| 122 | set_cmd="SENS<self:cnum>:SWE:TIME <arg>", |
| 123 | doc="""The time in seconds for a single sweep [s]""", |
| 124 | validator=FloatValidator(), |
| 125 | ) |
| 126 | |
| 127 | sweep_type = VNA.command( |
| 128 | get_cmd="SENS<self:cnum>:SWE:TYPE?", |
| 129 | set_cmd="SENS<self:cnum>:SWE:TYPE <arg>", |
| 130 | doc="""The type of sweep (linear, log, etc)""", |
| 131 | validator=EnumValidator(SweepType), |
| 132 | ) |
| 133 | |
| 134 | averaging_on = VNA.command( |
| 135 | get_cmd="SENS<self:cnum>:AVER:STATE?", |
| 136 | set_cmd="SENS<self:cnum>:AVER:STATE <arg>", |
| 137 | doc="""Whether averaging is on or off""", |
| 138 | validator=BooleanValidator(), |
| 139 | ) |
| 140 | |
| 141 | averaging_count = VNA.command( |
| 142 | get_cmd="SENS<self:cnum>:AVER:COUN?", |
| 143 | set_cmd="SENS<self:cnum>:AVER:COUN <arg>", |
| 144 | doc="""The number of measurements combined for an average""", |
| 145 | validator=IntValidator(1, 1000), |
| 146 | ) |
| 147 | |
| 148 | averaging_mode = VNA.command( |
| 149 | get_cmd="SENS<self:cnum>:AVER:MODE?", |
| 150 | set_cmd="SENS<self:cnum>:AVER:MODE <arg>", |
| 151 | doc="""How measurements are averaged together""", |
| 152 | validator=EnumValidator(AveragingMode), |
| 153 | ) |
| 154 | |
| 155 | sweep_mode = VNA.command( |
| 156 | get_cmd="INIT<self:cnum>:CONT?", |
| 157 | set_cmd="INIT<self:cnum>:CONT <arg>", |
| 158 | doc="""This channel's sweep mode""", |
| 159 | validator=EnumValidator(SweepMode), |
| 160 | ) |
| 161 | |
| 162 | @property |
| 163 | def frequency(self) -> skrf.Frequency: |
| 164 | f = skrf.Frequency( |
| 165 | start=self.freq_start, |
| 166 | stop=self.freq_stop, |
| 167 | npoints=self.npoints, |
| 168 | unit="Hz", |
| 169 | ) |
| 170 | return f |
| 171 | |
| 172 | @frequency.setter |
| 173 | def frequency(self, f: skrf.Frequency) -> None: |
| 174 | self.freq_start = f.start |
| 175 | self.freq_stop = f.stop |
| 176 | self.npoints = f.npoints |
| 177 | |
| 178 | @property |
| 179 | def measurements(self) -> list[tuple[str, str]]: |
| 180 | msmnts = self.query(f"CALC{self.cnum}:PAR:CAT?").strip("'").split(",") |
| 181 | return list(zip(msmnts[::2], msmnts[1::2])) |
| 182 | |
| 183 | @property |
| 184 | def measurement_names(self) -> list[str]: |
| 185 | return [msmnt[0] for msmnt in self.measurements] |
| 186 | |
| 187 | @property |
| 188 | def calibration(self) -> skrf.Calibration: |
| 189 | raise NotImplementedError() |
| 190 | |
| 191 | @calibration.setter |
| 192 | def calibration(self, cal: skrf.Calibration) -> None: |
| 193 | raise NotImplementedError() |
| 194 | |
| 195 | @property |
| 196 | def active_trace_sdata(self) -> np.ndarray: |
| 197 | active_measurement = self.query(f"CALC{self.cnum}:PAR:SEL?").replace("'", "").split(",")[0] |
| 198 | if active_measurement == "": |
| 199 | raise RuntimeError("No trace is active. Must select measurement first.") |
| 200 | return self.query_values(f"CALC{self.cnum}:DATA? SDATA", complex_values=True) |
| 201 | |
| 202 | def clear_averaging(self) -> None: |
| 203 | self.write(f"SENS{self.cnum}:AVER:CLE") |
| 204 | |
| 205 | def create_measurement(self, name: str, parameter: str) -> None: |
| 206 | self.write(f"CALC{self.cnum}:PAR:SDEF '{name}','{parameter}'") |
| 207 | self.write(f"DISP:TRAC:EFE '{name}'") |
| 208 | |
| 209 | def delete_measurement(self, name: str) -> None: |
| 210 | self.write(f"CALC{self.cnum}:PAR:DEL '{name}'") |
| 211 | |
| 212 | def get_measurement(self, name: str) -> skrf.Network: |
| 213 | if name not in self.measurement_names: |
| 214 | raise KeyError(f"{name} does not exist") |
| 215 | |
| 216 | self.parent.active_measurement = name |
| 217 | ntwk = self.get_active_trace() |
| 218 | ntwk.name = name |
| 219 | return ntwk |
| 220 | |
| 221 | def get_active_trace(self) -> skrf.Network: |
| 222 | self.sweep() |
| 223 | orig_query_fmt = self.parent.query_format |
| 224 | self.parent.query_format = ValuesFormat.BINARY_64 |
| 225 | |
| 226 | ntwk = skrf.Network() |
| 227 | ntwk.frequency = self.frequency |
| 228 | ntwk.s = self.active_trace_sdata |
| 229 | |
| 230 | self.parent.query_format = orig_query_fmt |
| 231 | |
| 232 | return ntwk |
| 233 | |
| 234 | def get_sdata(self, a: int | str, b: int | str) -> skrf.Network: |
| 235 | self.sweep() |
| 236 | orig_query_fmt = self.parent.query_format |
| 237 | self.parent.query_format = ValuesFormat.BINARY_64 |
| 238 | param = f"S{a}{b}" |
| 239 | self.create_measurement("SKRF_TMP", param) |
| 240 | self.parent.active_measurement = "SKRF_TMP" |
| 241 | |
| 242 | ntwk = skrf.Network() |
| 243 | ntwk.frequency = self.frequency |
| 244 | ntwk.s = self.active_trace_sdata |
| 245 | |
| 246 | self.delete_measurement("SKRF_TMP") |
| 247 | self.parent.query_format = orig_query_fmt |
| 248 | |
| 249 | return ntwk |
| 250 | |
| 251 | def create_sparam_group(self, ports: Sequence[int]) -> None: |
| 252 | self.write(f"CALC{self.cnum}:PAR:DEF:SGR {','.join(map(str, ports))}") |
| 253 | |
| 254 | def get_snp_network( |
| 255 | self, |
| 256 | ports: Sequence | None = None, |
| 257 | ) -> skrf.Network: |
| 258 | if ports is None: |
| 259 | ports = list(range(1, self.parent.nports + 1)) |
| 260 | |
| 261 | orig_query_fmt = self.parent.query_format |
| 262 | self.parent.query_format = ValuesFormat.BINARY_64 |
| 263 | self.parent.active_channel = self |
| 264 | |
| 265 | self.create_sparam_group(ports) |
| 266 | |
| 267 | self.sweep() |
| 268 | |
| 269 | raw = self.query_values( |
| 270 | f"CALC{self.cnum}:DATA:SGR? SDAT", |
| 271 | container=np.array, |
| 272 | complex_values=True, |
| 273 | ) |
| 274 | self.parent.wait_for_complete() |
| 275 | |
| 276 | ntwk = skrf.Network() |
| 277 | ntwk.frequency = self.frequency |
| 278 | ntwk.s = raw.reshape(len(ports), len(ports), -1).transpose(2, 0, 1) |
| 279 | |
| 280 | self.parent.query_format = orig_query_fmt |
| 281 | |
| 282 | return ntwk |
| 283 | |
| 284 | def sweep(self) -> None: |
| 285 | orig_sweep_mode = self.sweep_mode |
| 286 | self.sweep_mode = SweepMode.SINGLE |
| 287 | self.parent._resource.clear() |
| 288 | |
| 289 | self.write(f"INIT{self.cnum}:IMM") |
| 290 | self.parent.wait_for_complete() |
| 291 | self.sweep_mode = orig_sweep_mode |
| 292 | |
| 293 | def __init__(self, address: str, backend: str = "@py") -> None: |
| 294 | super().__init__(address, backend) |
| 295 | |
| 296 | self._resource.read_termination = "\n" |
| 297 | self._resource.write_termination = "\n" |
| 298 | |
| 299 | self.create_channel(1, "Channel 1") |
| 300 | self.active_channel = self.ch1 |
| 301 | |
| 302 | self.model = self.id.split(",")[1] |
| 303 | if self.model not in self._models: |
| 304 | print( |
| 305 | f"WARNING: This model ({self.model}) has not been tested with " |
| 306 | "scikit-rf. By default, all features are turned on but older " |
| 307 | "instruments might be missing SCPI support for some commands " |
| 308 | "which will cause errors. Consider submitting an issue on GitHub to " |
| 309 | "help testing and adding support.", |
| 310 | file=sys.stderr, |
| 311 | ) |
| 312 | |
| 313 | def _supports(self, feature: str) -> bool: |
| 314 | model_config = self._models.get(self.model, self._models["default"]) |
| 315 | return feature not in model_config["unsupported"] |
| 316 | |
| 317 | def _model_param(self, param: str): |
| 318 | model_config = self._models.get(self.model, self._models["default"]) |
| 319 | return model_config[param] |
| 320 | |
| 321 | channels = VNA.command( |
| 322 | get_cmd="CONF:CHAN<self:cnum>:CAT?", |
| 323 | set_cmd=None, |
| 324 | doc="""The channel numbers and names currently in use""", |
| 325 | validator=DelimitedStrValidator(), |
| 326 | ) |
| 327 | |
| 328 | @property |
| 329 | def nports(self) -> int: |
| 330 | if self._supports("nports"): |
| 331 | return int(self.query("INST:PORT:COUN?")) |
| 332 | else: |
| 333 | return self._model_param("nports") |
| 334 | |
| 335 | @property |
| 336 | def active_channel(self) -> Channel | None: |
| 337 | active_ch = int(self.query("INST:NSEL?")) |
| 338 | return next(ch for ch in self.channels if ch.cnum == active_ch) |
| 339 | |
| 340 | @active_channel.setter |
| 341 | def active_channel(self, ch: Channel) -> None: |
| 342 | if self.active_channel.cnum == ch.cnum: |
| 343 | return |
| 344 | |
| 345 | self.write(f"INST:NSEL {ch.cnum}") |
| 346 | |
| 347 | @property |
| 348 | def query_format(self) -> vna.ValuesFormat: |
| 349 | fmt = self.query("FORM?") |
| 350 | if fmt == "ASC,0": |
| 351 | self._values_fmt = vna.ValuesFormat.ASCII |
| 352 | elif fmt == "REAL,32": |
| 353 | self._values_fmt = vna.ValuesFormat.BINARY_32 |
| 354 | elif fmt == "REAL,64": |
| 355 | self._values_fmt = vna.ValuesFormat.BINARY_64 |
| 356 | return self._values_fmt |
| 357 | |
| 358 | @query_format.setter |
| 359 | def query_format(self, fmt: vna.ValuesFormat) -> None: |
| 360 | if fmt == vna.ValuesFormat.ASCII: |
| 361 | self._values_fmt = vna.ValuesFormat.ASCII |
| 362 | self.write("FORM ASC,0") |
| 363 | elif fmt == vna.ValuesFormat.BINARY_32: |
| 364 | self._values_fmt = vna.ValuesFormat.BINARY_32 |
| 365 | self.write("FORM:BORD SWAP") |
| 366 | self.write("FORM REAL,32") |
| 367 | elif fmt == vna.ValuesFormat.BINARY_64: |
| 368 | self._values_fmt = vna.ValuesFormat.BINARY_64 |
| 369 | self.write("FORM:BORD SWAP") |
| 370 | self.write("FORM REAL,64") |
| 371 | |
| 372 | @property |
| 373 | def active_measurement(self) -> str: |
| 374 | return self.query(f"CALC{self.cnum}:PAR:SEL?") |
| 375 | |
| 376 | @active_measurement.setter |
| 377 | def active_measurement(self, name: str) -> None: |
| 378 | measurements = {name: channel for channel in self.channels for name in channel.measurement_names} |
| 379 | |
| 380 | if name not in measurements: |
| 381 | raise KeyError(f"{name} does not exist") |
| 382 | |
| 383 | self.write(f"CALC{measurements[name].cnum}:PAR:SEL '{name}'") |