def gainFromS21(likelike_logmag, likeunlike_logmag, distance_m,header=17):
    '''
    Calculates the gain from the S21 following the source:
    https://www.pasternack.com/t-calculator-fspl.aspx

    This assumes the two antennas have the same gain. 
    '''
    try:
        freqs_Hz, logmag_vals = ff.readerFieldFox(likelike_logmag,header=header)
        fspl = numpy.abs(logmag_vals)
        p1 = 20*numpy.log10(distance_m)
        p2 = 20*numpy.log10(freqs_Hz)
        p3 = 20*numpy.log10(4*numpy.pi/(299792458))
        gain_like = (p1 + p2 + p3 - fspl)/2.0

        freqs_Hz, logmag_vals = ff.readerFieldFox(likeunlike_logmag,header=header)
        fspl = numpy.abs(logmag_vals)
        p1 = 20*numpy.log10(distance_m)
        p2 = 20*numpy.log10(freqs_Hz)
        p3 = 20*numpy.log10(4*numpy.pi/(299792458))
        gain_unlike = p1 + p2 + p3 - fspl - gain_like

        #import pdb; pdb.set_trace()

        return freqs_Hz, gain_like, gain_unlike
    except Exception as e:
            print(e)
def testing():
    try:
        #

        datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
        infiles = numpy.array(glob.glob(datapath + '*.csv'))
        roots = numpy.unique([
            infile.replace('_PHASE', '_FILLER').replace('_LOGMAG', '_FILLER')
            for infile in infiles
        ])

        #Prepare the starting S11 that is to be shifted around.
        for root in roots:
            if 'NOSHUNT' in root:
                starter_root = root
        roots = roots[roots != starter_root]
        starter_logmag_infile = starter_root.replace('_FILLER', '_LOGMAG')
        starter_phase_infile = starter_root.replace('_FILLER', '_PHASE')

        #Load intial data and make feed
        starter_freqs, starter_logmag_vals = ff.readerFieldFox(
            starter_logmag_infile, header=17)
        starter_freqs, starter_phase_vals = ff.readerFieldFox(
            starter_phase_infile, header=17)
        feed = Feed(starter_freqs,
                    starter_logmag_vals,
                    starter_phase_vals,
                    z_0=50)

        #L wiLL given as an "Effective Inductance" given by L/effective_inductor_factor.
        #effective_inductor_factor = 1.0 #We thought this should be 3 but testing with 1 makes things more accurate to measurements.
        measured_inductor_values_nH = numpy.zeros(len(roots))

        plt.figure()
        w = numpy.ones(len(feed.freqs))
        w[feed.freqs / 1e6 < 450] = 3
        w[numpy.logical_and(
            feed.freqs / 1e6 >= 450, feed.freqs / 1e6 < 550)] = numpy.linspace(
                1, 3,
                sum(
                    numpy.logical_and(feed.freqs / 1e6 >= 450,
                                      feed.freqs / 1e6 < 550)))[::-1]
        plt.plot(feed.freqs, w)
        plt.plot(feed.freqs,
                 4 - numpy.linspace(1, numpy.sqrt(3), len(feed.freqs))**2)
        plt.plot(feed.freqs,
                 4 - numpy.linspace(1, 3**(1 / 0.25), len(feed.freqs))**0.25)
        plt.plot(feed.freqs, 4 - numpy.linspace(1, 3, len(feed.freqs)))

        test_resistor_values = [0.0]  #[0.0,0.5/3,0.5,1.5]
        #for effective_inductor_factor in [1.0,3.0,4 - numpy.linspace(1,numpy.sqrt(3),len(feed.freqs))**2]:
        for effective_inductor_factor in [w]:
            for plot_cut_ll, plot_cut_ul in [[250, 500], [500, 850],
                                             [100, 850]]:
                for index, root in enumerate(roots):
                    logmag_infile = root.replace('_FILLER', '_LOGMAG')
                    phase_infile = root.replace('_FILLER', '_PHASE')
                    L = root.split('/')[-1].split('_')[2]
                    if L.lower() == 'noshunt':
                        L = 0
                    else:
                        L = float(L.lower().replace('nh', ''))
                    if L != 22:
                        continue
                    measured_inductor_values_nH[index] = L
                    freqs, logmag_vals = ff.readerFieldFox(logmag_infile,
                                                           header=17)
                    freqs, phase_vals = ff.readerFieldFox(phase_infile,
                                                          header=17)
                    measured_feed = Feed(freqs,
                                         logmag_vals,
                                         phase_vals,
                                         z_0=50)
                    #measured_feed.plotCurrentLogMagS11(label=root)

                    feed.reset()
                    compare_fig = plt.figure()
                    plt.suptitle('%0.1f to %0.1f MHz' %
                                 (plot_cut_ll, plot_cut_ul))
                    compare_ax1 = plt.subplot(2, 1, 1)

                    compare_ax1 = feed.plotCurrentLogMagS11(
                        ax=compare_ax1,
                        label='Measured: No Inductors',
                        plot_cut_ll=plot_cut_ll,
                        plot_cut_ul=plot_cut_ul
                    )  #Labelling assumes starter_ curves are for no shunt curve.
                    for added_series_resistance in test_resistor_values:
                        if added_series_resistance != 0:
                            feed.addRLC('sr', added_series_resistance)
                        #feed.addRLC('pl',(L/effective_inductor_factor)*1e-9)
                        feed.add22nHShuntInductor(
                            effective_inductor_factor=effective_inductor_factor
                        )
                        compare_ax1 = feed.plotCurrentLogMagS11(
                            ax=compare_ax1,
                            label=
                            'Predicted: %0.2f nH Inductors\nAdded %0.4f Ohm Series R'
                            % (L, added_series_resistance),
                            plot_cut_ll=plot_cut_ll,
                            plot_cut_ul=plot_cut_ul)
                        feed.reset()

                    compare_ax1 = measured_feed.plotCurrentLogMagS11(
                        ax=compare_ax1,
                        label='Measured: %0.2f nH Inductors' % L,
                        plot_cut_ll=plot_cut_ll,
                        plot_cut_ul=plot_cut_ul)
                    plt.ylabel('dB', fontsize=14)
                    plt.xlabel('MHz', fontsize=14)
                    plt.minorticks_on()
                    plt.grid(b=True, which='major', color='k', linestyle='-')
                    plt.grid(b=True,
                             which='minor',
                             color='tab:gray',
                             linestyle='--',
                             alpha=0.5)

                    feed.reset()
                    #compare_ax2 = plt.subplot(2,1,2)
                    compare_ax2 = get_smith(compare_fig,
                                            rect=212,
                                            plot_impedance=False,
                                            plot_ticks=True,
                                            plot_admittance=True,
                                            plot_labels=False)
                    plt.ylabel('Im($\\Gamma$)', fontsize=14)
                    plt.xlabel('Re($\\Gamma$)', fontsize=14)
                    compare_ax2 = feed.plotSmithChart(
                        ax=compare_ax2,
                        plot_cut_ll=plot_cut_ll,
                        plot_cut_ul=plot_cut_ul
                    )  #Labelling assumes starter_ curves are for no shunt curve.
                    for added_series_resistance in test_resistor_values:
                        if added_series_resistance != 0:
                            feed.addRLC('sr', added_series_resistance)
                        #feed.addRLC('pl',(L/effective_inductor_factor)*1e-9)
                        feed.add22nHShuntInductor(
                            effective_inductor_factor=effective_inductor_factor
                        )
                        compare_ax2 = feed.plotSmithChart(
                            ax=compare_ax2,
                            plot_cut_ll=plot_cut_ll,
                            plot_cut_ul=plot_cut_ul)
                        feed.reset()
                    compare_ax2 = measured_feed.plotSmithChart(
                        ax=compare_ax2,
                        plot_cut_ll=plot_cut_ll,
                        plot_cut_ul=plot_cut_ul)

    except Exception as e:
        print(e)
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
def makeGeneralPlots():
    '''
    This will make s11 expected v.s. measured plots in both logmag and smith chart.   As well as the general
    curve for expected points below threshold.  
    '''
    try:
        #datapath ='/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
        datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_al_tube_testing_sep2020/s11/'
        infiles = numpy.array(glob.glob(datapath + '*.csv'))
        roots = numpy.unique([
            infile.replace('_PHASE', '_FILLER').replace('_LOGMAG', '_FILLER')
            for infile in infiles
        ])

        #Prepare the starting S11 that is to be shifted around.
        for root in roots:
            if 'NOSHUNT' in root:
                starter_root = root
        roots = roots[roots != starter_root]
        starter_logmag_infile = starter_root.replace('_FILLER', '_LOGMAG')
        starter_phase_infile = starter_root.replace('_FILLER', '_PHASE')

        #Load intial data and make feed
        starter_freqs, starter_logmag_vals = ff.readerFieldFox(
            starter_logmag_infile, header=17)
        starter_freqs, starter_phase_vals = ff.readerFieldFox(
            starter_phase_infile, header=17)
        feed = Feed(starter_freqs,
                    starter_logmag_vals,
                    starter_phase_vals,
                    z_0=50)

        #inductor_values_nH = numpy.linspace(15,250,300)
        inductor_values_of_interest = numpy.array([])
        inductor_values_nH = numpy.linspace(
            5.0, 200.0, 500)  #numpy.array([47.0,56,100,110])
        test_statistic = numpy.zeros_like(inductor_values_nH)
        db_cut = -4  #dB

        #L wiLL given as an "Effective Inductance" given by L/effective_inductor_factor.
        effective_inductor_factor = 3.0  #We thought this should be 3 but testing with 1 makes things more accurate to measurements.

        for index, L in enumerate(inductor_values_nH):
            feed.addRLC('pl', (L / effective_inductor_factor) * 1.0e-9)
            test_statistic[index] = feed.getPercentBelow(db=db_cut)
            for roi in inductor_values_of_interest:
                if numpy.argmin(numpy.abs(roi - inductor_values_nH)) == index:
                    feed.plotCurrentLogMagS11(
                        label='L = %0.2f, (closest to requested %0.2f)' %
                        (L, roi))
            feed.reset()

        fig = plt.figure()
        curve_ax = plt.gca()
        plt.plot(inductor_values_nH,
                 test_statistic,
                 label='Predicted Curve from Varying No Shunt Feed')
        plt.xlabel('Individual Added Shunt Inductor Values (nH)')
        plt.ylabel('Fraction of S11 Points below %0.2f dB' % db_cut)
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-', alpha=0.75)
        plt.grid(b=True,
                 which='minor',
                 color='tab:gray',
                 linestyle='--',
                 alpha=0.25)
        feed.reset()
        plt.axhline(feed.getPercentBelow(db=db_cut),
                    c='r',
                    linestyle='--',
                    label='Measured No Shunt Fraction')

        measured_test_statistic = numpy.zeros(len(roots))
        measured_inductor_values_nH = numpy.zeros(len(roots))
        for index, root in enumerate(roots):
            logmag_infile = root.replace('_FILLER', '_LOGMAG')
            phase_infile = root.replace('_FILLER', '_PHASE')
            L = root.split('/')[-1].split('_')[2]
            if L.lower() == 'noshunt':
                L = 0
            else:
                L = float(L.lower().replace('nh', ''))
            measured_inductor_values_nH[index] = L
            freqs, logmag_vals = ff.readerFieldFox(logmag_infile, header=17)
            freqs, phase_vals = ff.readerFieldFox(phase_infile, header=17)
            measured_feed = Feed(freqs, logmag_vals, phase_vals, z_0=50)
            measured_test_statistic[index] = measured_feed.getPercentBelow(
                db=db_cut)
            #measured_feed.plotCurrentLogMagS11(label=root)

            feed.reset()
            compare_fig = plt.figure()
            compare_ax1 = plt.subplot(2, 1, 1)

            compare_ax1 = feed.plotCurrentLogMagS11(
                ax=compare_ax1, label='Measured: No Inductors'
            )  #Labelling assumes starter_ curves are for no shunt curve.
            feed.addRLC('pl', (L / effective_inductor_factor) * 1e-9)
            compare_ax1 = measured_feed.plotCurrentLogMagS11(
                ax=compare_ax1, label='Measured: %0.2f nH Inductors' % L)
            compare_ax1 = feed.plotCurrentLogMagS11(
                ax=compare_ax1, label='Predicted: %0.2f nH Inductors' % L)
            plt.ylabel('dB', fontsize=14)
            plt.xlabel('MHz', fontsize=14)
            plt.minorticks_on()
            plt.grid(b=True, which='major', color='k', linestyle='-')
            plt.grid(b=True,
                     which='minor',
                     color='tab:gray',
                     linestyle='--',
                     alpha=0.5)

            feed.reset()
            #compare_ax2 = plt.subplot(2,1,2)
            compare_ax2 = get_smith(compare_fig,
                                    rect=212,
                                    plot_impedance=False,
                                    plot_ticks=True,
                                    plot_admittance=True,
                                    plot_labels=False)
            plt.ylabel('Im($\\Gamma$)', fontsize=14)
            plt.xlabel('Re($\\Gamma$)', fontsize=14)
            compare_ax2 = feed.plotSmithChart(
                ax=compare_ax2
            )  #Labelling assumes starter_ curves are for no shunt curve.
            feed.addRLC('pl', (L / effective_inductor_factor) * 1e-9)
            compare_ax2 = measured_feed.plotSmithChart(ax=compare_ax2)
            compare_ax2 = feed.plotSmithChart(ax=compare_ax2)

        curve_ax.scatter(measured_inductor_values_nH,
                         measured_test_statistic,
                         color='r',
                         edgecolors='k',
                         label='Measured Feed Values')
        curve_ax.legend(loc='upper right')

    except Exception as e:
        rint('\nError in %s' % inspect.stack()[0][3])
        print(e)
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
def makeGeneralPlots3():
    '''
    This will make s11 expected v.s. measured plots in both logmag and smith chart.   As well as the general
    curve for expected points below threshold.  
    '''
    try:
        if False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_al_tube_testing_sep2020/s11/'
            starter_root_match = 'hpol_ernieemu_100nh_2p7pf'
        elif False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_ape_110nh_2p7pf'
        elif False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_bee_56nh_2p7pf'
        elif False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_bee_47nh_2p7pf'
        elif True:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_chuckape_27nh_2p7pf'
        elif False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_davebee_27nh_2p7pf'
        elif False:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_bee_22nh_2p7pf'
        else:
            datapath = '/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_copper_tab_testing_aug2020/s11/'
            starter_root_match = 'hpol_bee_NOSHUNT_2p7pF'

        #inductor_values_nH = numpy.linspace(15,250,300)
        inductor_values_of_interest = numpy.array([])
        inductor_values_nH = numpy.linspace(
            1.0, 150.0, 150)  #numpy.array([47.0,56,100,110])
        test_statistic = numpy.zeros_like(inductor_values_nH)

        cap_values_pF = numpy.linspace(1.0, 15.0, 300)
        cap_values_of_interest = numpy.array([])
        test_statistic_cap = numpy.zeros_like(cap_values_pF)

        db_cut = -4  #dB
        freq_low_cut_MHz = 100
        freq_high_cut_MHz = 800

        #Prep calculations
        starter_shunt_inductor_value = starter_root_match.split('/')[-1].split(
            '_')[2]  #nH
        if starter_shunt_inductor_value.lower() == 'noshunt':
            starter_shunt_inductor_value = 0
        else:
            starter_shunt_inductor_value = float(
                starter_shunt_inductor_value.lower().replace('nh',
                                                             '')) * 1e-9  #H
        starter_cap_value = float(
            starter_root_match.split('/')[-1].split('_')[3].lower().replace(
                'pf', '').replace('p', '.')) * 1e-12  #F

        mesh_L, mesh_C = numpy.meshgrid(inductor_values_nH, cap_values_pF)
        mesh_ts = numpy.zeros_like(mesh_L)

        #datapath ='/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_al_tube_testing_sep2020/s11/'
        infiles = numpy.array(glob.glob(datapath + '*.csv'))
        roots = numpy.unique([
            infile.replace('_PHASE', '_FILLER').replace('_LOGMAG', '_FILLER')
            for infile in infiles
        ])
        #Prepare the starting S11 that is to be shifted around.
        for root in roots:
            if starter_root_match in root:
                starter_root = root
        roots = roots[roots != starter_root]
        starter_logmag_infile = starter_root.replace('_FILLER', '_LOGMAG')
        starter_phase_infile = starter_root.replace('_FILLER', '_PHASE')

        #Load intial data and make feed
        starter_freqs, starter_logmag_vals = ff.readerFieldFox(
            starter_logmag_infile, header=17)
        starter_freqs, starter_phase_vals = ff.readerFieldFox(
            starter_phase_infile, header=17)
        feed = SmithMatcher(
            starter_freqs,
            starter_logmag_vals,
            starter_phase_vals,
            initial_capacitor_value=starter_cap_value,
            initial_shunt_inductor_value=starter_shunt_inductor_value,
            z_0=50)

        for c_index, C in enumerate(cap_values_pF):
            sys.stdout.write('(%i/%i)\t\t\t\r' %
                             (c_index + 1, len(cap_values_pF)))
            sys.stdout.flush()
            for l_index, L in enumerate(inductor_values_nH):
                feed.swapRLC('sc', C * 1.0e-12)
                feed.swapRLC('pl', L * 1.0e-9)
                mesh_ts[c_index][l_index] = feed.getPercentBelow(
                    db=db_cut,
                    freq_low_cut_MHz=freq_low_cut_MHz,
                    freq_high_cut_MHz=freq_high_cut_MHz)
                feed.reset()

        fig = plt.figure()
        mesh_ax = plt.gca()
        im = mesh_ax.pcolormesh(mesh_L,
                                mesh_C,
                                mesh_ts,
                                vmin=None,
                                vmax=None,
                                cmap=plt.cm.coolwarm)
        cbar = fig.colorbar(im)
        cbar.set_label('Fraction of S11 Points below %0.2f dB' % db_cut)
        plt.xlabel('Individual Swapped Shunt Inductor Values (nH)')
        plt.ylabel('Individual Swapped Series Cap Values (pF)')
        #plt.zlabel('Fraction of S11 Points below %0.2f dB'%db_cut)
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-', alpha=0.75)
        plt.grid(b=True,
                 which='minor',
                 color='tab:gray',
                 linestyle='--',
                 alpha=0.25)
        plt.axhline(2.7, linestyle='--', c='k')
        feed.reset()

        for index, C in enumerate(cap_values_pF):
            feed.swapRLC('sc', C * 1.0e-12)
            test_statistic_cap[index] = feed.getPercentBelow(
                db=db_cut,
                freq_low_cut_MHz=freq_low_cut_MHz,
                freq_high_cut_MHz=freq_high_cut_MHz)
            for roi in cap_values_of_interest:
                if numpy.argmin(numpy.abs(roi - cap_values_pF)) == index:
                    feed.plotCurrentLogMagS11(
                        label='C = %0.2f, (closest to requested %0.2f)' %
                        (C, roi))
            feed.reset()

        for index, L in enumerate(inductor_values_nH):
            feed.swapRLC('pl', L * 1.0e-9)
            test_statistic[index] = feed.getPercentBelow(
                db=db_cut,
                freq_low_cut_MHz=freq_low_cut_MHz,
                freq_high_cut_MHz=freq_high_cut_MHz)
            for roi in inductor_values_of_interest:
                if numpy.argmin(numpy.abs(roi - inductor_values_nH)) == index:
                    feed.plotCurrentLogMagS11(
                        label='L = %0.2f, (closest to requested %0.2f)' %
                        (L, roi))
            feed.reset()

        fig = plt.figure()
        curve_ax = plt.gca()
        plt.plot(
            cap_values_pF,
            test_statistic_cap,
            label='Predicted Curve from Varying %.2f pF %.2f nH Feed' %
            (starter_cap_value * 1e12, starter_shunt_inductor_value * 1e9))
        plt.xlabel('Individual Swapped Series Cap Values (pF)')
        plt.ylabel('Fraction of S11 Points below %0.2f dB' % db_cut)
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-', alpha=0.75)
        plt.grid(b=True,
                 which='minor',
                 color='tab:gray',
                 linestyle='--',
                 alpha=0.25)
        feed.reset()

        fig = plt.figure()
        curve_ax = plt.gca()
        plt.plot(inductor_values_nH,
                 test_statistic,
                 label='Predicted Curve from Varying %.2f nH Feed' %
                 (starter_shunt_inductor_value * 1e9))
        plt.xlabel('Individual Added Shunt Inductor Values (nH)')
        plt.ylabel('Fraction of S11 Points below %0.2f dB' % db_cut)
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-', alpha=0.75)
        plt.grid(b=True,
                 which='minor',
                 color='tab:gray',
                 linestyle='--',
                 alpha=0.25)
        feed.reset()
        plt.axhline(feed.getPercentBelow(db=db_cut),
                    c='r',
                    linestyle='--',
                    label='Measured %.2f nH Fraction' %
                    (starter_shunt_inductor_value * 1e9))

        measured_test_statistic = numpy.zeros(len(roots))
        measured_inductor_values_nH = numpy.zeros(len(roots))
        for index, root in enumerate(roots):
            logmag_infile = root.replace('_FILLER', '_LOGMAG')
            phase_infile = root.replace('_FILLER', '_PHASE')
            L = root.split('/')[-1].split('_')[2]  #nH
            if L.lower() == 'noshunt':
                L = 0
            else:
                L = float(L.lower().replace('nh', ''))
            C = float(
                root.split('/')[-1].split('_')[3].lower().replace(
                    'pf', '').replace('p', '.'))  #pF
            measured_inductor_values_nH[index] = L
            freqs, logmag_vals = ff.readerFieldFox(logmag_infile, header=17)
            freqs, phase_vals = ff.readerFieldFox(phase_infile, header=17)
            measured_feed = SmithMatcher(freqs,
                                         logmag_vals,
                                         phase_vals,
                                         initial_capacitor_value=C * 1e-12,
                                         initial_shunt_inductor_value=L * 1e-9,
                                         z_0=50)
            measured_test_statistic[index] = measured_feed.getPercentBelow(
                db=db_cut)
            #measured_feed.plotCurrentLogMagS11(label=root)

            feed.reset()
            compare_fig = plt.figure()
            compare_ax1 = plt.subplot(2, 1, 1)

            compare_ax1 = feed.plotCurrentLogMagS11(
                ax=compare_ax1,
                label='Measured: %0.2f nH Inductors' %
                (starter_shunt_inductor_value * 1e9)
            )  #Labelling assumes starter_ curves are for no shunt curve.
            feed.swapRLC('pl', L * 1e-9)
            compare_ax1 = measured_feed.plotCurrentLogMagS11(
                ax=compare_ax1, label='Measured: %0.2f nH Inductors' % L)
            compare_ax1 = feed.plotCurrentLogMagS11(
                ax=compare_ax1, label='Predicted: %0.2f nH Inductors' % L)
            plt.ylabel('dB', fontsize=14)
            plt.xlabel('MHz', fontsize=14)
            plt.minorticks_on()
            plt.grid(b=True, which='major', color='k', linestyle='-')
            plt.grid(b=True,
                     which='minor',
                     color='tab:gray',
                     linestyle='--',
                     alpha=0.5)

            feed.reset()
            #compare_ax2 = plt.subplot(2,1,2)
            compare_ax2 = get_smith(compare_fig,
                                    rect=212,
                                    plot_impedance=False,
                                    plot_ticks=True,
                                    plot_admittance=True,
                                    plot_labels=False)
            plt.ylabel('Im($\\Gamma$)', fontsize=14)
            plt.xlabel('Re($\\Gamma$)', fontsize=14)
            compare_ax2 = feed.plotSmithChart(
                ax=compare_ax2
            )  #Labelling assumes starter_ curves are for no shunt curve.
            feed.swapRLC('pl', L * 1e-9)
            compare_ax2 = measured_feed.plotSmithChart(ax=compare_ax2)
            compare_ax2 = feed.plotSmithChart(ax=compare_ax2)

        curve_ax.scatter(measured_inductor_values_nH,
                         measured_test_statistic,
                         color='r',
                         edgecolors='k',
                         label='Measured Feed Values')
        curve_ax.legend(loc='upper right')

    except Exception as e:
        print('\nError in %s' % inspect.stack()[0][3])
        print(e)
        exc_type, exc_obj, exc_tb = sys.exc_info()
        fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
        print(exc_type, fname, exc_tb.tb_lineno)
        starter_cap_value = float(starter_root_match.split('/')[-1].split('_')[3].lower().replace('pf','').replace('p','.'))*1e-12 #F

        #datapath ='/home/dsouthall/Projects/Greenland/hpol_prototype/data/hpol_al_tube_testing_sep2020/s11/'
        infiles = numpy.array(glob.glob(datapath + '*.csv'))
        roots = numpy.unique([infile.replace('_PHASE','_FILLER').replace('_LOGMAG','_FILLER') for infile in infiles])
        
        #Prepare the starting S11 that is to be shifted around.
        for root in roots:
            if starter_root_match in root:
                starter_root = root
        roots = roots[roots != starter_root]
        starter_logmag_infile = starter_root.replace('_FILLER','_LOGMAG')
        starter_phase_infile = starter_root.replace('_FILLER','_PHASE')

        #Load intial data and make feed
        starter_freqs, starter_logmag_vals = ff.readerFieldFox(starter_logmag_infile,header=17)
        starter_freqs, starter_phase_vals = ff.readerFieldFox(starter_phase_infile,header=17)
        feed = SmithMatcher(starter_freqs, starter_logmag_vals, starter_phase_vals, initial_capacitor_value=starter_cap_value, initial_shunt_inductor_value=starter_shunt_inductor_value, z_0=50, readout_transformer_factor=readout_transformer_factor)

        # feed.plotCurrentLogMagS11(label='Untransformed',plot_transformed=False)
        # feed.plotCurrentLogMagS11(label='transformed',plot_transformed=True)

        compare_fig = plt.figure()
        compare_ax1 = plt.subplot(2,1,1)

        compare_ax1 = feed.plotCurrentLogMagS11(ax=compare_ax1,label='Untransformed',plot_transformed=False) #Labelling assumes starter_ curves are for no shunt curve.
        compare_ax1 = feed.plotCurrentLogMagS11(ax=compare_ax1,label='transformed',plot_transformed=True)
        plt.ylabel('dB',fontsize=14)
        plt.xlabel('MHz',fontsize=14)
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-')
            antenna_name = root.split('/')[-1].split('_')[1]
            shunt_inductor_value = root.split('/')[-1].split('_')[2]  #nH
            if shunt_inductor_value.lower() == 'noshunt':
                shunt_inductor_value = 0
            else:
                shunt_inductor_value = float(
                    shunt_inductor_value.lower().replace('nh', '')) * 1e-9  #H
            cap_value = float(
                root.split('/')[-1].split('_')[3].lower().replace(
                    'pf', '').replace('p', '.')) * 1e-12  #F

            #Load data
            logmag_infile = root.replace('_FILLER', '_LOGMAG')
            phase_infile = root.replace('_FILLER', '_PHASE')

            freqs, logmag_vals = ff.readerFieldFox(logmag_infile, header=17)
            freqs, phase_vals = ff.readerFieldFox(phase_infile, header=17)

            #Make feed, incase I want to plot Smith plot later.
            feed = SmithMatcher(
                freqs,
                logmag_vals,
                phase_vals,
                initial_capacitor_value=cap_value,
                initial_shunt_inductor_value=shunt_inductor_value,
                z_0=50)

            all_ax_lm.plot(
                freqs / 1e6,
                logmag_vals,
                linestyle=linestyle,
def plotBoardS21(header=17,figsize=(16,9),dpi=108*4,outpath=None):
    '''
    This will plot the gain assuming that both ends of the s21 measurement are similar antennas.
    '''
    datapath = os.path.join(os.environ['BEACON_ANALYSIS_DIR'],'data','beacon2021_board_s21','boards')
    infiles = numpy.array(glob.glob(os.path.join(datapath , '*s21*logmag*.csv')))
    infiles = infiles[numpy.argsort([int(os.path.split(i)[-1].split('_')[2].replace('b','')) for i in infiles])]

    plot_cut_ul = 250            

    plt.rcParams['axes.prop_cycle'].by_key()['color']

    all_linestyles = numpy.array(list(lines.lineStyles.keys()))
    linestyles = numpy.array(['-', '--',':', '-.', ' ', ''], dtype='<U4')#all_linestyles[~numpy.isin(all_linestyles,['None'])]

    #PLOT PREPPING
    alpha = 0.8
    thickness = 4

    fig = plt.figure()
    primary_ax = plt.subplot(2,1,1)
    plt.ylabel('S21 (dB)')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    
    for infile_index, infile in enumerate(infiles):
        try:
            freqs_Hz, logmag_vals = ff.readerFieldFox(infile,header=header)
            if infile_index == 0:
                all_log_vals = logmag_vals
            else:
                all_log_vals = numpy.vstack((all_log_vals,logmag_vals))
            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            linestyle = '-'
            label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')

            primary_ax.plot(freqs_Hz[plot_cut]/1e6, logmag_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)
        except Exception as e:
            print(e)

    primary_ax.legend(loc = 'upper right')
    primary_ax.set_xlim([0,plot_cut_ul])
    primary_ax.set_ylim([10,50])

    ax = plt.subplot(2,1,2,sharex=primary_ax)
    plt.ylabel('S21 (degrees)')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    
    for infile_index, infile in enumerate(infiles):
        try:
            infile = infile.replace('logmag','phase')
            freqs_Hz, phase_vals = ff.readerFieldFox(infile,header=header)
            if infile_index == 0:
                all_phase_vals = phase_vals
            else:
                all_phase_vals = numpy.vstack((all_phase_vals,phase_vals))

            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            linestyle = '-'
            label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')

            ax.plot(freqs_Hz[plot_cut]/1e6, phase_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)
        except Exception as e:
            print(e)

    ax.legend(loc = 'upper right')
    ax.set_xlim([0,plot_cut_ul])

    if outpath is not None:
        fig.set_size_inches(figsize[0], figsize[1])
        plt.tight_layout()
        fig.savefig(os.path.join(outpath,'boards_s21.png'),dpi=dpi)


    #Residuals
    fig = plt.figure()
    ax = plt.subplot(2,1,1,sharex=primary_ax)
    plt.ylabel('S21 (dB)\nResiduals')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    
    mean_log_vals = numpy.mean(all_log_vals,axis=0)
    mean_phase_vals = numpy.mean(all_phase_vals,axis=0)

    for infile_index, infile in enumerate(infiles):
        try:
            freqs_Hz, logmag_vals = ff.readerFieldFox(infile,header=header)
            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            linestyle = '-'
            label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')

            ax.plot(freqs_Hz[plot_cut]/1e6, logmag_vals[plot_cut] - mean_log_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)
        except Exception as e:
            print(e)

    ax.legend(loc = 'upper right')
    ax.set_xlim([0,plot_cut_ul])

    ax = plt.subplot(2,1,2,sharex=primary_ax)
    plt.ylabel('S21 (degrees)\nResiduals')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    
    for infile_index, infile in enumerate(infiles):
        try:
            infile = infile.replace('logmag','phase')
            freqs_Hz, phase_vals = ff.readerFieldFox(infile,header=header)
            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            linestyle = '-'
            label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')

            ax.plot(freqs_Hz[plot_cut]/1e6, phase_vals[plot_cut] - mean_phase_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)
        except Exception as e:
            print(e)

    ax.legend(loc = 'upper right')
    ax.set_xlim([0,plot_cut_ul])

    if outpath is not None:
        fig.set_size_inches(figsize[0], figsize[1])
        plt.tight_layout()
        fig.savefig(os.path.join(outpath,'boards_s21_residuals.png'),dpi=dpi)
def plotS11(distance_m,header=17,figsize=(16,9),dpi=108*4,outpath=None):
    '''
    This will plot the gain assuming that both ends of the s21 measurement are similar antennas.
    '''
    datapath1 = os.path.join(os.environ['BEACON_ANALYSIS_DIR'],'data','beacon_s21_june6_2021')
    #infiles = numpy.append(numpy.array(glob.glob(os.path.join(datapath1 , '*s11*logmag*.csv'))),numpy.array(glob.glob(os.path.join(datapath1 , '*s22*logmag*.csv'))))
    infiles = numpy.array(glob.glob(os.path.join(datapath1 , '*s11*logmag*.csv')))
    
    if True:
        datapath2 = os.path.join(os.environ['BEACON_ANALYSIS_DIR'],'data','beacon_tx_s11_june7_2021')
        new_infiles = numpy.array(glob.glob(os.path.join(datapath2 , '*s11*logmag*.csv')))
        new_infiles = new_infiles[numpy.argsort([int(i.split('bowtie')[-1][0]) for i in new_infiles])]

        infiles = numpy.append(infiles, new_infiles)

    if True:
        datapath3 = os.path.join(os.environ['BEACON_ANALYSIS_DIR'],'data','beacon_s21_june11_2021')
        new_infiles = numpy.append(numpy.array(glob.glob(os.path.join(datapath3 , '*s11*logmag*.csv'))),numpy.array(glob.glob(os.path.join(datapath3 , '*s22*logmag*.csv'))))
        infiles = numpy.append(infiles, new_infiles)


    plt.rcParams['axes.prop_cycle'].by_key()['color']

    all_linestyles = numpy.array(list(lines.lineStyles.keys()))
    linestyles = numpy.array(['-', '--',':', '-.', ' ', ''], dtype='<U4')#all_linestyles[~numpy.isin(all_linestyles,['None'])]

    #PLOT PREPPING
    alpha = 0.8
    thickness = 4
    #S11 plot
    fig = plt.figure()
    ax = plt.subplot(1,1,1)
    plt.ylabel('dB')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    

    plt.title('Comparing BEACON Tx Antennas')
    for infile in infiles:

        label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')
        if 'tx1' in label:
            label = 'beacon s11 dipole 0'
        label = label.replace(' logmag','').replace('bowtie','bowtie ')

        try:
            freqs_Hz, logmag_vals = ff.readerFieldFox(infile,header=header)
            
            plot_cut_ul = 250            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            linestyle = '-'

            ax.plot(freqs_Hz[plot_cut]/1e6, logmag_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)


        except Exception as e:
            print(e)

    ax.legend()
    ax.set_xlim([0,plot_cut_ul])

    if outpath is not None:
        fig.set_size_inches(figsize[0], figsize[1])
        plt.tight_layout()
        fig.savefig(os.path.join(outpath,'all_s11.png'),dpi=dpi)


    #Highlighted S11 plot
    for index in range(len(infiles)):
        fig = plt.figure()
        ax = plt.subplot(1,1,1)
        plt.ylabel('dB')
        plt.xlabel('MHz')
        plt.minorticks_on()
        plt.grid(b=True, which='major', color='k', linestyle='-')
        plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
        
        plt.title('Comparing BEACON Tx Antennas')
        for i, infile in enumerate(infiles):
            if i == index:
                label = os.path.split(infile)[-1].replace('.csv','').replace('_', ' ')
                if 'tx1' in label:
                    label = 'beacon s11 dipole 0'
                label = label.replace(' logmag','').replace('bowtie','bowtie ')
            try:
                freqs_Hz, logmag_vals = ff.readerFieldFox(infile,header=header)
                
                plot_cut_ul = 250            
                plot_cut = freqs_Hz/1e6 < plot_cut_ul

                linestyle = '-'

                if i == index:
                    ax.plot(freqs_Hz[plot_cut]/1e6, logmag_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle, color = 'r')
                else:
                    ax.plot(freqs_Hz[plot_cut]/1e6, logmag_vals[plot_cut],linewidth=thickness/2,alpha=alpha*.66,linestyle=linestyle, color = 'k')


            except Exception as e:
                print(e)

        ax.legend()
        ax.set_xlim([0,plot_cut_ul])
        if outpath is not None:
            fig.set_size_inches(figsize[0], figsize[1])
            plt.tight_layout()
            fig.savefig(os.path.join(outpath,label.replace(' ','_') + '.png'),dpi=dpi)
def plotS21LikeUnlike(distance_m,datapath,header=17,figsize=(16,9),dpi=108*4,outpath=None):
    '''
    This will plot the gain assuming that both ends of the s21 measurement are similar antennas.
    '''
    infiles_tx = numpy.array(glob.glob(os.path.join(datapath , '*txtx*s21*logmag*.csv')))
    infiles_rx = numpy.array(glob.glob(os.path.join(datapath , '*rxtx*s21*logmag*.csv')))
    try:
        infiles_rx = infiles_rx[numpy.argsort([int(os.path.split(i)[-1].split('_')[3].replace('ant','')) for i in infiles_rx])]
    except Exception as e:
        print(e)
        



    plt.rcParams['axes.prop_cycle'].by_key()['color']

    all_linestyles = numpy.array(list(lines.lineStyles.keys()))
    linestyles = numpy.array(['-', '--',':', '-.', ' ', ''], dtype='<U4')#all_linestyles[~numpy.isin(all_linestyles,['None'])]

    #PLOT PREPPING
    alpha = 0.8
    thickness = 4
    #PLOT Gain
    fig = plt.figure()
    ax = plt.subplot(1,1,1)
    plt.title('S21')
    plt.ylabel('dB')
    plt.xlabel('MHz')
    plt.minorticks_on()
    plt.grid(b=True, which='major', color='k', linestyle='-')
    plt.grid(b=True, which='minor', color='tab:gray', linestyle='--',alpha=0.5)
    
    infile_tx = infiles_tx[0]
    label = ''

    for infile_index, infile_rx in enumerate(infiles_rx):
        try:
            

            linestyle = '-'

            if infile_index == 0:
                freqs_Hz, s21_tx_logmag_vals = ff.readerFieldFox(infile_tx,header=header)
                plot_cut_ul = 250            
                plot_cut = freqs_Hz/1e6 < plot_cut_ul
                label = os.path.split(infile_tx)[-1].replace('.csv','').replace('_', ' ').replace(' logmag', '').replace('beacon ','').replace(' s21','').replace('txtx','tx -> tx')
                ax.plot(freqs_Hz[plot_cut]/1e6, s21_tx_logmag_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)

            freqs_Hz, s21_rx_logmag_vals = ff.readerFieldFox(infile_rx,header=header)
            plot_cut_ul = 250            
            plot_cut = freqs_Hz/1e6 < plot_cut_ul

            label = os.path.split(infile_rx)[-1].replace('.csv','').replace('_', ' ').replace(' logmag', '').replace('beacon ','').replace(' s21','').replace('rxtx','tx -> rx')
            ax.plot(freqs_Hz[plot_cut]/1e6, s21_rx_logmag_vals[plot_cut],linewidth=thickness,label=label,alpha=alpha,linestyle=linestyle)


        except Exception as e:
            print(e)

    ax.legend()
    ax.set_xlim([0,plot_cut_ul])

    if outpath is not None:
        fig.set_size_inches(figsize[0], figsize[1])
        plt.tight_layout()
        fig.savefig(os.path.join(outpath,'s21_likelike.png'),dpi=dpi)