1 from __future__
import absolute_import, division, print_function
2 from builtins
import object
13 """Wrapper for getting a value from a metadata object by key. 14 @param[in] metadata metadata object to query for value 15 @param[in] key key to use for value lookup 16 @returns value associated with key, None if key does not exist 18 mdKeys = metadata.paramNames()
20 return metadata.get(key)
26 """Wrapper for setting a value in a metadata object. Deals with case 27 where the key already exists. 28 @param[in, out] metadata metadata object ot modify in place. 29 @param[in] key key to associate with value 30 @param[in] value value to assign in the metadata object 31 @param[in] clobber Clobber the value if the key already exisists? 33 mdKeys = metadata.paramNames()
34 if key
not in mdKeys
or (key
in mdKeys
and clobber):
35 metadata.set(key, value)
39 """ Class to hold mapping of header cards to attributes""" 41 def addEntry(self, keyname, attribute_name, default=None, transform=lambda x: x):
42 """Adds an entry to the registr 43 @param[in] keyname Key used to retrieve the header record 44 @param[in] attribute_name Name of the attribute to store the value in 45 @param[jn] default Default velue to store if the header card is not available 46 @param[in] transform Transform to apply to the header value before assigning it to the 49 self.__setitem__(attribute_name, {
'keyName': keyname,
51 'transform': transform})
54 """Sets the attributes on the give object given a metadata object. 55 @param[in, out] obj Object on which to operate in place 56 @param[in] metadata Metadata object used for applying the mapping 57 @param[in] doRaise Raise exceptions on calling methods on the 58 input object that do not exist? 60 for key, attrDict
in self.items():
62 value =
getByKey(metadata, attrDict[
'keyName'])
66 self.
_applyVal(obj, value, key, attrDict[
'transform'])
70 value = attrDict[
'default']
71 self.
_applyVal(obj, value, key,
lambda x: x)
72 except Exception
as e:
76 warnings.warn(
'WARNING: Failed to set %s attribute with %s value: %s' %
79 def _applyVal(self, obj, value, attrName, transform):
80 raise NotImplementedError(
'Must be implemented in sub-class')
84 """ Class to hold mapping of header cards to AmpInfoTable attributes 85 The amp info is stored using setters, thus calling the attribute as a function. 88 def _applyVal(self, obj, value, attrName, transform):
93 """ Class to hold mapping of header cards to Detector attributes 94 Detector information is stored as attributes on a Config object. 97 def _applyVal(self, obj, value, attrName, transform):
98 obj.__setattr__(attrName,
transform(value))
102 def __init__(self, detectorFileName, ampFileNameList, inAmpCoords=True, plateScale=1.,
103 radialCoeffs=(0., 1.), clobberMetadata=
False, doRaise=
True):
104 ''' @param[in] detectorFileName FITS file containing the detector description. 105 May use [] notation to specify an extension in an MEF. 106 @param[in] ampFileNameList List of FITS file names to use in building the amps. 107 May contain duplicate entries if the raw data are assembled. 108 @param[in] inAmpCoords Boolean, True if raw data are in amp coordinates, False if raw data 109 are assembled into pseudo detector pixel arrays 110 @param[in] plateScale Nominal platescale (arcsec/mm) 111 @param[in] radialCoeffs Radial distortion coefficients for a radial polynomial in normalized 113 @param[in] clobberMetadata Clobber metadata from input files if 114 overridden in the _sanitizeMetadata method 115 @param[in] doRaise Raise exception if not all non-defaulted 116 keywords are defined? Default is True. 127 for fileName
in ampFileNameList:
134 def _sanitizeHeaderMetadata(self, metadata, clobber):
135 """This method is called for all metadata and gives an opportunity to add/modify 136 header information for use downstream. 137 Override this method if more than the default is needed. 138 @param[in, out] metadata Metadata to read/modify 139 @param[in] clobber Clobber keys that exist with default keys? 143 def _defaultSanitization(self, metadata, clobber):
144 """Does the default sanitization of the header metadata. 145 @param[in,out] metadata Header metadata to extend/modify 146 @param[in] clobber Override values in existing header cards? 157 if dtm1
is not None and dtm2
is not None:
158 setByKey(metadata,
'FLIPX', dtm1 < 0, clobber)
159 setByKey(metadata,
'FLIPY', dtm2 < 0, clobber)
161 afwTable.ReadoutCorner.LL), clobber)
163 setByKey(metadata,
'FLIPX',
False, clobber)
164 setByKey(metadata,
'FLIPY',
True, clobber)
167 setByKey(metadata,
'RDCRNR',
None, clobber)
172 if xext
is not None and yext
is not None:
173 setByKey(metadata,
'RAWBBOX',
'[%i:%i,%i:%i]'%(
174 1, xext, 1, yext), clobber)
178 if dtv1
is not None and dtv2
is not None:
179 setByKey(metadata,
'XYOFF', [dtv1, dtv2], clobber)
183 if metadata.isArray(
'BIASSEC'):
184 keylist = [
'HOSCAN',
'PRESCAN',
'VOSCAN']
185 biassecs =
getByKey(metadata,
'BIASSEC')
186 for i, biassec
in enumerate(biassecs):
187 setByKey(metadata, keylist[i], biassec, clobber)
189 biassec =
getByKey(metadata,
'BIASSEC')
190 if biassec
is not None:
191 setByKey(metadata,
'HOSCAN', biassec, clobber)
193 def _makeDefaultAmpMap(self):
194 """Make the default map from header information to amplifier information 195 @return The HeaderAmpMap object containing the mapping 199 mapList = [(
'EXTNAME',
'setName'),
200 (
'DETSEC',
'setBBox',
None, self.
_makeBbox),
201 (
'GAIN',
'setGain', 1.),
202 (
'RDNOISE',
'setReadNoise', 0.),
203 (
'SATURATE',
'setSaturation', 2 << 15),
204 (
'RDCRNR',
'setReadoutCorner', int(
205 afwTable.ReadoutCorner.LL), afwTable.ReadoutCorner),
206 (
'LINCOEFF',
'setLinearityCoeffs', [0., 1.]),
207 (
'LINTYPE',
'setLinearityType',
'POLY'),
208 (
'RAWBBOX',
'setRawBBox',
None, self.
_makeBbox),
209 (
'DATASEC',
'setRawDataBBox',
None, self.
_makeBbox),
210 (
'FLIPX',
'setRawFlipX',
False),
211 (
'FLIPY',
'setRawFlipY',
False),
213 (
'HOSCAN',
'setRawHorizontalOverscanBBox',
215 (
'VOSCAN',
'setRawVerticalOverscanBBox',
217 (
'PRESCAN',
'setRawPrescanBBox', emptyBBox, self.
_makeBbox),
223 def _makeDefaultDetectorMap(self):
224 """Make the default map from header information to detector information 225 @return The HeaderDetectorMap object containing the mapping 228 mapList = [(
'CCDNAME',
'name',
'ccdName'),
236 (
'OBSTYPE',
'detectorType', int(
237 afwCameraGeom.DetectorType.SCIENCE)),
238 (
'SERSTR',
'serial',
'none'),
239 (
'XPOS',
'offset_x', 0.),
240 (
'YPOS',
'offset_y', 0.),
241 (
'XPIX',
'refpos_x', 0.),
242 (
'YPIX',
'refpos_y', 0.),
243 (
'YAWDEG',
'yawDeg', 0.),
244 (
'PITCHDEG',
'pitchDeg', 0.),
245 (
'ROLLDEG',
'rollDeg', 0.),
246 (
'XPIXSIZE',
'pixelSize_x', 1.),
247 (
'YPIXSIZE',
'pixelSize_y', 1.),
248 (
'TRNSPOSE',
'transposeDetector',
False),
254 def _makeExt(self, extArr):
255 """Helper function to make an extent from an array 256 @param[in] extArr Length 2 array to use in creating the Extent object 257 @return Extent2I constructed from the input list 261 def _makeBbox(self, boxString):
262 """Helper funtion to make a bounding box from a string representing a FITS style bounding box 263 @param[in] boxString String describing the bounding box 264 @return Box2I for the bounding box 267 x1, x2, y1, y2 = [int(el)
for el
in re.split(
268 '[:,]', boxString.strip()[1:-1])]
275 def _getBboxX0(self, boxString):
276 return self.
_makeBbox(boxString).getMinX()
278 def _getBboxX1(self, boxString):
279 return self.
_makeBbox(boxString).getMaxX()
281 def _getBboxY0(self, boxString):
282 return self.
_makeBbox(boxString).getMinY()
284 def _getBboxY1(self, boxString):
285 return self.
_makeBbox(boxString).getMaxY()
287 def _makeRadialTransform(self, radialCoeffs):
288 """Helper function to get the radial transform given the radial polynomial coefficients given in 290 @param[in] radialCoeffs List of coefficients describing a polynomial radial distortion in 292 @return Transform object describing the radial distortion 298 """Take all the information and build a Detector object. The Detector object is necessary for doing 299 things like assembly. 300 @return Detector object 305 schema = afwTable.AmpInfoTable.makeMinimalSchema()
306 ampInfo = afwTable.AmpInfoCatalog(schema)
308 record = ampInfo.addNew()
310 record.setHasRawInfo(
True)
312 detConfig = afwCameraGeom.DetectorConfig()
315 self.
detector = afwCameraGeom.makeDetector(
320 """PLaceholder for subclasses to implement construction of a calib to associate with the exposure. 321 @return empty afwImage.Calib object 326 """Method for constructing an exposure object from an image and the information contained in this 327 class to construct the Detector and Calib objects. 328 @param[in] im Image used to construct the exposure 329 @param[in] mask Optional mask plane as a <askU 330 @param[in] variance Optional variance plance as an image of the same type as im 331 @param[out] Exposure object 343 exp.setDetector(detector)
def makeExposure(self, im, mask=None, variance=None)
def _makeRadialTransform(self, radialCoeffs)
def _getBboxY1(self, boxString)
def _makeDefaultAmpMap(self)
std::shared_ptr< TransformPoint2ToPoint2 > makeRadialTransform(std::vector< double > const &forwardCoeffs, std::vector< double > const &inverseCoeffs)
A purely radial polynomial distortion.
def _makeDefaultDetectorMap(self)
def _makeBbox(self, boxString)
Describe an exposure's calibration.
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
def _defaultSanitization(self, metadata, clobber)
An integer coordinate rectangle.
def _getBboxY0(self, boxString)
std::shared_ptr< Exposure< ImagePixelT, MaskPixelT, VariancePixelT > > makeExposure(MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > &mimage, std::shared_ptr< geom::SkyWcs const > wcs=std::shared_ptr< geom::SkyWcs const >())
A function to return an Exposure of the correct type (cf.
def _sanitizeHeaderMetadata(self, metadata, clobber)
Represent a 2-dimensional array of bitmask pixels.
def getByKey(metadata, key)
def _getBboxX0(self, boxString)
def _makeExt(self, extArr)
constexpr double arcsecToRad(double x) noexcept
def _getBboxX1(self, boxString)
table::Key< int > transform
def __init__(self, detectorFileName, ampFileNameList, inAmpCoords=True, plateScale=1., radialCoeffs=(0., 1.), clobberMetadata=False, doRaise=True)
def setByKey(metadata, key, value, clobber)