Ejemplo n.º 1
0
    def plot_matches(self, type, index=None, data=None, nmatchmin=5):
        if data is None:
            data=self.read_matched(type)

        if index is None:
            index=numpy.arange(data.size, dtype='i4')
        else:
            index=numpy.array(index, ndmin=1, dtype='i4')

        mspec = data['mspec'][index]
        h,rev = eu.stat.histogram(mspec, binsize=1, rev=True)

        for i in xrange(h.size):
            if rev[i] != rev[i+1]:
                w=rev[ rev[i]:rev[i+1] ]

                if w.size >= nmatchmin:
                    cmag = data['cmodelmag_dered_r'][w]
                    cmagerr = data['cmodelmag_dered_err_r'][w]
                    flux, ivar = sdsspy.mag2nmgy(cmag, cmagerr)
                    err=sqrt(1./ivar)

                    minf = (flux-3.5*err).min()
                    maxf = (flux+3.5*err).max()
                    fvals = linspace(minf, maxf, 100)

                    plt=biggles.FramedPlot()

                    for wi in xrange(w.size):
                        if data['survey_primary'][w[wi]] == 1:
                            color='red'
                            pmag = cmag[wi]
                        else:
                            color='black'
                        g = exp(-0.5*( (fvals-flux[wi])/err[wi])**2)/sqrt(2.*PI)/err[wi]
                        gcurve = biggles.Curve(fvals, g, color=color)
                        plt.add(gcurve)

                    #std = cmag.std()
                    #binsize=std*0.4
                    #plt=eu.plotting.bhist(cmag, binsize=binsize, show=False)

                    nlab=biggles.PlotLabel(0.05,0.95,'nmatch: %s' % w.size, halign='left')
                    maglab = biggles.PlotLabel(0.05,0.9,'primary mag: %0.2f' % pmag, halign='left')
                    plt.add(nlab, maglab)
                    plt.title=type
                    plt.xlabel = 'flux'
                    plt.show()

                    k=raw_input('hit a key (q to quit): ')
                    if k.lower() == 'q':
                        return
                else:
                    print("less than %s matches" % nmatchmin)
Ejemplo n.º 2
0
    def extract_good_matches(self, ra, dec, cmag_r, cmag_err_r, survey_primary, htminfo,
                             spec_ra, spec_dec, 
                             nsigma=2.5, matchrad=1./3600.):
        """
        Match by ra/dec.

        Demand one of the matches is survey_primary, and use it as a reference
        point to remove outliers at nsigma in flux.

        Choosing nsigma=2.5 and 1'' match radius to make sure we get good matches

        """

        if survey_primary is None:
            maxmatch=1
        else:
            maxmatch=100
        print("Matching to %0.2f arcsec" % (matchrad*3600.,))
        h=eu.htm.HTM(10)

        mspec,mphot,d12 = h.match(spec_ra,spec_dec, ra, dec,
                                  matchrad,
                                  htmid2=htminfo['htmid'], 
                                  minid=htminfo['minid'],
                                  maxid=htminfo['maxid'],
                                  htmrev2=htminfo['htmrev'],
                                  maxmatch=maxmatch)

        print("Matched: %s/%s" % (mspec.size,spec_ra.size))
        if mspec.size == 0 or survey_primary==None:
            return mspec, mphot


        print("  unique:",numpy.unique(mspec).size)
        print("  getting good flux matches")

        h,rev = eu.stat.histogram(mspec, binsize=1, rev=True)
        keep = numpy.zeros(mspec.size, dtype='i1')

        for i in xrange(h.size):
            if rev[i] != rev[i+1]:

                # indices of mspec/mphot that have a particular index into
                # mspec
                w=rev[ rev[i]:rev[i+1] ]


                # make sure this makes sense
                wbad=where1(mspec[w] != mspec[w[0]])
                if wbad != 0:
                    raise ValueError("expected all indices to have same mspec")
                
                # indices into the photometric sample
                wphot=mphot[w]
                flux, ivar = sdsspy.mag2nmgy(cmag_r[wphot], cmag_err_r[wphot])

                werr=where1(ivar > 0.)
                if werr.size > 0:
                    # reduce the indices into mspec/mphot
                    w=w[werr]

                    # trim photometric data
                    flux=flux[werr]
                    ivar=ivar[werr]
                    wphot=wphot[werr]


                    # demand a survey primary match
                    wp = where1(survey_primary[wphot] == 1)
                    if wp.size > 0:
                        # always keep the primary
                        keep[w[wp]] = 1

                        wphot_primary = wphot[wp[0]]


                        # flux in primary will be reference
                        pflux, pivar = sdsspy.mag2nmgy(cmag_r[wphot_primary], 
                                                       cmag_err_r[wphot_primary])

                        # nsigma in the combined error
                        err = sqrt(1./ivar)
                        perr = sqrt(1./pivar)

                        comb_err = sqrt(err**2 + perr**2)

                        wgood = where1( abs(flux-pflux) < nsigma*comb_err)
                        if wgood.size > 0:
                            w=w[wgood]
                            keep[w] = 1

        wkeep = where1(keep == 1)
        print("  kept: %s/%s" % (wkeep.size, keep.size))
        if wkeep.size > 0:
            mspec = mspec[wkeep]
            mphot = mphot[wkeep]
        else:
            mspec = numpy.array([],dtype='i4')
            mphot = numpy.array([],dtype='i4')

        return mspec, mphot