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

from __future__ import with_statement 

from builtins import zip 

from builtins import object 

import os 

import numpy as np 

import unittest 

import tempfile 

import shutil 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.sims.utils.CodeUtilities import sims_clean_up 

from lsst.sims.utils import defaultSpecMap, altAzPaFromRaDec, ObservationMetaData 

from lsst.sims.catalogs.definitions import CompoundInstanceCatalog 

from lsst.sims.catalogs.db import fileDBObject 

from lsst.sims.catUtils.baseCatalogModels import SNDBObj 

from lsst.sims.catUtils.utils import (testStarsDBObj, testGalaxyDiskDBObj, 

testGalaxyBulgeDBObj, testGalaxyAgnDBObj) 

from lsst.sims.catUtils.exampleCatalogDefinitions import (PhoSimCatalogSersic2D, PhoSimCatalogPoint, 

PhoSimCatalogZPoint, PhoSimCatalogSSM, 

PhoSimCatalogSN) 

from lsst.sims.catUtils.utils import makePhoSimTestDB 

 

# 2016 November 22 

# For some reason, the Jenkins nodes used for continuous integration 

# cannot properly load the astropy config directories used by sncosmo. 

# To prevent this from crashing every build, we will test whether 

# the directories can be accessed and, if they cannot, use unittest.skipIf() 

# to skip all of the unit tests in this file. 

from astropy.config import get_config_dir 

 

_skip_sn_tests = False 

try: 

get_config_dir() 

except: 

_skip_sn_tests = True 

 

ROOT = os.path.abspath(os.path.dirname(__file__)) 

 

 

def createTestSNDB(): 

""" 

Create a CatalogDBObject-like database that contains everything needed to create 

a supernova catalog. Return the CatalogDBObject and an ObservationMetaData pointing 

to its center. 

 

Note: the OpsimMetaData for the returned ObservationMetaData will be inconsistent. 

It is just there so that PhoSim InstanceCatalog classes can write out a header. 

""" 

 

raCenter = 23.0 

decCenter = -19.0 

radius = 0.1 

obs = ObservationMetaData(pointingRA=raCenter, pointingDec=decCenter, boundType='circle', 

boundLength=radius, rotSkyPos=33.0, mjd=59580.0, 

bandpassName='r') 

 

# these will be totally inconsistent; just need something to put in the header 

obs.OpsimMetaData = {'dist2Moon': 0.1, 'moonalt': -0.2, 

'moonra': 1.1, 'moondec': 0.5, 

'rottelpos': 0.4, 'sunalt': -1.1} 

 

rng = np.random.RandomState(88) 

n_obj = 10 

rr = rng.random_sample(n_obj)*radius 

theta = rng.random_sample(n_obj)*2.0*np.pi 

ra_list = raCenter + rr*np.cos(theta) 

dec_list = decCenter + rr*np.sin(theta) 

t0_list = 0.5*rng.random_sample(n_obj) 

x0_list = rng.random_sample(n_obj)*1.0e-4 

x1_list = rng.random_sample(n_obj) 

c_list = rng.random_sample(n_obj)*2.0 

z_list = rng.random_sample(n_obj)*1.1 + 0.1 

 

txt_file_name = tempfile.mktemp(dir=ROOT, prefix='test_phosim_sn_source', suffix='.txt') 

 

dtype = np.dtype([('id', int), ('snra', float), ('sndec', float), 

('t0', float), ('x0', float), ('x1', float), 

('c', float), ('redshift', float), ('galtileid', int)]) 

 

with open(txt_file_name, 'w') as output_file: 

for ix, (ra, dec, t0, x0, x1, c, z) in enumerate(zip(ra_list, dec_list, t0_list, x0_list, 

x1_list, c_list, z_list)): 

output_file.write('%d %e %e %e %e %e %e %e %d\n' % 

(ix, ra, dec, t0, x0, x1, c, z, ix)) 

 

class testSNDBObj(SNDBObj, fileDBObject): 

tableid = 'test' 

 

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

return fileDBObject.query_columns(self, *args, **kwargs) 

 

dbobj = testSNDBObj(txt_file_name, runtable='test', dtype=dtype) 

 

