Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

# This file is part of meas_astrom. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

__all__ = ["FitSipDistortionTask", "FitSipDistortionConfig"] 

 

 

import lsst.sphgeom 

import lsst.pipe.base 

import lsst.geom 

import lsst.afw.image 

import lsst.afw.geom 

import lsst.afw.display 

 

from .scaledPolynomialTransformFitter import ScaledPolynomialTransformFitter, OutlierRejectionControl 

from .sipTransform import SipForwardTransform, SipReverseTransform, makeWcs 

from .makeMatchStatistics import makeMatchStatisticsInRadians 

 

from .setMatchDistance import setMatchDistance 

 

 

class FitSipDistortionConfig(lsst.pex.config.Config): 

"""Config for FitSipDistortionTask""" 

order = lsst.pex.config.RangeField( 

doc="Order of SIP polynomial", 

dtype=int, 

default=4, 

min=0, 

) 

numRejIter = lsst.pex.config.RangeField( 

doc="Number of rejection iterations", 

dtype=int, 

default=3, 

min=0, 

) 

rejSigma = lsst.pex.config.RangeField( 

doc="Number of standard deviations for clipping level", 

dtype=float, 

default=3.0, 

min=0.0, 

) 

nClipMin = lsst.pex.config.Field( 

doc="Minimum number of matches to reject when sigma-clipping", 

dtype=int, 

default=0 

) 

nClipMax = lsst.pex.config.Field( 

doc="Maximum number of matches to reject when sigma-clipping", 

dtype=int, 

default=1 

) 

maxScatterArcsec = lsst.pex.config.RangeField( 

doc="Maximum median scatter of a WCS fit beyond which the fit fails (arcsec); " 

"be generous, as this is only intended to catch catastrophic failures", 

dtype=float, 

default=10, 

min=0, 

) 

refUncertainty = lsst.pex.config.Field( 

doc="RMS uncertainty in reference catalog positions, in pixels. Will be added " 

"in quadrature with measured uncertainties in the fit.", 

dtype=float, 

default=0.25, 

) 

nGridX = lsst.pex.config.Field( 

doc="Number of X grid points used to invert the SIP reverse transform.", 

dtype=int, 

default=100, 

) 

nGridY = lsst.pex.config.Field( 

doc="Number of Y grid points used to invert the SIP reverse transform.", 

dtype=int, 

default=100, 

) 

gridBorder = lsst.pex.config.Field( 

doc="When setting the gird region, how much to extend the image " 

"bounding box (in pixels) before transforming it to intermediate " 

"world coordinates using the initial WCS.", 

dtype=float, 

default=50.0, 

) 

 

 

class FitSipDistortionTask(lsst.pipe.base.Task): 

"""Fit a TAN-SIP WCS given a list of reference object/source matches. 

""" 

ConfigClass = FitSipDistortionConfig 

_DefaultName = "fitWcs" 

 

def __init__(self, **kwargs): 

lsst.pipe.base.Task.__init__(self, **kwargs) 

self.outlierRejectionCtrl = OutlierRejectionControl() 

self.outlierRejectionCtrl.nClipMin = self.config.nClipMin 

self.outlierRejectionCtrl.nClipMax = self.config.nClipMax 

self.outlierRejectionCtrl.nSigma = self.config.rejSigma 

 

@lsst.pipe.base.timeMethod 

def fitWcs(self, matches, initWcs, bbox=None, refCat=None, sourceCat=None, exposure=None): 

"""Fit a TAN-SIP WCS from a list of reference object/source matches. 

 

Parameters 

---------- 

matches : `list` of `lsst.afw.table.ReferenceMatch` 

A sequence of reference object/source matches. 

The following fields are read: 

- match.first (reference object) coord 

- match.second (source) centroid 

 

The following fields are written: 

- match.first (reference object) centroid 

- match.second (source) centroid 

- match.distance (on sky separation, in radians) 

 

initWcs : `lsst.afw.geom.SkyWcs` 

An initial WCS whose CD matrix is used as the final CD matrix. 

bbox : `lsst.geom.Box2I` 

The region over which the WCS will be valid (PARENT pixel coordinates); 

if `None` or an empty box then computed from matches 

refCat : `lsst.afw.table.SimpleCatalog` 

Reference object catalog, or `None`. 

If provided then all centroids are updated with the new WCS, 

otherwise only the centroids for ref objects in matches are updated. 

Required fields are "centroid_x", "centroid_y", "coord_ra", and "coord_dec". 

sourceCat : `lsst.afw.table.SourceCatalog` 

Source catalog, or `None`. 

If provided then coords are updated with the new WCS; 

otherwise only the coords for sources in matches are updated. 

Required input fields are "slot_Centroid_x", "slot_Centroid_y", 

"slot_Centroid_xErr", "slot_Centroid_yErr", and optionally 

"slot_Centroid_x_y_Cov". The "coord_ra" and "coord_dec" fields 

will be updated but are not used as input. 

exposure : `lsst.afw.image.Exposure` 

An Exposure or other displayable image on which matches can be 

overplotted. Ignored (and may be `None`) if display-based debugging 

is not enabled via lsstDebug. 

 

Returns 

------- 

An lsst.pipe.base.Struct with the following fields: 

- wcs : `lsst.afw.geom.SkyWcs` 

The best-fit WCS. 

- scatterOnSky : `lsst.geom.Angle` 

The median on-sky separation between reference objects and 

sources in "matches", as an `lsst.geom.Angle` 

""" 

