Debian Codegraph @

pymeasure/instruments/rohdeschwarz/fsl.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 import logging
26
27 import numpy as np
28 from pymeasure.instruments.validators import strict_discrete_set
29 from pymeasure.instruments import Instrument, SCPIMixin
30
31 log = logging.getLogger(__name__)
32 log.addHandler(logging.NullHandler())
33
34
35 def _number_or_auto(value):
36 # helper for the bandwidth setting
37 if isinstance(value, str) and value.upper() == "AUTO":
38 return ":AUTO ON"
39 else:
40 # There is no space in the set commands, so we have to add it
41 return " " + str(value)
42
43
44 class FSL(SCPIMixin, Instrument):
45 """
46 Represents a Rohde&Schwarz FSL spectrum analyzer.
47
48 All physical values that can be set can either be as a string of a value
49 and a unit (e.g. "1.2 GHz") or as a float value in the base units (Hz,
50 dBm, etc.).
51 """
52
53 def __init__(self, adapter, name="Rohde&Schwarz FSL", **kwargs):
54 super().__init__(
55 adapter, name,
56 **kwargs
57 )
58
59 # Frequency settings ------------------------------------------------------
60
61 freq_span = Instrument.control(
62 "FREQ:SPAN?",
63 "FREQ:SPAN %s",
64 "Frequency span in Hz.",
65 )
66
67 freq_center = Instrument.control(
68 "FREQ:CENT?",
69 "FREQ:CENT %s",
70 "Center frequency in Hz.",
71 )
72
73 freq_start = Instrument.control(
74 "FREQ:STAR?",
75 "FREQ:STAR %s",
76 "Start frequency in Hz.",
77 )
78
79 freq_stop = Instrument.control(
80 "FREQ:STOP?",
81 "FREQ:STOP %s",
82 "Stop frequency in Hz.",
83 )
84
85 attenuation = Instrument.control(
86 "INP:ATT?",
87 "INP:ATT %s",
88 "Attenuation in dB.",
89 )
90
91 res_bandwidth = Instrument.control(
92 "BAND:RES?",
93 # There is no space between RES and %s on purpose, see _number_or_auto.
94 "BAND:RES%s",
95 "Resolution bandwidth in Hz. Can be set to 'AUTO'",
96 set_process=_number_or_auto,
97 )
98
99 video_bandwidth = Instrument.control(
100 "BAND:VID?",
101 "BAND:VID%s",
102 "Video bandwidth in Hz. Can be set to 'AUTO'",
103 set_process=_number_or_auto,
104 )
105
106 # Sweeping ----------------------------------------------------------------
107
108 sweep_time = Instrument.control(
109 "SWE:TIME?",
110 # No space between TIME and %s on purpose, see _number_or_auto.
111 "SWE:TIME%s",
112 "Sweep time in s. Can be set to 'AUTO'.",
113 set_process=_number_or_auto,
114 )
115
116 continuous_sweep = Instrument.control(
117 "INIT:CONT?",
118 "INIT:CONT %s",
119 "Continuous (True) or single sweep (False)",
120 validator=strict_discrete_set,
121 values={True: 1, False: 0},
122 map_values=True,
123 )
124
125 def single_sweep(self):
126 """Perform a single sweep with synchronization."""
127 self.write("INIT; *WAI")
128
129 def continue_single_sweep(self):
130 """Continue with single sweep with synchronization."""
131 self.write("INIT:CONM; *WAI")
132
133 # Traces ------------------------------------------------------------------
134
135 def read_trace(self, n_trace=1):
136 """
137 Read trace data.
138
139 :param n_trace: The trace number (1-6). Default is 1.
140 :return: 2d numpy array of the trace data, [[frequency], [amplitude]].
141 """
142 y = np.array(self.values(f"TRAC{n_trace}? TRACE{n_trace}"))
143 x = np.linspace(self.freq_start, self.freq_stop, len(y))
144 return np.array([x, y])
145
146 trace_mode = Instrument.control(
147 "DISP:TRAC:MODE?",
148 "DISP:TRAC:MODE %s",
149 "Trace mode ('WRIT', 'MAXH', 'MINH', 'AVER' or 'VIEW')",
150 validator=strict_discrete_set,
151 values=["WRIT", "MAXH", "MINH", "AVER", "VIEW"],
152 )
153
154 # Markers -----------------------------------------------------------------
155
156 def create_marker(self, num=1, is_delta_marker=False):
157 """
158 Create a marker.
159
160 :param num: The marker number (1-4)
161 :param is_delta_marker: True if the marker is a delta marker, default
162 is False.
163 :return: The marker object.
164 """
165 return self.Marker(self, num, is_delta_marker)
166
167 class Marker:
168 def __init__(self, instrument, num, is_delta_marker):
169 """
170 Marker and Delta Marker class.
171
172 :param instrument: The FSL instrument.
173 :param num: The marker number (1-4)
174 :param is_delta_marker: True if the marker is a delta marker,
175 defaults to False.
176 """
177 self.instrument = instrument
178 self.is_delta_marker = is_delta_marker
179 # Building the marker name for the commands.
180 if self.is_delta_marker:
181 # Smallest delta marker number is 2.
182 self.name = "DELT" + str(max(2, num))
183 else:
184 self.name = "MARK"
185 if num > 1:
186 # Marker 1 doesn't get a number.
187 self.name = self.name + str(num)
188
189 self.activate()
190
191 def read(self):
192 return self.instrument.read()
193
194 def write(self, command):
195 self.instrument.write(f"CALC:{self.name}:{command}")
196
197 def ask(self, command):
198 return self.instrument.ask(f"CALC:{self.name}:{command}")
199
200 def values(self, command, **kwargs):
201 """
202 Reads a set of values from the instrument through the adapter,
203 passing on any keyword arguments.
204 """
205 return self.instrument.values(
206 f"CALC:{self.name}:{command}", **kwargs
207 )
208
209 def activate(self):
210 """Activate a marker."""
211 self.write("STAT ON")
212
213 def disable(self):
214 """Disable a marker."""
215 self.write("STAT OFF")
216
217 x = Instrument.control(
218 "X?", "X %s", "Position of marker on the frequency axis in Hz."
219 )
220
221 y = Instrument.control(
222 "Y?", "Y %s", "Amplitude of the marker position in dBm."
223 )
224
225 peak_excursion = Instrument.control(
226 "PEXC?",
227 "PEXC %s",
228 "Peak excursion in dB.",
229 )
230
231 def to_trace(self, n_trace=1):
232 """
233 Set marker to trace.
234
235 :param n_trace: The trace number (1-6). Default is 1.
236 """
237 self.write(f"TRAC {n_trace}")
238
239 def to_peak(self):
240 """Set marker to highest peak within the span."""
241 self.write("MAX")
242
243 def to_next_peak(self, direction="right"):
244 """
245 Set marker to next peak.
246
247 :param direction: Direction of the next peak ('left' or 'right' of
248 the current position).
249 """
250 self.write(f"MAX:{direction}")
251
252 def zoom(self, value):
253 """
254 Zoom in to a frequency span or by a factor.
255
256 :param value: The value to zoom in by. If a number is passed it is
257 interpreted as a factor. If a string (number with unit) is
258 passed it is interpreted as a frequency span.
259 """
260 self.write(f"FUNC:ZOOM {value}; *WAI")