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:
'use_dark', 'bl_poly_deg', 'bl_signal_range', 'fields')
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 the run `runNB`. A thin wrapper around `toolbox_scs.load`.
......@@ -150,6 +150,8 @@ class Viking:
add_attrs: bool
if True, adds the camera parameters as attributes to the dataset
(see get_camera_params())
tid_offset: int
train Id offset of Newton camera
Output
------
......@@ -165,11 +167,20 @@ class Viking:
data2 = v.from_run(155) # load run 155
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),
'dim': ['newt_y', 'newt_x']}}}
run, data = tb.load(self.PROPOSAL, runNB,
fields=self.FIELDS, rois=roi)
data['newton'] = data['newton'].astype(float)
run, newton = tb.load(self.PROPOSAL, runNB, 'newton', rois=roi)
newton = newton.shift(trainId=tid_offset).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['newt_x']))
if add_attrs:
......@@ -259,13 +270,19 @@ class Viking:
'endX': 'imageSpecifications.endX.value',
'startY': 'imageSpecifications.startY.value',
'endY': 'imageSpecifications.endY.value',
'temperature': 'CoolerActual.temperature.value',
'temperature': 'coolerActual.temperature.value',
'high_capacity': 'HighCapacity.value',
'exposure_s': 'exposureTime.value'
}
ret = {}
for k, v in dic.items():
ret[k] = run.get_run_value('SCS_EXP_NEWTON/CAM/CAMERA', v)
try:
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['photoelectrons_per_count'] = self.e_per_counts(run, ret['gain'])
return ret
......@@ -380,7 +397,7 @@ class Viking:
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
camera pixels into energy in eV. The Viking spectrometer is calibrated
......@@ -402,6 +419,7 @@ class Viking:
energy_calib: np.array
the calibration coefficients (2nd degree polynomial)
"""
old_calib = self.ENERGY_CALIB
self.ENERGY_CALIB = [0, 1, 0]
x_pos = []
nrj = []
......@@ -411,11 +429,16 @@ class Viking:
for i, runNB in enumerate(runList):
if plot:
print(runNB)
xrange0 = self.X_RANGE
self.X_RANGE=xrange
ds = self.from_run(runNB)
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),
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:
popt, pcov = curve_fit(gauss1d, avg_spectrum['newt_x'].values,
avg_spectrum.values, p0=p0)
......@@ -423,17 +446,21 @@ class Viking:
gaussian_fits.append(popt)
spectra.append(avg_spectrum)
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:
print(f'error with run {runNB}:', e)
self.ENERGY_CALIB = old_calib
finally:
self.X_RANGE = xrange0
x_pos = np.array(x_pos)
nrj = np.array(nrj)
idx = np.argsort(x_pos)
x_pos = x_pos[idx]
nrj = nrj[idx]
energy_calib = np.polyfit(x_pos, nrj, 2)
energy_calib = np.polyfit(x_pos, nrj, order)
if plot:
plot_viking_calibration(spectra, x_pos, nrj, energy_calib,
gaussian_fits, runNBs)
self.ENERGY_CALIB = energy_calib
self.GAUSSIAN_FITS = gaussian_fits
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