Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

# This file is part of ip_isr. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import numpy as np 

import tempfile 

 

import lsst.afw.image as afwImage 

import lsst.afw.geom as afwGeom 

import lsst.afw.cameraGeom.utils as afwUtils 

import lsst.afw.cameraGeom.testUtils as afwTestUtils 

from lsst.meas.algorithms import Defect 

import lsst.pex.config as pexConfig 

import lsst.pipe.base as pipeBase 

 

 

__all__ = ["IsrMockConfig", "IsrMock", "RawMock", "TrimmedRawMock", "RawDictMock", 

"CalibratedRawMock", "MasterMock", 

"BiasMock", "DarkMock", "FlatMock", "FringeMock", "UntrimmedFringeMock", 

"BfKernelMock", "DefectMock", "CrosstalkCoeffMock", "TransmissionMock", 

"DataRefMock"] 

 

 

class IsrMockConfig(pexConfig.Config): 

r"""Configuration parameters for isrMock. 

 

These parameters produce generic fixed position signals from 

various sources, and combine them in a way that matches how those 

signals are combined to create real data. The camera used is the 

test camera defined by the afwUtils code. 

""" 

# Detector parameters. "Exposure" parameters. 

isLsstLike = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="If True, products have one raw image per amplifier, otherwise, one raw image per detector.", 

) 

isTrimmed = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="If True, amplifiers have been trimmed and mosaicked to remove regions outside the data BBox.", 

) 

detectorIndex = pexConfig.Field( 

dtype=int, 

default=20, 

doc="Index for the detector to use. The default value uses a standard 2x4 array of amps.", 

) 

rngSeed = pexConfig.Field( 

dtype=int, 

default=20000913, 

doc="Seed for random number generator used to add noise.", 

) 

# TODO: DM-18345 Check that mocks scale correctly when gain != 1.0 

gain = pexConfig.Field( 

dtype=float, 

default=1.0, 

doc="Gain for simulated data in e^-/DN.", 

) 

readNoise = pexConfig.Field( 

dtype=float, 

default=5.0, 

doc="Read noise of the detector in e-.", 

) 

expTime = pexConfig.Field( 

dtype=float, 

default=5.0, 

doc="Exposure time for simulated data.", 

) 

 

# Signal parameters 

skyLevel = pexConfig.Field( 

dtype=float, 

default=1000.0, 

doc="Background contribution to be generated from 'the sky' in DN.", 

) 

sourceFlux = pexConfig.ListField( 

dtype=float, 

default=[45000.0], 

doc="Peak flux level (in DN) of simulated 'astronomical sources'.", 

) 

sourceAmp = pexConfig.ListField( 

dtype=int, 

default=[0], 

doc="Amplifier to place simulated 'astronomical sources'.", 

) 

sourceX = pexConfig.ListField( 

dtype=float, 

default=[50.0], 

doc="Peak position (in amplifier coordinates) of simulated 'astronomical sources'.", 

) 

sourceY = pexConfig.ListField( 

dtype=float, 

default=[25.0], 

doc="Peak position (in amplifier coordinates) of simulated 'astronomical sources'.", 

) 

overscanScale = pexConfig.Field( 

dtype=float, 

default=100.0, 

doc="Amplitude (in DN) of the ramp function to add to overscan data.", 

) 

biasLevel = pexConfig.Field( 

dtype=float, 

default=8000.0, 

doc="Background contribution to be generated from the bias offset in DN.", 

) 

darkRate = pexConfig.Field( 

dtype=float, 

default=5.0, 

doc="Background level contribution (in e-/s) to be generated from dark current.", 

) 

darkTime = pexConfig.Field( 

dtype=float, 

default=5.0, 

doc="Exposure time for the dark current contribution.", 

) 

flatDrop = pexConfig.Field( 

dtype=float, 

default=0.1, 

doc="Fractional flux drop due to flat from center to edge of detector along x-axis.", 

) 

fringeScale = pexConfig.Field( 

dtype=float, 

default=200.0, 

doc="Peak flux for the fringe ripple in DN.", 

) 

 

# Inclusion parameters 

doAddSky = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Apply 'sky' signal to output image.", 

) 

