예제 #1
0
def quicklook():

    balun_loss = hkl.load('cal_data/balun_loss.hkl')
    vna_cal = hkl.load('cal_data/vna_calibration.hkl')

    ant_ids = ['a252x']  #, 'a254x', 'a255x']
    fig = plt.figure()
    for ant_id in ant_ids:
        ra = vna_cal[ant_id]["ra"]
        rl = vna_cal[ant_id]["rl"]
        f = vna_cal["f"]
        F = compute_F(ra, rl)
        G = compute_G(rl)
        plt.subplot(3, 1, 1)
        plt.plot(f, (1 - mag2(ra)), c='#002147', label='$H_{\\rm{ant}}$')
        plt.yticks([0.5, 0.6, 0.7, 0.8])
        plt.ylim(0.5, 0.8)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   ["", "", "", "", "", "", "", "", "", ""])
        plt.subplot(3, 1, 3)
        plt.plot(f, G, c='#002147', label='$H_{\\rm{lna}}$')
        plt.yticks([0.997, 0.998, 0.999, 1.000])
        plt.ylim(0.997, 1.000)
        plt.subplot(3, 1, 2)
        plt.plot(f, mag2(F), c='#002147', label='$|F|^2$')
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   ["", "", "", "", "", "", "", "", "", ""])

    for ii in (1, 2, 3):
        plt.subplot(3, 1, ii)
        plt.xlim(40, 85)
        plt.rcParams["legend.fontsize"] = 12
        plt.legend(loc=4, frameon=False)
    #plt.legend(["$|F|^2$"], frameon=False, loc=4)

    plt.xlabel("Frequency [MHz]")
    plt.tight_layout()
    #plt.legend(["$G$"], frameon=False, loc=4)
    #plt.text(40, 0.999, "$G$")
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()
예제 #2
0
def quicklook(filename, save, noshow, t):
    h5 = tb.open_file(filename)

    if t >= len(h5.root.data.cols.ant252_x):
        raise RuntimeError("Time step doesn't exist")

    (bottom, top) = mid_range(h5, t)

    fig, ax = plt.subplots(figsize=(8, 6))

    plt.subplot(2, 1, 1)
    plt.plot(h5.root.data.cols.ant252_x[t], label="252A")
    plt.plot(h5.root.data.cols.ant254_x[t], label="254A")
    plt.plot(h5.root.data.cols.ant255_x[t], label="255A")
    plt.ylim(bottom, top)
    plt.legend(frameon=False)
    plt.ylabel("Data")
    plt.minorticks_on()

    plt.subplot(2, 1, 2)
    plt.plot(0, 0)
    plt.plot(h5.root.data.cols.ant252_y[t], label="252A")
    plt.plot(h5.root.data.cols.ant254_y[t], label="254B")
    plt.plot(h5.root.data.cols.ant255_y[t], label="255B")
    plt.ylim(bottom, top)
    plt.legend(frameon=False)

    plt.minorticks_on()

    plt.xlabel("Channel")

    plt.legend(frameon=False)
    plt.tight_layout()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)

    if save:
        plt.savefig("raw_" + os.path.basename(filename)[:-3] + ".png")
    if not noshow:
        plt.show()
예제 #3
0
def quicklook(filename):

    balun_loss = hkl.load('cal_data/balun_loss.hkl')
    vna_cal = hkl.load('cal_data/vna_calibration.hkl')

    ant_id = 'a252x'
    ra = vna_cal[ant_id]["ra"]
    rl = vna_cal[ant_id]["rl"]
    f = vna_cal["f"]
    F0 = compute_F(ra, rl)
    G = compute_G(rl)

    T_noise = compute_noisewave('a252x')

    fig = plt.figure()
    plt.plot(f, T_noise)
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()
예제 #4
0
  a255y_s11 = s2p_interp(f_leda, 'balun.18may/fialkov.de-embed.baluns.Using.Lab.Measued.Cables.18aug01/Zres_255B.s2p',
           'lna.s11.18may/leda.lna.s11.cable.de-embedded_252A.s2p')

  a256y_s11 = s2p_interp(f_leda, 'balun.18may/fialkov.de-embed.baluns.Using.Lab.Measued.Cables.18aug01/Zres_256B.s2p',
           'lna.s11.18may/leda.lna.s11.cable.de-embedded_252A.s2p')


ant_s11_dict = {
    'f'           : f_leda,
    'a252x'       : {'ra': a252x_s11[0], 'rl': a252x_s11[1]},
    'a252y'       : {'ra': a252y_s11[0], 'rl': a252y_s11[1]},
    'a254x'       : {'ra': a254x_s11[0], 'rl': a254x_s11[1]},
    'a254y'       : {'ra': a254y_s11[0], 'rl': a254y_s11[1]},
    'a255x'       : {'ra': a255x_s11[0], 'rl': a255x_s11[1]},
    'a255y'       : {'ra': a255y_s11[0], 'rl': a255y_s11[1]},
    'fingerprint' : get_repo_fingerprint()
    }

hkl.dump(ant_s11_dict, 'cal_data/vna_calibration.hkl')

exit()    # Need scikit-rf module to run the rest
###########
## BALUN
###########
import skrf as rf

balun = rf.Network()
balun.read_touchstone('component-data/ADT4-6T___Plus25degC.S3P')
ff  = balun.frequency.f / 1e6
s12 = balun.s12.s_db[:, 0,0]
s13 = balun.s13.s_db[:, 0,0]
예제 #5
0
    if len(lst_ind) > 0:
        bin_ok = True
    else:
        bin_ok = False
    for i in lst_ind[1:]:
        if d["lsts"][i] - d["lsts"][i - 1] > 1 / 60.0:  # 1 minute
            print "Discontinuity at LST", d["lsts"][i], (
                d["lsts"][i] - d["lsts"][i - 1]) * 60 * 60, "seconds"
            bin_ok = False
    use_bins.append(bin_ok)

Time_bins = []
bp_window_t = 8
av_data_dict = {}
av_data_dict["fingerprint"] = get_repo_fingerprint()
av_data_dict["Bins"] = [{} for i in range(len(vecTbins) - 1)]
av_data_dict["Freqs"] = scrunch(d["frequencies"])
fig = plt.figure()

flagsp = 1
for indD in range(len(dipoles)):
    ant = sys.argv[2] + dipoles[indD]  #"255A"

    #print "file", sys.argv[1][0:len(sys.argv[1])-4]
    #tlst = d["lsts"]# time in hours

    #print "Length Freq",len(d["frequencies"])
    #print "D", d[ant][1000][100]
    #print "min T", min(d["lsts"])
    #print "max T", max(d["lsts"])
