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

""" 

This file defines the following classes: 

 

GalSimInterpreter -- a class which takes objects passed by a GalSim Instance Catalog 

(see galSimCatalogs.py) and uses GalSim to write them to FITS images. 

 

GalSimDetector -- a class which stored information about a detector in a way that 

GalSimInterpreter expects. 

""" 

from __future__ import print_function 

 

import math 

from builtins import object 

import os 

import pickle 

import numpy as np 

import galsim 

from lsst.sims.utils import radiansFromArcsec 

from lsst.sims.coordUtils import pixelCoordsFromPupilCoords 

from lsst.sims.GalSimInterface import make_galsim_detector, SNRdocumentPSF, \ 

Kolmogorov_and_Gaussian_PSF 

 

__all__ = ["make_gs_interpreter", "GalSimInterpreter", "GalSimSiliconInterpeter"] 

 

def make_gs_interpreter(obs_md, detectors, bandpassDict, noiseWrapper, 

epoch=None, seed=None, apply_sensor_model=False): 

gs_interpreter \ 

= GalSimSiliconInterpeter if apply_sensor_model else GalSimInterpreter 

 

return gs_interpreter(obs_metadata=obs_md, detectors=detectors, 

bandpassDict=bandpassDict, noiseWrapper=noiseWrapper, 

epoch=epoch, seed=seed) 

 

 

class GalSimInterpreter(object): 

""" 

This is the class which actually takes the objects contained in the GalSim 

InstanceCatalog and converts them into FITS images. 

""" 

 

def __init__(self, obs_metadata=None, detectors=None, 

bandpassDict=None, noiseWrapper=None, 

epoch=None, seed=None): 

 

""" 

@param [in] obs_metadata is an instantiation of the ObservationMetaData class which 

carries data about this particular observation (telescope site and pointing information) 

 

@param [in] detectors is a list of GalSimDetectors for which we are drawing FITS images 

 

@param [in] bandpassDict is a BandpassDict containing all of the bandpasses for which we are 

generating images 

 

@param [in] noiseWrapper is an instantiation of a NoiseAndBackgroundBase 

class which tells the interpreter how to add sky noise to its images. 

 

@param [in] seed is an integer that will use to seed the random number generator 

used when drawing images (if None, GalSim will automatically create a random number 

generator seeded with the system clock) 

""" 

 

self.obs_metadata = obs_metadata 

self.epoch = epoch 

self.PSF = None 

self.noiseWrapper = noiseWrapper 

 

if seed is not None: 

self._rng = galsim.UniformDeviate(seed) 

else: 

self._rng = None 

 

72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never true if detectors is None: 

raise RuntimeError("Will not create images; you passed no detectors to the GalSimInterpreter") 

 

self.detectors = detectors 

 

self.detectorImages = {} # this dict will contain the FITS images (as GalSim images) 

self.bandpassDict = bandpassDict 

self.blankImageCache = {} # this dict will cache blank images associated with specific detectors. 

# It turns out that calling the image's constructor is more 

# time-consuming than returning a deep copy 

self.checkpoint_file = None 

self.drawn_objects = set() 

self.nobj_checkpoint = 1000 

 

def setPSF(self, PSF=None): 

""" 

Set the PSF wrapper for this GalSimInterpreter 

 

@param [in] PSF is an instantiation of a class which inherits from PSFbase and defines _getPSF() 

""" 

self.PSF = PSF 

 

def _getFileName(self, detector=None, bandpassName=None): 

""" 

Given a detector and a bandpass name, return the name of the FITS file to be written 

 

@param [in] detector is an instantiation of GalSimDetector 

 

@param [in] bandpassName is a string i.e. 'u' denoting the filter being drawn 

 

The resulting filename will be detectorName_bandpassName.fits 

""" 

return detector.fileName+'_'+bandpassName+'.fits' 

 

def _doesObjectImpingeOnDetector(self, xPupil=None, yPupil=None, detector=None, 

imgScale=None, nonZeroPixels=None): 

