Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions python/pysmurf/client/command/smurf_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,7 @@ def _caput(self, pvname, val, index=-1, cast_type=True, write_log=False, log_lev
# setDisp handles enum values
var.setDisp(val, index=index)
elif cast_type:
# rogue is strict about variable types for arrays
var_val = var.value() # like get(read=False)
if isinstance(var_val, np.ndarray):
var_type = var_val.dtype.type
else:
var_type = type(var_val)
val = var_type(val)
val = tools.coerce_value_for_var(var, val)
var.set(val, check=wait_done, index=index)
else:
var.set(val, check=wait_done, index=index)
Expand Down Expand Up @@ -1173,7 +1167,7 @@ def set_eta_scan_amplitude(self, band, val, **kwargs):
"""
self._caput(
self._cryo_root(band) + self._eta_scan_amplitude_reg,
np.uint(val), **kwargs)
np.uint64(val), **kwargs)

def get_eta_scan_amplitude(self, band, **kwargs):
"""
Expand Down Expand Up @@ -1424,7 +1418,7 @@ def set_amplitude_scale_array(self, band, val, **kwargs):
"""
self._caput(
self._cryo_root(band) + self._amplitude_scale_array_reg,
np.array(val).astype(np.uint), **kwargs)
np.array(val).astype(np.uint64), **kwargs)

def get_amplitude_scale_array(self, band, **kwargs):
"""
Expand Down Expand Up @@ -1461,7 +1455,7 @@ def set_amplitude_scale_array_currentchans(self, band, tone_power,

old_amp = self.get_amplitude_scale_array(band, **kwargs)
n_channels=self.get_number_channels(band)
new_amp = np.zeros((n_channels,),dtype=np.uint)
new_amp = np.zeros((n_channels,), dtype=np.uint64)
new_amp[np.where(old_amp!=0)] = tone_power
self.set_amplitude_scale_array(self, new_amp, **kwargs)

Expand Down Expand Up @@ -2637,7 +2631,7 @@ def set_amplitude_scale_channel(self, band, channel, val,
self._caput(
self._channel_root(band, channel) +
self._amplitude_scale_channel_reg,
np.uint(val), **kwargs)
np.uint64(val), **kwargs)

def get_amplitude_scale_channel(self, band, channel, **kwargs):
"""
Expand Down
61 changes: 61 additions & 0 deletions python/pysmurf/client/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,67 @@
import numpy as np
from scipy.optimize import curve_fit


TYPEMAP = {
'UInt8': np.uint8,
'UInt16': np.uint16,
'UInt32': np.uint32,
'UInt64': np.uint64,
'Int8': np.int8,
'Int16': np.int16,
'Int32': np.int32,
'Int64': np.int64,
'Float32': np.float32,
'Double': np.float64,
'Bool': bool,
'String': str,
}


def coerce_value_for_var(var, val):
"""Coerce val to the type expected by a Rogue 6 variable.

Uses var.typeStr metadata to determine the expected type without
performing a register read. Handles scalar registers, array registers
identified by the '[np]' typeStr suffix, and raises TypeError for any
unrecognized typeStr.

Parameters
----------
var : pyrogue.Variable
The rogue variable node that will receive the value.
val : any
The value to coerce.

Returns
-------
any
val cast to the type expected by var. For scalar registers, returns
TYPEMAP[var.typeStr](val). For array registers (typeStr ending in
'[np]'), returns np.asarray(val, dtype=element_dtype), which is a
zero-copy passthrough when the dtype already matches (ARRY-02).

Raises
------
TypeError
If var.typeStr is not recognized. The exception message includes
both the variable path (var.path) and the unrecognized typeStr.
"""
ts = var.typeStr
if ts.endswith('[np]'):
base = ts[:-4]
if base not in TYPEMAP:
raise TypeError(
f"Unrecognized typeStr '{ts}' for variable '{var.path}'"
)
return np.asarray(val, dtype=TYPEMAP[base])
if ts not in TYPEMAP:
raise TypeError(
f"Unrecognized typeStr '{ts}' for variable '{var.path}'"
)
return TYPEMAP[ts](val)


def skewed_lorentzian(x, bkg, bkg_slp, skw, mintrans, res_f, Q):
""" Skewed Lorentzian model.

Expand Down