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

# This file is part of afw. 

# 

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

 

__all__ = ['getByKey', 'setByKey', 'HeaderMap', 'HeaderAmpMap', 

'HeaderDetectorMap', 'DetectorBuilder'] 

 

import re 

import warnings 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.table as afwTable 

from lsst.afw.fits import readMetadata 

import lsst.afw.cameraGeom as afwCameraGeom 

import lsst.afw.geom 

 

 

def getByKey(metadata, key): 

"""Wrapper for getting a value from a metadata object by key. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

Metadata object to query for value. 

key : `str` 

Key to use for value lookup. 

 

Returns 

------- 

value : `object` 

Value associated with key, None if key does not exist. 

""" 

mdKeys = metadata.paramNames() 

if key in mdKeys: 

return metadata.getScalar(key) 

else: 

return None 

 

 

def setByKey(metadata, key, value, clobber): 

"""Wrapper for setting a value in a metadata object. Deals with case 

where the key already exists. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

Metadata object ot modify in place. 

key : `str` 

Key to associate with value. 

value : `object` 

Value to assign in the metadata object. 

clobber : `bool` 

Clobber the value if the key already exists? 

""" 

mdKeys = metadata.paramNames() 

if key not in mdKeys or (key in mdKeys and clobber): 

metadata.set(key, value) 

 

 

class HeaderMap(dict): 

""" Class to hold mapping of header cards to attributes""" 

 

81 ↛ exitline 81 didn't run the lambda on line 81 def addEntry(self, keyname, attribute_name, default=None, transform=lambda x: x): 

"""Adds an entry to the registry. 

 

Parameters 

---------- 

keyname : `str` 

Key used to retrieve the header record. 

attribute_name : `str` 

Name of the attribute to store the value in. 

default : `object` 

Default value to store if the header card is not available. 

transform : `callable` 

Transform to apply to the header value before assigning it to the 

attribute. 

""" 

self.__setitem__(attribute_name, {'keyName': keyname, 

'default': default, 

'transform': transform}) 

 

def setAttributes(self, obj, metadata, doRaise=True): 

"""Sets the attributes on the given object given a metadata object. 

 

Parameters 

---------- 

obj : `object` 

Object on which to operate in place. 

metadata : `lsst.daf.base.PropertySet` 

Metadata object used for applying the mapping. 

doRaise : `bool` 

Raise exceptions on calling methods on the input object that 

do not exist? 

""" 

for key, attrDict in self.items(): 

try: 

value = getByKey(metadata, attrDict['keyName']) 

# if attrDict['keyName'] == "RDCRNR" and value == 0: 

# import ipdb; ipdb.set_trace() 

if value is not None: 

self._applyVal(obj, value, key, attrDict['transform']) 

else: 

# Only apply transform if the metadata has a value for this key 

# otherwise assume the default value is transformed. 

value = attrDict['default'] 

if value is not None: 

self._applyVal(obj, value, key, lambda x: x) 

except Exception as e: 

if doRaise: 

raise 

else: 

warnings.warn('WARNING: Failed to set %s attribute with %s value: %s' % 

(key, value, str(e))) 

 

def _applyVal(self, obj, value, attrName, transform): 

raise NotImplementedError('Must be implemented in sub-class') 

 

 

class HeaderAmpMap(HeaderMap): 

""" Class to hold mapping of header cards to AmpInfoTable attributes 

The amp info is stored using setters, thus calling the attribute as a function. 

""" 

 

def _applyVal(self, obj, value, attrName, transform): 

getattr(obj, attrName)(transform(value)) 

 

 

class HeaderDetectorMap(HeaderMap): 

""" Class to hold mapping of header cards to Detector attributes 

Detector information is stored as attributes on a Config object. 

""" 

 

def _applyVal(self, obj, value, attrName, transform): 

obj.__setattr__(attrName, transform(value)) 

 

 

class DetectorBuilder: 

