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

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

 

"""Simple sqlite3 interface for storing and retrieving DIAObjects and 

DIASources. This class is mainly for testing purposes in the context of 

ap_pipe/ap_verify. 

""" 

 

__all__ = ["AssociationL1DBProtoConfig", 

"AssociationL1DBProtoTask"] 

 

import os 

 

from lsst.meas.algorithms.indexerRegistry import IndexerRegistry 

import lsst.afw.table as afwTable 

import lsst.afw.geom as afwGeom 

from lsst.ap.association import \ 

make_minimal_dia_object_schema, \ 

make_minimal_dia_source_schema 

from .afwUtils import convert_dia_source_to_asssoc_schema 

from lsst.dax.ppdb import Ppdb, PpdbConfig 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

 

 

def _data_file_name(basename, module_name): 

"""Return path name of a data file. 

 

Parameters 

---------- 

basename : `str` 

Name of the file to add to the path string. 

module_name : `str` 

Name of lsst stack package environment variable. 

 

Returns 

------- 

data_file_path : `str` 

Fill path of the file to load from the "data" directory in a given 

repository. 

""" 

# TODO: Should probably use getPackageDir(module_name) instead of getenv 

return os.path.join(os.environ.get(module_name), "data", basename) 

 

 

class AssociationL1DBProtoConfig(pexConfig.Config): 

"""Configuration parameters for the AssociationL1DBProtoTask. 

""" 

indexer = IndexerRegistry.makeField( 

doc='Select the spatial indexer to use within the database.', 

default='HTM' 

) 

l1db_config = pexConfig.ConfigField( 

dtype=PpdbConfig, 

doc='Configuration for l1dbproto.', 

) 

filter_names = pexConfig.ListField( 

dtype=str, 

doc='List of filter names to store and expect from in this DB.', 

default=['u', 'g', 'r', 'i', 'z', 'y'], 

) 

 

def setDefaults(self): 

self.l1db_config.db_url = "sqlite:///l1dbproto.db" 

self.l1db_config.isolation_level = "READ_UNCOMMITTED" 

self.l1db_config.dia_object_index = "baseline" 

self.l1db_config.read_sources_months = 12 

self.l1db_config.read_forced_sources_months = 6 

self.l1db_config.object_last_replace = True 

self.l1db_config.explain = False 

self.l1db_config.schema_file = _data_file_name( 

"ppdb-schema.yaml", "DAX_PPDB_DIR") 

self.l1db_config.column_map = _data_file_name( 

"l1db-ap-pipe-afw-map.yaml", "AP_ASSOCIATION_DIR") 

self.l1db_config.extra_schema_file = _data_file_name( 

"l1db-ap-pipe-schema-extra.yaml", "AP_ASSOCIATION_DIR") 

 

 

class AssociationL1DBProtoTask(pipeBase.Task): 

"""Task wrapping `lsst.dax.ppdb` enabling it to be used in ap_association. 

 

Handles computation of HTM indices, trimming of DIAObject catalogs to the 

CCD geometry, and assuring input DIASource catalog schemas are compatible 

with the db. 

""" 

 

ConfigClass = AssociationL1DBProtoConfig 

_DefaultName = "associationL1DBProto" 

 

def __init__(self, **kwargs): 

 

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

self.indexer = IndexerRegistry[self.config.indexer.name]( 

self.config.indexer.active) 

 

self._dia_object_afw_schema = make_minimal_dia_object_schema( 

self.config.filter_names) 

self._dia_source_afw_schema = make_minimal_dia_source_schema() 

 

afw_schema = dict( 

DiaObject=self._dia_object_afw_schema, 

DiaSource=self._dia_source_afw_schema) 

self.db = Ppdb(config=self.config.l1db_config, 

afw_schemas=afw_schema) 

 

def load_dia_objects(self, exposure): 

"""Load all DIAObjects within the exposure. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

An exposure with a solved WCS representing the area on the sky to 

load DIAObjects. 

 

Returns 

------- 

dia_objects : `lsst.afw.table.SourceCatalog` 

Catalog of DIAObjects that are contained with the the bounding 

box defined by the exposure bounding box. 

""" 

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

wcs = exposure.getWcs() 

 

ctr_coord = wcs.pixelToSky(bbox.getCenter()) 

max_radius = max( 

ctr_coord.separation(wcs.pixelToSky(pp)) 

for pp in bbox.getCorners()) 

 

indexer_indices, on_boundry = self.indexer.getShardIds( 

ctr_coord, max_radius) 

# Index types must be cast to int to work with dax_ppdb. 