if os.path.exists(txt_file_name): 

os.unlink(txt_file_name) 

 

return dbobj, obs 

 

 

test_header_map = {'dist2moon': ('dist2moon', np.degrees), 

'moonalt': ('moonalt', np.degrees), 

'moondec': ('moondec', np.degrees), 

'moonra': ('moonra', np.degrees), 

'rottelpos': ('rottelpos', np.degrees), 

'sunalt': ('sunalt', np.degrees)} 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

class PhoSimCatalogTest(unittest.TestCase): 

 

longMessage = True 

 

@classmethod 

def setUpClass(cls): 

cls.scratch_dir = tempfile.mkdtemp(dir=ROOT, prefix='PhoSimCatalogTest-') 

 

@classmethod 

def tearDownClass(cls): 

sims_clean_up() 

123 ↛ exitline 123 didn't return from function 'tearDownClass', because the condition on line 123 was never false if os.path.exists(cls.scratch_dir): 

shutil.rmtree(cls.scratch_dir) 

 

def setUp(self): 

self.tempDB = os.path.join(self.scratch_dir, 'PhoSimTestDatabase.db') 

self.obs_metadata = makePhoSimTestDB(size=10, filename=self.tempDB) 

self.bulgeDB = testGalaxyBulgeDBObj(driver='sqlite', database=self.tempDB) 

self.diskDB = testGalaxyDiskDBObj(driver='sqlite', database=self.tempDB) 

self.agnDB = testGalaxyAgnDBObj(driver='sqlite', database=self.tempDB) 

self.starDB = testStarsDBObj(driver='sqlite', database=self.tempDB) 

filter_translation = {'u': 0, 'g': 1, 'r': 2, 'i': 3, 'z': 4, 'y': 5} 

alt, az, pa = altAzPaFromRaDec(self.obs_metadata.pointingRA, 

self.obs_metadata.pointingDec, 

self.obs_metadata, includeRefraction=False) 

self.control_header = ['moondec %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moondec']), 

'rottelpos %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['rottelpos']), 

'declination %.17f\n' % self.obs_metadata.pointingDec, 

'moonalt %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moonalt']), 

'rotskypos %.17f\n' % self.obs_metadata.rotSkyPos, 

'moonra %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['moonra']), 

'sunalt %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['sunalt']), 

'mjd %.17f\n' % (self.obs_metadata.mjd.TAI+16.5/86400.0), 

'azimuth %.17f\n' % az, 

'rightascension %.17f\n' % self.obs_metadata.pointingRA, 

'dist2moon %.7f\n' % np.degrees(self.obs_metadata.OpsimMetaData['dist2moon']), 

'filter %d\n' % filter_translation[self.obs_metadata.bandpass], 

'altitude %.17f\n' % alt] 

 

def tearDown(self): 

del self.starDB 

del self.bulgeDB 

del self.diskDB 

del self.agnDB 

156 ↛ exitline 156 didn't return from function 'tearDown', because the condition on line 156 was never false if os.path.exists(self.tempDB): 

os.unlink(self.tempDB) 

 

def verify_catalog(self, file_name): 

""" 

Verify that the catalog specified by file_name has the expected header and is not empty 

""" 

with open(file_name, 'r') as input_file: 

lines = input_file.readlines() 

for control_line in self.control_header: 

self.assertIn(control_line, lines) 

 

self.assertGreater(len(lines), len(self.control_header)+3) 

 

def testSpecFileMap(self): 

""" 

Test that the PhoSim InstanceCatalog SpecFileMaps map MLT dwarf spectra 

to the starSED/phoSimMLT/ directory (that is where the MLT spectra which 

have been clipped to honor PhoSim's 'no more than 24,999 lines per SED 

file' requirement reside) 

""" 

 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=self.obs_metadata) 

self.assertEqual(cat.specFileMap['lte_123.txt'], 'starSED/phoSimMLT/lte_123.txt.gz') 

self.assertEqual(cat.specFileMap['lte_123.txt.gz'], 'starSED/phoSimMLT/lte_123.txt.gz') 

self.assertNotEqual(cat.specFileMap['lte_123.txt'], defaultSpecMap['lte_123.txt']) 

 

# verify that the usual stellar mappings still apply 

for test_name in ('kp_123.txt', 'km_123.txt', 'Const_123.txt', 'Exp_123.txt', 'Burst_123.txt', 

'bergeron_123.txt', 'burrows_123.txt', 'm1_123.txt', 'L1_123.txt', 'l1_123.txt', 

'Inst_123.txt'): 

 

self.assertEqual(cat.specFileMap[test_name], defaultSpecMap[test_name]) 

 

def test_incomplete_obs(self): 

""" 

Test that an exception gets raised if you try to make a PhoSim InstanceCatalog 

with an ObservationMetaData that lacks RA, Dec, mjd, bandpass, or rotSkyPos 

""" 

obs = ObservationMetaData(pointingDec=19.0, mjd=43000.0, rotSkyPos=19.0, bandpassName='u') 

with self.assertRaises(TypeError): 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

obs = ObservationMetaData(pointingRA=19.0, mjd=43000.0, rotSkyPos=19.0, bandpassName='u') 

with self.assertRaises(TypeError): 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

obs = ObservationMetaData(pointingRA=88.0, pointingDec=19.0, rotSkyPos=19.0, bandpassName='u') 

with self.assertRaises(RuntimeError): 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

obs = ObservationMetaData(pointingRA=88.0, pointingDec=19.0, mjd=43000.0, bandpassName='u') 

with self.assertRaises(TypeError): 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

obs = ObservationMetaData(pointingRA=88.0, pointingDec=19.0, mjd=43000.0, rotSkyPos=19.0) 

with self.assertRaises(KeyError): 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

obs = ObservationMetaData(pointingRA=88.0, pointingDec=19.0, mjd=43000.0, rotSkyPos=19.0, 

bandpassName='u') 

 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=obs) 

cat.phoSimHeaderMap = {} 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

cat.write_catalog(catName) 

 

def testCatalog(self): 

""" 

This test writes a PhoSim input catalog and compares it, one line at a time 

to a previously written catalog that should be identical. 

""" 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata = self.obs_metadata) 

testDisk = PhoSimCatalogSersic2D(self.diskDB, obs_metadata = self.obs_metadata) 

testAgn = PhoSimCatalogZPoint(self.agnDB, obs_metadata = self.obs_metadata) 

testStar = PhoSimCatalogPoint(self.starDB, obs_metadata = self.obs_metadata) 

 

testBulge.phoSimHeaderMap = test_header_map 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

testDisk.write_catalog(catName, write_header=False, write_mode='a') 

testAgn.write_catalog(catName, write_header=False, write_mode='a') 

testStar.write_catalog(catName, write_header=False, write_mode='a') 

 

self.verify_catalog(catName) 

 

def testHeaderMap(self): 

""" 

Test the behavior of the phoSimHeaderMap 

""" 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

testBulge.phoSimHeaderMap = {'lunar_distance': ('dist2moon', None), 

'rotation_of_the_telescope': ('rottelpos', np.degrees), 

'other_rotation': ('rottelpos', lambda x: x*x)} 

 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

with open(catName, 'r') as input_file: 

input_header = {} 

271 ↛ 278line 271 didn't jump to line 278, because the loop on line 271 didn't complete for line in input_file: 

vv = line.split() 

if vv[0] != 'object': 

input_header[vv[0]] = vv[1] 

else: 

break 

 

self.assertIn('rightascension', input_header) 

self.assertIn('declination', input_header) 

self.assertIn('altitude', input_header) 

self.assertIn('azimuth', input_header) 

self.assertIn('filter', input_header) 

self.assertIn('rotskypos', input_header) 

self.assertIn('mjd', input_header) 

self.assertIn('lunar_distance', input_header) 

self.assertAlmostEqual(float(input_header['lunar_distance']), 

self.obs_metadata.OpsimMetaData['dist2moon'], 6) 

self.assertIn('rotation_of_the_telescope', input_header) 

self.assertAlmostEqual(float(input_header['rotation_of_the_telescope']), 

np.degrees(self.obs_metadata.OpsimMetaData['rottelpos']), 

delta=1.0e-6*np.degrees(self.obs_metadata.OpsimMetaData['rottelpos'])) 

