Commit b22aaa84 authored by bliss administrator's avatar bliss administrator
Browse files

continuation of the work to convert the spec macro to Python;

we realised we were not converting the right focusing method, so here
are the latest changes to do interaction matrix calculations...
To be continued
parent 59fef344
......@@ -13,6 +13,44 @@ import functools
import numpy as np
import time
def kb_slit_scan(axis, rel_start, rel_end, npts, ctime, counter, name="kbf_scan"):
slit_scan = dscan(axis, rel_start, rel_end, npts, ctime, counter, return_scan=True, save=False, name=name)
scan_data = scans.get_data(slit_scan)
print "SLIT SCAN DATA"
print scan_data[counter.name]
print scan_data[axis.name]
#### Fit scan data
coeffs = np.polyfit(scan_data[axis.name], scan_data[counter.name], 2)
print "COEFFS SLIT SCAN"
print coeffs
# Display and save statistics about 1st scan:
# min/max/pic_to_valley/average/std_deviation on raw and fitted data.
# Stats are saved to have an history of the changes.
# stats_display_and_save(scan_data, coeffs)
return scan_data, coeffs
"""
def kb_calc_INTM(coeffs, bender_inc):
# Calculation of interaction matrix.
ka1 = ( coeffs[1][0] - coeffs[0][1] ) / bender_inc
ka2 = ( coeffs[2][0] - coeffs[1][1] ) / bender_inc
kb1 = ( coeffs[1][1] - coeffs[0][2] ) / bender_inc
kb2 = ( coeffs[2][1] - coeffs[1][2] ) / bender_inc
# NB : no use of "c" coeffs ?
det = ka1*kb2 - kb1*ka2
if det == 0:
raise ValueError("determinent of interaction matrix is 0....")
if ka1*ka2*kb1*kb2 == 0:
raise ValueError("error: there is a 0 element in interaction matrix")
return numpy.array([[ka1, kb1], [ka2, kb2]])
"""
def kb_focus(name, bender_inc, rel_start, rel_end, npts=20, ctime=0.01, method="intm"):
cfg = static.get_config().get(name)
slits_axes = [slit_cfg['axis'] for slit_cfg in cfg['slits']]
......@@ -21,10 +59,8 @@ def kb_focus(name, bender_inc, rel_start, rel_end, npts=20, ctime=0.01, method="
rel_max = float(cfg['scan']['rel_max'])
bpm_device = cfg['bpm']['device']
bpm_counter = getattr(bpm_device, cfg['bpm']['counter'])
wait_after_bend = ???
fits_coeff = list()
bender_stabilisation_time = 1 #stabilisation time (in s.)
fits_coeffs = list()
if rel_end > rel_max:
raise RuntimeError("kb_focus: cannot move axis %r more than rel_max (=%f)" % (offset_axis.name, rel_max))
......@@ -37,146 +73,85 @@ def kb_focus(name, bender_inc, rel_start, rel_end, npts=20, ctime=0.01, method="
"""
1st scan
"""
scan_index = 1
scan1 = dscan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter,
return_scan=True, save=False, name="kbf_scan1")
scan1_data = scans.get_data(scan1)
print "DATA OF SCAN1"
print scan1_data[bpm_counter.name]
print scan1_data[offset_axis.name]
# # # # Fit scan1
coeffs = np.polyfit(scan1_data[offset_axis.name], scan1_data[bpm_counter.name], 2)
print "COEFFS SCAN1"
print coeffs
scan1_data, coeffs = kb_slit_scan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter, name="kbf_scan1")
# Save coefficients of 1st fit.
fits_coeff.append(coeffs)
# Display and save statistics about 1st scan:
# min/max/pic_to_valley/average/std_deviation on raw and fitted data.
# Stats are saved to have an history of the changes.
stats_dispaly_and_save(scan_index, scan1_data, fits_coeff)
fits_coeffs.append(coeffs)
"""
1st move ( interaction matrix method only )
"""
# Move only DOWN bender
bender_axes[0].move_relative(bender_inc)
time.sleep(wait_after_bend)
bender_axes[1].move_relative(bender_inc)
time.sleep(bender_stabilisation_time)
"""
2nd scan
"""
scan_index = 2
scan2 = dscan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter,
return_scan=True, save=False, name="kbf_scan2")
scan2_data = scans.get_data(scan2)
print "DATA OF SCAN2"
print scan2_data[bpm_counter.name]
#### fit scan2
coeffs = np.polyfit(scan2_data[offset_axis.name], scan2_data[bpm_counter.name], 2)
print "COEFFS SCAN2"
print coeffs
fits_coeff.append(coeffs)
stats_dispaly_and_save(scan_index, scan2_data,fits_coeff )
scan2_data, coeffs = kb_slit_scan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter, name="kbf_scan2")
# Save coefficients of 2nd fit.
fits_coeffs.append(coeffs)
# substract previous scan data to current scan data and divde result by bender_inc...
KBF_INTM_DAT_1 = ( scan2_data[bpm_counter.name] - scan1_data[bpm_counter.name] ) / bender_inc
# Substract mean value of current scan data to current scan data
KBF_INTM_DAT_1 -= numpy.mean(KBF_INTM_DAT_1)
"""
2nd move
"""
# Move only UP bender.
bender_axes[1].move_relative(bender_inc)
time.sleep(wait_after_bend)
bender_axes[0].move_relative(bender_inc)
time.sleep(bender_stabilisation_time)
"""
3rd/Nth scan
3rd scan
"""
scan_index = 3
scanN = dscan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter,
return_scan=True, save=False, name="kbf_scanN")
scanN_data = scans.get_data(scanN)
print "DATA OF SCAN3"
print scanN_data[bpm_counter.name]
#### fit scan3
coeffs = np.polyfit(scanN_data[offset_axis.name], scanN_data[bpm_counter.name], 2)
print "COEFFS SCAN3"
print coeffs
fits_coeff.append(coeffs)
stats_dispaly_and_save(scan_index, scanN_data,fits_coeff )
# scan1 scan2 scan3
# fits_coeff = [ [a,b,c], [a,b,c], [a,b,c] ]
"""
Calculation of interaction matrix.
"""
ka1 = ( coeffs[1][0] - coeffs[0][1] ) / bender_inc
ka2 = ( coeffs[2][0] - coeffs[1][1] ) / bender_inc
kb1 = ( coeffs[1][1] - coeffs[0][2] ) / bender_inc
kb2 = ( coeffs[2][1] - coeffs[1][2] ) / bender_inc
# NB : no use of "c" coeffs ?
det = ka1*kb2 - kb1*ka2
if det == 0 :
print "determinent of interaction matrix is 0...."
raise ???
if ka1*ka2*kb1*kb2 == 0:
print "error: there is a 0 element in interaction matrix"
raise ???
scan3_data, coeffs = kb_slit_scan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter, name="kbf_scan3")
# Save coefficients of 3rd fit.
fits_coeffs.append(coeffs)
# substract previous scan data to current scan data and divde result by bender_inc...
KBF_INTM_DAT_2 = ( scan3_data[bpm_counter.name] - scan2_data[bpm_counter.name] ) / bender_inc
# Substract mean value of current scan data to current scan data
KBF_INTM_DAT_2 -= numpy.mean(KBF_INTM_DAT_2)
"""
Convergence loop
"""
last_scan_data = scan3_data
while True:
KBF_INTM_DAT_0 = - ( last_scan_data - numpy.mean(last_scan_data) )
while(True):
#### fit scanN
coeffs = np.polyfit(scann_data[offset_axis.name], scann_data[bpm_counter.name], 2)
print "COEFFS scanN"
r = numpy.linalg.lstsq(numpy.column_stack((KBF_INTM_DAT_1, KBF_INTM_DAT_2)), KBF_INTM_DAT_0)
coeffs = r[0]
print r
print coeffs
fits_coeff.append(coeffs)
stats_dispaly_and_save(scan_index, scanN_data,fits_coeff )
# Correction (INTM)
scan_index
# scan1 scan2 ... scanN
# fits_coeff = [ [a,b,c], [a,b,c], ... [a,b,c] ]
corr1 = ( -fits_coeff[scan_index][0] * kb2 + fits_coeff[scan_index][1] * ka2 ) / det
corr2 = ( -fits_coeff[scan_index][1] * ka1 + fits_coeff[scan_index][0] * kb1 ) / det
# ??? fit intm ???
kbf_intm_fit(cnt_number)
corr1_intm = KBF_INTM_PARS[0]
corr2_intm = KBF_INTM_PARS[1]
ans = query_action()
def stats_dispaly_and_save(scan_idx, scan_data, fits_coeff):
# corrections to apply
corr1_intm = coeffs[0]
corr2_intm = coeffs[1]
import pdb;pdb.set_trace()
#bender_axes[0].move_relative(corr2_intm, wait=False)
#bender_axes[1].move_relative(corr1_intm)
#bender_axes[0].wait_move()
#scan_data, coeffs = kb_slit_scan(offset_axis, rel_start, rel_end, npts, ctime, bpm_counter, name="kbf_scanN")
#print "Will iterate again in 10 seconds...."
#time.sleep(10)
def stats_display_and_save(scan_idx, scan_data, fits_coeff):
print("STATISTICS FOR SCAN %d" % scan_idx)
print "..."
def query_action():
print "What do you want to do ???"
Supports Markdown
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