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

""" 

photUtils - 

 

 

ljones@astro.washington.edu (and ajc@astro.washington.edu) 

 

and now (2014 March 28): scott.f.daniel@gmail.com 

 

Collection of utilities to aid usage of Sed and Bandpass with dictionaries. 

 

""" 

 

from builtins import zip 

from builtins import range 

from builtins import object 

import os 

import numpy 

from collections import OrderedDict 

from lsst.utils import getPackageDir 

from lsst.sims.photUtils import Sed, Bandpass, LSSTdefaults, calcGamma, \ 

calcMagError_m5, calcSNR_m5, PhotometricParameters, magErrorFromSNR, \ 

BandpassDict 

from lsst.sims.utils import defaultSpecMap 

from lsst.sims.catalogs.decorators import compound 

from lsst.sims.photUtils import SedList 

from lsst.sims.utils import defaultSpecMap 

from lsst.utils import getPackageDir 

 

__all__ = ["PhotometryBase", "PhotometryGalaxies", "PhotometryStars", "PhotometrySSM"] 

 

 

class PhotometryBase(object): 

""" 

This class provides an InstanceCatalog with a member variable photParams, 

which is an instance of the class PhotometricParameters. 

 

It also provides the method calculateMagnitudeUncertainty, which takes magnitudes, 

a BandpassDict, and an ObservationMetaData as inputs and returns the uncertainties 

on those magnitudes. 

""" 

 

#an object carrying around photometric parameters like readnoise, effective area, plate scale, etc. 

#defaults to LSST values 

photParams = PhotometricParameters() 

 

 

def _cacheGamma(self, m5_names, bandpassDict): 

""" 

Generate or populate the cache of gamma values used by this InstanceCatalog 

to calculate photometric uncertainties (gamma is defined in equation 5 of 

the LSST overview paper arXiv:0805.2366) 

 

@param [in] m5_names is a list of the names of keys by which m5 values are 

referred to in the dict self.obs_metadata.m5 

 

@param [in] bandpassDict is the bandpassDict containing the bandpasses 

corresponding to those m5 values. 

""" 

 

if not hasattr(self, '_gamma_cache'): 

self._gamma_cache = {} 

 

for mm, bp in zip(m5_names, bandpassDict.values()): 

if mm not in self._gamma_cache and mm in self.obs_metadata.m5: 

self._gamma_cache[mm] = calcGamma(bp, self.obs_metadata.m5[mm], photParams=self.photParams) 

 

 

def _magnitudeUncertaintyGetter(self, column_name_list, m5_name_list, bandpassDict_name): 

""" 

Generic getter for magnitude uncertainty columns. 

 

Columns must be named 'sigma_xx' where 'xx' is the column name of 

the associated magnitude 

 

@param [in] column_name_list is the list of magnitude column names 

associated with the uncertainties calculated by this getter 

(the 'xx' in the 'sigma_xx' above) 

 

@param [in] m5_name_list are the keys to the self.obs_metadata.m5 dict 

corresponding to the bandpasses in column_names. For example: in 

the case of galaxies, the magnitude columns 

 

column_names = ['uBulge', 'gBulge', 'rBulge', 'iBulge', 'zBulge', 'yBulge'] 

 

may correspond to m5 values keyed to 

 

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

 

@param [in] bandpassDict_name is a string indicating the name of 

the InstanceCatalog member variable containing the BandpassDict 

to be used when calculating these magnitude uncertainties. 

The BandpassDict itself will be accessed using 

getattr(self, bandpassDict_name) 

 

@param [out] returns a 2-D numpy array in which the first index 

is the uncertainty column and the second index is the catalog row 

(i.e. output suitable for a getter in an InstanceCatalog) 

""" 

 

# make sure that the magnitudes associated with any requested 

# uncertainties actually are calculated 

num_elements = None 

mag_dict = {} 

for name in column_name_list: 

if 'sigma_%s' % name in self._actually_calculated_columns: 

mag_dict[name] = self.column_by_name(name) 

if num_elements is None: 

num_elements = len(mag_dict[name]) 

 

# These lines must come after the preceding lines; 

# the bandpassDict will not be loaded until the magnitude 

# getters are called 

bandpassDict = getattr(self, bandpassDict_name) 

self._cacheGamma(m5_name_list, bandpassDict) 

 

 

output = [] 

 

for name, m5_name, bp in zip(column_name_list, m5_name_list, bandpassDict.values()): 