self.assertIn('other_rotation', input_header) 

self.assertAlmostEqual(float(input_header['other_rotation']), 

self.obs_metadata.OpsimMetaData['rottelpos']**2, 

delta=1.0e-6*self.obs_metadata.OpsimMetaData['rottelpos']**2) 

self.assertEqual(len(input_header), 10) 

 

def testBlankHeaderMap(self): 

""" 

Test behavior of a blank header map 

""" 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

testBulge.phoSimHeaderMap = {} 

 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

with open(catName, 'r') as input_file: 

input_header = {} 

310 ↛ 320line 310 didn't jump to line 320, because the loop on line 310 didn't complete for line in input_file: 

vv = line.split() 

if vv[0] != 'object': 

input_header[vv[0]] = vv[1] 

else: 

break 

 

# verify that only the default header parameters are included in the 

# PhoSimInstanceCatalog, even though obs_metadata has a non-None 

# OpsimMetaData 

self.assertIn('rightascension', input_header) 

self.assertIn('declination', input_header) 

self.assertIn('altitude', input_header) 

self.assertIn('azimuth', input_header) 

self.assertIn('filter', input_header) 

self.assertIn('rotskypos', input_header) 

self.assertIn('mjd', input_header) 

self.assertEqual(len(input_header), 7) 

self.assertGreater(len(self.obs_metadata.OpsimMetaData), 0) 

 

def testNoHeaderMap(self): 

""" 

Test that the correct error is raised if no header map is specified 

""" 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

 

with self.assertRaises(RuntimeError) as context: 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

self.assertIn("without specifying a phoSimHeaderMap", 

context.exception.args[0]) 

 

343 ↛ 348line 343 didn't jump to line 348, because the condition on line 343 was never false if os.path.exists(catName): 

os.unlink(catName) 

 

# now make sure that the exception is raised, even if ObservationMetaData 

# does not have an OpsimMetaData 

obs = ObservationMetaData(pointingRA=35.0, pointingDec=-23.0, 

mjd=43900.0, rotSkyPos=22.0, 

boundType='circle', boundLength=1.75) 

 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=obs) 

with self.assertRaises(RuntimeError) as context: 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

357 ↛ 360line 357 didn't jump to line 360, because the condition on line 357 was never false if os.path.exists(catName): 

os.unlink(catName) 

 

self.assertIn("without specifying a phoSimHeaderMap", 

context.exception.args[0]) 

self.assertIn("you may wish to consider adding default PhoSim parameters", 

context.exception.args[0]) 

 

def test_default_values_in_header_map(self): 

""" 

Test that default PhoSim header values in the header map get appropriately applied 

""" 

test_header_map = {'lunar_distance': ('dist2moon', None), 

'nsnap': 3} 

 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

testBulge.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

with open(catName, 'r') as input_file: 

input_header = {} 

380 ↛ 387line 380 didn't jump to line 387, because the loop on line 380 didn't complete for line in input_file: 

vv = line.split() 

if vv[0] != 'object': 

input_header[vv[0]] = vv[1] 

else: 

break 

 

self.assertIn('rightascension', input_header) 

self.assertIn('declination', input_header) 

self.assertIn('altitude', input_header) 

self.assertIn('azimuth', input_header) 

self.assertIn('filter', input_header) 

self.assertIn('rotskypos', input_header) 

self.assertIn('mjd', input_header) 

self.assertIn('lunar_distance', input_header) 

self.assertAlmostEqual(float(input_header['lunar_distance']), 

self.obs_metadata.OpsimMetaData['dist2moon'], 6) 

self.assertIn('nsnap', input_header) 

self.assertEqual(int(input_header['nsnap']), 3) 

self.assertEqual(len(input_header), 9) 

 

def test_non_existent_values_in_header_map(self): 

""" 

Test that header params that are defined in the header map but not 

in OpsimMetaData are ommitted from the header 

""" 

406 ↛ exitline 408 didn't finish the lambda on line 408 test_header_map = {'lunar_distance': ('dist2moon', None), 

'nsnap': 3, 