doAddSource = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Add simulated source to output image.", 

) 

doAddCrosstalk = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Apply simulated crosstalk to output image. This cannot be corrected by ISR, " 

"as detector.hasCrosstalk()==False.", 

) 

doAddOverscan = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="If untrimmed, add overscan ramp to overscan and data regions.", 

) 

doAddBias = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Add bias signal to data.", 

) 

doAddDark = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Add dark signal to data.", 

) 

doAddFlat = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Add flat signal to data.", 

) 

doAddFringe = pexConfig.Field( 

dtype=bool, 

default=True, 

doc="Add fringe signal to data.", 

) 

 

# Datasets to create and return instead of generating an image. 

doTransmissionCurve = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a simulated transmission curve.", 

) 

doDefects = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a simulated defect list.", 

) 

doBrighterFatter = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a simulated brighter-fatter kernel.", 

) 

doCrosstalkCoeffs = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return the matrix of crosstalk coefficients.", 

) 

doDataRef = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a simulated gen2 butler dataRef.", 

) 

doGenerateImage = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return the generated output image if True.", 

) 

doGenerateData = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a non-image data structure if True.", 

) 

doGenerateAmpDict = pexConfig.Field( 

dtype=bool, 

default=False, 

doc="Return a dict of exposure amplifiers instead of an afwImage.Exposure.", 

) 

 

 

class IsrMock(pipeBase.Task): 

r"""Class to generate consistent mock images for ISR testing. 

 

ISR testing currently relies on one-off fake images that do not 

accurately mimic the full set of detector effects. This class 

uses the test camera/detector/amplifier structure defined in 

`lsst.afw.cameraGeom.testUtils` to avoid making the test data 

dependent on any of the actual obs package formats. 

""" 

ConfigClass = IsrMockConfig 

_DefaultName = "isrMock" 

 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.rng = np.random.RandomState(self.config.rngSeed) 

self.crosstalkCoeffs = np.array([[0.0, 0.0, 0.0, 0.0, 0.0, -1e-3, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 0.0, 0.0, 2.2e-2, 0.0, 0.0, 0.0, 0.0], 

[1e-2, 5e-3, 5e-4, 3e-3, 4e-2, 5e-3, 5e-3, 0.0]]) 

 

self.bfKernel = np.array([[1., 4., 7., 4., 1.], 

[4., 16., 26., 16., 4.], 

[7., 26., 41., 26., 7.], 

[4., 16., 26., 16., 4.], 

[1., 4., 7., 4., 1.]]) / 273.0 

 

def run(self): 

r"""Generate a mock ISR product, and return it. 

 

Returns 

------- 

image : `lsst.afw.image.Exposure` 

Simulated ISR image with signals added. 

dataProduct : 

Simulated ISR data products. 

None : 

Returned if no valid configuration was found. 

 

Raises 

------ 

RuntimeError 

Raised if both doGenerateImage and doGenerateData are specified. 

""" 

if self.config.doGenerateImage and self.config.doGenerateData: 

raise RuntimeError("Only one of doGenerateImage and doGenerateData may be specified.") 

elif self.config.doGenerateImage: 

return self.makeImage() 

elif self.config.doGenerateData: 

return self.makeData() 

else: 

return None 

 

def makeData(self): 

r"""Generate simulated ISR data. 

 

Currently, only the class defined crosstalk coefficient 

matrix, brighter-fatter kernel, a constant unity transmission 

curve, or a simple single-entry defect list can be generated. 

 

Returns 

------- 

dataProduct : 

Simulated ISR data product. 

""" 

if sum(map(bool, [self.config.doBrighterFatter, 

self.config.doDefects, 

self.config.doTransmissionCurve, 

self.config.doCrosstalkCoeffs])) != 1: 

raise RuntimeError("Only one data product can be generated at a time.") 

elif self.config.doBrighterFatter is True: 

return self.makeBfKernel() 

elif self.config.doDefects is True: 

return self.makeDefectList() 

elif self.config.doTransmissionCurve is True: 

return self.makeTransmissionCurve() 

elif self.config.doCrosstalkCoeffs is True: 

return self.crosstalkCoeffs 

else: 

return None 

 