""" 

Parameters 

---------- 

detectorFileName : `str` 

FITS file containing the detector description. 

May use [] notation to specify an extension in an MEF. 

ampFileNameList : `list` of `str` 

List of FITS file names to use in building the amps. 

May contain duplicate entries if the raw data are assembled. 

inAmpCoords : `bool` 

True if raw data are in amp coordinates, False if raw data 

are assembled into pseudo detector pixel arrays. 

plateScale : `float` 

Nominal platescale (arcsec/mm). 

radialCoeffs : `iterable` of `float` 

Radial distortion coefficients for a radial polynomial in 

normalized units. 

clobberMetadata : `bool` 

Clobber metadata from input files if overridden in 

_sanitizeMetadata(). 

doRaise : `bool` 

Raise exception if not all non-defaulted keywords are defined? 

""" 

def __init__(self, detectorFileName, ampFileNameList, inAmpCoords=True, plateScale=1., 

radialCoeffs=(0., 1.), clobberMetadata=False, doRaise=True): 

self.inAmpCoords = inAmpCoords 

self.defaultAmpMap = self._makeDefaultAmpMap() 

self.defaultDetectorMap = self._makeDefaultDetectorMap() 

self.detectorMetadata = readMetadata(detectorFileName) 

self._sanitizeHeaderMetadata( 

self.detectorMetadata, clobber=clobberMetadata) 

self.ampMetadataList = [] 

self.detector = None 

self.doRaise = doRaise 

for fileName in ampFileNameList: 

self.ampMetadataList.append(readMetadata(fileName)) 

self._sanitizeHeaderMetadata( 

self.ampMetadataList[-1], clobber=clobberMetadata) 

self.plateScale = plateScale 

self.focalPlaneToField = self._makeRadialTransform(radialCoeffs) 

 

def _sanitizeHeaderMetadata(self, metadata, clobber): 

"""This method is called for all metadata and gives an opportunity to 

add/modify header information for use downstream. 

 

Override this method if more than the default is needed. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

Metadata to read/modify 

clobber : `bool` 

Clobber keys that exist with default keys? 

""" 

self._defaultSanitization(metadata, clobber) 

 

def _defaultSanitization(self, metadata, clobber): 

"""Does the default sanitization of the header metadata. 

 

Parameters 

---------- 

metadata : `lsst.daf.base.PropertySet` 

Header metadata to extend/modify. 

clobber : `bool` 

Override values in existing header cards? 

""" 

 

if self.inAmpCoords: 

# Deal with DTM to get flipX and flipY for assembly and add as 'FLIPX', 'FLIPY' 

# The DTM array is a transformation matrix. As I understand it, it transforms between 

# electronic and assembled coordintates. As such, a negative value in the DTM1_1 spot 

# corresponds to a flip of the x-axis and a negative value in the DTM2_2 spot 

# corresponds to a flip of the y-axis. 

dtm1 = getByKey(metadata, 'DTM1_1') 

dtm2 = getByKey(metadata, 'DTM2_2') 

if dtm1 is not None and dtm2 is not None: 

setByKey(metadata, 'FLIPX', dtm1 < 0, clobber) 

setByKey(metadata, 'FLIPY', dtm2 < 0, clobber) 

setByKey(metadata, 'RDCRNR', int( 

afwTable.ReadoutCorner.LL), clobber) 

else: 

setByKey(metadata, 'FLIPX', False, clobber) 

setByKey(metadata, 'FLIPY', True, clobber) 

# I don't know how to figure out the read corner if already 

# assembled 

setByKey(metadata, 'RDCRNR', None, clobber) 

 

# Deal with NAXIS1, NAXIS2 to make rawBBox as 'RAWBBOX' 

xext = getByKey(metadata, 'NAXIS1') 

yext = getByKey(metadata, 'NAXIS2') 

if xext is not None and yext is not None: 

setByKey(metadata, 'RAWBBOX', '[%i:%i,%i:%i]'%( 

1, xext, 1, yext), clobber) 

# Deal with DTV1, DTV2 to make 'XYOFF 

dtv1 = getByKey(metadata, 'DTV1') 

dtv2 = getByKey(metadata, 'DTV2') 

if dtv1 is not None and dtv2 is not None: 

setByKey(metadata, 'XYOFF', [dtv1, dtv2], clobber) 

# map biassec[1] to HOSCAN 

# map biassec[3] to VOSCAN 

# map biassec[2] to PRESCAN 

if metadata.isArray('BIASSEC'): 

keylist = ['HOSCAN', 'PRESCAN', 'VOSCAN'] 

biassecs = getByKey(metadata, 'BIASSEC') 

for i, biassec in enumerate(biassecs): 

setByKey(metadata, keylist[i], biassec, clobber) 

else: 

biassec = getByKey(metadata, 'BIASSEC') 

if biassec is not None: 

setByKey(metadata, 'HOSCAN', biassec, clobber) 

 

def _makeDefaultAmpMap(self): 

"""Make the default map from header information to amplifier 

information. 

 

Returns 

------- 

headerAmMap : `HeaderAmpMap` 

The HeaderAmpMap object containing the mapping 

""" 

hMap = HeaderAmpMap() 

emptyBBox = lsst.geom.BoxI() 

mapList = [('EXTNAME', 'setName'), 

('DETSEC', 'setBBox', None, self._makeBbox), 

('GAIN', 'setGain', 1.), 

('RDNOISE', 'setReadNoise', 0.), 

('SATURATE', 'setSaturation', 2 << 15), 

('RDCRNR', 'setReadoutCorner', int( 

afwTable.ReadoutCorner.LL), afwTable.ReadoutCorner), 

('LINCOEFF', 'setLinearityCoeffs', [0., 1.]), 

('LINTYPE', 'setLinearityType', 'POLY'), 

('RAWBBOX', 'setRawBBox', None, self._makeBbox), 

('DATASEC', 'setRawDataBBox', None, self._makeBbox), 

('FLIPX', 'setRawFlipX', False), 

('FLIPY', 'setRawFlipY', False), 

('XYOFF', 'setRawXYOffset', lsst.geom.ExtentI(0, 0), self._makeExt), 

('HOSCAN', 'setRawHorizontalOverscanBBox', 

emptyBBox, self._makeBbox), 

('VOSCAN', 'setRawVerticalOverscanBBox', 

emptyBBox, self._makeBbox), 

('PRESCAN', 'setRawPrescanBBox', emptyBBox, self._makeBbox), 

] 

for tup in mapList: 

hMap.addEntry(*tup) 

return hMap 

 

def _makeDefaultDetectorMap(self): 

"""Make the default map from header information to detector information. 

 

Returns 

------- 

headerDetectorMap : `HeaderDetectorMap` 

The HeaderDetectorMap object containing the mapping. 

""" 

hMap = HeaderDetectorMap() 

mapList = [('CCDNAME', 'name', 'ccdName'), 

('DETSIZE', 'bbox_x0', 0, self._getBboxX0), 

('DETSIZE', 'bbox_y0', 0, self._getBboxY0), 

('DETSIZE', 'bbox_x1', 0, self._getBboxX1), 

('DETSIZE', 'bbox_y1', 0, self._getBboxY1), 

('DETID', 'id', 0), 

# DetectorConfig.detectorType is of type `int`, not 

# `DetectorType` 

('OBSTYPE', 'detectorType', int( 

afwCameraGeom.DetectorType.SCIENCE)), 

('SERSTR', 'serial', 'none'), 

('XPOS', 'offset_x', 0.), 

('YPOS', 'offset_y', 0.), 

('XPIX', 'refpos_x', 0.), 

('YPIX', 'refpos_y', 0.), 

('YAWDEG', 'yawDeg', 0.), 

('PITCHDEG', 'pitchDeg', 0.), 

('ROLLDEG', 'rollDeg', 0.), 

('XPIXSIZE', 'pixelSize_x', 1.), 

('YPIXSIZE', 'pixelSize_y', 1.), 

('TRNSPOSE', 'transposeDetector', False), 

] 

for tup in mapList: 

hMap.addEntry(*tup) 

return hMap 

 

def _makeExt(self, extArr): 

"""Helper function to make an extent from an array 

 

Parameters 

---------- 

extArr : `array` of `int` 

Length 2 array to use in creating the Extent object. 

 

Returns 

------- 

extent : `lsst.geom.Extent2I` 

Extent constructed from the input list. 

""" 

