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

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 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/>. 

# 

 

__all__ = ["IngestIndexedReferenceConfig", "IngestIndexedReferenceTask", "DatasetConfig"] 

 

import math 

 

import astropy.time 

import numpy as np 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

import lsst.geom 

import lsst.afw.table as afwTable 

from lsst.afw.image import fluxFromABMag, fluxErrFromABMagErr 

from .indexerRegistry import IndexerRegistry 

from .readTextCatalogTask import ReadTextCatalogTask 

from .loadReferenceObjects import LoadReferenceObjectsTask 

 

_RAD_PER_DEG = math.pi / 180 

_RAD_PER_MILLIARCSEC = _RAD_PER_DEG/(3600*1000) 

 

 

class IngestReferenceRunner(pipeBase.TaskRunner): 

"""Task runner for the reference catalog ingester 

 

Data IDs are ignored so the runner should just run the task on the parsed command. 

""" 

 

def run(self, parsedCmd): 

"""Run the task. 

 

Several arguments need to be collected to send on to the task methods. 

 

Parameters 

---------- 

parsedCmd : `argparse.Namespace` 

Parsed command. 

 

Returns 

------- 

results : `lsst.pipe.base.Struct` or `None` 

A empty struct if self.doReturnResults, else None 

""" 

files = parsedCmd.files 

butler = parsedCmd.butler 

task = self.TaskClass(config=self.config, log=self.log, butler=butler) 

task.writeConfig(parsedCmd.butler, clobber=self.clobberConfig, doBackup=self.doBackup) 

 

task.createIndexedCatalog(files) 

71 ↛ 72line 71 didn't jump to line 72, because the condition on line 71 was never true if self.doReturnResults: 

return pipeBase.Struct() 

 

 

class DatasetConfig(pexConfig.Config): 

ref_dataset_name = pexConfig.Field( 

dtype=str, 

default='cal_ref_cat', 

doc='String to pass to the butler to retrieve persisted files.', 

) 

indexer = IndexerRegistry.makeField( 

default='HTM', 

doc='Name of indexer algoritm to use. Default is HTM', 

) 

 

 

class IngestIndexedReferenceConfig(pexConfig.Config): 

dataset_config = pexConfig.ConfigField( 

dtype=DatasetConfig, 

doc="Configuration for reading the ingested data", 

) 

file_reader = pexConfig.ConfigurableField( 

target=ReadTextCatalogTask, 

doc='Task to use to read the files. Default is to expect text files.' 

) 

ra_name = pexConfig.Field( 

dtype=str, 

doc="Name of RA column", 

) 

dec_name = pexConfig.Field( 

dtype=str, 

doc="Name of Dec column", 

) 

ra_err_name = pexConfig.Field( 

dtype=str, 

doc="Name of RA error column", 

optional=True, 

) 

dec_err_name = pexConfig.Field( 

dtype=str, 

doc="Name of Dec error column", 

optional=True, 

) 

mag_column_list = pexConfig.ListField( 

dtype=str, 

doc="The values in the reference catalog are assumed to be in AB magnitudes. " 

"List of column names to use for photometric information. At least one entry is required." 

) 

mag_err_column_map = pexConfig.DictField( 

keytype=str, 

itemtype=str, 

default={}, 

doc="A map of magnitude column name (key) to magnitude error column (value)." 

) 

is_photometric_name = pexConfig.Field( 

dtype=str, 

optional=True, 

doc='Name of column stating if satisfactory for photometric calibration (optional).' 

) 

is_resolved_name = pexConfig.Field( 

dtype=str, 

optional=True, 

doc='Name of column stating if the object is resolved (optional).' 

) 

is_variable_name = pexConfig.Field( 

dtype=str, 

optional=True, 

doc='Name of column stating if the object is measured to be variable (optional).' 

) 

id_name = pexConfig.Field( 

dtype=str, 

optional=True, 

doc='Name of column to use as an identifier (optional).' 

) 

pm_ra_name = pexConfig.Field( 

dtype=str, 

doc="Name of proper motion RA column", 

optional=True, 

) 

pm_dec_name = pexConfig.Field( 

dtype=str, 

doc="Name of proper motion Dec column", 

optional=True, 

) 

pm_ra_err_name = pexConfig.Field( 

dtype=str, 

doc="Name of proper motion RA error column", 

optional=True, 

) 

pm_dec_err_name = pexConfig.Field( 

dtype=str, 

doc="Name of proper motion Dec error column", 

optional=True, 

) 

pm_scale = pexConfig.Field( 

dtype=float, 

doc="Scale factor by which to multiply proper motion values to obtain units of milliarcsec/year", 

default=1.0, 

) 

