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