'nonesense': ('gobbledygook', lambda x: 2.0*x)} 

 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

testBulge.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

with open(catName, 'r') as input_file: 

input_header = {} 

418 ↛ 425line 418 didn't jump to line 425, because the loop on line 418 didn't complete for line in input_file: 

vv = line.split() 

if vv[0] != 'object': 

input_header[vv[0]] = vv[1] 

else: 

break 

 

self.assertIn('rightascension', input_header) 

self.assertIn('declination', input_header) 

self.assertIn('altitude', input_header) 

self.assertIn('azimuth', input_header) 

self.assertIn('filter', input_header) 

self.assertIn('rotskypos', input_header) 

self.assertIn('mjd', input_header) 

self.assertIn('lunar_distance', input_header) 

self.assertAlmostEqual(float(input_header['lunar_distance']), 

self.obs_metadata.OpsimMetaData['dist2moon'], 6) 

self.assertIn('nsnap', input_header) 

self.assertEqual(int(input_header['nsnap']), 3) 

self.assertEqual(len(input_header), 9) 

 

# now try it with no OpsimMetaData at all 

obs = ObservationMetaData(pointingRA=23.0, pointingDec=-11.0, 

mjd=43000.0, rotSkyPos=44.0, 

bandpassName='g', 

boundType='circle', boundLength=1.0) 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=obs) 

testBulge.phoSimHeaderMap = test_header_map 

with lsst.utils.tests.getTempFilePath('.txt') as catName: 

testBulge.write_catalog(catName) 

 

with open(catName, 'r') as input_file: 

input_header = {} 

for line in input_file: 

vv = line.split() 

453 ↛ 456line 453 didn't jump to line 456, because the condition on line 453 was never false if vv[0] != 'object': 

input_header[vv[0]] = vv[1] 

else: 

break 

 

self.assertIn('rightascension', input_header) 

self.assertIn('declination', input_header) 

self.assertIn('altitude', input_header) 

self.assertIn('azimuth', input_header) 

self.assertIn('filter', input_header) 

self.assertIn('rotskypos', input_header) 

self.assertIn('mjd', input_header) 

self.assertIn('nsnap', input_header) 

self.assertEqual(int(input_header['nsnap']), 3) 

self.assertEqual(len(input_header), 8) 

 

def testCompoundCatalog(self): 

""" 

This test writes a PhoSim input catalog and compares it, one line at a time 

to a previously written catalog that should be identical. 

 

This test uses CompoundInstanceCatalog 

""" 

 

# first, generate the catalog without a CompoundInstanceCatalog 

single_catName = tempfile.mktemp(dir=ROOT, prefix='phoSimTestCatalog_single', 

suffix='.txt') 

 

testBulge = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata = self.obs_metadata) 

testDisk = PhoSimCatalogSersic2D(self.diskDB, obs_metadata = self.obs_metadata) 

testAgn = PhoSimCatalogZPoint(self.agnDB, obs_metadata = self.obs_metadata) 

testStar = PhoSimCatalogPoint(self.starDB, obs_metadata = self.obs_metadata) 

 

testBulge.phoSimHeaderMap = test_header_map 

testBulge.write_catalog(single_catName) 

testDisk.write_catalog(single_catName, write_header=False, write_mode='a') 

testAgn.write_catalog(single_catName, write_header=False, write_mode='a') 

testStar.write_catalog(single_catName, write_header=False, write_mode='a') 

 

# now, generate the catalog using CompoundInstanceCatalog 

# 

# because the CompoundCatalogDBObject requires that database 

# connection parameters be set in the input CatalogDBObject 

# daughter class definitions, we have to declare dummy 

# CatalogDBObject daughter classes below 

 

class dummyDBbase(object): 

driver = 'sqlite' 

database = 'PhoSimTestDatabase.db' 

 

dummyDBbase.database = self.tempDB 

 

class dummyBulgeDB(dummyDBbase, testGalaxyBulgeDBObj): 

objid = 'dummy_bulge' 

 

class dummyDiskDB(dummyDBbase, testGalaxyDiskDBObj): 

objid = 'dummy_disk' 

 

class dummyAgnDB(dummyDBbase, testGalaxyAgnDBObj): 

objid = 'dummy_agn' 

 

class dummyStarDB(dummyDBbase, testStarsDBObj): 

objid = 'dummy_stars' 

 

compoundCatalog = CompoundInstanceCatalog([PhoSimCatalogSersic2D, PhoSimCatalogSersic2D, 

PhoSimCatalogZPoint, PhoSimCatalogPoint], 

[dummyBulgeDB, dummyDiskDB, dummyAgnDB, dummyStarDB], 

obs_metadata=self.obs_metadata) 

 

self.assertEqual(len(compoundCatalog._dbObjectGroupList[0]), 3) 

 

compound_catName = tempfile.mktemp(dir=ROOT, prefix='phoSimTestCatalog_compound', 

suffix='.txt') 

 

compoundCatalog.phoSimHeaderMap = test_header_map 

compoundCatalog.write_catalog(compound_catName) 

 

# verify that the compound catalog is what we expect 

self.verify_catalog(compound_catName) 

 

# verify that the two catalogs are equivalent 

with open(single_catName, 'r') as single_file: 

with open(compound_catName, 'r') as compound_file: 

single_lines = single_file.readlines() 

compound_lines = compound_file.readlines() 

 

for line in single_lines: 

self.assertIn(line, compound_lines) 

 

for line in compound_lines: 

self.assertIn(line, single_lines) 

 

545 ↛ 548line 545 didn't jump to line 548, because the condition on line 545 was never false if os.path.exists(compound_catName): 

os.unlink(compound_catName) 

 

548 ↛ exitline 548 didn't return from function 'testCompoundCatalog', because the condition on line 548 was never false if os.path.exists(single_catName): 

os.unlink(single_catName) 

 

def testPointSourceSchema(self): 

""" 

Create a PhoSim InstanceCatalog of point sources (stars). Verify 

that the schema of the actual objects conforms to what PhoSim expects, 

as defined here 

 

https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog 

""" 

cat = PhoSimCatalogPoint(self.starDB, obs_metadata=self.obs_metadata) 

cat.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

cat.write_catalog(cat_name) 

 

with open(cat_name, 'r') as input_file: 

cat_lines = input_file.readlines() 

 

n_obj = 0 

for line in cat_lines: 

params = line.split() 

if len(params) > 2: 

n_obj += 1 

self.assertEqual(len(params), 17) 

self.assertEqual(params[0], 'object') 

self.assertEqual(round(float(params[1])), float(params[1]), 10) # id 

float(params[2]) # ra 

float(params[3]) # dec 

float(params[4]) # mag norm 

self.assertIn('starSED', params[5]) # sed name 

self.assertAlmostEqual(float(params[6]), 0.0, 10) # redshift 

self.assertAlmostEqual(float(params[7]), 0.0, 10) # gamma1 

self.assertAlmostEqual(float(params[8]), 0.0, 10) # gamma2 

self.assertAlmostEqual(float(params[9]), 0.0, 10) # kappa 

self.assertAlmostEqual(float(params[10]), 0.0, 10) # delta_ra 

self.assertAlmostEqual(float(params[11]), 0.0, 10) # delta_dec 

self.assertEqual(params[12], 'point') # source type 

dust_msg = ('It is possible you are outputting Milky Way dust parameters before ' 

'internal dust parameters; internal dust should come first') 

self.assertEqual(params[13], 'none', msg=dust_msg) # internal dust 

self.assertEqual(params[14], 'CCM', msg=dust_msg) # Milky Way dust 

self.assertGreater(float(params[15]), 0.0, msg=dust_msg) # Av 

self.assertAlmostEqual(float(params[16]), 3.1, msg=dust_msg) # Rv 

 

self.assertGreater(n_obj, 0) 

 

def testSersicSchema(self): 

""" 

Create a PhoSim InstanceCatalog of Sersic profiles (galaxy bulges). Verify 

that the schema of the actual objects conforms to what PhoSim expects, 

as defined here 

 

https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog 

""" 

cat = PhoSimCatalogSersic2D(self.bulgeDB, obs_metadata=self.obs_metadata) 

cat.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

cat.write_catalog(cat_name) 

 

