Files
saresb/script/psss/CrystalHeightScan.py
2021-05-07 15:43:37 +02:00

82 lines
2.7 KiB
Python
Executable File

###############################################################################
#Scan the PSSS crystal height
#Purpose:
#The PSSS signal level is very sensitive to the crystal height. This script will scan the height and set the position to the maximum signal
if get_exec_pars().source == CommandSource.ui:
#User inputs - define travel range of camera
RANGE_FROM = -0.8
RANGE_TO = -1.7
STEPS = 10 #20
NUM_SHOTS= 10 # 100
PLOT=None
p = plot(None, title="Data")[0] if (PLOT is None) else PLOT
p.clear()
p.removeMarker(None)
p.setLegendVisible(True)
p.addSeries(LinePlotSeries("PSSS Spectrum Average"))
run("cpython/wrapper")
#Setup and functions setup¶
if not is_dry_run():
xstal_height=Channel("SARFE10-PSSS059:MOTOR_Y3.VAL", name="xstal_height")
else:
xstal_height=DummyRegister("xstal_height")
av = create_averager(psss_spectrum_y, NUM_SHOTS, interval=-1, name="spectrum_average")
av_samples = av.samples
av_samples.alias = "spectrum_samples"
#Scan and take data
def after_read(record, scan):
p.getSeries(0).setData(psss_spectrum_x.take(), record[av])
p.setTitle("Xtal Height = %1.3f" %(record[xstal_height]))
r = lscan(xstal_height, (av, av_samples), RANGE_FROM, RANGE_TO, STEPS, latency=2.0, after_read = after_read, save=False)
#User inputs - define travel range of crystal
#It is unlikely these values need to be changed
average, samples, xstal_range = r.getReadable(0), r.getReadable(1), r.getPositions(0)
#return maxium position
[amp, mean_val, sigma, offset], projection = fit_crystal_height(RANGE_FROM, RANGE_TO, STEPS+1, samples)
print(mean_val)
if not (RANGE_FROM < mean_val < RANGE_TO or RANGE_TO < mean_val < RANGE_FROM):
raise Exception ("Invalid fit mean: " + str(mean_val))
#Set max position
#Cell below will push the maximum position to the xstal height
xstal_height.write(mean_val)
xstal_height.close()
#Plots
"""
plt.figure(figsize=[10,5])
plt.subplot(121)
plt.title('PSSS scan of crystal height')
plt.pcolormesh(energy_axis, xstal_range, Scan_spec.mean(axis=1),cmap='CMRmap')
plt.xlim([energy_axis[0],energy_axis[-1]])
plt.ylim([xstal_range[0], xstal_range[-1]])
plt.xlabel('PSSS energy axis')
plt.ylabel('Set crystal position [mm] \n'+PSSS_xstal_height_name[0:-4])
plt.subplot(122)
plt.plot(projection,xstal_range,linewidth = 2, color = 'orange',label ='projected signal')
plt.plot(gaus(xstal_range_fit,*popt),xstal_range_fit,'r:',label='fit')
plt.ylim([xstal_range[0], xstal_range[-1]])
plt.title('Signal max at %.3f [mm] (from fit)'%popt[1])
plt.xticks([])
plt.legend()
plt.grid(True)
"""
p.clear()
p.setTitle("")
plot_gauss_fit(xstal_range, projection, gauss_pars=(offset, amp, mean_val, sigma), p=p, title = "Data")
set_return(mean_val)