def makeBfKernel(self): 

r"""Generate a simple Gaussian brighter-fatter kernel. 

 

Returns 

------- 

kernel : `numpy.ndarray` 

Simulated brighter-fatter kernel. 

""" 

return self.bfKernel 

 

def makeDefectList(self): 

r"""Generate a simple single-entry defect list. 

 

Returns 

------- 

defectList : `list` of `Defects` 

Simulated defect list 

""" 

defectList = [] 

bbox = afwGeom.BoxI(afwGeom.PointI(0, 0), 

afwGeom.ExtentI(40, 50)) 

defectList.append(Defect(bbox)) 

return defectList 

 

def makeCrosstalkCoeff(self): 

r"""Generate the simulated crosstalk coefficients. 

 

Returns 

------- 

coeffs : `numpy.ndarray` 

Simulated crosstalk coefficients. 

""" 

 

return self.crosstalkCoeffs 

 

def makeTransmissionCurve(self): 

r"""Generate a simulated flat transmission curve. 

 

Returns 

------- 

transmission : `lsst.afw.image.TransmissionCurve` 

Simulated transmission curve. 

""" 

 

return afwImage.TransmissionCurve.makeIdentity() 

 

def makeImage(self): 

r"""Generate a simulated ISR image. 

 

Returns 

------- 

exposure : `lsst.afw.image.Exposure` or `dict` 

Simulated ISR image data. 

 

Notes 

----- 

This method currently constructs a "raw" data image by: 

* Generating a simulated sky with noise 

* Adding a single Gaussian "star" 

* Adding the fringe signal 

* Multiplying the frame by the simulated flat 

* Adding dark current (and noise) 

* Adding a bias offset (and noise) 

* Adding an overscan gradient parallel to the pixel y-axis 

* Simulating crosstalk by adding a scaled version of each 

amplifier to each other amplifier. 

 

The exposure with image data constructed this way is in one of 

three formats. 

* A single image, with overscan and prescan regions retained 

* A single image, with overscan and prescan regions trimmed 

* A `dict`, containing the amplifer data indexed by the 

amplifier name. 

 

The nonlinearity, CTE, and brighter fatter are currently not 

implemented. 

 

Note that this method generates an image in the reverse 

direction as the ISR processing, as the output image here has 

had a series of instrument effects added to an idealized 

exposure. 

""" 

exposure = self.getExposure() 

 

for idx, amp in enumerate(exposure.getDetector()): 

bbox = None 

if self.config.isTrimmed is True: 

bbox = amp.getBBox() 

else: 

bbox = amp.getRawDataBBox() 

 

ampData = exposure.image[bbox] 

 

if self.config.doAddSky is True: 

self.amplifierAddNoise(ampData, self.config.skyLevel, np.sqrt(self.config.skyLevel)) 

 

if self.config.doAddSource is True: 

for sourceAmp, sourceFlux, sourceX, sourceY in zip(self.config.sourceAmp, 

self.config.sourceFlux, 

self.config.sourceX, 

self.config.sourceY): 

if idx == sourceAmp: 

self.amplifierAddSource(ampData, sourceFlux, sourceX, sourceY) 

 

if self.config.doAddFringe is True: 

self.amplifierAddFringe(amp, ampData, self.config.fringeScale) 

 

if self.config.doAddFlat is True: 

if ampData.getArray().sum() == 0.0: 

self.amplifierAddNoise(ampData, 1.0, 0.0) 

u0 = exposure.getDimensions().getX() 

v0 = exposure.getDimensions().getY() 

self.amplifierMultiplyFlat(amp, ampData, self.config.flatDrop, u0=u0, v0=v0) 

 

if self.config.doAddDark is True: 

self.amplifierAddNoise(ampData, 

self.config.darkRate * self.config.darkTime / self.config.gain, 

np.sqrt(self.config.darkRate * 

self.config.darkTime / self.config.gain)) 

 

if self.config.doAddCrosstalk is True: 

for idxS, ampS in enumerate(exposure.getDetector()): 

for idxT, ampT in enumerate(exposure.getDetector()): 

if self.config.isTrimmed is True: 

ampDataS = exposure.image[ampS.getBBox()] 

ampDataT = exposure.image[ampT.getBBox()] 

else: 

ampDataS = exposure.image[ampS.getRawDataBBox()] 

ampDataT = exposure.image[ampT.getRawDataBBox()] 

self.amplifierAddCT(ampDataS, ampDataT, self.crosstalkCoeffs[idxT][idxS]) 

 

for amp in exposure.getDetector(): 

bbox = None 

if self.config.isTrimmed is True: 

bbox = amp.getBBox() 

else: 

bbox = amp.getRawDataBBox() 

 

ampData = exposure.image[bbox] 

 

if self.config.doAddBias is True: 

self.amplifierAddNoise(ampData, self.config.biasLevel, 

self.config.readNoise / self.config.gain) 

 

if self.config.doAddOverscan is True: 

oscanBBox = amp.getRawHorizontalOverscanBBox() 

oscanData = exposure.image[oscanBBox] 

self.amplifierAddNoise(oscanData, self.config.biasLevel, 

self.config.readNoise / self.config.gain) 

 

self.amplifierAddYGradient(ampData, -1.0 * self.config.overscanScale, 

1.0 * self.config.overscanScale) 

self.amplifierAddYGradient(oscanData, -1.0 * self.config.overscanScale, 

1.0 * self.config.overscanScale) 

 

if self.config.doGenerateAmpDict is True: 

expDict = dict() 

for amp in exposure.getDetector(): 

expDict[amp.getName()] = exposure 

return expDict 

else: 

return exposure 

 

# afw primatives to construct the image structure 

def getCamera(self): 

r"""Construct a test camera object. 

 

Returns 

------- 

camera : `lsst.afw.cameraGeom.camera` 

Test camera. 

""" 

cameraWrapper = afwTestUtils.CameraWrapper(self.config.isLsstLike) 

camera = cameraWrapper.camera 

return camera 

 

def getExposure(self): 

r"""Construct a test exposure. 

 

The test exposure has a simple WCS set, as well as a list of 

unlikely header keywords that can be removed during ISR 

processing to exercise that code. 

 

Returns 

------- 

exposure : `lsst.afw.exposure.Exposure` 

Construct exposure containing masked image of the 

appropriate size. 

""" 

camera = self.getCamera() 

detector = camera[self.config.detectorIndex] 

image = afwUtils.makeImageFromCcd(detector, 

isTrimmed=self.config.isTrimmed, 

showAmpGain=False, 

rcMarkSize=0, 

binSize=1, 

imageFactory=afwImage.ImageF) 

 

var = afwImage.ImageF(image.getDimensions()) 

mask = afwImage.Mask(image.getDimensions()) 

image.assign(0.0) 

 

maskedImage = afwImage.makeMaskedImage(image, mask, var) 

exposure = afwImage.makeExposure(maskedImage) 

exposure.setDetector(detector) 

exposure.setWcs(self.getWcs()) 

 

visitInfo = afwImage.VisitInfo(exposureTime=self.config.expTime, darkTime=self.config.darkTime) 

exposure.getInfo().setVisitInfo(visitInfo) 

 

metadata = exposure.getMetadata() 

metadata.add("SHEEP", 7.3, "number of sheep on farm") 

metadata.add("MONKEYS", 155, "monkeys per tree") 

metadata.add("VAMPIRES", 4, "How scary are vampires.") 

 

for amp in exposure.getDetector(): 

amp.setLinearityCoeffs((0., 1., 0., 0.)) 

amp.setLinearityType("Polynomial") 

amp.setGain(self.config.gain) 

 

exposure.image.array[:] = np.zeros(exposure.getImage().getDimensions()).transpose() 

exposure.mask.array[:] = np.zeros(exposure.getMask().getDimensions()).transpose() 

exposure.variance.array[:] = np.zeros(exposure.getVariance().getDimensions()).transpose() 

 

return exposure 

 

def getWcs(self): 

r"""Construct a dummy WCS object. 

 

Taken from the deprecated ip_isr/examples/exampleUtils.py. 

 

This is not guaranteed, given the distortion and pixel scale 

listed in the afwTestUtils camera definition. 

 

Returns 

------- 

wcs : `lsst.afw.geom.SkyWcs` 

Test WCS transform. 

""" 

