#Script imported from: Fe_hyst_plus.xml

#Parameters
START_FIELD = -1.0
END_FIELD = 1.0
ENERGIES = (707.90, 703.90, 700.90)
RAMP_RATE = 2.0


if len(ENERGIES) ==2:
    dif_series = plot([],"Dif", title="Dif")[0].getSeries(0)
else:
    dif_series = None
    
set_preference(Preference.ENABLED_PLOTS,  ['field', 'tey_norm', 'trans_norm'])
set_preference(Preference.PLOT_TYPES, {'tey_norm':1, 'trans_norm':1})

scan = ManualScan(['field', 'Energy'], ['tey_norm','trans_norm'], [0.0, ENERGIES[0]], [0.0, ENERGIES[-1]], [0, len(ENERGIES)-1])
scan.start()

#Stop condition



index = 0
while(True):
    #Dimension 2
    #ArrayPositioner Energy
    for setpoint2 in ENERGIES:
        out.write(setpoint2) # TODO: Set appropriate timeout
        readback2 = out.read()

        tey_norm = sin.read()
        if dif_series is not None:
            if setpoint2 == ENERGIES[0]:
                first = tey_norm
            else:
                dif = tey_norm-first
                dif_series.appendData(index,dif)

        scan.append ([index, setpoint2], [index, readback2], [tey_norm, sinp.read()])


    index = index+1
    if index >1000:
        break



scan.end()