if 'sigma_%s' % name not in self._actually_calculated_columns: 

output.append(numpy.ones(num_elements)*numpy.NaN) 

else: 

try: 

m5 = self.obs_metadata.m5[m5_name] 

gamma = self._gamma_cache[m5_name] 

 

sigma_list, gamma = calcMagError_m5(mag_dict[name], bp, m5, self.photParams, gamma=gamma) 

 

output.append(sigma_list) 

 

except KeyError as kk: 

msg = 'You got the KeyError: %s' % kk.args[0] 

raise KeyError('%s \n' % msg \ 

+ 'Is it possible your ObservationMetaData does not have the proper\n' 

'm5 values defined?') 

 

 

return numpy.array(output) 

 

 

@compound('sigma_lsst_u','sigma_lsst_g','sigma_lsst_r','sigma_lsst_i', 

'sigma_lsst_z','sigma_lsst_y') 

def get_lsst_photometric_uncertainties(self): 

""" 

Getter for photometric uncertainties associated with lsst bandpasses 

""" 

 

return self._magnitudeUncertaintyGetter(['lsst_u', 'lsst_g', 'lsst_r', 

'lsst_i', 'lsst_z', 'lsst_y'], 

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

'lsstBandpassDict') 

 

 

def calculateVisibility(self, magFilter, sigma=0.1, randomSeed=None, pre_generate_randoms=False): 

""" 

Determine (probabilistically) whether a source was detected or not. 

 

The 'completeness' of source detection at any magnitude is calculated by 

completeness = (1 + e^^(magFilter-m5)/sigma)^(-1) 

For each source with a magnitude magFilter, if a uniform random number [0-1) 

is less than or equal to 'completeness', then it is counted as "detected". 

See equation 24, figure 8 and table 5 from SDSS completeness analysis in 

http://iopscience.iop.org/0004-637X/794/2/120/pdf/apj_794_2_120.pdf 

"THE SLOAN DIGITAL SKY SURVEY COADD: 275 deg2 OF DEEP SLOAN DIGITAL SKY SURVEY IMAGING ON STRIPE 82" 

 

@ param [in] magFilter is the magnitude of the object in the observed filter. 

 

@ param [in] sigma is the FWHM of the distribution (default = 0.1) 

 

@ param [in] randomSeed is an option to set a random seed (default None) 

@ param [in] pre_generate_randoms is an option (default False) to pre-generate a series of 12,000,000 random numbers 

for use throughout the visibility calculation [the random numbers used are randoms[objId]]. 

 

@ param [out] visibility (None/1). 

""" 

if len(magFilter) == 0: 

return numpy.array([]) 

# Calculate the completeness at the magnitude of each object. 

completeness = 1.0 / (1 + numpy.exp((magFilter - self.obs_metadata.m5[self.obs_metadata.bandpass])/sigma)) 

# Seed numpy if desired and not previously done. 

if (randomSeed is not None) and (not hasattr(self, 'ssm_random_seeded')): 

numpy.random.seed(randomSeed) 

self.ssm_random_seeded = True 

# Pre-generate random numbers, if desired and not previously done. 

if pre_generate_randoms and not hasattr(self, 'ssm_randoms'): 

self.ssm_randoms = numpy.random.rand(14000000) 

# Calculate probability values to compare to completeness. 

if hasattr(self, 'ssm_randoms'): 

# Grab the random numbers from self.randoms. 

probability = self.ssm_randoms[self.column_by_name('objId')] 

else: 

probability = numpy.random.random_sample(len(magFilter)) 

# Compare the random number to the completeness. 

visibility = numpy.where(probability <= completeness, 1, None) 

return visibility 

 

def _variabilityGetter(self, columnNames): 

""" 

Find columns named 'delta_*' and return them to be added 

to '*' magnitude columns (i.e. look for delta_lsst_u so that 

it can be added to lsst_u) 

 

Parameters 

---------- 

columnNames is a list of the quiescent columns (lsst_u in the 

example above) whose deltas we are looking for 

 

Returns 

------- 

A numpy array in which each row is a delta magnitude and each 

column is an astrophysical object/database row 

""" 

 

num_obj = len(self.column_by_name(self.db_obj.idColKey)) 

delta = [] 

 

# figure out which of these columns we are actually calculating 

indices = [ii for ii, name in enumerate(columnNames) 

if name in self._actually_calculated_columns] 

 

for ix, columnName in enumerate(columnNames): 

if indices is None or ix in indices: 

delta_name = 'delta_' + columnName 

if delta_name in self._all_available_columns: 

delta.append(self.column_by_name(delta_name)) 

else: 

delta.append(numpy.zeros(num_obj)) 

else: 

delta.append(numpy.zeros(num_obj)) 

 

return numpy.array(delta) 

 

class PhotometryGalaxies(PhotometryBase): 

""" 

This mixin provides the code necessary for calculating the component magnitudes associated with 

galaxies. It assumes that we want LSST filters. 

""" 

 

def _hasCosmoDistMod(self): 

""" 

Determine whether or not this InstanceCatalog has a column 

specifically devoted to the cosmological distance modulus. 

""" 

if 'cosmologicalDistanceModulus' in self._all_available_columns: 

return True 

return False 

 

 

def _loadBulgeSedList(self, wavelen_match): 

""" 

Load a SedList of galaxy bulge Seds. 

The list will be stored in the variable self._bulgeSedList. 

 

@param [in] wavelen_match is the wavelength grid (in nm) 

on which the Seds are to be sampled. 

""" 

 

sedNameList = self.column_by_name('sedFilenameBulge') 

magNormList = self.column_by_name('magNormBulge') 

redshiftList = self.column_by_name('redshift') 

internalAvList = self.column_by_name('internalAvBulge') 

cosmologicalDimming = not self._hasCosmoDistMod() 

 

if len(sedNameList)==0: 

return numpy.ones((0)) 

 

267 ↛ 276line 267 didn't jump to line 276, because the condition on line 267 was never false if not hasattr(self, '_bulgeSedList'): 

self._bulgeSedList = SedList(sedNameList, magNormList, 

internalAvList=internalAvList, 

redshiftList=redshiftList, 

cosmologicalDimming=cosmologicalDimming, 

wavelenMatch=wavelen_match, 

fileDir=getPackageDir('sims_sed_library'), 

specMap=defaultSpecMap) 

else: 

self._bulgeSedList.flush() 

self._bulgeSedList.loadSedsFromList(sedNameList, magNormList, 

internalAvList=internalAvList, 

redshiftList=redshiftList) 

 

 

def _loadDiskSedList(self, wavelen_match): 

""" 

Load a SedList of galaxy disk Seds. 

The list will be stored in the variable self._bulgeSedList. 

 

@param [in] wavelen_match is the wavelength grid (in nm) 

on which the Seds are to be sampled. 

""" 

 

sedNameList = self.column_by_name('sedFilenameDisk') 

magNormList = self.column_by_name('magNormDisk') 

redshiftList = self.column_by_name('redshift') 

internalAvList = self.column_by_name('internalAvDisk') 

cosmologicalDimming = not self._hasCosmoDistMod() 

 

if len(sedNameList)==0: 

return numpy.ones((0)) 

 

300 ↛ 309line 300 didn't jump to line 309, because the condition on line 300 was never false if not hasattr(self, '_diskSedList'): 

self._diskSedList = SedList(sedNameList, magNormList, 

internalAvList=internalAvList, 

redshiftList=redshiftList, 

cosmologicalDimming=cosmologicalDimming, 

wavelenMatch=wavelen_match, 

fileDir=getPackageDir('sims_sed_library'), 

specMap=defaultSpecMap) 

else: 

self._diskSedList.flush() 

self._diskSedList.loadSedsFromList(sedNameList, magNormList, 

internalAvList=internalAvList, 

redshiftList=redshiftList) 

 

 

def _loadAgnSedList(self, wavelen_match): 

""" 

Load a SedList of galaxy AGN Seds. 

The list will be stored in the variable self._bulgeSedList. 

 

@param [in] wavelen_match is the wavelength grid (in nm) 

on which the Seds are to be sampled. 

""" 

 

sedNameList = self.column_by_name('sedFilenameAgn') 

magNormList = self.column_by_name('magNormAgn') 

redshiftList = self.column_by_name('redshift') 

cosmologicalDimming = not self._hasCosmoDistMod() 

 

if len(sedNameList)==0: 

return numpy.ones((0)) 

 

if not hasattr(self, '_agnSedList'): 

self._agnSedList = SedList(sedNameList, magNormList, 

redshiftList=redshiftList, 

cosmologicalDimming=cosmologicalDimming, 

wavelenMatch=wavelen_match, 

fileDir=getPackageDir('sims_sed_library'), 

specMap=defaultSpecMap) 

else: 

self._agnSedList.flush() 

self._agnSedList.loadSedsFromList(sedNameList, magNormList, 

redshiftList=redshiftList) 

 

 

def sum_magnitudes(self, disk = None, bulge = None, agn = None): 

""" 

Sum the component magnitudes of a galaxy and return the answer 

 

@param [in] disk is the disk magnitude must be a numpy array or a float 

 

@param [in] bulge is the bulge magnitude must be a numpy array or a float 

 

@param [in] agn is the agn magnitude must be a numpy array or a float 

 

@param [out] outMag is the total magnitude of the galaxy 

""" 

 

baselineType = type(None) 

359 ↛ 364line 359 didn't jump to line 364, because the condition on line 359 was never false if not isinstance(disk, type(None)): 

baselineType = type(disk) 

if baselineType == numpy.ndarray: 

elements=len(disk) 

 

364 ↛ 373line 364 didn't jump to line 373, because the condition on line 364 was never false if not isinstance(bulge, type(None)): 

365 ↛ 366line 365 didn't jump to line 366, because the condition on line 365 was never true if baselineType == type(None): 

baselineType = type(bulge) 

if baselineType == numpy.ndarray: 

elements = len(bulge) 

369 ↛ 370line 369 didn't jump to line 370, because the condition on line 369 was never true elif not isinstance(bulge, baselineType): 

raise RuntimeError("All non-None arguments of sum_magnitudes need to be " + 

"of the same type (float or numpy array)") 

 

elif not isinstance(agn, type(None)): 

if baseLineType == type(None): 

baselineType = type(agn) 

if baselineType == numpy.ndarray: 

elements = len(agn) 

elif not isinstance(agn, baselineType): 

raise RuntimeError("All non-None arguments of sum_magnitudes need to be " + 

"of the same type (float or numpy array)") 

 

382 ↛ 387line 382 didn't jump to line 387, because the condition on line 382 was never true if baselineType is not float and \ 

baselineType is not numpy.ndarray and \ 

baselineType is not numpy.float and \ 

baselineType is not numpy.float64: 

 

raise RuntimeError("Arguments of sum_magnitudes need to be " + 

"either floats or numpy arrays; you appear to have passed %s " % baselineType) 

 

mm_0 = 22. 

tol = 1.0e-30 

 

if baselineType == numpy.ndarray: 

nn = numpy.zeros(elements) 

else: 

nn = 0.0 

 

398 ↛ 401line 398 didn't jump to line 401, because the condition on line 398 was never false if disk is not None: 

nn += numpy.where(numpy.isnan(disk), 0.0, numpy.power(10, -0.4*(disk - mm_0))) 

 

401 ↛ 404line 401 didn't jump to line 404, because the condition on line 401 was never false if bulge is not None: 

nn += numpy.where(numpy.isnan(bulge), 0.0, numpy.power(10, -0.4*(bulge - mm_0))) 

 

404 ↛ 407line 404 didn't jump to line 407, because the condition on line 404 was never false if agn is not None: 

nn += numpy.where(numpy.isnan(agn), 0.0, numpy.power(10, -0.4*(agn - mm_0))) 

 

if baselineType == numpy.ndarray: 

# according to this link 

# http://stackoverflow.com/questions/25087769/runtimewarning-divide-by-zero-error-how-to-avoid-python-numpy 

# we will still get a divide by zero error from log10, but numpy.where will be 

# circumventing the offending value, so it is probably okay 

return numpy.where(nn>tol, -2.5*numpy.log10(nn) + mm_0, numpy.NaN) 

else: 

if nn>tol: 

return -2.5*numpy.log10(nn) + mm_0 

else: 

return numpy.NaN 

 

 

def _quiescentMagnitudeGetter(self, componentName, bandpassDict, columnNameList): 

""" 

A generic getter for quiescent magnitudes of galaxy components. 

 

@param [in] componentName is either 'bulge', 'disk', or 'agn' 

 

@param [in] bandpassDict is a BandpassDict of the bandpasses 

in which to calculate the magnitudes 

 

@param [in] columnNameList is a list of the columns corresponding to 

these magnitudes (for purposes of applying variability). 

 

@param [out] magnitudes is a 2-D numpy array of magnitudes in which 

rows correspond to bandpasses and columns correspond to astronomical 

objects. 

""" 

 

# figure out which of these columns we are actually calculating 

indices = [ii for ii, name in enumerate(columnNameList) 

if name in self._actually_calculated_columns] 

 

if len(indices) == len(columnNameList): 

indices = None 

 

if componentName == 'bulge': 

self._loadBulgeSedList(bandpassDict.wavelenMatch) 

if not hasattr(self, '_bulgeSedList'): 

sedList = None 

else: 

sedList = self._bulgeSedList 

elif componentName == 'disk': 

self._loadDiskSedList(bandpassDict.wavelenMatch) 

if not hasattr(self, '_diskSedList'): 

sedList = None 

else: 

sedList = self._diskSedList 

456 ↛ 463line 456 didn't jump to line 463, because the condition on line 456 was never false elif componentName == 'agn': 

self._loadAgnSedList(bandpassDict.wavelenMatch) 

if not hasattr(self, '_agnSedList'): 

sedList = None 

else: 

sedList = self._agnSedList 

else: 

raise RuntimeError('_quiescentMagnitudeGetter does not understand component %s ' \ 

% componentName) 

 

if sedList is None: 

magnitudes = numpy.ones((len(columnNameList), 0)) 

else: 

magnitudes = bandpassDict.magListForSedList(sedList, indices=indices).transpose() 

 

if self._hasCosmoDistMod(): 

cosmoDistMod = self.column_by_name('cosmologicalDistanceModulus') 

if len(cosmoDistMod)>0: 

for ix in range(magnitudes.shape[0]): 

magnitudes[ix] += cosmoDistMod 

 

return magnitudes 

 

 

@compound('sigma_uBulge', 'sigma_gBulge', 'sigma_rBulge', 

'sigma_iBulge', 'sigma_zBulge', 'sigma_yBulge') 

def get_photometric_uncertainties_bulge(self): 

""" 

Getter for photometric uncertainties associated with galaxy bulges 

""" 

 

return self._magnitudeUncertaintyGetter(['uBulge', 'gBulge', 'rBulge', 

'iBulge', 'zBulge', 'yBulge'], 

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

'lsstBandpassDict') 

 

 

@compound('sigma_uDisk', 'sigma_gDisk', 'sigma_rDisk', 

'sigma_iDisk', 'sigma_zDisk', 'sigma_yDisk') 

def get_photometric_uncertainties_disk(self): 

""" 

Getter for photometeric uncertainties associated with galaxy disks 

""" 

 

return self._magnitudeUncertaintyGetter(['uDisk', 'gDisk', 'rDisk', 

'iDisk', 'zDisk', 'yDisk'], 

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

'lsstBandpassDict') 

 

 

@compound('sigma_uAgn', 'sigma_gAgn', 'sigma_rAgn', 

'sigma_iAgn', 'sigma_zAgn', 'sigma_yAgn') 

def get_photometric_uncertainties_agn(self): 

""" 

Getter for photometric uncertainties associated with Agn 

""" 

 

return self._magnitudeUncertaintyGetter(['uAgn', 'gAgn', 'rAgn', 

'iAgn', 'zAgn', 'yAgn'], 

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

'lsstBandpassDict') 

 

 

@compound('uBulge', 'gBulge', 'rBulge', 'iBulge', 'zBulge', 'yBulge') 

def get_lsst_bulge_mags(self): 

""" 

Getter for bulge magnitudes in LSST bandpasses 

""" 

 

# load a BandpassDict of LSST bandpasses, if not done already 

if not hasattr(self, 'lsstBandpassDict'): 

self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

 

# actually calculate the magnitudes 

mag = self._quiescentMagnitudeGetter('bulge', self.lsstBandpassDict, 

self.get_lsst_bulge_mags._colnames) 

 

mag += self._variabilityGetter(self.get_lsst_bulge_mags._colnames) 

return mag 

 

 

@compound('uDisk', 'gDisk', 'rDisk', 'iDisk', 'zDisk', 'yDisk') 

def get_lsst_disk_mags(self): 

""" 

Getter for galaxy disk magnitudes in the LSST bandpasses 

""" 

 

# load a BandpassDict of LSST bandpasses, if not done already 

544 ↛ 545line 544 didn't jump to line 545, because the condition on line 544 was never true if not hasattr(self, 'lsstBandpassDict'): 

self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

 

# actually calculate the magnitudes 

mag = self._quiescentMagnitudeGetter('disk', self.lsstBandpassDict, 

self.get_lsst_disk_mags._colnames) 

 

mag += self._variabilityGetter(self.get_lsst_disk_mags._colnames) 

return mag 

 

 