return lsst.geom.ExtentI(*extArr) 

 

def _makeBbox(self, boxString): 

"""Helper funtion to make a bounding box from a string representing a 

FITS style bounding box. 

 

Parameters 

---------- 

boxString : `str` 

String describing the bounding box. 

 

Returns 

------- 

bbox : `lsst.geom.Box2I` 

The bounding box. 

""" 

# strip off brackets and split into parts 

x1, x2, y1, y2 = [int(el) for el in re.split( 

'[:,]', boxString.strip()[1:-1])] 

box = lsst.geom.BoxI(lsst.geom.PointI(x1, y1), lsst.geom.PointI(x2, y2)) 

# account for the difference between FITS convention and LSST convention for 

# index of LLC. 

box.shift(lsst.geom.Extent2I(-1, -1)) 

return box 

 

def _getBboxX0(self, boxString): 

return self._makeBbox(boxString).getMinX() 

 

def _getBboxX1(self, boxString): 

return self._makeBbox(boxString).getMaxX() 

 

def _getBboxY0(self, boxString): 

return self._makeBbox(boxString).getMinY() 

 

def _getBboxY1(self, boxString): 

return self._makeBbox(boxString).getMaxY() 

 

def _makeRadialTransform(self, radialCoeffs): 

"""Helper function to get the radial transform given the radial 

polynomial coefficients given in the constructor. 

 

Parameters 

---------- 

radialCoeffs : `iterable` of `float` 

List of coefficients describing a polynomial radial distortion in 

normalized units. The first value must be 0. 

 

Returns 

------- 

transform : `lsst.afw.geom.TransformPoint2ToPoint2` 

Transform object describing the radial distortion 

""" 

pScaleRad = lsst.geom.arcsecToRad(self.plateScale) 

return lsst.afw.geom.makeRadialTransform([el/pScaleRad for el in radialCoeffs]) 

 

def buildDetector(self): 

"""Take all the information and build a Detector object. 

The Detector object is necessary for doing things like assembly. 

 

Returns 

------- 

detector : `lsst.afw.cameraGeom.Detector` 

The detector. 

""" 

if self.detector is not None: 

return self.detector 

 

schema = afwTable.AmpInfoTable.makeMinimalSchema() 

ampInfo = afwTable.AmpInfoCatalog(schema) 

for ampMetadata in self.ampMetadataList: 

record = ampInfo.addNew() 

self.defaultAmpMap.setAttributes(record, ampMetadata, self.doRaise) 

record.setHasRawInfo(True) 

 

detConfig = afwCameraGeom.DetectorConfig() 

self.defaultDetectorMap.setAttributes( 

detConfig, self.detectorMetadata, self.doRaise) 

self.detector = afwCameraGeom.makeDetector( 

detConfig, ampInfo, self.focalPlaneToField) 

return self.detector 

 

def makeCalib(self): 

"""Placeholder for subclasses to implement construction of a calib to 

associate with the exposure. 

 

Returns 

------- 

calibration : `lsst.afw.image.Calib` 

Empty calibration 

""" 

return afwImage.Calib() 

 

def makeExposure(self, im, mask=None, variance=None): 

"""Method for constructing an exposure object from an image and the 

information contained in this class to construct the Detector and 

Calib objects. 

 

Parameters 

---------- 

im : `lsst.afw.image.Image` 

Image used to construct the exposure. 

mask : `lsst.afw.image.MaskU` 

Optional mask plane. 

variance : `lsst.afw.image.Image` 

Optional variance plance as an image of the same type as im. 

 

Returns 

------- 

exposure : `lsst.afw.image.Exposure` 

Constructed exposure (specific type will match that of ``im``). 

""" 

if mask is None: 

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

if variance is None: 

variance = im 

mi = afwImage.makeMaskedImage(im, mask, variance) 

detector = self.buildDetector() 

 

calib = self.makeCalib() 

exp = afwImage.makeExposure(mi) 

exp.setCalib(calib) 

exp.setDetector(detector) 

return exp