import lsstDebug 

display = lsstDebug.Info(__name__).display 

displayFrame = lsstDebug.Info(__name__).frame 

displayPause = lsstDebug.Info(__name__).pause 

 

if bbox is None: 

bbox = lsst.geom.Box2D() 

for match in matches: 

bbox.include(match.second.getCentroid()) 

bbox = lsst.geom.Box2I(bbox) 

 

wcs = self.makeInitialWcs(matches, initWcs) 

cdMatrix = lsst.geom.LinearTransform(wcs.getCdMatrix()) 

 

# Fit the "reverse" mapping from intermediate world coordinates to 

# pixels, rejecting outliers. Fitting in this direction first makes it 

# easier to handle the case where we have uncertainty on source 

# positions but not reference positions. That's the case we have 

# right now for purely bookeeeping reasons, and it may be the case we 

# have in the future when we us Gaia as the reference catalog. 

revFitter = ScaledPolynomialTransformFitter.fromMatches(self.config.order, matches, wcs, 

self.config.refUncertainty) 

revFitter.fit() 

for nIter in range(self.config.numRejIter): 

revFitter.updateModel() 

intrinsicScatter = revFitter.updateIntrinsicScatter() 

clippedSigma, nRejected = revFitter.rejectOutliers(self.outlierRejectionCtrl) 

self.log.debug( 

"Iteration {0}: intrinsic scatter is {1:4.3f} pixels, " 

"rejected {2} outliers at {3:3.2f} sigma.".format( 

nIter+1, intrinsicScatter, nRejected, clippedSigma 

) 

) 

if display: 

displayFrame = self.display(revFitter, exposure=exposure, bbox=bbox, 

frame=displayFrame, displayPause=displayPause) 

revFitter.fit() 

revScaledPoly = revFitter.getTransform() 

# Convert the generic ScaledPolynomialTransform result to SIP form 

# with given CRPIX and CD (this is an exact conversion, up to 

# floating-point round-off error) 

sipReverse = SipReverseTransform.convert(revScaledPoly, wcs.getPixelOrigin(), cdMatrix) 

 

# Fit the forward mapping to a grid of points created from the reverse 

# transform. Because that grid needs to be defined in intermediate 

# world coordinates, and we don't have a good way to get from pixels to 

# intermediate world coordinates yet (that's what we're fitting), we'll 

# first grow the box to make it conservatively large... 

gridBBoxPix = lsst.geom.Box2D(bbox) 

gridBBoxPix.grow(self.config.gridBorder) 

# ...and then we'll transform using just the CRPIX offset and CD matrix 

# linear transform, which is the TAN-only (no SIP distortion, and 

# hence approximate) mapping from pixels to intermediate world 

# coordinates. 

gridBBoxIwc = lsst.geom.Box2D() 

for point in gridBBoxPix.getCorners(): 

point -= lsst.geom.Extent2D(wcs.getPixelOrigin()) 

gridBBoxIwc.include(cdMatrix(point)) 

fwdFitter = ScaledPolynomialTransformFitter.fromGrid(self.config.order, gridBBoxIwc, 

self.config.nGridX, self.config.nGridY, 

revScaledPoly) 

fwdFitter.fit() 

# Convert to SIP forward form. 

fwdScaledPoly = fwdFitter.getTransform() 

sipForward = SipForwardTransform.convert(fwdScaledPoly, wcs.getPixelOrigin(), cdMatrix) 

 

# Make a new WCS from the SIP transform objects and the CRVAL in the 

# initial WCS. 

wcs = makeWcs(sipForward, sipReverse, wcs.getSkyOrigin()) 

 

if refCat is not None: 

self.log.debug("Updating centroids in refCat") 

lsst.afw.table.updateRefCentroids(wcs, refList=refCat) 

else: 

self.log.warn("Updating reference object centroids in match list; refCat is None") 

lsst.afw.table.updateRefCentroids(wcs, refList=[match.first for match in matches]) 

 

if sourceCat is not None: 

self.log.debug("Updating coords in sourceCat") 

lsst.afw.table.updateSourceCoords(wcs, sourceList=sourceCat) 

else: 

self.log.warn("Updating source coords in match list; sourceCat is None") 

lsst.afw.table.updateSourceCoords(wcs, sourceList=[match.second for match in matches]) 

 

self.log.debug("Updating distance in match list") 

