Debian Codegraph @

pymeasure/instruments/agilent/agilent8722ES.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 from pymeasure.instruments import Instrument, SCPIUnknownMixin
26 from pymeasure.instruments.validators import discreteTruncate
27 from pymeasure.errors import RangeException
28 from pyvisa import VisaIOError
29
30 import numpy as np
31 import re
32 from io import BytesIO
33 import warnings
34
35
36 class Agilent8722ES(SCPIUnknownMixin, Instrument):
37 """ Represents the Agilent8722ES Vector Network Analyzer
38 and provides a high-level interface for taking scans of the
39 scattering parameters.
40 """
41
42 SCAN_POINT_VALUES = [3, 11, 21, 26, 51, 101, 201, 401, 801, 1601]
43 SCATTERING_PARAMETERS = ("S11", "S12", "S21", "S22")
44 S11, S12, S21, S22 = SCATTERING_PARAMETERS
45
46 start_frequency = Instrument.control(
47 "STAR?", "STAR %e Hz",
48 """ A floating point property that represents the start frequency
49 in Hz. This property can be set.
50 """
51 )
52 stop_frequency = Instrument.control(
53 "STOP?", "STOP %e Hz",
54 """ A floating point property that represents the stop frequency
55 in Hz. This property can be set.
56 """
57 )
58 sweep_time = Instrument.control(
59 "SWET?", "SWET%.2e",
60 """ A floating point property that represents the sweep time
61 in seconds. This property can be set.
62 """
63 )
64 averages = Instrument.control(
65 "AVERFACT?", "AVERFACT%d",
66 """ An integer representing the number of averages to take. Note that
67 averaging must be enabled for this to take effect. This property can be set.
68 """,
69 cast=lambda x: int(float(x)) # need float() to convert scientific notation in strings
70 )
71 averaging_enabled = Instrument.control(
72 "AVERO?", "AVERO%d",
73 """ A bool that indicates whether or not averaging is enabled. This property
74 can be set.""",
75 cast=bool
76 )
77
78 def __init__(self, adapter, name="Agilent 8722ES Vector Network Analyzer", **kwargs):
79 super().__init__(
80 adapter,
81 name,
82 **kwargs
83 )
84
85 def set_fixed_frequency(self, frequency):
86 """ Sets the scan to be of only one frequency in Hz """
87 self.start_frequency = frequency
88 self.stop_frequency = frequency
89 self.scan_points = 3
90
91 @property
92 def parameter(self):
93 for parameter in Agilent8722ES.SCATTERING_PARAMETERS:
94 if int(self.values("%s?" % parameter)) == 1:
95 return parameter
96 return None
97
98 @parameter.setter
99 def parameter(self, value):
100 if value in Agilent8722ES.SCATTERING_PARAMETERS:
101 self.write("%s" % value)
102 else:
103 raise Exception("Invalid scattering parameter requested"
104 " for Agilent 8722ES")
105
106 @property
107 def scan_points(self):
108 """ Gets the number of scan points
109 """
110 search = re.search(r"\d\.\d+E[+-]\d{2}$", self.ask("POIN?"),
111 re.MULTILINE)
112 if search:
113 return int(float(search.group()))
114 else:
115 raise Exception("Improper message returned for the"
116 " number of points")
117
118 @scan_points.setter
119 def scan_points(self, points):
120 """ Sets the number of scan points, truncating to an allowed
121 value if not properly provided
122 """
123 points = discreteTruncate(points, Agilent8722ES.SCAN_POINT_VALUES)
124 if points:
125 self.write("POIN%d" % points)
126 else:
127 raise RangeException("Maximum scan points (1601) for"
128 " Agilent 8722ES exceeded")
129
130 def set_IF_bandwidth(self, bandwidth):
131 """ Sets the resolution bandwidth (IF bandwidth) """
132 allowedBandwidth = [10, 30, 100, 300, 1000, 3000, 3700, 6000]
133 bandwidth = discreteTruncate(bandwidth, allowedBandwidth)
134 if bandwidth:
135 self.write("IFBW%d" % bandwidth)
136 else:
137 raise RangeException("Maximum IF bandwidth (6000) for Agilent "
138 "8722ES exceeded")
139
140 def set_averaging(self, averages):
141 """Sets the number of averages and enables/disables averaging. Should be
142 between 1 and 999"""
143 averages = int(averages)
144 if not 1 <= averages <= 999:
145 assert RangeException("Set", averages, "must be in the range 1 to 999")
146 self.averages = averages
147 self.averaging_enabled = (averages > 1)
148
149 def disable_averaging(self):
150 """Disables averaging"""
151 warnings.warn(
152 "Don't use disable_averaging(), use averaging_enabled = False instead", FutureWarning)
153 self.averaging_enabled = False
154
155 def enable_averaging(self):
156 """Enables averaging"""
157 warnings.warn(
158 "Don't use enable_averaging(), use averaging_enabled = True instead", FutureWarning)
159 self.averaging_enabled = True
160
161 def is_averaging(self):
162 """ Returns True if averaging is enabled """
163 warnings.warn("Don't use is_averaging(), use averaging_enabled instead", FutureWarning)
164 return self.averaging_enabled
165
166 def restart_averaging(self, averages):
167 warnings.warn("Don't use restart_averaging(), use scan_single() instead", FutureWarning)
168 self.scan_single()
169
170 def scan(self, averages=None, blocking=None, timeout=None, delay=None):
171 """ Initiates a scan with the number of averages specified and
172 blocks until the operation is complete.
173 """
174 if averages is not None or blocking is not None or timeout is not None or delay is not None:
175 warnings.warn(
176 "averages, blocking, timeout, and delay arguments are no longer used by scan()",
177 FutureWarning
178 )
179 self.write("*CLS")
180 self.scan_single()
181 # All queries will block until the scan is done, so use NOOP? to check.
182 # These queries will time out after several seconds though,
183 # so query repeatedly until the scan finishes.
184 while True:
185 try:
186 self.ask("NOOP?")
187 except VisaIOError as e:
188 if e.abbreviation != "VI_ERROR_TMO":
189 raise e
190 else:
191 break
192
193 def scan_single(self):
194 """ Initiates a single scan """
195 if self.averaging_enabled:
196 self.write("NUMG%d" % self.averages)
197 else:
198 self.write("SING")
199
200 def scan_continuous(self):
201 """ Initiates a continuous scan """
202 self.write("CONT")
203
204 @property
205 def frequencies(self):
206 """ Returns a list of frequencies from the last scan
207 """
208 return np.linspace(
209 self.start_frequency,
210 self.stop_frequency,
211 num=self.scan_points
212 )
213
214 @property
215 def data_complex(self):
216 """ Returns the complex power from the last scan
217 """
218 # TODO: Implement binary transfer instead of ASCII
219 data = np.loadtxt(
220 BytesIO(self.ask("FORM4;OUTPDATA").encode()),
221 delimiter=',',
222 dtype=np.float32
223 )
224 data_complex = data[:, 0] + 1j * data[:, 1]
225 return data_complex
226
227 @property
228 def data_log_magnitude(self):
229 """ Returns the absolute magnitude values in dB from the last scan
230 """
231 return 20 * np.log10(self.data_magnitude)
232
233 @property
234 def data_magnitude(self):
235 """ Returns the absolute magnitude values from the last scan
236 """
237 return np.abs(self.data_complex)
238
239 @property
240 def data_phase(self):
241 """ Returns the phase in degrees from the last scan
242 """
243 return np.degrees(np.angle(self.data_complex))
244
245 @property
246 def data(self):
247 """ Returns the real and imaginary data from the last scan
248 """
249 warnings.warn("Don't use this function, use data_complex instead", FutureWarning)
250 data_complex = self.data_complex
251 return data_complex.real, data_complex.complex
252
253 def log_magnitude(self, real, imaginary):
254 """ Returns the magnitude in dB from a real and imaginary
255 number or numpy arrays
256 """
257 warnings.warn("Don't use log_magnitude(), use data_log_magnitude instead", FutureWarning)
258 return 20 * np.log10(self.magnitude(real, imaginary))
259
260 def magnitude(self, real, imaginary):
261 """ Returns the magnitude from a real and imaginary
262 number or numpy arrays
263 """
264 warnings.warn("Don't use magnitude(), use data_magnitude", FutureWarning)
265 return np.sqrt(real**2 + imaginary**2)
266
267 def phase(self, real, imaginary):
268 """ Returns the phase in degrees from a real and imaginary
269 number or numpy arrays
270 """
271 warnings.warn("Don't use phase(), use data_phase instead", FutureWarning)
272 return np.arctan2(imaginary, real) * 180 / np.pi