74 {
75
76 KernelCandidate<PixelT> *kCandidate = dynamic_cast<KernelCandidate<PixelT> *>(candidate);
77 if (kCandidate == NULL) {
79 "Failed to cast SpatialCellCandidate to KernelCandidate");
80 }
81 if (!(kCandidate->isInitialized())) {
82 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
83 LOGL_DEBUG(
"TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
84 "Cannot process candidate %d, continuing", kCandidate->getId());
85 return;
86 }
87
88 LOGL_DEBUG(
"TRACE1.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
89 "Processing candidate %d", kCandidate->getId());
90
91
92
93
94
95
97 double kSum = _spatialKernel->
computeImage(kImage,
false,
98 kCandidate->getXCenter(), kCandidate->getYCenter());
101
102
103 double background = (*_spatialBackground)(kCandidate->getXCenter(), kCandidate->getYCenter());
104
105 MaskedImageT diffim = kCandidate->getDifferenceImage(kernelPtr, background);
106
108 kImage.
writeFits(str(boost::format(
"askv_k%d.fits") % kCandidate->getId()));
109 diffim.writeFits(str(boost::format("askv_d%d.fits") % kCandidate->getId()));
110 }
111
112
113 try {
114 if (_useCoreStats)
115 _imstats.apply(diffim, _coreRadius);
116 else
117 _imstats.apply(diffim);
119 LOGL_DEBUG(
"TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
120 "Unable to calculate imstats for Candidate %d", kCandidate->getId());
121 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
122 return;
123 }
124
125 _nProcessed += 1;
126
127 LOGL_DEBUG(
"TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
128 "Chi2 = %.3f", _imstats.getVariance());
129 LOGL_DEBUG(
"TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
130 "X = %.2f Y = %.2f",
131 kCandidate->getXCenter(),
132 kCandidate->getYCenter());
133 LOGL_DEBUG(
"TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
134 "Kernel Sum = %.3f", kSum);
135 LOGL_DEBUG(
"TRACE4.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
136 "Background = %.3f", background);
137 LOGL_DEBUG(
"TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
138 "Candidate %d resids = %.3f +/- %.3f sigma (%d pix)",
139 kCandidate->getId(),
140 _imstats.getMean(),
141 _imstats.getRms(),
142 _imstats.getNpix());
143
144 bool meanIsNan =
std::isnan(_imstats.getMean());
145 bool rmsIsNan =
std::isnan(_imstats.getRms());
146 if (meanIsNan || rmsIsNan) {
147 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
148 LOGL_DEBUG(
"TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
149 "Rejecting candidate %d, encountered NaN",
150 kCandidate->getId());
151 _nRejected += 1;
152 return;
153 }
154
155 if (_ps->getAsBool("spatialKernelClipping")) {
156 if (
fabs(_imstats.getMean()) > _ps->getAsDouble(
"candidateResidualMeanMax")) {
157 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
158 LOGL_DEBUG(
"TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
159 "Rejecting candidate %d; bad mean residual : |%.3f| > %.3f",
160 kCandidate->getId(),
161 _imstats.getMean(),
162 _ps->getAsDouble("candidateResidualMeanMax"));
163 _nRejected += 1;
164 }
165 else if (_imstats.getRms() > _ps->getAsDouble("candidateResidualStdMax")) {
166 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
167 LOGL_DEBUG(
"TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
168 "Rejecting candidate %d; bad residual rms : %.3f > %.3f",
169 kCandidate->getId(),
170 _imstats.getRms(),
171 _ps->getAsDouble("candidateResidualStdMax"));
172 _nRejected += 1;
173 }
174 else {
175 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
176 LOGL_DEBUG(
"TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
177 "Spatial kernel OK");
178 _nGood += 1;
179 }
180 }
181 else {
182 kCandidate->setStatus(afwMath::SpatialCellCandidate::GOOD);
183 LOGL_DEBUG(
"TRACE5.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
184 "Sigma clipping not enabled");
185 _nGood += 1;
186 }
187
188
189 if (!(_useCoreStats)) {
190 try {
191 _imstats.apply(diffim, _coreRadius);
193 LOGL_DEBUG(
"TRACE2.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
194 "Unable to calculate core imstats for Candidate %d",
195 kCandidate->getId());
196 kCandidate->setStatus(afwMath::SpatialCellCandidate::BAD);
197 return;
198 }
199 LOGL_DEBUG(
"TRACE3.ip.diffim.AssessSpatialKernelVisitor.processCandidate",
200 "Candidate %d core resids = %.3f +/- %.3f sigma (%d pix)",
201 kCandidate->getId(),
202 _imstats.getMean(),
203 _imstats.getRms(),
204 _imstats.getNpix());
205 }
206 }
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
A class to represent a 2-dimensional array of pixels.
void writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > imageMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > maskMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > varianceMetadata=std::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
A kernel created from an Image.
lsst::geom::Extent2I const getDimensions() const
Return the Kernel's dimensions (width, height)
double computeImage(lsst::afw::image::Image< Pixel > &image, bool doNormalize, double x=0.0, double y=0.0) const
Compute an image (pixellized representation of the kernel) in place.
Provides consistent interface for LSST exceptions.
Reports errors in the logical structure of the program.