import traceback

THETA_RANGE = (0.0, 5.0)
THETA_STEP = 1.0
PHI_RANGE = (-160.0, +160.0)
PHI_STEP = 160.0 #40.0

LATENCY = 0.0
ZIGZAG = True
ENDSCAN = False
MOTORS = (motor)
SENSORS = (sin, arr,  out, inp, motor.readback)


set_preference(Preference.PLOT_TYPES, {'Scienta spectrum':1})

# time per scienta acquisition in second
scienta_time = 0.1
print "scienta_time: ", scienta_time



# time for one theta scan in seconds
THETA_NSTEPS = int((THETA_RANGE[1] - THETA_RANGE[0]) / THETA_STEP) + 1
theta_time = scienta_time * THETA_NSTEPS
print "theta_time: ", theta_time

PHI_NSTEPS = int((PHI_RANGE[1] - PHI_RANGE[0]) / PHI_STEP) + 1
phi_positions = [PHI_RANGE[0] + PHI_STEP * i for i in range(PHI_NSTEPS)]
print "phi_positions: ", phi_positions

def before_pass(pass_num, scan):
    print "Initializing pass ", pass_num
    phi = phi_positions[pass_num-1]
    inp.write(phi)
    #print "phi = ", phi

    
    
try:
        try:
            while(True):
                cscan(MOTORS, SENSORS, THETA_RANGE[0], THETA_RANGE[1], THETA_NSTEPS - 1, latency=0.1, time=theta_time, passes = len(phi_positions), zigzag = ZIGZAG, before_pass=before_pass, check_positions = False)
        except Exception:
            #print sys.exc_info()
            traceback.print_exc()
            traceback.print_stack()
            print "Caught"
    
finally:
    if ENDSCAN:
        after_scan()