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

# 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__ = ["FitAffineWcsTask", "FitAffineWcsConfig", "TransformedSkyWcsMaker"] 

 

 

import astshim 

import numpy as np 

from scipy.optimize import least_squares 

 

from lsst.afw.geom import makeSkyWcs, degrees, arcseconds, radians, SkyWcs 

import lsst.afw.math 

from lsst.geom import Point2D 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

 

from .makeMatchStatistics import makeMatchStatisticsInRadians 

from .setMatchDistance import setMatchDistance 

 

 

def _chiFunc(x, refPoints, srcPixels, wcsMaker): 

"""Function to minimize to fit the shift and rotation in the WCS. 

 

Parameters 

---------- 

x : `numpy.ndarray` 

Current fit values to test. Float values in array are: 

 

- ``bearingTo``: Direction to move the wcs coord in. 

- ``separation``: Distance along sphere to move wcs coord in. 

- ``affine0,0``: [0, 0] value of the 2x2 affine transform matrix. 

- ``affine0,1``: [0, 1] value of the 2x2 affine transform matrix. 

- ``affine1,0``: [1, 0] value of the 2x2 affine transform matrix. 

- ``affine1,1``: [1, 1] value of the 2x2 affine transform matrix. 

refPoints : `list` of `lsst.afw.geom.SpherePoint` 

Reference object on Sky locations. 

srcPixels : `list` of `lsst.geom.Point2D` 

Source object positions on the pixels. 

wcsMaker : `TransformedSkyWcsMaker` 

Container class for producing the updated Wcs. 

 

Returns 

------- 

outputSeparations : `list` of `float` 

Separation between predicted source location and reference location in 

radians. 

""" 

wcs = wcsMaker.makeWcs(x[:2], x[2:].reshape((2, 2))) 

 

outputSeparations = [] 

# Fit both sky to pixel and pixel to sky to avoid any non-invertible 

# affine matrices. 

for ref, src in zip(refPoints, srcPixels): 

skySep = ref.getTangentPlaneOffset(wcs.pixelToSky(src)) 

outputSeparations.append(skySep[0].asArcseconds()) 

outputSeparations.append(skySep[1].asArcseconds()) 

xySep = src - wcs.skyToPixel(ref) 

# Convert the pixel separations to units, arcseconds to match units 

# of sky separation. 

outputSeparations.append( 

xySep[0] * wcs.getPixelScale(src).asArcseconds()) 

outputSeparations.append( 

xySep[1] * wcs.getPixelScale(src).asArcseconds()) 

 

return outputSeparations 

 

 

# Keeping this around for now in case any of the fit parameters need to be 

# configurable. Likely the maximum allowed shift magnitude (parameter 2 in the 

# fit.) 

class FitAffineWcsConfig(pexConfig.Config): 

"""Config for FitTanSipWcsTask.""" 

pass 

 

 

class FitAffineWcsTask(pipeBase.Task): 

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

 

This WCS fitter should be used on top of a cameraGeom distortion model as 

the model assumes that only a shift the WCS center position and a small 

affine transform are required. 

""" 

ConfigClass = FitAffineWcsConfig 

_DefaultName = "fitAffineWcs" 

 

@pipeBase.timeMethod 

def fitWcs(self, 

matches, 

initWcs, 

bbox=None, 

refCat=None, 

sourceCat=None, 

exposure=None): 

"""Fit a simple Affine transform with a shift to the matches and update 

the WCS. 

 

This method assumes that the distortion model of the telescope is 

applied correctly and is accurate with only a slight rotation, 

rotation, and "squish" required to fit to the reference locations. 

 

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` 

Ignored; present for consistency with FitSipDistortionTask. 

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`) 

""" 

# Create a data-structure that decomposes the input Wcs frames and 

# appends the new transform. 

wcsMaker = TransformedSkyWcsMaker(initWcs) 

 

refPoints = [] 

srcPixels = [] 

offsetLong = 0 

offsetLat = 0 

# Grab reference coordinates and source centroids. Compute the average 

# direction and separation between the reference and the sources. 

for match in matches: 

refCoord = match.first.getCoord() 

refPoints.append(refCoord) 

srcCentroid = match.second.getCentroid() 

srcPixels.append(srcCentroid) 

srcCoord = initWcs.pixelToSky(srcCentroid) 

deltaLong, deltaLat = srcCoord.getTangentPlaneOffset(refCoord) 

offsetLong += deltaLong.asArcseconds() 

offsetLat += deltaLat.asArcseconds() 

offsetLong /= len(srcPixels) 

offsetLat /= len(srcPixels) 

offsetDist = np.sqrt(offsetLong ** 2 + offsetLat ** 2) 

if offsetDist > 0.: 

offsetDir = np.degrees(np.arccos(offsetLong / offsetDist)) 

else: 

offsetDir = 0. 

offsetDir *= np.sign(offsetLat) 

self.log.debug("Initial shift guess: Direction: %.3f, Dist %.3f..." % 

(offsetDir, offsetDist)) 

 

# Best performing fitter in scipy tried so far (vs. default settings in 

# minimize). Exits early because of the xTol value which cannot be 

# disabled in scipy1.2.1. Matrix starting values are non-zero as this 

# results in better fit off-diagonal terms. 

fit = least_squares( 

_chiFunc, 

x0=[offsetDir, offsetDist, 1., 1e-8, 1e-8, 1.], 

args=(refPoints, srcPixels, wcsMaker), 

method='dogbox', 

bounds=[[-360, -np.inf, -np.inf, -np.inf, -np.inf, -np.inf], 

[360, np.inf, np.inf, np.inf, np.inf, np.inf]], 

ftol=2.3e-16, 

gtol=2.31e-16, 

xtol=2.3e-16) 

self.log.debug("Best fit: Direction: %.3f, Dist: %.3f, " 

"Affine matrix: [[%.6f, %.6f], [%.6f, %.6f]]..." % 

(fit.x[0], fit.x[1], 

fit.x[2], fit.x[3], fit.x[4], fit.x[5])) 

 

wcs = wcsMaker.makeWcs(fit.x[:2], fit.x[2:].reshape((2, 2))) 

 

# Copied from other fit*WcsTasks. 

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]) 

setMatchDistance(matches) 

 

stats = makeMatchStatisticsInRadians(wcs, 

matches, 

lsst.afw.math.MEDIAN) 

scatterOnSky = stats.getValue() * radians 

 

self.log.debug("In fitter scatter %.4f" % scatterOnSky.asArcseconds()) 

 

return lsst.pipe.base.Struct( 

wcs=wcs, 

scatterOnSky=scatterOnSky, 

) 

 

 

class TransformedSkyWcsMaker(): 

"""Convenience class for appending a shifting an input SkyWcs on sky and 

appending an affine transform. 

 

The class assumes that all frames are sequential and are mapped one to the 

next. 

 

Parameters 

---------- 

input_sky_wcs : `lsst.afw.geom.SkyWcs` 

WCS to decompose and append affine matrix and shift in on sky 

location to. 

""" 

 

def __init__(self, inputSkyWcs): 

self.frameDict = inputSkyWcs.getFrameDict() 

 

# Grab the order of the frames by index. 

# TODO: DM-20825 

# Change the frame the transform is appended to to be explicitly 

# the FIELD_ANGLE->IWC transform. Requires related tickets to be 

# completed. 

domains = self.frameDict.getAllDomains() 

self.frameIdxs = np.sort([self.frameDict.getIndex(domain) 

for domain in domains]) 

self.frameMin = np.min(self.frameIdxs) 

self.frameMax = np.max(self.frameIdxs) 

 

# Find frame just before the final mapping to sky and store those 

# indices and mappings for later. 

self.mapFrom = self.frameMax - 2 

if self.mapFrom < self.frameMin: 

self.mapFrom = self.frameMin 

self.mapTo = self.frameMax - 1 

if self.mapTo <= self.mapFrom: 

self.mapTo = self.frameMax 

self.lastMapBeforeSky = self.frameDict.getMapping( 

self.mapFrom, self.mapTo) 

 

# Get the original WCS sky location. 

 

self.origin = inputSkyWcs.getSkyOrigin() 

 

def makeWcs(self, crvalOffset, affMatrix): 

"""Apply a shift and affine transform to the WCS internal to this 

class. 

 

A new SkyWcs with these transforms applied is returns. 

 

Parameters 

---------- 

crval_shift : `numpy.ndarray`, (2,) 

Shift in radians to apply to the Wcs origin/crvals. 

aff_matrix : 'numpy.ndarray', (3, 3) 

Affine matrix to apply to the mapping/transform to add to the 

WCS. 

 

Returns 

------- 

outputWcs : `lsst.afw.geom.SkyWcs` 

Wcs with a final shift and affine transform applied. 

""" 

# Create a WCS that only maps from IWC to Sky with the shifted 

# Sky origin position. This is simply the final undistorted tangent 

# plane to sky. The PIXELS to SKY map will be become our IWC to SKY 

# map and gives us our final shift position. 

iwcsToSkyWcs = makeSkyWcs( 

Point2D(0., 0.), 

self.origin.offset(crvalOffset[0] * degrees, 

crvalOffset[1] * arcseconds), 

np.array([[1., 0.], [0., 1.]])) 

iwcToSkyMap = iwcsToSkyWcs.getFrameDict().getMapping("PIXELS", "SKY") 

 

# Append a simple affine Matrix transform to the current to the 

# second to last frame mapping. e.g. the one just before IWC to SKY. 

newMapping = self.lastMapBeforeSky.then(astshim.MatrixMap(affMatrix)) 

 

# Create a new frame dict starting from the input_sky_wcs's first 

# frame. Append the correct mapping created above and our new on 

# sky location. 

outputFrameDict = astshim.FrameDict( 

self.frameDict.getFrame(self.frameMin)) 

for frameIdx in self.frameIdxs: 

if frameIdx == self.mapFrom: 

outputFrameDict.addFrame( 

self.mapFrom, 

newMapping, 

self.frameDict.getFrame(self.mapTo)) 

elif frameIdx >= self.mapTo: 

continue 

else: 

outputFrameDict.addFrame( 

frameIdx, 

self.frameDict.getMapping(frameIdx, frameIdx + 1), 

self.frameDict.getFrame(frameIdx + 1)) 

# Append the final sky frame to the frame dict. 

outputFrameDict.addFrame( 

self.frameMax - 1, 

iwcToSkyMap, 

iwcsToSkyWcs.getFrameDict().getFrame("SKY")) 

 

return SkyWcs(outputFrameDict)