def plot_pulse_cal_dac_offsets_vs_days(s, decimation=21):
    data_directory = ["", "Brooks", "pulseCal", "dac_step_offsets"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [29]
    datasets_ref = np.linspace(20, 39, 20)
    # datasets_ref = [20]
    datasets = np.linspace(40, 279, 240)
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-"]

    diff_chans = ["Diff+", "Diff-"]

    for data_ref in datasets_ref:
        data_ref = np.int(data_ref)
        for diff_chan in diff_chans:
            count = 0
            fig = plt.figure()
            for ds in datasets:
                ds = np.int(ds)
                print "ds: {}; %20: {}".format(ds, ds % 20)
                if (ds % 20) + 20 == data_ref:
                    data = d[ds]
                    print "current dataset: {}".format(ds)
                    data_times = data.get_column("Time")["us"]

                    data_voltage = data.get_column(diff_chan)["V"]

                    data_voltage, amp, offset = normalize_pulse(data_voltage, percent=1.0 / 15)

                    data_voltage = smoothing(data_voltage, decimation)
                    data_times = smoothing(data_times, decimation)

                    ref_data = d[np.int(data_ref)]
                    ref_voltage = ref_data.get_column(diff_chan)["V"]
                    ref_voltage, amp, offset = normalize_pulse(ref_voltage, percent=1.0 / 15)
                    ref_voltage = smoothing(ref_voltage, decimation)

                    plt.plot(
                        data_times,
                        data_voltage - ref_voltage,
                        markers[count % 7],
                        label="{}, step#: {}".format(diff_chan, count),
                    )
                    gc.collect()
                    count += 1
                    plt.title(
                        "Data-Reference vs Time.  ACQ:RESP: {}, low: {}; high: {}".format(
                            data.parameters["scope_response_filter"],
                            data.parameters["low_dac_amp"],
                            data.parameters["high_dac_amp"],
                        )
                    )
            plt.xlabel("Time (us)")
            plt.ylabel("% of step")
            plt.axis([-100, 700, -0.05, 0.05])
            plt.tight_layout()
            plt.legend(loc=4, numpoints=1)
            fig.savefig("{}; low_amp = {}.png".format(diff_chan, data.parameters["low_dac_amp"]), bbox_inches="tight")
            plt.close("all")
def plot_pulse_cals_vs_channels(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [1, 2, 3, 4]

    plt.figure()
    plt.title("Calibration Pulse Different sample rates:  Ch1")
    markers = ["r-", "b-", "g-", "m-"]
    count = 0
    for ds in datasets:
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(
            data_times / 1.0e3,
            data_voltage * 1.0e3 / 5,
            markers[count],
            label="ch{}".format(data.parameters["channel"]),
        )
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (% of step)")
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cals_vs_y_pos(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [104, 105, 106, 107, 108, 109, 110, 111, 112]
    plt.figure()
    plt.title("Calibration Pulse Different y_offset:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-", "r--", "b--"]
    count = 0
    for ds in datasets:
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(
            data_times[::2] / 1.0e3,
            data_voltage[::2] * 1.0e3,
            markers[count],
            label="{} V".format(data.parameters["y_position_v"]),
        )
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cal_steps(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A", "DAC_source"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = np.linspace(4, 299, 296)
    # datasets = np.linspace(4, 20, 17)

    plt.figure()
    plt.title("Calibration Pulse Repetitions, Fs: 1e9Hz, 50mV/div:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-", "r--"]
    count = 0
    for ds in datasets:
        if ds % 10 == 4:
            ds = np.int(ds)
            data = d[ds]
            print "current dataset: {}".format(ds)
            if data.parameters["dac_low_amp"] == -0.5:
                print "found match, dataset: {}".format(ds)
                data_times = data.get_column("Time")["ns"]
                data_voltage = data.get_column("Voltage")["V"]
                data_voltage -= np.mean(data_voltage)
                plt.plot(
                    data_times[::2] / 1.0e3, data_voltage[::2] * 1.0e3, markers[count % 7], label="{}".format(count)
                )
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cals_vs_x_axis(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [24, 25, 26, 27, 28, 29, 30]

    plt.figure()
    plt.title("Calibration Pulse Different x-scales, 1e9Hz, 50mV/div:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-", "r--"]
    count = 0
    for ds in datasets:
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(
            data_times[::2] / 1.0e3,
            data_voltage[::2] * 1.0e3,
            markers[count],
            label="{} s/div".format(data.parameters["horizontal_scale"]),
        )
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cals_vs_sample_rate2(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [11, 12, 13, 14, 15]

    plt.figure()
    plt.title("Calibration Pulse Different sample rates:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-"]
    count = 0
    for ds in datasets:
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(
            data_times[::2] / 1.0e3,
            data_voltage[::2] * 1.0e3,
            markers[count],
            label="Fs: {} Hz".format(data.parameters["sample_rate"]),
        )
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cals_vs_scales(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [16, 17, 18, 19, 20, 21, 22, 23]

    plt.figure()
    plt.title("Calibration Pulse Different scaled, 1e9Hz:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-", "r--"]
    count = 0
    for ds in datasets:
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(
            data_times[::2] / 1.0e3,
            data_voltage[::2] * 1.0e3,
            markers[count],
            label="{} V/div".format(data.parameters["y_scale_v_per_div"]),
        )
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def plotPhaseCalPowers(s, IQ = True, chooseCal = False):
    dataDir = ['', 'Brooks', 'parAmp', 'impaNGC_CuBox_CuAuPCB', 'longZParamp', '160604'] #longZParamp in NGC Cu Box, CuAuPCB

    cxn = s._cxn

    d = dvw(dataDir,cxn)
    dataSets = d.keys()

    cm = plt.cm.get_cmap('RdYlBu')

    for dataSet in dataSets:
        if dataSet[-3::] == 'dBm':  #then scan is at 18dBm
            data = d[dataSet]
            color = np.float(string.split(data.parameters.get('loPower'))[0])
            print color
            
            phase = IQtoPhase(data['Q']/data['I'])
            
            if IQ:
                sc = plt.scatter(data['I'], data['Q'], c = np.zeros(len(phase)) + color, vmin = 0, vmax = 20, cmap = cm, edgecolors = 'none' )
            else:
                sc = plt.scatter(data['FluxV [V]'], phase, c = np.zeros(len(phase)) + color, vmin = 0, vmax = 20, cmap = cm, edgecolors = 'none' )
            
    
    plt.colorbar(sc, label = 'CW Tone Power (dBm)')
    if IQ:
        plt.title('IQ Plane vs CW Tone Power (dBm) @ 4.8 GHz',fontsize=20)
        plt.ylabel('Q (V)')
        plt.xlabel('I (V)')
    else:
        plt.title('Phase (rad) vs Flux Voltage (V) @ 4.8 GHz',fontsize=20)
        plt.ylabel('Phase (rad)')
        plt.xlabel('Flux Voltage (V)')
    plt.show()
def plotPhaseCalFreqs(sample, IQ = True, freq = None, chooseCal = False, dataDir = None):
    cxn = sample._cxn

    if dataDir == None:
        d = dvw(sample,cxn)
    else:
        d = dvw(dataDir,cxn)
    dataSets = d.keys()

    dataSets = dataSets[38::-1]

    cm = plt.cm.get_cmap('RdYlBu')

    freqtemp = freq
    if freq == None:
        freqtemp = 1.

    for dataSet in dataSets:
        if dataSet[-3::] == 'GHz' and (np.int(np.float(dataSet[-7:-4])*10.0) == np.int(10.0*freqtemp) or freq == None):  #then scan is at 18dBm
            data = d[dataSet]
            print dataSet
            color = np.float(data.parameters.get('loFreq')['Hz'])/1000000000.0
            pwr = np.float(data.parameters.get('loPower')['dBm'])
            print color

            phase = IQtoPhase(data['Q']/data['I'])

            # DCIQcofit(sample, dataSet = dataSet, plot = True,dataDir = None, useReg = False)
            if IQ:
                sc = plt.scatter(data['I'], data['Q'], c = np.zeros(len(phase)) + color, vmin = 4, vmax = 6.5, cmap = cm, edgecolors = 'none' )
            else:
                sc = plt.scatter(data['FluxV [V]'], phase, c = np.zeros(len(phase)) + color, vmin = 4, vmax = 6.5, cmap = cm, edgecolors = 'none' )

    plt.colorbar(sc, label = 'CW Tone Frequency (GHz)')
    if IQ:
        plt.title('IQ Plane: %s GHz @ %s dBm'%(color, pwr),fontsize=20)
        plt.ylabel('Q (V)')
        plt.xlabel('I (V)')
    else:
        plt.title('Phase (rad) vs Flux Voltage (V):  18dBm Drive',fontsize=20)
        plt.xlabel('Flux Voltage (V)')
        plt.ylabel('Phase (rad)')
    plt.show()
def plot_pulse_cal_dac_offsets(s, decimation=21):
    data_directory = ["", "Brooks", "pulseCal", "dac_step_offsets"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [29]
    datasets.append(np.linspace(20, 28, 9))
    datasets.append(np.linspace(30, 39, 10))
    datasets = np.hstack(datasets)
    datasets = [21, 41]
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-"]

    diff_chans = ["Diff+", "Diff-"]

    for diff_chan in diff_chans:
        count = 0
        plt.figure()
        for ds in datasets:
            ds = np.int(ds)
            data = d[ds]
            print "current dataset: {}".format(ds)
            data_times = data.get_column("Time")["us"]

            data_voltage = data.get_column(diff_chan)["V"]

            data_voltage, amp, offset = normalize_pulse(data_voltage, percent=1.0 / 15)

            data_voltage = smoothing(data_voltage, decimation)
            data_times = smoothing(data_times, decimation)

            if count == 0:
                ref_voltage = data_voltage

            plt.plot(
                data_times,
                data_voltage - ref_voltage,
                markers[count % 7],
                label="{}, step#: {}".format(diff_chan, count),
            )
            gc.collect()
            count += 1
        plt.title(
            "0.1Dac_amp Step, diff_offset, Fs: 1e9Hz, 50mV/div:  Ch1, {} resp".format(
                data.parameters["scope_response_filter"]
            )
        )
        plt.xlabel("Time (us)")
        plt.ylabel("% of step")
        plt.axis([-100, 700, -0.15, 0.15])
        plt.tight_layout()
        plt.legend(loc=4, numpoints=1)
def plot_pulse_cal_attens(s, decimation=55):
    data_directory = ["", "Brooks", "pulseCal", "fixed_sensitivity_atten_rfpg"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = [2, 12, 10, 8, 6, 4]  # guassian
    datasets = [1, 11, 9, 7, 5, 3]  # flat

    plt.figure()

    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-"]
    count = 0

    for ds in datasets:
        ds = int(ds)
        data = d[ds]
        print "current dataset: {}".format(ds)
        data_times = data.get_column("Time")["us"]
        data_voltage = data.get_column("Voltage")["V"]

        data_voltage, amp, offset = normalize_pulse(data_voltage, percent=1.0 / 15)

        data_voltage = smoothing(data_voltage, decimation)
        data_times = smoothing(data_times, decimation)

        if count == 0:
            ref_voltage = data_voltage

        plt.plot(
            data_times,
            data_voltage - ref_voltage,
            markers[count % 7],
            label="{} db".format(data.parameters["attenuation"]),
        )
        gc.collect()
        count += 1
    plt.title(
        "Calibration Pulse w/ different atten, Fs: 1e9Hz, 50mV/div:  Ch1, {} resp".format(
            data.parameters["scope_response_filter"]
        )
    )
    plt.xlabel("Time (us)")
    plt.ylabel("% of step")
    plt.axis([-100, 700, -0.15, 0.15])
    plt.tight_layout()
    plt.legend(loc=4, numpoints=1)
def plot_pulse_cal_repetitions(s):
    data_directory = ["", "Brooks", "pulseCal", "AgilentDSA90804A"]
    cxn = s._cxn
    d = dvw(data_directory, cxn)

    datasets = np.linspace(36, 97, 62)

    plt.figure()
    plt.title("Calibration Pulse Repetitions, Fs: 1e9Hz, 50mV/div:  Ch1")
    markers = ["r-", "b-", "g-", "m-", "c-", "k-", "y-", "r--"]
    count = 0
    for ds in datasets:
        ds = np.int(ds)
        data = d[ds]
        data_times = data.get_column("Time")["ns"]
        data_voltage = data.get_column("Voltage")["V"]
        plt.plot(data_times[::2] / 1.0e3, data_voltage[::2] * 1.0e3, markers[count % 7], label="{}".format(count))
        gc.collect()
        count += 1
    plt.xlabel("Time (us)")
    plt.ylabel("Voltage (mV)")
    plt.legend(loc=4, numpoints=1)
def checkStep(sample, stepDataSet, stepDataDir = None):
    cxn = sample._cxn
    
    # get calibration datasets
    if stepDataDir == None:
        d = dvw(sample, cxn)
    else:
        d = dvw(dataDir, cxn)
        
    stepData = d[stepDataSet]

    calDataSet = stepData.parameters['IQcalibrationDataSet']
    calDataDir = stepData.parameters['IQcalibrationDir']
    calD = dvw(calDataDir, cxn)

    calData = calD[calDataSet]
    fluxVcal = calData['FluxV']
    Ical = calData['I']
    Qcal = calData['Q']
    
    # convert flux voltage from dmmV to DAC amp
    fluxDACampCal = dmmVtoDACamp(fluxVcal * V, calData, useReg = False)
    # convert DAC amp flux voltage to flux
    fluxCal = dacAmpToFlux(fluxDACampCal, calData, useReg = False)
    
    # compute Cal phase
    phaseCal = IQtoPhase(Ical, Qcal)
    
    times = stepData['Time']
    fluxV = stepData['FluxV']
    fluxVDAC = scopeVtoDACamp(fluxV * V, stepData, useReg = False)
    flux = dacAmpToFlux(fluxVDAC, stepData, useReg = False)
    
    I = stepData['I']
    Q = stepData['Q']
    phase = IQtoPhase(I, Q)
    
    startI = stepData.parameters['startI']
    endI = stepData.parameters['endI']
    startQ = stepData.parameters['startQ']
    endQ = stepData.parameters['endQ']
    startDACamp = stepData.parameters['startDACamp']
    endDACamp = stepData.parameters['endDACamp']

    plt.figure()
    plt.title('I step vs expected')
    plt.plot(times, I, 'rs', label = 'response I')
    plt.plot([times[0],times[-1]], [startI, startI], 'b-', linewidth = 5, label = 'Expected Start I')
    plt.plot([times[0],times[-1]], [endI, endI], 'g-', linewidth = 5, label = 'Expected end I')
    plt.legend(numpoints = 1, loc = 3)
    plt.show()
    
    plt.figure()
    plt.title('Q step vs expected')
    plt.plot(times, Q, 'rs', label = 'response Q')
    plt.plot([times[0],times[-1]], [startQ, startQ], 'b-', linewidth = 5, label = 'Expected Start Q')
    plt.plot([times[0],times[-1]], [endQ, endQ], 'g-', linewidth = 5, label = 'Expected end Q')
    plt.legend(numpoints = 1, loc = 3)
    plt.show()
    
    plt.figure()
    plt.title('fluxV step vs expected')
    plt.plot(times, fluxVDAC, 'rs', label = 'response fluxV')
    plt.plot([times[0],times[-1]], [startDACamp, startDACamp], 'b-', linewidth = 5, label = 'Expected Start fluxV')
    plt.plot([times[0],times[-1]], [endDACamp,endDACamp], 'g-', linewidth = 5, label = 'Expected end fluxV')
    plt.legend(numpoints = 1, loc = 3)
    plt.show()
    
    
def plot_settle_datasets(sample, set_qubits=None, set_amps=None, first=False,
                         directories=None, dir_sets=None,
                         plot=False, frequencies=False, title='', autoscale=True,
                         labels=None, plot_axes=[1, 5e5, 90.0, 100.4],
                         use_legend=True, leg_loc=4):
    if directories is None:
        directories = [sample._dir]

    colors = ['r', 'b', 'g', 'c', 'm', 'y', 'k']


    if dir_sets is not None:
        for dir_set in dir_sets:
            d = dvw(dir_set[0], sample._cxn)
            data = d[dir_set[1]]
            if 'Z Settle Time Long Rise' not in data.name:
                raise Exception('Error, this is not Z-settle dataset. Requested'
                                'data set is: {}'.format(data.name))

            qubit = data.parameters['measure'][0]
            q = data.parameters[qubit]

            # Get spec peaks
            fit_gauss_max = lambda x, y: fitting.getMaxGauss(x, y,
                                                             fitToMax=True)
            times, freqs_MHz = fitting.minima_cuts(data, 0, method=fit_gauss_max,
                                               plot=False)
            # offset datavault times by 50ns + spectroscopyLen/2
            spectroscopylen = data.parameters[qubit]['spectroscopyLen']['ns']
            times += 50 + spectroscopylen/2.

            # get zfunc and inverse zfunc
            Z = zfuncs.TransmonFrequency(q['zFuncAna'])

            def fit_settle(ts, a0, t0, a1, t1, a2, t2, b):
                outp = []
                for t in ts:
                    outp.append((a0 * np.exp(-t/t0)) + (a1 * np.exp(-t/t1)) + (a2 * np.exp(-t/t2)) + b)
                return np.array(outp)

            # seed estimates [A0, T0, A1, T1, A2, T2, B]
            # A0 ~ 5 MHz
            # A1 ~ 5 MHz
            # A2 ~ 1 MHz
            # T0 ~ 10 nsec
            # T1 ~ 1000 nsec
            # T2 ~ 50000 nsec
            # B ~ a frequency.

            # fit qubit frequency vs time
            popt1, pcov1 = curve_fit(fit_settle, times, freqs_MHz,
                                     p0=[5., 10., 5., 100., 1000., 50000.,
                                         freqs_MHz[-1]], maxfev=5200)

            # calculate step amplitude in frequency
            a0, t0, a1, t1, a2, t2, f_inf = popt1


            ordering = t0 < t1 and t1 < t2
            # if not ordering:
            #     raise Exception('Error! Time constants out of order! t0: {} ns;'
            #                     ' t1: {} ns; t2: {} ns'.format(t0, t1, t2))

            z_amp_inf = Z.freq_to_amp((f_inf / 1000.) * GHz)

            # plot_times
            plot_times = np.linspace(np.min(times), np.max(times), 1000)
            # compute frequencies for fit plot
            plot_frequencies = fit_settle(plot_times, a0, t0, a1, t1, a2, t2, f_inf)
            # scale fit frequencies to
            plot_z_amplitudes = Z.freq_to_amp((plot_frequencies / 1000.) * GHz)
            # scale z_amps to fraction
            plot_percent = plot_z_amplitudes * 100. / z_amp_inf

            # Amplitude of step from DAC
            z_amplitude = float(data.parameters['stack']['frame0']['arg']['z_amplitude'])

            if np.abs(z_amplitude-z_amp_inf) > 0.03:
                raise Exception('Error!  Requested amplitude ({}) and measured '
                                'amplitude ({}) do not match!'
                                ''.format(z_amplitude, z_amp_inf))

            # Amplitude of step according to zfunc
            zfunc_amplitudes = Z.freq_to_amp(freqs_MHz / 1000. * GHz)
            # normalized z_func_amplitudes
            zfunc_percent = 100. * zfunc_amplitudes / z_amp_inf

            # convert settle amplitudes to fraction of step
            f_step_amp = ((Z.amp_to_freq(0.0)['GHz']) * 1000.) - f_inf
            a0 /= f_step_amp
            a1 /= f_step_amp
            a2 /= f_step_amp

            f_step_amp = np.int(f_step_amp*10.)/10.
            # data plot
            if frequencies:
                plt.semilogx(times, freqs_MHz, lw=0, marker='s',
                             label='{}: z_amp: {}; f_amp: {} MHz'.format(
                                 qubit,
                                 z_amplitude, f_step_amp))
                plt.ylabel('Qubit Frequency (MHz)', fontsize=18)

                if not autoscale:
                    plt.axis(plot_axes)
                if len(dir_sets) == 1:
                    plt.semilogx(plot_times, plot_frequencies, lw=2, marker='s',
                                 markeredgecolor='none')

            else:
                plt.semilogx(times, zfunc_percent, lw=0, marker='s',
                             label='{}; {} MHz'.format(
                                 z_amplitude, f_step_amp))
                plt.ylabel('Z Amp (% of step)', fontsize=18)
                if not autoscale:
                    plt.axis(plot_axes)
            #  fit
            # plt.semilogx(x_vals/1e3,y_vals*100, color = colors[counter], lw = 2, marker = None, label = 'z_step_amp: %s ;Tshort %s us; Ashort: %s; Tlong %s us; Along: %s'%(str(z_amplitude),str(shortTime), str(shortAmp), str(longTime), str(longAmp)))
                if len(dir_sets) == 1:
                    plt.semilogx(plot_times, plot_percent, lw=2, marker='s', markeredgecolor='none')

        if plot:
            plt.title(title,
                      fontsize=18)

            plt.xlabel('Time (ns)', fontsize=18)
            plt.tick_params(labelsize=15)
            if use_legend:
                if labels is not None:
                    plt.legend(labels, numpoints=1, loc=leg_loc)
                else:
                    plt.legend(numpoints=1, loc=leg_loc)
            plt.tight_layout()
            plt.show()

        if False:
            plt.figure()
            plt.title(
                'Settle Amplitude vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
                fontsize=11)
            plt.ylabel('Settle amplitude (% of step)')
            plt.xlabel('Step amplitude (DAC units)')
            plt.plot(zamps, a0s, 'rs', label='a0s')
            plt.plot(zamps, a1s, 'bs', label='a1s')
            plt.plot(zamps, a2s, 'gs', label='a2s')
            plt.legend(numpoints=1, loc=2)
            plt.axis(plot_axes)
            plt.tight_layout()
            plt.show()

            plt.figure()
            plt.title(
                'Settle Amplitude vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
                fontsize=11)
            plt.ylabel('Settle amplitude (% of step)')
            plt.xlabel('Step amplitude (DAC units)')
            plt.semilogy(zamps, np.abs(a0s), 'rs', label='a0s')
            plt.semilogy(zamps, np.abs(a1s), 'bs', label='a1s')
            plt.semilogy(zamps, np.abs(a2s), 'gs', label='a2s')
            plt.legend(numpoints=1, loc=3)
            # plt.axis([-1.0,1.0, 0, 10])
            plt.tight_layout()
            plt.show()

            plt.figure()
            plt.title(
                'Settle Times vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
                fontsize=11)
            plt.ylabel('Settle Time nsec')
            plt.xlabel('Step amplitude (DAC units)')
            plt.semilogy(zamps, t0s, 'rs', label='t0s')
            plt.semilogy(zamps, t1s, 'bs', label='t1s')
            plt.semilogy(zamps, t2s, 'gs', label='t2s')
            plt.legend(numpoints=1, loc=2)
            # plt.axis([-1.0,1.0, 0, 10])
            plt.tight_layout()
            plt.show()

    return times, zfunc_percent
def cal_IQ(sample, DACamps=np.linspace(-1.0,1.0,num=101)/4.0, loFreq=6.4 * GHz,
            loPower=18.0 * dBm, averages=5, name='DC_calibration',
            update=True, calScope=True, useScope=False):
    if calScope and not useScope:
        calibrateToDAC(sample, numPoints = 7, plot = False)
    
    with labrad.connect() as cxn1:
        independents = [('FluxV','V')]
        dependents = [('I','','V'),('Q','','V')]
        
        # create a DV reference for storing the summary data
        dv1 = cxn1.data_vault
        dv1.cd(sample._dir, True)
        dv1.new(name, independents, dependents)

        cxn = sample._cxn
        adr = cxn.adr_server
        ADR = adr.list_devices()

        sample['ADR'] = '%s'%ADR
        sample['averages'] = averages
        adr.select_device()
        temperatures = adr.temperatures()
        sample['50K_Temp'] = temperatures[0]
        sample['4K_Temp'] = temperatures[1]
        sample['4K_Mag_Temp'] = temperatures[2]
        ruox = adr.ruox_status()
        sample['Ruox_Temp'] = ruox[0]
        sample['Ruox_Resistance'] = ruox[1]
        sample['Magnet_Current'] = adr.magnet_status()[0]
        sample['Magnet_Voltage'] = adr.magnet_status()[1]
                
        p1 = dv1.packet()
        start = time.time()
        sample['Start_time'] = start*1e9*ns
        d = dvw(sample, cxn)
        
        sample['IQcalibrationDir'] = sample._dir
        sample['IQcalibrationDataSet'] = d[-1].name
        sample['scanType'] = 'DC_calibration'
        if useScope:
            sample['DC_inst'] = 'Infiniium Scope'
        else:
            sample['DC_inst'] = 'Three Agilent DMMs'
        
        #set FPGA voltage
        DAC_DC(sample, amp = DACamps[0])
                
        # Set up Hittite
        config_Hittite(sample, loFreq = loFreq, loPower = loPower, outputState = 'ON')
        
        if not useScope:
            # Configure DMMS
            dmm = cxn.agilent_34401a_dmm
            dmms = dmm.list_devices()
        
        for val in DACamps:
            I = []
            Q = []
            fluxV = []
            print 'Current DC voltage:  ', val
            
            # Set DAC Value
            DAC_DC(sample, amp = val)
            time.sleep(1)
            
            if useScope:
                data = InfiniiumScan(sample, averages = averages, config = True, record = False, name = 'Scopetrace', sampleRate = 40e9, horizScale = 1e-06, trig_level = 0.0, amp = val)
                fluxV = np.array(data.T[1]).mean()
                I = np.array(data.T[2]).mean()
                Q = np.array(data.T[3]).mean()
            #Take DMM points and average
            else:
                avgCount = 0
                while avgCount < averages:
                    dmm.select_device(dmms[0][1])
                    I.append(dmm.voltage()['V'])
                    dmm.select_device(dmms[1][1])
                    Q.append(dmm.voltage()['V'])
                    dmm.select_device(dmms[2][1])
                    fluxV.append(dmm.voltage()['V'])
                    avgCount += 1

                I = np.array(I).mean()
                Q = np.array(Q).mean()
                fluxV = np.array(fluxV).mean()

            print 'fluxV: ',fluxV
            print 'I: ',I
            print 'Q: ',Q

            currentTime = time.time()-start
                      
            dataPoint=[]
            dataPoint.append(fluxV)
            dataPoint.append(I)
            dataPoint.append(Q)
            dataPoint = np.array(dataPoint).T
            dv1.add(dataPoint)
            
        DCIQcofit(sample, dataSet = None, plot = False, dataDir = None, useReg = True)
        
        for key in sample.keys():
            p1.add_parameter(key, sample.__getitem__(key))
        p1.send()
    
    
    DAC_DC(sample, amp = 0.0)
    config_Hittite(sample, loFreq = loFreq, loPower = loPower,  outputState = 'OFF')
def IQtoFluxStep(s, dataDir = ['', 'Brooks', 'parAmp', 'impaNGC_CuBox_CuAuPCB', '151218', 'step'], box = ''):

    if box == 'UCSB':
        dataDir = ['', 'Brooks', 'parAmp', 'impaUCSBAlBox', '151118', 'calibration'] #UCSB IMPA Box
        dataSet = 783
        dataSet = 889
        dataSet = 513

    if box == 'NGC Cu':
        dataDir = ['', 'Brooks', 'parAmp', 'impaNGC_CuBox_CuAuPCB', '151218', 'step']
        dataSet = 783
    if box == 'NGC Cu2':
        dataDir = ['', 'Brooks', 'parAmp', 'impaNGC_CuBox_CuAuPCB', '160109', 'step']
        dataSet = 853

    # Get Calibration
    fluxVScale, fluxVOffset, aI, aQ, thetaI, thetaQ, Z0, Ic = DCIQcofit(s, plot = False, bound = 0.1, dataDir = dataDir, box = box)
    # poptQ, pcovQ, poptI, pcovI, popt1 = fitIQ(s, dataSet = 34, plot = False)

    phi0 = 2.067833758 * (10**-15)  #Wb
    omega = 2 * np.pi * 4.2 * 10**9 # rad/sec
    cap = 4.0 * 10**-12 # Farads


    cxn = s._cxn

    d = dvw(dataDir,cxn)
    dataSets = d.keys()
    # dataSets = dataSets[538:850]
    # dataSets = dataSets[538:850]
    # dataSets = dataSets[1220:1221]

    if box == 'UCSB':
        dataSets = dataSets[889:1254]
        # dataSets = dataSets[::-1]
        # dataSets = dataSets[14::]
        dataSets = dataSets[::20]

    if box == 'NGC Cu':
        # dataSets = dataSets[20:341]
        dataSets = dataSets[365::]
        dataSets = dataSets[::-1]
        dataSets = dataSets[17::]
        dataSets = dataSets[::20]

    if box == 'NGC Cu2':
        # dataSets = dataSets[dataSet+1:133]
        # dataSets = dataSets[::4]
        dataSets = [dataSets[4]]
        # dataSets = dataSets[0:1]
        # del dataSets[3]


    cm = plt.cm.get_cmap('RdYlBu')

    for dataSet in dataSets:
        #
        # Import Data Set:  Time, fluxV, I and Q
        #
        data = d[dataSet]
        color = np.float(string.split(data.parameters.get('stepAmp'))[0])
        print 'color:  ',color
        if color > 0.28:
            decimation = 1
            times = data.T[0]
            fluxV = data.T[1]
            flux = (fluxV * fluxVScale + fluxVOffset - np.pi/2)/np.pi
            I = data.T[2]
            Q = data.T[3]


            #
            # Shift I and Q data back in time to align step edge
            #
            shift = 38.9e-9 # sec
            shiftPts = np.int(shift/(((times.max()-times.min())/len(times))*10e-10))
            print 'shiftPts:  ',shiftPts
            times = np.array(times[:-shiftPts])
            flux = np.array(flux[:-shiftPts])
            fluxV = np.array(fluxV[:-shiftPts])
            I = np.array(I[shiftPts:])
            Q = np.array(Q[shiftPts:])



            #
            # Rotate I and Q by fit params (arbitrary theta rotation) back to 0.
            #
            Iscaled = aI * (I * np.cos(-thetaI) - Q * np.sin(-thetaI))
            Qscaled = aQ * (I * np.sin(-thetaQ) + Q * np.cos(-thetaQ))

            # if box == 'NGC Cu' or box == 'NGC Cu2':
            if box == 'NGC Cu':
                thetaI = 1.5
                thetaQ = 1.5
                Iscaled = 1 * (I * np.cos(-thetaI) - Q * np.sin(-thetaI))
                Qscaled = 1 * (I * np.sin(-thetaQ) + Q * np.cos(-thetaQ))
            # Iscaled = I
            # Qscaled = Q

            #
            # Compute and unwrap the phase
            # reverse the phase for unwrapping since the ending phase should always be the same.
            #
            # I = I[::-1]
            # Q = Q[::-1]
            phases = np.arctan(Qscaled/Iscaled)
            phases = (np.unwrap(phases[::-1]*2, discont = np.pi)/2)/np.pi
            phases = phases[::-1]

            #
            # Compute Lj from phase and fit params
            #

            Lj = 1/(omega *((np.tan(phases)/(2*Z0)) + (omega * cap)))


            #
            # Compute phi/phi0 from Lj and fit params
            #

            fluxComputed = np.arccos((phi0/(Lj * 2 * np.pi * Ic)))/np.pi


            #
            # Decimate to plot
            #
            times = times [::decimation]
            flux = flux[::decimation]
            fluxV = fluxV[::decimation]
            I = I[::decimation]
            Q = Q[::decimation]
            phases = phases[::decimation]
            fluxComputed = fluxComputed[::decimation]
            #
            #  Convert phase to

            A1 = np.array(flux[100]).mean()
            B1 = np.array(flux[-100:]).mean()

            A2 = np.array(fluxComputed[100]).mean()
            B2 = np.array(fluxComputed[-100]).mean()

            fluxComputed = (fluxComputed - A2)/(B1-A1)
            flux= (flux - A1)/(B1-A1)


            # phase = np.arctan(Q/I)
            cm = plt.cm.get_cmap('RdYlBu')
            # sc = plt.scatter(times, phases, c = np.zeros(len(times)) + color, vmin = 0, vmax = 3.5, cmap = cm, edgecolors='none')
            if max(np.abs(phases)) < 60 and (np.max(phases) - np.min(phases) > 0.75):
                # sc = plt.scatter(times, fluxComputed, c = np.zeros(len(times)) + color, vmin = 0.3, vmax = 3.5, cmap = cm, edgecolors='none', alpha = 0.5, label = '%s V'%color)
                # sc = plt.scatter(times, flux, c = np.zeros(len(times)) + color, vmin = 0.3, vmax = 3.5, cmap = cm, edgecolors='none', alpha = 0.5, label = '%s V'%color)
                plt.plot(times, fluxComputed, 'rs--', label = 'response', markeredgecolor='none')
                plt.plot(times, flux, 'bo--', label = 'input', markeredgecolor='none')
                # plt.plot(times, fluxComputed/flux, 'cs--', label = 'input', markeredgecolor='none')

            # plt.plot(times, phases, alpha = 0.5, c = color, cmap = cm)

            # Lj = 2 * Z0 / (np.tan(phases%np.pi) - 2 * omega * cap * Z0)
            # computedFlux = (1/np.pi) * np.arccos(phi0 / (2 * np.pi * Ic * Lj))
            # sc = plt.scatter(times, computedFlux, c = np.zeros(len(times)) + color, vmin = 0, vmax = 3.5, cmap = cm, edgecolors='none')


            # plt.colorbar(sc, label = 'Step Voltage (Volts)')
            # plt.title('Output Phase/Input Voltage vs. Time (nsec)',fontsize=20)
            # plt.ylabel('Arb (not even linear)')
            plt.title('Flux (Phi0) vs. Time (nsec)',fontsize=20)
            plt.ylabel('Flux (Phi0)')
            plt.xlabel('Time (nsec)')
            plt.legend(numpoints =1,loc = 1)
            # plt.axis([-10, 100, -0.2,0.2 ])
            plt.show()
def DCIQcofit(sample, dataSet = None, plot = False, bound = 0.15, dataDir = None, useReg = False):

    cxn = sample._cxn
    dvw.cache.clear()
    if dataDir == None:
        d = dvw(sample['IQcalibrationDir'], cxn)
    else:
        d = dvw(dataDir,cxn)

    if dataSet == None:
        dataSet = sample['IQcalibrationDataSet']

    #
    # Import Data Set:  fluxV, I and Q
    #
    data = d[dataSet]
    I = data['I']
    Q  = data['Q']
    fluxV = data['FluxV']

    # convert flux voltage from DMM volts to DAC amp using the scaling from the datavault
    if useReg:
        paramSource = sample
    else:
        paramSource = data

    #convert flux voltage from DMM volts to DAC amp
    fluxV = dmmVtoDACamp(fluxV * V, paramSource, useReg = useReg)



    phase = IQtoPhase(I, Q)


    # create interp with linear spacing in flux.
    phaseToVolts = sc.interpolate.interp1d(phase, fluxV)
    voltsToPhase = sc.interpolate.interp1d(fluxV, phase)

    linFluxV = np.linspace(np.min(fluxV), np.max(fluxV), 501)
    linPhase = voltsToPhase(linFluxV)


    #
    #  Function to fit the fluxV per Phi0 period
    #
    def periodFit(fluxV, Amplitude, Offset, Phase, Period):
        return Amplitude * np.abs(np.cos(fluxV * Period  + Phase)) + Offset

    # p0s = [Amplitude, Offset, Phase, Period]

    p0s = [1.3,-1.0,0.1 * np.pi ,2*np.pi/0.7]
    poptA, pcovA = curve_fit(periodFit, linFluxV, linPhase, p0 = p0s, maxfev = 5200)

    p0s = [1.3,-1.0,0.5 * np.pi ,2*np.pi/0.7]
    poptB, pcovB = curve_fit(periodFit, linFluxV, linPhase, p0 = p0s, maxfev = 5200)

    perrA = np.linalg.norm(np.sqrt(pcovA[2][2]**2 + pcovA[3][3]**2))
    perrB = np.linalg.norm(np.sqrt(pcovB[2][2]**2 + pcovB[3][3]**2))

    if perrA < perrB:
        popt = poptA
        pcov = pcovA

    else:
        popt = poptB
        pcov = pcovB


    dacAmpToFluxGain = popt[3]/np.pi
    dacAmpToFluxOffset = popt[2]/np.pi

    sample['dacAmpToFluxGain'] = dacAmpToFluxGain
    sample['dacAmpToFluxOffset'] = dacAmpToFluxOffset

    print 'Volts per cycle: ', (2.0/dacAmpToFluxGain)
    print 'Volts shift:  ', dacAmpToFluxOffset

    #
    # plot fit on data.
    #
    xvals = np.linspace(np.min(fluxV), np.max(fluxV), 601)
    yvals = periodFit(xvals, popt[0], popt[1], popt[2], popt[3])

    if plot:
        plt.figure()
        plt.plot(xvals, yvals, 'b--', label = 'fit')
        plt.plot(linFluxV, linPhase, 'rs', label = 'data')
        plt.title('Make sure fit period and phase match data',fontsize=20)
        plt.ylabel('phase (arb)')
        plt.xlabel('Flux Voltage (Volts)')
        plt.legend(numpoints =1,loc = 1)
        plt.show()

    flux = dacAmpToFlux(linFluxV, paramSource, useReg = useReg)

    sample['minFlux'] = np.min(flux)
    sample['maxFlux'] = np.max(flux)

    if plot:
        plt.figure()
        plt.plot(flux, linPhase, 'rs', label = 'Phase')
        plt.title('Make sure Phase centered at 0 flux and zero at phi0/2',fontsize=20)
        plt.ylabel('Phase (rad)')
        plt.xlabel('Flux (phi0)')
        plt.show()
def step_response(sample, startFlux = 0.0, endFlux = 0.39, averages = 50, plot = False, sampleRate = 5e8, horizScale = 203e-06):
    cxn = sample._cxn
    
    dvw.cache.clear()
    print 'DIR:  ', sample['IQcalibrationDir']
    print 'DataSet:  ', sample['IQcalibrationDataSet']
    d = dvw(sample['IQcalibrationDir'], cxn)
    dataSet = sample['IQcalibrationDataSet']
    calData = d[dataSet]
    
    Ical = calData['I']
    Qcal = calData['Q']

    # get FluxV in DMM volts and scale to DACamp
    fluxV = calData['FluxV']
    fluxV = dmmVtoDACamp(fluxV*V, calData, useReg = False)
    
    # compute phase and unwrap
    phase = IQtoPhase(Ical, Qcal)
    
    # generate interpolation functions from calibration
    phaseToVolts = sc.interpolate.interp1d(phase, fluxV)
    voltsToPhase = sc.interpolate.interp1d(fluxV, phase)
    dacAmpToI = sc.interpolate.interp1d(fluxV, Ical)
    dacAmpToQ = sc.interpolate.interp1d(fluxV, Qcal)
    

    
    #  Config Hittite to match settings from calibration
    config_Hittite(sample,
                   loFreq = calData.parameters['loFreq'], 
                   loPower = calData.parameters['loPower'],
                   outputState = 'ON')
    
    
    sample['startFlux'] = startFlux
    sample['endFlux'] = endFlux
    #  Compute start and end DAC amps to generate the step
    startDACamp = fluxToDACamp(startFlux, calData, useReg = False)
    endDACamp = fluxToDACamp(endFlux, calData, useReg = False)
    sample['startDACamp'] = startDACamp
    sample['endDACamp'] = endDACamp
    
    #  Compute start and end DMM voltages
    startDMMvoltage = dacAmpToDMMV(startDACamp, calData, useReg = False)
    endDMMvoltage = dacAmpToDMMV(endDACamp, calData, useReg = False)
    sample['startDMMvoltage'] = startDMMvoltage
    sample['endDMMvoltage'] = endDMMvoltage
    
    #  Compute start and end DMM voltages
    startScopeVoltage = dacAmpToScopeV(startDACamp, calData, useReg = False)
    endScopeVoltage = dacAmpToScopeV(endDACamp, calData, useReg = False)
    sample['startScopeVoltage'] = startScopeVoltage
    sample['endScopeVoltage'] = endScopeVoltage
    
    #  Compute start and end I values
    startI = np.float(dacAmpToI(startDACamp))
    endI = np.float(dacAmpToI(endDACamp))
    print 'startI: ', type(startI), startI
    sample['startI'] = startI
    sample['endI'] = endI
    
    #  Compute start and end Q values
    startQ = np.float(dacAmpToQ(startDACamp))
    endQ = np.float(dacAmpToQ(endDACamp))
    sample['startQ'] = startQ
    sample['endQ'] = endQ
    
    
    print ''
    print ''
    print 'start I voltage: %s , end I voltage:  %s'%(startI, endI)
    print 'start Q voltage: %s , end Q voltage:  %s'%(startQ, endQ)
    print 'start Flux: %s , end Flux:  %s'%(startFlux, endFlux)
    print 'start DAC voltage: %s , end DAC voltage:  %s'%(startDACamp, endDACamp)
    print 'start DMM voltage: %s , end DMM voltage:  %s'%(startDMMvoltage, endDMMvoltage)
    print 'start scope voltage: %s , end scope voltage:  %s'%(startScopeVoltage, endScopeVoltage)
    print ''
    print ''
    
    sample['scanType'] = 'step_response'
    DAC_square(sample, lowAmp = startDACamp, highAmp = endDACamp)
    
    xs = []
    ys = []
    zs = []
    for (x,y,z) in zip(fluxV, Ical, Qcal):
        if np.min([startDACamp, endDACamp]) < x and x < np.max([startDACamp, endDACamp]):
           xs.append(x)
           ys.append(y)
           zs.append(z)
    
    minI = np.min(ys)
    maxI = np.max(ys)
    print 'minI: %s ; maxI: %s'%(minI, maxI)
    
    minQ = np.min(zs)
    maxQ = np.max(zs)
    print 'minQ: %s ; maxQ: %s'%(minQ, maxQ)
    
    overRange = (3.0/8.0) #150% of range/ 8 divisions
    
    # compute scales and y positions for scope channels
    # scales = [np.abs(startScopeVoltage['V'] - endScopeVoltage['V'])*overRange, np.abs(minI-maxI)*overRange, np.abs(minQ-maxQ)*overRange, 0.5]
    # positions = [np.mean([startScopeVoltage['V'],endScopeVoltage['V']]), np.mean([minI,maxI]), np.mean([minQ,maxQ]), 0.0]
    trig_level = np.mean([startScopeVoltage['V'],endScopeVoltage['V']])+ 0.0 * np.abs(endScopeVoltage['V'] - startScopeVoltage['V'])
    print 'Trigger at: ', trig_level
    
    fluxVsub = np.array(xs)
    phaseSub = voltsToPhase(xs)
    Isub = dacAmpToI(xs)
    Qsub = dacAmpToQ(xs)

    if plot:
        # plt.figure()
        # plt.title('Calibration IQ points vs DMM Flux Voltage')
        # plt.plot(dacAmpToFlux(fluxV, calData, useReg = False), phase, 'rs-', label = 'phase')
        # plt.plot(dacAmpToFlux(fluxVsub, calData, useReg = False), phaseSub, 'bs', label = 'step phase')
        # plt.xlabel('Flux (phi0)')
        # plt.ylabel('DMM Volts')
        # plt.legend(numpoints = 1, loc = 4)
        # plt.show()
    
        # plt.figure()
        # plt.title('Calibration Phase vs DAC amp')
        # plt.plot(fluxV, phase, 'rs-', label = 'phase')
        # plt.plot(fluxVsub, phaseSub, 'bs', label = 'step phase')
        # plt.xlabel('DAC amp')
        # plt.ylabel('rad')
        # plt.legend(numpoints = 1, loc = 4)
        # plt.show()
    
        plt.figure()
        plt.title('Calibration IQ points vs DAC amp')
        plt.plot(fluxV, Ical, 'r-', label = 'I')
        plt.plot(fluxVsub, Isub, 'rs', label = 'Istep')
        plt.plot(fluxV, Qcal, 'b-', label = 'Q')
        plt.plot(fluxVsub, Qsub, 'bs', label = 'Qstep')
        plt.plot(startDACamp, startI, 'rx', markersize = 10, linewidth = 10, label = 'start I')
        plt.plot(startDACamp, startQ, 'bx', markersize = 10, linewidth = 10, label = 'start Q')
        plt.plot(endDACamp, endI, 'rs', markersize = 10, linewidth = 10, label = 'end I')
        plt.plot(endDACamp, endQ, 'bs', markersize = 10, linewidth = 10, label = 'end Q')
        plt.xlabel('DAC amp')
        plt.ylabel('Scope Volts')
        plt.legend(numpoints = 1, loc = 4)
        plt.show()

    # plt.figure()
    # plt.title('Calibration IQ points vs DMM Flux Voltage')
    # plt.plot(fluxV, calData['I'], 'rs-', label = 'I')
    # plt.plot(fluxV, calData['Q'], 'bs-', label = 'Q')
    
    # plt.xlabel('Flux Voltage (DMM volts)')
    # plt.ylabel('DMM Volts')
    # plt.legend(numpoints = 1, loc = 4)
    # plt.show()

    
    stepResp = InfiniiumScan(sample, averages = averages, sampleRate = sampleRate, 
                  horizScale = horizScale, config = True, record = True, 
                  name = 'step_response', 
                  trig_level = trig_level)
    if plot:
        checkStep(sample, -1)
    # times = stepResp.T[0]
    # inputVoltage = stepResp.T[1] * V
    # respI = stepResp.T[2]
    # respQ = stepResp.T[3]

    # plt.figure()
    # plt.title('I step vs expected')
    # plt.plot(times, respI, 'rs', label = 'response I')
    # plt.plot([times[0],times-1], [startScopeV, startScopeV], 'b-', linewidth = 5, label = 'Expected Start I')
    # plt.plot([times[0],times-1], [endScopeV, endScopeV], 'g-', linewidth = 5, label = 'Expected end I')
    # plt.legend(numpoints = 1, loc = 3)
    # plt.show()
    

    
    # # inputVoltage = scopeVtoDACamp(inputVoltage, calData, useReg = False)
    # # inputPhase = voltsToPhase(inputVoltage)
    # # # inputFlux = dacAmpToFlux(inputVoltage, calData, useReg = False)
    
    # responsePhase = np.arctan(respQ/respI)
    # responsePhase = ((np.unwrap(responsePhase[::-1]*2, discont = np.pi)/2)/np.pi)[::-1]
    
    # # responseFluxV = phaseToVolts(responsePhase)
    # # responseFlux = dacAmpToFlux(responsePhase)
    
    # # plt.plot(times, inputPhase, 'r-', label = 'input Flux')
    # plt.plot(times, responsePhase, 'b-', label = 'response Flux')
    # plt.xlabel('times')
    # plt.ylabel('flux (Phi0)')
    # plt.legend(loc = 4, numpoints = 1)
    # plt.show()
    del sample['startFlux']
    del sample['endFlux']
    del sample['startDACamp'] 
    del sample['endDACamp']
    del sample['startDMMvoltage']
    del sample['endDMMvoltage']
    del sample['startScopeVoltage'] 
    del sample['endScopeVoltage']
    
    return stepResp
def summary(s, set_qubit=None, directory=None, plot=False, Brass=False,
            falling=False, set_amps=None, frequencies=False):
    dataDirsTFunc = []
    longZdataSets = []

    # Directory to look for settle times:
    if directory is None:
        dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'NGQ', '141020'])
    else:
        dataDirsTFunc.append(directory)

    cxn = s._cxn
    plt1 = plt.figure()
    counter = 0
    colors = ['r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y',
              'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm',
              'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c',
              'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g',
              'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b',
              'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r',
              'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k',
              'r', 'b', 'g', 'c', 'm', 'y', 'k', 'r', 'b', 'g', 'c', 'm', 'y',
              'k', 'r', 'b', 'g', 'c', 'm', 'y', 'k']
    ordering = []
    t0s = []
    t1s = []
    t2s = []
    a0s = []
    a1s = []
    a2s = []
    zamps = []
    for dataDirTFunc in dataDirsTFunc:
        d = dvw(dataDirTFunc, cxn)

        # Pull out all data sets of this type in the directory
        settleDataSets = (d[int(x[:5])] for x in d.keys() if
                          'Z Settle Time Long Rise' in x)

        if falling:
            settleDataSets = (d[int(x[:5])] for x in d.keys() if
                              'Spectroscopy Z settle falling' in x)

        for data in settleDataSets:

            biasvals = []
            freqvals = []
            freqvals2 = []
            timevals = []

            print 'dir: {}'.format(dataDirTFunc)
            print 'DATA SET:', data.name
            # get data set from data vault
            qubit = data.parameters['measure'][0]
            q = data.parameters[qubit]

            # Get spec peaks
            fit_gauss_max = lambda x, y: fitting.getMaxGauss(x, y,
                                                             fitToMax=True)
            spectroscopylen = data.parameters[qubit]['spectroscopyLen']['ns']
            times, freqs = fitting.minima_cuts(data, 0, method=fit_gauss_max, plot=False)
            times += 50 + spectroscopylen/2.

            z_amplitude = float(
                data.parameters['stack']['frame0']['arg']['z_amplitude'])

            conditions = True
            if set_qubit is not None:
                if qubit != set_qubit:
                    conditions = False
            if set_amps is not None:
                if float(
                    data.parameters['stack']['frame0']['arg']['z_amplitude']) not in set_amps:
                    conditions = False
                    print 'set_amp: {}; data_amp: {}'.format(set_amps, float(
                    data.parameters['stack']['frame0']['arg']['z_amplitude']))

            if len(times) > 50 and conditions:
                zamps.append(z_amplitude)
                # get zfunc and inverse zfunc
                Z = zfuncs.TransmonFrequency(q['zFuncAna'])

                def fitSettle(ts, a0, t0, a1, t1, a2, t2, b):
                    outp = []
                    for t in ts:
                        # outp.append((a0 * np.exp(-t/t0)) + (a1 * np.exp(-t/t1)) + (a2 * np.exp(-t/t2)) + b)
                        # two Tau fit.
                        outp.append(
                            (a1 * np.exp(-t / t1)) + (a2 * np.exp(-t / t2)) + b)
                    return np.array(outp)

                # seed estimates [A0, T0, A1, T1, A2, T2, B]
                # A0 ~ 5 MHz
                # A1 ~ 5 MHz
                # A2 ~ 1 MHz
                # T0 ~ 10 nsec
                # T1 ~ 1000 nsec
                # T2 ~ 50000 nsec
                # B ~ a frequency.

                popt1, pcov1 = curve_fit(fitSettle, times, freqs,
                                         p0=[5, 10, 5, 1000, 1, 50000,
                                             freqs[-1]], maxfev=5200)

                f_step_amp = ((Z.amp_to_freq(0.0)['GHz']) * 1000) - popt1[6]
                a0s.append(popt1[0] / f_step_amp)
                a1s.append(popt1[2] / f_step_amp)
                a2s.append(popt1[4] / f_step_amp)
                t0s.append(popt1[1])
                t1s.append(popt1[3])
                t2s.append(popt1[5])

                print 'a0: ', a0s[-1]
                print 't0: ', t0s[-1]
                print 'a1: ', a1s[-1]
                print 't1: ', t1s[-1]
                print 'a2: ', a2s[-1]
                print 't2: ', t2s[-1]
                print 'b: ', popt1[6]
                ordering.append((popt1[1] < popt1[3] and popt1[3] < popt1[5]))
                print 'correct ordeR???', ordering[-1]
                infSettleZ = Z.freq_to_amp((popt1[6] / 1000) * GHz)

                xs = np.array(times)
                ys = np.array(freqs)
                x_vals = np.linspace(np.min(times), np.max(times), 10000)
                y_vals = fitSettle(x_vals, popt1[0], popt1[1], popt1[2],
                                   popt1[3], popt1[4], popt1[5], popt1[6])
                y_vals = Z.freq_to_amp((np.array(y_vals) / 1000) * GHz)
                y_vals = y_vals / infSettleZ
                decay_time = np.floor(popt1[2] / 100) / 10

                shortAmp = ((popt1[0] / f_step_amp) * 100)
                shortTime = np.floor(popt1[2] / 100) / 10
                print 'Decay short amp:  %s percent' % shortAmp
                print 'Decay short time:  ', shortTime, 'us'

                longAmp = ((popt1[5] / f_step_amp) * 100)
                longTime = np.floor(popt1[6] / 100) / 10
                print 'Decay long amp:  %s percent' % longAmp
                print 'Decay long time:  ', longTime, 'us'

                print qubit
                # Amplitude of step from DAC
                z_amplitude = float(
                    data.parameters['stack']['frame0']['arg']['z_amplitude'])
                print ' z_amplitude', z_amplitude

                # Amplitude of step according to zfunc
                zfunc_amplitudes = (Z.freq_to_amp((np.array(freqs) / (1000)) * GHz))
                zfunc_amplitudes = zfunc_amplitudes / infSettleZ

                counter = int(z_amplitude * 10)

                #  data
                # plt.semilogx(times/1000,zfunc_amplitudes*100, color = colors[counter], lw = 0, marker = 's',markeredgecolor='none', label = labels[counter])
                # plt.semilogx(times/1000,zfunc_amplitudes*100, color = colors[counter], lw = 0, marker = 's',markeredgecolor='none', label = 'z_step_amp: %0.1f ;Tshort %.5f us; Ashort: %0.2d; Tlong %02d us; Along: %0.2d'%(z_amplitude,float(shortTime), np.float(shortAmp), longTime, np.float(longAmp)))
                f_step_amp = np.int(f_step_amp*10.)/10.
                if frequencies:
                    plt.semilogx(times / 1000., freqs,
                                 color=colors[counter], lw=0, marker='s',
                                 label='<{}> {}: z_amplitude: {}; f_step_amp: {} MHz'.format(
                                     np.int(data.name.split()[0]), qubit,
                                     z_amplitude, f_step_amp))
                    plt.ylabel('Qubit Frequency (MHz)', fontsize=18)
                else:
                    plt.semilogx(times / 1000., zfunc_amplitudes * 100,
                                 color=colors[counter], lw=0, marker='s',
                                 label='<{}> {}: z_amplitude: {}; f_step_amp: '
                                       '{} MHz'.format(
                                     np.int(data.name.split()[0]), qubit,
                                     z_amplitude, f_step_amp))
                    plt.ylabel('Z Amp (% of step)', fontsize=18)
                    # plt.axis([0.03, 24000, 90, 102])
                print type(z_amplitude), type(shortTime)
                #  fit
                # plt.semilogx(x_vals/1e3,y_vals*100, color = colors[counter], lw = 2, marker = None, label = 'z_step_amp: %s ;Tshort %s us; Ashort: %s; Tlong %s us; Along: %s'%(str(z_amplitude),str(shortTime), str(shortAmp), str(longTime), str(longAmp)))
                plt.semilogx(x_vals / 1000, y_vals * 100, color=colors[counter],
                             lw=2, marker='s', markeredgecolor='none')
                counter += 1
                # plt.title(dataDirTFunc, fontsize=18)
                # plt.xlabel('Time (us)', fontsize=18)
                # plt.tick_params(labelsize=15)
                # plt.legend(numpoints=1, loc=4)
                # plt.tight_layout
                # plt.show()

    if plot:
        plt.title(dataDirTFunc, fontsize=18)
        plt.xlabel('Time (us)', fontsize=18)
        plt.tick_params(labelsize=15)
        plt.legend(numpoints=1, loc=4)
        plt.tight_layout()
        plt.show()

    if False:
        plt.figure()
        plt.title(
            'Settle Amplitude vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
            fontsize=11)
        plt.ylabel('Settle amplitude (% of step)')
        plt.xlabel('Step amplitude (DAC units)')
        plt.plot(zamps, a0s, 'rs', label='a0s')
        plt.plot(zamps, a1s, 'bs', label='a1s')
        plt.plot(zamps, a2s, 'gs', label='a2s')
        plt.legend(numpoints=1, loc=2)
        plt.axis([-1.0, 1.0, -0.5, 0.5])
        plt.tight_layout()
        plt.show()

        plt.figure()
        plt.title(
            'Settle Amplitude vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
            fontsize=11)
        plt.ylabel('Settle amplitude (% of step)')
        plt.xlabel('Step amplitude (DAC units)')
        plt.semilogy(zamps, np.abs(a0s), 'rs', label='a0s')
        plt.semilogy(zamps, np.abs(a1s), 'bs', label='a1s')
        plt.semilogy(zamps, np.abs(a2s), 'gs', label='a2s')
        plt.legend(numpoints=1, loc=3)
        # plt.axis([-1.0,1.0, 0, 10])
        plt.tight_layout()
        plt.show()

        plt.figure()
        plt.title(
            'Settle Times vs Step Amplitude: a0*exp(-t/t0) + a1*exp(-t/t1) + a2*exp(-t/t2)',
            fontsize=11)
        plt.ylabel('Settle Time nsec')
        plt.xlabel('Step amplitude (DAC units)')
        plt.semilogy(zamps, t0s, 'rs', label='t0s')
        plt.semilogy(zamps, t1s, 'bs', label='t1s')
        plt.semilogy(zamps, t2s, 'gs', label='t2s')
        plt.legend(numpoints=1, loc=2)
        # plt.axis([-1.0,1.0, 0, 10])
        plt.tight_layout()
        plt.show()

    return zamps, a0s, a1s, a2s, t0s, t1s, t2s

    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxViasPCB', '160415'])
    # longZdataSets.append(63)
    # longZdataSets.append(64)
    # longZdataSets.append(65)
    # longZdataSets.append(66)
    # longZdataSets.append(67)
    # longZdataSets.append(68)
    # longZdataSets.append(69)



    # # title_plot = 'Al Box, Copper vias Q2'
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxAlCuPCB', '160305'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxAlCuPCB', '160305'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxAlCuPCB', '160305'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AlBoxAlCuPCB', '160305'])
    # longZdataSets.append(77)
    # longZdataSets.append(78)
    # longZdataSets.append(79)
    # longZdataSets.append(80)

    # # machined Al box, Q3
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AndrewsChip', 'D1', '160304'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AndrewsChip', 'D1', '160304'])
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'AndrewsChip', 'D1', '160304'])
    # longZdataSets.append(17)
    # longZdataSets.append(13)
    # longZdataSets.append(13)

    #  Brass box Cu Vias, Al PCB
    # dataDirsTFunc.append(['', 'Brooks', 'qubitData', 'BrassBoxCuViasAlPCB', '160418'])
    # # Amp = 0.4
    # longZdataSets.append(90)
def go(sample, stepDataSet, stepDataDir = None, calDataSet = None, plot = False):
    cxn = sample._cxn
    
    # get calibration datasets
    if stepDataDir == None:
        d = dvw(sample, cxn)
    else:
        d = dvw(dataDir, cxn)
        
    calDataSet = sample['IQcalibrationDataSet']
    calDataDir = sample['IQcalibrationDir']
    calD = dvw(calDataDir, cxn)

    calData = calD[calDataSet]
    fluxVcal = calData['FluxV']
    Ical = calData['I']
    Qcal = calData['Q']
    
    # convert flux voltage from dmmV to DAC amp
    fluxDACampCal = dmmVtoDACamp(fluxVcal * V, calData, useReg = False)
    # convert DAC amp flux voltage to flux
    fluxCal = dacAmpToFlux(fluxDACampCal, calData, useReg = False)
    
    # compute Cal phase
    phaseCal = IQtoPhase(Ical, Qcal)
    
    stepData = d[stepDataSet]
    fluxV = stepData['FluxV']
    fluxVDAC = scopeVtoDACamp(fluxV * V, stepData, useReg = False)
    
    times = stepData['Time']
    flux = dacAmpToFlux(fluxVDAC, stepData, useReg = False)
    I = stepData['I']
    Q = stepData['Q']

    # shift I back to Q
    shift = 1.1 # sec
    shiftPts = np.int(shift/(((times.max()-times.min())/len(times))))
    print 'shiftPts:  ',shiftPts
    if shiftPts > 0:
        times = np.array(times[:-shiftPts])
        flux = np.array(flux[:-shiftPts])
        Q = np.array(Q[:-shiftPts])
        I = np.array(I[shiftPts:])
    
    if plot:
        # plot I&Q to time check shift
        
        plt.figure()
        plt.title('CHECK TIME SHIFT Response Signals I and Q')
        plt.plot(times, I, 'r-',label = 'I')
        plt.plot(times, Q, 'b-',label = 'Q')
        plt.ylabel('Voltage (V)')
        plt.xlabel('Time (ns)')
        plt.legend(numpoints =1,loc = 3)
        plt.tight_layout()
        plt.show()  

    phase = IQtoPhase(I, Q)
    
    phaseSub = []
    fluxSub = []
    for p,f in zip(phaseCal, fluxCal):
        if f < np.max(flux) and f > np.min(flux):
            phaseSub.append(p)
            fluxSub.append(f)
            
    phaseToFlux = sc.interpolate.interp1d(phaseSub, fluxSub)
    
    fluxToPhase = sc.interpolate.interp1d(fluxCal, phaseCal)
    
    interpFluxes = np.linspace(np.min(fluxSub), np.max(fluxSub), 1000)
    interpPhases = fluxToPhase(interpFluxes)
    
    # plot volts to phase interp func
    if plot:
        plt.figure()
        plt.title('Phase response (rad) vs. Flux Voltage (Volts)')
        plt.plot(fluxCal, phaseCal, 'rs',label = 'data')
        plt.plot(interpFluxes, interpPhases, 'b-', label = 'interp')
        plt.plot(flux[0], phase[0], 'rx', markersize = 20, label = 'start Flux')
        plt.plot(flux[-1], phase[-1], 'bx', markersize = 20, label = 'end Flux')
        plt.ylabel('phase (rad)')
        plt.xlabel('Flux (phi0)')
        plt.legend(numpoints =1,loc = 3)
        plt.tight_layout()
        plt.show()

    interpPhases = np.linspace(np.min(phaseSub), np.max(phaseSub), 1000)
    interpFluxes = phaseToFlux(interpPhases)

    # plot phase to volts interp func
    if plot:
        plt.figure()
        plt.title('Flux (phi0) vs. Phase response (rad)')
        plt.plot(phaseCal, fluxCal, 'rs',label = 'data')
        plt.plot(interpPhases, interpFluxes, 'b-', label = 'interp')
        plt.xlabel('phase (rad)')
        plt.ylabel('Flux (phi0)')
        plt.legend(numpoints =1,loc = 3)
        plt.tight_layout()
        plt.show()
        

        plt.figure()
        plt.title('Response Phase (rad) vs Time (ns)')
        plt.plot(times, phase, 'r-', linewidth = 10, label = 'phase')
        plt.xlabel('Time (ns)')
        plt.ylabel('phase (rad)')
        plt.legend(numpoints = 1, loc = 3)
        plt.tight_layout()
        plt.show()
        

    
    
    # shift Response phase back to stimulus flux
    shift = 37.3 # sec
    shiftPts = np.int(shift/(((times.max()-times.min())/len(times))))
    print 'shiftPts:  ',shiftPts
    times = np.array(times[:-shiftPts])
    flux = np.array(flux[:-shiftPts])
    phase = np.array(phase[shiftPts:])
    
    
        
    # plot input step & response phase to time check shift
    if plot:
        plt.figure()
        plt.title('CHECK TIME SHIFT')
        plt.plot(times, flux, 'r-',label = 'stimulus')
        plt.plot(times, phase, 'b-',label = 'response')
        plt.ylabel('NOT SAME Y SCALES')
        plt.xlabel('Time (ns)')
        plt.legend(numpoints =1,loc = 3)
        plt.tight_layout()
        plt.show()    
    
    def savitzky_golay(y, window_size, order, deriv=0, rate=1):
        
        try:
            window_size = np.abs(np.int(window_size))
            order = np.abs(np.int(order))
        except ValueError, msg:
            raise ValueError("window_size and order have to be of type int")
        if window_size % 2 != 1 or window_size < 1:
            raise TypeError("window_size size must be a positive odd number")
        if window_size < order + 2:
            raise TypeError("window_size is too small for the polynomials order")
        order_range = range(order+1)
        half_window = (window_size -1) // 2
        # precompute coefficients
        b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
        m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
        # pad the signal at the extremes with
        # values taken from the signal itself
        firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
        lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
        y = np.concatenate((firstvals, y, lastvals))
        return np.convolve( m[::-1], y, mode='valid')