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

# 

# LSST Data Management System 

# Copyright 2008-2016 AURA/LSST. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

# 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

__all__ = ["AstrometryConfig", "AstrometryTask"] 

 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from .ref_match import RefMatchTask, RefMatchConfig 

from .fitTanSipWcs import FitTanSipWcsTask 

from .display import displayAstrometry 

 

 

class AstrometryConfig(RefMatchConfig): 

"""Config for AstrometryTask. 

""" 

wcsFitter = pexConfig.ConfigurableField( 

target=FitTanSipWcsTask, 

doc="WCS fitter", 

) 

forceKnownWcs = pexConfig.Field( 

dtype=bool, 

doc="If True then load reference objects and match sources but do not fit a WCS; " 

"this simply controls whether 'run' calls 'solve' or 'loadAndMatch'", 

default=False, 

) 

maxIter = pexConfig.RangeField( 

doc="maximum number of iterations of match sources and fit WCS" 

"ignored if not fitting a WCS", 

dtype=int, 

default=3, 

min=1, 

) 

minMatchDistanceArcSec = pexConfig.RangeField( 

doc="the match distance below which further iteration is pointless (arcsec); " 

"ignored if not fitting a WCS", 

dtype=float, 

default=0.001, 

min=0, 

) 

 

 

class AstrometryTask(RefMatchTask): 

"""Match an input source catalog with objects from a reference catalog and 

solve for the WCS. 

 

# TODO: DM-16868 remove explicit and unused schema from class input. 

 

Parameters 

---------- 

refObjLoader : `lsst.meas.algorithms.ReferenceLoader` 

A reference object loader object 

schema : `lsst.afw.table.Schema` 

ignored; available for compatibility with an older astrometry task 

**kwargs 

additional keyword arguments for pipe_base 

`lsst.pipe.base.Task.__init__` 

""" 

ConfigClass = AstrometryConfig 

_DefaultName = "astrometricSolver" 

 

def __init__(self, refObjLoader, schema=None, **kwargs): 

RefMatchTask.__init__(self, refObjLoader, schema=schema, **kwargs) 

 

if schema is not None: 

self.usedKey = schema.addField("calib_astrometry_used", type="Flag", 

doc="set if source was used in astrometric calibration") 

else: 

self.usedKey = None 

 

self.makeSubtask("wcsFitter") 

 

@pipeBase.timeMethod 

def run(self, sourceCat, exposure): 

"""Load reference objects, match sources and optionally fit a WCS. 

 

This is a thin layer around solve or loadAndMatch, depending on 

config.forceKnownWcs. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

exposure whose WCS is to be fit 

The following are read only: 

 

- bbox 

- calib (may be absent) 

- filter (may be unset) 

- detector (if wcs is pure tangent; may be absent) 

 

The following are updated: 

 

- wcs (the initial value is used as an initial guess, and is 

required) 

 

sourceCat : `lsst.afw.table.SourceCatalog` 

catalog of sources detected on the exposure 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

with these fields: 

 

- ``refCat`` : reference object catalog of objects that overlap the 

exposure (with some margin) (`lsst.afw.table.SimpleCatalog`). 

- ``matches`` : astrometric matches 

(`list` of `lsst.afw.table.ReferenceMatch`). 

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

objects and sources in "matches" 

(`lsst.afw.geom.Angle`) or `None` if config.forceKnownWcs True 

- ``matchMeta`` : metadata needed to unpersist matches 

(`lsst.daf.base.PropertyList`) 

""" 

if self.config.forceKnownWcs: 

res = self.loadAndMatch(exposure=exposure, sourceCat=sourceCat) 

res.scatterOnSky = None 

else: 

res = self.solve(exposure=exposure, sourceCat=sourceCat) 

return res 

 

@pipeBase.timeMethod 

def solve(self, exposure, sourceCat): 

"""Load reference objects overlapping an exposure, match to sources and 

fit a WCS 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

Result struct with components: 

 

- ``refCat`` : reference object catalog of objects that overlap the 

exposure (with some margin) (`lsst::afw::table::SimpleCatalog`). 

- ``matches`` : astrometric matches 

(`list` of `lsst.afw.table.ReferenceMatch`). 

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

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

- ``matchMeta`` : metadata needed to unpersist matches 

(`lsst.daf.base.PropertyList`) 

 

Notes 

----- 

ignores config.forceKnownWcs 

""" 

import lsstDebug 

debug = lsstDebug.Info(__name__) 

 

expMd = self._getExposureMetadata(exposure) 

 

loadRes = self.refObjLoader.loadPixelBox( 

bbox=expMd.bbox, 

wcs=expMd.wcs, 

filterName=expMd.filterName, 

calib=expMd.calib, 

epoch=expMd.epoch, 

) 

matchMeta = self.refObjLoader.getMetadataBox( 

bbox=expMd.bbox, 

wcs=expMd.wcs, 

filterName=expMd.filterName, 

calib=expMd.calib, 

epoch=expMd.epoch, 

) 

 

if debug.display: 

frame = int(debug.frame) 