예제 #6
0
def quicklook(filename, flag, ant='252A', freq=60):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    lst = T_ant['lst']

    print T_ant.keys()

    ant_ids = [
        ant,
    ]

    print("Plotting %s..." % ant_ids[0])
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = closest(lst, 11)
    print T_ant['lst'][mid]
    sl = 20

    if flag:
        T_flagged = rfi_flag(T_ant[ant_ids[0]], freqs=f_leda)
    else:
        T_flagged = T_ant[ant_ids[0]]

    plt.title(ant_ids[0])
    plt.subplot(2, 1, 1)
    plt.plot(f_leda,
             T_flagged[mid - sl:mid + sl].mean(axis=0),
             label='LST 11:00',
             c='#333333')
    plt.axvline(x=freq, ls='dashed', c='#888888')
    plt.xlim(30, 90)
    plt.minorticks_on()
    plt.ylim(500, 7000)

    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())

    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Temperature [K]")
    plt.legend(frameon=False)

    plt.subplot(2, 1, 2)
    sl = 10
    #mid = closest(f_leda, 40)
    #plt.plot(lst, T_flagged[:, mid-sl:mid+sl].mean(axis=1), label='40 MHz')
    mid = closest(f_leda, freq)
    plt.plot(lst,
             T_flagged[:, mid - sl:mid + sl].mean(axis=1),
             label='%.0f MHz' % freq,
             c='#333333')
    plt.axvline(x=11, ls='dashed', c='#888888')
    #mid = closest(f_leda, 80)
    #plt.plot(lst, T_flagged[:, mid-sl:mid+sl].mean(axis=1), label='80 MHz')

    #plt.xlim(30, 90)
    plt.xlabel("Sidereal time [hr]")
    plt.ylabel("Temperature [K]")
    plt.minorticks_on()
    plt.xlim(0, 24)
    plt.xticks([0, 4, 8, 12, 16, 20])

    ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
    ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())

    plt.legend(frameon=False, loc=2)
    plt.tight_layout()

    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)

    if ant_ids[0] == '252A':
        plt.savefig("figures/a252-cuts.pdf")
    plt.show()
    sl = 250
    plt.plot(
        f_leda,
        T_flagged[mid - sl:mid + sl].mean(axis=0) - T_flagged.mean(axis=0))
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()
예제 #7
0
def quicklook(filename, flatten, ant='252A'):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']

    ant_ids = [
        ant,
    ]

    print("Plotting %s..." % ant_ids[0])
    fig, axes = plt.subplots(figsize=(12, 6), nrows=1, ncols=1)
    #plt.suptitle(h5.filename)

    lst_stamps = T_ant['lst']
    utc_stamps = T_ant['utc']
    xlims = (f_leda[0], f_leda[-1])
    #ylims = mdates.date2num((T_ant['utc'][0], T_ant['utc'][-1]))
    #hfmt = mdates.DateFormatter('%m/%d %H:%M')
    ylims = (T_ant['lst'][0], T_ant['lst'][-1])
    T_flagged = T_ant[ant_ids[0]]
    #T_flagged = np.fft.fft(T_flagged, axis=0)
    #T_flagged -= T_flagged.mean(axis=0)
    #T_flagged = 10*np.log10(np.abs(np.fft.ifft(T_flagged)))

    T_flagged = rfi_flag(T_flagged, freqs=f_leda)

    if flatten:
        abp = np.ma.median(T_flagged.data, axis=0)
        abp /= np.ma.median(abp)
        T_flagged /= abp
    clim = (percentile(T_flagged.compressed(),
                       5), percentile(T_flagged.compressed(), 95))

    im = plt.imshow(
        T_flagged,  # / np.median(xx, axis=0), 
        cmap='jet',
        aspect='auto',
        interpolation='nearest',
        clim=clim,
        extent=(xlims[0], xlims[1], ylims[1], ylims[0]))
    plt.title(ant_ids[0])
    plt.xlabel("Frequency [MHz]")

    plt.ylabel("LST [hr]")
    plt.colorbar()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.savefig("figures/rfi-flagged.pdf")
    plt.show()

    plt.figure()
    #plt.plot(f_leda, np.sum(T_flagged.mask, axis=0).astype('float') / T_flagged.mask.shape[0], label='total')
    day = T_flagged[0:2000].mask
    night = T_flagged[2250:2750].mask
    plt.plot(f_leda,
             np.sum(night, axis=0).astype('float') / night.shape[0],
             label='night')
    plt.plot([0])
    plt.plot(f_leda,
             np.sum(day, axis=0).astype('float') / day.shape[0],
             label='day')
    plt.xlim(40, 85)
    plt.ylim(-0.025, 0.25)

    plt.title(ant_ids[0])
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Flagged fraction")
    plt.minorticks_on()
    plt.legend(frameon=True, loc=2)
    plt.tight_layout()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.savefig("figures/rfi-fraction.pdf")
    plt.show()

    plt.figure()
    plt.plot(f_leda, kurtosis(T_flagged, axis=0))
    plt.title(ant_ids[0])
    plt.ylabel("Kurtosis")
    plt.xlabel("Frequency [MHz]")
    plt.xlim(40, 85)
    plt.ylim(-50, 1600)
    plt.minorticks_on()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()

    plt.figure()
예제 #8
0
def quicklook(filename, save, flag, noshow):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    
    ant_ids = ['252A', '254A', '255A', '252B', '254B', '255B'] # Added 252B, why was it missing? HG
    mmax = 0
    for ant in ant_ids:
      if flag:
        T_flagged = rfi_flag(T_ant[ant], freqs=f_leda)
        T_ant[ant] = np.ma.filled(T_flagged, 0)

      if flag: tmax = np.max(T_ant[ant])
      else: tmax = np.percentile(T_ant[ant], 99.95)
      if tmax  > mmax: mmax = tmax  

    print("Plotting...")
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = T_ant["252A"].shape[0]/2    # Currently plots the middle bit of the observation
    print T_ant['lst'][mid]           # Print the LST about which we are averaging
    sl  = 50                          # The number of integrations to average either side
    
    print ant_ids[0], mid, sl
    print ant_ids[1], mid, sl
    print ant_ids[2], mid, sl
    
    plt.subplot(2,1,1)
    plt.plot(f_leda, np.max(T_ant[ant_ids[0]], axis=0), label=ant_ids[0])
    plt.plot(f_leda, np.max(T_ant[ant_ids[1]], axis=0), label=ant_ids[1])
    plt.plot(f_leda, np.max(T_ant[ant_ids[2]], axis=0), label=ant_ids[2])
    plt.legend(frameon=False)
    plt.ylabel("Temperature [K]")
    plt.xlim(30, 85)
    plt.minorticks_on()
    plt.ylim(500, mmax)
    
    plt.subplot(2,1,2)
    plt.plot(0, 0)
    plt.plot(f_leda, np.max(T_ant[ant_ids[3]], axis=0), label=ant_ids[3])
    plt.plot(f_leda, np.max(T_ant[ant_ids[4]], axis=0), label=ant_ids[4])
    plt.plot(f_leda, np.max(T_ant[ant_ids[4]], axis=0), label=ant_ids[5])

    plt.legend(frameon=False)
    
    plt.xlim(30, 85)
    plt.minorticks_on()
    plt.ylim(500, mmax)
    
    
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Temperature [K]")
    
    plt.legend(frameon=False)
    plt.tight_layout()
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.savefig("figures/compare-spectra.pdf")
    
    if save:
        if flag:
            plt.savefig("peaks_"+os.path.basename(filename)[:-3]+"_"+"flagged.png")
        else:
            plt.savefig("peaks_"+os.path.basename(filename)[:-3]+".png")
    if not noshow:
        plt.show()
