Skip to content
Snippets Groups Projects
Commit 01c89028 authored by Laurent Mercadier's avatar Laurent Mercadier
Browse files

improve calibrate() and fix issue with get_camera_params()

parent d7b247dc
No related branches found
No related tags found
1 merge request!326Up8062
...@@ -138,7 +138,7 @@ class Viking: ...@@ -138,7 +138,7 @@ class Viking:
'use_dark', 'bl_poly_deg', 'bl_signal_range', 'fields') 'use_dark', 'bl_poly_deg', 'bl_signal_range', 'fields')
return {param: getattr(self, param.upper()) for param in params} return {param: getattr(self, param.upper()) for param in params}
def from_run(self, runNB, add_attrs=True): def from_run(self, runNB, add_attrs=True, tid_offset=-1):
"""load a run """load a run
Load the run `runNB`. A thin wrapper around `toolbox_scs.load`. Load the run `runNB`. A thin wrapper around `toolbox_scs.load`.
...@@ -150,6 +150,8 @@ class Viking: ...@@ -150,6 +150,8 @@ class Viking:
add_attrs: bool add_attrs: bool
if True, adds the camera parameters as attributes to the dataset if True, adds the camera parameters as attributes to the dataset
(see get_camera_params()) (see get_camera_params())
tid_offset: int
train Id offset of Newton camera
Output Output
------ ------
...@@ -165,11 +167,20 @@ class Viking: ...@@ -165,11 +167,20 @@ class Viking:
data2 = v.from_run(155) # load run 155 data2 = v.from_run(155) # load run 155
data = xarray.concat([data1, data2], 'trainId') # combine both data = xarray.concat([data1, data2], 'trainId') # combine both
""" """
#separately load newton image and deal with trainId mismatch
roi = {'newton': {'newton': {'roi': (self.Y_RANGE, self.X_RANGE), roi = {'newton': {'newton': {'roi': (self.Y_RANGE, self.X_RANGE),
'dim': ['newt_y', 'newt_x']}}} 'dim': ['newt_y', 'newt_x']}}}
run, data = tb.load(self.PROPOSAL, runNB, run, newton = tb.load(self.PROPOSAL, runNB, 'newton', rois=roi)
fields=self.FIELDS, rois=roi) newton = newton.shift(trainId=tid_offset).astype(float)
data['newton'] = data['newton'].astype(float)
#load the rest
fields = [f for f in self.FIELDS if f != 'newton']
if len(fields) == 0:
data = newton
else:
run, data = tb.load(self.PROPOSAL, runNB,
fields=fields)
data = data.merge(newton, join='inner')
data = data.assign_coords(newt_x=np.polyval(self.ENERGY_CALIB, data = data.assign_coords(newt_x=np.polyval(self.ENERGY_CALIB,
data['newt_x'])) data['newt_x']))
if add_attrs: if add_attrs:
...@@ -259,13 +270,19 @@ class Viking: ...@@ -259,13 +270,19 @@ class Viking:
'endX': 'imageSpecifications.endX.value', 'endX': 'imageSpecifications.endX.value',
'startY': 'imageSpecifications.startY.value', 'startY': 'imageSpecifications.startY.value',
'endY': 'imageSpecifications.endY.value', 'endY': 'imageSpecifications.endY.value',
'temperature': 'CoolerActual.temperature.value', 'temperature': 'coolerActual.temperature.value',
'high_capacity': 'HighCapacity.value', 'high_capacity': 'HighCapacity.value',
'exposure_s': 'exposureTime.value' 'exposure_s': 'exposureTime.value'
} }
ret = {} ret = {}
for k, v in dic.items(): try:
ret[k] = run.get_run_value('SCS_EXP_NEWTON/CAM/CAMERA', v) for k, v in dic.items():
ret[k] = run.get_run_value('SCS_EXP_NEWTON/CAM/CAMERA', v)
except Exception as e:
print(e)
dic['temperature'] = 'CoolerActual.temperature.value'
for k, v in dic.items():
ret[k] = run.get_run_value('SCS_EXP_NEWTON/CAM/CAMERA', v)
ret['gain'] = self.get_camera_gain(run) ret['gain'] = self.get_camera_gain(run)
ret['photoelectrons_per_count'] = self.e_per_counts(run, ret['gain']) ret['photoelectrons_per_count'] = self.e_per_counts(run, ret['gain'])
return ret return ret
...@@ -380,7 +397,7 @@ class Viking: ...@@ -380,7 +397,7 @@ class Viking:
return ds return ds
def calibrate(self, runList, plot=True): def calibrate(self, runList, source='nrj', order=2, xrange=slice(None,None), plot=True):
""" """
This routine determines the calibration coefficients to translate the This routine determines the calibration coefficients to translate the
camera pixels into energy in eV. The Viking spectrometer is calibrated camera pixels into energy in eV. The Viking spectrometer is calibrated
...@@ -402,6 +419,7 @@ class Viking: ...@@ -402,6 +419,7 @@ class Viking:
energy_calib: np.array energy_calib: np.array
the calibration coefficients (2nd degree polynomial) the calibration coefficients (2nd degree polynomial)
""" """
old_calib = self.ENERGY_CALIB
self.ENERGY_CALIB = [0, 1, 0] self.ENERGY_CALIB = [0, 1, 0]
x_pos = [] x_pos = []
nrj = [] nrj = []
...@@ -411,11 +429,16 @@ class Viking: ...@@ -411,11 +429,16 @@ class Viking:
for i, runNB in enumerate(runList): for i, runNB in enumerate(runList):
if plot: if plot:
print(runNB) print(runNB)
xrange0 = self.X_RANGE
self.X_RANGE=xrange
ds = self.from_run(runNB) ds = self.from_run(runNB)
self.integrate(ds) self.integrate(ds)
avg_spectrum = ds['spectrum'].mean(dim='trainId') ds = self.removePolyBaseline(ds)
avg_spectrum = ds['spectrum_nobl'].mean(dim='trainId')
p0 = [np.max(avg_spectrum)-np.min(avg_spectrum), p0 = [np.max(avg_spectrum)-np.min(avg_spectrum),
avg_spectrum.argmax().values, 10, np.min(avg_spectrum)] avg_spectrum['newt_x'][avg_spectrum.argmax()].values,
10, np.min(avg_spectrum)]
eV_unit = 1e3 if 'UND' in source else 1
try: try:
popt, pcov = curve_fit(gauss1d, avg_spectrum['newt_x'].values, popt, pcov = curve_fit(gauss1d, avg_spectrum['newt_x'].values,
avg_spectrum.values, p0=p0) avg_spectrum.values, p0=p0)
...@@ -423,17 +446,21 @@ class Viking: ...@@ -423,17 +446,21 @@ class Viking:
gaussian_fits.append(popt) gaussian_fits.append(popt)
spectra.append(avg_spectrum) spectra.append(avg_spectrum)
runNBs.append(runNB) runNBs.append(runNB)
nrj.append(tb.load_run_values(self.PROPOSAL, runNB)['nrj']) nrj.append(tb.load_run_values(self.PROPOSAL, runNB)[source]*eV_unit)
except Exception as e: except Exception as e:
print(f'error with run {runNB}:', e) print(f'error with run {runNB}:', e)
self.ENERGY_CALIB = old_calib
finally:
self.X_RANGE = xrange0
x_pos = np.array(x_pos) x_pos = np.array(x_pos)
nrj = np.array(nrj) nrj = np.array(nrj)
idx = np.argsort(x_pos) idx = np.argsort(x_pos)
x_pos = x_pos[idx] x_pos = x_pos[idx]
nrj = nrj[idx] nrj = nrj[idx]
energy_calib = np.polyfit(x_pos, nrj, 2) energy_calib = np.polyfit(x_pos, nrj, order)
if plot: if plot:
plot_viking_calibration(spectra, x_pos, nrj, energy_calib, plot_viking_calibration(spectra, x_pos, nrj, energy_calib,
gaussian_fits, runNBs) gaussian_fits, runNBs)
self.ENERGY_CALIB = energy_calib self.ENERGY_CALIB = energy_calib
self.GAUSSIAN_FITS = gaussian_fits
return energy_calib return energy_calib
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment