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

# 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__ = ["FitTanSipWcsTask", "FitTanSipWcsConfig"] 

 

 

import numpy as np 

 

import lsst.geom 

import lsst.sphgeom 

import lsst.afw.geom as afwGeom 

import lsst.afw.table as afwTable 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from .setMatchDistance import setMatchDistance 

from .sip import makeCreateWcsWithSip 

 

 

class FitTanSipWcsConfig(pexConfig.Config): 

"""Config for FitTanSipWcsTask.""" 

order = pexConfig.RangeField( 

doc="order of SIP polynomial", 

dtype=int, 

default=4, 

min=0, 

) 

numIter = pexConfig.RangeField( 

doc="number of iterations of fitter (which fits X and Y separately, and so benefits from " 

"a few iterations", 

dtype=int, 

default=3, 

min=1, 

) 

numRejIter = pexConfig.RangeField( 

doc="number of rejection iterations", 

dtype=int, 

default=1, 

min=0, 

) 

rejSigma = pexConfig.RangeField( 

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

dtype=float, 

default=3.0, 

min=0.0, 

) 

maxScatterArcsec = pexConfig.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, 

) 

 

 

class FitTanSipWcsTask(pipeBase.Task): 

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

""" 

ConfigClass = FitTanSipWcsConfig 

_DefaultName = "fitWcs" 

 

@pipeBase.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` 

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` 

initial WCS 

bbox : `lsst.geom.Box2I` 

the region over which the WCS will be valid (an lsst:afw::geom::Box2I); 

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 fields are "slot_Centroid_x", "slot_Centroid_y", and "coord_ra", and "coord_dec". 

exposure : `lsst.afw.image.Exposure` 

Ignored; present for consistency with FitSipDistortionTask. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

with the following fields: 

 

- ``wcs`` : the fit WCS (`lsst.afw.geom.SkyWcs`) 

- ``scatterOnSky`` : median on-sky separation between reference 

objects and sources in "matches" (`lsst.afw.geom.Angle`) 

""" 

if bbox is None: 

bbox = lsst.geom.Box2I() 

 

import lsstDebug 

debug = lsstDebug.Info(__name__) 

 

wcs = self.initialWcs(matches, initWcs) 

rejected = np.zeros(len(matches), dtype=bool) 

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

sipObject = self._fitWcs([mm for i, mm in enumerate(matches) if not rejected[i]], wcs) 

wcs = sipObject.getNewWcs() 

rejected = self.rejectMatches(matches, wcs, rejected) 

if rejected.sum() == len(rejected): 

raise RuntimeError("All matches rejected in iteration %d" % (rej + 1,)) 

self.log.debug( 

"Iteration {0} of astrometry fitting: rejected {1} outliers, " 

"out of {2} total matches.".format( 

rej, rejected.sum(), len(rejected) 

) 

) 

if debug.plot: 

print("Plotting fit after rejection iteration %d/%d" % (rej + 1, self.config.numRejIter)) 

self.plotFit(matches, wcs, rejected) 

# Final fit after rejection 

sipObject = self._fitWcs([mm for i, mm in enumerate(matches) if not rejected[i]], wcs) 

wcs = sipObject.getNewWcs() 

if debug.plot: 

print("Plotting final fit") 

self.plotFit(matches, wcs, rejected) 

 

if refCat is not None: 

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

afwTable.updateRefCentroids(wcs, refList=refCat) 

else: 

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

afwTable.updateRefCentroids(wcs, refList=[match.first for match in matches]) 

 

if sourceCat is not None: 

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

afwTable.updateSourceCoords(wcs, sourceList=sourceCat) 

else: 

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

afwTable.updateSourceCoords(wcs, sourceList=[match.second for match in matches]) 

 

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

setMatchDistance(matches) 

 

scatterOnSky = sipObject.getScatterOnSky() 

 

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