예제 #9
0
def quicklook(filename):
    
    n = 1
    fs, fe = 40, 83
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    lst_leda = T_ant["lst"]

    
    print T_ant.keys()
    
    ant_ids = ['252A']#, '254A', '255A', '254B', '255B']
      
    print("Plotting...")
    T0_all = []
    alpha_all = []
    lst_trials = np.arange(1, 23, 0.5)
    
    for ll in lst_trials:
    
        mid = closest(lst_leda, ll)
        sl  = 100
    
        
        for ant_id in ant_ids:
            T_flagged = T_ant[ant_id][mid-sl:mid+sl]
            print T_ant[ant_id].shape
            T_flagged = rfi_flag(T_flagged, freqs=f_leda)
            #T_flagged = np.ma.array(T_flagged)
    
            d = T_flagged.mean(axis=0)
            d_std = T_flagged.std(axis=0)

            print d.shape, f_leda.shape
        
            print ant_id, mid, sl

            f_t, d_t = trim(f_leda, d, fs, fe)
            f_t, d_std_t = trim(f_leda, d_std, fs, fe)
            f_t = np.ma.array(f_t)
            f_t.mask = d_t.mask
    
            params = Parameters()
            params.add('a0', value=1e6)
            params.add('b', value=-2.49)

            fc = f_t.compressed()
            dc = d_t.compressed()
            dc_errs = d_std_t.compressed()
        
            out = minimize(residual, params, args=(model, fc, dc, dc_errs))
    
            print report_fit(out)
            alpha_all.append(out.params['b'].value)
            T0_all.append(out.params['a0'].value)
            
            print "B: ", out.params['b'].value
    
            # print out.values
            #p = fit_poly(f_leda, d, n=n, print_fit=True)
    
            p = model(out.params, fc)
    
            print p.shape
            plt.figure("ANTZ")
            plt.plot(fc, dc, label=ant_id)
            #plt.plot(fc, p, label='fit')
    
            plt.xlim(fs, fe)
    plt.minorticks_on()
    plt.ylim(500, 7000)
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Temperature [K]")
    plt.legend(frameon=False)
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.show()
    

    alpha = np.array(alpha_all)
    print "mean std max min"
    print np.mean(alpha), np.std(alpha), np.max(alpha), np.min(alpha)
    plt.plot(lst_trials, alpha, c='#333333')
    plt.xlabel("Sidereal time [hr]")
    
    plt.ylabel("Spectral index $\\alpha$")
    plt.minorticks_on()
    plt.ylim(-2.4, -2.27)
    plt.twinx()
    plt.plot(lst_trials, T0_all, c='#cc0000', ls='dashed')
    plt.ylabel("$T_{70}$ [K]", color='#cc0000')
    plt.tight_layout()
    plt.minorticks_on()
    plt.xlim(0, 24)
    plt.ylim(1500, 3300)
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.savefig("figures/spectral-index.pdf")
    plt.show()