displayAstrometry( 

refCat=loadRes.refCat, 

sourceCat=sourceCat, 

exposure=exposure, 

bbox=expMd.bbox, 

frame=frame, 

title="Reference catalog", 

) 

 

res = None 

wcs = expMd.wcs 

match_tolerance = None 

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

iterNum = i + 1 

try: 

tryRes = self._matchAndFitWcs( # refCat, sourceCat, refFluxField, bbox, wcs, exposure=None 

refCat=loadRes.refCat, 

sourceCat=sourceCat, 

refFluxField=loadRes.fluxField, 

bbox=expMd.bbox, 

wcs=wcs, 

exposure=exposure, 

match_tolerance=match_tolerance, 

) 

except Exception as e: 

# if we have had a succeessful iteration then use that; otherwise fail 

if i > 0: 

self.log.info("Fit WCS iter %d failed; using previous iteration: %s" % (iterNum, e)) 

iterNum -= 1 

break 

else: 

raise 

 

match_tolerance = tryRes.match_tolerance 

tryMatchDist = self._computeMatchStatsOnSky(tryRes.matches) 

self.log.debug( 

"Match and fit WCS iteration %d: found %d matches with scatter = %0.3f +- %0.3f arcsec; " 

"max match distance = %0.3f arcsec", 

iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(), 

tryMatchDist.distStdDev.asArcseconds(), tryMatchDist.maxMatchDist.asArcseconds()) 

 

maxMatchDist = tryMatchDist.maxMatchDist 

res = tryRes 

wcs = res.wcs 

if maxMatchDist.asArcseconds() < self.config.minMatchDistanceArcSec: 

self.log.debug( 

"Max match distance = %0.3f arcsec < %0.3f = config.minMatchDistanceArcSec; " 

"that's good enough", 

maxMatchDist.asArcseconds(), self.config.minMatchDistanceArcSec) 

break 

match_tolerance.maxMatchDist = maxMatchDist 

 

self.log.info( 

"Matched and fit WCS in %d iterations; " 

"found %d matches with scatter = %0.3f +- %0.3f arcsec" % 

(iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(), 

tryMatchDist.distStdDev.asArcseconds())) 

for m in res.matches: 

if self.usedKey: 

m.second.set(self.usedKey, True) 

exposure.setWcs(res.wcs) 

 

return pipeBase.Struct( 

refCat=loadRes.refCat, 

matches=res.matches, 

scatterOnSky=res.scatterOnSky, 

matchMeta=matchMeta, 

) 

 

@pipeBase.timeMethod 

def _matchAndFitWcs(self, refCat, sourceCat, refFluxField, bbox, wcs, match_tolerance, 

exposure=None): 

"""Match sources to reference objects and fit a WCS. 

 

Parameters 

---------- 

refCat : `lsst.afw.table.SimpleCatalog` 

catalog of reference objects 

sourceCat : `lsst.afw.table.SourceCatalog` 

catalog of sources detected on the exposure 

refFluxField : 'str' 

field of refCat to use for flux 

bbox : `lsst.geom.Box2I` 

bounding box of exposure 

wcs : `lsst.afw.geom.SkyWcs` 

initial guess for WCS of exposure 

match_tolerance : `lsst.meas.astrom.MatchTolerance` 

a MatchTolerance object (or None) specifying 

internal tolerances to the matcher. See the MatchTolerance 

definition in the respective matcher for the class definition. 

exposure : `lsst.afw.image.Exposure` 

exposure whose WCS is to be fit, or None; used only for the debug 

display. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

Result struct with components: 

 

- ``matches``: astrometric matches 

(`list` of `lsst.afw.table.ReferenceMatch`). 

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

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

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

""" 

import lsstDebug 

debug = lsstDebug.Info(__name__) 

matchRes = self.matcher.matchObjectsToSources( 

refCat=refCat, 

sourceCat=sourceCat, 

wcs=wcs, 

refFluxField=refFluxField, 

match_tolerance=match_tolerance, 

) 

self.log.debug("Found %s matches", len(matchRes.matches)) 

if debug.display: 

frame = int(debug.frame) 

displayAstrometry( 

refCat=refCat, 

sourceCat=matchRes.usableSourceCat, 

matches=matchRes.matches, 

exposure=exposure, 

bbox=bbox, 

frame=frame + 1, 

title="Initial WCS", 

) 

 

self.log.debug("Fitting WCS") 

fitRes = self.wcsFitter.fitWcs( 

matches=matchRes.matches, 

initWcs=wcs, 

bbox=bbox, 

refCat=refCat, 

sourceCat=sourceCat, 

exposure=exposure, 

) 

fitWcs = fitRes.wcs 

scatterOnSky = fitRes.scatterOnSky 

if debug.display: 

frame = int(debug.frame) 

displayAstrometry( 

refCat=refCat, 

sourceCat=matchRes.usableSourceCat, 

matches=matchRes.matches, 

exposure=exposure, 

bbox=bbox, 

frame=frame + 2, 

title="Fit TAN-SIP WCS", 

) 

 

return pipeBase.Struct( 

matches=matchRes.matches, 

wcs=fitWcs, 

scatterOnSky=scatterOnSky, 

match_tolerance=matchRes.match_tolerance, 

)