Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import seabreeze.spectrometers as sb
- import datetime
- import matplotlib.pyplot as plt
- import numpy as np
- import time
- import thread
- import json
- import sys
- startTime=datetime.datetime.now()
- ## Generating the filename
- TIMESTAMP=startTime.strftime('%Y-%m-%d_%H%M%S')
- FILENAME="spectro_"+TIMESTAMP+".json"
- ## Constant for total running time
- RUNNINGTIME=datetime.timedelta(seconds=30)
- ## List for exporting to JSON
- measurements=[];
- print "Program started at"+str(startTime)
- ## Function responsible for exporting data to a JSON file
- def exportData():
- while True:
- measurements.append(filteredData)
- with open(FILENAME, 'w') as outfile:
- json.dump(measurements, outfile)
- print "Exported JSON at: "+str(datetime.datetime.now())
- time.sleep(30)
- ## List of connected spectrometers
- devices = sb.list_devices()
- spec = sb.Spectrometer(devices[0])
- spec.integration_time_micros(12000)
- print "Connected device: "+str(spec)
- ## First reading and combination to a single list
- x = np.array(spec.wavelengths())
- y = np.array(spec.intensities())
- data = zip(x,y)
- ## Filtering to get PAR with a margin of 50 nanometers
- filteredData = [s for s in data if s[0]>=350 and s[0]<=750]
- ## Making a plot
- plt.ion()
- fig = plt.figure()
- ax = fig.add_subplot(111)
- line1, = ax.plot([i[0] for i in filteredData], [i[1] for i in filteredData], 'r-') # Returns a tuple of line objects, thus the comma
- plt.ylim([80,4000])
- ## Creating a thread managing periodical data export
- thread.start_new_thread(exportData, ())
- ## A loop making readings and updating the plot in real time
- while datetime.datetime.now()<startTime+RUNNINGTIME:
- x = np.array(spec.wavelengths())
- y = np.array(spec.intensities())
- data = zip(x,y)
- filteredData = [s for s in data if s[0]>=350 and s[0]<=750]
- line1. set_xdata([i[0] for i in filteredData])
- line1.set_ydata([i[1] for i in filteredData])
- fig.canvas.draw()
- print "Program finished at "+str(datetime.datetime.now())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement