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

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

# See COPYRIGHT file at the top of the source tree. 

# 

# This file is part of fgcmcal. 

# 

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

"""Build star observations for input to FGCM. 

 

This task finds all the visits and calexps in a repository (or a subset 

based on command line parameters) and extract all the potential calibration 

stars for input into fgcm. This task additionally uses fgcm to match 

star observations into unique stars, and performs as much cleaning of 

the input catalog as possible. 

""" 

 

import sys 

import time 

import traceback 

 

import numpy as np 

 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

import lsst.afw.table as afwTable 

import lsst.geom as geom 

from lsst.daf.base.dateTime import DateTime 

import lsst.daf.persistence.butlerExceptions as butlerExceptions 

from lsst.meas.algorithms.sourceSelector import sourceSelectorRegistry 

 

from .fgcmLoadReferenceCatalog import FgcmLoadReferenceCatalogTask 

 

import fgcm 

 

__all__ = ['FgcmBuildStarsConfig', 'FgcmBuildStarsTask', 'FgcmBuildStarsRunner'] 

 

 

class FgcmBuildStarsConfig(pexConfig.Config): 

"""Config for FgcmBuildStarsTask""" 

 

instFluxField = pexConfig.Field( 

doc=("Name of the source instFlux field to use. The associated flag field " 

"('<name>_flag') will be implicitly included in badFlags"), 

dtype=str, 

default='slot_CalibFlux_instFlux', 

) 

minPerBand = pexConfig.Field( 

doc="Minimum observations per band", 

dtype=int, 

default=2, 

) 

matchRadius = pexConfig.Field( 

doc="Match radius (arcseconds)", 

dtype=float, 

default=1.0, 

) 

isolationRadius = pexConfig.Field( 

doc="Isolation radius (arcseconds)", 

dtype=float, 

default=2.0, 

) 

densityCutNside = pexConfig.Field( 

doc="Density cut healpix nside", 

dtype=int, 

default=128, 

) 

densityCutMaxPerPixel = pexConfig.Field( 

doc="Density cut number of stars per pixel", 

dtype=int, 

default=1000, 

) 

matchNside = pexConfig.Field( 

doc="Healpix Nside for matching", 

dtype=int, 

default=4096, 

) 

coarseNside = pexConfig.Field( 

doc="Healpix coarse Nside for partitioning matches", 

dtype=int, 

default=8, 

) 

filterMap = pexConfig.DictField( 

doc="Mapping from 'filterName' to band.", 

keytype=str, 

itemtype=str, 

default={}, 

) 

requiredBands = pexConfig.ListField( 

doc="Bands required for each star", 

dtype=str, 

default=(), 

) 

primaryBands = pexConfig.ListField( 

doc=("Bands for 'primary' star matches. " 

"A star must be observed in one of these bands to be considered " 

"as a calibration star."), 

dtype=str, 

default=None 

) 

referenceCCD = pexConfig.Field( 

doc="Reference CCD for scanning visits", 

dtype=int, 

default=13, 

) 

checkAllCcds = pexConfig.Field( 

doc="Check all CCDs. Necessary for testing", 

dtype=bool, 

default=False, 

) 

visitDataRefName = pexConfig.Field( 

doc="dataRef name for the 'visit' field", 

dtype=str, 

default="visit" 

) 

ccdDataRefName = pexConfig.Field( 

doc="dataRef name for the 'ccd' field", 

dtype=str, 

default="ccd" 

) 

applyJacobian = pexConfig.Field( 

doc="Apply Jacobian correction?", 

dtype=bool, 

default=False 

) 

jacobianName = pexConfig.Field( 

doc="Name of field with jacobian correction", 

dtype=str, 

default="base_Jacobian_value" 

) 

sourceSelector = sourceSelectorRegistry.makeField( 

doc="How to select sources", 

default="science" 

) 

apertureInnerInstFluxField = pexConfig.Field( 

doc="Field that contains inner aperture for aperture correction proxy", 

dtype=str, 

default='base_CircularApertureFlux_12_0_instFlux' 

) 

apertureOuterInstFluxField = pexConfig.Field( 

doc="Field that contains outer aperture for aperture correction proxy", 

dtype=str, 

default='base_CircularApertureFlux_17_0_instFlux' 

) 

doReferenceMatches = pexConfig.Field( 

doc="Match reference catalog as additional constraint on calibration", 

dtype=bool, 

default=True, 

) 

fgcmLoadReferenceCatalog = pexConfig.ConfigurableField( 

target=FgcmLoadReferenceCatalogTask, 

doc="FGCM reference object loader", 

) 

 

def setDefaults(self): 

sourceSelector = self.sourceSelector["science"] 

sourceSelector.setDefaults() 

 

fluxFlagName = self.instFluxField[0: -len('instFlux')] + 'flag' 

 

sourceSelector.flags.bad = ['base_PixelFlags_flag_edge', 

'base_PixelFlags_flag_interpolatedCenter', 

'base_PixelFlags_flag_saturatedCenter', 

'base_PixelFlags_flag_crCenter', 

'base_PixelFlags_flag_bad', 

'base_PixelFlags_flag_interpolated', 

'base_PixelFlags_flag_saturated', 

'slot_Centroid_flag', 

fluxFlagName] 

 

sourceSelector.doFlags = True 

sourceSelector.doUnresolved = True 

sourceSelector.doSignalToNoise = True 

sourceSelector.doIsolated = True 

 

sourceSelector.signalToNoise.fluxField = self.instFluxField 

sourceSelector.signalToNoise.errField = self.instFluxField + 'Err' 

sourceSelector.signalToNoise.minimum = 10.0 

sourceSelector.signalToNoise.maximum = 1000.0 

 

# FGCM operates on unresolved sources, and this setting is 

# appropriate for the current base_classificationExtendedness 

sourceSelector.unresolved.maximum = 0.5 

 

 

class FgcmBuildStarsRunner(pipeBase.ButlerInitializedTaskRunner): 

"""Subclass of TaskRunner for fgcmBuildStarsTask 

 

fgcmBuildStarsTask.run() takes a number of arguments, one of which is the 

butler (for persistence and mapper data), and a list of dataRefs 

extracted from the command line. Note that FGCM runs on a large set of 

dataRefs, and not on single dataRef/tract/patch. 

This class transforms the process arguments generated by the ArgumentParser 

into the arguments expected by FgcmBuildStarsTask.run(). 

This runner does not use any parallelization. 

 

""" 

 

@staticmethod 

def getTargetList(parsedCmd): 

""" 

Return a list with one element: a tuple with the butler and 

list of dataRefs 

""" 

# we want to combine the butler with any (or no!) dataRefs 

return [(parsedCmd.butler, parsedCmd.id.refList)] 

 

def __call__(self, args): 

""" 

Parameters 

---------- 

args: `tuple` with (butler, dataRefList) 

 

Returns 

------- 

exitStatus: `list` with `pipeBase.Struct` 

exitStatus (0: success; 1: failure) 

""" 

butler, dataRefList = args 

 

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

 

exitStatus = 0 

if self.doRaise: 

task.runDataRef(butler, dataRefList) 

else: 

try: 

task.runDataRef(butler, dataRefList) 

except Exception as e: 

exitStatus = 1 

task.log.fatal("Failed: %s" % e) 

if not isinstance(e, pipeBase.TaskError): 

traceback.print_exc(file=sys.stderr) 

 

task.writeMetadata(butler) 

 

# The task does not return any results: 

return [pipeBase.Struct(exitStatus=exitStatus)] 

 

def run(self, parsedCmd): 

""" 

Run the task, with no multiprocessing 

 

Parameters 

---------- 

parsedCmd: ArgumentParser parsed command line 

""" 

 

resultList = [] 

 

if self.precall(parsedCmd): 

targetList = self.getTargetList(parsedCmd) 

resultList = self(targetList[0]) 

 

return resultList 

 

 

class FgcmBuildStarsTask(pipeBase.CmdLineTask): 

""" 

Build stars for the FGCM global calibration 

""" 

 

ConfigClass = FgcmBuildStarsConfig 

RunnerClass = FgcmBuildStarsRunner 

_DefaultName = "fgcmBuildStars" 

 

def __init__(self, butler=None, **kwargs): 

""" 

Instantiate an FgcmBuildStarsTask. 

 

Parameters 

---------- 

butler : `lsst.daf.persistence.Butler` 

""" 

 

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

self.makeSubtask("sourceSelector") 

# Only log warning and fatal errors from the sourceSelector 

self.sourceSelector.log.setLevel(self.sourceSelector.log.WARN) 

 

@classmethod 

def _makeArgumentParser(cls): 

"""Create an argument parser""" 

 

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

parser.add_id_argument("--id", "calexp", help="Data ID, e.g. --id visit=6789 (optional)") 

 

return parser 

 

# no saving of metadata for now 

def _getMetadataName(self): 

return None 

 

@pipeBase.timeMethod 

def runDataRef(self, butler, dataRefs): 

""" 

Cross-match and make star list for FGCM Input 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

dataRefs: `list` of `lsst.daf.persistence.ButlerDataRef` 

Data references for the input visits. 

If this is an empty list, all visits with src catalogs in 

the repository are used. 

Only one individual dataRef from a visit need be specified 

and the code will find the other source catalogs from 

each visit. 

 

Raises 

------ 

RuntimeErrror: Raised if config.doReferenceMatches is set and 

an fgcmLookUpTable is not available. 

""" 

 

if self.config.doReferenceMatches: 

# Ensure that we have a LUT 

if not butler.datasetExists('fgcmLookUpTable'): 

raise RuntimeError("Must have fgcmLookUpTable if using config.doReferenceMatches") 

 

# Make a subtask for reference loading 

self.makeSubtask("fgcmLoadReferenceCatalog", butler=butler) 

 

# Make the visit catalog if necessary 

if not butler.datasetExists('fgcmVisitCatalog'): 

# we need to build visitCat 

visitCat = self._fgcmMakeVisitCatalog(butler, dataRefs) 

else: 

self.log.info("Found fgcmVisitCatalog.") 

visitCat = butler.get('fgcmVisitCatalog') 

 

# Compile all the stars 

if not butler.datasetExists('fgcmStarObservations'): 

self._fgcmMakeAllStarObservations(butler, visitCat) 

else: 

self.log.info("Found fgcmStarObservations") 

 

if not butler.datasetExists('fgcmStarIds') or not butler.datasetExists('fgcmStarIndices'): 

self._fgcmMatchStars(butler, visitCat) 

else: 

self.log.info("Found fgcmStarIds and fgcmStarIndices") 

 

def _fgcmMakeVisitCatalog(self, butler, dataRefs): 

""" 

Make a visit catalog with all the key data from each visit 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

dataRefs: `list` of `lsst.daf.persistence.ButlerDataRef` 

Data references for the input visits. 

If this is an empty list, all visits with src catalogs in 

the repository are used. 

Only one individual dataRef from a visit need be specified 

and the code will find the other source catalogs from 

each visit. 

 

Returns 

------- 

visitCat: `afw.table.BaseCatalog` 

""" 

 

startTime = time.time() 

 

camera = butler.get('camera') 

nCcd = len(camera) 

 

# TODO: related to DM-13730, this dance of looking for source visits 

# will be unnecessary with Gen3 Butler. This should be part of 

# DM-13730. 

 

if len(dataRefs) == 0: 

srcVisits, srcCcds = self._findSourceVisits(butler, nCcd) 

else: 

# get the visits from the datarefs, only for referenceCCD 

srcVisits = [d.dataId[self.config.visitDataRefName] for d in dataRefs if 

d.dataId[self.config.ccdDataRefName] == self.config.referenceCCD] 

srcCcds = [self.config.referenceCCD] * len(srcVisits) 

 

# Sort the visits for searching/indexing 

srcVisits.sort() 

 

self.log.info("Found %d visits in %.2f s" % 

(len(srcVisits), time.time() - startTime)) 

 

schema = self._makeFgcmVisitSchema(nCcd) 

 

visitCat = afwTable.BaseCatalog(schema) 

visitCat.table.preallocate(len(srcVisits)) 

 

startTime = time.time() 

 

self._fillVisitCatalog(butler, visitCat, srcVisits, srcCcds) 

 

self.log.info("Found all VisitInfo in %.2f s" % (time.time() - startTime)) 

 

return visitCat 

 

def _findSourceVisits(self, butler, nCcd): 

""" 

Find all source catalogs in the repository 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

nCcd: `int` 

Number of CCDs in the camera 

 

Returns 

------- 

(srcVisits, srcCcds): `tuple` of `list`s 

""" 

 

# We did not specify any datarefs, so find all of them 

if not self.config.checkAllCcds: 

# Faster mode, scan through referenceCCD 

allVisits = butler.queryMetadata('src', 

format=[self.config.visitDataRefName, 'filter'], 

dataId={self.config.ccdDataRefName: 

self.config.referenceCCD}) 

srcVisits = [] 

srcCcds = [] 

for dataset in allVisits: 

if (butler.datasetExists('src', dataId={self.config.visitDataRefName: dataset[0], 

self.config.ccdDataRefName: 

self.config.referenceCCD})): 

srcVisits.append(dataset[0]) 

srcCcds.append(self.config.referenceCCD) 

else: 

# Slower mode, check all CCDs 

allVisits = butler.queryMetadata('src', 

format=[self.config.visitDataRefName, 'filter']) 

srcVisits = [] 

srcCcds = [] 

 

for dataset in allVisits: 

if dataset[0] in srcVisits: 

continue 

for ccd in range(nCcd): 

if (butler.datasetExists('src', dataId={self.config.visitDataRefName: dataset[0], 

self.config.ccdDataRefName: 

ccd})): 

srcVisits.append(dataset[0]) 

srcCcds.append(ccd) 

# Once we find that a butler dataset exists, break out 

break 

 

return (srcVisits, srcCcds) 

 

def _fillVisitCatalog(self, butler, visitCat, srcVisits, srcCcds): 

""" 

Fill the visit catalog with visit metadata 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

visitCat: `afw.table.BaseCatalog` 

Catalog with schema from _createFgcmVisitSchema() 

srcVisits: `list' 

List of source visits 

srcCcds: `list` 

List of source CCDs 

""" 

 

bbox = geom.BoxI(geom.PointI(0, 0), geom.PointI(1, 1)) 

 

# now loop over visits and get the information 

for i, srcVisit in enumerate(srcVisits): 

# We don't use the bypasses since we need the psf info which does 

# not have a bypass 

# TODO: When DM-15500 is implemented in the Gen3 Butler, this 

# can be fixed 

 

dataId = {self.config.visitDataRefName: srcVisit, 

self.config.ccdDataRefName: srcCcds[i]} 

 

exp = butler.get('calexp_sub', dataId=dataId, bbox=bbox, 

flags=afwTable.SOURCE_IO_NO_FOOTPRINTS) 

visitInfo = exp.getInfo().getVisitInfo() 

f = exp.getFilter() 

psf = exp.getPsf() 

 

rec = visitCat.addNew() 

rec['visit'] = srcVisit 

rec['filtername'] = f.getName() 

radec = visitInfo.getBoresightRaDec() 

rec['telra'] = radec.getRa().asDegrees() 

rec['teldec'] = radec.getDec().asDegrees() 

rec['telha'] = visitInfo.getBoresightHourAngle().asDegrees() 

rec['mjd'] = visitInfo.getDate().get(system=DateTime.MJD) 

rec['exptime'] = visitInfo.getExposureTime() 

# convert from Pa to millibar 

# Note that I don't know if this unit will need to be per-camera config 

rec['pmb'] = visitInfo.getWeather().getAirPressure() / 100 

# Flag to signify if this is a "deep" field. Not currently used 

rec['deepFlag'] = 0 

# Relative flat scaling (1.0 means no relative scaling) 

rec['scaling'][:] = 1.0 

# Median delta aperture, to be measured from stars 

rec['deltaAper'] = 0.0 

 

rec['psfSigma'] = psf.computeShape().getDeterminantRadius() 

 

if butler.datasetExists('calexpBackground', dataId=dataId): 

# Get background for reference CCD 

# This approximation is good enough for now 

bgStats = (bg[0].getStatsImage().getImage().array 

for bg in butler.get('calexpBackground', 

dataId=dataId)) 

rec['skyBackground'] = sum(np.median(bg[np.isfinite(bg)]) for bg in bgStats) 

else: 

rec['skyBackground'] = -1.0 

 

def _fgcmMakeAllStarObservations(self, butler, visitCat): 

""" 

Compile all good star observations from visits in visitCat 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

visitCat: `afw.table.BaseCatalog` 

Catalog with visit data for FGCM 

""" 

 

startTime = time.time() 

 

# create our source schema 

sourceSchema = butler.get('src_schema', immediate=True).schema 

 

sourceMapper = self._makeSourceMapper(sourceSchema) 

 

# We also have a temporary catalog that will accumulate aperture measurements 

aperMapper = self._makeAperMapper(sourceSchema) 

 

# we need to know the ccds... 

camera = butler.get('camera') 

 

outputSchema = sourceMapper.getOutputSchema() 

fullCatalog = afwTable.BaseCatalog(outputSchema) 

 

# FGCM will provide relative calibration for the flux in config.instFluxField 

 

instFluxKey = sourceSchema[self.config.instFluxField].asKey() 

instFluxErrKey = sourceSchema[self.config.instFluxField + 'Err'].asKey() 

visitKey = outputSchema['visit'].asKey() 

ccdKey = outputSchema['ccd'].asKey() 

instMagKey = outputSchema['instMag'].asKey() 

instMagErrKey = outputSchema['instMagErr'].asKey() 

 

aperOutputSchema = aperMapper.getOutputSchema() 

 

instFluxAperInKey = sourceSchema[self.config.apertureInnerInstFluxField].asKey() 

instFluxErrAperInKey = sourceSchema[self.config.apertureInnerInstFluxField + 'Err'].asKey() 

instFluxAperOutKey = sourceSchema[self.config.apertureOuterInstFluxField].asKey() 

instFluxErrAperOutKey = sourceSchema[self.config.apertureOuterInstFluxField + 'Err'].asKey() 

instMagInKey = aperOutputSchema['instMag_aper_inner'].asKey() 

instMagErrInKey = aperOutputSchema['instMagErr_aper_inner'].asKey() 

instMagOutKey = aperOutputSchema['instMag_aper_outer'].asKey() 

instMagErrOutKey = aperOutputSchema['instMagErr_aper_outer'].asKey() 

 

# loop over visits 

for visit in visitCat: 

expTime = visit['exptime'] 

 

nStarInVisit = 0 

 

# Reset the aperture catalog (per visit) 

aperVisitCatalog = afwTable.BaseCatalog(aperOutputSchema) 

 

# loop over CCDs 

for ccdIndex, detector in enumerate(camera): 

 

ccdId = detector.getId() 

 

try: 

sources = butler.get('src', dataId={self.config.visitDataRefName: 

visit['visit'], 

self.config.ccdDataRefName: ccdId}, 

flags=afwTable.SOURCE_IO_NO_FOOTPRINTS) 

except butlerExceptions.NoResults: 

# this is not a problem if this ccd isn't there 

continue 

 

goodSrc = self.sourceSelector.selectSources(sources) 

 

tempCat = afwTable.BaseCatalog(fullCatalog.schema) 

tempCat.reserve(goodSrc.selected.sum()) 

tempCat.extend(sources[goodSrc.selected], mapper=sourceMapper) 

tempCat[visitKey][:] = visit['visit'] 

tempCat[ccdKey][:] = ccdId 

# Compute "magnitude" by scaling flux with exposure time. 

# Add an arbitrary zeropoint that needs to be investigated. 

scaledInstFlux = sources[instFluxKey][goodSrc.selected] * visit['scaling'][ccdIndex] 

tempCat[instMagKey][:] = (-2.5 * np.log10(scaledInstFlux) + 2.5 * np.log10(expTime)) 

# instMagErr is computed with original (unscaled) flux 

k = 2.5 / np.log(10.) 

tempCat[instMagErrKey][:] = k * (sources[instFluxErrKey][goodSrc.selected] / 

sources[instFluxKey][goodSrc.selected]) 

 

if self.config.applyJacobian: 

tempCat[instMagKey][:] -= 2.5 * np.log10(tempCat['jacobian'][:]) 

 

fullCatalog.extend(tempCat) 

 

# And the aperture information 

tempAperCat = afwTable.BaseCatalog(aperVisitCatalog.schema) 

tempAperCat.reserve(goodSrc.selected.sum()) 

tempAperCat.extend(sources[goodSrc.selected], mapper=aperMapper) 

 

with np.warnings.catch_warnings(): 

# Ignore warnings, we will filter infinities and 

# nans below. 

np.warnings.simplefilter("ignore") 

 

tempAperCat[instMagInKey][:] = -2.5 * np.log10( 

sources[instFluxAperInKey][goodSrc.selected]) 

tempAperCat[instMagErrInKey][:] = (2.5 / np.log(10.)) * ( 

sources[instFluxErrAperInKey][goodSrc.selected] / 

sources[instFluxAperInKey][goodSrc.selected]) 

tempAperCat[instMagOutKey][:] = -2.5 * np.log10( 

sources[instFluxAperOutKey][goodSrc.selected]) 

tempAperCat[instMagErrOutKey][:] = (2.5 / np.log(10.)) * ( 

sources[instFluxErrAperOutKey][goodSrc.selected] / 

sources[instFluxAperOutKey][goodSrc.selected]) 

 

aperVisitCatalog.extend(tempAperCat) 

 

nStarInVisit += len(tempCat) 

 

# Compute the median delta-aper 

if not aperVisitCatalog.isContiguous(): 

aperVisitCatalog = aperVisitCatalog.copy(deep=True) 

 

instMagIn = aperVisitCatalog[instMagInKey] 

instMagErrIn = aperVisitCatalog[instMagErrInKey] 

instMagOut = aperVisitCatalog[instMagOutKey] 

instMagErrOut = aperVisitCatalog[instMagErrOutKey] 

 

ok = (np.isfinite(instMagIn) & np.isfinite(instMagErrIn) & 

np.isfinite(instMagOut) & np.isfinite(instMagErrOut)) 

 

visit['deltaAper'] = np.median(instMagIn[ok] - instMagOut[ok]) 

 

self.log.info(" Found %d good stars in visit %d (deltaAper = %.3f)" % 

(nStarInVisit, visit['visit'], visit['deltaAper'])) 

 

self.log.info("Found all good star observations in %.2f s" % 

(time.time() - startTime)) 

 

# Write all the observations 

butler.put(fullCatalog, 'fgcmStarObservations') 

 

# And overwrite the visitCatalog with delta_aper info 

butler.put(visitCat, 'fgcmVisitCatalog') 

 

self.log.info("Done with all stars in %.2f s" % 

(time.time() - startTime)) 

 

def _fgcmMatchStars(self, butler, visitCat): 

""" 

Use FGCM code to match observations into unique stars. 

 

Parameters 

---------- 

butler: `lsst.daf.persistence.Butler` 

visitCat: `afw.table.BaseCatalog` 

Catalog with visit data for FGCM 

 

Outputs 

------- 

Butler will output fgcmStarIds (matched star identifiers) and 

fgcmStarIndices (index values linking fgcmStarObservations to 

fgcmStarIds). 

""" 

 

obsCat = butler.get('fgcmStarObservations') 

 

# get filter names into a numpy array... 

# This is the type that is expected by the fgcm code 

visitFilterNames = np.zeros(len(visitCat), dtype='a2') 

for i in range(len(visitCat)): 

visitFilterNames[i] = visitCat[i]['filtername'] 

 

# match to put filterNames with observations 

visitIndex = np.searchsorted(visitCat['visit'], 

obsCat['visit']) 

 

obsFilterNames = visitFilterNames[visitIndex] 

 

if self.config.doReferenceMatches: 

# Get the reference filter names, using the LUT 

lutCat = butler.get('fgcmLookUpTable') 

 

stdFilterDict = {filterName: stdFilter for (filterName, stdFilter) in 

zip(lutCat[0]['filterNames'].split(','), 

lutCat[0]['stdFilterNames'].split(','))} 

stdLambdaDict = {stdFilter: stdLambda for (stdFilter, stdLambda) in 

zip(lutCat[0]['stdFilterNames'].split(','), 

lutCat[0]['lambdaStdFilter'])} 

 

del lutCat 

 

referenceFilterNames = self._getReferenceFilterNames(visitCat, 

stdFilterDict, 

stdLambdaDict) 

self.log.info("Using the following reference filters: %s" % 

(', '.join(referenceFilterNames))) 

 

else: 

# This should be an empty list 

referenceFilterNames = [] 

 

# make the fgcm starConfig dict 

 

starConfig = {'logger': self.log, 

'filterToBand': self.config.filterMap, 

'requiredBands': self.config.requiredBands, 

'minPerBand': self.config.minPerBand, 

'matchRadius': self.config.matchRadius, 

'isolationRadius': self.config.isolationRadius, 

'matchNSide': self.config.matchNside, 

'coarseNSide': self.config.coarseNside, 

'densNSide': self.config.densityCutNside, 

'densMaxPerPixel': self.config.densityCutMaxPerPixel, 

'primaryBands': self.config.primaryBands, 

'referenceFilterNames': referenceFilterNames} 

 

# initialize the FgcmMakeStars object 

fgcmMakeStars = fgcm.FgcmMakeStars(starConfig) 

 

# make the primary stars 

# note that the ra/dec native Angle format is radians 

# We determine the conversion from the native units (typically 

# radians) to degrees for the first observation. This allows us 

# to treate ra/dec as numpy arrays rather than Angles, which would 

# be approximately 600x slower. 

conv = obsCat[0]['ra'].asDegrees() / float(obsCat[0]['ra']) 

fgcmMakeStars.makePrimaryStars(obsCat['ra'] * conv, 

obsCat['dec'] * conv, 

filterNameArray=obsFilterNames, 

bandSelected=False) 

 

# and match all the stars 

fgcmMakeStars.makeMatchedStars(obsCat['ra'] * conv, 

obsCat['dec'] * conv, 

obsFilterNames) 

 

if self.config.doReferenceMatches: 

fgcmMakeStars.makeReferenceMatches(self.fgcmLoadReferenceCatalog) 

 

# now persist 

 

objSchema = self._makeFgcmObjSchema() 

 

# make catalog and records 

fgcmStarIdCat = afwTable.BaseCatalog(objSchema) 

fgcmStarIdCat.reserve(fgcmMakeStars.objIndexCat.size) 

for i in range(fgcmMakeStars.objIndexCat.size): 

fgcmStarIdCat.addNew() 

 

# fill the catalog 

fgcmStarIdCat['fgcm_id'][:] = fgcmMakeStars.objIndexCat['fgcm_id'] 

fgcmStarIdCat['ra'][:] = fgcmMakeStars.objIndexCat['ra'] 

fgcmStarIdCat['dec'][:] = fgcmMakeStars.objIndexCat['dec'] 

fgcmStarIdCat['obsArrIndex'][:] = fgcmMakeStars.objIndexCat['obsarrindex'] 

fgcmStarIdCat['nObs'][:] = fgcmMakeStars.objIndexCat['nobs'] 

 

butler.put(fgcmStarIdCat, 'fgcmStarIds') 

 

obsSchema = self._makeFgcmObsSchema() 

 

fgcmStarIndicesCat = afwTable.BaseCatalog(obsSchema) 

fgcmStarIndicesCat.reserve(fgcmMakeStars.obsIndexCat.size) 

for i in range(fgcmMakeStars.obsIndexCat.size): 

fgcmStarIndicesCat.addNew() 

 

fgcmStarIndicesCat['obsIndex'][:] = fgcmMakeStars.obsIndexCat['obsindex'] 

 

butler.put(fgcmStarIndicesCat, 'fgcmStarIndices') 

 

if self.config.doReferenceMatches: 

refSchema = self._makeFgcmRefSchema(len(referenceFilterNames)) 

 

fgcmRefCat = afwTable.BaseCatalog(refSchema) 

fgcmRefCat.reserve(fgcmMakeStars.referenceCat.size) 

 

for i in range(fgcmMakeStars.referenceCat.size): 

fgcmRefCat.addNew() 

 

fgcmRefCat['fgcm_id'][:] = fgcmMakeStars.referenceCat['fgcm_id'] 

fgcmRefCat['refMag'][:, :] = fgcmMakeStars.referenceCat['refMag'] 

fgcmRefCat['refMagErr'][:, :] = fgcmMakeStars.referenceCat['refMagErr'] 

 

butler.put(fgcmRefCat, 'fgcmReferenceStars') 

 

def _makeFgcmVisitSchema(self, nCcd): 

""" 

Make a schema for an fgcmVisitCatalog 

 

Parameters 

---------- 

nCcd: `int` 

Number of CCDs in the camera 

 

Returns 

------- 

schema: `afwTable.Schema` 

""" 

 

schema = afwTable.Schema() 

schema.addField('visit', type=np.int32, doc="Visit number") 

# Note that the FGCM code currently handles filternames up to 2 characters long 

schema.addField('filtername', type=str, size=2, doc="Filter name") 

schema.addField('telra', type=np.float64, doc="Pointing RA (deg)") 

schema.addField('teldec', type=np.float64, doc="Pointing Dec (deg)") 

schema.addField('telha', type=np.float64, doc="Pointing Hour Angle (deg)") 

schema.addField('mjd', type=np.float64, doc="MJD of visit") 

schema.addField('exptime', type=np.float32, doc="Exposure time") 

schema.addField('pmb', type=np.float32, doc="Pressure (millibar)") 

schema.addField('psfSigma', type=np.float32, doc="PSF sigma (reference CCD)") 

schema.addField('deltaAper', type=np.float32, doc="Delta-aperture") 

schema.addField('skyBackground', type=np.float32, doc="Sky background (ADU) (reference CCD)") 

# the following field is not used yet 

schema.addField('deepFlag', type=np.int32, doc="Deep observation") 

schema.addField('scaling', type='ArrayD', doc="Scaling applied due to flat adjustment", 

size=nCcd) 

 

return schema 

 

def _makeSourceMapper(self, sourceSchema): 

""" 

Make a schema mapper for fgcm sources 

 

Parameters 

---------- 

sourceSchema: `afwTable.Schema` 

Default source schema from the butler 

 

Returns 

------- 

sourceMapper: `afwTable.schemaMapper` 

Mapper to the FGCM source schema 

""" 

 

# create a mapper to the preferred output 

sourceMapper = afwTable.SchemaMapper(sourceSchema) 

 

# map to ra/dec 

sourceMapper.addMapping(sourceSchema['coord_ra'].asKey(), 'ra') 

sourceMapper.addMapping(sourceSchema['coord_dec'].asKey(), 'dec') 

sourceMapper.addMapping(sourceSchema[self.config.jacobianName].asKey(), 

'jacobian') 

sourceMapper.addMapping(sourceSchema['slot_Centroid_x'].asKey(), 'x') 

sourceMapper.addMapping(sourceSchema['slot_Centroid_y'].asKey(), 'y') 

 

# and add the fields we want 

sourceMapper.editOutputSchema().addField( 

"visit", type=np.int32, doc="Visit number") 

sourceMapper.editOutputSchema().addField( 

"ccd", type=np.int32, doc="CCD number") 

sourceMapper.editOutputSchema().addField( 

"instMag", type=np.float32, doc="Instrumental magnitude") 

sourceMapper.editOutputSchema().addField( 

"instMagErr", type=np.float32, doc="Instrumental magnitude error") 

 

return sourceMapper 

 

def _makeAperMapper(self, sourceSchema): 

""" 

Make a schema mapper for fgcm aperture measurements 

 

Parameters 

---------- 

sourceSchema: `afwTable.Schema` 

Default source schema from the butler 

 

Returns 

------- 

aperMapper: `afwTable.schemaMapper` 

Mapper to the FGCM aperture schema 

""" 

 

aperMapper = afwTable.SchemaMapper(sourceSchema) 

aperMapper.addMapping(sourceSchema['coord_ra'].asKey(), 'ra') 

aperMapper.addMapping(sourceSchema['coord_dec'].asKey(), 'dec') 

aperMapper.editOutputSchema().addField('instMag_aper_inner', type=np.float64, 

doc="Magnitude at inner aperture") 

aperMapper.editOutputSchema().addField('instMagErr_aper_inner', type=np.float64, 

doc="Magnitude error at inner aperture") 

aperMapper.editOutputSchema().addField('instMag_aper_outer', type=np.float64, 

doc="Magnitude at outer aperture") 

aperMapper.editOutputSchema().addField('instMagErr_aper_outer', type=np.float64, 

doc="Magnitude error at outer aperture") 

 

return aperMapper 

 

def _makeFgcmObjSchema(self): 

""" 

Make a schema for the objIndexCat from fgcmMakeStars 

 

Returns 

------- 

schema: `afwTable.Schema` 

""" 

 

objSchema = afwTable.Schema() 

objSchema.addField('fgcm_id', type=np.int32, doc='FGCM Unique ID') 

# Will investigate making these angles... 

objSchema.addField('ra', type=np.float64, doc='Mean object RA (deg)') 

objSchema.addField('dec', type=np.float64, doc='Mean object Dec (deg)') 

objSchema.addField('obsArrIndex', type=np.int32, 

doc='Index in obsIndexTable for first observation') 

objSchema.addField('nObs', type=np.int32, doc='Total number of observations') 

 

return objSchema 

 

def _makeFgcmObsSchema(self): 

""" 

Make a schema for the obsIndexCat from fgcmMakeStars 

 

Returns 

------- 

schema: `afwTable.Schema` 

""" 

 

obsSchema = afwTable.Schema() 

obsSchema.addField('obsIndex', type=np.int32, doc='Index in observation table') 

 

return obsSchema 

 

def _makeFgcmRefSchema(self, nReferenceBands): 

""" 

Make a schema for the referenceCat from fgcmMakeStars 

 

Parameters 

---------- 

nReferenceBands: `int` 

Number of reference bands 

 

Returns 

------- 

schema: `afwTable.Schema` 

""" 

 

refSchema = afwTable.Schema() 

refSchema.addField('fgcm_id', type=np.int32, doc='FGCM Unique ID') 

refSchema.addField('refMag', type='ArrayF', doc='Reference magnitude array (AB)', 

size=nReferenceBands) 

refSchema.addField('refMagErr', type='ArrayF', doc='Reference magnitude error array', 

size=nReferenceBands) 

 

return refSchema 

 

def _getReferenceFilterNames(self, visitCat, stdFilterDict, stdLambdaDict): 

""" 

Get the reference filter names, in wavelength order, from the visitCat and 

information from the look-up-table. 

 

Parameters 

---------- 

visitCat: `afw.table.BaseCatalog` 

Catalog with visit data for FGCM 

stdFilterDict: `dict` 

Mapping of filterName to stdFilterName from LUT 

stdLambdaDict: `dict` 

Mapping of stdFilterName to stdLambda from LUT 

 

Returns 

------- 

referenceFilterNames: `list` 

Wavelength-ordered list of reference filter names 

""" 

 

# Find the unique list of filter names in visitCat 

filterNames = np.unique(visitCat.asAstropy()['filtername']) 

 

# Find the unique list of "standard" filters 

stdFilterNames = {stdFilterDict[filterName] for filterName in filterNames} 

 

# And sort these by wavelength 

referenceFilterNames = sorted(stdFilterNames, key=stdLambdaDict.get) 

 

return referenceFilterNames