Commit 14677c11 authored by Francesco Tripaldi's avatar Francesco Tripaldi
Browse files

Improve configuring aatributes

parent d9493931
......@@ -11,6 +11,7 @@
# attr;ScriptErr;string;''
# attr;publish_to_elog;bool;1
# attr;graph_title;string;GUN grid Scan
# attr;elog_text;string;Scan Graph
# attr;best_vgrid_result;double;0
# attr;plot_best_vgrid;bool;1
......@@ -23,12 +24,14 @@ import io
import matplotlib.pyplot as plt
import requests
def fig2elog(fig):
def fig2elog(fig, text):
buf = io.BytesIO()
fig.savefig(buf)
buf.seek(0)
try:
rs = requests.post('http://elog.elettra.eu/upload/upload.php', files={'file': ('booster_performance.png', buf, 'image/png')})
rs = requests.post('http://elog.elettra.eu/upload/upload.php', files={'file': ('booster_performance.png', buf, 'image/png')}, data={'string':text})
except Exception as e:
selfseq_dev.write_attribute("ScriptErr", f"{type(e).__name__} while connection to elog: {e}") # scrive l'eventuale errore sull'attributo "ScriptErr" della sequenza
sys.exit(1)
......@@ -47,28 +50,46 @@ def main():
scan_start = selfseq_dev.read_attribute("scan_start").value
scan_stop = selfseq_dev.read_attribute("scan_stop").value
scan_step = selfseq_dev.read_attribute("scan_step").value
dcct_samples = selfseq_dev.read_attribute("dcct_mean_samples").value
dcct_samples = int(selfseq_dev.read_attribute("dcct_mean_samples").value)
starting_gun = gun_dev.read_attribute('GridVoltage').value
selfseq_dev.write_attribute("ScriptErr","")
publish_to_elog = bool(selfseq_dev.read_attribute("publish_to_elog").value)
plot_best_vgrid = bool(selfseq_dev.read_attribute("plot_best_vgrid").value)
graph_title = selfseq_dev.read_attribute("graph_title").value
elog_text = str(selfseq_dev.read_attribute("elog_text").value)
trigger_gun = PyTango.DeviceProxy('booster/accessi/plc_accessi')
timing_enable = PyTango.DeviceProxy('b/timing/injexttiming_b')
except Exception as e:
#print(str(e))
selfseq_dev.write_attribute("ScriptErr", f"{type(e).__name__} while defining tango devices: {e}") # scrive l'eventuale errore sull'attributo "ScriptErr" della sequenza
sys.exit(1)
if bool(trigger_gun.read_attribute("gun_trigger_on").value) == False:
selfseq_dev.write_attribute("ScriptErr", 'GUN DISABLED')
sys.exit(1)
first_time = False
for x in np.arange(scan_start,scan_stop,scan_step):
if str(selfseq_dev.State()) != 'RUNNING':
selfseq_dev.write_attribute("ScriptErr","Sequence not running")
gun_dev.write_attribute('FastGridVoltage',starting_gun)
sys.exit(1)
if bool(timing_enable.read_attribute("ExtractionEnabled").value) == False:
selfseq_dev.write_attribute("ScriptErr", 'SE Extraction DISABLED')
sys.exit(1)
if float(gun_dev.read_attribute('GridVoltage').value) < 0.5 :
selfseq_dev.write_attribute("ScriptErr","Grid disabled or zero voltage during scan")
gun_dev.write_attribute('FastGridVoltage',starting_gun)
sys.exit(1)
#print("valore gun: " + str(x))
gun_dev.write_attribute('FastGridVoltage',x)
if not first_time:
#print('first time')
time.sleep(0.1)
time.sleep(0.3)
first_time = True
temp_store =0
for mean in range(0, dcct_samples,1):
......@@ -78,34 +99,40 @@ def main():
X.append(x)
Y.append(dcct_value)
gun_dev.write_attribute('FastGridVoltage',starting_gun)
smooth_width = 50
z = np.polyfit(X, Y, 5)
p = np.poly1d(z)
xp = np.linspace(scan_start, scan_stop, smooth_width)
# create convolution kernel for calculating
# the smoothed second order derivative
smooth_width = int((scan_stop - scan_start)/scan_step)
xp = xp[:-3]
x1 = np.linspace(-3,3,smooth_width)
norm = np.sum(np.exp(-x1**2)) * (x1[1]-x1[0]) # ad hoc normalization
y1 = (4*x1**2 - 2) * np.exp(-x1**2) / smooth_width *8#norm*(x1[1]-x1[0])
yp=p(xp)
# calculate second order deriv.
y_conv = np.convolve(Y, y1, mode="same")
#print(np.where(y_conv == np.amin(y_conv))[0][0])
best_vgrid = X[np.where(y_conv == np.amin(y_conv))[0][0]]
y_conv = np.convolve(y1, yp, mode="same")
best_vgrid = xp[np.where(y_conv == np.amin(y_conv))[0][0]]
best_current = yp[np.where(y_conv == np.amin(y_conv))[0][0]]
gun_dev.write_attribute('FastGridVoltage',best_vgrid)
selfseq_dev.write_attribute("best_vgrid_result", float(best_vgrid))
best_current = Y[np.where(y_conv == np.amin(y_conv))[0][0]]
#print(best_vgrid)
if publish_to_elog == True:
plt.style.use(['seaborn-darkgrid'])
if plot_best_vgrid == True:
plt.plot(best_vgrid, best_current, marker="o", markersize=15, markeredgecolor="red", markerfacecolor="green")
plt.text(scan_start+((scan_stop-scan_start)/2), 0.05, 'Best gun Vgrid: '+str(best_vgrid)+'V', fontsize = 12, bbox = dict(facecolor = 'red', alpha = 0.5))
plt.plot(X, Y,label = "GUN Vgrid")
#plt.plot(X,y_conv, label = "second deriv")
plt.text(scan_start+((scan_stop-scan_start)/2), 0.05,'Best Vgrid: '+f'{best_vgrid:.2f}'+'V', fontsize = 12, bbox = dict(facecolor = 'red', alpha = 0.5))
plt.scatter(X, Y,label = "GUN Vgrid scan data")
plt.plot(xp,p(xp), label = "Fitting")
#plt.plot(xp,y_conv[:-3], label = "2nd order derivate")
plt.title(str(graph_title))
plt.xlabel('Vgrid')
plt.xlabel('GUN Vgrid')
plt.ylabel('Booster current')
plt.legend(loc=0)
fig = plt.gcf()
fig2elog(fig)
fig2elog(fig,elog_text)
sys.exit(0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment