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

# This file is part of jointcal. 

 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

 

# 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 GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

"""Functions to help create jointcal tests by generating fake data.""" 

 

__all__ = ['createFakeCatalog', 'createTwoFakeCcdImages', 'getMeasuredStarsFromCatalog'] 

 

import os 

import numpy as np 

 

import lsst.afw.geom 

import lsst.afw.table 

import lsst.daf.persistence 

import lsst.obs.lsstSim 

import lsst.pipe.base 

 

import lsst.jointcal.star 

 

 

def createTwoFakeCcdImages(num1=4, num2=4, seed=100): 

"""Return two fake ccdImages built on CFHT Megacam metadata. 

 

If ``num1 == num2``, the catalogs will align on-sky so each source will 

have a match in the other catalog. 

 

This uses the butler dataset stored in `tests/data/cfht_minimal` to 

bootstrap the metadata. 

 

Parameters 

---------- 

num1, num2 : `int`, optional 

Number of sources to put in the first and second catalogs. Should be 

a square, to have sqrt(num) centroids on a grid. 

seed : `int`, optional 

Seed value for np.random. 

 

Returns 

------- 

struct : `lsst.pipe.base.Struct` 

Result struct with components: 

 

- `camera` : Camera representing these catalogs 

(`lsst.afw.cameraGeom.Camera`). 

- `catalogs` : Catalogs containing fake sources 

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

- `ccdImageList` : CcdImages containing the metadata and fake sources 

(`list` of `lsst.jointcal.CcdImage`). 

- `bbox` : Bounding Box of the image (`lsst.afw.geom.Box2I`). 

""" 

np.random.seed(seed) 

 

visit1 = 849375 

visit2 = 850587 

ccdId = 12 

instFluxKeyName = "SomeFlux" 

 

# Load or fake the necessary metadata for each CcdImage 

dataDir = lsst.utils.getPackageDir('jointcal') 

inputDir = os.path.join(dataDir, 'tests/data/cfht_minimal') 

butler = lsst.daf.persistence.Butler(inputDir) 

 

# so we can access parts of the camera later (e.g. focal plane) 

camera = butler.get('camera', visit=visit1, ccd=ccdId) 

 

struct1 = createFakeCcdImage(butler, visit1, ccdId, num1, instFluxKeyName, 

photoCalibMean=100.0, photoCalibErr=1.0) 

struct2 = createFakeCcdImage(butler, visit2, ccdId, num2, instFluxKeyName, 

photoCalibMean=120.0, photoCalibErr=5.0) 

 

return lsst.pipe.base.Struct(camera=camera, 

catalogs=[struct1.catalog, struct2.catalog], 

ccdImageList=[struct1.ccdImage, struct2.ccdImage], 

bbox=struct1.bbox) 

 

 

def createFakeCcdImage(butler, visit, ccdId, num, instFluxKeyName, 

photoCalibMean=100.0, photoCalibErr=1.0): 

"""Create a fake CcdImage by making a fake catalog. 

 

Parameters 

---------- 

butler : `lsst.daf.persistence.Butler` 

Butler to load metadata from. 

visit : `int` 

Visit identifier to build a butler dataId. 

ccdId : `int` 

CCD identifier to build a butler dataId. 

num : `int` 

Number of sources to put in the catalogs. Should be 

a square, to have sqrt(num) centroids on a grid. 

instFluxKeyName : `str` 

Name of the instFluxKey to populate in the catalog. 

photoCalibMean : `float`, optional 

Value to set for calibrationMean in the created PhotoCalib. 

photoCalibErr : `float`, optional 

Value to set for calibrationErr in the created PhotoCalib. 

 

Returns 

------- 

struct : `lsst.pipe.base.Struct` 

Result struct with components: 

 

- `catalog` : Catalogs containing fake sources 

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

- `ccdImage` : CcdImage containing the metadata and fake sources 

(`lsst.jointcal.CcdImage`). 

- `bbox` : Bounding Box of the image (`lsst.afw.geom.Box2I`). 

""" 

dataId = dict(visit=visit, ccd=ccdId) 

skyWcs = butler.get('calexp_wcs', dataId=dataId) 

visitInfo = butler.get('calexp_visitInfo', dataId=dataId) 

bbox = butler.get('calexp_bbox', dataId=dataId) 

detector = butler.get('calexp_detector', dataId=dataId) 

filt = butler.get("calexp_filter", dataId=dataId).getName() 

photoCalib = lsst.afw.image.PhotoCalib(photoCalibMean, photoCalibErr) 

 

catalog = createFakeCatalog(num, bbox, instFluxKeyName, skyWcs=skyWcs) 

ccdImage = lsst.jointcal.ccdImage.CcdImage(catalog, skyWcs, visitInfo, bbox, filt, photoCalib, 

detector, visit, ccdId, instFluxKeyName) 

 

return lsst.pipe.base.Struct(catalog=catalog, ccdImage=ccdImage, bbox=bbox) 

 

 

def createFakeCatalog(num, bbox, instFluxKeyName, skyWcs=None, refCat=False): 

"""Return a fake minimally-useful catalog for jointcal. 

 

Parameters 

---------- 

num : `int` 

Number of sources to put in the catalogs. Should be 

a square, to have sqrt(num) centroids on a grid. 

bbox : `lsst.afw.geom.Box2I` 

Bounding Box of the detector to populate. 

instFluxKeyName : `str` 

Name of the instFluxKey to populate in the catalog. 

skyWcs : `lsst.afw.geom.SkyWcs` or None, optional 

If supplied, use this to fill in coordinates from centroids. 

refCat : `bool`, optional 

Return a ``SimpleCatalog`` so that it behaves like a reference catalog? 

 

Returns 

------- 

catalog : `lsst.afw.table.SourceCatalog` 

A populated source catalog. 

""" 

schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

# centroid 

centroidKey = lsst.afw.table.Point2DKey.addFields(schema, "centroid", "centroid", "pixels") 

xErrKey = schema.addField("centroid_xSigma", type="F") 

yErrKey = schema.addField("centroid_ySigma", type="F") 

# shape 

shapeKey = lsst.afw.table.QuadrupoleKey.addFields(schema, "shape", "", 

lsst.afw.table.CoordinateType.PIXEL) 

# Put the fake sources in the minimal catalog. 

schema.addField(instFluxKeyName+"_flux", type="D", doc="post-ISR instFlux") 

schema.addField(instFluxKeyName+"_fluxSigma", type="D", doc="post-ISR instFlux stddev") 

schema.addField(instFluxKeyName+"_calFlux", type="D", doc="maggies") 

schema.addField(instFluxKeyName+"_calFluxErr", type="D", doc="maggies stddev") 

schema.addField(instFluxKeyName+"_mag", type="D", doc="magnitude") 

schema.addField(instFluxKeyName+"_magErr", type="D", doc="magnitude stddev") 

return fillCatalog(schema, num, bbox, 

centroidKey, xErrKey, yErrKey, shapeKey, instFluxKeyName, 

skyWcs=skyWcs, refCat=refCat) 

 

 

def fillCatalog(schema, num, bbox, 

centroidKey, xErrKey, yErrKey, shapeKey, instFluxKeyName, 

skyWcs=None, fluxErrFraction=0.05, refCat=False): 

"""Return a catalog populated with fake, but reasonable, sources. 

 

Centroids are placed on a uniform grid, errors are normally distributed. 

 

Parameters 

---------- 

schema : `lsst.afw.table.Schema` 

Pre-built schema to make the catalog from. 

num : `int` 

Number of sources to put in the catalog. 

bbox : `lsst.afw.geom.Box2I` 

Bounding box of the ccd to put sources in. 

centroidKey : `lsst.afw.table.Key` 

Key for the centroid field to populate. 

xErrKey : `lsst.afw.table.Key` 

Key for the xErr field to populate. 

yErrKey : `lsst.afw.table.Key` 

Key for the yErr field to populate. 

shapeKey : `lsst.afw.table.Key` 

Key for the shape field to populate. 

instFluxKeyName : `str` 

Name of instFlux field to populate (i.e. instFluxKeyName+'_flux') 

skyWcs : `lsst.afw.geom.SkyWcs` or None, optional 

If supplied, use this to fill in coordinates from centroids. 

fluxErrFraction : `float`, optional 

Fraction of instFlux to use for the instFluxErr. 

refCat : `bool`, optional 

Return a ``SimpleCatalog`` so that it behaves like a reference catalog? 

 

Returns 

------- 

catalog : `lsst.afw.table.SourceCatalog` 

The filled catalog. 

""" 

table = lsst.afw.table.SourceTable.make(schema) 

table.defineCentroid('centroid') 

table.defineShape('shape') 

table.defineInstFlux(instFluxKeyName) 

if refCat: 

catalog = lsst.afw.table.SimpleCatalog(table) 

else: 

catalog = lsst.afw.table.SourceCatalog(table) 

 

instFlux = np.random.random(num) 

instFluxErr = instFlux * fluxErrFraction 

xx = np.linspace(bbox.getMinX(), bbox.getMaxX(), int(np.sqrt(num))) 

yy = np.linspace(bbox.getMinY(), bbox.getMaxY(), int(np.sqrt(num))) 

xv, yv = np.meshgrid(xx, yy) 

vx = np.random.normal(scale=0.1, size=num) 

vy = np.random.normal(scale=0.1, size=num) 

 

# make all the sources perfectly spherical, for simplicity. 

mxx = 1 

myy = 1 

mxy = 0 

 

for i, (x, y) in enumerate(zip(xv.ravel(), yv.ravel())): 

record = catalog.addNew() 

record.set('id', i) 

record.set(centroidKey, lsst.afw.geom.Point2D(x, y)) 

record.set(shapeKey, lsst.afw.geom.ellipses.Quadrupole(mxx, myy, mxy)) 

 

248 ↛ 251line 248 didn't jump to line 251, because the condition on line 248 was never false if skyWcs is not None: 

lsst.afw.table.updateSourceCoords(skyWcs, catalog) 

 

catalog[xErrKey] = vx 

catalog[yErrKey] = vy 

catalog[instFluxKeyName + '_flux'] = instFlux 

catalog[instFluxKeyName + '_fluxSigma'] = instFluxErr 

 

return catalog 

 

 

def getMeasuredStarsFromCatalog(catalog, pixToFocal): 

"""Return a list of measuredStars built from a catalog. 

 

Parameters 

---------- 

catalog : `lsst.afw.table.SourceCatalog` 

The table to get sources from. 

pixToFocal : `lsst.afw.geom.TransformPoint2ToPoint2` 

Transform that goes from pixel to focal plane coordinates, to set the 

MeasuredStar x/y focal points. 

 

Returns 

------- 

stars : `list` of `lsst.jointcal.MeasuredStar` 

MeasuredStars built from the catalog sources. 

""" 

stars = [] 

for record in catalog: 

star = lsst.jointcal.star.MeasuredStar() 

star.x = record.getX() 

star.y = record.getY() 

star.setInstFlux(record.getInstFlux()) 

star.setInstFluxErr(record.getInstFluxErr()) 

# TODO: cleanup after DM-4044 

point = lsst.afw.geom.Point2D(star.x, star.y) 

pointFocal = pixToFocal.applyForward(point) 

star.setXFocal(pointFocal.getX()) 

star.setYFocal(pointFocal.getY()) 

stars.append(star) 

 

return stars