return afwGeom.makeSkyWcs(crpix=afwGeom.Point2D(0.0, 100.0), 

crval=afwGeom.SpherePoint(45.0, 25.0, afwGeom.degrees), 

cdMatrix=afwGeom.makeCdMatrix(scale=1.0*afwGeom.degrees)) 

 

def localCoordToExpCoord(self, ampData, x, y): 

r"""Convert between a local amplifier coordinate and the full 

exposure coordinate. 

 

Parameters 

---------- 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to use for conversions. 

x : `int` 

X-coordinate of the point to transform. 

y : `int` 

Y-coordinate of the point to transform. 

 

Returns 

------- 

u : `int` 

Transformed x-coordinate. 

v : `int` 

Transformed y-coordinate. 

 

Notes 

----- 

The output is transposed intentionally here, to match the 

internal transpose between numpy and afw.image coordinates. 

""" 

u = x + ampData.getBBox().getBeginX() 

v = y + ampData.getBBox().getBeginY() 

 

return (v, u) 

 

# Simple data values. 

def amplifierAddNoise(self, ampData, mean, sigma): 

r"""Add Gaussian noise to an amplifier's image data. 

 

This method operates in the amplifier coordinate frame. 

 

Parameters 

---------- 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to operate on. 

mean : `float` 

Mean value of the Gaussian noise. 

sigma : `float` 

Sigma of the Gaussian noise. 

""" 

ampArr = ampData.array 

ampArr[:] = ampArr[:] + self.rng.normal(mean, sigma, 

size=ampData.getDimensions()).transpose() 

 

def amplifierAddYGradient(self, ampData, start, end): 

r"""Add a y-axis linear gradient to an amplifier's image data. 

 

This method operates in the amplifier coordinate frame. 

 

Parameters 

---------- 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to operate on. 

start : `float` 

Start value of the gradient (at y=0). 

end : `float` 

End value of the gradient (at y=ymax). 

""" 

nPixY = ampData.getDimensions().getY() 

ampArr = ampData.array 

ampArr[:] = ampArr[:] + (np.interp(range(nPixY), (0, nPixY - 1), (start, end)).reshape(nPixY, 1) + 

np.zeros(ampData.getDimensions()).transpose()) 

 

def amplifierAddSource(self, ampData, scale, x0, y0): 

r"""Add a single Gaussian source to an amplifier. 

 

This method operates in the amplifier coordinate frame. 

 

Parameters 

---------- 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to operate on. 

scale : `float` 

Peak flux of the source to add. 

x0 : `float` 

X-coordinate of the source peak. 

y0 : `float` 

Y-coordinate of the source peak. 

""" 

for x in range(0, ampData.getDimensions().getX()): 

for y in range(0, ampData.getDimensions().getY()): 

ampData.array[y][x] = (ampData.array[y][x] + 

scale * np.exp(-0.5 * ((x - x0)**2 + (y - y0)**2) / 3.0**2)) 

 

def amplifierAddCT(self, ampDataSource, ampDataTarget, scale): 

r"""Add a scaled copy of an amplifier to another, simulating crosstalk. 

 

This method operates in the amplifier coordinate frame. 

 

Parameters 

---------- 

ampDataSource : `lsst.afw.image.ImageF` 

Amplifier image to add scaled copy from. 

ampDataTarget : `lsst.afw.image.ImageF` 

Amplifier image to add scaled copy to. 

scale : `float` 

Flux scale of the copy to add to the target. 

 

Notes 

----- 

This simulates simple crosstalk between amplifiers. 

""" 

ampDataTarget.array[:] = (ampDataTarget.array[:] + 

scale * ampDataSource.array[:]) 

 

# Functional form data values 

def amplifierAddFringe(self, amp, ampData, scale): 

r"""Add a fringe-like ripple pattern to an amplifier's image data. 

 

Parameters 

---------- 

amp : `lsst.afw.ampInfo.AmpInfoRecord` 

Amplifier to operate on. Needed for amp<->exp coordinate transforms. 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to operate on. 

scale : `float` 

Peak intensity scaling for the ripple. 

 

Notes 

----- 

This uses an offset sinc function to generate a ripple 

pattern. True fringes have much finer structure, but this 

pattern should be visually identifiable. The (x, y) 

coordinates are in the frame of the amplifier, and (u, v) in 

the frame of the full trimmed image. 

""" 