""" 

Compare an astronomical object to a detector and determine whether or not that object will cast any 

light on that detector (in case the object is near the edge of a detector and will cast some 

incidental light onto it). 

 

This method is called by the method findAllDetectors. findAllDetectors will generate a test image 

of an astronomical object. It will find all of the pixels in that test image with flux above 

a certain threshold and pass that list of pixels into this method along with data characterizing 

the detector in question. This method compares the pupil coordinates of those pixels with the pupil 

coordinate domain of the detector. If some of those pixels fall inside the detector, then this method 

returns True (signifying that the astronomical object does cast light on the detector). If not, this 

method returns False. 

 

@param [in] xPupil the x pupil coordinate of the image's origin in arcseconds 

 

@param [in] yPupil the y pupil coordinate of the image's origin in arcseconds 

 

@param [in] detector an instantiation of GalSimDetector. This is the detector against 

which we will compare the object. 

 

@param [in] nonZeroPixels is a numpy array of non-zero pixels from the test image referenced 

above. nonZeroPixels[0] is their x coordinate (in pixel value). nonZeroPixels[1] is 

ther y coordinate. 

 

@param [in] imgScale is the platescale of the test image in arcseconds per pixel 

""" 

 

135 ↛ 136line 135 didn't jump to line 136, because the condition on line 135 was never true if detector is None: 

return False 

 

xPupilList = radiansFromArcsec(np.array([xPupil + ix*imgScale for ix in nonZeroPixels[0]])) 

yPupilList = radiansFromArcsec(np.array([yPupil + iy*imgScale for iy in nonZeroPixels[1]])) 

 

answer = detector.containsPupilCoordinates(xPupilList, yPupilList) 

 

if True in answer: 

return True 

else: 

return False 

 

def findAllDetectors(self, gsObject): 

 

""" 

Find all of the detectors on which a given astronomical object casts light. 

 

This method works by drawing a test image of the astronomical object and comparing 

the pixels in that image with flux above a certain threshold value to the pixel 

domains of the detectors in the camera. Any detectors which overlap these 

'active' pixels are considered illumined by the object. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject class 

carrying information about the object whose image is to be drawn 

 

@param [out] outputString is a string indicating which chips the object illumines 

(suitable for the GalSim InstanceCatalog classes) 

 

@param [out] outputList is a list of detector instantiations indicating which 

detectors the object illumines 

 

@param [out] centeredObj is a GalSim GSObject centered on the chip 

 

Note: parameters that only apply to Sersic profiles will be ignored in the case of 

pointSources, etc. 

""" 

 

outputString = '' 

outputList = [] 

centeredObj = None 

testScale = 0.1 

 

# create a GalSim Object centered on the chip. 

centeredObj = self.createCenteredObject(gsObject) 

 

181 ↛ 182line 181 didn't jump to line 182, because the condition on line 181 was never true if centeredObj is None: 

return 

 

# 4 March 2015 

# create a test image of the object to compare against the pixel 

# domains of each detector. Use photon shooting rather than real space integration 

# for reasons of speed. A flux of 1000 photons ought to be enough to plot the true 

# extent of the object, but this is just a guess. 

centeredImage = centeredObj.drawImage(scale=testScale, method='phot', n_photons=1000, rng=self._rng) 

xmax = testScale*(centeredImage.xmax/2) + gsObject.xPupilArcsec 

xmin = testScale*(-1*centeredImage.xmax/2) + gsObject.xPupilArcsec 

ymax = testScale*(centeredImage.ymax/2) + gsObject.yPupilArcsec 

ymin = testScale*(-1*centeredImage.ymin/2) + gsObject.yPupilArcsec 

 

# first assemble a list of detectors which have any hope 

# of overlapping the test image 

viableDetectors = [] 

for dd in self.detectors: 

xOverLaps = False 

if xmax > dd.xMinArcsec and xmax < dd.xMaxArcsec: 

xOverLaps = True 

elif xmin > dd.xMinArcsec and xmin < dd.xMaxArcsec: 

xOverLaps = True 

elif xmin < dd.xMinArcsec and xmax > dd.xMaxArcsec: 

xOverLaps = True 

 

yOverLaps = False 

if ymax > dd.yMinArcsec and ymax < dd.yMaxArcsec: 

yOverLaps = True 

elif ymin > dd.yMinArcsec and ymin < dd.yMaxArcsec: 

yOverLaps = True 

