95 def __init__(self, config, name, schema, metadata, logName=None):
98 super().
__init__(config, name, schema, metadata, logName=logName)
101 self.
keyRa = schema.addField(name +
"_ra", type=
"D", doc=
"Trail centroid right ascension.")
102 self.
keyDec = schema.addField(name +
"_dec", type=
"D", doc=
"Trail centroid declination.")
103 self.
keyRaErr = schema.addField(name +
"_raErr", type=
"D",
104 doc=
"Trail centroid right ascension error.", units=
"arcsec")
105 self.
keyDecErr = schema.addField(name +
"_decErr", type=
"D",
106 doc=
"Trail centroid declination error.", units=
"arcsec")
107 self.
keyX0 = schema.addField(name +
"_x0", type=
"D", doc=
"Trail head X coordinate.", units=
"pixel")
108 self.
keyY0 = schema.addField(name +
"_y0", type=
"D", doc=
"Trail head Y coordinate.", units=
"pixel")
109 self.
keyX1 = schema.addField(name +
"_x1", type=
"D", doc=
"Trail tail X coordinate.", units=
"pixel")
110 self.
keyY1 = schema.addField(name +
"_y1", type=
"D", doc=
"Trail tail Y coordinate.", units=
"pixel")
111 self.
keyFlux = schema.addField(name +
"_flux", type=
"D", doc=
"Trailed source flux.", units=
"count")
112 self.
keyLength = schema.addField(name +
"_length", type=
"D", doc=
"Trail length.", units=
"pixel")
113 self.
keyAngle = schema.addField(name +
"_angle", type=
"D", doc=
"Angle measured from +x-axis.")
116 self.
keyX0Err = schema.addField(name +
"_x0Err", type=
"D",
117 doc=
"Trail head X coordinate error.", units=
"pixel")
118 self.
keyY0Err = schema.addField(name +
"_y0Err", type=
"D",
119 doc=
"Trail head Y coordinate error.", units=
"pixel")
120 self.
keyX1Err = schema.addField(name +
"_x1Err", type=
"D",
121 doc=
"Trail tail X coordinate error.", units=
"pixel")
122 self.
keyY1Err = schema.addField(name +
"_y1Err", type=
"D",
123 doc=
"Trail tail Y coordinate error.", units=
"pixel")
124 self.
keyFluxErr = schema.addField(name +
"_fluxErr", type=
"D",
125 doc=
"Trail flux error.", units=
"count")
127 doc=
"Trail length error.", units=
"pixel")
128 self.
keyAngleErr = schema.addField(name +
"_angleErr", type=
"D", doc=
"Trail angle error.")
130 doc=
"Algorithm key indicating which algorithm is "
131 "used to measure trailed source.")
134 self.
FAILURE = flagDefs.addFailureFlag(
"No trailed-source measured")
135 self.
NO_FLUX = flagDefs.add(
"flag_noFlux",
"No suitable prior flux measurement")
136 self.
NO_CONVERGE = flagDefs.add(
"flag_noConverge",
"The root finder did not converge")
137 self.
NO_SIGMA = flagDefs.add(
"flag_noSigma",
"No PSF width (sigma)")
138 self.
EDGE = flagDefs.add(
"flag_edge",
"Trail contains edge pixels")
139 self.
OFFIMAGE = flagDefs.add(
"flag_off_image",
"Trail extends off image")
140 self.
NAN = flagDefs.add(
"flag_nan",
"One or more trail coordinates are missing")
142 "Trail length is greater than three times the psf radius")
143 self.
SHAPE = flagDefs.add(
"flag_shape",
"Shape flag is set, trail length not calculated")
145 "Centroid flag is set, trail length not calculated")
148 self.
log = logging.getLogger(self.logName)
151 """Run the Naive trailed source measurement algorithm.
155 measRecord : `lsst.afw.table.SourceRecord`
156 Record describing the object being measured.
157 exposure : `lsst.afw.image.Exposure`
158 Pixel data to be measured.
162 lsst.meas.base.SingleFramePlugin.measure
165 use_sdss_shape =
True
166 if measRecord[
'base_SdssShape_flag']:
167 if measRecord.getShapeFlag():
168 self.
log.debug(
"HSM shape flag is also set for measRecord: %s. Trail measurement "
169 "will not be made. All trail values will be set to nan.", measRecord.getId())
174 use_sdss_shape =
False
177 "SDSS Shape flag is set for measRecord: %s. Falling back"
178 "to HSMshape. No error measurements will be made.", measRecord.getId())
179 xc = measRecord[
"slot_Shape_x"]
180 yc = measRecord[
"slot_Shape_y"]
181 Ixx, Iyy, Ixy = measRecord.getShape().getParameterVector()
185 xc = measRecord[
"base_SdssShape_x"]
186 yc = measRecord[
"base_SdssShape_y"]
187 Ixx = measRecord[
"base_SdssShape_xx"]
188 Iyy = measRecord[
"base_SdssShape_yy"]
189 Ixy = measRecord[
"base_SdssShape_xy"]
191 if not np.isfinite(xc)
or not np.isfinite(yc):
202 a2 = 0.5 * (xpy +
sqrt(xmy2 + 4.0*xy2))
203 b2 = 0.5 * (xpy -
sqrt(xmy2 + 4.0*xy2))
206 length, gradLength, results = self.
findLength(a2, b2)
207 if not results.converged:
208 self.
log.info(
"Results not converged: %s", results.flag)
214 theta = 0.5 * np.arctan2(2.0 * Ixy, xmy)
218 dydtheta = radius*np.cos(theta)
219 dxdtheta = radius*np.sin(theta)
225 self.
check_trail(measRecord, exposure, x0, y0, x1, y1, length)
228 cutout = getMeasurementCutout(measRecord, exposure)
231 params = np.array([xc, yc, 1.0, length, theta])
233 flux, gradFlux = model.computeFluxWithGradient(params)
236 flux_is_fallback =
False
237 if (
not np.isfinite(flux)) | (np.abs(flux) > self.config.maxFlux):
238 if np.isfinite(measRecord.getApInstFlux()):
239 flux = measRecord.getApInstFlux()
240 flux_is_fallback =
True
252 IxxErr2 = measRecord[
"base_SdssShape_xxErr"]**2
253 IyyErr2 = measRecord[
"base_SdssShape_yyErr"]**2
254 IxyErr2 = measRecord[
"base_SdssShape_xyErr"]**2
257 xcErr2 = measRecord[
"base_SdssCentroid_xErr"]**2
258 ycErr2 = measRecord[
"base_SdssCentroid_yErr"]**2
261 wcs = exposure.getWcs()
262 jacobian = wcs.linearizePixelToSky(
Point2D(xc, yc), arcseconds).getLinear().getMatrix()
263 raErr =
sqrt(jacobian[0, 0]**2 * xcErr2 + jacobian[0, 1]**2 * ycErr2)
264 decErr =
sqrt(jacobian[1, 0]**2 * xcErr2 + jacobian[1, 1]**2 * ycErr2)
267 desc =
sqrt(xmy2 + 4.0*xy2)
268 da2dIxx = 0.5*(1.0 + (xmy/desc))
269 da2dIyy = 0.5*(1.0 - (xmy/desc))
270 da2dIxy = 2.0*Ixy / desc
271 a2Err2 = IxxErr2*da2dIxx*da2dIxx + IyyErr2*da2dIyy*da2dIyy + IxyErr2*da2dIxy*da2dIxy
272 b2Err2 = IxxErr2*da2dIyy*da2dIyy + IyyErr2*da2dIxx*da2dIxx + IxyErr2*da2dIxy*da2dIxy
273 dLda2, dLdb2 = gradLength
274 lengthErr = np.sqrt(dLda2*dLda2*a2Err2 + dLdb2*dLdb2*b2Err2)
277 dThetadIxx = -Ixy / (xmy2 + 4.0*xy2)
278 dThetadIxy = xmy / (xmy2 + 4.0*xy2)
279 thetaErr =
sqrt(dThetadIxx*dThetadIxx*(IxxErr2 + IyyErr2) + dThetadIxy*dThetadIxy*IxyErr2)
282 if not flux_is_fallback:
283 dFdxc, dFdyc, _, dFdL, dFdTheta = gradFlux
284 fluxErr =
sqrt(dFdL*dFdL*lengthErr*lengthErr + dFdTheta*dFdTheta*thetaErr*thetaErr
285 + dFdxc*dFdxc*xcErr2 + dFdyc*dFdyc*ycErr2)
290 dxdradius = np.cos(theta)
291 dydradius = np.sin(theta)
292 radiusErr2 = lengthErr*lengthErr/4.0
294 xErr2 =
sqrt(xcErr2 + radiusErr2*dxdradius*dxdradius + thetaErr*thetaErr*dxdtheta*dxdtheta)
295 yErr2 =
sqrt(ycErr2 + radiusErr2*dydradius*dydradius + thetaErr*thetaErr*dydtheta*dydtheta)
300 measRecord.set(self.
keyRaErr, raErr)
302 measRecord.set(self.
keyX0Err, x0Err)
303 measRecord.set(self.
keyY0Err, y0Err)
304 measRecord.set(self.
keyX1Err, x0Err)
305 measRecord.set(self.
keyY1Err, y0Err)
311 measRecord.set(self.
keyRa, ra)
312 measRecord.set(self.
keyDec, dec)
313 measRecord.set(self.
keyX0, x0)
314 measRecord.set(self.
keyY0, y0)
315 measRecord.set(self.
keyX1, x1)
316 measRecord.set(self.
keyY1, y1)
317 measRecord.set(self.
keyFlux, flux)
319 measRecord.set(self.
keyAngle, theta)
321 def check_trail(self, measRecord, exposure, x0, y0, x1, y1, length):
322 """ Set flags for edge pixels, off chip, and nan trail coordinates and
323 flag if trail length is three times larger than psf.
325 Check if the coordinates of the beginning and ending of the trail fall
326 inside the exposures bounding box. If not, set the off_chip flag.
327 If the beginning or ending falls within a pixel marked as edge, set the
328 edge flag. If any of the coordinates happens to fall on a nan, then
330 Additionally, check if the trail is three times larger than the psf. If
331 so, set the suspect trail flag.
335 measRecord: `lsst.afw.MeasurementRecord`
336 Record describing the object being measured.
337 exposure: `lsst.afw.Exposure`
338 Pixel data to be measured.
341 x coordinate of the beginning of the trail.
343 y coordinate of the beginning of the trail.
345 x coordinate of the end of the trail.
347 y coordinate of the end of the trail.
354 if np.isnan(x_coords).any()
or np.isnan(y_coords).any():
356 x_coords = [x
for x
in x_coords
if not np.isnan(x)]
357 y_coords = [y
for y
in y_coords
if not np.isnan(y)]
360 if not (all(exposure.getBBox().beginX <= x <= exposure.getBBox().endX
for x
in x_coords)
361 and all(exposure.getBBox().beginY <= y <= exposure.getBBox().endY
for y
in y_coords)):
367 for (x_val, y_val)
in zip(x_coords, y_coords):
368 if x_val
is not np.nan
and y_val
is not np.nan:
369 if exposure.mask[
Point2I(int(x_val),
370 int(y_val))] & exposure.mask.getPlaneBitMask(
'EDGE') != 0:
374 elif not (all(exposure.getBBox().beginX <= x <= exposure.getBBox().endX
for x
in x_coords)
375 and all(exposure.getBBox().beginY <= y <= exposure.getBBox().endY
for y
in y_coords)):
382 if exposure.mask[
Point2I(int(x0), int(y0))]
and exposure.mask[
Point2I(int(x1), int(y1))]:
383 if ((exposure.mask[
Point2I(int(x0), int(y0))] & exposure.mask.getPlaneBitMask(
'EDGE') != 0)
384 or (exposure.mask[
Point2I(int(x1), int(y1))]
385 & exposure.mask.getPlaneBitMask(
'EDGE') != 0)):
388 psfShape = exposure.psf.computeShape(exposure.getBBox().getCenter())
389 psfRadius = psfShape.getDeterminantRadius()
391 if length > psfRadius*3.0: