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

#!/usr/bin/env python 

 

# 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/>. 

 

from __future__ import division 

from __future__ import print_function 

 

import os.path 

import sys 

 

import numpy as np 

 

import lsst.daf.persistence as dafPersist 

import lsst.pipe.base as pipeBase 

import lsst.afw.table as afwTable 

import lsst.afw.geom as afwGeom 

 

 

def loadAndMatchData(repo, visits, fields, ref, ref_field, camcol, filter): 

"""Load data from specific visit+field pairs. Match with reference. 

 

@param repo The repository. This is generally the directory on disk 

that contains the repository and mapper. 

@param visits 'runs' in SDSS nomenclature. List. 

@param fields Field within a camcol. List. 

@param ref The run of the reference image set. Scalar. 

@param ref_field The field of the reference image set. Scalar. 

@param camcol Camera column to use. List. 

@param filter Name of the filter. Scalar 

 

Return a pipeBase.Struct with mag, dist, and number of matches. 

 

Notes: {visit, filter, field, camcol} are sufficient to unique specfiy 

a data ID for the Butler in the obs_sdss camera mapping. 

""" 

 

flags = ["base_PixelFlags_flag_saturated", "base_PixelFlags_flag_cr", "base_PixelFlags_flag_interpolated", 

"base_PsfFlux_flag_edge"] 

 

# setup butler 

butler = dafPersist.Butler(repo) 

 

for indx, c in enumerate(camcol): 

dataid = {'run': ref, 'filter': filter, 'field': ref_field, 'camcol': c} 

oldSrc = butler.get('src', dataid, immediate=True) 

print(len(oldSrc), "sources in camcol :", c) 

65 ↛ 78line 65 didn't jump to line 78, because the condition on line 65 was never false if indx == 0: 

# retrieve the schema of the source catalog and extend it in order 

# to add a field to record the camcol number 

oldSchema = oldSrc.getSchema() 

mapper = afwTable.SchemaMapper(oldSchema) 

mapper.addMinimalSchema(oldSchema) 

mapper.addOutputField(afwTable.Field[np.int32]("camcol", "camcol number")) 

newSchema = mapper.getOutputSchema() 

 

# create the new extented source catalog 

srcRef = afwTable.SourceCatalog(newSchema) 

 

# create temporary catalog 

tmpCat = afwTable.SourceCatalog(srcRef.table) 

tmpCat.extend(oldSrc, mapper=mapper) 

# fill in the camcol information in numpy mode in order to be efficient 

tmpCat['camcol'][:] = c 

# append the temporary catalog to the extended source catalog 

srcRef.extend(tmpCat, deep=False) 

 

print(len(srcRef), "Sources in reference visit :", ref) 

 

mag = [] 

dist = [] 

for v, f in zip(visits, fields): 

if v == ref: 

continue 

for indx, c in enumerate(camcol): 

dataid = {'run': v, 'filter': filter, 'field': f, 'camcol': c} 

94 ↛ 97line 94 didn't jump to line 97, because the condition on line 94 was never false if indx == 0: 

srcVis = butler.get('src', dataid, immediate=True) 

else: 

srcVis.extend(butler.get('src', dataid, immediate=True), False) 

print(len(srcVis), "sources in camcol : ", c) 

 

match = afwTable.matchRaDec(srcRef, srcVis, afwGeom.Angle(1, afwGeom.arcseconds)) 

matchNum = len(match) 

print("Visit :", v, matchNum, "matches found") 

 

schemaRef = srcRef.getSchema() 

schemaVis = srcVis.getSchema() 

extRefKey = schemaRef["base_ClassificationExtendedness_value"].asKey() 

extVisKey = schemaVis["base_ClassificationExtendedness_value"].asKey() 

flagKeysRef = [] 

flagKeysVis = [] 

for fl in flags: 

keyRef = schemaRef[fl].asKey() 

flagKeysRef.append(keyRef) 

keyVis = schemaVis[fl].asKey() 

flagKeysVis.append(keyVis) 

 

for m in match: 

mRef = m.first 

mVis = m.second 

 

for fl in flagKeysRef: 

if mRef.get(fl): 

continue 

for fl in flagKeysVis: 

if mVis.get(fl): 

continue 

 

# cleanup the reference sources in order to keep only decent star-like objects 

if mRef.get(extRefKey) >= 1.0 or mVis.get(extVisKey) >= 1.0: 

continue 

 

ang = afwGeom.radToMas(m.distance) 

 

# retrieve the camcol corresponding to the reference source 

camcolRef = mRef.get('camcol') 

# retrieve the calibration object associated to the camcol 

did = {'run': ref, 'filter': filter, 'field': ref_field, 'camcol': camcolRef} 

photoCalib = butler.get("calexp_photoCalib", did) 

# compute magnitude 

refMag = photoCalib.instFluxToMagnitude(mRef.get('base_PsfFlux_instFlux')) 

 

mag.append(refMag) 

dist.append(ang) 

 

return pipeBase.Struct( 

mag=mag, 

dist=dist, 

match=matchNum 

) 

 

 

def plotAstrometry(mag, dist, match, good_mag_limit=19.5): 

"""Plot angular distance between matched sources from different exposures.""" 

 

# Defer importing of matplotlib until we need it. 

import matplotlib.pylab as plt 

 

plt.rcParams['axes.linewidth'] = 2 

plt.rcParams['mathtext.default'] = 'regular' 

 

fig, ax = plt.subplots(ncols=2, nrows=3, figsize=(18, 22)) 

ax[0][0].hist(dist, bins=80) 

ax[0][1].scatter(mag, dist, s=10, color='b') 

ax[0][0].set_xlim([0., 900.]) 

ax[0][0].set_xlabel("Distance in mas", fontsize=20) 

ax[0][0].tick_params(labelsize=20) 

ax[0][0].set_title("Median : %.1f mas" % (np.median(dist)), fontsize=20, x=0.6, y=0.88) 

ax[0][1].set_xlabel("Magnitude", fontsize=20) 

ax[0][1].set_ylabel("Distance in mas", fontsize=20) 

ax[0][1].set_ylim([0., 900.]) 

ax[0][1].tick_params(labelsize=20) 

ax[0][1].set_title("Number of matches : %d" % match, fontsize=20) 

 

ax[1][0].hist(dist, bins=150) 

ax[1][0].set_xlim([0., 400.]) 

ax[1][1].scatter(mag, dist, s=10, color='b') 

ax[1][1].set_ylim([0., 400.]) 

ax[1][0].set_xlabel("Distance in mas", fontsize=20) 

ax[1][1].set_xlabel("Magnitude", fontsize=20) 

ax[1][1].set_ylabel("Distance in mas", fontsize=20) 

ax[1][0].tick_params(labelsize=20) 

ax[1][1].tick_params(labelsize=20) 

 

idxs = np.where(np.asarray(mag) < good_mag_limit) 

 

ax[2][0].hist(np.asarray(dist)[idxs], bins=100) 

ax[2][0].set_xlabel("Distance in mas - mag < %.1f" % good_mag_limit, fontsize=20) 

ax[2][0].set_xlim([0, 200]) 

ax[2][0].set_title("Median (mag < %.1f) : %.1f mas" % 

(good_mag_limit, np.median(np.asarray(dist)[idxs])), 

fontsize=20, x=0.6, y=0.88) 

ax[2][1].scatter(np.asarray(mag)[idxs], np.asarray(dist)[idxs], s=10, color='b') 

ax[2][1].set_xlabel("Magnitude", fontsize=20) 

ax[2][1].set_ylabel("Distance in mas - mag < %.1f" % good_mag_limit, fontsize=20) 

ax[2][1].set_ylim([0., 200.]) 

ax[2][0].tick_params(labelsize=20) 

