def saveTrace(filename=''):
    ''' Very basic, give it a file name and it will save a trace from SpecAn'''
    date_string = datetime.datetime.today().strftime('%y-%m-%d')

    filepath = "C:\Users\Milos\Desktop\James\\" + date_string + ' ' + filename + '.txt'
    try:
        os.remove(filename)
    except OSError:
        pass
    file = open(filepath, 'w')

    #Write header
    SpecAn.write('CF?')
    center = float(SpecAn.read())

    SpecAn.write('SP?')
    span = float(SpecAn.read())

    file.write('Center freq: ' + str(center) + ' Span: ' + str(span) + '\n')
    SpecAn.write('RL?')
    ref_level = SpecAn.read()
    file.write('Reference Level: ' + ref_level + '\n')

    spec_data_temp = getTrace()
    data_db = SIH.convert2db(SpecAn, spec_data_temp)

    x = np.linspace(center - span / 2, center + span / 2, 601)
    data = np.vstack([x, data_db]).T
    np.savetxt(file, data, fmt='%.10e')

    file.close()
Beispiel #2
0
def quick_rec(SpecAn, filename):
    filepath = "C:\Users\Milos\Desktop\James\\" + filename + '.txt'
    try:
        os.remove(filename)
    except OSError:
        pass
    file = open(filepath, 'w')

    span = float(SpecAn.query('SP?'))
    center = float(SpecAn.query('CF?'))

    x = np.linspace(center - span / 2, center + span / 2, 601)
    spec_data_temp = np.zeros(601)

    #Gets the trace from the SpecAn
    SpecAn.write('TRA?')
    binary = SpecAn.read_raw()
    spec_data_temp = np.frombuffer(
        binary, '>u2')  # Data format is big-endian unsigned integers

    spec_data_db = SIH.convert2db(SpecAn, spec_data_temp)

    #Conjoins both x and spec_data_temp vectors and saves them to file
    data = np.vstack([x, spec_data_db]).T
    np.savetxt(file, data, fmt='%.10e')
Beispiel #3
0
def multi_record(SpecAn, d_time, n, filepath):
    '''Takes 'n' scans on the HP Spectrum Analyzer at ~d_time intervals.
       This is an augmented version of record_trace from holeburn_james.
       Also records the times for each data recording event.'''
    #Run the following first to set up the text file.
    #hb.create_file(SpecAn, filename = '', compensated = 'N', sweep_again = 'Y', n=1, burn_time = '')

    SpecAn.write('SP?')
    span = float(SpecAn.read())
    SpecAn.write('CF?')
    center = float(SpecAn.read())

    file = open(filepath, 'a')
    x = np.linspace(center - span / 2, center + span / 2, 601)
    spec_data_db = np.zeros((601, n))

    record_time = []
    for i in range(n):
        SpecAn.write('TS')
        #Waits for Spectrum Analyser to finish Data sweep
        SpecAn.wait_for_srq(timeout=30000)

        #Gets the trace from the SpecAn
        SpecAn.write('TRA?')
        binary = SpecAn.read_raw()
        spec_data_temp = np.frombuffer(
            binary, '>u2')  # Data format is big-endian unsigned integers

        record_time.append(datetime.now().strftime("%H:%M:%S.%f"))

        spec_data_db[:, i] = SIH.convert2db(SpecAn, spec_data_temp)

        if compensated == 'Y':
            spec_data_db[:, i] = compensate(spec_data_temp, span)

        SpecAn.write("CLEAR; RQS 4")
        pb.Sequence([(['ch2', 'ch5'], d_time)],
                    [(['ch2', 'ch5', 'ch4'], 1 * ms)],
                    loop=False)

    #Conjoins both x and spec_data_temp vectors and saves them to file
    data = np.vstack([x, spec_data_db]).T
    np.savetxt(file, data, fmt='%.10e')

    file.close()
    pylab.ion()

    if sweep_again == 'Y':
        HP8560E_SpecAn_Trigger("FREE", 'CONTS', SpecAn)

    return x, spec_data_db, filepath
Beispiel #4
0
def record_trace(SpecAn,
                 filepath,
                 filename='',
                 compensated='Y',
                 sweep_again='Y',
                 n=1,
                 burn_time='',
                 opt_bac=''):
    ''' Records a single trace from SpecAn '''
    SpecAn.write('SP?')
    span = float(SpecAn.read())
    SpecAn.write('CF?')
    center = float(SpecAn.read())

    SpecAn.write('TS')
    #Waits for Spectrum Analyser to finish Data sweep
    SpecAn.wait_for_srq(timeout=30000)
    ##     import time
    ##     #time.sleep(10)

    file = open(filepath, 'a')
    x = np.linspace(center - span / 2, center + span / 2, 601)
    spec_data_temp = np.zeros(601)

    #Gets the trace from the SpecAn
    SpecAn.write('TRA?')
    binary = SpecAn.read_raw()
    spec_data_temp = np.frombuffer(
        binary, '>u2')  # Data format is big-endian unsigned integers

    spec_data_db = SIH.convert2db(SpecAn, spec_data_temp)

    if compensated == 'Y':
        spec_data_db = compensate(spec_data_db, span, opt_back=opt_bac)

    #Conjoins both x and spec_data_temp vectors and saves them to file
    data = np.vstack([x, spec_data_db]).T
    np.savetxt(file, data, fmt='%.10e')

    file.close()
    pylab.ion()

    if sweep_again == 'Y':
        HP8560E_SpecAn_Trigger("FREE", 'CONTS', SpecAn)

    return filepath
Beispiel #5
0
def laser_stability_measure(freq,
                            t_space=100 * ms,
                            t=4 * s,
                            f_name='',
                            show='Y'):
    [SpecAn, SpecAn_Bool] = Initialise_HP8560E_SpecAn()
    SpecAn.write('ST 0.05')  #Sweep Time 50ms
    n = int(t / t_space)  #Amount of times scans needed
    ##     mc.setup(centerFreqL = freq, widthL = 10e4, sweepTimeL = 1e-4)

    if t_space < 50 * ms:
        raise RuntimeError, "SpecAn's fastest sweep time & reset is 100ms, t_space cannot be faster."
    file = make_file(f_name)

    span = 5 * MHz
    freq_sit = freq
    SpecAn.write('CF ' + str(freq_sit))
    SpecAn.write('SP ' + str(span))
    time.sleep(0.5)

    #Take Background measurement
    SIH.save_offset(10, 'n')

    #Burn a hole
    pb.Sequence([(['ch1'], 50 * ms), (['ch2', 'ch4', 'ch5'], 1 * ms)],
                loop=False)
    HP8560E_SpecAn_Trigger('EXT', 'CONTS', SpecAn)

    print "Recording for {}s at {} Hz".format(t, 1 / t_space)

    #Get raw data
    l = []
    for k in range(n):
        l.append([time.time(), getTrace(SpecAn, t_space)])

    HP8560E_SpecAn_Trigger('FREE', 'CONTS', SpecAn)

    t_init = l[0][0]
    t_arr = []
    center = []
    freq_arr = np.linspace(freq_sit - span / 2, freq_sit + span / 2, 601)

    for i in range(n):
        #Convert to dB and background subtract post data collection
        dat = SIH.convert2db(SpecAn, l[i][1])
        dat = hb.compensate(dat, span)
        #Make time and freq peak array
        t_arr.append(l[i][0] - t_init)
        index = np.argmax(
            dat[50:])  #The carrier also makes a hole, this should ignore it
        center.append(freq_arr[index])

    data = np.vstack([t_arr, center]).T
    np.savetxt(file, data, fmt='%.10e')

    ##
    pb.Sequence([(['ch2', 'ch5', 'ch4'], 1 * ms)], loop=False)
    #Plot
    if show == 'Y':
        plt.plot(t_arr, center)
        plt.show()

    return l, t_arr, center, data