<gh_stars>0
from __future__ import print_function

import qt
import shutil
import sys
import os
import time

from constants import *

def generate_meta_file(count):
    metafile = open('%s.meta.txt' % data.get_filepath()[:-4], 'w')
    metafile.write('#inner loop\n%s\n%s\n%s\n%s\n'%
            (probe_numpoints, probe_start_freq, probe_stop_freq, 'Frequency(Hz)'))
    metafile.write('#outer loop\n%s\n%s\n%s\n%s\n'%
            (count+1, count, 0, 'Counts'))
    metafile.write('#outermost loop (unused)\n1\n0\n1\nNothing\n')

    metafile.write('#for each of the values\n')
    values = data.get_values()
    i=0
    while i<len(values):
        metafile.write('%d\n%s\n'% (i+3, values[i]['name']))
        i+=1
    metafile.close()

def copy_script(once):
  if once:
    shutil.copy2('%s'%sys.argv[0],'%s/%s'%(data.get_filepath()[:-(len(data.get_filename())+1)],os.path.basename(sys.argv[0])))


#############################
# Measurement Parameters
#############################
# VNA sweep parameters
probe_center = 6.25*GHz
probe_span = 500*MHz
probe_start_freq = probe_center-probe_span/2
probe_stop_freq = probe_center+probe_span/2
probe_numpoints = 301
if_bw = 100*Hz
probe_power = -40 #dBm 6+ 2.5dB attenuation at VNA
s_params = ['S21']
filename = raw_input('Filename : ')


#############################
# Initialize Instruments
#############################
znb = qt.instruments.create('ZNB20', 'RhodeSchwartz_ZNB20', address=ZNB20_ADDRESS, reset=True)
# smf = qt.instruments.create('SMF100', 'RhodeSchwartz_SMF100', address = SMF100_ADDRESS, reset=True)

# setup SMF100 as source
# smf.set_frequency(drive_freq)

# Setup VNA as source
znb.set_external_reference(True)
znb.set_external_reference_frequency(10)
znb.set_source_power(probe_power)
znb.set_start_frequency(probe_start_freq)
znb.set_stop_frequency(probe_stop_freq)
znb.set_numpoints(probe_numpoints)
znb.set_if_bandwidth(if_bw)
znb.add_trace('S21')

# Turn on sources
znb.rf_on()
# smf.rf_on()

# Test trigger
znb.send_trigger(wait=True)
# znb.autoscale()
go_on = raw_input('Continue? [y/n] ')

assert go_on.strip().upper() != 'N'

### SETTING UP DATA FILE
data=qt.Data(name=filename)
# data.add_comment('No. of repeated measurements for average is 60')
data.add_coordinate('Drive Power', units='Counts')
data.add_coordinate('Drive Frequency', units='Hz')
data.add_value('S21 real')
data.add_value('S21 imag')
data.add_value('S21 abs')
data.add_value('S21 phase')


freq_array = np.linspace(probe_start_freq, probe_stop_freq, num=probe_numpoints)
qt.mstart()

#traces_sum = [np.zeros(drive_numpoints), np.zeros(drive_numpoints)]

#num_avs = 0
once = True

index_count = 0
while(True):
	print(index_count)
	index_count+=1
	znb.send_trigger(wait=True)
	znb.autoscale()
	traces = []
	trace = znb.get_data('S21')
	traces.append(np.real(trace))
	traces.append(np.imag(trace))
	traces.append(np.absolute(trace))
	traces.append(np.angle(trace))
	index_arr = np.linspace(index_count, index_count, num=probe_numpoints)
	data.add_data_point(index_arr, freq_array, *traces)
	qt.msleep(30)
	generate_meta_file(index_count)
	copy_script(once);once = False


#create script in data directory
shutil.copy2('%s'%sys.argv[0],'%s/%s'%(data.get_filepath()[:-(len(filename)+11)],os.path.basename(sys.argv[0])))
# copy_script(sys.argv[0], filename)
data.close_file(sys.argv[0])
from __future__ import print_function

import qt
import shutil
import sys
import os
import time

from constants import *

