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

# 

# 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__ = ['RefMatchConfig', 'RefMatchTask'] 

 

import astropy.time 

 

import lsst.geom 

from lsst.daf.base import DateTime 

import lsst.afw.math as afwMath 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

from lsst.meas.algorithms import ScienceSourceSelectorTask, ReferenceSourceSelectorTask 

from .matchOptimisticB import MatchOptimisticBTask 

from .display import displayAstrometry 

from . import makeMatchStatistics 

 

 

class RefMatchConfig(pexConfig.Config): 

matcher = pexConfig.ConfigurableField( 

target=MatchOptimisticBTask, 

doc="reference object/source matcher", 

) 

matchDistanceSigma = pexConfig.RangeField( 

doc="the maximum match distance is set to " 

" mean_match_distance + matchDistanceSigma*std_dev_match_distance; " 

"ignored if not fitting a WCS", 

dtype=float, 

default=2, 

min=0, 

) 

sourceSelection = pexConfig.ConfigurableField(target=ScienceSourceSelectorTask, 

doc="Selection of science sources") 

referenceSelection = pexConfig.ConfigurableField(target=ReferenceSourceSelectorTask, 

doc="Selection of reference sources") 

 

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

## \addtogroup LSST_task_documentation 

## \{ 

## \page measAlgorithms_RefMatchTask 

## \ref RefMatchTask_ "RefMatchTask" 

## Basic functionality for all calibration tasks: i.e. a matcher 

## \} 

 

 

class RefMatchTask(pipeBase.Task): 

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

 

@anchor RefMatchTask_ 

""" 

ConfigClass = RefMatchConfig 

_DefaultName = "calibrationBaseClass" 

 

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

r"""!Construct a RefMatchTask 

 

@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\_\_ 

""" 

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

self.refObjLoader = refObjLoader 

self.makeSubtask("matcher") 

self.makeSubtask("sourceSelection") 

self.makeSubtask("referenceSelection") 

 

@pipeBase.timeMethod 

def loadAndMatch(self, exposure, sourceCat): 

"""!Load reference objects overlapping an exposure and match to sources detected on that exposure 

 

@param[in] exposure exposure that the sources overlap 

@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 a list of lsst.afw.table.ReferenceMatch 

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

 

@note ignores config.matchDistanceSigma 

""" 

import lsstDebug 

debug = lsstDebug.Info(__name__) 

 

expMd = self._getExposureMetadata(exposure) 

 

sourceSelection = self.sourceSelection.run(sourceCat) 

 

loadRes = self.refObjLoader.loadPixelBox( 

bbox=expMd.bbox, 

wcs=expMd.wcs, 

filterName=expMd.filterName, 

calib=expMd.calib, 

) 

 

refSelection = self.referenceSelection.run(loadRes.refCat) 

 

matchMeta = self.refObjLoader.getMetadataBox( 

bbox=expMd.bbox, 

wcs=expMd.wcs, 

filterName=expMd.filterName, 

calib=expMd.calib, 

) 

 

matchRes = self.matcher.matchObjectsToSources( 

refCat=refSelection.sourceCat, 

sourceCat=sourceSelection.sourceCat, 

wcs=expMd.wcs, 

refFluxField=loadRes.fluxField, 

match_tolerance=None, 

) 

 

distStats = self._computeMatchStatsOnSky(matchRes.matches) 

self.log.info( 

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

(len(matchRes.matches), distStats.distMean.asArcseconds(), distStats.distStdDev.asArcseconds()) 

) 

 

if debug.display: 

frame = int(debug.frame) 

displayAstrometry( 

refCat=refSelection.sourceCat, 

sourceCat=sourceSelection.sourceCat, 

matches=matchRes.matches, 

exposure=exposure, 

bbox=expMd.bbox, 

frame=frame, 

title="Matches", 

) 

 

return pipeBase.Struct( 

refCat=loadRes.refCat, 

refSelection=refSelection, 

sourceSelection=sourceSelection, 

matches=matchRes.matches, 

matchMeta=matchMeta, 

) 

 

def _computeMatchStatsOnSky(self, matchList): 

"""Compute on-sky radial distance statistics for a match list 

 

@param[in] matchList list of matches between reference object and sources; 

the distance field is the only field read and it must be set to distance in radians 

 

@return a pipe_base Struct containing these fields: 

- distMean clipped mean of on-sky radial separation 

- distStdDev clipped standard deviation of on-sky radial separation 

- maxMatchDist distMean + self.config.matchDistanceSigma*distStdDev 

""" 

distStatsInRadians = makeMatchStatistics(matchList, afwMath.MEANCLIP | afwMath.STDEVCLIP) 

distMean = distStatsInRadians.getValue(afwMath.MEANCLIP)*lsst.geom.radians 

distStdDev = distStatsInRadians.getValue(afwMath.STDEVCLIP)*lsst.geom.radians 

return pipeBase.Struct( 

distMean=distMean, 

distStdDev=distStdDev, 

maxMatchDist=distMean + self.config.matchDistanceSigma*distStdDev, 

) 

 

def _getExposureMetadata(self, exposure): 

"""!Extract metadata from an exposure 

 

@return an lsst.pipe.base.Struct containing the following exposure metadata: 

- bbox: parent bounding box 

- wcs: WCS (an lsst.afw.geom.Wcs) 

- calib calibration (an lsst.afw.image.Calib), or None if unknown 

- filterName: name of filter, or None if unknown 

- epoch: date of exposure (an astropy.time.Time), or None 

""" 

exposureInfo = exposure.getInfo() 

filterName = exposureInfo.getFilter().getName() or None 

if filterName == "_unknown_": 

filterName = None 

epoch = None 

if exposure.getInfo().hasVisitInfo(): 

epochTaiMjd = exposure.getInfo().getVisitInfo().getDate().get(system=DateTime.MJD, 

scale=DateTime.TAI) 

epoch = astropy.time.Time(epochTaiMjd, scale="tai", format="mjd") 

 

return pipeBase.Struct( 

bbox=exposure.getBBox(), 

wcs=exposureInfo.getWcs(), 

calib=exposureInfo.getCalib() if exposureInfo.hasCalib() else None, 

filterName=filterName, 

epoch=epoch, 

)