@compound('uAgn', 'gAgn', 'rAgn', 'iAgn', 'zAgn', 'yAgn') 

def get_lsst_agn_mags(self): 

""" 

Getter for AGN magnitudes in the LSST bandpasses 

""" 

 

# load a BandpassDict of LSST bandpasses, if not done already 

if not hasattr(self, 'lsstBandpassDict'): 

self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

 

# actually calculate the magnitudes 

mag = self._quiescentMagnitudeGetter('agn', self.lsstBandpassDict, 

self.get_lsst_agn_mags._colnames) 

 

mag += self._variabilityGetter(self.get_lsst_agn_mags._colnames) 

return mag 

 

@compound('lsst_u', 'lsst_g', 'lsst_r', 'lsst_i', 'lsst_z', 'lsst_y') 

def get_lsst_total_mags(self): 

""" 

Getter for total galaxy magnitudes in the LSST bandpasses 

""" 

 

idList = self.column_by_name('uniqueId') 

numObj = len(idList) 

output = [] 

 

# Loop over the columns calculated by this getter. For each 

# column, calculate the bluge, disk, and agn magnitude in the 

# corresponding bandpass, then sum them using the 

# sum_magnitudes method. 

for columnName in self.get_lsst_total_mags._colnames: 

if columnName not in self._actually_calculated_columns: 

sub_list = [numpy.NaN]*numObj 

else: 

bandpass = columnName[-1] 

bulge = self.column_by_name('%sBulge' % bandpass) 

disk = self.column_by_name('%sDisk' % bandpass) 

agn = self.column_by_name('%sAgn' % bandpass) 

sub_list = self.sum_magnitudes(bulge=bulge, disk=disk, agn=agn) 

 

output.append(sub_list) 

return numpy.array(output) 

 

 

 

 

class PhotometryStars(PhotometryBase): 

""" 

This mixin provides the infrastructure for doing photometry on stars 

 

It assumes that we want LSST filters. 

""" 

 

def _loadSedList(self, wavelen_match): 

""" 

Method to load the member variable self._sedList, which is a SedList. 

If self._sedList does not already exist, this method sets it up. 

If it does already exist, this method flushes its contents and loads a new 

chunk of Seds. 

""" 

 

sedNameList = self.column_by_name('sedFilename') 

magNormList = self.column_by_name('magNorm') 

galacticAvList = self.column_by_name('galacticAv') 

 

if len(sedNameList)==0: 

return numpy.ones((0)) 

 

if not hasattr(self, '_sedList'): 

self._sedList = SedList(sedNameList, magNormList, 

galacticAvList=galacticAvList, 

wavelenMatch=wavelen_match, 

fileDir=getPackageDir('sims_sed_library'), 

specMap=defaultSpecMap) 

else: 

self._sedList.flush() 

self._sedList.loadSedsFromList(sedNameList, magNormList, 

galacticAvList=galacticAvList) 

 

 

def _quiescentMagnitudeGetter(self, bandpassDict, columnNameList): 

""" 

This method gets the magnitudes for an InstanceCatalog, returning them 

in a 2-D numpy array in which rows correspond to bandpasses and columns 

correspond to astronomical objects. 

 

@param [in] bandpassDict is a BandpassDict containing the bandpasses 

whose magnitudes are to be calculated 

 

@param [in] columnNameList is a list of the names of the magnitude columns 

being calculated 

 

@param [out] magnitudes is a 2-D numpy array of magnitudes in which 

rows correspond to bandpasses in bandpassDict and columns correspond 

to astronomical objects. 

""" 

 

# figure out which of these columns we are actually calculating 

indices = [ii for ii, name in enumerate(columnNameList) 

if name in self._actually_calculated_columns] 

 

if len(indices) == len(columnNameList): 

indices = None 

 

self._loadSedList(bandpassDict.wavelenMatch) 

 

if not hasattr(self, '_sedList'): 

magnitudes = numpy.ones((len(columnNameList),0)) 

else: 

magnitudes = bandpassDict.magListForSedList(self._sedList, indices=indices).transpose() 

 

return magnitudes 

 

@compound('quiescent_lsst_u', 'quiescent_lsst_g', 'quiescent_lsst_r', 

'quiescent_lsst_i', 'quiescent_lsst_z', 'quiescent_lsst_y') 

def get_quiescent_lsst_magnitudes(self): 

 

if not hasattr(self, 'lsstBandpassDict'): 

self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

 

return self._quiescentMagnitudeGetter(self.lsstBandpassDict, 

self.get_quiescent_lsst_magnitudes._colnames) 

 

@compound('lsst_u','lsst_g','lsst_r','lsst_i','lsst_z','lsst_y') 

def get_lsst_magnitudes(self): 

""" 

getter for LSST stellar magnitudes 

""" 

 

magnitudes = numpy.array([self.column_by_name('quiescent_lsst_u'), 

self.column_by_name('quiescent_lsst_g'), 

self.column_by_name('quiescent_lsst_r'), 

self.column_by_name('quiescent_lsst_i'), 

self.column_by_name('quiescent_lsst_z'), 

self.column_by_name('quiescent_lsst_y')]) 

 

delta = self._variabilityGetter(self.get_lsst_magnitudes._colnames) 

magnitudes += delta 

 

return magnitudes 

 

class PhotometrySSM(PhotometryBase): 

""" 

A mixin to calculate photometry for solar system objects. 

""" 

# Because solar system objects will not have dust extinctions, we should be able to read in every 

# SED exactly once, calculate the colors and magnitudes, and then get actual magnitudes by adding 

# an offset based on magNorm. 

 

def _quiescentMagnitudeGetter(self, bandpassDict, columnNameList, bandpassTag='lsst'): 

""" 

Method that actually does the work calculating magnitudes for solar system objects. 

 

Because solar system objects have no dust extinction, this method works by loading 

each unique Sed once, normalizing it, calculating its magnitudes in the desired 

bandpasses, and then storing the normalizing magnitudes and the bandpass magnitudes 

in a dict. Magnitudes for subsequent objects with identical Seds will be calculated 

by adding an offset to the magnitudes. The offset is determined by comparing normalizing 

magnitues. 

 

@param [in] bandpassDict is an instantiation of BandpassDict representing the bandpasses 

to be integrated over 

 

@param [in] columnNameList is a list of the names of the columns being calculated 

by this getter 

 

@param [in] bandpassTag (optional) is a string indicating the name of the bandpass system 

(i.e. 'lsst', 'sdss', etc.). This is in case the user wants to calculate the magnitudes 

in multiple systems simultaneously. In that case, the dict will store magnitudes for each 

Sed in each magnitude system separately. 

 

@param [out] a numpy array of magnitudes corresponding to bandpassDict. 

""" 

 

# figure out which of these columns we are actually calculating 

indices = [ii for ii, name in enumerate(columnNameList) 

if name in self._actually_calculated_columns] 

 

if len(indices) == len(columnNameList): 

indices = None 

 

if not hasattr(self, '_ssmMagDict'): 

self._ssmMagDict = {} 

self._ssmMagNormDict = {} 

self._file_dir = getPackageDir('sims_sed_library') 

self._spec_map = defaultSpecMap 

self._normalizing_bandpass = Bandpass() 

self._normalizing_bandpass.imsimBandpass() 

 

sedNameList = self.column_by_name('sedFilename') 

magNormList = self.column_by_name('magNorm') 

 

if len(sedNameList)==0: 

# need to return something when InstanceCatalog goes through 

# it's "dry run" to determine what columns are required from 

# the database 

return numpy.zeros((len(bandpassDict.keys()),0)) 

 

magListOut = [] 

 

for sedName, magNorm in zip(sedNameList, magNormList): 

magTag = bandpassTag+'_'+sedName 

if sedName not in self._ssmMagNormDict or magTag not in self._ssmMagDict: 

dummySed = Sed() 

dummySed.readSED_flambda(os.path.join(self._file_dir, self._spec_map[sedName])) 

fnorm = dummySed.calcFluxNorm(magNorm, self._normalizing_bandpass) 

dummySed.multiplyFluxNorm(fnorm) 

magList = bandpassDict.magListForSed(dummySed, indices=indices) 

self._ssmMagDict[magTag] = magList 

self._ssmMagNormDict[sedName] = magNorm 

else: 

dmag = magNorm - self._ssmMagNormDict[sedName] 

magList = self._ssmMagDict[magTag] + dmag 

magListOut.append(magList) 

 

return numpy.array(magListOut).transpose() 

 

 

@compound('lsst_u','lsst_g','lsst_r','lsst_i','lsst_z','lsst_y') 

def get_lsst_magnitudes(self): 

""" 

getter for LSST magnitudes of solar system objects 

""" 

if not hasattr(self, 'lsstBandpassDict'): 