parallax_name = pexConfig.Field( 

dtype=str, 

doc="Name of parallax column", 

optional=True, 

) 

parallax_err_name = pexConfig.Field( 

dtype=str, 

doc="Name of parallax error column", 

optional=True, 

) 

parallax_scale = pexConfig.Field( 

dtype=float, 

doc="Scale factor by which to multiply parallax values to obtain units of milliarcsec", 

default=1.0, 

) 

epoch_name = pexConfig.Field( 

dtype=str, 

doc="Name of epoch column", 

optional=True, 

) 

epoch_format = pexConfig.Field( 

dtype=str, 

doc="Format of epoch column: any value accepted by astropy.time.Time, e.g. 'iso' or 'unix'", 

optional=True, 

) 

epoch_scale = pexConfig.Field( 

dtype=str, 

doc="Scale of epoch column: any value accepted by astropy.time.Time, e.g. 'utc'", 

optional=True, 

) 

extra_col_names = pexConfig.ListField( 

dtype=str, 

default=[], 

doc='Extra columns to add to the reference catalog.' 

) 

 

def validate(self): 

pexConfig.Config.validate(self) 

 

def assertAllOrNone(*names): 

"""Raise ValueError unless all the named fields are set or are 

all none (or blank) 

""" 

setNames = [name for name in names if bool(getattr(self, name))] 

if len(setNames) in (len(names), 0): 

return 

prefix = "Both or neither" if len(names) == 2 else "All or none" 

raise ValueError("{} of {} must be set, but only {} are set".format( 

prefix, ", ".join(names), ", ".join(setNames))) 

 

220 ↛ 221line 220 didn't jump to line 221, because the condition on line 220 was never true if not (self.ra_name and self.dec_name and self.mag_column_list): 

raise ValueError( 

"ra_name and dec_name and at least one entry in mag_column_list must be supplied.") 

if self.mag_err_column_map and set(self.mag_column_list) != set(self.mag_err_column_map.keys()): 

raise ValueError( 

"mag_err_column_map specified, but keys do not match mag_column_list: {} != {}".format( 

sorted(self.mag_err_column_map.keys()), sorted(self.mag_column_list))) 

assertAllOrNone("ra_err_name", "dec_err_name") 

assertAllOrNone("epoch_name", "epoch_format", "epoch_scale") 

assertAllOrNone("pm_ra_name", "pm_dec_name") 

assertAllOrNone("pm_ra_err_name", "pm_dec_err_name") 

231 ↛ 232line 231 didn't jump to line 232, because the condition on line 231 was never true if self.pm_ra_err_name and not self.pm_ra_name: 

raise ValueError('"pm_ra/dec_name" must be specified if "pm_ra/dec_err_name" are specified') 

233 ↛ 234line 233 didn't jump to line 234, because the condition on line 233 was never true if (self.pm_ra_name or self.parallax_name) and not self.epoch_name: 

raise ValueError( 

'"epoch_name" must be specified if "pm_ra/dec_name" or "parallax_name" are specified') 

 

 

class IngestIndexedReferenceTask(pipeBase.CmdLineTask): 

"""Class for producing and loading indexed reference catalogs. 

 

This implements an indexing scheme based on hierarchical triangular 

mesh (HTM). The term index really means breaking the catalog into 

localized chunks called shards. In this case each shard contains 

the entries from the catalog in a single HTM trixel 

 

For producing catalogs this task makes the following assumptions 

about the input catalogs: 

- RA, Dec, RA error and Dec error are all in decimal degrees. 

- Epoch is available in a column, in a format supported by astropy.time.Time. 

- There are no off-diagonal covariance terms, such as covariance 

between RA and Dec, or between PM RA and PM Dec. Gaia is a well 

known example of a catalog that has such terms, and thus should not 

be ingested with this task. 

 

Parameters 

---------- 

butler : `lsst.daf.persistence.Butler` 

Data butler for reading and writing catalogs 

""" 

canMultiprocess = False 

ConfigClass = IngestIndexedReferenceConfig 

RunnerClass = IngestReferenceRunner 

_DefaultName = 'IngestIndexedReferenceTask' 

 

_flags = ['photometric', 'resolved', 'variable'] 

 

@classmethod 

def _makeArgumentParser(cls): 

"""Create an argument parser. 

 

This returns a standard parser with an extra "files" argument. 

""" 

parser = pipeBase.InputOnlyArgumentParser(name=cls._DefaultName) 

parser.add_argument("files", nargs="+", help="Names of files to index") 

return parser 

 

def __init__(self, *args, **kwargs): 

self.butler = kwargs.pop('butler') 

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

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

self.config.dataset_config.indexer.active) 

self.makeSubtask('file_reader') 

 