elif ymin < dd.yMinArcsec and ymax > dd.yMaxArcsec: 

yOverLaps = True 

 

if xOverLaps and yOverLaps and dd not in outputList: 

viableDetectors.append(dd) 

 

if len(viableDetectors) > 0: 

 

# Find the pixels that have a flux greater than 0.001 times the flux of 

# the central pixel (remember that the object is centered on the test image) 

maxPixel = centeredImage(centeredImage.xmax/2, centeredImage.ymax/2) 

activePixels = np.where(centeredImage.array > maxPixel*0.001) 

 

# Find the bounds of those active pixels in pixel coordinates 

xmin = testScale * (activePixels[0].min() - centeredImage.xmax/2) + gsObject.xPupilArcsec 

xmax = testScale * (activePixels[0].max() - centeredImage.xmax/2) + gsObject.xPupilArcsec 

ymin = testScale * (activePixels[1].min() - centeredImage.ymax/2) + gsObject.yPupilArcsec 

ymax = testScale * (activePixels[1].max() - centeredImage.ymax/2) + gsObject.yPupilArcsec 

 

# find all of the detectors that overlap with the bounds of the active pixels. 

for dd in viableDetectors: 

xOverLaps = False 

if xmax > dd.xMinArcsec and xmax < dd.xMaxArcsec: 

xOverLaps = True 

elif xmin > dd.xMinArcsec and xmin < dd.xMaxArcsec: 

xOverLaps = True 

elif xmin < dd.xMinArcsec and xmax > dd.xMaxArcsec: 

xOverLaps = True 

 

yOverLaps = False 

if ymax > dd.yMinArcsec and ymax < dd.yMaxArcsec: 

yOverLaps = True 

elif ymin > dd.yMinArcsec and ymin < dd.yMaxArcsec: 

yOverLaps = True 

elif ymin < dd.yMinArcsec and ymax > dd.yMaxArcsec: 

yOverLaps = True 

 

# specifically test that these overlapping detectors do contain active pixels 

if xOverLaps and yOverLaps: 

if self._doesObjectImpingeOnDetector(xPupil=gsObject.xPupilArcsec - 

centeredImage.xmax*testScale/2.0, 

yPupil=gsObject.yPupilArcsec - 

centeredImage.ymax*testScale/2.0, 

detector=dd, imgScale=centeredImage.scale, 

nonZeroPixels=activePixels): 

 

if outputString != '': 

outputString += '//' 

outputString += dd.name 

outputList.append(dd) 

 

if outputString == '': 

outputString = None 

 

return outputString, outputList, centeredObj 

 

def blankImage(self, detector=None): 

""" 

Draw a blank image associated with a specific detector. The image will have the correct size 

for the given detector. 

 

param [in] detector is an instantiation of GalSimDetector 

""" 

 

# in order to speed up the code (by a factor of ~2), this method 

# only draws a new blank image the first time it is called on a 

# given detector. It then caches the blank images it has drawn and 

# uses GalSim's copy() method to return copies of cached blank images 

# whenever they are called for again. 

 

if detector.name in self.blankImageCache: 

return self.blankImageCache[detector.name].copy() 

else: 

image = galsim.Image(detector.xMaxPix-detector.xMinPix+1, detector.yMaxPix-detector.yMinPix+1, 

wcs=detector.wcs) 

 

self.blankImageCache[detector.name] = image 

return image.copy() 

 

def drawObject(self, gsObject): 

""" 

Draw an astronomical object on all of the relevant FITS files. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject 

class carrying all of the information for the object whose image 

is to be drawn 

 

@param [out] outputString is a string denoting which detectors the astronomical 

object illumines, suitable for output in the GalSim InstanceCatalog 

""" 

 

# find the detectors which the astronomical object illumines 

outputString, \ 

detectorList, \ 

centeredObj = self.findAllDetectors(gsObject) 

 

if len(detectorList) == 0: 

# there is nothing to draw 

return outputString 

 

self._addNoiseAndBackground(detectorList) 

 

for bandpassName in self.bandpassDict: 

 

# create a new object if one has not already been created or if the PSF is wavelength 

# dependent (in which case, each filter is going to need its own initialized object) 

318 ↛ 319line 318 didn't jump to line 319, because the condition on line 318 was never true if centeredObj is None: 