with open(cat_name, 'r') as input_file: 

cat_lines = input_file.readlines() 

 

n_obj = 0 

for line in cat_lines: 

params = line.split() 

if len(params) > 2: 

n_obj += 1 

self.assertEqual(len(params), 23) 

self.assertEqual(params[0], 'object') 

self.assertEqual(round(float(params[1])), float(params[1]), 10) # id 

float(params[2]) # ra 

float(params[3]) # dec 

float(params[4]) # mag norm 

self.assertIn('galaxySED', params[5]) # sed name 

self.assertGreater(float(params[6]), 0.0, 10) # redshift 

self.assertAlmostEqual(float(params[7]), 0.0, 10) # gamma1 

self.assertAlmostEqual(float(params[8]), 0.0, 10) # gamma2 

self.assertAlmostEqual(float(params[9]), 0.0, 10) # kappa 

self.assertAlmostEqual(float(params[10]), 0.0, 10) # delta_ra 

self.assertAlmostEqual(float(params[11]), 0.0, 10) # delta_dec 

self.assertEqual(params[12], 'sersic2d') # source type 

self.assertGreater(float(params[13]), 0.0) # major axis 

self.assertGreater(float(params[14]), 0.0) # minor axis 

self.assertGreater(float(params[15]), 0.0) # position angle 

self.assertAlmostEqual(float(params[16]), 4.0, 13) # n_s (bulges have sersic index=4) 

self.assertEqual(params[17], 'CCM') # internal dust 

dust_msg = ('It is possible you are outputting Milky Way dust parameters before ' 

'internal dust parameters; internal dust should come first') 

self.assertLess(float(params[18]), 0.31, msg=dust_msg) # Av 

self.assertLess(float(params[19]), 2.11, msg=dust_msg) # Rv 

self.assertEqual(params[20], 'CCM') # Milky Way dust 

self.assertGreater(float(params[21]), 0.0, msg=dust_msg) # Av 

self.assertAlmostEqual(float(params[22]), 3.1, msg=dust_msg) # Rv 

 

self.assertGreater(n_obj, 0) 

 

def testZPointSourceSchema(self): 

""" 

Create a PhoSim InstanceCatalog of extra-galactic point sources (agns). Verify 

that the schema of the actual objects conforms to what PhoSim expects, 

as defined here 

 

https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog 

""" 

cat = PhoSimCatalogZPoint(self.agnDB, obs_metadata=self.obs_metadata) 

cat.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

cat.write_catalog(cat_name) 

 

with open(cat_name, 'r') as input_file: 

cat_lines = input_file.readlines() 

 

n_obj = 0 

for line in cat_lines: 

params = line.split() 

if len(params) > 2: 

n_obj += 1 

self.assertEqual(len(params), 17) 

self.assertEqual(params[0], 'object') 

self.assertEqual(round(float(params[1])), float(params[1]), 10) # id 

float(params[2]) # ra 

float(params[3]) # dec 

float(params[4]) # mag norm 

self.assertIn('agnSED', params[5]) # sed name 

self.assertGreater(float(params[6]), 0.0) # redshift 

self.assertAlmostEqual(float(params[7]), 0.0, 10) # gamma1 

self.assertAlmostEqual(float(params[8]), 0.0, 10) # gamma2 

self.assertAlmostEqual(float(params[9]), 0.0, 10) # kappa 

self.assertAlmostEqual(float(params[10]), 0.0, 10) # delta_ra 

self.assertAlmostEqual(float(params[11]), 0.0, 10) # delta_dec 

self.assertEqual(params[12], 'point') # source type 

dust_msg = ('It is possible you are outputting Milky Way dust parameters before ' 

'internal dust parameters; internal dust should come first') 

self.assertEqual(params[13], 'none', msg=dust_msg) # internal dust 

self.assertEqual(params[14], 'CCM', msg=dust_msg) # Milky Way dust 

self.assertGreater(float(params[15]), 0.0, msg=dust_msg) # Av 

self.assertAlmostEqual(float(params[16]), 3.1, msg=dust_msg) # Rv 

 

self.assertGreater(n_obj, 0) 

 

