Ejemplo n.º 1
0
def FitTOD(tod, ra, dec, obs, clon, clat, prefix='', destripe=False):
    # Beam solid angle aken from James' report on wiki Optics
    nubeam = np.array([26., 33., 40.])
    srbeam = np.array([2.1842e-6, 1.6771e-6, 1.4828e-6])
    pmdl = interp1d(nubeam, srbeam) # straight interpolation

    nHorns = tod.shape[0]
    nSidebands = tod.shape[1]
    nChans = tod.shape[2]
    nSamps = tod.shape[3]
    nParams = 5 + 0# 2


    wcs, xr, yr = Mapping.DefineWCS(naxis, cdelt, crval)

    # Crossing indices
    crossings = np.zeros(nHorns)


    # Calculate RMS from adjacent pairs
    splitTOD = (tod[:,:,:,:(nSamps//2) * 2:2] - tod[:,:,:,1:(nSamps//2)*2:2])
    rms = np.std(splitTOD,axis=3)/np.sqrt(2)

    t = np.arange(nSamps)
    P1 = np.zeros((nHorns, nSidebands, nChans, nParams+1))
    errors = np.zeros((nHorns, nSidebands, nChans, nParams))

    fig = pyplot.figure(figsize=(16,16))
    # Rotate the ra/dec
    width = 4.
    pixwidth = 2./60.
    nbins = int(width/pixwidth)
    xygrid = [np.linspace(-width/2, width/2,nbins+1), np.linspace(-width/2, width/2.,nbins+1)]
    xgrid, ygrid = np.meshgrid(np.linspace(-width/2, width/2,nbins)+pixwidth/2., np.linspace(-width/2, width/2.,nbins) + pixwidth/2.)
    for i in  range( nHorns):
        x, y = Pointing.Rotate(ra[i,:], dec[i,:], clon, clat, 0)
        
        # Bin the map up, apply a filter to remove spikes, to give first guess at pixel centre.
        hmap = np.histogram2d(y,x, xygrid)[0]
        todTemp = np.median(np.median(tod[i,:,:,:],axis=0),axis=0)
        rmsTemp = np.std(todTemp[1:todTemp.size//2 *2:2] - todTemp[:todTemp.size//2 * 2:2])/np.sqrt(2)
        smap = np.histogram2d(y,x, xygrid, weights = todTemp)[0]
        xpix = (x+width/2)//pixwidth 
        ypix = (y+width/2)//pixwidth
        #pyplot.plot(xpix, ypix, 'o')
        #pyplot.figure()
        #pyplot.plot(x, y, 'o')
        #pyplot.show()

        if destripe:
            pixels = (ypix + nbins*xpix).astype(int)
            offset = 300
            gd = (pixels > 0) & (pixels < nbins*nbins-1)
            m, offsets = Mapping.Destripe(todTemp[gd], pixels[gd], obs[gd], offset, nbins*nbins)
            m = np.reshape(m, (nbins, nbins)).T 
            #m = smap/hmap
        else:
            #h, wx, wy = np.histogram2d(x,y, (xygrid[0], xygrid[1]))
            #s, wx, wy = np.histogram2d(x,y, (xygrid[0], xygrid[1]), weights=todTemp)
            #m = s/h
            offsets  =0
            m, hits = Mapping.MakeMapSimple(todTemp, x, y, wcs)
            m = m/hits

        m -= np.nanmedian(m)
        #m /= rmsTemp
        m[np.isnan(m)] = 0.

        #pyplot.plot(ra[i,:], todTemp-offsets,'.')
        #pyplot.figure()
        #pyplot.plot(dec[i,:], todTemp-offsets,'.')
        #pyplot.show()


        r = np.sqrt((xgrid)**2 + (ygrid)**2)   
        
        d1 = ImagePeaks(m, r, 4)
        # if len(coords1) > 0:
        #     ximax, yimax = coords1[0]
        # else:
        #     m = median_filter(m, size=(2,2))
        # Remove background?
        xgrid2,ygrid2 = np.meshgrid(np.linspace(-d1.shape[0]/2, d1.shape[0]/2, d1.shape[0]), np.linspace(-d1.shape[1]/2, d1.shape[1]/2, d1.shape[1]))
        background = np.sqrt((xgrid2 - 0)**2 + (ygrid2 - 0)**2) > 25
        print(d1.shape, xgrid.shape)
        d1[background] = 0
        
        ximax, yimax = np.unravel_index(np.argmax(d1), m.shape)
        print(ximax, yimax, np.max(d1))
        #pyplot.imshow(m)
        #pyplot.plot(yimax, ximax,'ow')
        #pyplot.figure()
        #pyplot.imshow(d1)
        #pyplot.plot(yimax, ximax,'ow')
        #pyplot.show()

        # +/- 180
        x[x > 180] -= 360

        #pyplot.plot(x,y)
        #pyplot.figure()
        #pyplot.plot(az[i,:], el[i,:])
        #pyplot.show()

        #xbins = np.linspace(-4, 4, 60*8+1)
        #ybins = np.linspace(-4, 4, 60*8+1)

        # Just select the near data
        y0,x0 = -xr[ximax,yimax], yr[ximax,yimax]
        
        print(x0, y0)
        
        #print(x0, y0)
        #pyplot.figure(figsize=(6,6))
        #pyplot.plot(yr.flatten(),m.flatten(),'-')
        #pyplot.axvline(y0,color='r')
        #pyplot.show()


        background = np.sqrt((x - x0)**2 + (y - y0)**2) > 30./60.
        
        

        #x0, y0 = 0, 0
        #pyplot.plot(x,tod[0,0,0,:])
        #pyplot.axvline(x0)
        #pyplot.axvline(x[np.argmax(tod[0,0,0,:])],color='r')
        #pyplot.figure()
        #pyplot.plot(tod[0,0,0,:])
        #pyplot.show()
        r = np.sqrt((x-x0)**2 + (y-y0)**2)
        close = (r < 12.5/60.)
        near  = (r < 25./60.) & (r > 15/60.)
        far   = (r > 15./60.)
        fitselect = (r < 20./60.)
        time = np.arange(tod.shape[-1])
        #pyplot.plot(time[:], tod[0,0,0,:])
        #pyplot.plot(time[fitselect], tod[0,0,0,fitselect])
        #pyplot.show()
        #extent = [-naxis[0]/2. * cdelt[0], naxis[0]/2. * cdelt[0], 
        #          -naxis[1]/2. * cdelt[1], naxis[1]/2. * cdelt[1]]

        #pyplot.figure(figsize=(6,6))
        
        #m2 = m*0
        #r2 = np.sqrt((xr-x0)**2 + (yr-y0)**2)
        
        #m2 = (r2 < 10./60.)
        #pyplot.imshow(m.T,extent=extent,origin='lower')
        #pyplot.scatter(x0,y0, marker='o',color='r')
        #pyplot.figure()
        #pyplot.imshow(m2.T,extent=extent,origin='lower')
        #pyplot.scatter(x0,y0, marker='o',color='r')
        #pyplot.show()
        

        #pyplot.plot(time[:],todTemp[:],'.')
        #pyplot.plot(time[fitselect],todTemp[fitselect],'.')
        #pyplot.show()

        plotselect = (r < 120./60.)
        for j in range(nSidebands):
            
            for k in range(nChans):
                
                rmdl = np.poly1d(np.polyfit(time[background], tod[i,j,k,background]-offsets,11))
                todBackground =RemoveBackground(tod[i,j,k,:], rms[i,j,k], close, sampleRate=50, cutoff=1.)
                
                if destripe:
                    m, offsets = Mapping.Destripe(tod[i,j,k,gd], pixels[gd], obs[gd], offset, nbins*nbins)
                    m = np.reshape(m, (nbins,nbins)).T
                else:
                    m, hits = Mapping.MakeMapSimple(tod[i,j,k,:], x, y, wcs)
                    m = m/hits


                #tod[i,j,k,:] = tod[i,j,k,:] -offsets #-(rmdl(ra[i,:])+dmdl(dec[i,:]))
                tod[i,j,k,:] -= todBackground#np.median(tod[i,j,k,:])
                fitdata = tod[i,j,k,fitselect]
                fitra = x[fitselect]
                fitdec= y[fitselect]

                amax = np.argmax(fitdata)

                P0 = [np.max(fitdata) -np.median(fitdata) ,
                      4./60./2.355,
                      4./60./2.355,
                      np.median(fitdata),
                      x0,
                      y0,
                      0.,
                      0., 0.]#,0.,0., 0., 0.]
                P0 = [np.max(fitdata) -np.median(fitdata) ,
                      4./60./2.355,
                      x0,
                      y0,
                      np.median(fitdata),
                      0., 0.]#,0.,0., 0., 0.]
                P0 = [np.max(fitdata) -np.median(fitdata) ,
                      4./60./2.355,
                      x0,
                      y0,
                      np.median(fitdata)]#,0.,0., 0., 0.]


                #P1[i,j,k,:nParams], cov_x, info, mesg, s = leastsq(Error, P0, args=(fitra, fitdec, fitdata, 0,0), full_output=True)
                #fout = fmin(ErrorFmin, P0, maxfun=3000, args=(fitra, fitdec, fitdata, 0,0), full_output=True)
                #fout = leastsq(ErrorLstSq, P0, args=(fitra, fitdec, fitdata, 0,0), full_output=True)
                                
                ndim, nwalkers = len(P0), 100
                pos = [np.array(P0) + 1e-4*np.random.randn(ndim) for iwalker in range(nwalkers)]
                sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprobNoGrad, args=(fitra, fitdec, fitdata, rms[i,j,k]))
                sampler.run_mcmc(pos, 1200)
                samples = sampler.chain[:, 500:sampler.chain.shape[1]:3, :].reshape((-1, ndim))
                
                fout = np.mean(samples,axis=0), 
                P1[i,j,k,:nParams] = fout[0]
                print(fout[0])
                print (P0)
                print(x0, y0)
                #import corner
                #pyplot.clf()
                #fig = corner.corner(samples)
                #pyplot.show()
                
                P2 = P1[i,j,k,:nParams]*1.
                P2[0] = 0.

                ntod = Gauss2dNoGrad(P1[i,j,k,:nParams], x, y, 0,0)
                nulltod = Gauss2dNoGrad(P2, x, y, 0.,0.)
                otod = tod[i,j,k,:]
                omaps, hits = Mapping.MakeMapSimple(otod, x, y, wcs)
                nmaps, hits = Mapping.MakeMapSimple(ntod, x, y, wcs)
                nulls, hits = Mapping.MakeMapSimple(nulltod, x, y, wcs)


                xgrid, ygrid = np.meshgrid(np.arange(naxis[0]), np.arange(naxis[1]))
                xgrid = (xgrid-naxis[0]/2.)
                ygrid = (ygrid-naxis[1]/2.)
                rgrid = np.sqrt(xgrid**2 + ygrid**2)
                getchi = (rgrid < 5)
                nearChi = (rgrid > 7.5) & (rgrid < 15)

                maprms = np.nanstd((nmaps[nearChi]-omaps[nearChi])/hits[nearChi])
                rmap = (omaps[getchi]-nmaps[getchi])/hits[getchi]
                mapChi = np.nansum((rmap-np.mean(rmap))**2/maprms**2)/(nParams-1.)
                mapOrig = np.nansum(((omaps[getchi]-nulls[getchi])/hits[getchi])**2/maprms**2)/(nParams-1.)

                
                origChi = np.sum((tod[i,j,k,plotselect] -  Gauss2dNoGrad(P2, x[plotselect], y[plotselect], 0.,0.))**2/rms[i,j,k]**2)/(nParams-1.)
                reducedChi = np.sum((tod[i,j,k,plotselect] -  Gauss2dNoGrad(P1[i,j,k,:nParams], x[plotselect], y[plotselect], 0.,0.))**2/rms[i,j,k]**2)/(nParams-1.)
                #print('CHI2', origChi, reducedChi, mapChi, mapOrig, reducedChi/origChi)
                P1[i,j,k,nParams] = rms[i,j,k]


                if (k >= 0):
                    #r2 = np.sqrt((x-P1[i,j,k,2])**2 + (y-P1[i,j,k,3])**2)
                    #plotselect = (r2 < 15./60.)

                    ax = fig.add_subplot(3,1,1)
                    todPlot = tod[i,j,k,plotselect]
                    todPlot -= np.median(todPlot)
                    pyplot.plot(todPlot-Gauss2dNoGrad(P1[i,j,k,:nParams], x[plotselect], y[plotselect], 0,0))
                    pyplot.title('Horn {}, Sideband {}, Avg. Channel {} \n {}'.format(i,j,k, prefix))
                    pyplot.xlabel('Sample')
                    pyplot.ylabel('Detector Units')
                    pyplot.text(0.9,0.9,r'$rms$ = '+'{:.3f}'.format(rms[i,j,k]), ha='right', transform=ax.transAxes) 
                    ax = fig.add_subplot(3,1,2)
                    pyplot.plot(todPlot)

                    P3 = P1[i,j,k,:nParams]*1.
                    P3[3] = 0
                    P3[7:8] = 0
                    p3Model = Gauss2dNoGrad(P3, x[plotselect], y[plotselect], 0,0)
                    pyplot.plot(Gauss2dNoGrad(P1[i,j,k,:nParams], x[plotselect], y[plotselect], 0,0))#p3Model - np.nanmedian(p3Model))
                    pyplot.plot(Gauss2dNoGrad(P2, x[plotselect], y[plotselect], 0,0))

                    pyplot.xlabel('Sample')
                    pyplot.ylabel('Detector Units')
                    pyplot.text(0.9,0.9,r'$\chi^2$ = '+'{:.3f}'.format(mapChi), ha='right', transform=ax.transAxes) 
                    
                    
                    extent = [-naxis[0]/2. * cdelt[0], naxis[0]/2. * cdelt[0], 
                              -naxis[1]/2. * cdelt[1], naxis[1]/2. * cdelt[1]]
                    ax = fig.add_subplot(3,3,7)
                    ax.imshow(m, aspect='auto', extent=extent)
                    ax.scatter(P1[i,j,k,2], P1[i,j,k,3], marker='o',color='r')
                    ax = fig.add_subplot(3,3,8)
                    ax.imshow(nmaps/hits, extent=extent)
                    ax.scatter(P1[i,j,k,2], P1[i,j,k,3], marker='o',color='r')

                    ax = fig.add_subplot(3,3,9)
                    ax.imshow(nmaps/hits-m, extent=extent)
                    ax.scatter(P1[i,j,k,2], P1[i,j,k,3], marker='o',color='r')

                    #pyplot.tight_layout(True)
                    #pyplot.show()
                    pyplot.savefig('TODResidPlots/TODResidual_{}_H{}_S{}_C{}.png'.format(prefix, i,j,k), bbox_inches='tight')
                    pyplot.clf()
        cross = np.argmax(Gauss2dNoGrad(np.median(P1[i,0,:,:nParams],axis=0), x, y, 0,0))
        print( cross, nHorns)
        crossings[i] = cross
    return P1, errors, crossings
Ejemplo n.º 2
0
            #    for j in range(nsidebands):
            #        for k in range(nchans):
            #            print(tod.shape, pix.shape, obs.shape)
            #            m[i,j,k,:],a0 = Mapping.Destripe(tod[i,j,k,:], pix, obs, int(5/0.02), int(naxis[0]*naxis[1]))

            # Merge the horns?
            pix = Mapping.ang2pixWCS(wcs, dec.flatten(),
                                     ra.flatten()).astype('int')
            for j in range(nsidebands):
                for k in range(nchans):
                    print(tod.shape, pix.shape, obs.shape)
                    todTemp = tod[:, j, k, :].flatten()
                    obsTemp = np.repeat(obs, tod.shape[0])
                    m[0, j,
                      k, :], a0 = Mapping.Destripe(todTemp, pix, obsTemp,
                                                   int(5 / 0.02),
                                                   int(naxis[0] * naxis[1]))

            m[m == 0] = np.nan
            m = np.reshape(
                m, (m.shape[0], m.shape[1], m.shape[2], naxis[0], naxis[1]))
            m = m[:, :, :, :, ::-1]
            print(m.shape)
            pyplot.subplot(1, 2, 1, projection=wcs)
            pyplot.imshow(m[0, 0, 0, :, :], origin='lower',
                          aspect='auto')  #,(int(naxis[0]), int(naxis[1]) ) ))
            pyplot.colorbar()
            pyplot.plot(ra[0, :],
                        dec[0, :],
                        transform=pyplot.gca().get_transform('world'),
                        alpha=0.1,