ax[2][1].tick_params(labelsize=20) 

 

plt.suptitle("Astrometry Check", fontsize=30) 

plotPath = os.path.join("astrometry_sdss.png") 

plt.savefig(plotPath, format="png") 

 

 

def checkAstrometry(mag, dist, match, 

good_mag_limit=19.5, 

medianRef=100, matchRef=500): 

"""Print out the astrometric scatter for all stars, and for good stars. 

 

@param medianRef Median reference astrometric scatter in arcseconds. 

@param matchRef Should match at least matchRef stars. 

 

Return a boolean indicating whether the astrometric scatter was less than 

the supplied reference, and the astrometric scatter (RMS, arcsec) for all 

good stars. 

 

Notes: 

The scatter and match defaults are appropriate to SDSS are stored here. 

For SDSS, stars with mag < 19.5 should be completely well measured. 

""" 

 

print("Median value of the astrometric scatter - all magnitudes:", 

np.median(dist), "mas") 

 

idxs = np.where(np.asarray(mag) < good_mag_limit) 

astromScatter = np.median(np.asarray(dist)[idxs]) 

print("Astrometric scatter (median) - mag < %.1f :" % good_mag_limit, astromScatter, "mas") 

 

227 ↛ 228line 227 didn't jump to line 228, because the condition on line 227 was never true if astromScatter > medianRef: 

print("Median astrometric scatter %.1f mas is larger than reference : %.1f mas " % 

(astromScatter, medianRef)) 

passed = False 

else: 

passed = True 

233 ↛ 234line 233 didn't jump to line 234, because the condition on line 233 was never true if match < matchRef: 

print("Number of matched sources %d is too small (should be > %d)" % (match, matchRef)) 

passed = False 

 

return passed, astromScatter 

 

 

def main(repo, runs, fields, ref, ref_field, camcol, filter, plot=False): 

"""Main executable. 

 

Returns True if the test passed, False otherwise. 

""" 

 

struct = loadAndMatchData(repo, runs, fields, ref, ref_field, camcol, filter) 

mag = struct.mag 

dist = struct.dist 

match = struct.match 

 

# Limit depends on filter 

medianRef = 100 

253 ↛ 256line 253 didn't jump to line 256, because the condition on line 253 was never false if filter == 'i': 

medianRef = 105 

 

passed, astromScatter = checkAstrometry(mag, dist, match, medianRef=medianRef) 

257 ↛ 258line 257 didn't jump to line 258, because the condition on line 257 was never true if plot: 

plotAstrometry(mag, dist, match) 

return passed 

 

 

def defaultData(repo): 

# List of SDSS runs to be considered 

runs = [4192, 6377] 

fields = [300, 399] 

 

# Reference visit (the other visits will be compared to this one 

ref = 4192 

ref_field = 300 

 

# List of camcol to be considered (source calalogs will be concateneted) 

camcol = [4] 

filter = 'i' 

 

return runs, fields, ref, ref_field, camcol, filter 

 

 

278 ↛ exitline 278 didn't exit the module, because the condition on line 278 was never falseif __name__ == "__main__": 

279 ↛ 280line 279 didn't jump to line 280, because the condition on line 279 was never true if len(sys.argv) != 2: 

print("""Usage: check_astrometry repo 

where repo is the path to a repository containing the output of processCcd 

""") 

sys.exit(1) 

 

repo = sys.argv[1] 

286 ↛ 287line 286 didn't jump to line 287, because the condition on line 286 was never true if not os.path.isdir(repo): 

print("Could not find repo %r" % (repo,)) 

sys.exit(1) 

 

runs, fields, ref, ref_field, camcol, filter = defaultData(repo) 

passed = main(repo, runs, fields, ref, ref_field, camcol, filter) 

292 ↛ 295line 292 didn't jump to line 295, because the condition on line 292 was never false if passed: 

print("Ok.") 

else: 

sys.exit(1)