def generate_meta_file(count):
    metafile = open('%s.meta.txt' % data.get_filepath()[:-4], 'w')
    metafile.write('#inner loop\n%s\n%s\n%s\n%s\n'%
            (probe_numpoints, probe_start_freq, probe_stop_freq, 'Frequency(Hz)'))
    metafile.write('#outer loop\n%s\n%s\n%s\n%s\n'%
            (count+1, count, 0, 'Counts'))
    metafile.write('#outermost loop (unused)\n1\n0\n1\nNothing\n')

    metafile.write('#for each of the values\n')
    values = data.get_values()
    i=0
    while i<len(values):
        metafile.write('%d\n%s\n'% (i+3, values[i]['name']))
        i+=1
    metafile.close()

def copy_script(once):
  if once:
    shutil.copy2('%s'%sys.argv[0],'%s/%s'%(data.get_filepath()[:-(len(data.get_filename())+1)],os.path.basename(sys.argv[0])))


#############################
# Measurement Parameters
#############################
# VNA sweep parameters
probe_center = 6.25*GHz
probe_span = 500*MHz
probe_start_freq = probe_center-probe_span/2
probe_stop_freq = probe_center+probe_span/2
probe_numpoints = 301
if_bw = 100*Hz
probe_power = -40 #dBm 6+ 2.5dB attenuation at VNA
s_params = ['S21']
filename = raw_input('Filename : ')


#############################
# Initialize Instruments
#############################
znb = qt.instruments.create('ZNB20', 'RhodeSchwartz_ZNB20', address=ZNB20_ADDRESS, reset=True)
# smf = qt.instruments.create('SMF100', 'RhodeSchwartz_SMF100', address = SMF100_ADDRESS, reset=True)

# setup SMF100 as source
# smf.set_frequency(drive_freq)

# Setup VNA as source
znb.set_external_reference(True)
znb.set_external_reference_frequency(10)
znb.set_source_power(probe_power)
znb.set_start_frequency(probe_start_freq)
znb.set_stop_frequency(probe_stop_freq)
znb.set_numpoints(probe_numpoints)
znb.set_if_bandwidth(if_bw)
znb.add_trace('S21')

# Turn on sources
znb.rf_on()
# smf.rf_on()

# Test trigger
znb.send_trigger(wait=True)
# znb.autoscale()
go_on = raw_input('Continue? [y/n] ')

assert go_on.strip().upper() != 'N'

### SETTING UP DATA FILE
data=qt.Data(name=filename)
# data.add_comment('No. of repeated measurements for average is 60')
data.add_coordinate('Drive Power', units='Counts')
data.add_coordinate('Drive Frequency', units='Hz')
data.add_value('S21 real')
data.add_value('S21 imag')
data.add_value('S21 abs')
data.add_value('S21 phase')


freq_array = np.linspace(probe_start_freq, probe_stop_freq, num=probe_numpoints)
qt.mstart()

#traces_sum = [np.zeros(drive_numpoints), np.zeros(drive_numpoints)]

#num_avs = 0
once = True

index_count = 0
while(True):
	print(index_count)
	index_count+=1
	znb.send_trigger(wait=True)
	znb.autoscale()
	traces = []
	trace = znb.get_data('S21')
	traces.append(np.real(trace))
	traces.append(np.imag(trace))
	traces.append(np.absolute(trace))
	traces.append(np.angle(trace))
	index_arr = np.linspace(index_count, index_count, num=probe_numpoints)
	data.add_data_point(index_arr, freq_array, *traces)
	qt.msleep(30)
	generate_meta_file(index_count)
	copy_script(once);once = False


#create script in data directory
shutil.copy2('%s'%sys.argv[0],'%s/%s'%(data.get_filepath()[:-(len(filename)+11)],os.path.basename(sys.argv[0])))
# copy_script(sys.argv[0], filename)
data.close_file(sys.argv[0])
############################# # Measurement Parameters ############################# # VNA sweep parameters #dBm 6+ 2.5dB attenuation at VNA ############################# # Initialize Instruments ############################# # smf = qt.instruments.create('SMF100', 'RhodeSchwartz_SMF100', address = SMF100_ADDRESS, reset=True) # setup SMF100 as source # smf.set_frequency(drive_freq) # Setup VNA as source # Turn on sources # smf.rf_on() # Test trigger # znb.autoscale() ### SETTING UP DATA FILE # data.add_comment('No. of repeated measurements for average is 60') #traces_sum = [np.zeros(drive_numpoints), np.zeros(drive_numpoints)] #num_avs = 0 #create script in data directory # copy_script(sys.argv[0], filename)