#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

This program loops through moving a stage holding a detector and taking data, 
repeatedly, then plots the intensity as a function of position.

@author: Seth and Viva
"""
# loading libraries used by the code
import numpy as np
import matplotlib.pyplot as plt
import time
from LDrive import LDrive

# open the computer's connection to the instruments. This will error if already open.
s = LDrive()        

# set the number of steps of data to take
numsteps = 350

# set the size of each step, in units of fundamental steps
stepsize = 3 

# Create arrays, intensity and x, and initialize each with zeros for now
intensity = np.zeros(numsteps)
x = np.zeros(numsteps)
   
for i in range(numsteps):
    # choose left or right motion
    s.right(stepsize) # move the stage to the right
    #s.left(stepsize) # move the stage to the left
    time.sleep(.1) # wait a little to let the instrument settle
    intensity[i] = s.avg() # take intensity data!
    x[i] = i
    
   
# Plot Intensity versus position   
plt.plot(x,intensity)
plt.xlabel("Distance ()")
plt.ylabel("Intensity (arb. units)")
plt.show()

# save the data to the computer as an npy file
np.save("intensity.npy",intensity)

# return to the starting spot (change this to right if you went left)
s.left(stepsize * numsteps) 

# close the computer's connection to the instruments
s.close()
print('Please power off the ETS-7000! It is so easy to overheat the motor.')