self.lsstBandpassDict = BandpassDict.loadTotalBandpassesFromFiles() 

 

return self._quiescentMagnitudeGetter(self.lsstBandpassDict, self.get_lsst_magnitudes._colnames) 

 

 

def get_magFilter(self): 

""" 

Generate the magnitude in the filter of the observation. 

""" 

magFilter = 'lsst_' + self.obs_metadata.bandpass 

return self.column_by_name(magFilter) 

 

def get_magSNR(self): 

""" 

Calculate the SNR for the observation, given m5 from obs_metadata and the trailing losses. 

""" 

magFilter = self.column_by_name('magFilter') 

bandpass = self.lsstBandpassDict[self.obs_metadata.bandpass] 

# Get m5 for the visit 

m5 = self.obs_metadata.m5[self.obs_metadata.bandpass] 

# Adjust the magnitude of the source for the trailing losses. 

dmagSNR = self.column_by_name('dmagTrailing') 

magObj = magFilter - dmagSNR 

if len(magObj) == 0: 

snr = [] 

else: 

snr, gamma = calcSNR_m5(magObj, bandpass, m5, self.photParams) 

return snr 

 

def get_visibility(self): 

""" 

Generate a None/1 flag indicating whether the object was detected or not. 

 

Sets the random seed for 'calculateVisibility' using the obs_metadata.obsHistId 

""" 

magFilter = self.column_by_name('magFilter') 

dmagDetect = self.column_by_name('dmagDetection') 

magObj = magFilter - dmagDetect 

# Adjusted m5 value, accounting for the fact these are moving objects. 

mjdSeed = numpy.int(self.obs_metadata.mjd.TAI * 1000000) % 4294967295 

visibility = self.calculateVisibility(magObj, randomSeed=mjdSeed, pre_generate_randoms=True) 

return visibility 

 

 

@compound('dmagTrailing', 'dmagDetection') 

def get_ssm_dmag(self): 

""" 

This getter will calculate: 

 

dmagTrailing: the offset in m5 used to represent the loss in signal to noise 

resulting from the fact that the object's motion smears out its PSF 

 

dmagDetection: the offset in m5 used to represent the shift in detection 

threshold resulting from the fact that the object's motion smears out 

its PSF 

""" 

 

if self.obs_metadata.seeing is None: 

raise RuntimeError("Cannot calculate dmagTraling/dmagDetection. " 

"Your catalog's ObservationMetaData does not " 

"specify seeing.") 

 

if len(self.obs_metadata.seeing)>1: 

valueList = list(self.obs_metadata.seeing.values()) 

844 ↛ 851line 844 didn't jump to line 851, because the loop on line 844 didn't complete for ix in range(1, len(valueList)): 

845 ↛ 844line 845 didn't jump to line 844, because the condition on line 845 was never false if numpy.abs(valueList[ix]-valueList[0])>0.0001: 

 

raise RuntimeError("dmagTrailing/dmagDetection calculation is confused. " 

"Your catalog's ObservationMetaData contains multiple " 

"seeing values. Re-create your catalog with only one seeing value.") 

 

if not hasattr(self, 'photParams') or self.photParams is None: 

raise RuntimeError("You cannot calculate dmagTrailing/dmagDetection. " 

"Your catalog does not have an associated PhotometricParameters " 

"member variable. It is impossible to know what the exposure time is.") 

 

dradt = self.column_by_name('velRa') # in radians per day (actual sky velocity; 

# i.e., no need to divide by cos(dec)) 

 

ddecdt = self.column_by_name('velDec') # in radians per day 

 

if len(dradt)==0: 

return numpy.zeros((2,0)) 

 

a_trail = 0.76 

b_trail = 1.16 

a_det = 0.42 

b_det = 0.00 

seeing = self.obs_metadata.seeing[self.obs_metadata.bandpass] # this will be in arcsec 

texp = self.photParams.nexp*self.photParams.exptime # in seconds 

velocity = numpy.sqrt(numpy.power(numpy.degrees(dradt),2) + numpy.power(numpy.degrees(ddecdt),2)) # in degrees/day 

x = velocity*texp/(24.0*seeing) 

xsq = numpy.power(x,2) 

dmagTrail = 1.25*numpy.log10(1.0 + a_trail * xsq/(1.0+b_trail*x)) 

dmagDetect = 1.25*numpy.log10(1.0 + a_det * xsq/(1.0 + b_det*x)) 

 

return numpy.array([dmagTrail, dmagDetect])