return outputString 

 

for detector in detectorList: 

 

name = self._getFileName(detector=detector, bandpassName=bandpassName) 

 

xPix, yPix = detector.camera_wrapper.pixelCoordsFromPupilCoords(gsObject.xPupilRadians, 

gsObject.yPupilRadians, 

detector.name, 

self.obs_metadata) 

 

obj = centeredObj 

 

# convolve the object's shape profile with the spectrum 

obj = obj.withFlux(gsObject.flux(bandpassName)) 

 

self.detectorImages[name] = obj.drawImage(method='phot', 

gain=detector.photParams.gain, 

offset=galsim.PositionD(xPix-detector.xCenterPix, 

yPix-detector.yCenterPix), 

rng=self._rng, 

maxN=int(1e6), 

image=self.detectorImages[name], 

add_to_image=True) 

 

self.drawn_objects.add(gsObject.uniqueId) 

self.write_checkpoint() 

return outputString 

 

def _addNoiseAndBackground(self, detectorList): 

""" 

Go through the list of detector/bandpass combinations and 

initialize all of the FITS files we will need (if they have 

not already been initialized) 

""" 

for detector in detectorList: 

for bandpassName in self.bandpassDict: 

name = self._getFileName(detector=detector, bandpassName=bandpassName) 

if name not in self.detectorImages: 

self.detectorImages[name] = self.blankImage(detector=detector) 

if self.noiseWrapper is not None: 

# Add sky background and noise to the image 

self.detectorImages[name] = \ 

self.noiseWrapper.addNoiseAndBackground(self.detectorImages[name], 

bandpass=self.bandpassDict[bandpassName], 

m5=self.obs_metadata.m5[bandpassName], 

FWHMeff= 

self.obs_metadata.seeing[bandpassName], 

photParams=detector.photParams, 

detector=detector) 

 

 

def drawPointSource(self, gsObject, psf=None): 

""" 

Draw an image of a point source. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject class 

carrying information about the object whose image is to be drawn 

 

@param [in] psf PSF to use for the convolution. If None, then use self.PSF. 

""" 

if psf is None: 

381 ↛ 382line 381 didn't jump to line 382, because the condition on line 381 was never true if self.PSF is None: 

raise RuntimeError("Cannot draw a point source in GalSim without a PSF") 

psf = self.PSF 

 

return psf.applyPSF(xPupil=gsObject.xPupilArcsec, yPupil=gsObject.yPupilArcsec) 

 

def drawSersic(self, gsObject, psf=None): 

""" 

Draw the image of a Sersic profile. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject class 

carrying information about the object whose image is to be drawn 

 

@param [in] psf PSF to use for the convolution. If None, then use self.PSF. 

""" 

 

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

psf = self.PSF 

 

# create a Sersic profile 

centeredObj = galsim.Sersic(n=float(gsObject.sindex), 

half_light_radius=float(gsObject.halfLightRadiusArcsec)) 

 

# Turn the Sersic profile into an ellipse 

centeredObj = centeredObj.shear(q=gsObject.minorAxisRadians/gsObject.majorAxisRadians, 

beta=(0.5*np.pi+gsObject.positionAngleRadians)*galsim.radians) 

 

# Apply weak lensing distortion. 

centeredObj = centeredObj.lens(gsObject.g1, gsObject.g2, gsObject.mu) 

 

# Apply the PSF. 

if psf is not None: 

centeredObj = psf.applyPSF(xPupil=gsObject.xPupilArcsec, 

yPupil=gsObject.yPupilArcsec, 

obj=centeredObj) 

 

return centeredObj 

 

def drawRandomWalk(self, gsObject, psf=None): 

""" 

Draw the image of a RandomWalk light profile. In orider to allow for 

reproducibility, the specific realisation of the random walk is seeded 

by the object unique identifier, if provided. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject class 

carrying information about the object whose image is to be drawn 

 

@param [in] psf PSF to use for the convolution. If None, then use self.PSF. 

""" 

430 ↛ 433line 430 didn't jump to line 433, because the condition on line 430 was never false if psf is None: 

psf = self.PSF 

# Seeds the random walk with the object id if available 

433 ↛ 434line 433 didn't jump to line 434, because the condition on line 433 was never true if gsObject.uniqueId is None: 

rng=None 

else: 

rng=galsim.BaseDeviate(int(gsObject.uniqueId)) 

 

# Create the RandomWalk profile 

centeredObj = galsim.RandomWalk(npoints=int(gsObject.npoints), 

half_light_radius=float(gsObject.halfLightRadiusArcsec), 

rng=rng) 

 

# Apply intrinsic ellipticity to the profile 

centeredObj = centeredObj.shear(q=gsObject.minorAxisRadians/gsObject.majorAxisRadians, 

beta=(0.5*np.pi+gsObject.positionAngleRadians)*galsim.radians) 

 

# Apply weak lensing distortion. 

centeredObj = centeredObj.lens(gsObject.g1, gsObject.g2, gsObject.mu) 

 

# Apply the PSF. 

451 ↛ 452line 451 didn't jump to line 452, because the condition on line 451 was never true if psf is not None: 

centeredObj = psf.applyPSF(xPupil=gsObject.xPupilArcsec, 

yPupil=gsObject.yPupilArcsec, 

obj=centeredObj) 

 

return centeredObj 

 

def createCenteredObject(self, gsObject, psf=None): 

""" 

Create a centered GalSim Object (i.e. if we were just to draw this object as an image, 

the object would be centered on the frame) 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject class 

carrying information about the object whose image is to be drawn 

 

Note: parameters that obviously only apply to Sersic profiles will be ignored in the case 

of point sources 

""" 

 

if gsObject.galSimType == 'sersic': 

centeredObj = self.drawSersic(gsObject, psf=psf) 

 

elif gsObject.galSimType == 'pointSource': 

centeredObj = self.drawPointSource(gsObject, psf=psf) 

 

476 ↛ 480line 476 didn't jump to line 480, because the condition on line 476 was never false elif gsObject.galSimType == 'RandomWalk': 

centeredObj = self.drawRandomWalk(gsObject, psf=psf) 

 

else: 

print("Apologies: the GalSimInterpreter does not yet have a method to draw ") 

print(gsObject.galSimType) 

print(" objects\n") 

centeredObj = None 

 

return centeredObj 

 

def writeImages(self, nameRoot=None): 

""" 

Write the FITS files to disk. 

 

@param [in] nameRoot is a string that will be prepended to the names of the output 

FITS files. The files will be named like 

 

@param [out] namesWritten is a list of the names of the FITS files written 

 

nameRoot_detectorName_bandpassName.fits 

 

myImages_R_0_0_S_1_1_y.fits is an example of an image for an LSST-like camera with 

nameRoot = 'myImages' 

""" 

namesWritten = [] 

for name in self.detectorImages: 

503 ↛ 506line 503 didn't jump to line 506, because the condition on line 503 was never false if nameRoot is not None: 

fileName = nameRoot+'_'+name 

else: 

fileName = name 

self.detectorImages[name].write(file_name=fileName) 

namesWritten.append(fileName) 

 

return namesWritten 

 

def write_checkpoint(self, force=False): 

""" 

Write a pickle file of detector images packaged with the 

objects that have been drawn. By default, write the checkpoint 

every self.nobj_checkpoint objects. 

""" 

if self.checkpoint_file is None: 

return 

if force or len(self.drawn_objects) % self.nobj_checkpoint == 0: 

# The galsim.Images in self.detectorImages cannot be 

# pickled because they contain references to unpickleable 

# afw objects, so just save the array data and rebuild 

# the galsim.Images from scratch, given the detector name. 

images = {key: value.array for key, value 

in self.detectorImages.items()} 

image_state = dict(images=images, 

rng=self._rng, 

drawn_objects=self.drawn_objects) 

with open(self.checkpoint_file, 'wb') as output: 

pickle.dump(image_state, output) 

 

def restore_checkpoint(self, camera_wrapper, phot_params, obs_metadata, 

epoch=2000.0): 

""" 

Restore self.detectorImages, self._rng, and self.drawn_objects states 

from the checkpoint file. 

 

Parameters 

---------- 

camera_wrapper: lsst.sims.GalSimInterface.GalSimCameraWrapper 

An object representing the camera being simulated 

 

phot_params: lsst.sims.photUtils.PhotometricParameters 

An object containing the physical parameters representing 

the photometric properties of the system 

 

obs_metadata: lsst.sims.utils.ObservationMetaData 

Characterizing the pointing of the telescope 

 

epoch: float 

Representing the Julian epoch against which RA, Dec are 

reckoned (default = 2000) 

""" 

if (self.checkpoint_file is None 

or not os.path.isfile(self.checkpoint_file)): 

return 

with open(self.checkpoint_file, 'rb') as input_: 

image_state = pickle.load(input_) 

images = image_state['images'] 

for key in images: 

# Unmangle the detector name. 

detname = "R:{},{} S:{},{}".format(*tuple(key[1:3] + key[5:7])) 

# Create the galsim.Image from scratch as a blank image and 

# set the pixel data from the persisted image data array. 

detector = make_galsim_detector(camera_wrapper, detname, 

phot_params, obs_metadata, 

epoch=epoch) 

self.detectorImages[key] = self.blankImage(detector=detector) 

self.detectorImages[key] += image_state['images'][key] 

self._rng = image_state['rng'] 

self.drawn_objects = image_state['drawn_objects'] 

 

 

class GalSimSiliconInterpeter(GalSimInterpreter): 

""" 

This subclass of GalSimInterpreter applies the Silicon sensor 

model to the drawn objects. 

""" 

def __init__(self, obs_metadata=None, detectors=None, bandpassDict=None, 

noiseWrapper=None, epoch=None, seed=None): 

super(GalSimSiliconInterpeter, self)\ 

.__init__(obs_metadata=obs_metadata, detectors=detectors, 

bandpassDict=bandpassDict, noiseWrapper=noiseWrapper, 

epoch=epoch, seed=seed) 

 

self.sky_bg_per_pixel = None 

 

# Create a PSF that's fast to evaluate for the postage stamp 

# size calculation for extended objects in .getStampBounds. 

FWHMgeom = obs_metadata.OpsimMetaData['FWHMgeom'] 

self._double_gaussian_psf = SNRdocumentPSF(FWHMgeom) 

 

# Save the parameters needed to create a Kolmogorov PSF for a 

# custom value of gsparams.folding_threshold. That PSF will 

# to be used in the .getStampBounds function for bright stars. 

altRad = np.radians(obs_metadata.OpsimMetaData['altitude']) 

self._airmass = 1.0/np.sqrt(1.0-0.96*(np.sin(0.5*np.pi-altRad))**2) 

self._rawSeeing = obs_metadata.OpsimMetaData['rawSeeing'] 

self._band = obs_metadata.bandpass 

 

# Save the default folding threshold for determining when to recompute 

# the PSF for bright point sources. 

self._ft_default = galsim.GSParams().folding_threshold 

 

def drawObject(self, gsObject): 

""" 

Draw an astronomical object on all of the relevant FITS files. 

 

@param [in] gsObject is an instantiation of the GalSimCelestialObject 

class carrying all of the information for the object whose image 

is to be drawn 

 

@param [out] outputString is a string denoting which detectors the astronomical 

object illumines, suitable for output in the GalSim InstanceCatalog 

""" 

 

# find the detectors which the astronomical object illumines 

outputString, \ 

detectorList, \ 

centeredObj = self.findAllDetectors(gsObject) 

 

if len(detectorList) == 0: 

# there is nothing to draw 

return outputString 

 

self._addNoiseAndBackground(detectorList) 

 

# Create a surface operation to sample incident angles and a 

# galsim.SED object for sampling the wavelengths of the 

# incident photons. 

fratio = 1.234 # From https://www.lsst.org/scientists/keynumbers 

obscuration = 0.606 # (8.4**2 - 6.68**2)**0.5 / 8.4 

angles = galsim.FRatioAngles(fratio, obscuration, self._rng) 

 

sed_lut = galsim.LookupTable(x=gsObject.sed.wavelen, 

f=gsObject.sed.flambda) 

gs_sed = galsim.SED(sed_lut, wave_type='nm', flux_type='flambda', 

redshift=0.) 

 

for bandpassName in self.bandpassDict: 

# create a new object if one has not already been created 

# or if the PSF is wavelength dependent (in which case, 

