| 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 |
import logging |
| 25 |
from time import sleep |
| 26 |
import numpy as np |
| 27 |
from pymeasure.instruments import Instrument, SCPIUnknownMixin |
| 28 |
from pymeasure.instruments.validators import ( |
| 29 |
strict_discrete_set, |
| 30 |
truncated_discrete_set, |
| 31 |
truncated_range, |
| 32 |
joined_validators |
| 33 |
) |
| 34 |
import re |
| 35 |
|
| 36 |
log = logging.getLogger(__name__) |
| 37 |
log.addHandler(logging.NullHandler()) |
| 38 |
|
| 39 |
# Analysis Results with Units, ie -24.5DBM -> (-24.5, 'DBM') |
| 40 |
r_value_units = re.compile(r"([-\d]*\.\d*)(.*)") |
| 41 |
|
| 42 |
# Join validators to allow for special sets of characters |
| 43 |
truncated_range_or_off = joined_validators(strict_discrete_set, truncated_range) |
| 44 |
|
| 45 |
|
| 46 |
def _int_or_neg_one(v): |
| 47 |
try: |
| 48 |
return int(v) |
| 49 |
except ValueError: |
| 50 |
return -1 |
| 51 |
|
| 52 |
|
| 53 |
def _parse_trace_peak(vals): |
| 54 |
"""Parse the returned value from a trace peak query.""" |
| 55 |
l, p = vals |
| 56 |
res = [l] |
| 57 |
m = r_value_units.match(p) |
| 58 |
if m is not None: |
| 59 |
data = list(m.groups()) |
| 60 |
data[0] = float(data[0]) |
| 61 |
res.extend(data) |
| 62 |
else: |
| 63 |
res.append(float(p)) |
| 64 |
return res |
| 65 |
|
| 66 |
|
| 67 |
class AnritsuMS9710C(SCPIUnknownMixin, Instrument): |
| 68 |
"""Anritsu MS9710C Optical Spectrum Analyzer.""" |
| 69 |
|
| 70 |
def __init__(self, adapter, name="Anritsu MS9710C Optical Spectrum Analyzer", **kwargs): |
| 71 |
"""Constructor.""" |
| 72 |
self.analysis_mode = None |
| 73 |
super().__init__(adapter, name=name, **kwargs) |
| 74 |
|
| 75 |
############# |
| 76 |
# Mappings # |
| 77 |
############# |
| 78 |
ONOFF = ["ON", "OFF"] |
| 79 |
ONOFF_MAPPING = {True: 'ON', False: 'OFF', 1: 'ON', 0: 'OFF', 'ON': 'ON', 'OFF': 'OFF'} |
| 80 |
|
| 81 |
###################### |
| 82 |
# Status Registers # |
| 83 |
###################### |
| 84 |
|
| 85 |
ese2 = Instrument.control( |
| 86 |
"ESE2?", "ESE2 %d", |
| 87 |
"Control Extended Event Status Enable Register 2", |
| 88 |
get_process=int |
| 89 |
) |
| 90 |
|
| 91 |
esr2 = Instrument.control( |
| 92 |
"ESR2?", "ESR2 %d", |
| 93 |
"Control Extended Event Status Register 2", |
| 94 |
get_process=_int_or_neg_one |
| 95 |
) |
| 96 |
|
| 97 |
########### |
| 98 |
# Modes # |
| 99 |
########### |
| 100 |
|
| 101 |
measure_mode = Instrument.measurement( |
| 102 |
"MOD?", "Get the current Measure Mode the OSA is in.", |
| 103 |
values={None: 0, "SINGLE": 1.0, "AUTO": 2.0, "POWER": 3.0}, |
| 104 |
map_values=True |
| 105 |
) |
| 106 |
|
| 107 |
#################################### |
| 108 |
# Spectrum Parameters - Wavelength # |
| 109 |
#################################### |
| 110 |
wavelength_center = Instrument.control( |
| 111 |
'CNT?', 'CNT %g', "Control Center Wavelength of Spectrum Scan in nm.") |
| 112 |
|
| 113 |
wavelength_span = Instrument.control( |
| 114 |
'SPN?', 'SPN %g', "Control Wavelength Span of Spectrum Scan in nm.") |
| 115 |
|
| 116 |
wavelength_start = Instrument.control( |
| 117 |
'STA?', 'STA %g', "Control Wavelength Start of Spectrum Scan in nm.") |
| 118 |
|
| 119 |
wavelength_stop = Instrument.control( |
| 120 |
'STO?', 'STO %g', "Control Wavelength Stop of Spectrum Scan in nm.") |
| 121 |
|
| 122 |
wavelength_marker_value = Instrument.control( |
| 123 |
'MKV?', 'MKV %s', |
| 124 |
"Control Wavelength Marker Value (wavelength or freq.?)", |
| 125 |
validator=strict_discrete_set, |
| 126 |
values=["WL", "FREQ"] |
| 127 |
) |
| 128 |
|
| 129 |
wavelength_value_in = Instrument.control( |
| 130 |
'WDP?', 'WDP %s', |
| 131 |
"Control Wavelength value in Vacuum or Air", |
| 132 |
validator=strict_discrete_set, |
| 133 |
values=["VACUUM", "AIR"] |
| 134 |
) |
| 135 |
|
| 136 |
level_scale = Instrument.measurement( |
| 137 |
'LVS?', "Get Current Level Scale", |
| 138 |
values=["LOG", "LIN"] |
| 139 |
) |
| 140 |
|
| 141 |
level_log = Instrument.control( |
| 142 |
"LOG?", "LOG %f", "Control Level Log Scale (/div)", |
| 143 |
validator=truncated_range, values=[0.1, 10.0] |
| 144 |
) |
| 145 |
|
| 146 |
level_lin = Instrument.control( |
| 147 |
"LIN?", "LIN %f", "Control Level Linear Scale (/div)", |
| 148 |
validator=truncated_range, values=[1e-12, 1] |
| 149 |
) |
| 150 |
|
| 151 |
level_opt_attn = Instrument.control( |
| 152 |
"ATT?", "ATT %s", "Control Optical Attenuation Status (ON/OFF)", |
| 153 |
validator=strict_discrete_set, |
| 154 |
values=ONOFF |
| 155 |
) |
| 156 |
|
| 157 |
resolution = Instrument.control( |
| 158 |
"RES?", "RES %f", "Control Resolution (nm)", |
| 159 |
validator=truncated_discrete_set, |
| 160 |
values=[0.05, 0.07, 0.1, 0.2, 0.5, 1.0] |
| 161 |
) |
| 162 |
|
| 163 |
resolution_actual = Instrument.control( |
| 164 |
"ARES?", "ARES %s", "Control Resolution Actual (ON/OFF)", |
| 165 |
validator=strict_discrete_set, |
| 166 |
values=ONOFF, |
| 167 |
map_values=True |
| 168 |
|
| 169 |
) |
| 170 |
|
| 171 |
resolution_vbw = Instrument.control( |
| 172 |
"VBW?", "VBW %s", "Control Video Bandwidth Resolution", |
| 173 |
validator=strict_discrete_set, |
| 174 |
values=["1MHz", "100kHz", "10kHz", "1kHz", "100Hz", "10Hz"] |
| 175 |
) |
| 176 |
|
| 177 |
average_point = Instrument.control( |
| 178 |
"AVT?", "AVT %d", |
| 179 |
"Control number of averages to take on each point (2-1000), or OFF", |
| 180 |
validator=truncated_range_or_off, |
| 181 |
values=[["OFF"], [2, 1000]] |
| 182 |
) |
| 183 |
|
| 184 |
average_sweep = Instrument.control( |
| 185 |
"AVS?", "AVS %d", |
| 186 |
"Control number of averages to make on a sweep (2-1000) or OFF", |
| 187 |
validator=truncated_range_or_off, |
| 188 |
values=[["OFF"], [2, 1000]] |
| 189 |
) |
| 190 |
|
| 191 |
sampling_points = Instrument.control( |
| 192 |
"MPT?", "MPT %d", "Control number of sampling points", |
| 193 |
validator=truncated_discrete_set, |
| 194 |
values=[51, 101, 251, 501, 1001, 2001, 5001], |
| 195 |
get_process=lambda v: int(v) |
| 196 |
) |
| 197 |
|
| 198 |
##################################### |
| 199 |
# Analysis Peak Search Parameters # |
| 200 |
##################################### |
| 201 |
|
| 202 |
peak_search = Instrument.control( |
| 203 |
"PKS?", "PKS %s", "Control Peak Search Mode", |
| 204 |
validator=strict_discrete_set, |
| 205 |
values=["PEAK", "NEXT", "LAST", "LEFT", "RIGHT"] |
| 206 |
) |
| 207 |
|
| 208 |
dip_search = Instrument.control( |
| 209 |
"DPS?", "DPS %s", "Control Dip Search Mode", |
| 210 |
validator=strict_discrete_set, |
| 211 |
values=["DIP", "NEXT", "LAST", "LEFT", "RIGHT"] |
| 212 |
) |
| 213 |
|
| 214 |
analysis = Instrument.control( |
| 215 |
"ANA?", "ANA %s", "Control Analysis Control" |
| 216 |
) |
| 217 |
|
| 218 |
analysis_result = Instrument.measurement( |
| 219 |
"ANAR?", "Get anaysis result from current scan." |
| 220 |
) |
| 221 |
|
| 222 |
########################## |
| 223 |
# Data Memory Commands # |
| 224 |
########################## |
| 225 |
|
| 226 |
data_memory_a_size = Instrument.measurement( |
| 227 |
'DBA?', |
| 228 |
"Get the number of points sampled in data memory register A." |
| 229 |
) |
| 230 |
|
| 231 |
data_memory_b_size = Instrument.measurement( |
| 232 |
'DBB?', |
| 233 |
"Get the number of points sampled in data memory register B." |
| 234 |
) |
| 235 |
|
| 236 |
data_memory_a_condition = Instrument.measurement( |
| 237 |
"DCA?", |
| 238 |
"""Get the data condition of data memory register A. |
| 239 |
Starting wavelength, and a sampling point (l1, l2, n).""" |
| 240 |
) |
| 241 |
|
| 242 |
data_memory_b_condition = Instrument.measurement( |
| 243 |
"DCB?", |
| 244 |
"""Get the data condition of data memory register B. |
| 245 |
Starting wavelength, and a sampling point (l1, l2, n).""" |
| 246 |
) |
| 247 |
|
| 248 |
data_memory_a_values = Instrument.measurement( |
| 249 |
"DMA?", |
| 250 |
"Get the binary data from memory register A." |
| 251 |
) |
| 252 |
|
| 253 |
data_memory_b_values = Instrument.measurement( |
| 254 |
"DMA?", |
| 255 |
"Get the binary data from memory register B." |
| 256 |
) |
| 257 |
|
| 258 |
data_memory_select = Instrument.control( |
| 259 |
"MSL?", "MSL %s", |
| 260 |
"Control Memory Data Select.", |
| 261 |
validator=strict_discrete_set, |
| 262 |
values=["A", "B"] |
| 263 |
) |
| 264 |
|
| 265 |
########################### |
| 266 |
# Trace Marker Commands # |
| 267 |
########################### |
| 268 |
|
| 269 |
trace_marker_center = Instrument.setting( |
| 270 |
"TMC %s", "Set Trace Marker at Center. Set to 1 or True to initiate command", |
| 271 |
map_values=True, |
| 272 |
values={True: ''} |
| 273 |
) |
| 274 |
|
| 275 |
trace_marker = Instrument.control( |
| 276 |
"TMK?", "TMK %f", |
| 277 |
"Control the trace marker with a wavelength. Returns the trace wavelength and power.", |
| 278 |
get_process=_parse_trace_peak |
| 279 |
) |
| 280 |
|
| 281 |
@property |
| 282 |
def wavelengths(self): |
| 283 |
"""Get a numpy array of the current wavelengths of scans.""" |
| 284 |
return np.linspace( |
| 285 |
self.wavelength_start, |
| 286 |
self.wavelength_stop, |
| 287 |
self.sampling_points |
| 288 |
) |
| 289 |
|
| 290 |
def read_memory(self, slot="A"): |
| 291 |
"""Read the scan saved in a memory slot.""" |
| 292 |
cond_attr = f"data_memory_{slot.lower()}_condition" |
| 293 |
data_attr = f"data_memory_{slot.lower()}_values" |
| 294 |
|
| 295 |
scan = getattr(self, cond_attr) |
| 296 |
wavelengths = np.linspace(scan[0], scan[1], int(scan[2])) |
| 297 |
power = np.fromstring(getattr(self, data_attr), sep="\r\n") |
| 298 |
|
| 299 |
return wavelengths, power |
| 300 |
|
| 301 |
def wait(self, n=3, delay=1): |
| 302 |
"""Query OPC Command and waits for appropriate response.""" |
| 303 |
log.info("Wait for OPC") |
| 304 |
res = self.ask("*OPC?") |
| 305 |
n_attempts = n |
| 306 |
while res == '': |
| 307 |
log.debug(f"Empty OPC Response. {n_attempts} remaining") |
| 308 |
if n_attempts == 0: |
| 309 |
break |
| 310 |
n_attempts -= 1 |
| 311 |
sleep(delay) |
| 312 |
res = self.read().strip() |
| 313 |
|
| 314 |
log.debug(res) |
| 315 |
|
| 316 |
def wait_for_sweep(self, n=20, delay=0.5): |
| 317 |
"""Wait for a sweep to stop. |
| 318 |
|
| 319 |
This is performed by checking bit 1 of the ESR2. |
| 320 |
""" |
| 321 |
log.debug("Waiting for spectrum sweep") |
| 322 |
|
| 323 |
while self.esr2 != 3 and n > 0: |
| 324 |
log.debug(f"Wait for sweep [{n}]") |
| 325 |
# log.debug("ESR2: {}".format(esr2)) |
| 326 |
sleep(delay) |
| 327 |
n -= 1 |
| 328 |
|
| 329 |
if n <= 0: |
| 330 |
log.warning(f"Sweep Timeout Occurred ({int(delay * n)} s)") |
| 331 |
|
| 332 |
def single_sweep(self, **kwargs): |
| 333 |
"""Perform a single sweep and wait for completion.""" |
| 334 |
log.debug("Performing a Spectrum Sweep") |
| 335 |
self.clear() |
| 336 |
self.write('SSI') |
| 337 |
self.wait_for_sweep(**kwargs) |
| 338 |
|
| 339 |
def center_at_peak(self, **kwargs): |
| 340 |
"""Center the spectrum at the measured peak.""" |
| 341 |
self.write("PKC") |
| 342 |
self.wait(**kwargs) |
| 343 |
|
| 344 |
def measure_peak(self): |
| 345 |
"""Measure the peak and return the trace marker.""" |
| 346 |
self.peak_search = "PEAK" |
| 347 |
return self.trace_marker |