Beispiel #1
0
void AspFrame::clear() {
   clearAnnotations();
   getSelTraceList()->clearList();
   getTraceList()->clearList();
   getPeakList()->clearList();
   initPeakFlag(0);
   initDisFlag(0);
   initSpecFlag(0);
   Wclear(2);
}
std::vector<PotentialStar*> StarFinder::findStars(
    std::vector<PotentialStar*>& allobj)
{
    dbg<<"starting FindStars, allobj.size() = "<<allobj.size()<<std::endl;

    // sort allobj by magnitude
    std::sort(allobj.begin(),allobj.end(),std::mem_fun(
            &PotentialStar::isBrighterThan));
    dbg<<"sorted allobj\n";


    // First stage:
    // Split area into sections
    // For each secion find as many stars as possible.
    // Use these stars to fit the variation in rsq across the image.

    // Make bounds of whole region called total_bounds
    Bounds total_bounds;
    const int nobj = allobj.size();
    for(int k=0;k<nobj;++k) total_bounds += allobj[k]->getPos();
    dbg<<"totalbounds = \n"<<total_bounds<<std::endl;

    // boundsar is the 3x3 (ndivx x ndivy) array of quadrant bounds
    // call it qbounds even though it won't be quadrants unless 2x2
    std::vector<Bounds> qbounds = total_bounds.divide(_ndivx,_ndivy);
    xdbg<<"made qbounds\n";

    // probstars will be our first pass list of probable stars
    std::vector<PotentialStar*> probstars;

    // For each section, find the stars and add to probstars
    const int nsection = qbounds.size();

    double frac_nobj=0;
    for (int i=0; i<nobj; ++i) {
        if(isOkSg(allobj[i]->getSg()) && 
           isOkSgMag(allobj[i]->getMag())) frac_nobj++;
    }
    dbg<<"  Found "<<frac_nobj<<" with star-galaxy cuts  "<<std::endl;
    frac_nobj/=nobj;

    bool ignore_sg_cut=false;
    if( frac_nobj < _min_sg_frac ) {
        dbg<<"  Fraction of objects in star-galaxy range too small: "<<
            frac_nobj<<std::endl;
        dbg<<"  Ignoring star-galaxy cut selection"<<std::endl;
        ignore_sg_cut=true;
    }


    if(!ignore_sg_cut) {   
        for(int k=0;k<nobj;++k) {
            if (isOkSg(allobj[k]->getSg()) && isOkSgMag(allobj[k]->getMag())) {
                probstars.insert(probstars.end(),allobj[k]);
            }
        }

        // dummy fit to pass to rejection function
        Legendre2D fit(total_bounds);

        rejectOutliersIter(probstars,_reject1,0.1,fit,true,1e-4,8);
        dbg<<"rejected outliers, now have "<<probstars.size()<<" stars\n";
        std::sort(probstars.begin(),probstars.end(),
            std::mem_fun(&PotentialStar::isBrighterThan));

        dbg<<"Current stars mag/size:"<<std::endl;
        for(size_t k=0;k<probstars.size();++k) {
            dbg<<probstars[k]->getMag()<<" "<<probstars[k]->getSize()<<std::endl;
        }
    } else {
        for(int i=0;i<nsection;++i) {
            dbg<<"i = "<<i<<": bounds = "<<qbounds[i]<<std::endl;

            // someobj are the objects in this section
            // Note that someobj is automatically sorted by magnitude, since
            // allobj was sorted.
            std::vector<PotentialStar*> someobj;
            for(int k=0;k<nobj;++k) {
                if (qbounds[i].includes(allobj[k]->getPos())) 
                    someobj.push_back(allobj[k]);
            }
            dbg<<"added "<<someobj.size()<<" obj\n";

            // Does a really quick and dirty fit to the bright stars
            // Basically it takes the 10 smallest of the 50 brightest objects,
            // finds the peakiest 5, then fits their sizes to a 1st order function.
            // It also gives us a rough value for the sigma
            Legendre2D flinear(qbounds[i]);
            double sigma;
            roughlyFitBrightStars(someobj,&flinear,&sigma);
            dbg<<"fit bright stars: sigma = "<<sigma<<std::endl;

            // Calculate the min and max values of the (adjusted) sizes
            double min_size,max_size;
            findMinMax(someobj,&min_size,&max_size,flinear);
            dbg<<"min,max = "<<min_size<<','<<max_size<<std::endl;

            // Find the objects clustered around the stellar peak.
            std::vector<PotentialStar*> qpeak_list =
                getPeakList(
                    someobj,_bin_size1,min_size,max_size,
                    int(_nstart1*someobj.size()),_min_iter1,_mag_step1,_max_ratio1,
                    true,flinear);
            const int npeak = qpeak_list.size();
            dbg<<"peaklist has "<<npeak<<" stars\n";

            // Remove outliers using a median,percentile rejection scheme.
            // _bin_size1/2. is the minimum value of "sigma".
            rejectOutliers(qpeak_list,_reject1,_bin_size1/2.,flinear);
            dbg<<"rejected outliers, now have "<<npeak<<" stars\n";

            // Use at most 10 (stars_per_bin) stars per region to prevent one region 
            // from dominating the fit.  Use the 10 brightest stars to prevent being
            // position biased (as one would if it were based on size
            int nstars_expected = int(_star_frac * someobj.size());
            if (npeak < nstars_expected) {
                if (npeak < int(0.2 * nstars_expected)) {
                    if (_des_qa) {
                        std::cout<<"STATUS3BEG Warning: Only "<<
                            qpeak_list.size()<<" stars found in section "<<
                            i<<". STATUS3END"<<std::endl;
                    }
                    dbg<<"Warning: only "<<qpeak_list.size()<<
                        " stars found in section "<<i<<
                        "  "<<qbounds[i]<<std::endl;
                }
                probstars.insert(probstars.end(),qpeak_list.begin(),qpeak_list.end());
            } else {
                std::sort(qpeak_list.begin(),qpeak_list.end(),
                          std::mem_fun(&PotentialStar::isBrighterThan));
                probstars.insert(probstars.end(),qpeak_list.begin(),
                                 qpeak_list.begin()+nstars_expected);
            }
            dbg<<"added to probstars\n";
        }
        xdbg<<"done qbounds loop\n";
    }
    int nstars = probstars.size();
    dbg<<"nstars = "<<nstars<<std::endl;

    // Now we have a first estimate of which objects are stars.
    // Fit a quadratic function to them to characterize the variation in size
    Legendre2D f(total_bounds);
    double sigma;
    fitStellarSizes(&f,_fit_order,_fit_sig_clip,probstars,&sigma);

    // Second stage:
    // Use the fitted function for the size we just got to adjust
    // the measured sizes according to their position in the image.
    // Make the histogram using measured - predicted sizes (still log
    // size actually) which should then have the peak very close to 0.
    // Set the bin size for this new histogram to be the rms scatter of the
    // stellar peak from pass 1.
    // This time step by 0.25 mag (mag_step2).

    // Find the values of min_size,max_size for the whole thing with the new f
    double min_size,max_size;
    findMinMax(allobj,&min_size,&max_size,f);
    dbg<<"new minmax = "<<min_size<<','<<max_size<<std::endl;

    // Find the objects clustered around the stellar peak.
    // Note that the bin_size is a set fraction of sigma (0.5), so this
    // should make the stellar peak clearer.
    // Also, the f at the end means the functional fitted size will be
    // subtracted off before adding to the histogram.
    probstars = getPeakList(
        allobj,0.5*sigma,min_size,max_size,
        int(_nstart2*allobj.size()),_min_iter2,_mag_step2,_purity_ratio,false,f);
    nstars = probstars.size();
    dbg<<"probstars has "<<nstars<<" stars\n";

    // Remove outliers using a iterative median,percentile rejection scheme.
    rejectOutliersIter(probstars,_reject2,0.1,f,false);
    dbg<<"rejected outliers, now have "<<probstars.size()<<" stars\n";
    std::sort(probstars.begin(),probstars.end(),
              std::mem_fun(&PotentialStar::isBrighterThan));
    

    // If you get a bad fit the first time through, it can take a 
    // few passes to fix it.
    // And always do at least one refit.
    bool refit = true;
    for(int iter=0;refit && iter<_max_refit_iter;++iter) {
        dbg<<"starting refit\n"<<std::endl;
        refit = false;
        std::vector<std::vector<PotentialStar*> > stars_list(nsection);

        // Add each star to the appropriate sublist
        for(int k=0;k<nstars;++k) {
            for(int i=0;i<nsection;++i) {
                if(qbounds[i].includes(probstars[k]->getPos()))
                    stars_list[i].push_back(probstars[k]);
            }
        }

        // Make fit_list, use sg cuts if enough objects otherwise use the 
        // list of the 10 brightest stars per section
        std::vector<PotentialStar*> fit_list;

        if(!ignore_sg_cut) {
            for(int k=0;k<nobj;++k) {

                if (isOkSg(allobj[k]->getSg()) && isOkSgMag(allobj[k]->getMag())) {
                    fit_list.insert(fit_list.end(),allobj[k]);
                }
            }
            rejectOutliersIter(fit_list,_reject1,0.1,f,false);
            dbg<<"rejected outliers, now have "<<fit_list.size()<<" stars\n";
            std::sort(fit_list.begin(),fit_list.end(),
                      std::mem_fun(&PotentialStar::isBrighterThan));
            


        } else {

            dbg<<"  Fraction of objects in star-galaxy range too small "<<std::endl;
            for(int i=0;i<nsection;++i) {
                // if there are still < 10 stars, give a warning, and
                // just add all the stars to fit_list
                if (int(stars_list[i].size()) < _stars_per_bin) {
                    if (_des_qa) {
                        std::cout<<"STATUS3BEG Warning: Only "<<
                            stars_list[i].size()<<" stars in section "<<i<<
                            ". STATUS3END"<<std::endl;
                    }
                    dbg<<"Warning: only "<<stars_list[i].size()<<
                        " stars in section ";
                    dbg<<i<<"  "<<qbounds[i]<<std::endl;
                    fit_list.insert(fit_list.end(),stars_list[i].begin(),
                                    stars_list[i].end());
                    refit = true;
                } else {
                    // sort the sublist by magnitude
                    std::sort(stars_list[i].begin(),stars_list[i].end(),
                              std::mem_fun(&PotentialStar::isBrighterThan));

                    // add the brightest 10 to fit_list
                    fit_list.insert(fit_list.end(),stars_list[i].begin(),
                                    stars_list[i].begin()+_stars_per_bin);
                }
            }
        }

        // Do all the same stuff as before (refit f, get new min,max,
        // find the peak_list again, and reject the outliers)
        nstars = fit_list.size();
        fitStellarSizes(&f,_fit_order,_fit_sig_clip,fit_list,&sigma);
        findMinMax(allobj,&min_size,&max_size,f);
        dbg<<"new minmax = "<<min_size<<','<<max_size<<std::endl;
        probstars = getPeakList(
            allobj,0.5*sigma,min_size,max_size,
            int(_nstart2*allobj.size()),_min_iter2,_mag_step2,
            _purity_ratio,false,f);
        dbg<<"probstars has "<<probstars.size()<<" stars\n";
        rejectOutliersIter(probstars,_reject2,0.1,f,false);
        dbg<<"rejected outliers, now have "<<probstars.size()<<" stars\n";
        if (int(probstars.size()) < nstars) {
            refit = true;
            dbg<<"fewer than "<<nstars<<" - so refit\n";
        }
        nstars = probstars.size();
    }

    dbg<<"done FindStars\n";

    for(int i=0;i<nstars;++i) {
        xdbg<<"stars["<<i<<"]: size = "<<probstars[i]->getSize();
        xdbg<<", size - f(pos) = ";
        xdbg<<probstars[i]->getSize()-f(probstars[i]->getPos())<<std::endl;
        xdbg<<probstars[i]->getLine()<<std::endl;
    }
    return probstars;
}