Exemplo n.º 1
0
def get_angular_profile(maps, thmax=25, nbins=20, label='', center=np.array([316.44761929, -58.75808063]),
                        allstokes=False, fontsize=None, doplot=False, separate=False):
    vec0 = hp.ang2vec(center[0], center[1], lonlat=True)
    sh = np.shape(maps)
    ns = hp.npix2nside(sh[0])
    vecpix = hp.pix2vec(ns, np.arange(12 * ns ** 2))
    angs = np.degrees(np.arccos(np.dot(vec0, vecpix)))
    rng = np.array([0, thmax])
    xx, yyI, dx, dyI, _ = ft.profile(angs, maps[:, 0], nbins=nbins, plot=False, rng=rng)
    xx, yyQ, dx, dyQ, _ = ft.profile(angs, maps[:, 1], nbins=nbins, plot=False, rng=rng)
    xx, yyU, dx, dyU, _ = ft.profile(angs, maps[:, 2], nbins=nbins, plot=False, rng=rng)
    avg = np.sqrt((dyI ** 2 + dyQ ** 2 / 2 + dyU ** 2 / 2) / 3)
    if doplot:
        plot(xx, avg, 'o', label=label)
        if allstokes:
            plot(xx, dyI, label=label + ' I', alpha=0.3)
            plot(xx, dyQ / np.sqrt(2), label=label + ' Q/sqrt(2)', alpha=0.3)
            plot(xx, dyU / np.sqrt(2), label=label + ' U/sqrt(2)', alpha=0.3)
        xlabel('Angle [deg.]')
        ylabel('RMS')
        legend(fontsize=fontsize)
    if separate:
        return xx, dyI, dyQ, dyU
    else:
        return xx, avg
Exemplo n.º 2
0
def get_flatmap(TESNum,
                directory,
                azmin=None,
                azmax=None,
                elmin=None,
                elmax=None,
                remove=None,
                fitted_directory=None):
    themap = np.array(
        FitsArray(directory + '/Flat/imgflat_TESNum_{}.fits'.format(TESNum)))
    az = np.array(FitsArray(directory + '/Flat/azimuth.fits'.format(TESNum)))
    el = np.array(FitsArray(directory + '/Flat/elevation.fits'.format(TESNum)))
    if azmin is None:
        azmin = np.min(az)
    if azmax is None:
        azmax = np.max(az)
    if elmin is None:
        elmin = np.min(el)
    if elmax is None:
        elmax = np.max(el)
    okaz = (az >= azmin) & (az <= azmax)
    az = az[okaz]
    okel = (el >= elmin) & (el <= elmax)
    el = el[okel]
    themap = themap[:, okaz][okel, :]
    if remove is not None:
        mm = np.mean(remove)
        ss = np.std(remove)
        xc, yval, dx, dy, others = ft.profile(remove,
                                              themap,
                                              rng=[mm - 2 * ss, mm + 2 * ss],
                                              mode=True,
                                              nbins=20,
                                              cutbad=True,
                                              plot=False,
                                              dispersion=False,
                                              clip=3)
        bla = np.polyfit(xc, yval, 1, w=1. / dy**2)
        pp = np.poly1d(bla)
        themap -= pp(remove)

    if fitted_directory is None:
        return themap, az, el
    else:
        ### Read fitted synthesized beam
        thefile = open(fitted_directory + '/fit-TES{}.pk'.format(TESNum), 'rb')
        fitpars = pickle.load(thefile, encoding='latin1')
        thefile.close()
        sbfitmodel = SbModelIndepPeaks(nrings=2,
                                       common_fwhm=True,
                                       no_xy_shift=False,
                                       distortion=False)
        x = np.meshgrid(az * np.cos(np.radians(50)), np.flip(el))
        fitmap, newxxyy = sbfitmodel(x, fitpars, return_peaks=True)
        return themap, az, el, fitmap, newxxyy
Exemplo n.º 3
0
def find_right_period(guess, t_data, data_oneTES):
    ppp = np.linspace(guess - 1.5, guess + 1.5, 250)
    rms = np.zeros(len(ppp))
    for i in range(len(ppp)):
        xin = t_data % ppp[i]
        yin = data_oneTES
        xx, yy, dx, dy, o = ft.profile(xin, yin, nbins=100, plot=False)
        rms[i] = np.std(yy)
    period = ppp[np.argmax(rms)]

    return ppp, rms, period
