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

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

 

"""Task for pre-loading DiaSources and DiaObjects within ap_pipe. 

""" 

import pandas as pd 

 

import lsst.geom as geom 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

import lsst.sphgeom as sphgeom 

 

__all__ = ("LoadDiaCatalogsTask", "LoadDiaCatalogsConfig") 

 

 

class LoadDiaCatalogsConfig(pexConfig.Config): 

"""Config class for LoadDiaCatalogsConfig. 

""" 

htmLevel = pexConfig.RangeField( 

dtype=int, 

doc="Level of the HTM pixelization.", 

default=20, 

min=1, 

) 

htmMaxRanges = pexConfig.RangeField( 

dtype=int, 

doc="Maximum number of HTM (min, max) ranges to return.", 

default=128, 

min=2, 

) 

pixelMargin = pexConfig.RangeField( 

doc="Padding to add to 4 all edges of the bounding box (pixels)", 

dtype=int, 

default=300, 

min=0, 

) 

loadDiaSourcesByPixelId = pexConfig.Field( 

doc="Load DiaSources by their HTM pixelId instead of by their " 

"associated diaObjectId", 

dtype=bool, 

default=False, 

) 

 

 

class LoadDiaCatalogsTask(pipeBase.Task): 

"""Retrieve DiaObjects and associated DiaSources from the Apdb given an 

input exposure. 

""" 

ConfigClass = LoadDiaCatalogsConfig 

_DefaultName = "loadDiaCatalogs" 

 

def __init__(self, **kwargs): 

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

self.pixelator = sphgeom.HtmPixelization(self.config.htmLevel) 

 

@pipeBase.timeMethod 

def run(self, exposure, apdb): 

"""Preload all DiaObjects and DiaSources from the Apdb given the 

current exposure. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

An exposure with a bounding box. 

apdb : `lsst.dax.apdb.Apdb` 

AP database connection object. 

 

Returns 

------- 

result : `lsst.pipe.base.Struct` 

Results struct with components. 

 

- ``diaObjects`` : Complete set of DiaObjects covering the input 

exposure padded by ``pixelMargin``. DataFrame is indexed by 

the ``diaObjectId`` column. (`pandas.DataFrame`) 

- ``diaSources`` : Complete set of DiaSources covering the input 

exposure padded by ``pixelMargin``. DataFrame is indexed by 

``diaObjectId``, ``filterName``, ``diaSourceId`` columns. 

(`pandas.DataFrame`) 

""" 

pixelRanges = self._getPixelRanges(exposure) 

 

diaObjects = self.loadDiaObjects(pixelRanges, apdb) 

 

dateTime = exposure.getInfo().getVisitInfo().getDate().toPython() 

 

diaSources = self.loadDiaSources(diaObjects, 

dateTime, 

pixelRanges, 

apdb) 

 

return pipeBase.Struct( 

diaObjects=diaObjects, 

diaSources=diaSources) 

 

@pipeBase.timeMethod 

def loadDiaObjects(self, pixelRanges, apdb): 

"""Load DiaObjects from the Apdb based on their HTM location. 

 

Parameters 

---------- 

pixelRanges : `tuple` [`int`] 

Ranges of pixel values that cover region of interest. 

apdb : `lsst.dax.apdb.Apdb` 

Database connection object to load from. 

 

Returns 

------- 

diaObjects : `pandas.DataFrame` 

DiaObjects loaded from the Apdb that are within the area defined 

by ``pixelRanges``. 

""" 

if len(pixelRanges) == 0: 

# If no area is specified return an empty DataFrame with the 

# the column used for indexing later in AssociationTask. 

diaObjects = pd.DataFrame(columns=["diaObjectId"]) 

else: 

diaObjects = apdb.getDiaObjects(pixelRanges, return_pandas=True) 

diaObjects.set_index("diaObjectId", drop=False, inplace=True) 

return diaObjects 

 

@pipeBase.timeMethod 

def loadDiaSources(self, diaObjects, pixelRanges, dateTime, apdb): 

"""Load DiaSources from the Apdb based on their diaObjectId or 

pixelId location. 

 

Variable used to load sources is set in config. 

 

Parameters 

---------- 

diaObjects : `pandas.DataFrame` 

DiaObjects loaded from the Apdb that are within the area defined 

by ``pixelRanges``. 

pixelRanges : `list` of `tuples` 

Ranges of pixelIds that cover region of interest. 

dataTime : `datetime.datetime` 

Time of the current visit 

apdb : `lsst.dax.apdb.Apdb` 

Database connection object to load from. 

 

Returns 

------- 

DiaSources : `pandas.DataFrame` 

DiaSources loaded from the Apdb that are within the area defined 

by ``pixelRange`` and associated with ``diaObjects``. 

""" 

if self.config.loadDiaSourcesByPixelId: 

if len(pixelRanges) == 0: 

# If no area is specified return an empty DataFrame with the 

# the column used for indexing later in AssociationTask. 

diaSources = pd.DataFrame(columns=["diaObjectId", 

"filterName", 

"diaSourceId"]) 

else: 

diaSources = apdb.getDiaSourcesInRegion(pixelRanges, 

dateTime, 

return_pandas=True) 

else: 

if len(diaObjects) == 0: 

# If no diaObjects are available return an empty DataFrame with 

# the the column used for indexing later in AssociationTask. 

diaSources = pd.DataFrame(columns=["diaObjectId", 

"filterName", 

"diaSourceId"]) 

else: 

diaSources = apdb.getDiaSources( 

diaObjects.loc[:, "diaObjectId"], 

dateTime, 

return_pandas=True) 

 

diaSources.set_index(["diaObjectId", "filterName", "diaSourceId"], 

drop=False, 

inplace=True) 

return diaSources 

 

@pipeBase.timeMethod 

def _getPixelRanges(self, exposure): 

"""Calculate covering HTM pixels for the current exposure. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Exposure object with calibrated WCS. 

 

Returns 

------- 

htmRanges : `list` of `tuples` 

A list of tuples containing `int` values. 

""" 

bbox = geom.Box2D(exposure.getBBox()) 

bbox.grow(self.config.pixelMargin) 

wcs = exposure.getWcs() 

 

region = sphgeom.ConvexPolygon([wcs.pixelToSky(pp).getVector() 

for pp in bbox.getCorners()]) 

 

indices = self.pixelator.envelope(region, self.config.htmMaxRanges) 

 

return indices.ranges()