index_ranges = [[int(indexer_idx), int(indexer_idx) + 1] 

for indexer_idx in indexer_indices] 

covering_dia_objects = self.db.getDiaObjects(index_ranges) 

 

output_dia_objects = afwTable.SourceCatalog( 

covering_dia_objects.getSchema()) 

for cov_dia_object in covering_dia_objects: 

if self._check_dia_object_position(cov_dia_object, bbox, wcs): 

output_dia_objects.append(cov_dia_object) 

 

# Return deep copy to enforce contiguity. 

return output_dia_objects.copy(deep=True) 

 

def _check_dia_object_position(self, dia_object_record, bbox, wcs): 

"""Check the RA, DEC position of the current dia_object_record against 

the bounding box of the exposure. 

 

Parameters 

---------- 

dia_object_record : `lsst.afw.table.SourceRecord` 

A SourceRecord object containing the DIAObject we would like to 

test against our bounding box. 

bbox : `lsst.geom.Box2D` 

Bounding box of exposure. 

wcs : `lsst.geom.SkyWcs` 

WCS of exposure. 

 

Return 

------ 

is_contained : `bool` 

Object position is contained within the bounding box of expMd. 

""" 

point = wcs.skyToPixel(dia_object_record.getCoord()) 

return bbox.contains(point) 

 

def load_dia_sources(self, dia_obj_ids): 

"""Retrieve all DIASources associated with this collection of DIAObject 

ids. 

 

Parameters 

---------- 

dia_obj_ids : array-like of `int` 

Id of the DIAObject that is associated with the DIASources 

of interest. 

 

Returns 

------- 

dia_sources : `lsst.afw.table.SourceCatalog` 

SourceCatalog of DIASources 

""" 

 

# l1db proto does not currently use the dt (dateTime) variables for 

# this function. 

return self.db.getDiaSources(dia_obj_ids, None) 

 

def store_dia_objects(self, 

dia_objects, 

compute_spatial_index=False, 

exposure=None): 

"""Store all DIAObjects in this SourceCatalog. 

 

Parameters 

---------- 

dia_objects : `lsst.afw.table.SourceCatalog` 

Catalog of DIAObjects to store. 

compute_spatial_index : `bool` 

If True, compute the spatial search indices using the 

indexer specified at class instantiation. 

exposure : `lsst.afw.image.Exposure` (optional) 

CcdExposure associated with these DIAObjects being inserted. 

Inserts the CcdVisitInfo for this exposure in the CcdVisitTable. 

""" 

if compute_spatial_index: 

for dia_object in dia_objects: 

pixelId = self.compute_indexer_id(dia_object.getCoord()) 

dia_object['pixelId'] = pixelId 

 

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

self.db.storeDiaObjects(dia_objects, dt) 

 

def compute_indexer_id(self, sphere_point): 

"""Compute the pixel index of the given point. 

 

Parameters 

---------- 

sphere_point : `lsst.afw.geom.SpherePoint` 

Point to compute pixel index for. 

 

Returns 

------- 

index : `int` 

Index of the pixel the point is contained in. 

""" 

return self.indexer.indexPoints( 

[sphere_point.getRa().asDegrees()], 

[sphere_point.getDec().asDegrees()])[0] 

 

def store_dia_sources(self, 

dia_sources, 

associated_ids=None, 

exposure=None): 

"""Store all DIASources in this SourceCatalog. 

 

Parameters 

---------- 

dia_sources : `lsst.afw.table.SourceCatalog` 

Catalog of DIASources to store. 

associated_ids : array-like of `int` (optional) 

DIAObject ids that have been associated with these DIASources 

exposure : `lsst.afw.image.Exposure` 

Exposure object the DIASources were detected in. 

""" 

if (associated_ids is None and exposure is None and 

dia_sources.getSchema().contains( 

make_minimal_dia_source_schema())): 

self.db.storeDiaSources(dia_sources) 

else: 

dia_sources_to_store = convert_dia_source_to_asssoc_schema( 

dia_sources, associated_ids, exposure) 

 

self.db.storeDiaSources(dia_sources_to_store) 

 

@property 

def dia_object_afw_schema(self): 

"""Retrieve the Schema of the DIAObjects in this database. 

 

Returns 

------- 

schema : `lsst.afw.table.Schema` 

Schema of the DIAObjects in this database. 

""" 

return self._dia_object_afw_schema 

 

@property 

def dia_source_afw_schema(self): 

"""Retrieve the Schema of the DIASources in this database. 

 

Returns 

------- 

schema : `lsst.afw.table.Schema` 

Schema of the DIASources in this database. 

""" 

return self._dia_source_afw_schema