def quicklook(filename, ant='252A', lfsm=False, emp=False, n_poly=7):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    lst    = T_ant['lst']
    
    print T_ant.keys()
    
    ant_ids = [ant,]
      
    print("Plotting %s..." % ant_ids[0])
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = closest(lst, 11)
    print T_ant['lst'][mid]
    sl  = 20
    
    T_flagged = rfi_flag(T_ant[ant_ids[0]], freqs=f_leda)
    
    sl  = 250
    
    FA, FZ = 50, 85
    
    
    #plt.imshow(T_flagged, aspect='auto', cmap='viridis')
    #plt.show()
    
    #plt.subplot2grid((3, 1), (0, 0), rowspan=2) 
    #plt.plot(f_leda, T_flagged[mid-sl:mid+sl].mean(axis=0), c='#009933')
    
    
    import hickle as hkl
    gsm = hkl.load("cal_data/gsm-spec-lst11.hkl")
    #plt.plot(gsm["f"], gsm["T_hsm"], c='#333333', ls='dashed')
    
    if lfsm and emp:
        smdl = SkyModelLFSMEmp
        smlbl = 'LFSM+Emp'
    elif lfsm and not emp:
        smdl = SkyModelLFSM
        smlbl = 'LFSM'
    elif not lfsm and emp:
        smdl = SkyModelGSMEmp
        smlbl = 'GSM+Emp'
    else:
        smdl = SkyModelGSM        
        smlbl = 'GSM'
    s = smdl(pol='y' if ant_ids[0][-1] == 'A' else 'x')
    asm = s.generate_tsky(lst[mid-sl:mid+sl], f_leda*1e6).mean(axis=0)
    
    d = T_flagged[mid-sl:mid+sl].mean(axis=0)
    
    d_flags = T_flagged.mask[mid-sl:mid+sl].sum(axis=0)
    
    d_errs  = T_flagged[mid-sl:mid+sl].std(axis=0) / np.sqrt(d_flags)
    
    #plt.figure("FLGS")
    #plt.plot(d_flags)
    #plt.show()
    
    f_t, d_t = trim(f_leda, d, FA, FZ)
    f_t, d_errs_t = trim(f_leda, d_errs, FA, FZ)
    if ant_ids[0][-1] == 'A':
        T_hsm = np.interp(f_t, gsm["f"], gsm["T_ew"])
    else:
        T_hsm = np.interp(f_t, gsm["f"], gsm["T_ns"])
    T_asm = np.interp(f_t, f_leda, asm)
    
    scale_offset = np.mean(T_hsm / d_t)
    scale_offset_asm = np.mean(T_asm / d_t)
    print scale_offset, scale_offset_asm
    
    resid0 = scale_offset * d_t - T_hsm
    resid0_asm = scale_offset_asm * d_t - T_asm
    
    print resid0, resid0_asm
    
    model = poly_fit(f_t, resid0, n_poly, log=False)
    model_asm = poly_fit(f_t, resid0_asm, n_poly, log=False)
    
    n_chan = 42
    A, B, C = rebin(f_t, n_chan), rebin(resid0, n_chan), rebin(d_errs_t, n_chan)
    print A.shape, B.shape, C.shape
    
    #print d.shape, d_flags.shape, d_errs.shape
    x = np.column_stack((A, B, C))
    np.savetxt("data_resids_gianni.txt", x)
    fh = open("data_resids_gianni.txt", "a")
    fh.write("# %s\n" % get_repo_fingerprint())
    fh.close()
    
    fig = plt.figure("AAA @ %i terms" % n_poly)
    #plt.plot(f_t, resid0)
    #plt.plot(f_t, model)
    plt.plot(rebin(f_t, n_chan), rebin(resid0-model, n_chan), linestyle='--')
    plt.plot(rebin(f_t, n_chan), rebin(resid0_asm-model, n_chan))
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    
    fig = plt.figure("BBB @ %i terms" % n_poly)
    plt.plot(scale_offset * d_t - T_hsm, linestyle='--')
    plt.plot(scale_offset_asm * d_t - T_asm)
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.show()
    
    
    #plt.plot(f_)
    plt.xlim(FA, FZ)
    plt.ylim(500, 2000)
    plt.minorticks_on()
    plt.ylabel("Temperature [K]")
    plt.legend([ant_ids[0], "GSM (.hkl)", smlbl])
    
    plt.subplot2grid((3, 1), (2, 0), rowspan=1)
    plt.plot(rebin(f_t, n_chan), rebin(d_t / T_hsm, n_chan), c='#333333', linestyle='--')
    plt.plot(rebin(f_t, n_chan), rebin(scale_offset * d_t / T_hsm, n_chan), c='#333333', linestyle='--')
    plt.plot(rebin(f_t, n_chan), rebin(d_t / T_asm, n_chan), c='#333333')
    plt.plot(rebin(f_t, n_chan), rebin(scale_offset_asm * d_t / T_asm, n_chan), c='#333333')
    plt.xlabel("Frequency [MHz]")
    #plt.yticks([0.85, 0.87, 0.89, 0.91, 0.93])
    #  plt.ylim(0.85, 0.93)
    plt.ylabel("data / model")
    plt.tight_layout()
    plt.minorticks_on()
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.savefig("figures/skymodel-compare.pdf")
    plt.show()
    
    resid = d_t - T_hsm
    resid -= fit_poly(f_t, resid, n_poly)
    resid_asm = d_t - T_asm
    resid_asm -= fit_poly(f_t, resid_asm, n_poly)
    
    plt.plot(f_t, resid, linestyle='--')
    plt.plot(f_t, resid_asm)
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.show()
예제 #11
0
def quicklook(filename,
              save,
              dump,
              flag,
              merge,
              flatten,
              no_show,
              all_lsts,
              new_cal,
              sky=False,
              lfsm=False,
              emp=False):
    h5 = tb.open_file(filename)

    if new_cal: T_ant = apply_new_calibration(h5)
    else: T_ant = apply_calibration(h5)
    f_leda = T_ant['f']

    ant_ids = ['252', '254', '255']

    print("Plotting...")
    fig = plt.figure(figsize=(20, 20))
    #plt.suptitle(h5.filename)

    lst_stamps = T_ant['lst']
    indexes = np.arange(len(lst_stamps), dtype=np.int)

    if len(lst_stamps) == 0:
        raise RuntimeError("No LSTs in file")

    # Report discontinuities in time
    for i in range(1, len(lst_stamps)):
        if lst_stamps[i] - lst_stamps[i - 1] > 1 / 60.0:  # 1 minute
            print "Discontinuity at LST", lst_stamps[i], (
                lst_stamps[i] - lst_stamps[i - 1]) * 60 * 60, "seconds"

    utc_stamps = T_ant['utc']
    xlims = (f_leda[0], f_leda[-1])
    #ylims = mdates.date2num((T_ant['utc'][0], T_ant['utc'][-1]))
    #hfmt = mdates.DateFormatter('%m/%d %H:%M')
    ylims = (T_ant['lst'][0], T_ant['lst'][-1])

    # Work out altitude of Gal center and Sun. Use whichever is highest
    # and put that in the padding, which is the stripe.
    pad_length = 70
    padding = np.full((len(lst_stamps), pad_length), 10000)
    timing = lst_timing.LST_Timing(lst_stamps, utc_stamps)
    border_bottom, night_bottom, night_top, border_top = timing.calc_night()
    padding[night_bottom:night_top, :] = 1000

    #for ant in ant_ids:
    #  lst_stamps, T_ant[ant+"A"] = timing.align(T_ant[ant+"A"])
    #  lst_stamps, T_ant[ant+"B"] = timing.align(T_ant[ant+"B"])

    if night_bottom:
        print "Night", lst_stamps[night_bottom], "-", lst_stamps[night_top - 1]
    else:
        print "Night 0 - 0"

    # Use night only
    if not all_lsts:
        if not border_top:
            raise RuntimeError(
                "No LSTs available at night time (use --all_lsts to see all)")
        lst_stamps = lst_stamps[night_bottom:night_top]
        utc_stamps = utc_stamps[night_bottom:night_top]
        indexes = indexes[night_bottom:night_top]
        padding = padding[night_bottom:night_top]
        ylims = (lst_stamps[0], lst_stamps[-1])
        print len(lst_stamps), "usable LSTs"
    else:
        print "Using all LSTs"

    if len(lst_stamps) == 0:
        raise RuntimeError(
            "There are no data to display (number of LSTs is 0)")

    yloc = []
    ylabel = []
    try:
        for i in range(0, len(lst_stamps), len(lst_stamps) / 7):
            yloc.append(lst_stamps[i]), ylabel.append(("%.1f" % lst_stamps[i]))
    except:
        yloc.append(lst_stamps[0]), ylabel.append(("%.1f" % lst_stamps[0]))
        yloc.append(lst_stamps[-1]), ylabel.append(("%.1f" % lst_stamps[-1]))
    if all_lsts:
        new_x_high = xlims[1] + pad_length * (xlims[1] -
                                              xlims[0]) / len(f_leda)
    else:
        new_x_high = xlims[1]

    dump_data = {}

    if sky:
        if lfsm and emp:
            smdl = SkyModelLFSMEmp
            smlbl = 'LFSM+Emp'
        elif lfsm and not emp:
            smdl = SkyModelLFSM
            smlbl = 'LFSM'
        elif not lfsm and emp:
            smdl = SkyModelGSMEmp
            smlbl = 'GSM+Emp'
        else:
            smdl = SkyModelGSM
            smlbl = 'GSM'
        sy = smdl(pol='y')
        sx = smdl(pol='x')
        T_y_asm = sy.generate_tsky(lst_stamps, f_leda * 1e6)
        T_x_asm = sx.generate_tsky(lst_stamps, f_leda * 1e6)

    if flag and merge:
        # If we are going to merge the flags across antennas, we need to flag them all now
        for p in (0, 1):
            for ii, key in enumerate(ant_ids):
                ant = key + ("B" if p else "A")
                T_flagged = T_ant[ant]
                if not all_lsts:
                    # Do flagging with a border around the data in time
                    masks = rfi_flag(T_flagged[border_bottom:border_top],
                                     freqs=f_leda)
                    new_mask = masks.combine(do_not_excise_dtv=True)

                    new_mask = new_mask[night_bottom -
                                        border_bottom:night_top -
                                        border_bottom]  # remove border
                else:
                    masks = rfi_flag(T_flagged, freqs=f_leda)
                    new_mask = masks.combine(do_not_excise_dtv=True)

                    print ant, "Biggest DTV gap", lst_stamps[biggest_gap(
                        masks.dtv_tms)[1]], "-", lst_stamps[biggest_gap(
                            masks.dtv_tms)[0]], "waterfall"
                try:
                    merged_mask |= new_mask
                except NameError:
                    merged_mask = new_mask

    for p in [0, 1]:
        for ii, key in enumerate(ant_ids):
            if p == 0 and ii == 0:
                ax = fig.add_subplot(2, 3, 3 * p + ii + 1)
                origAX = ax
            else:
                ax = fig.add_subplot(2,
                                     3,
                                     3 * p + ii + 1,
                                     sharex=origAX,
                                     sharey=origAX)

            if p == 0:
                ant = key + "A"
            else:
                ant = key + "B"

            T_flagged = T_ant[ant]
            if not all_lsts:
                T_flagged = T_flagged[night_bottom:night_top]

            print "Max", np.max(T_flagged), "Min", np.min(T_flagged)

            masks = {}
            if flag:
                if merge:
                    ## Already done
                    T_flagged = np.ma.array(T_flagged, mask=merged_mask)
                else:
                    ## Need to do it now - there's probably a way to deal with
                    ## this all in one pass
                    if not all_lsts:
                        masks = rfi_flag(T_ant[ant][border_bottom:border_top],
                                         freqs=f_leda)
                        T_flagged = masks.apply_as_mask(
                            T_ant[ant][border_bottom:border_top],
                            do_not_excise_dtv=True)
                        T_flagged = T_flagged[night_bottom -
                                              border_bottom:night_top -
                                              border_bottom]  # Remove border

                        masks.chop(night_bottom - border_bottom,
                                   night_top - border_bottom)
                    else:
                        masks = rfi_flag(T_flagged, freqs=f_leda)
                        T_flagged = masks.apply_as_mask(T_flagged,
                                                        do_not_excise_dtv=True)

                        print ant, "Biggest DTV gap", lst_stamps[biggest_gap(
                            masks.dtv_tms)[1]], "-", lst_stamps[biggest_gap(
                                masks.dtv_tms)[0]], "waterfall"
                print "After flagging", "Max", np.ma.max(
                    T_flagged), "Min", np.ma.min(T_flagged)

            try:
                T_asm = T_y_asm if p == 0 else T_x_asm
                scale_offset_asm = robust.mean(T_asm / T_flagged)
                T_flagged = T_flagged - T_asm / scale_offset_asm
            except NameError:
                pass

            T_flagged = pad_data(T_flagged)  # Up to 2400 channels

            if dump:
                if not all_lsts:
                    if masks:
                        dump_data[ant + "_flagged"] = masks.apply_as_nan(
                            T_ant[ant][night_bottom:night_top])

                    dump_data[ant] = T_ant[ant][night_bottom:night_top]
                else:
                    if masks:
                        dump_data[ant + "_flagged"] = masks.apply_as_nan(
                            T_ant[ant])
                    dump_data[ant] = T_ant[ant]
                dump_data[ant + "_rms"] = add_uncertainties(T_flagged)
                av = np.ma.average(T_flagged, axis=0)
                weighted = av / dump_data[ant + "_rms"]**2
                dump_data[ant + "_weighted"] = weighted
                if masks:
                    dump_data[ant + "_dtv_times"] = np.array(masks.dtv_tms)
                    dump_data[ant + "_masks"] = masks.masks

            if flag:
                total = T_flagged.shape[0] * T_flagged.shape[1]
                num_in = np.ma.MaskedArray.count(T_flagged)
                print ant, ("%.1f%%" % (100 * float(total - num_in) / total)
                            ), "flagged.", "Count:", total - num_in

            # Add the stripe onto the right edge of the data and adjust the extent of the x-axis (frequency) to cover the stripe.
            if all_lsts:
                T_flagged_plot = np.ma.concatenate((T_flagged, padding),
                                                   axis=1)
            else:
                T_flagged_plot = T_flagged

            ax.set_yticks(yloc)
            ax.set_yticklabels(ylabel)
            ax.tick_params(axis='y', pad=2)

            if flatten:
                if type(T_flagged_plot) is np.ma.core.MaskedArray:
                    abp = np.ma.median(T_flagged_plot.data, axis=0)
                else:
                    abp = np.ma.median(T_flagged_plot, axis=0)
                abp /= np.ma.median(abp)
                T_flagged_plot /= abp
                try:
                    clim = (percentile(T_flagged_plot.compressed(), 5),
                            percentile(T_flagged_plot.compressed(), 95))
                except AttributeError:
                    clim = (percentile(T_flagged_plot,
                                       5), percentile(T_flagged_plot, 95))

            elif sky:
                clim = (-250, 500)
            else:
                clim = (1000, 10000)
            if ant != "252B":
                im = ax.imshow(
                    T_flagged_plot,  # / np.median(xx, axis=0), 
                    cmap="viridis",
                    aspect='auto',
                    interpolation='nearest',
                    clim=clim,
                    extent=(xlims[0], new_x_high, ylims[1], ylims[0]))

            ax.set_title(ant)
            if p == 1:
                ax.set_xlabel("Frequency [MHz]")
            if ii == 0:
                ax.set_ylabel("LST [hr]")
            #ax.yaxis_date()
            #ax.yaxis.set_major_formatter(hfmt)
            #

    if not flatten:
        fig.subplots_adjust(left=0.07)
        fig.subplots_adjust(right=0.875)
        cbar_ax = fig.add_axes([0.9, 0.125, 0.025, 0.75])
        cbar = fig.colorbar(im, cax=cbar_ax)

        #plt.subplot(2,3,3)
        #cbar = plt.colorbar()
        if sky:
            cbar.set_label("Temperature - %s [K]" % smlbl)
        else:
            cbar.set_label("Temperature [K]")
        cbar.ax.tick_params(axis='y', pad=2)
        #plt.tight_layout()

    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)

    if save:
        plt.savefig(os.path.basename(filename)[:-3] + ".png")
    if not no_show:
        plt.show()

    if dump:
        dump_data["lsts"] = lst_stamps
        dump_data["utcs"] = np.array([str(pytime) for pytime in utc_stamps])
        dump_data["indexes"] = indexes
        dump_data["frequencies"] = pad_frequencies(f_leda)
        dump_data["options"] = "Flag="+str(flag) \
          + " Filename="+filename \
          + " New cal="+str(new_cal) \
                               + " Merge="+str(merge) \
                               + " Flatten="+str(flatten) \
                               + " All LSTs="+str(all_lsts) \
                               + " Sky Model Substract="+str(sky) \
                               + " Use LFSM="+str(lfsm) \
                               + " Apply empirical gain correction="+str(emp)
        dump_data["fingerprint"] = get_repo_fingerprint()
        import json

        def jdefault(o):
            return o.__dict__

        dump_data["params"] = json.dumps(params, default=jdefault)

        hickle.dump(dump_data, os.path.basename(filename)[:-3] + ".hkl")
