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

# This file is part of ap_association. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://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 <https://www.gnu.org/licenses/>. 

 

"""Methods for force photometering direct and difference images at DiaObject 

locations. 

""" 

 

__all__ = ["DiaForcedSourceTask", "DiaForcedSourcedConfig"] 

 

import lsst.afw.table as afwTable 

from lsst.daf.base import DateTime 

import lsst.geom as geom 

from lsst.meas.base.pluginRegistry import register 

from lsst.meas.base import ( 

ForcedMeasurementTask, 

ForcedTransformedCentroidConfig, 

ForcedTransformedCentroidPlugin) 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

 

 

class ForcedTransformedCentroidFromCoordConfig(ForcedTransformedCentroidConfig): 

"""Configuration for the forced transformed coord algorithm. 

""" 

pass 

 

 

@register("ap_assoc_TransformedCentroid") 

class ForcedTransformedCentroidFromCoordPlugin(ForcedTransformedCentroidPlugin): 

"""Record the transformation of the reference catalog coord. 

The coord recorded in the reference catalog is tranformed to the 

measurement coordinate system and stored. 

 

Parameters 

---------- 

config : `ForcedTransformedCentroidFromCoordConfig` 

Plugin configuration 

name : `str` 

Plugin name 

schemaMapper : `lsst.afw.table.SchemaMapper` 

A mapping from reference catalog fields to output 

catalog fields. Output fields are added to the output schema. 

metadata : `lsst.daf.base.PropertySet` 

Plugin metadata that will be attached to the output catalog. 

 

Notes 

----- 

This can be used as the slot centroid in forced measurement when only a 

reference coord exits, allowing subsequent measurements to simply refer to 

the slot value just as they would in single-frame measurement. 

""" 

 

ConfigClass = ForcedTransformedCentroidFromCoordConfig 

 

def measure(self, measRecord, exposure, refRecord, refWcs): 

targetWcs = exposure.getWcs() 

 

targetPos = targetWcs.skyToPixel(refRecord.getCoord()) 

measRecord.set(self.centroidKey, targetPos) 

 

if self.flagKey is not None: 

measRecord.set(self.flagKey, refRecord.getCentroidFlag()) 

 

 

class DiaForcedSourcedConfig(pexConfig.Config): 

"""Configuration for the generic DiaForcedSourcedTask class. 

""" 

forcedMeasurement = pexConfig.ConfigurableField( 

target=ForcedMeasurementTask, 

doc="Subtask to force photometer DiaObjects in the direct and " 

"difference images.", 

) 

dropColumns = pexConfig.ListField( 

dtype=str, 

doc="Columns produced in forced measurement that can be dropped upon " 

"creation and storage of the final pandas data.", 

) 

 

def setDefaults(self): 

self.forcedMeasurement.plugins = ["ap_assoc_TransformedCentroid", 

"base_PsfFlux"] 

self.forcedMeasurement.doReplaceWithNoise = False 

self.forcedMeasurement.copyColumns = { 

"id": "diaObjectId", 

"coord_ra": "coord_ra", 

"coord_dec": "coord_dec"} 

self.forcedMeasurement.slots.centroid = "ap_assoc_TransformedCentroid" 

self.forcedMeasurement.slots.psfFlux = "base_PsfFlux" 

self.forcedMeasurement.slots.shape = None 

self.dropColumns = ['coord_ra', 'coord_dec', 'parent', 

'ap_assoc_TransformedCentroid_x', 

'ap_assoc_TransformedCentroid_y', 

'base_PsfFlux_instFlux', 

'base_PsfFlux_instFluxErr', 'base_PsfFlux_area', 

'slot_PsfFlux_area', 'base_PsfFlux_flag', 

'slot_PsfFlux_flag', 

'base_PsfFlux_flag_noGoodPixels', 

'slot_PsfFlux_flag_noGoodPixels', 

'base_PsfFlux_flag_edge', 'slot_PsfFlux_flag_edge'] 

 

 

class DiaForcedSourceTask(pipeBase.Task): 

"""Task for measuring and storing forced sources at DiaObject locations 

in both difference and direct images. 

""" 

ConfigClass = DiaForcedSourcedConfig 

_DefaultName = "diaForcedSource" 

 

def __init__(self, **kwargs): 

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

self.makeSubtask("forcedMeasurement", 

refSchema=afwTable.SourceTable.makeMinimalSchema()) 

 

def run(self, dia_objects, expIdBits, exposure, diffim): 

