#!/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
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 = 50


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

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