1 from __future__
import absolute_import, division, print_function
2 from builtins
import object
12 """Wrapper for getting a value from a metadata object by key. 13 @param[in] metadata metadata object to query for value 14 @param[in] key key to use for value lookup 15 @returns value associated with key, None if key does not exist 17 mdKeys = metadata.paramNames()
19 return metadata.get(key)
25 """Wrapper for setting a value in a metadata object. Deals with case 26 where the key already exists. 27 @param[in, out] metadata metadata object ot modify in place. 28 @param[in] key key to associate with value 29 @param[in] value value to assign in the metadata object 30 @param[in] clobber Clobber the value if the key already exisists? 32 mdKeys = metadata.paramNames()
33 if key
not in mdKeys
or (key
in mdKeys
and clobber):
34 metadata.set(key, value)
38 """ Class to hold mapping of header cards to attributes""" 40 def addEntry(self, keyname, attribute_name, default=None, transform=lambda x: x):
41 """Adds an entry to the registr 42 @param[in] keyname Key used to retrieve the header record 43 @param[in] attribute_name Name of the attribute to store the value in 44 @param[jn] default Default velue to store if the header card is not available 45 @param[in] transform Transform to apply to the header value before assigning it to the 48 self.__setitem__(attribute_name, {
'keyName': keyname,
50 'transform': transform})
53 """Sets the attributes on the give object given a metadata object. 54 @param[in, out] obj Object on which to operate in place 55 @param[in] metadata Metadata object used for applying the mapping 56 @param[in] doRaise Raise exceptions on calling methods on the 57 input object that do not exist? 59 for key, attrDict
in self.items():
61 value =
getByKey(metadata, attrDict[
'keyName'])
65 self.
_applyVal(obj, value, key, attrDict[
'transform'])
69 value = attrDict[
'default']
70 self.
_applyVal(obj, value, key,
lambda x: x)
71 except Exception
as e:
75 warnings.warn(
'WARNING: Failed to set %s attribute with %s value: %s' %
78 def _applyVal(self, obj, value, attrName, transform):
79 raise NotImplementedError(
'Must be implemented in sub-class')
83 """ Class to hold mapping of header cards to AmpInfoTable attributes 84 The amp info is stored using setters, thus calling the attribute as a function. 87 def _applyVal(self, obj, value, attrName, transform):
92 """ Class to hold mapping of header cards to Detector attributes 93 Detector information is stored as attributes on a Config object. 96 def _applyVal(self, obj, value, attrName, transform):
97 obj.__setattr__(attrName,
transform(value))
101 def __init__(self, detectorFileName, ampFileNameList, inAmpCoords=True, plateScale=1.,
102 radialCoeffs=(0., 1.), clobberMetadata=
False, doRaise=
True):
103 ''' @param[in] detectorFileName FITS file containing the detector description. 104 May use [] notation to specify an extension in an MEF. 105 @param[in] ampFileNameList List of FITS file names to use in building the amps. 106 May contain duplicate entries if the raw data are assembled. 107 @param[in] inAmpCoords Boolean, True if raw data are in amp coordinates, False if raw data 108 are assembled into pseudo detector pixel arrays 109 @param[in] plateScale Nominal platescale (arcsec/mm) 110 @param[in] radialCoeffs Radial distortion coefficients for a radial polynomial in normalized 112 @param[in] clobberMetadata Clobber metadata from input files if 113 overridden in the _sanitizeMetadata method 114 @param[in] doRaise Raise exception if not all non-defaulted 115 keywords are defined? Default is True. 126 for fileName
in ampFileNameList:
133 def _sanitizeHeaderMetadata(self, metadata, clobber):
134 """This method is called for all metadata and gives an opportunity to add/modify 135 header information for use downstream. 136 Override this method if more than the default is needed. 137 @param[in, out] metadata Metadata to read/modify 138 @param[in] clobber Clobber keys that exist with default keys? 142 def _defaultSanitization(self, metadata, clobber):
143 """Does the default sanitization of the header metadata. 144 @param[in,out] metadata Header metadata to extend/modify 145 @param[in] clobber Override values in existing header cards? 156 if dtm1
is not None and dtm2
is not None:
157 setByKey(metadata,
'FLIPX', dtm1 < 0, clobber)
158 setByKey(metadata,
'FLIPY', dtm2 < 0, clobber)
160 afwTable.ReadoutCorner.LL), clobber)
162 setByKey(metadata,
'FLIPX',
False, clobber)
163 setByKey(metadata,
'FLIPY',
True, clobber)
166 setByKey(metadata,
'RDCRNR',
None, clobber)
171 if xext
is not None and yext
is not None:
172 setByKey(metadata,
'RAWBBOX',
'[%i:%i,%i:%i]'%(
173 1, xext, 1, yext), clobber)
177 if dtv1
is not None and dtv2
is not None:
178 setByKey(metadata,
'XYOFF', [dtv1, dtv2], clobber)
182 if metadata.isArray(
'BIASSEC'):
183 keylist = [
'HOSCAN',
'PRESCAN',
'VOSCAN']
184 biassecs =
getByKey(metadata,
'BIASSEC')
185 for i, biassec
in enumerate(biassecs):
186 setByKey(metadata, keylist[i], biassec, clobber)
188 biassec =
getByKey(metadata,
'BIASSEC')
189 if biassec
is not None:
190 setByKey(metadata,
'HOSCAN', biassec, clobber)
192 def _makeDefaultAmpMap(self):
193 """Make the default map from header information to amplifier information 194 @return The HeaderAmpMap object containing the mapping 198 mapList = [(
'EXTNAME',
'setName'),
199 (
'DETSEC',
'setBBox',
None, self.
_makeBbox),
200 (
'GAIN',
'setGain', 1.),
201 (
'RDNOISE',
'setReadNoise', 0.),
202 (
'SATURATE',
'setSaturation', 2 << 15),
203 (
'RDCRNR',
'setReadoutCorner', int(
204 afwTable.ReadoutCorner.LL), afwTable.ReadoutCorner),
205 (
'LINCOEFF',
'setLinearityCoeffs', [0., 1.]),
206 (
'LINTYPE',
'setLinearityType',
'POLY'),
207 (
'RAWBBOX',
'setRawBBox',
None, self.
_makeBbox),
208 (
'DATASEC',
'setRawDataBBox',
None, self.
_makeBbox),
209 (
'FLIPX',
'setRawFlipX',
False),
210 (
'FLIPY',
'setRawFlipY',
False),
212 (
'HOSCAN',
'setRawHorizontalOverscanBBox',
214 (
'VOSCAN',
'setRawVerticalOverscanBBox',
216 (
'PRESCAN',
'setRawPrescanBBox', emptyBBox, self.
_makeBbox),
222 def _makeDefaultDetectorMap(self):
223 """Make the default map from header information to detector information 224 @return The HeaderDetectorMap object containing the mapping 227 mapList = [(
'CCDNAME',
'name',
'ccdName'),
235 (
'OBSTYPE',
'detectorType', int(
236 afwCameraGeom.DetectorType.SCIENCE)),
237 (
'SERSTR',
'serial',
'none'),
238 (
'XPOS',
'offset_x', 0.),
239 (
'YPOS',
'offset_y', 0.),
240 (
'XPIX',
'refpos_x', 0.),
241 (
'YPIX',
'refpos_y', 0.),
242 (
'YAWDEG',
'yawDeg', 0.),
243 (
'PITCHDEG',
'pitchDeg', 0.),
244 (
'ROLLDEG',
'rollDeg', 0.),
245 (
'XPIXSIZE',
'pixelSize_x', 1.),
246 (
'YPIXSIZE',
'pixelSize_y', 1.),
247 (
'TRNSPOSE',
'transposeDetector',
False),
253 def _makeExt(self, extArr):
254 """Helper function to make an extent from an array 255 @param[in] extArr Length 2 array to use in creating the Extent object 256 @return Extent2I constructed from the input list 260 def _makeBbox(self, boxString):
261 """Helper funtion to make a bounding box from a string representing a FITS style bounding box 262 @param[in] boxString String describing the bounding box 263 @return Box2I for the bounding box 266 x1, x2, y1, y2 = [int(el)
for el
in re.split(
267 '[:,]', boxString.strip()[1:-1])]
274 def _getBboxX0(self, boxString):
275 return self.
_makeBbox(boxString).getMinX()
277 def _getBboxX1(self, boxString):
278 return self.
_makeBbox(boxString).getMaxX()
280 def _getBboxY0(self, boxString):
281 return self.
_makeBbox(boxString).getMinY()
283 def _getBboxY1(self, boxString):
284 return self.
_makeBbox(boxString).getMaxY()
286 def _makeRadialTransform(self, radialCoeffs):
287 """Helper function to get the radial transform given the radial polynomial coefficients given in 289 @param[in] radialCoeffs List of coefficients describing a polynomial radial distortion in 291 @return Transform object describing the radial distortion 297 """Take all the information and build a Detector object. The Detector object is necessary for doing 298 things like assembly. 299 @return Detector object 304 schema = afwTable.AmpInfoTable.makeMinimalSchema()
305 ampInfo = afwTable.AmpInfoCatalog(schema)
307 record = ampInfo.addNew()
309 record.setHasRawInfo(
True)
311 detConfig = afwCameraGeom.DetectorConfig()
314 self.
detector = afwCameraGeom.makeDetector(
319 """PLaceholder for subclasses to implement construction of a calib to associate with the exposure. 320 @return empty afwImage.Calib object 325 """Method for constructing an exposure object from an image and the information contained in this 326 class to construct the Detector and Calib objects. 327 @param[in] im Image used to construct the exposure 328 @param[in] mask Optional mask plane as a <askU 329 @param[in] variance Optional variance plance as an image of the same type as im 330 @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< Wcs > makeWcs(coord::Coord const &crval, geom::Point2D const &crpix, double CD11, double CD12, double CD21, double CD22)
Create a Wcs object from crval, crpix, CD, using CD elements (useful from python) ...
std::shared_ptr< TransformPoint2ToPoint2 > makeRadialTransform(std::vector< double > const &forwardCoeffs, std::vector< double > const &inverseCoeffs)
A purely radial polynomial distortion.
def _makeDefaultDetectorMap(self)
std::shared_ptr< daf::base::PropertyList > readMetadata(std::string const &fileName, int hdu=fits::DEFAULT_HDU, bool strip=false)
Return the metadata (header entries) from a FITS file.
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)
def _sanitizeHeaderMetadata(self, metadata, clobber)
std::shared_ptr< Exposure< ImagePixelT, MaskPixelT, VariancePixelT > > makeExposure(MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > &mimage, std::shared_ptr< Wcs const > wcs=std::shared_ptr< Wcs const >())
A function to return an Exposure of the correct type (cf.
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)