"""Measure forced sources on the direct and difference images. 

 

Parameters 

---------- 

dia_objects : `pandas.DataFrame` 

Catalog of previously observed and newly created DiaObjects 

contained within the difference and direct images. 

expIdBits : `int` 

Bit length of the exposure id. 

exposure : `lsst.afw.image.Exposure` 

Direct image exposure. 

diffim : `lsst.afw.image.Exposure` 

Difference image. 

 

Returns 

------- 

output_forced_sources : `pandas.DataFrame` 

Catalog of calibrated forced photometered fluxes on both the 

difference and direct images at DiaObject locations. 

""" 

 

afw_dia_objects = self._convert_from_pandas(dia_objects) 

 

idFactoryDiff = afwTable.IdFactory.makeSource( 

diffim.getInfo().getVisitInfo().getExposureId(), 

afwTable.IdFactory.computeReservedFromMaxBits(int(expIdBits))) 

 

diffForcedSources = self.forcedMeasurement.generateMeasCat( 

diffim, 

afw_dia_objects, 

diffim.getWcs(), 

idFactory=idFactoryDiff) 

self.forcedMeasurement.run( 

diffForcedSources, diffim, afw_dia_objects, diffim.getWcs()) 

 

directForcedSources = self.forcedMeasurement.generateMeasCat( 

exposure, 

afw_dia_objects, 

exposure.getWcs()) 

self.forcedMeasurement.run( 

directForcedSources, exposure, afw_dia_objects, exposure.getWcs()) 

 

output_forced_sources = self._calibrate_and_merge(diffForcedSources, 

directForcedSources, 

diffim, 

exposure) 

 

return output_forced_sources 

 

def _convert_from_pandas(self, input_objects): 

"""Create minimal schema SourceCatalog from a pandas DataFrame. 

 

We need a catalog of this type to run within the forced measurement 

subtask. 

 

Parameters 

---------- 

input_objects : `pandas.DataFrame` 

DiaObjects with locations and ids. `` 

 

Returns 

------- 

outputCatalog : `lsst.afw.table.SourceTable` 

Output catalog with minimal schema. 

""" 

schema = afwTable.SourceTable.makeMinimalSchema() 

 

outputCatalog = afwTable.SourceCatalog(schema) 

outputCatalog.reserve(len(input_objects)) 

 

for obj_id, df_row in input_objects.iterrows(): 

outputRecord = outputCatalog.addNew() 

outputRecord.setId(obj_id) 

outputRecord.setCoord( 

geom.SpherePoint(df_row["ra"], 

df_row["decl"], 

geom.degrees)) 

return outputCatalog 

 

def _calibrate_and_merge(self, 

diff_sources, 

direct_sources, 

diff_exp, 

direct_exp): 

"""Take the two output catalogs from the ForcedMeasurementTasks and 

calibrate, combine, and convert them to Pandas. 

 

Parameters 

---------- 

diff_sources : `lsst.afw.table.SourceTable` 

Catalog with PsFluxes measured on the difference image. 

direct_sources : `lsst.afw.table.SourceTable` 

Catalog with PsfFluxes measured on the direct (calexp) image. 

diff_exp : `lsst.afw.image.Exposure` 

Difference exposure ``diff_sources`` were measured on. 

direct_exp : `lsst.afw.image.Exposure` 

Direct (calexp) exposure ``direct_sources`` were measured on. 

 

Returns 

------- 

output_catalog : `pandas.DataFrame` 

Catalog calibrated diaForcedSources. 

""" 

diff_calib = diff_exp.getPhotoCalib() 

direct_calib = direct_exp.getPhotoCalib() 

 

diff_fluxes = diff_calib.instFluxToNanojansky(diff_sources, 

"slot_PsfFlux") 

direct_fluxes = direct_calib.instFluxToNanojansky(direct_sources, 

"slot_PsfFlux") 

 

output_catalog = diff_sources.asAstropy().to_pandas() 

output_catalog.rename(columns={"id": "diaForcedSourceId", 

"slot_PsfFlux_instFlux": "psFlux", 

"slot_PsfFlux_instFluxErr": "psFluxErr", 

"slot_Centroid_x": "x", 

"slot_Centroid_y": "y"}, 

inplace=True) 

output_catalog.loc[:, "psFlux"] = diff_fluxes[:, 0] 

output_catalog.loc[:, "psFluxErr"] = diff_fluxes[:, 1] 

 

output_catalog["totFlux"] = direct_fluxes[:, 0] 

output_catalog["totFluxErr"] = direct_fluxes[:, 1] 

 

visit_info = direct_exp.getInfo().getVisitInfo() 

ccdVisitId = visit_info.getExposureId() 

midPointTaiMJD = visit_info.getDate().get(system=DateTime.MJD) 

output_catalog["ccdVisitId"] = ccdVisitId 

output_catalog["midPointTai"] = midPointTaiMJD 

 

# Drop superfluous columns from output DataFrame. 

output_catalog.drop(columns=self.config.dropColumns, inplace=True) 

 

return output_catalog