setMatchDistance(matches) 

 

stats = makeMatchStatisticsInRadians(wcs, matches, lsst.afw.math.MEDIAN) 

scatterOnSky = stats.getValue()*lsst.geom.radians 

 

if scatterOnSky.asArcseconds() > self.config.maxScatterArcsec: 

raise lsst.pipe.base.TaskError( 

"Fit failed: median scatter on sky = %0.3f arcsec > %0.3f config.maxScatterArcsec" % 

(scatterOnSky.asArcseconds(), self.config.maxScatterArcsec)) 

 

return lsst.pipe.base.Struct( 

wcs=wcs, 

scatterOnSky=scatterOnSky, 

) 

 

def display(self, revFitter, exposure=None, bbox=None, frame=0, pause=True): 

"""Display positions and outlier status overlaid on an image. 

 

This method is called by fitWcs when display debugging is enabled. It 

always drops into pdb before returning to allow interactive inspection, 

and hence it should never be called in non-interactive contexts. 

 

Parameters 

---------- 

revFitter : :cpp:class:`lsst::meas::astrom::ScaledPolynomialTransformFitter` 

Fitter object initialized with `fromMatches` for fitting a "reverse" 

distortion: the mapping from intermediate world coordinates to 

pixels. 

exposure : :cpp:class:`lsst::afw::image::Exposure` 

An Exposure or other displayable image on which matches can be 

overplotted. 

bbox : :cpp:class:`lsst::afw::geom::Box2I` 

Bounding box of the region on which matches should be plotted. 

""" 

data = revFitter.getData() 

disp = lsst.afw.display.getDisplay(frame=frame) 

if exposure is not None: 

disp.mtv(exposure) 

elif bbox is not None: 

disp.mtv(exposure=lsst.afw.image.ExposureF(bbox)) 

else: 

raise TypeError("At least one of 'exposure' and 'bbox' must be provided.") 

data = revFitter.getData() 

srcKey = lsst.afw.table.Point2DKey(data.schema["src"]) 

srcErrKey = lsst.afw.table.CovarianceMatrix2fKey(data.schema["src"], ["x", "y"]) 

refKey = lsst.afw.table.Point2DKey(data.schema["initial"]) 

modelKey = lsst.afw.table.Point2DKey(data.schema["model"]) 

rejectedKey = data.schema.find("rejected").key 

with disp.Buffering(): 

for record in data: 

colors = ((lsst.afw.display.RED, lsst.afw.display.GREEN) 

if not record.get(rejectedKey) else 

(lsst.afw.display.MAGENTA, lsst.afw.display.CYAN)) 

rx, ry = record.get(refKey) 

disp.dot("x", rx, ry, size=10, ctype=colors[0]) 

mx, my = record.get(modelKey) 

disp.dot("o", mx, my, size=10, ctype=colors[0]) 

disp.line([(rx, ry), (mx, my)], ctype=colors[0]) 

sx, sy = record.get(srcKey) 

sErr = record.get(srcErrKey) 

sEllipse = lsst.afw.geom.Quadrupole(sErr[0, 0], sErr[1, 1], sErr[0, 1]) 

disp.dot(sEllipse, sx, sy, ctype=colors[1]) 

if pause or pause is None: # default is to pause 

print("Dropping into debugger to allow inspection of display. Type 'continue' when done.") 

import pdb 

pdb.set_trace() 

return frame 

else: 

return frame + 1 # increment and return the frame for the next iteration. 

 

def makeInitialWcs(self, matches, wcs): 

"""Generate a guess Wcs from the astrometric matches 

 

We create a Wcs anchored at the center of the matches, with the scale 

of the input Wcs. This is necessary because the Wcs may have a very 

approximation position (as is common with telescoped-generated Wcs). 

We're using the best of each: positions from the matches, and scale 

from the input Wcs. 

 

Parameters 

---------- 

matches : list of :cpp:class:`lsst::afw::table::ReferenceMatch` 

A sequence of reference object/source matches. 

The following fields are read: 

 

- match.first (reference object) coord 

- match.second (source) centroid 

 

wcs : :cpp:class:`lsst::afw::geom::SkyWcs` 

An initial WCS whose CD matrix is used as the CD matrix of the 

result. 

 

Returns 

------- 

newWcs : `lsst.afw.geom.SkyWcs` 

A new WCS guess. 

""" 

crpix = lsst.geom.Extent2D(0, 0) 

crval = lsst.sphgeom.Vector3d(0, 0, 0) 

for mm in matches: 

crpix += lsst.geom.Extent2D(mm.second.getCentroid()) 

crval += mm.first.getCoord().getVector() 

crpix /= len(matches) 

crval /= len(matches) 

cd = wcs.getCdMatrix() 

newWcs = lsst.afw.geom.makeSkyWcs(crpix=lsst.geom.Point2D(crpix), 

crval=lsst.geom.SpherePoint(crval), 

cdMatrix=cd) 

return newWcs