예제 #12
0
def quicklook(filename):

    n = 1
    fs, fe = 40, 80
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']

    print T_ant.keys()

    ant_ids = ['252A', '254A', '255A']
    pol_id = 'y'

    print("Plotting...")
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = T_ant["252A"].shape[0] / 2

    print T_ant['lst'][mid]
    sl = 250

    f0 = np.arange(40, 70)
    f1 = f0 + 10
    f_trials = zip(f0, f1)
    f_trials.append((40, 80))
    alpha_out = []

    for fs, fe in f_trials:
        T_flagged = T_ant[ant_ids[0]][mid - sl:mid + sl]
        T_flagged = rfi_flag(T_flagged, freqs=f_leda)

        d = np.median(T_flagged, axis=0)
        d_errs = np.std(T_flagged, axis=0)

        f_trim, d = trim(f_leda, d, fs, fe)
        f_trim = np.ma.array(f_trim)
        f_trim.mask = d.mask

        f_trim2, d_errs = trim(f_leda, d_errs, fs, fe)
        d_errs = np.ma.array(d_errs)
        d_errs.mask = d.mask

        params = Parameters()
        params.add('amp', value=1e8, min=1e4, max=1e10)
        params.add('alpha', value=-2.5, min=-4, max=-1)

        fc = f_trim.compressed()
        dc = d.compressed()
        dc_errs = d_errs.compressed()

        print dc.shape, dc_errs.shape
        out = minimize(residual, params, args=(model, fc, dc, dc_errs))

        print report_fit(out)

        # print out.values
        #p = fit_poly(f_leda, d, n=n, print_fit=True)

        p = model(out.params, fc)
        alpha_out.append(out.params['alpha'].value)
        """
        plt.plot(fc, dc, label=ant_ids[0])
        plt.plot(fc, p, label='fit')

        plt.xlim(fs, fe)
        plt.minorticks_on()
        plt.ylim(500, 7000)

        ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
        ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())

        plt.xlabel("Frequency [MHz]")
        plt.ylabel("Temperature [K]")

        plt.legend(frameon=False)
        plt.show()
        """
    print "HERE", f0.shape, len(alpha_out)
    plt.plot(f0, alpha_out[:-1])

    plt.figure()
    res = dc - p
    res_1p = dc - poly_fit(fc, dc, 1, log=True)
    #res_2p = dc - poly_fit(fc, dc, 2, log=True)
    res_3p = dc - poly_fit(fc, dc, 3, log=True)
    #res_4p = dc - poly_fit(fc, dc, 4, log=True)
    res_5p = dc - poly_fit(fc, dc, 5, log=True)
    res_7p = dc - poly_fit(fc, dc, 7, log=True)
    #res_3p3f = res_3p - fourier_fit(res_3p, 0, 3)

    #plt.plot(fc, res)
    plt.plot(rebin(fc, 8), rebin(res_1p, 8))
    plt.plot(rebin(fc, 8), rebin(res_3p, 8))
    plt.plot(rebin(fc, 8), rebin(res_5p, 8))
    plt.plot(rebin(fc, 8), rebin(res_7p, 8))
    #plt.plot(rebin(fc, 8), rebin(res_5p, 8))

    print np.std(rebin(res_1p, 8))
    print np.std(rebin(res_3p, 8))
    print np.std(rebin(res_5p, 8))
    print np.std(rebin(res_7p, 8))
    #print np.std(rebin(res_5p, 8))
    #plt.plot(rebin(fc, 8), rebin(res_3p3f, 8))

    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)

    plt.show()