def createIndexedCatalog(self, files): 

"""Index a set of files comprising a reference catalog. 

 

Outputs are persisted in the data repository. 

 

Parameters 

---------- 

files : `list` 

A list of file paths to read. 

""" 

rec_num = 0 

first = True 

for filename in files: 

arr = self.file_reader.run(filename) 

index_list = self.indexer.indexPoints(arr[self.config.ra_name], arr[self.config.dec_name]) 

if first: 

schema, key_map = self.makeSchema(arr.dtype) 

# persist empty catalog to hold the master schema 

dataId = self.indexer.makeDataId('master_schema', 

self.config.dataset_config.ref_dataset_name) 

self.butler.put(self.getCatalog(dataId, schema), 'ref_cat', 

dataId=dataId) 

first = False 

pixel_ids = set(index_list) 

for pixel_id in pixel_ids: 

dataId = self.indexer.makeDataId(pixel_id, self.config.dataset_config.ref_dataset_name) 

catalog = self.getCatalog(dataId, schema) 

els = np.where(index_list == pixel_id) 

for row in arr[els]: 

record = catalog.addNew() 

rec_num = self._fillRecord(record, row, rec_num, key_map) 

self.butler.put(catalog, 'ref_cat', dataId=dataId) 

dataId = self.indexer.makeDataId(None, self.config.dataset_config.ref_dataset_name) 

self.butler.put(self.config.dataset_config, 'ref_cat_config', dataId=dataId) 

 

@staticmethod 

def computeCoord(row, ra_name, dec_name): 

"""Create an ICRS coord. from a row of a catalog being ingested. 

 

Parameters 

---------- 

row : structured `numpy.array` 

Row from catalog being ingested. 

ra_name : `str` 

Name of RA key in catalog being ingested. 

dec_name : `str` 

Name of Dec key in catalog being ingested. 

 

Returns 

------- 

coord : `lsst.geom.SpherePoint` 

ICRS coordinate. 

""" 

return lsst.geom.SpherePoint(row[ra_name], row[dec_name], lsst.geom.degrees) 

 

def _setCoordErr(self, record, row, key_map): 

"""Set coordinate error in a record of an indexed catalog. 

 

The errors are read from the specified columns, and installed 

in the appropriate columns of the output. 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

354 ↛ exitline 354 didn't return from function '_setCoordErr', because the condition on line 354 was never false if self.config.ra_err_name: # IngestIndexedReferenceConfig.validate ensures all or none 

record.set(key_map["coord_raErr"], row[self.config.ra_err_name]*_RAD_PER_DEG) 

record.set(key_map["coord_decErr"], row[self.config.dec_err_name]*_RAD_PER_DEG) 

 

def _setFlags(self, record, row, key_map): 

"""Set flags in an output record 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

names = record.schema.getNames() 

for flag in self._flags: 

if flag in names: 

attr_name = 'is_{}_name'.format(flag) 

record.set(key_map[flag], bool(row[getattr(self.config, attr_name)])) 

 

def _setFlux(self, record, row, key_map): 

"""Set flux fields in a record of an indexed catalog. 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

for item in self.config.mag_column_list: 

record.set(key_map[item+'_flux'], fluxFromABMag(row[item])) 

390 ↛ exitline 390 didn't return from function '_setFlux', because the condition on line 390 was never false if len(self.config.mag_err_column_map) > 0: 

for err_key in self.config.mag_err_column_map.keys(): 

error_col_name = self.config.mag_err_column_map[err_key] 

record.set(key_map[err_key+'_fluxErr'], 

fluxErrFromABMagErr(row[error_col_name], row[err_key])) 

 

def _setProperMotion(self, record, row, key_map): 

"""Set proper motion fields in a record of an indexed catalog. 

 

The proper motions are read from the specified columns, 

scaled appropriately, and installed in the appropriate 

columns of the output. 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

412 ↛ 413line 412 didn't jump to line 413, because the condition on line 412 was never true if self.config.pm_ra_name is None: # IngestIndexedReferenceConfig.validate ensures all or none 

return 

radPerOriginal = _RAD_PER_MILLIARCSEC*self.config.pm_scale 

record.set(key_map["pm_ra"], row[self.config.pm_ra_name]*radPerOriginal*lsst.geom.radians) 

record.set(key_map["pm_dec"], row[self.config.pm_dec_name]*radPerOriginal*lsst.geom.radians) 

record.set(key_map["epoch"], self._epochToMjdTai(row[self.config.epoch_name])) 

418 ↛ exitline 418 didn't return from function '_setProperMotion', because the condition on line 418 was never false if self.config.pm_ra_err_name is not None: # pm_dec_err_name also, by validation 