for x in range(0, ampData.getDimensions().getX()): 

for y in range(0, ampData.getDimensions().getY()): 

(u, v) = self.localCoordToExpCoord(amp, x, y) 

ampData.array[y][x] = (ampData.array[y][x] + 

scale * np.sinc(((u - 100)/150)**2 + (v / 150)**2)) 

 

def amplifierMultiplyFlat(self, amp, ampData, fracDrop, u0=100.0, v0=100.0): 

r"""Multiply an amplifier's image data by a flat-like pattern. 

 

Parameters 

---------- 

amp : `lsst.afw.ampInfo.AmpInfoRecord` 

Amplifier to operate on. Needed for amp<->exp coordinate transforms. 

ampData : `lsst.afw.image.ImageF` 

Amplifier image to operate on. 

fracDrop : `float` 

Fractional drop from center to edge of detector along x-axis. 

u0 : `float` 

Peak location in detector coordinates. 

v0 : `float` 

Peak location in detector coordinates. 

 

Notes 

----- 

This uses a 2-d Gaussian to simulate an illumination pattern 

that falls off towards the edge of the detector. The (x, y) 

coordinates are in the frame of the amplifier, and (u, v) in 

the frame of the full trimmed image. 

""" 

if fracDrop >= 1.0: 

raise RuntimeError("Flat fractional drop cannot be greater than 1.0") 

 

sigma = u0 / np.sqrt(-2.0 * np.log(fracDrop)) 

 

for x in range(0, ampData.getDimensions().getX()): 

for y in range(0, ampData.getDimensions().getY()): 

(u, v) = self.localCoordToExpCoord(amp, x, y) 

f = np.exp(-0.5 * ((u - u0)**2 + (v - v0)**2) / sigma**2) 

ampData.array[y][x] = (ampData.array[y][x] * f) 

 

 

class RawMock(IsrMock): 

r"""Generate a raw exposure suitable for ISR. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.isTrimmed = False 

self.config.doGenerateImage = True 

self.config.doGenerateAmpDict = False 

self.config.doAddOverscan = True 

self.config.doAddSky = True 

self.config.doAddSource = True 

self.config.doAddCrosstalk = False 

self.config.doAddBias = True 

self.config.doAddDark = True 

 

 

class TrimmedRawMock(RawMock): 

r"""Generate a trimmed raw exposure. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.isTrimmed = True 

self.config.doAddOverscan = False 

 

 

class CalibratedRawMock(RawMock): 

r"""Generate a trimmed raw exposure. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.isTrimmed = True 

self.config.doAddOverscan = False 

self.config.doAddBias = True 

self.config.doAddDark = False 

self.config.doAddFlat = False 

self.config.doAddFringe = False 

self.config.doAddCrosstalk = True 

self.config.biasLevel = 0.0 

self.config.readNoise = 10.0 

 

 

class RawDictMock(RawMock): 

r"""Generate a raw exposure dict suitable for ISR. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doGenerateAmpDict = True 

 

 

class MasterMock(IsrMock): 

r"""Parent class for those that make master calibrations. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.isTrimmed = True 

self.config.doGenerateImage = True 

self.config.doAddOverscan = False 

self.config.doAddSky = False 

self.config.doAddSource = False 

self.config.doAddCrosstalk = False 

 

self.config.doAddBias = False 

self.config.doAddDark = False 

self.config.doAddFlat = False 

self.config.doAddFringe = False 

 

 

class BiasMock(MasterMock): 

r"""Simulated master bias calibration. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doAddBias = True 

self.config.readNoise = 10.0 

 

 

class DarkMock(MasterMock): 

r"""Simulated master dark calibration. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doAddDark = True 

self.config.darkTime = 1.0 

 

 

class FlatMock(MasterMock): 

r"""Simulated master flat calibration. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doAddFlat = True 

 

 

class FringeMock(MasterMock): 