예제 #13
0
def quicklook(filename):

    balun_loss = hkl.load('cal_data/balun_loss.hkl')
    vna_cal = hkl.load('cal_data/vna_calibration.hkl')

    ant_ids = ['a252x']  #, 'a254x', 'a255x']

    fig = plt.figure(figsize=(10, 4))
    for ant_id in ant_ids:
        ra = vna_cal[ant_id]["ra"]
        rl = vna_cal[ant_id]["rl"]
        f = vna_cal["f"]
        F = compute_F(ra, rl)
        G = compute_G(rl)
        #G_f = fourier_fit(G, 0, 21)
        G_f = poly_fit(f, G, 11)
        F_f = fourier_fit(mag2(F), 0, 21)

        #ra_f = fourier_fit((1-mag2(ra)), 0, 21)
        ra_f = poly_fit(f, (1 - mag2(ra)), 5)
        ra_f2 = fourier_fit((1 - mag2(ra)) - ra_f, 0, 21)

        ra_f = ra_f + ra_f2

        #plt.subplot(2, 3, 1)
        plt.subplot2grid((3, 3), (0, 0), rowspan=2)
        plt.plot(f, (1 - mag2(ra)), c='#cc0000', label='$H_{\\rm{ant}}$')
        plt.plot(f, ra_f, c='#333333', label='$H_{\\rm{ant}}$')
        plt.yticks([0.5, 0.6, 0.7, 0.8])
        plt.ylim(0.5, 0.8)
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   ["", "", "", "", "", "", "", "", "", ""])

        #plt.subplot(2,3,4)
        plt.subplot2grid((3, 3), (2, 0), rowspan=1)
        plt.plot(f, (1 - mag2(ra)) - ra_f, c='#333333')
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   [40, "", 50, "", 60, "", 70, "", 80, ""])
        plt.ylim(-0.002, 0.002)
        plt.yticks([-0.002, -0.001, 0, 0.001, 0.002])
        plt.xlabel("Frequency [MHz]")

        #plt.subplot(2, 3, 2)
        plt.subplot2grid((3, 3), (0, 1), rowspan=2)
        plt.plot(f, G, c='#cc0000', label='$H_{\\rm{lna}}$')
        plt.plot(f, G_f, c='#333333', label='$H_{\\rm{lna}}$')
        plt.yticks([0.997, 0.998, 0.999, 1.000])
        plt.ylim(0.997, 1.000)
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   ["", "", "", "", "", "", "", "", "", ""])

        plt.subplot2grid((3, 3), (2, 1), rowspan=1)
        plt.plot(f, G - G_f, c='#333333')
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   [40, "", 50, "", 60, "", 70, "", 80, ""])
        plt.ylim(-0.0001, 0.0001)
        plt.xlabel("Frequency [MHz]")

        #plt.subplot(2, 3, 3)
        plt.subplot2grid((3, 3), (0, 2), rowspan=2)
        plt.plot(f, mag2(F), c='#cc0000', label='$|F|^2$')
        plt.plot(f, F_f, c='#333333', label='$|F|^2$')
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   ["", "", "", "", "", "", "", "", "", ""])

        #plt.subplot(2,3,6)
        plt.subplot2grid((3, 3), (2, 2), rowspan=1)
        plt.plot(f, mag2(F) - F_f, c='#333333')
        plt.xlim(40, 85)
        plt.xticks([40, 45, 50, 55, 60, 65, 70, 75, 80, 85],
                   [40, "", 50, "", 60, "", 70, "", 80, ""])
        plt.ylim(-0.002, 0.002)
        plt.yticks([-0.002, -0.001, 0, 0.001, 0.002])
        plt.xlabel("Frequency [MHz]")

    plt.tight_layout()
    #plt.legend(["$G$"], frameon=False, loc=4)
    #plt.text(40, 0.999, "$G$")
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.savefig("figures/cal-params.pdf")
    plt.show()
def quicklook(filename):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    
    ant_ids = ['252A', '254A', '255A', '254B', '255B']
    pol_id  = 'y'
      
    print("Plotting...")
    fig, axes = plt.subplots(figsize=(8, 6))
    #plt.suptitle(h5.filename)
    
    lst_stamps = T_ant['lst']
    utc_stamps = T_ant['utc']
    xlims = (f_leda[0], f_leda[-1])
    #ylims = mdates.date2num((T_ant['utc'][0], T_ant['utc'][-1]))
    #hfmt = mdates.DateFormatter('%m/%d %H:%M')
    ylims = (T_ant['lst'][0], T_ant['lst'][-1])
    
    T_avg = (T_ant["252A"] + T_ant["254A"] + T_ant["255A"]) / 3
    
    mid = T_ant["252A"].shape[0]/2
    sl  = 250
    
    plt.subplot(2,1,1)
    T_avg = np.median(T_avg[mid-sl:mid+sl], axis=0)
    T_252 = np.median(T_ant[ant_ids[0]][mid-sl:mid+sl], axis=0) - T_avg
    T_254 = np.median(T_ant[ant_ids[1]][mid-sl:mid+sl], axis=0) - T_avg
    T_255 = np.median(T_ant[ant_ids[2]][mid-sl:mid+sl], axis=0) - T_avg
    
    T_252 = simple_flag(T_252, 20.0)
    T_254 = simple_flag(T_254, 20.0)
    T_255 = simple_flag(T_255, 20.0)
    
    T_252 = T_252 / T_avg
    T_254 = T_254 / T_avg
    T_255 = T_255 / T_avg
    
    plt.plot(f_leda, T_252, label='252A')
    plt.plot(f_leda, T_254, label='254A')
    plt.plot(f_leda, T_255, label='255A')
    
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Fractional difference")

    plt.xlim(40, 85)
    plt.ylim(-0.07, 0.07)
    plt.legend(loc=4, frameon=True, ncol=3)
    plt.minorticks_on()
    
    plt.subplot(2,1,2)
    T_avg = (T_ant["254B"] + T_ant["255B"]) / 2.0
    
    mid = T_ant["252A"].shape[0]/2
    sl  = 250
    
    T_avg = np.median(T_avg[mid-sl:mid+sl], axis=0)
    #T_252 = np.median(T_ant[ant_ids[0]][mid-sl:mid+sl], axis=0) - T_avg
    T_254 = np.median(T_ant[ant_ids[3]][mid-sl:mid+sl], axis=0) - T_avg
    T_255 = np.median(T_ant[ant_ids[4]][mid-sl:mid+sl], axis=0) - T_avg
    
    #T_252 = simple_flag(T_252, 20.0)
    T_254 = simple_flag(T_254, 20.0)
    T_255 = simple_flag(T_255, 20.0)
    
    #T_252 = T_252 / T_avg
    T_254 = T_254 / T_avg
    T_255 = T_255 / T_avg
    
    #plt.plot(f_leda, T_252, label='252A')
    plt.plot(0,0)
    plt.plot(f_leda, T_254, label='254B')
    plt.plot(f_leda, T_255, label='255B')
    
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Fractional difference")

    plt.xlim(40, 85)
    plt.ylim(-0.07, 0.07)
    plt.legend(loc=4, frameon=True, ncol=3)
    plt.minorticks_on()
    
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    
    plt.savefig("figures/compare-ants.pdf")
    plt.show()