Exemplo n.º 4
0
def get_noise_invcov_profile(maps, coverage, covcut=0.1, nbins=100, fit=True, label='',
                             norm=False, allstokes=False, fitlim=None, doplot=False, QUsep=True):
    seenpix = coverage > (covcut * np.max(coverage))
    covnorm = coverage / np.max(coverage)

    xx, yyI, dx, dyI, _ = ft.profile(np.sqrt(1. / covnorm[seenpix]), maps[seenpix, 0], nbins=nbins, plot=False)
    xx, yyQ, dx, dyQ, _ = ft.profile(np.sqrt(1. / covnorm[seenpix]), maps[seenpix, 1], nbins=nbins, plot=False)
    xx, yyU, dx, dyU, _ = ft.profile(np.sqrt(1. / covnorm[seenpix]), maps[seenpix, 2], nbins=nbins, plot=False)
    avg = np.sqrt((dyI ** 2 + dyQ ** 2 / 2 + dyU ** 2 / 2) / 3)
    avgQU = np.sqrt((dyQ ** 2 / 2 + dyU ** 2 / 2) / 2)
    if norm:
        fact = xx[0] / avg[0]
    else:
        fact = 1.
    myY = (avg / xx) * fact
    myYI = (dyI / xx) * fact
    myYQU = (avgQU / xx) * fact

    if doplot:
        if QUsep is False:
            p = plot(xx ** 2, myY, 'o', label=label + ' IQU')
            if allstokes:
                plot(xx ** 2, myYI, label=label + ' I', alpha=0.3)
                plot(xx ** 2, myYQU, label=label + ' Average Q, U /sqrt(2)', alpha=0.3)
        else:
            pi = plot(xx ** 2, myYI, 'o', label=label + ' I')
            pqu = plot(xx ** 2, myYQU, 'o', label=label + ' QU / sqrt(2)')

    if fit:
        ok = isfinite(myY)
        if fitlim is not None:
            print('Clipping fit from {} to {}'.format(fitlim[0], fitlim[1]))
            ok = ok & (xx >= fitlim[0]) & (xx <= fitlim[1])
        if QUsep is False:
            mymodel = lambda x, a, b, c, d, e: (a + b * x + c * np.exp(-d * (x - e)))  # /(a+b+c*np.exp(-d*(1-e)))
            myfit = curve_fit(mymodel, xx[ok] ** 2, myY[ok], p0=[np.min(myY[ok]), 0.4, 0, 2, 1.5], maxfev=100000,
                              ftol=1e-7)
        else:
            mymodel = lambda x, a, b, c, d, e, f, g: (
                    a + b * x + f * x ** 2 + g * x ** 3 + c * np.exp(-d * (x - e)))  # /(a+b+c*np.exp(-d*(1-e)))
            myfitI = curve_fit(mymodel, xx[ok] ** 2, myYI[ok], p0=[np.min(myY[ok]), 0.4, 0, 2, 1.5, 0., 0.],
                               maxfev=100000, ftol=1e-7)
            myfitQU = curve_fit(mymodel, xx[ok] ** 2, myYQU[ok], p0=[np.min(myY[ok]), 0.4, 0, 2, 1.5, 0., 0.],
                                maxfev=100000, ftol=1e-7)
        if doplot:
            if QUsep is False:
                plot(xx ** 2, mymodel(xx ** 2, *myfit[0]), label=label + ' Fit', color=p[0].get_color())
            else:
                plot(xx ** 2, mymodel(xx ** 2, *myfitI[0]), label=label + ' Fit I', color=pi[0].get_color())
                plot(xx ** 2, mymodel(xx ** 2, *myfitQU[0]), label=label + ' Fit QU / sqrt(2)',
                     color=pqu[0].get_color())

            # print(myfit[0])
        # Interpolation of the fit from invcov = 1 to 15
        invcov_samples = np.linspace(1, 15, 1000)
        if QUsep is False:
            eff_v = mymodel(invcov_samples, *myfit[0]) ** 2
            # Avoid extrapolation problem for pixels before the first bin or after the last one.
            eff_v[invcov_samples < xx[0] ** 2] = mymodel(xx[0] ** 2, *myfit[0]) ** 2
            eff_v[invcov_samples > xx[-1] ** 2] = mymodel(xx[-1] ** 2, *myfit[0]) ** 2

            effective_variance_invcov = np.array([invcov_samples, eff_v])
        else:
            eff_vI = mymodel(invcov_samples, *myfitI[0]) ** 2
            eff_vQU = mymodel(invcov_samples, *myfitQU[0]) ** 2
            # Avoid extrapolation problem for pixels before the first bin or after the last one.
            eff_vI[invcov_samples < xx[0] ** 2] = mymodel(xx[0] ** 2, *myfitI[0]) ** 2
            eff_vQU[invcov_samples > xx[-1] ** 2] = mymodel(xx[-1] ** 2, *myfitQU[0]) ** 2

            effective_variance_invcov = np.array([invcov_samples, eff_vI, eff_vQU])

    if doplot:
        xlabel('1./cov normed')
        if norm:
            add_yl = ' (Normalized to 1 at 1)'
        else:
            add_yl = ''
        ylabel('RMS Ratio w.r.t linear scaling' + add_yl)

    if fit:
        return xx, myY, effective_variance_invcov
    else:
        return xx, myY, None