r"""Simulated master fringe calibration. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doAddFringe = True 

 

 

class UntrimmedFringeMock(FringeMock): 

r"""Simulated untrimmed master fringe calibration. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.isTrimmed = False 

 

 

class BfKernelMock(IsrMock): 

r"""Simulated brighter-fatter kernel. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doGenerateImage = False 

self.config.doGenerateData = True 

self.config.doBrighterFatter = True 

self.config.doDefects = False 

self.config.doCrosstalkCoeffs = False 

self.config.doTransmissionCurve = False 

 

 

class DefectMock(IsrMock): 

r"""Simulated defect list. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doGenerateImage = False 

self.config.doGenerateData = True 

self.config.doBrighterFatter = False 

self.config.doDefects = True 

self.config.doCrosstalkCoeffs = False 

self.config.doTransmissionCurve = False 

 

 

class CrosstalkCoeffMock(IsrMock): 

r"""Simulated crosstalk coefficient matrix. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doGenerateImage = False 

self.config.doGenerateData = True 

self.config.doBrighterFatter = False 

self.config.doDefects = False 

self.config.doCrosstalkCoeffs = True 

self.config.doTransmissionCurve = False 

 

 

class TransmissionMock(IsrMock): 

r"""Simulated transmission curve. 

""" 

def __init__(self, **kwargs): 

super().__init__(**kwargs) 

self.config.doGenerateImage = False 

self.config.doGenerateData = True 

self.config.doBrighterFatter = False 

self.config.doDefects = False 

self.config.doCrosstalkCoeffs = False 

self.config.doTransmissionCurve = True 

 

 

class DataRefMock(object): 

r"""Simulated gen2 butler data ref. 

 

Currently only supports get and put operations, which are most 

likely to be called for data in ISR processing. 

 

""" 

dataId = "isrMock Fake Data" 

darkval = 2. # e-/sec 

oscan = 250. # DN 

gradient = .10 

exptime = 15.0 # seconds 

darkexptime = 15.0 # seconds 

 

def __init__(self, **kwargs): 

if 'config' in kwargs.keys(): 

self.config = kwargs['config'] 

else: 

self.config = None 

 

def expectImage(self): 

if self.config is None: 

self.config = IsrMockConfig() 

self.config.doGenerateImage = True 

self.config.doGenerateData = False 

 

def expectData(self): 

if self.config is None: 

self.config = IsrMockConfig() 

self.config.doGenerateImage = False 

self.config.doGenerateData = True 

 

def get(self, dataType, **kwargs): 

r"""Return an appropriate data product. 

 

Parameters 

---------- 

dataType : `str` 

Type of data product to return. 

 

Returns 

------- 

mock : IsrMock.run() result 

The output product. 

""" 

if "_filename" in dataType: 

self.expectData() 

return tempfile.mktemp(), "mock" 

elif 'transmission_' in dataType: 

self.expectData() 

return TransmissionMock(config=self.config).run() 

elif dataType == 'ccdExposureId': 

self.expectData() 

return 20090913 

elif dataType == 'camera': 

self.expectData() 

return IsrMock(config=self.config).getCamera() 

elif dataType == 'raw': 

self.expectImage() 

return RawMock(config=self.config).run() 

elif dataType == 'bias': 

self.expectImage() 

return BiasMock(config=self.config).run() 

elif dataType == 'dark': 

self.expectImage() 

return DarkMock(config=self.config).run() 

elif dataType == 'flat': 

self.expectImage() 

return FlatMock(config=self.config).run() 

elif dataType == 'fringe': 

self.expectImage() 

return FringeMock(config=self.config).run() 

elif dataType == 'defects': 

self.expectData() 

return DefectMock(config=self.config).run() 

elif dataType == 'bfKernel': 

self.expectData() 

return BfKernelMock(config=self.config).run() 

elif dataType == 'linearizer': 

return None 

elif dataType == 'crosstalkSources': 

return None 

else: 

raise RuntimeError("ISR DataRefMock cannot return %s.", dataType) 

 

def put(self, exposure, filename): 

r"""Write an exposure to a FITS file. 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Image data to write out. 

filename : `str` 

Base name of the output file. 

""" 

exposure.writeFits(filename+".fits")