예제 #15
0

if __name__ == "__main__":

    import sys
    try:
        filename = sys.argv[1]
    except:
        print "USAGE: ./quicklook.py filename_of_hdf5_observation"
        exit()

    f, d, d_s = quicklook(filename)

    np.savetxt("freqs.txt", f)
    fh = open("freqs.txt", "a")
    fh.write("# %s\n" % get_repo_fingerprint())
    fh.close()
    np.savetxt("spectrum.txt", d)
    fh = open("spectrum.txt", "a")
    fh.write("# %s\n" % get_repo_fingerprint())
    fh.close()
    np.savetxt("spectrum_errs.txt", d_s)
    fh = open("spectrum_errs.txt", "a")
    fh.write("# %s\n" % get_repo_fingerprint())
    fh.close()

    plt.subplot(2, 1, 1)
    plt.plot(f, d)
    plt.subplot(2, 1, 2)
    plt.plot(f, d_s)
    plt.text(0.005,
예제 #16
0
def quicklook(filename, ant='252A', lfsm=False, emp=False, n_poly=7):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    lst = T_ant['lst']

    print T_ant.keys()

    ant_ids = [
        ant,
    ]

    print("Plotting %s..." % ant_ids[0])
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = closest(lst, 11)
    print T_ant['lst'][mid]
    sl = 20

    T_flagged = rfi_flag(T_ant[ant_ids[0]], freqs=f_leda)

    sl = 250

    plt.subplot2grid((3, 1), (0, 0), rowspan=2)
    plt.plot(f_leda, T_flagged[mid - sl:mid + sl].mean(axis=0), c='#009933')

    import hickle as hkl
    gsm = hkl.load("cal_data/gsm-spec-lst11.hkl")
    plt.plot(gsm["f"], gsm["T_ew"], c='#333333', ls='dashed')

    if lfsm and emp:
        smdl = SkyModelLFSMEmp
        smlbl = 'LFSM+Emp'
    elif lfsm and not emp:
        smdl = SkyModelLFSM
        smlbl = 'LFSM'
    elif not lfsm and emp:
        smdl = SkyModelGSMEmp
        smlbl = 'GSM+Emp'
    else:
        smdl = SkyModelGSM
        smlbl = 'GSM'
    s = smdl(pol='y' if ant_ids[0][-1] == 'A' else 'x')
    asm = s.generate_tsky(lst[mid - sl:mid + sl], f_leda * 1e6).mean(axis=0)
    plt.plot(f_leda, asm)

    d = T_flagged[mid - sl:mid + sl].mean(axis=0)
    f_t, d_t = trim(f_leda, d, 40, 80)
    if ant_ids[0][-1] == 'A':
        T_hsm = np.interp(f_t, gsm["f"], gsm["T_ew"])
    else:
        T_hsm = np.interp(f_t, gsm["f"], gsm["T_ns"])
    T_asm = np.interp(f_t, f_leda, asm)

    scale_offset = np.mean(T_hsm / d_t)
    scale_offset_asm = np.mean(T_asm / d_t)
    print scale_offset, scale_offset_asm

    #plt.plot(f_)
    plt.xlim(40, 80)
    plt.ylim(500, 8000)
    plt.minorticks_on()
    plt.ylabel("Temperature [K]")
    plt.legend([ant_ids[0], "GSM (.hkl)", smlbl])

    plt.subplot2grid((3, 1), (2, 0), rowspan=1)
    plt.plot(rebin(f_t, 8), rebin(d_t / T_hsm, 8), c='#333333', linestyle='--')
    plt.plot(rebin(f_t, 8),
             rebin(scale_offset * d_t / T_hsm, 8),
             c='#333333',
             linestyle='--')
    plt.plot(rebin(f_t, 8), rebin(d_t / T_asm, 8), c='#333333')
    plt.plot(rebin(f_t, 8),
             rebin(scale_offset_asm * d_t / T_asm, 8),
             c='#333333')
    plt.xlabel("Frequency [MHz]")
    #plt.yticks([0.85, 0.87, 0.89, 0.91, 0.93])
    #  plt.ylim(0.85, 0.93)
    plt.ylabel("data / model")
    plt.tight_layout()
    plt.minorticks_on()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.savefig("figures/skymodel-compare.pdf")
    plt.show()

    resid = d_t - T_hsm
    resid -= fit_poly(f_t, resid, n_poly)
    resid_asm = d_t - T_asm
    resid_asm -= fit_poly(f_t, resid_asm, n_poly)

    plt.plot(f_t, resid, linestyle='--')
    plt.plot(f_t, resid_asm)
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()
def quicklook(filename):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']

    ant_ids = ['252A', '254A', '255A']
    pol_id = 'y'

    print("Plotting...")
    fig, axes = plt.subplots(figsize=(12, 6))
    #plt.suptitle(h5.filename)

    lst_stamps = T_ant['lst']
    utc_stamps = T_ant['utc']
    xlims = (f_leda[0], f_leda[-1])
    #ylims = mdates.date2num((T_ant['utc'][0], T_ant['utc'][-1]))
    #hfmt = mdates.DateFormatter('%m/%d %H:%M')
    ylims = (T_ant['lst'][0], T_ant['lst'][-1])

    T_avg = (T_ant["252A"] + T_ant["254A"] + T_ant["255A"]) / 3

    for ii, key in enumerate(ant_ids):
        ax = fig.add_subplot(1, 3, ii + 1)

        T_flagged = T_ant[key]
        #T_flagged = rfi_flag(T_ant[key], f_leda)

        T_ant[key] = T_flagged

        im = plt.imshow(
            T_flagged - T_avg,  # / np.median(xx, axis=0), 
            cmap='viridis',
            aspect='auto',
            interpolation='nearest',
            clim=(-250, 250),
            extent=(xlims[0], xlims[1], ylims[1], ylims[0]))
        plt.title(ant_ids[ii])
        plt.xlabel("Frequency [MHz]")
        #ax.yaxis_date()
        #ax.yaxis.set_major_formatter(hfmt)
        #

    plt.subplot(1, 3, 1)
    plt.ylabel("LST [hr]")

    fig.subplots_adjust(left=0.06)
    fig.subplots_adjust(right=0.875)
    cbar_ax = fig.add_axes([0.9, 0.125, 0.025, 0.75])
    cbar = fig.colorbar(im, cax=cbar_ax)

    plt.subplot(1, 3, 3)
    #cbar = plt.colorbar()
    cbar.set_label("Temperature [K]")

    #plt.tight_layout()
    plt.show()
    exit()  # I get an error if it goes further
    plt.xlabel("Frequency [MHz]")

    plt.ylabel("LST [hr]")

    #fig.subplots_adjust(left=0.06)
    #fig.subplots_adjust(right=0.875)
    #cbar_ax = fig.add_axes([0.9, 0.125, 0.025, 0.75])
    #cbar = fig.colorbar(im, cax=cbar_ax)
    cbar = plt.colorbar()

    #plt.subplot(1,3,3)
    #cbar = plt.colorbar()
    cbar.set_label("Temperature [K]")

    #plt.tight_layout()
    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)
    plt.show()
