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

357

358

359

360

361

362

363

364

365

# 

# 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): 

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, 

) 

 

# The following block adds links to this task from the Task Documentation page. 

## \addtogroup LSST_task_documentation 

## \{ 

## \page measAstrom_astrometryTask 

## \ref AstrometryTask_ "AstrometryTask" 

## Match an input source catalog with objects from a reference catalog and solve for the WCS 

## \} 

 

 

class AstrometryTask(RefMatchTask): 

"""!Match an input source catalog with objects from a reference catalog and solve for the WCS 

 

@anchor AstrometryTask_ 

 

@section meas_astrom_astrometry_Contents Contents 

 

- @ref meas_astrom_astrometry_Purpose 

- @ref meas_astrom_astrometry_Initialize 

- @ref meas_astrom_astrometry_IO 

- @ref meas_astrom_astrometry_Config 

- @ref meas_astrom_astrometry_Example 

- @ref meas_astrom_astrometry_Debug 

 

@section meas_astrom_astrometry_Purpose Description 

 

Match input sourceCat with a reference catalog and solve for the Wcs 

 

There are three steps, each performed by different subtasks: 

- Find position reference stars that overlap the exposure 

- Match sourceCat to position reference stars 

- Fit a WCS based on the matches 

 

@section meas_astrom_astrometry_Initialize Task initialisation 

 

@copydoc \_\_init\_\_ 

 

@section meas_astrom_astrometry_IO Invoking the Task 

 

@copydoc run 

 

@copydoc loadAndMatch 

 

@section meas_astrom_astrometry_Config Configuration parameters 

 

See @ref AstrometryConfig 

 

@section meas_astrom_astrometry_Example A complete example of using AstrometryTask 

 

See \ref pipe_tasks_photocal_Example. 

 

@section meas_astrom_astrometry_Debug Debug variables 

 

The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a 

flag @c -d to import @b debug.py from your @c PYTHONPATH; see @ref baseDebug for more about 

@b debug.py files. 

 

The available variables in AstrometryTask are: 

<DL> 

<DT> @c display (bool) 

<DD> If True display information at three stages: after finding reference objects, 

after matching sources to reference objects, and after fitting the WCS; defaults to False 

<DT> @c frame (int) 

<DD> ds9 frame to use to display the reference objects; the next two frames are used 

to display the match list and the results of the final WCS; defaults to 0 

</DL> 

 

To investigate the @ref meas_astrom_astrometry_Debug, put something like 

@code{.py} 

import lsstDebug 

def DebugInfo(name): 

debug = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 

if name == "lsst.meas.astrom.astrometry": 

debug.display = True 

 

return debug 

 

lsstDebug.Info = DebugInfo 

@endcode 

into your debug.py file and run this task with the @c --debug flag. 

""" 

ConfigClass = AstrometryConfig 

_DefaultName = "astrometricSolver" 

 

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

"""!Construct an AstrometryTask 

 

@param[in] refObjLoader A reference object loader object 

@param[in] schema ignored; available for compatibility with an older astrometry task 

@param[in] kwargs additional keyword arguments for pipe_base Task.\_\_init\_\_ 

""" 

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 

 

@param[in,out] 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) 

@param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog) 

@return an lsst.pipe.base.Struct with these fields: 

- refCat reference object catalog of objects that overlap the exposure (with some margin) 

(an lsst::afw::table::SimpleCatalog) 

- matches astrometric matches, a list of lsst.afw.table.ReferenceMatch 

- scatterOnSky median on-sky separation between reference objects and sources in "matches" 

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

- matchMeta metadata needed to unpersist matches (an 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 

 

@return an lsst.pipe.base.Struct with these fields: 

- refCat reference object catalog of objects that overlap the exposure (with some margin) 

(an lsst::afw::table::SimpleCatalog) 

- matches astrometric matches, a list of lsst.afw.table.ReferenceMatch 

- scatterOnSky median on-sky separation between reference objects and sources in "matches" 

(an lsst.afw.geom.Angle) 

- matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList) 

 

@note 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, 

) 

 

223 ↛ 224line 223 didn't jump to line 224, because the condition on line 223 was never true 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 

 

@param[in] refCat catalog of reference objects 

@param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog) 

@param[in] refFluxField field of refCat to use for flux 

@param[in] bbox bounding box of exposure (an lsst.afw.geom.Box2I) 

@param[in] wcs initial guess for WCS of exposure (an lsst.afw.geom.Wcs) 

@param[in] match_tolerance a MatchTolerance object (or None) specifying 

internal tolerances to the matcher. See the MatchTolerance 

definition in the respective matcher for the class definition. 

@param[in] exposure exposure whose WCS is to be fit, or None; used only for the debug display 

 

@return an lsst.pipe.base.Struct with these fields: 

- matches astrometric matches, a list of lsst.afw.table.ReferenceMatch 

- wcs the fit WCS (an lsst.afw.geom.Wcs) 

- scatterOnSky median on-sky separation between reference objects and sources in "matches" 

(an 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)) 

325 ↛ 326line 325 didn't jump to line 326, because the condition on line 325 was never true 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 

348 ↛ 349line 348 didn't jump to line 349, because the condition on line 348 was never true 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, 

)