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

 

__all__ = ["matchOptimisticB", "MatchOptimisticBTask", "MatchOptimisticBConfig", 

"MatchTolerance"] 

 

import math 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from lsst.meas.algorithms.sourceSelector import sourceSelectorRegistry 

 

from ..setMatchDistance import setMatchDistance 

from . import matchOptimisticB, MatchOptimisticBControl 

 

 

class MatchTolerance: 

"""Stores match tolerances for use in `lsst.meas.astrom.AstrometryTask` and 

later iterations of the matcher. 

 

MatchOptimsiticBTask relies on a maximum distance for matching 

set by either the default in MatchOptimisticBConfig or the 2 sigma 

scatter found after AstrometryTask has fit for a wcs. 

 

Parameters 

---------- 

maxMatchDist : `lsst.geom.Angle` 

Current maximum distance to consider a match. 

""" 

 

def __init__(self, maxMatchDist=None): 

self.maxMatchDist = maxMatchDist 

 

 

class MatchOptimisticBConfig(pexConfig.Config): 

"""Configuration for MatchOptimisticBTask 

""" 

maxMatchDistArcSec = pexConfig.RangeField( 

doc="Maximum separation between reference objects and sources " 

"beyond which they will not be considered a match (arcsec)", 

dtype=float, 

default=3, 

min=0, 

) 

numBrightStars = pexConfig.RangeField( 

doc="Number of bright stars to use", 

dtype=int, 

default=50, 

min=2, 

) 

minMatchedPairs = pexConfig.RangeField( 

doc="Minimum number of matched pairs; see also minFracMatchedPairs", 

dtype=int, 

default=30, 

min=2, 

) 

minFracMatchedPairs = pexConfig.RangeField( 

doc="Minimum number of matched pairs as a fraction of the smaller of " 

"the number of reference stars or the number of good sources; " 

"the actual minimum is the smaller of this value or minMatchedPairs", 

dtype=float, 

default=0.3, 

min=0, 

max=1, 

) 

maxOffsetPix = pexConfig.RangeField( 

doc="Maximum allowed shift of WCS, due to matching (pixel). " 

"When changing this value, the LoadReferenceObjectsConfig.pixelMargin should also be updated.", 

dtype=int, 

default=300, 

max=4000, 

) 

maxRotationDeg = pexConfig.RangeField( 

doc="Rotation angle allowed between sources and position reference objects (degrees)", 

dtype=float, 

default=1.0, 

max=6.0, 

) 

allowedNonperpDeg = pexConfig.RangeField( 

doc="Allowed non-perpendicularity of x and y (degree)", 

dtype=float, 

default=3.0, 

max=45.0, 

) 

numPointsForShape = pexConfig.Field( 

doc="number of points to define a shape for matching", 

dtype=int, 

default=6, 

) 

maxDeterminant = pexConfig.Field( 

doc="maximum determinant of linear transformation matrix for a usable solution", 

dtype=float, 

default=0.02, 

) 

sourceSelector = sourceSelectorRegistry.makeField( 

doc="How to select sources for cross-matching", 

default="matcher" 

) 

 

def setDefaults(self): 

sourceSelector = self.sourceSelector["matcher"] 

sourceSelector.setDefaults() 

 

 

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

# \addtogroup LSST_task_documentation 

# \{ 

# \page measAstrom_matchOptimisticBTask 

# \ref MatchOptimisticBTask "MatchOptimisticBTask" 

# Match sources to reference objects 

# \} 

 

 

class MatchOptimisticBTask(pipeBase.Task): 

"""Match sources to reference objects using the Optimistic Pattern Matcher 

B algorithm of Tabur 2007. 

""" 

ConfigClass = MatchOptimisticBConfig 

_DefaultName = "matchObjectsToSources" 

 

def __init__(self, **kwargs): 

pipeBase.Task.__init__(self, **kwargs) 

self.makeSubtask("sourceSelector") 

 

def filterStars(self, refCat): 

"""Extra filtering pass; subclass if desired. 

 

Parameters 

---------- 

refCat : `lsst.afw.table.SimpleCatalog` 

Catalog of reference objects. 

 

Returns 

------- 

trimmedRefCat : `lsst.afw.table.SimpleCatalog` 

Reference catalog with some filtering applied. Currently no 

filtering is applied. 

""" 

return refCat 

 

@pipeBase.timeMethod 

def matchObjectsToSources(self, refCat, sourceCat, wcs, refFluxField, 

match_tolerance=None): 

"""Match sources to position reference stars. 

 

Parameters 

---------- 

refCat : `lsst.afw.table.SimpleCatalog` 

Reference catalog to match. 

sourceCat : `lsst.afw.table.SourceCatalog` 

Source catalog to match. 

wcs : `lsst.afw.geom.SkyWcs` 

Current WCS of the exposure containing the sources. 

refFluxField : `str` 

Name of the reference catalog filter to use. 

match_tolerance : `lsst.meas.astrom.MatchTolerance` 

Object containing information from previous 

`lsst.meas.astrom.AstrometryTask` match/fit cycles for use in 

matching. If `None` is config defaults. 

 

Returns 

------- 

matchResult : `lsst.pipe.base.Struct` 

Result struct with components 

 

- ``matches`` : List of matches with distance below the maximum match 

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

- ``useableSourceCat`` : Catalog of sources matched and suited for 

WCS fitting (`lsst.afw.table.SourceCatalog`). 

- ``match_tolerance`` : MatchTolerance object updated from this 

match iteration (`lsst.meas.astrom.MatchTolerance`). 

""" 

import lsstDebug 

debug = lsstDebug.Info(__name__) 

 

preNumObj = len(refCat) 

refCat = self.filterStars(refCat) 

numRefObj = len(refCat) 

 

if self.log: 

self.log.info("filterStars purged %d reference stars, leaving %d stars" % 

(preNumObj - numRefObj, numRefObj)) 

 

if match_tolerance is None: 

match_tolerance = MatchTolerance() 

 

# usableSourceCat: sources that are good but may be saturated 

numSources = len(sourceCat) 

selectedSources = self.sourceSelector.run(sourceCat) 

usableSourceCat = selectedSources.sourceCat 

numUsableSources = len(usableSourceCat) 

self.log.info("Purged %d unusable sources, leaving %d usable sources" % 

(numSources - numUsableSources, numUsableSources)) 

 

if len(usableSourceCat) == 0: 

raise pipeBase.TaskError("No sources are usable") 

 

del sourceCat # avoid accidentally using sourceCat; use usableSourceCat or goodSourceCat from now on 

 

minMatchedPairs = min(self.config.minMatchedPairs, 

int(self.config.minFracMatchedPairs * min([len(refCat), len(usableSourceCat)]))) 

 

# match usable (possibly saturated) sources and then purge saturated sources from the match list 

usableMatches = self._doMatch( 

refCat=refCat, 

sourceCat=usableSourceCat, 

wcs=wcs, 

refFluxField=refFluxField, 

numUsableSources=numUsableSources, 

minMatchedPairs=minMatchedPairs, 

maxMatchDist=match_tolerance.maxMatchDist, 

sourceFluxField=self.sourceSelector.fluxField, 

verbose=debug.verbose, 

) 

 

# cull non-good sources 

matches = [] 

self._getIsGoodKeys(usableSourceCat.schema) 

for match in usableMatches: 

if self._isGoodTest(match.second): 

# Append the isGood match. 

matches.append(match) 

 

self.log.debug("Found %d usable matches, of which %d had good sources", 

len(usableMatches), len(matches)) 

 

if len(matches) == 0: 

raise RuntimeError("Unable to match sources") 

 

self.log.info("Matched %d sources" % len(matches)) 

if len(matches) < minMatchedPairs: 

self.log.warn("Number of matches is smaller than request") 

 

return pipeBase.Struct( 

matches=matches, 

usableSourceCat=usableSourceCat, 

match_tolerance=match_tolerance, 

) 

 

def _getIsGoodKeys(self, schema): 

"""Retrieve the keys needed for the isGoodTest from the source catalog 

schema. 

 

Parameters 

---------- 

schema : `lsst.afw.table.Schema` 

Source schema to retrieve `lsst.afw.table.Key` s from. 

""" 

self.edgeKey = schema["base_PixelFlags_flag_edge"].asKey() 

self.interpolatedCenterKey = schema["base_PixelFlags_flag_interpolatedCenter"].asKey() 

self.saturatedKey = schema["base_PixelFlags_flag_saturated"].asKey() 

 

def _isGoodTest(self, source): 

"""Test that an object is good for use in the WCS fitter. 

 

This is a hard coded version of the isGood flag from the old SourceInfo 

class that used to be part of this class. 

 

Parameters 

---------- 

source : `lsst.afw.table.SourceRecord` 

Source to test. 

 

Returns 

------- 

isGood : `bool` 

Source passes CCD edge and saturated tests. 

""" 

return (not source.get(self.edgeKey) and 

not source.get(self.interpolatedCenterKey) and 

not source.get(self.saturatedKey)) 

 

@pipeBase.timeMethod 

def _doMatch(self, refCat, sourceCat, wcs, refFluxField, numUsableSources, minMatchedPairs, 

maxMatchDist, sourceFluxField, verbose): 

"""Implementation of matching sources to position reference stars. 

 

Unlike matchObjectsToSources, this method does not check if the sources 

are suitable. 

 

Parameters 

---------- 

refCat : `lsst.afw.table.SimpleCatalog` 

Catalog of reference objects. 

sourceCat : `lsst.afw.table.SourceCatalog` 

Catalog of detected sources. 

wcs : `lsst.afw.geom.SkyWcs` 

Current best WCS of the image. 

refFluxFioeld : `str` 

Name of flux field in refCat to use. 

numUsableSources : `int` 

Total number of source usable for matching. 

mintMatchPairs : `int` 

Minimum number of objects to match between the refCat and sourceCat 

to consider a valid match. 

maxMatchDist : `lsst.geom.Angle` 

Maximum separation to considering a reference and a source a match. 

sourceFluxField : `str` 

Name of source catalog flux field. 

verbose : `bool` 

Print diagnostic information std::cout 

 

Returns 

------- 

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

""" 

numSources = len(sourceCat) 

posRefBegInd = numUsableSources - numSources 

if maxMatchDist is None: 

maxMatchDistArcSec = self.config.maxMatchDistArcSec 

else: 

maxMatchDistArcSec = min(maxMatchDist.asArcseconds(), self.config.maxMatchDistArcSec) 

configMatchDistPix = maxMatchDistArcSec/wcs.getPixelScale().asArcseconds() 

 

matchControl = MatchOptimisticBControl() 

matchControl.refFluxField = refFluxField 

matchControl.sourceFluxField = sourceFluxField 

matchControl.numBrightStars = self.config.numBrightStars 

matchControl.minMatchedPairs = self.config.minMatchedPairs 

matchControl.maxOffsetPix = self.config.maxOffsetPix 

matchControl.numPointsForShape = self.config.numPointsForShape 

matchControl.maxDeterminant = self.config.maxDeterminant 

 

for maxRotInd in range(4): 

matchControl.maxRotationDeg = self.config.maxRotationDeg * math.pow(2.0, 0.5*maxRotInd) 

for matchRadInd in range(3): 

matchControl.matchingAllowancePix = configMatchDistPix * math.pow(1.25, matchRadInd) 

 

for angleDiffInd in range(3): 

matchControl.allowedNonperpDeg = self.config.allowedNonperpDeg*(angleDiffInd+1) 

matches = matchOptimisticB( 

refCat, 

sourceCat, 

matchControl, 

wcs, 

posRefBegInd, 

verbose, 

) 

if matches is not None and len(matches) > 0: 

setMatchDistance(matches) 

return matches 

return matches