def quicklook(filename, lfsm=False, emp=False):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']
    lst    = T_ant['lst']
    
    print T_ant.keys()
    
    ant_ids, figout = ['252A', '254A', '255A'], "residuals-high-polA.pdf"
    #ant_ids, figout = ['BB', '254B', '255B'], "residuals-high-polB.pdf"
      
    print("Plotting...")
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = closest(lst, 11)
    print T_ant['lst'][mid]
    
    
    import hickle as hkl
    gsm = hkl.load("cal_data/gsm-spec-lst11.hkl")
    sl  = 250    
    n_poly = 5
    n_chan = 42
    
    if lfsm and emp:
        smdl = SkyModelLFSMEmp
        smlbl = 'LFSM+Emp'
    elif lfsm and not emp:
        smdl = SkyModelLFSM
        smlbl = 'LFSM'
    elif not lfsm and emp:
        smdl = SkyModelGSMEmp
        smlbl = 'GSM+Emp'
    else:
        smdl = SkyModelGSM        
        smlbl = 'GSM'
   
    fig = plt.figure("resid", figsize=(6, 8))

    for aa in ant_ids:
        if not aa == 'BB':
            T_flagged = rfi_flag(T_ant[aa], freqs=f_leda)

            #plt.plot(gsm["f"], gsm["T_ew"], c='#333333', ls='dashed')

            d = T_flagged[mid-sl:mid+sl].mean(axis=0)

            #d_flags = T_flagged.mask[mid-sl:mid+sl].sum(axis=0)
            #d_errs  = T_flagged[mid-sl:mid+sl].std(axis=0) / np.sqrt(d_flags)

            f_t, d_t = trim(f_leda, d, F_START, F_STOP)
            #f_t, d_errs_t = trim(f_leda, d_errs, 40, 80)
            
            s = smdl(pol='y' if aa[-1] == 'A' else 'x')
            asm = s.generate_tsky(lst[mid-sl:mid+sl], f_leda*1e6).mean(axis=0)
    
            T_hsm = np.interp(f_t, gsm["f"], gsm["T_ew"])
            T_asm = np.interp(f_t, f_leda, asm)

            scale_offset = np.mean(T_hsm / d_t)
            scale_offset_asm = np.mean(T_asm / d_t)
            print scale_offset, scale_offset_asm
            
            model_skypowerlaw = poly_fit(f_t, T_hsm, 1, log=True)
            model_skypowerlaw_asm = poly_fit(f_t, T_asm, 1, log=True)
            
            #resid0 = scale_offset * d_t - T_hsm
            resid0 = d_t #scale_offset * d_t #- T_hsm
            resid0_asm = d_t #scale_offset * d_t #- T_hsm
            
            #plt.figure("m0")
            #plt.plot(T_hsm - model_skypowerlaw)
            
            resid0 = resid0 #+ model_skypowerlaw
            resid0_asm = resid0_asm #+ model_skypowerlaw_asm
        
        pols = (1, 3, 5, 7)
        
        for ii, nn in enumerate(pols):
            plt.figure("resid")
            plt.subplot(len(pols), 1, ii+1)
            
            if aa == 'BB':
                plt.plot(0)
            else:
                if nn == 0:
                    model = 0
                else:
                    model = poly_fit(f_t, resid0, nn, log=True)
                    #model = fourier_fit(resid0, 0, nn)
                    model_asm = poly_fit(f_t, resid0_asm, nn, log=True)
                    #model_asm = fourier_fit(resid0_asm, 0, nn)
                #model = 0
                #model_asm = 0

                #plt.plot(f_t, resid0, linestyle='--')
                #plt.plot(f_t, model, linestyle='--')
                #plt.plot(f_t, resid0_asm)
                #plt.plot(f_t, model_asm)
                
                plt.plot(rebin(f_t, n_chan), rebin(resid0-model, n_chan), linestyle='--')
                plt.plot(rebin(f_t, n_chan), rebin(resid0_asm-model_asm, n_chan))
            plt.ylabel("Temperature [K]")
            plt.xlim(F_START, F_STOP)
            plt.minorticks_on()
            
    plt.xlabel("Frequency [MHz]")        
    
    plt.subplot(4,1,1,)
    plt.ylim(-100, 100)
    plt.subplot(4,1,2)
    plt.ylim(-30, 30)
    plt.subplot(4,1,3)
    plt.ylim(-30, 30)
    plt.subplot(4,1,4)
    plt.ylim(-12, 12)
    #plt.yticks([-40, -20, 0, 20, 40])
    
    plt.tight_layout()
    plt.text(0.005, 0.005, get_repo_fingerprint(), transform=fig.transFigure, size=8)
    plt.savefig(figout)
    plt.show()
예제 #19
0
def quicklook(filename, save, noshow):
    h5 = tb.open_file(filename)

    T_ant = apply_calibration(h5)
    f_leda = T_ant['f']

    print T_ant.keys()

    ant_ids = ['252A', '254A', '255A', '252B', '254B',
               '255B']  # Added 252B, why was it missing? HG
    pol_id = 'y'

    print("Plotting...")
    fig, ax = plt.subplots(figsize=(8, 6))

    mid = T_ant["252A"].shape[
        0] / 2  # Currently plots the middle bit of the observation
    print T_ant['lst'][mid]  # Print the LST about which we are averaging
    sl = 50  # The number of integrations to average either side

    print ant_ids[0], mid, sl
    print ant_ids[1], mid, sl
    print ant_ids[2], mid, sl

    plt.subplot(2, 1, 1)
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[0]][mid - sl:mid + sl], axis=0),
             label=ant_ids[0])
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[1]][mid - sl:mid + sl], axis=0),
             label=ant_ids[1])
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[2]][mid - sl:mid + sl], axis=0),
             label=ant_ids[2])
    plt.legend(frameon=False)
    plt.ylabel("Temperature [K]")
    plt.xlim(40, 85)
    plt.minorticks_on()
    #plt.ylim(500, 7000)

    plt.subplot(2, 1, 2)
    plt.plot(0, 0)
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[3]][mid - sl:mid + sl], axis=0),
             label=ant_ids[3])
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[4]][mid - sl:mid + sl], axis=0),
             label=ant_ids[4])
    plt.plot(f_leda,
             np.median(T_ant[ant_ids[5]][mid - sl:mid + sl], axis=0),
             label=ant_ids[5])

    plt.legend(frameon=False)

    plt.xlim(40, 85)
    plt.minorticks_on()
    #plt.ylim(500, 7000)

    plt.xlabel("Frequency [MHz]")
    plt.ylabel("Temperature [K]")

    plt.legend(frameon=False)
    plt.tight_layout()

    plt.text(0.005,
             0.005,
             get_repo_fingerprint(),
             transform=fig.transFigure,
             size=8)

    plt.savefig("figures/compare-spectra.pdf")

    if save:
        plt.savefig("spec_" + os.path.basename(filename)[:-3] + ".png")

    if not noshow:
        plt.show()