raise pipeBase.TaskError( 

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

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

 

return pipeBase.Struct( 

wcs=wcs, 

scatterOnSky=scatterOnSky, 

) 

 

def initialWcs(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 matching returns only 

matches with no estimated Wcs, and the input Wcs is a wild guess. 

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

from the input Wcs. 

 

Parameters 

---------- 

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

List of sources matched to references. 

wcs : `lsst.afw.geom.SkyWcs` 

Current WCS. 

 

Returns 

------- 

newWcs : `lsst.afw.geom.SkyWcs` 

Initial WCS guess from estimated crpix and crval. 

""" 

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) 

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

crval=lsst.geom.SpherePoint(crval), 

cdMatrix=wcs.getCdMatrix()) 

return newWcs 

 

def _fitWcs(self, matches, wcs): 

"""Fit a Wcs based on the matches and a guess Wcs. 

 

Parameters 

---------- 

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

List of sources matched to references. 

wcs : `lsst.afw.geom.SkyWcs` 

Current WCS. 

 

Returns 

------- 

sipObject : `lsst.meas.astrom.sip.CreateWcsWithSip` 

Fitted SIP object. 

""" 

for i in range(self.config.numIter): 

sipObject = makeCreateWcsWithSip(matches, wcs, self.config.order) 

wcs = sipObject.getNewWcs() 

return sipObject 

 

def rejectMatches(self, matches, wcs, rejected): 

"""Flag deviant matches 

 

We return a boolean numpy array indicating whether the corresponding 

match should be rejected. The previous list of rejections is used 

so we can calculate uncontaminated statistics. 

 

Parameters 

---------- 

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

List of sources matched to references. 

wcs : `lsst.afw.geom.SkyWcs` 

Fitted WCS. 

rejected : array-like of `bool` 

Array of matches rejected from the fit. Unused. 

 

Returns 

------- 

rejectedMatches : `ndarray` of type `bool` 

Matched objects found to be outside of tolerance. 

""" 

fit = [wcs.skyToPixel(m.first.getCoord()) for m in matches] 

dx = np.array([ff.getX() - mm.second.getCentroid().getX() for ff, mm in zip(fit, matches)]) 

dy = np.array([ff.getY() - mm.second.getCentroid().getY() for ff, mm in zip(fit, matches)]) 

good = np.logical_not(rejected) 

return (dx > self.config.rejSigma*dx[good].std()) | (dy > self.config.rejSigma*dy[good].std()) 

 

def plotFit(self, matches, wcs, rejected): 

"""Plot the fit 

 

We create four plots, for all combinations of (dx, dy) against 

(x, y). Good points are black, while rejected points are red. 

 

Parameters 

---------- 

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

List of sources matched to references. 

wcs : `lsst.afw.geom.SkyWcs` 

Fitted WCS. 

rejected : array-like of `bool` 

Array of matches rejected from the fit. 

""" 

try: 

import matplotlib.pyplot as plt 

except ImportError as e: 

self.log.warn("Unable to import matplotlib: %s", e) 

return 

 

fit = [wcs.skyToPixel(m.first.getCoord()) for m in matches] 

x1 = np.array([ff.getX() for ff in fit]) 

y1 = np.array([ff.getY() for ff in fit]) 

x2 = np.array([m.second.getCentroid().getX() for m in matches]) 

y2 = np.array([m.second.getCentroid().getY() for m in matches]) 

 

dx = x1 - x2 

dy = y1 - y2 

 

good = np.logical_not(rejected) 

 

figure = plt.figure() 

axes = figure.add_subplot(2, 2, 1) 

axes.plot(x2[good], dx[good], 'ko') 

axes.plot(x2[rejected], dx[rejected], 'ro') 

axes.set_xlabel("x") 

axes.set_ylabel("dx") 

 

axes = figure.add_subplot(2, 2, 2) 

axes.plot(x2[good], dy[good], 'ko') 

axes.plot(x2[rejected], dy[rejected], 'ro') 

axes.set_xlabel("x") 

axes.set_ylabel("dy") 

 

axes = figure.add_subplot(2, 2, 3) 

axes.plot(y2[good], dx[good], 'ko') 

axes.plot(y2[rejected], dx[rejected], 'ro') 

axes.set_xlabel("y") 

axes.set_ylabel("dx") 

 

axes = figure.add_subplot(2, 2, 4) 

axes.plot(y2[good], dy[good], 'ko') 

axes.plot(y2[rejected], dy[rejected], 'ro') 

axes.set_xlabel("y") 

axes.set_ylabel("dy") 

 

plt.show()