record.set(key_map["pm_raErr"], row[self.config.pm_ra_err_name]*radPerOriginal) 

record.set(key_map["pm_decErr"], row[self.config.pm_dec_err_name]*radPerOriginal) 

 

def _epochToMjdTai(self, nativeEpoch): 

"""Convert an epoch in native format to TAI MJD (a float). 

""" 

return astropy.time.Time(nativeEpoch, format=self.config.epoch_format, 

scale=self.config.epoch_scale).tai.mjd 

 

def _setExtra(self, record, row, key_map): 

"""Set extra data fields in a record of an indexed catalog. 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

for extra_col in self.config.extra_col_names: 

value = row[extra_col] 

# If data read from a text file contains string like entires, 

# numpy stores this as its own internal type, a numpy.str_ 

# object. This seems to be a consequence of how numpy stores 

# string like objects in fixed column arrays. This checks 

# if any of the values to be added to the catalog are numpy 

# string types, and if they are, casts them to a python string 

# which is what the python c++ records expect 

if isinstance(value, np.str_): 

value = str(value) 

record.set(key_map[extra_col], value) 

 

def _fillRecord(self, record, row, rec_num, key_map): 

"""Fill a record in an indexed catalog to be persisted. 

 

Parameters 

---------- 

record : `lsst.afw.table.SimpleRecord` 

Row from indexed catalog to modify. 

row : structured `numpy.array` 

Row from catalog being ingested. 

rec_num : `int` 

Starting integer to increment for the unique id 

key_map : `dict` mapping `str` to `lsst.afw.table.Key` 

Map of catalog keys. 

""" 

record.setCoord(self.computeCoord(row, self.config.ra_name, self.config.dec_name)) 

if self.config.id_name: 

record.setId(row[self.config.id_name]) 

else: 

rec_num += 1 

record.setId(rec_num) 

 

self._setCoordErr(record, row, key_map) 

self._setFlags(record, row, key_map) 

self._setFlux(record, row, key_map) 

self._setProperMotion(record, row, key_map) 

self._setExtra(record, row, key_map) 

return rec_num 

 

def getCatalog(self, dataId, schema): 

"""Get a catalog from the butler or create it if it doesn't exist. 

 

Parameters 

---------- 

dataId : `dict` 

Identifier for catalog to retrieve 

schema : `lsst.afw.table.Schema` 

Schema to use in catalog creation if the butler can't get it 

 

Returns 

------- 

catalog : `lsst.afw.table.SimpleCatalog` 

The catalog specified by `dataId` 

""" 

if self.butler.datasetExists('ref_cat', dataId=dataId): 

return self.butler.get('ref_cat', dataId=dataId) 

return afwTable.SimpleCatalog(schema) 

 

def makeSchema(self, dtype): 

"""Make the schema to use in constructing the persisted catalogs. 

 

Parameters 

---------- 

dtype : `numpy.dtype` 

Data type describing each entry in ``config.extra_col_names`` 

for the catalogs being ingested. 

 

Returns 

------- 

schemaAndKeyMap : `tuple` of (`lsst.afw.table.Schema`, `dict`) 

A tuple containing two items: 

- The schema for the output source catalog. 

- A map of catalog keys to use in filling the record 

""" 

self.config.validate() # just to be sure 

 

# make a schema with the standard fields 

schema = LoadReferenceObjectsTask.makeMinimalSchema( 

filterNameList=self.config.mag_column_list, 

addFluxErr=bool(self.config.mag_err_column_map), 

addCentroid=False, 

addIsPhotometric=bool(self.config.is_photometric_name), 

addIsResolved=bool(self.config.is_resolved_name), 

addIsVariable=bool(self.config.is_variable_name), 

coordErrDim=2 if bool(self.config.ra_err_name) else 0, 

addProperMotion=2 if bool(self.config.pm_ra_name) else 0, 

properMotionErrDim=2 if bool(self.config.pm_ra_err_name) else 0, 

addParallax=bool(self.config.parallax_name), 

addParallaxErr=bool(self.config.parallax_err_name), 

) 

keysToSkip = set(("id", "centroid_x", "centroid_y", "hasCentroid")) 

key_map = {fieldName: schema[fieldName].asKey() for fieldName in schema.getOrderedNames() 

if fieldName not in keysToSkip} 

 

def addField(name): 

if dtype[name].kind == 'U': 

# dealing with a string like thing. Need to get type and size. 

at_size = dtype[name].itemsize 

return schema.addField(name, type=str, size=at_size) 

else: 

at_type = dtype[name].type 

return schema.addField(name, at_type) 

 

for col in self.config.extra_col_names: 

key_map[col] = addField(col) 

return schema, key_map