LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
SizeMagnitudeStarSelector.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 #include "lsst/pex/logging.h"
25 #include "lsst/afw/image/Calib.h"
30 
31 namespace pexLogging = lsst::pex::logging;
32 
33 namespace lsst { namespace meas { namespace algorithms {
34 
35 // All of the functionality is imported from shapelet::SizeMagnitudeStarSelector
36 // Just repeat the constructor and destructor.
38 {
42 
43 public :
44  SizeMagnitudeStarSelectorImpl(ConfigFile & params, const Policy& policy) :
45  base(params,""),
46  _aperture(policy.getDouble("aperture"))
47  {}
48 
50 
51  double getAperture() const { return _aperture; }
52 
53 private :
54  // This parameter is used by the surface layer, not SizeMagnitudeStarSelectorAlgo.
55  double _aperture;
56 };
57 
59 {
61 
62  // Convert Policy info into my ConfigFile format:
63  ConfigFile params;
64  params["minsize"] = policy.getDouble("minsize");
65  params["maxsize"] = policy.getDouble("maxsize");
66  params["logsize"] = policy.getBool("logsize");
67  params["minmag"] = policy.getDouble("minmag");
68  params["maxmag"] = policy.getDouble("maxmag");
69  params["starfrac"] = policy.getDouble("starfrac");
70  params["startn1"] = policy.getDouble("startn1");
71  params["fitorder"] = policy.getInt("fitorder");
72  params["fitsigclip"] = policy.getDouble("fitsigclip");
73  params["starsperbin"] = policy.getInt("starsperbin");
74  params["purityratio"] = policy.getDouble("purityratio");
75 
76  // The rest of these are just given defaults.
77  // In my experience, there is not much reason to make these
78  // run-time modifiable.
79  params["maxoutmag"] = params["maxmag"];
80  params["ndivx"] = 1;
81  params["ndivy"] = 1;
82  params["magstep1"] = 0.25;
83  params["magstep2"] = 0.1;
84  params["reject1"] = params["fitsigclip"];
85  params["reject2"] = params["fitsigclip"];
86  params["maxratio1"] = 3.0 * double(params["purityratio"]);
87  params["binsize1"] = 0.1;
88  params["minbinsize"] = 0.01;
89  params["miniter1"] = 3;
90  params["miniter2"] = 2;
91  params["startn2"] = 3 * int(params["startn1"]);
92  params["okvalcount"] = 2;
93  params["maxrms"] = 0.05;
94  params["maxrefititer"] = 5;
95 
96  pImpl = boost::shared_ptr<SizeMagnitudeStarSelectorImpl>(new SizeMagnitudeStarSelectorImpl(params, policy));
97 }
98 
100  const SourceRecord & source,
101  const Exposure & exposure) const
102 {
103  double sigma = sqrt(source.getIxx() + source.getIyy());
104  if (!(sigma > 0.)) return -1.;
105  Shapelet shape(4, sigma);
106  double x = source.getX();
107  double y = source.getY();
108  PointD pos(x, y);
109  if (shape.measureFromImage(
110  source, pos, false, false, pImpl->getAperture(), exposure)) {
111  return shape.getSigma();
112  } else {
113  return -1.;
114  }
115 }
116 
117 /*
118  * Calculates a magnitude for a source.
119  *
120  * The star finder is written in terms of using magnitudes rather than
121  * fluxes, whereas Source stores fluxes; So this just translates the flux into a magnitude.
122  *
123  * Note This function may also be a good candidate for a having
124  * its action be specifiable by a Policy parameter.
125  */
126 static double calculateSourceMagnitude(lsst::afw::table::SourceRecord const & source,
128  )
129 {
130  return exposure.getCalib()->getMagnitude(source.getApFlux());
131 }
132 
134  const Exposure& exposure,
135  const SourceCatalog & sourceList,
137 ) const {
138  pexLogging::Debug traceLog("meas.algorithms.SizeMagnitudeStarSelector"); // trace output goes here
139  const unsigned int MIN_OBJ_TO_TRY = 30;
140 
141  typedef Exposure::MaskedImageT MaskedImage;
142  std::vector<shapelet::PotentialStar*> maybeStars;
143 
144  // First get a list of potential stars
145  const int nSources = sourceList.size();
146  traceLog.debug<4>("%d candidate stars", nSources);
147  for (int i=0; i<nSources; ++i) {
148  double const x = sourceList[i].getX();
149  double const y = sourceList[i].getY();
150  double const size = calculateSourceSize(sourceList[i], exposure);
151  double const mag = calculateSourceMagnitude(sourceList[i], exposure);
152 
153  shapelet::Position pos(x, y);
154 
155  // Range checking
156  bool ok = true;
157  if (!pImpl->isOkSize(size)) {
158  ok = false;
159  }
160  if (ok && !pImpl->isOkMag(mag)) {
161  ok = false;
162  }
163  traceLog.debug<5>("i, x, y, size, mag = %d %.1f, %.1f %g %g: %d", i, x, y, size, mag, ok);
164 
165  if (ok) {
166  double logSize = pImpl->convertToLogSize(size);
167  maybeStars.push_back(new shapelet::PotentialStar(pos, mag, logSize, i, ""));
168  }
169  }
170  traceLog.debug<4>("Total potential stars = %d", maybeStars.size());
171  if (maybeStars.size() < MIN_OBJ_TO_TRY) {
172  // Too few objects for algorithm to have any chance of producing reasonable output.
174  str(boost::format("Only %d viable objects for star selection. "
175  "This algorithm needs at least %d objects to try to find stars")
176  % maybeStars.size() % MIN_OBJ_TO_TRY));
177 
178  return PsfCandidateList();
179  }
180 
181  // Run the actual algorithm
182  std::vector<shapelet::PotentialStar*> stars = pImpl->findStars(maybeStars);
183  traceLog.debug<4>("Identified %d stars", stars.size());
184 
185  // Convert the results into a PsfCandidateList
186  //MaskedImage::ConstPtr imagePtr = MaskedImage::ConstPtr(new MaskedImage(exposure.getMaskedImage(), false));
187  CONST_PTR(Exposure) expPtr = CONST_PTR(Exposure)(new Exposure(exposure, false));
188  PsfCandidateList psfCandidateList;
189  const int nStars = stars.size();
190  for (int k=0; k<nStars; ++k) {
191  if (pImpl->isOkOutputMag(stars[k]->getMag())) {
192  int i=stars[k]->getIndex();
193  PTR(PsfCandidateT) psfCandidate(new PsfCandidateT(
194  sourceList.get(i),
195  expPtr,
196  stars[k]->getPos().getX(),
197  stars[k]->getPos().getY()));
198  psfCandidateList.push_back(psfCandidate);
199  }
200  }
201 
202  return psfCandidateList;
203 }
204 
205 }}} // namespace lsst::meas::algorithms
int y
int getInt(const std::string &name) const
Definition: Policy.h:603
boost::shared_ptr< SizeMagnitudeStarSelectorImpl > pImpl
bool getBool(const std::string &name) const
Definition: Policy.h:589
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:48
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
boost::shared_ptr< Calib > getCalib()
Return the Exposure&#39;s Calib object.
Definition: Exposure.h:217
double getY() const
Return the centroid slot y coordinate.
Definition: Source.h:933
static const int WARN
Definition: Log.h:177
#define PTR(...)
Definition: base.h:41
Defines the Shapelet class.
bool measureFromImage(const Source &source, const PointD &pos, bool isCentroidFixed, bool isSigmaFixed, double aperture, const Exposure &exposure, const MaskPixel okmask=0)
measure shapelet decomposition of an image
Definition: Shapelet.cc:358
void log(int importance, const std::string &message, const lsst::daf::base::PropertySet &properties)
std::vector< ReferenceMatch > ReferenceMatchVector
Definition: fwd.h:97
double getSigma() const
get the scale size of the shapelet
Definition: Shapelet.cc:173
PsfCandidateList selectStars(const Exposure &exposure, const SourceCatalog &sourceList, boost::shared_ptr< afw::table::ReferenceMatchVector const > matches=boost::shared_ptr< afw::table::ReferenceMatchVector const >()) const
Find a set of stars from an input list of Sources.
double getX() const
Return the centroid slot x coordinate.
Definition: Source.h:930
size_type size() const
Return the number of elements in the catalog.
Definition: Catalog.h:401
static Log & getDefaultLog()
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:43
Custom catalog class for record/table subclasses that are guaranteed to have an ID, and should generally be sorted by that ID.
Definition: fwd.h:55
SizeMagnitudeStarSelector(const Policy &policy)
Create a size-magnitude star selector from a policy.
A module for determining which objects are good PSF stars.
double x
lsst::meas::algorithms::PsfCandidate< PixelT > PsfCandidateT
boost::shared_ptr< RecordT > const get(size_type i) const
Return a pointer to the record at index i.
Definition: Catalog.h:439
double getDouble(const std::string &name) const
Definition: Policy.h:617
void debug(int verbosity, const std::string &message)
Definition: Debug.h:104
double calculateSourceSize(const SourceRecord &source, const Exposure &exposure) const
Calculates a robust size measurement for a source.
SizeMagnitudeStarSelectorImpl(ConfigFile &params, const Policy &policy)
#define CONST_PTR(...)
Definition: base.h:47
Class to ensure constraints for spatial modeling.
Record class that contains measurements made on a single exposure.
Definition: Source.h:81
double getIyy() const
Return the shape slot Iyy value.
Definition: Source.h:939
Class stored in SpatialCells for spatial Psf fitting.
Definition: PsfCandidate.h:57
FluxSlotDefinition::MeasValue getApFlux() const
Get the value of the ApFlux slot measurement.
Definition: Source.h:867
double getIxx() const
Return the shape slot Ixx value.
Definition: Source.h:936