# each filter is going to need its own initialized object) 

if centeredObj is None: 

return outputString 

 

bandpass = self.bandpassDict[bandpassName] 

index = np.where(bandpass.sb != 0) 

bp_lut = galsim.LookupTable(x=bandpass.wavelen[index], 

f=bandpass.sb[index]) 

gs_bandpass = galsim.Bandpass(bp_lut, wave_type='nm') 

waves = galsim.WavelengthSampler(sed=gs_sed, bandpass=gs_bandpass, 

rng=self._rng) 

 

# Set the object flux. 

flux = gsObject.flux(bandpassName) 

obj = centeredObj.withFlux(flux) 

 

for detector in detectorList: 

 

name = self._getFileName(detector=detector, 

bandpassName=bandpassName) 

 

xPix, yPix = detector.camera_wrapper\ 

.pixelCoordsFromPupilCoords(gsObject.xPupilRadians, 

gsObject.yPupilRadians, 

chipName=detector.name, 

obs_metadata=self.obs_metadata) 

 

sensor = galsim.SiliconSensor(rng=self._rng, 

treering_center=detector.tree_rings.center, 

treering_func=detector.tree_rings.func, 

transpose=True) 

surface_ops = [waves, angles] 

 

# Desired position to draw the object. 

image_pos = galsim.PositionD(xPix, yPix) 

 

# Find a postage stamp region to draw onto. Use (sky 

# noise)/3. as the nominal minimum surface brightness 

# for rendering an extended object. 

keep_sb_level = np.sqrt(self.sky_bg_per_pixel)/3. 

bounds = self.getStampBounds(gsObject, flux, image_pos, 

keep_sb_level, 3*keep_sb_level) 

 

# Ensure the bounds of the postage stamp lie within the image. 

bounds = bounds & self.detectorImages[name].bounds 

 

# offset is relative to the "true" center of the postage stamp. 

offset = image_pos - bounds.true_center 

 

if bounds.area() > 0: 

obj.drawImage(method='phot', 

offset=offset, 

rng=self._rng, 

maxN=int(1e6), 

image=self.detectorImages[name][bounds], 

sensor=sensor, 

surface_ops=surface_ops, 

add_to_image=True, 

gain=detector.photParams.gain) 

 

self.drawn_objects.add(gsObject.uniqueId) 

self.write_checkpoint() 

return outputString 

 

def getStampBounds(self, gsObject, flux, image_pos, keep_sb_level, 

large_object_sb_level, Nmax=1400, pixel_scale=0.2): 

""" 

Get the postage stamp bounds for drawing an object within the stamp 

to include the specified minimum surface brightness. Use the 

folding_threshold criterion for point source objects. For 

extended objects, use the getGoodPhotImageSize function, where 

if the initial stamp is too large (> Nmax**2 ~ 1GB of RSS 

memory for a 72 vertex/pixel sensor model), use the relaxed 

surface brightness level for large objects. 

 

Parameters 

---------- 

gsObject: GalSimCelestialObject 

This contains the information needed to construct a 

galsim.GSObject convolved with the desired PSF. 

flux: float 

The flux of the object in e-. 

keep_sb_level: float 

The minimum surface brightness (photons/pixel) out to which to 

extend the postage stamp, e.g., a value of 

sqrt(sky_bg_per_pixel)/3 would be 1/3 the Poisson noise 

per pixel from the sky background. 

large_object_sb_level: float 

Surface brightness level to use for large/bright objects that 

would otherwise yield stamps with more than Nmax**2 pixels. 

Nmax: int [1400] 

The largest stamp size to consider at the nominal keep_sb_level. 

1400**2*72*8/1024**3 = 1GB. 

pixel_scale: float [0.2] 

The CCD pixel scale in arcsec. 

 

Returns 

------- 

galsim.BoundsI: The postage stamp bounds. 

 

""" 

745 ↛ 764line 745 didn't jump to line 764, because the condition on line 745 was never false if gsObject.galSimType.lower() == "pointsource": 

# For bright stars, set the folding threshold for the 

# stamp size calculation. Use a 

# Kolmogorov_and_Gaussian_PSF since it is faster to 

# evaluate than an AtmosphericPSF. 

folding_threshold = self.sky_bg_per_pixel/flux 

if folding_threshold >= self._ft_default: 

gsparams = None 

else: 

gsparams = galsim.GSParams(folding_threshold=folding_threshold) 

psf = Kolmogorov_and_Gaussian_PSF(airmass=self._airmass, 

rawSeeing=self._rawSeeing, 

band=self._band, 

gsparams=gsparams) 

obj = self.drawPointSource(gsObject, psf=psf) 

image_size = obj.getGoodImageSize(pixel_scale) 

else: 

# For extended objects, recreate the object to draw, but 

# convolved with the faster DoubleGaussian PSF. 

obj = self.createCenteredObject(gsObject, 

psf=self._double_gaussian_psf) 

obj = obj.withFlux(flux) 

image_size = getGoodPhotImageSize(obj, keep_sb_level, 

pixel_scale=pixel_scale) 

if image_size > Nmax: 

image_size = getGoodPhotImageSize(obj, large_object_sb_level, 

pixel_scale=pixel_scale) 

image_size = max(image_size, Nmax) 

 

# Create the bounds object centered on the desired location. 

xmin = int(math.floor(image_pos.x) - image_size/2) 

xmax = int(math.ceil(image_pos.x) + image_size/2) 

ymin = int(math.floor(image_pos.y) - image_size/2) 

ymax = int(math.ceil(image_pos.y) + image_size/2) 

 

return galsim.BoundsI(xmin, xmax, ymin, ymax) 

 

 

def getGoodPhotImageSize(obj, keep_sb_level, pixel_scale=0.2): 

""" 

Get a postage stamp size (appropriate for photon-shooting) given a 

minimum surface brightness in photons/pixel out to which to 

extend the stamp region. 

 

Parameters 

---------- 

obj: galsim.GSObject 

The GalSim object for which we will call .drawImage. 

keep_sb_level: float 

The minimum surface brightness (photons/pixel) out to which to 

extend the postage stamp, e.g., a value of 

sqrt(sky_bg_per_pixel)/3 would be 1/3 the Poisson noise 

per pixel from the sky background. 

pixel_scale: float [0.2] 

The CCD pixel scale in arcsec. 

 

Returns 

------- 

int: The length N of the desired NxN postage stamp. 

 

Notes 

----- 

Use of this function should be avoided with PSF implementations that 

are costly to evaluate. A roughly equivalent DoubleGaussian 

could be used as a proxy. 

 

This function was originally written by Mike Jarvis. 

""" 

# The factor by which to adjust N in each step. 

factor = 1.1 

 

# Start with the normal image size from GalSim 

N = obj.getGoodImageSize(pixel_scale) 

#print('N = ',N) 

 

# This can be too small for bright stars, so increase it in steps until the edges are 

# all below the requested sb level. 

# (Don't go bigger than 4096) 

Nmax = 4096 

824 ↛ 826line 824 didn't jump to line 826, because the condition on line 824 was never true while N < Nmax: 

# Check the edges and corners of the current square 

h = N / 2 * pixel_scale 

xvalues = [ obj.xValue(h,0), obj.xValue(-h,0), 

obj.xValue(0,h), obj.xValue(0,-h), 

obj.xValue(h,h), obj.xValue(h,-h), 

obj.xValue(-h,h), obj.xValue(-h,-h) ] 

maxval = np.max(xvalues) 

#print(N, maxval) 

if maxval < keep_sb_level: 

break 

N *= factor 

 

N = min(N, Nmax) 

 

# This can be quite huge for Devauc profiles, but we don't actually have much 

# surface brightness way out in the wings. So cut it back some. 

# (Don't go below 64 though.) 

while N >= 64 * factor: 

# Check the edges and corners of a square smaller by a factor of N. 

h = N / (2 * factor) * pixel_scale 

xvalues = [ obj.xValue(h,0), obj.xValue(-h,0), 

obj.xValue(0,h), obj.xValue(0,-h), 

obj.xValue(h,h), obj.xValue(h,-h), 

obj.xValue(-h,h), obj.xValue(-h,-h) ] 

maxval = np.max(xvalues) 

#print(N, maxval) 

if maxval > keep_sb_level: 

break 

N /= factor 

 

return int(N)