Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import seabreeze.spectrometers as sb
  2. import datetime
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import time
  6. import thread
  7. import json
  8. import sys
  9.  
  10. startTime=datetime.datetime.now()
  11.  
  12. ## Generating the filename
  13. TIMESTAMP=startTime.strftime('%Y-%m-%d_%H%M%S')
  14. FILENAME="spectro_"+TIMESTAMP+".json"
  15.  
  16. ## Constant for total running time
  17. RUNNINGTIME=datetime.timedelta(seconds=30)
  18.  
  19. ## List for exporting to JSON
  20. measurements=[];
  21.  
  22. print "Program started at"+str(startTime)
  23.  
  24. ## Function responsible for exporting data to a JSON file
  25. def exportData():
  26.     while True:
  27.         measurements.append(filteredData)
  28.         with open(FILENAME, 'w') as outfile:
  29.             json.dump(measurements, outfile)
  30.         print "Exported JSON at: "+str(datetime.datetime.now())
  31.         time.sleep(30)
  32.  
  33. ## List of connected spectrometers
  34. devices = sb.list_devices()
  35. spec = sb.Spectrometer(devices[0])
  36. spec.integration_time_micros(12000)
  37. print "Connected device: "+str(spec)
  38.  
  39. ## First reading and combination to a single list
  40. x = np.array(spec.wavelengths())
  41. y = np.array(spec.intensities())
  42. data = zip(x,y)
  43.  
  44. ## Filtering to get PAR with a margin of 50 nanometers
  45. filteredData = [s for s in data if s[0]>=350 and s[0]<=750]
  46.  
  47. ## Making a plot
  48. plt.ion()
  49. fig = plt.figure()
  50. ax = fig.add_subplot(111)
  51. 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
  52. plt.ylim([80,4000])
  53.  
  54. ## Creating a thread managing periodical data export
  55. thread.start_new_thread(exportData, ())
  56.  
  57. ## A loop making readings and updating the plot in real time
  58. while datetime.datetime.now()<startTime+RUNNINGTIME:
  59.     x = np.array(spec.wavelengths())
  60.     y = np.array(spec.intensities())
  61.  
  62.     data = zip(x,y)
  63.  
  64.     filteredData = [s for s in data if s[0]>=350 and s[0]<=750]
  65.  
  66.     line1. set_xdata([i[0] for i in filteredData])
  67.     line1.set_ydata([i[1] for i in filteredData])
  68.     fig.canvas.draw()
  69.  
  70. print "Program finished at "+str(datetime.datetime.now())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement