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

# 

# LSST Data Management System 

# Copyright 2008-2017 LSST Corporation. 

# 

# 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 <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import os.path 

import math 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.geom as afwGeom 

import lsst.afw.table as afwTable 

import lsst.afw.image as afwImage 

import lsst.meas.base as measBase 

from lsst.daf.persistence import Butler 

from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask 

from lsst.meas.astrom import AstrometryTask 

 

 

class TestAstrometricSolver(lsst.utils.tests.TestCase): 

 

def setUp(self): 

refCatDir = os.path.join(os.path.dirname(__file__), "data", "sdssrefcat") 

 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(3001, 3001)) 

crpix = lsst.geom.Box2D(self.bbox).getCenter() 

self.tanWcs = afwGeom.makeSkyWcs(crpix=crpix, 

crval=lsst.geom.SpherePoint(215.5, 53.0, lsst.geom.degrees), 

cdMatrix=afwGeom.makeCdMatrix(scale=5.1e-5*lsst.geom.degrees)) 

self.exposure = afwImage.ExposureF(self.bbox) 

self.exposure.setWcs(self.tanWcs) 

self.exposure.setFilter(afwImage.Filter("r", True)) 

butler = Butler(refCatDir) 

self.refObjLoader = LoadIndexedReferenceObjectsTask(butler=butler) 

 

def tearDown(self): 

del self.tanWcs 

del self.exposure 

del self.refObjLoader 

 

def testTrivial(self): 

"""Test fit with no distortion 

""" 

self.doTest(afwGeom.makeIdentityTransform()) 

 

def testRadial(self): 

"""Test fit with radial distortion 

 

The offset comes from the fact that the CCD is not centered 

""" 

self.doTest(afwGeom.makeRadialTransform([0, 1.01, 1e-7])) 

 

def testUsedFlag(self): 

"""Test that the solver will record number of sources used to table 

if it is passed a schema on initialization. 

""" 

self.exposure.setWcs(self.tanWcs) 

loadRes = self.refObjLoader.loadPixelBox(bbox=self.bbox, wcs=self.tanWcs, filterName="r") 

refCat = loadRes.refCat 

refCentroidKey = afwTable.Point2DKey(refCat.schema["centroid"]) 

refFluxRKey = refCat.schema["r_flux"].asKey() 

 

sourceSchema = afwTable.SourceTable.makeMinimalSchema() 

measBase.SingleFrameMeasurementTask(schema=sourceSchema) # expand the schema 

config = AstrometryTask.ConfigClass() 

config.wcsFitter.order = 2 

config.wcsFitter.numRejIter = 0 

# schema must be passed to the solver task constructor 

solver = AstrometryTask(config=config, refObjLoader=self.refObjLoader, schema=sourceSchema) 

sourceCat = afwTable.SourceCatalog(sourceSchema) 

sourceCat.reserve(len(refCat)) 

sourceCentroidKey = afwTable.Point2DKey(sourceSchema["slot_Centroid"]) 

sourceInstFluxKey = sourceSchema["slot_ApFlux_instFlux"].asKey() 

sourceInstFluxErrKey = sourceSchema["slot_ApFlux_instFluxErr"].asKey() 

 

for refObj in refCat: 

src = sourceCat.addNew() 

src.set(sourceCentroidKey, refObj.get(refCentroidKey)) 

src.set(sourceInstFluxKey, refObj.get(refFluxRKey)) 

src.set(sourceInstFluxErrKey, refObj.get(refFluxRKey)/100) 

 

results = solver.run( 

sourceCat=sourceCat, 

exposure=self.exposure, 

) 

# check that the used flag is set the right number of times 

count = 0 

for source in sourceCat: 

if source.get('calib_astrometry_used'): 

count += 1 

self.assertEqual(count, len(results.matches)) 

 

def doTest(self, pixelsToTanPixels, order=3): 

"""Test using pixelsToTanPixels to distort the source positions 

""" 

distortedWcs = afwGeom.makeModifiedWcs(pixelTransform=pixelsToTanPixels, wcs=self.tanWcs, 

modifyActualPixels=False) 

self.exposure.setWcs(distortedWcs) 

sourceCat = self.makeSourceCat(distortedWcs) 

config = AstrometryTask.ConfigClass() 

config.wcsFitter.order = order 

config.wcsFitter.numRejIter = 0 

solver = AstrometryTask(config=config, refObjLoader=self.refObjLoader) 

results = solver.run( 

sourceCat=sourceCat, 

exposure=self.exposure, 

) 

fitWcs = self.exposure.getWcs() 

self.assertRaises(Exception, self.assertWcsAlmostEqualOverBBox, fitWcs, distortedWcs) 

self.assertWcsAlmostEqualOverBBox(distortedWcs, fitWcs, self.bbox, 

maxDiffSky=0.01*lsst.geom.arcseconds, maxDiffPix=0.02) 

 

srcCoordKey = afwTable.CoordKey(sourceCat.schema["coord"]) 

refCoordKey = afwTable.CoordKey(results.refCat.schema["coord"]) 

refCentroidKey = afwTable.Point2DKey(results.refCat.schema["centroid"]) 

maxAngSep = 0*lsst.geom.radians 

maxPixSep = 0 

for refObj, src, d in results.matches: 

refCoord = refObj.get(refCoordKey) 

refPixPos = refObj.get(refCentroidKey) 

srcCoord = src.get(srcCoordKey) 

srcPixPos = src.getCentroid() 

 

angSep = refCoord.separation(srcCoord) 

maxAngSep = max(maxAngSep, angSep) 

 

pixSep = math.hypot(*(srcPixPos-refPixPos)) 

maxPixSep = max(maxPixSep, pixSep) 

print("max angular separation = %0.4f arcsec" % (maxAngSep.asArcseconds(),)) 

print("max pixel separation = %0.3f" % (maxPixSep,)) 

self.assertLess(maxAngSep.asArcseconds(), 0.0038) 

self.assertLess(maxPixSep, 0.021) 

 

# try again, invoking the reference selector 

config.referenceSelector.doUnresolved = True 

config.referenceSelector.unresolved.name = 'resolved' 

solverRefSelect = AstrometryTask(config=config, refObjLoader=self.refObjLoader) 

self.exposure.setWcs(distortedWcs) 

resultsRefSelect = solverRefSelect.run( 

sourceCat=sourceCat, 

exposure=self.exposure, 

) 

self.assertLess(len(resultsRefSelect.matches), len(results.matches)) 

 

# try again, but without fitting the WCS, no reference selector 

config.referenceSelector.doUnresolved = False 

config.forceKnownWcs = True 

solverNoFit = AstrometryTask(config=config, refObjLoader=self.refObjLoader) 

self.exposure.setWcs(distortedWcs) 

resultsNoFit = solverNoFit.run( 

sourceCat=sourceCat, 

exposure=self.exposure, 

) 

self.assertIsNone(resultsNoFit.scatterOnSky) 

 

# fitting should result in matches that are at least as good 

# (strictly speaking fitting might result in a larger match list with 

# some outliers, but in practice this test passes) 

meanFitDist = np.mean([match.distance for match in results.matches]) 

meanNoFitDist = np.mean([match.distance for match in resultsNoFit.matches]) 

self.assertLessEqual(meanFitDist, meanNoFitDist) 

 

# try once again, without fitting the WCS, with the reference selector 

# (this goes through a different code path) 

config.referenceSelector.doUnresolved = True 

solverNoFitRefSelect = AstrometryTask(config=config, refObjLoader=self.refObjLoader) 

resultsNoFitRefSelect = solverNoFitRefSelect.run( 

sourceCat=sourceCat, 

exposure=self.exposure, 

) 

self.assertLess(len(resultsNoFitRefSelect.matches), len(resultsNoFit.matches)) 

 

def makeSourceCat(self, distortedWcs): 

"""Make a source catalog by reading the position reference stars and distorting the positions 

""" 

loadRes = self.refObjLoader.loadPixelBox(bbox=self.bbox, wcs=distortedWcs, filterName="r") 

refCat = loadRes.refCat 

refCentroidKey = afwTable.Point2DKey(refCat.schema["centroid"]) 

refFluxRKey = refCat.schema["r_flux"].asKey() 

 

sourceSchema = afwTable.SourceTable.makeMinimalSchema() 

measBase.SingleFrameMeasurementTask(schema=sourceSchema) # expand the schema 

sourceCat = afwTable.SourceCatalog(sourceSchema) 

sourceCentroidKey = afwTable.Point2DKey(sourceSchema["slot_Centroid"]) 

sourceInstFluxKey = sourceSchema["slot_ApFlux_instFlux"].asKey() 

sourceInstFluxErrKey = sourceSchema["slot_ApFlux_instFluxErr"].asKey() 

 

sourceCat.reserve(len(refCat)) 

for refObj in refCat: 

src = sourceCat.addNew() 

src.set(sourceCentroidKey, refObj.get(refCentroidKey)) 

src.set(sourceInstFluxKey, refObj.get(refFluxRKey)) 

src.set(sourceInstFluxErrKey, refObj.get(refFluxRKey)/100) 

return sourceCat 

 

 

class MemoryTester(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

225 ↛ 226line 225 didn't jump to line 226, because the condition on line 225 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()