Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Qmini spectrometer GUI #1314

Merged
merged 8 commits into from
Dec 20, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
1313 Improving Qmini spectrometer GUI and adding save functions
#################

API Breaks
----------
- N/A

Library Features
----------------
- N/A

Device Features
---------------
- N/A

New Devices
-----------
- N/A

Bugfixes
--------
- N/A

Maintenance
-----------
- Improving QminiSpectrometer.embedded.ui
- Adding QminiSpectrometer.detailed.ui
- Adding save_data() function and accompanying signals to qmini.py

Contributors
------------
- aberges-SLAC
52 changes: 52 additions & 0 deletions pcdsdevices/lasers/qmini.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
import logging
import os
import time

from ophyd import Component as Cpt
from ophyd import Device, EpicsSignal, EpicsSignalRO
from ophyd import FormattedComponent as FCpt
from ophyd.signal import AttributeSignal, Signal

from pcdsdevices.variety import set_metadata

Expand Down Expand Up @@ -83,6 +87,54 @@ class QminiSpectrometer(Device):
fit_stdev = Cpt(EpicsSignalRO, ':STDEV', kind='config')
fit_chisq = Cpt(EpicsSignalRO, ':CHISQ', kind='config')

# Save spectra functions
def save_data(self, file_dest: str = ''):
"""
Save the wavelength and spectrum PVs to a text file
"""
# Let's check to see if we set this in a non-gui context
if not file_dest.strip():
# let's try to use the signal instead
if not self.file_dest.get().strip():
# set a default destination for the file we didn't set it
_file = (os.getcwd() + '/' + self.name
+ time.strftime("_%Y-%m-%d_%H%M%S") + '.txt')
# otherwise just use it, silly
else:
_file = self.file_dest.get()
else:
_file = file_dest
self.log.info('Saving spectrum to disk...')
# Let's format to JSON for the science folk with sinful f-string mangling
_settings = ['sensitivity_cal', 'correct_prnu', 'correct_nonlinearity',
'normalize_exposure', 'adjust_offset', 'subtract_dark',
'remove_bad_pixels', 'remove_temp_bad_pixels']
_data = {'timestamp': time.strftime("%Y-%m-%d %H:%M:%S"),
'exposure (us)': self.exposure.get(),
'averages': self.exposures_to_average.get(),
# Lets do some sneaky conversion to bool from int
'settings': {f"{sig}": bool(getattr(self, sig).get())
for sig in _settings},
'wavelength (nm)': [str(x) for x in self.wavelengths.get()],
'intensity (a.u.)': [str(y) for y in self.spectrum.get()]
}
# and let's assume you have permission to save your file where you want to
with open(_file, 'w') as _f:
_f.write(json.dumps(_data))

save_spectrum = Cpt(AttributeSignal, attr='_save_spectrum', kind='omitted')
file_dest = Cpt(Signal, value='', kind='omitted')
set_metadata(save_spectrum, dict(variety='command-proc', value=1))

@property
def _save_spectrum(self):
return 0

# Setter will just save the data
@_save_spectrum.setter
def _save_spectrum(self, value):
self.save_data()


class QminiWithEvr(QminiSpectrometer):
"""
Expand Down
Loading