@unittest.skipIf(_skip_sn_tests, "cannot properly load astropy config dir") 

def testSNSchema(self): 

""" 

Create a PhoSim InstanceCatalog of supernovae. Verify 

that the schema of the actual objects conforms to what PhoSim expects, 

as defined here 

 

https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog 

""" 

db, obs = createTestSNDB() 

cat = PhoSimCatalogSN(db, obs_metadata=obs) 

cat.writeSEDFile = False 

cat.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

cat.write_catalog(cat_name) 

 

with open(cat_name, 'r') as input_file: 

cat_lines = input_file.readlines() 

 

n_obj = 0 

for line in cat_lines: 

params = line.split() 

if len(params) > 2: 

n_obj += 1 

self.assertEqual(len(params), 17) 

self.assertEqual(params[0], 'object') 

self.assertEqual(round(float(params[1])), float(params[1]), 10) # id 

float(params[2]) # ra 

float(params[3]) # dec 

float(params[4]) # mag norm 

self.assertIn('%s.dat' % obs.bandpass, params[5]) # sed name 

self.assertGreater(float(params[6]), 0.0) # redshift 

self.assertAlmostEqual(float(params[7]), 0.0, 10) # gamma1 

self.assertAlmostEqual(float(params[8]), 0.0, 10) # gamma2 

self.assertAlmostEqual(float(params[9]), 0.0, 10) # kappa 

self.assertAlmostEqual(float(params[10]), 0.0, 10) # delta_ra 

self.assertAlmostEqual(float(params[11]), 0.0, 10) # delta_dec 

self.assertEqual(params[12], 'point') # source type 

dust_msg = ('It is possible you are outputting Milky Way dust parameters before ' 

'internal dust parameters; internal dust should come first') 

self.assertEqual(params[13], 'none', msg=dust_msg) # internal dust 

self.assertEqual(params[14], 'CCM', msg=dust_msg) # Milky Way dust 

self.assertGreater(float(params[15]), 0.0, msg=dust_msg) # Av 

self.assertAlmostEqual(float(params[16]), 3.1, msg=dust_msg) # Rv 

 

self.assertGreater(n_obj, 0) 

 

def testSSMSchema(self): 

""" 

Create a PhoSim InstanceCatalog of point sources (stars) formatted by the 

PhoSimCatalogSSM class. Verify that the schema of the actual objects conforms 

to what PhoSim expects, as defined here 

 

https://bitbucket.org/phosim/phosim_release/wiki/Instance%20Catalog 

""" 

cat = PhoSimCatalogSSM(self.starDB, obs_metadata=self.obs_metadata) 

cat.phoSimHeaderMap = test_header_map 

 

with lsst.utils.tests.getTempFilePath('.txt') as cat_name: 

cat.write_catalog(cat_name) 

 

with open(cat_name, 'r') as input_file: 

cat_lines = input_file.readlines() 

 

n_obj = 0 

for line in cat_lines: 

params = line.split() 

if len(params) > 2: 

n_obj += 1 

self.assertEqual(len(params), 15) 

self.assertEqual(params[0], 'object') 

self.assertEqual(round(float(params[1])), float(params[1]), 10) # id 

float(params[2]) # ra 

float(params[3]) # dec 

float(params[4]) # mag norm 

self.assertIn('starSED', params[5]) # sed name 

self.assertAlmostEqual(float(params[6]), 0.0, 10) # redshift 

self.assertAlmostEqual(float(params[7]), 0.0, 10) # gamma1 

self.assertAlmostEqual(float(params[8]), 0.0, 10) # gamma2 

self.assertAlmostEqual(float(params[9]), 0.0, 10) # kappa 

self.assertAlmostEqual(float(params[10]), 0.0, 10) # delta_ra 

self.assertAlmostEqual(float(params[11]), 0.0, 10) # delta_dec 

self.assertEqual(params[12], 'point') # source type 

self.assertEqual(params[13], 'none') # internal dust 

self.assertEqual(params[14], 'none') # Milky Way dust 

 

self.assertGreater(n_obj, 0) 

 

 

class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

pass 

 

785 ↛ 786line 785 didn't jump to line 786, because the condition on line 785 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()