Getting started
Install the library
Install the optional example dependencies if you want to run the web server, Prometheus exporter, or plotting examples:
Connect to a device
Use a context manager so the USB handle or Bluetooth connection is always released:
from radiacode import RadiaCode
with RadiaCode() as device:
print(device.serial_number())
print(device.fw_version())
With no arguments, RadiaCode connects to the first available USB device. Use
serial_number when more than one USB device is connected:
For Bluetooth, pass the device identifier:
See Connections for platform requirements and error handling.
Read measurements
data_buf() returns all records currently buffered by the device. A call can
contain several record types, so narrow them with isinstance:
from radiacode import RadiaCode, RareData, RealTimeData
with RadiaCode() as device:
for record in device.data_buf():
if isinstance(record, RealTimeData):
print(record.dt, record.count_rate, record.dose_rate)
elif isinstance(record, RareData):
print(record.dt, record.temperature, record.charge_level)
The records already contain timestamps calculated from the device data. See Measurements and units before converting dose values.
Read a spectrum
from radiacode import RadiaCode, spectrum_channel_to_energy
with RadiaCode() as device:
spectrum = device.spectrum()
for channel, counts in enumerate(spectrum.counts):
energy_kev = spectrum_channel_to_energy(
channel, spectrum.a0, spectrum.a1, spectrum.a2
)
print(channel, energy_kev, counts)
The returned Spectrum.duration is a datetime.timedelta; call
total_seconds() when a numeric duration is needed.