import os import termios import serial import time import datetime import sys import re from numpy import * from channels import * from prologix_usb2gpib import * from tek7900_gpib import * from sa_data_functions import * def SaveRawData(obj, path) : # Path is absolute or relative to base directory, and must end with '/'. # obj must have the attribute data_set as a list of [x,y] pairs, and the attribute yUnits. nowString = datetime.datetime.now().strftime('%y-%m-%d_%H.%M.%S') data_file = path + nowString newFile = open(data_file,'w') # Make a header header = "# Signal scale = " + str(obj.yUnits) + ", Frequency scale = Hz\n" newFile.write(header) for i in range(0, len(obj.data_set)): newFile.write(str(obj.data_set[i][0]) + '\t' + str(obj.data_set[i][1]) + '\n') newFile.close() #print("Data saved to file: '" + nowString + "'") return data_file def SaveFilteredData(obj, filtered, file_name) : # file_name is the complete path and name of the file to be created. # obj must have the attribute data_set as a list of [x,y] pairs, and the attribute yUnits. # filtered is the array of filtered y data only. of = open(file_name,'w') for i in range(0, len(obj.data_set)): of.write(str(obj.data_set[i][0]) + '\t' + str(filtered[i]) + '\n') of.close() def Main() : channel = SerialUSB() device = Prologix(channel, 2) sa = Tek7900(device) duration = 3 # Time over which to accumulate data in seconds path = '/home/bob/radio/guy/data/09-03-17/lo=134938_span=4MHz_ref=-85dbm_rbw=1kHz/' # Path to data directory, with a trailing '/'. print "Set up" a = sa.SetUp() print a print "Reference Level query" b = sa.ReferenceLevel() print b print "Parameters" c = sa.Parameters() print c #mp = os.popen('python plot_filtered_data_set.py', 'w') # Open a plot_filtered_data_set.py process. gp_raw = os.popen('gnuplot', 'w') # Create a gnuplot process for the raw data gp_filtered = os.popen('gnuplot', 'w') # Create a gnuplot process for the filtered data data_set = 1 while 1 : print "Acquiring data set " + str(data_set) sa.AccumulateData(duration) #print sa.data sa.ScaleData() sa.MakeDataSet() saved_file = SaveRawData(sa, path) print("Data saved to file: " + saved_file) data_set += 1 cmds = ['plot "' + saved_file + '" with lines\n'] gp_raw.writelines(cmds) # Send lines to gnuplot process. gp_raw.flush() # Display the process but do not close it. # Filter the data, save to a temporary file and plot tau = 20 v = RCFilterFFT(sa.data, len(sa.data)/tau) # sa.data is the y or signal data only, and tau is a "time constant" in data points temp_file = path + "temp" SaveFilteredData(sa, v, temp_file) cmds = ['plot "' + temp_file + '" with lines\n'] gp_filtered.writelines(cmds) # Send lines to gnuplot process. gp_filtered.flush() # Display the process but do not close it. #mp.writelines(temp_file+'\n') #mp.flush() gp_raw.close() gp_filtered.close() #mp.close() #--------------------------------------------------------------------------------------------------------------------------- # Execute if __name__ == '__main__': Main()