from java.math import BigDecimal
import time
from localStation import finder
from gda.epics.CAClient import caput

# parameters
acquisition_start_angle = -72
acquisition_end_angle   = -41
motor_name = "delta"
motor_pv="BL11I-MO-DIFF-01:DELTA"
detector_name = "mythen"
target_count = 100000
data=[]

# get objects
motor = finder.find(motor_name)
detector = finder.find(detector_name)

def startAcquisition(duration):
    print "collecting data"
    detector.setCollectionTime(duration)
    detector.collectData()
    print "moving motor from %.2f to %.2f" % (start_angle, end_angle)
    moveMotorTo(end_angle)
    print "data collection complete"
    data = detector.readout()

def moveMotorTo(angle):
    motor.moveTo(angle)
    while (motor.getPosition() != angle):
        print "  waiting - motor is at %.1f" % motor.getPosition()
from gdascripts.utils import caget, caput
from time import sleep 
from gda.jython.commands.ScannableCommands import pos
from localStation import finder

fpitch2=finder.find("fpitch2")
Io=finder.find("Io")
# setup parameters for the scans
#start = -10
#step = 0.2
start = -4
step = 0.2
n = 176
ysize = 0.8
ycentre = -1.226

# wait for topup to complete if it will interrupt us
topup_time = float(caget("SR-CS-FILL-01:COUNTDOWN"))
needed_time = n*0.29 + 20.0
if needed_time > topup_time:
    waiting_time = topup_time + 10
    print "Waiting %.2fs for topup to complete..." % waiting_time
    sleep(waiting_time)
print "Starting Optimisation"

# setup scaler and disable fast feedback
caput("BL11I-EA-COUNT-01.TP",0.1)
caput("BL11I-OP-DCM-01:PID:AUTO", 0)
caput("BL11I-OP-DCM-01:PID.FBON", 0)
'''
s4yplus.asynchronousMoveTo(1.4)