22 __all__ = (
"FitsExposureFormatter", )
 
   24 from astro_metadata_translator 
import fix_header
 
   25 from lsst.daf.butler 
import Formatter
 
   30     """Interface for reading and writing Exposures to and from FITS files. 
   37         """The metadata read from this file. It will be stripped as 
   38         components are extracted from it 
   39         (`lsst.daf.base.PropertyList`). 
   46         """Read all header metadata directly into a PropertyList. 
   50         metadata : `~lsst.daf.base.PropertyList` 
   59         """Remove metadata entries that are parsed into components. 
   61         This is only called when just the metadata is requested; stripping 
   62         entries there forces code that wants other components to ask for those 
   63         components directly rather than trying to extract them from the 
   64         metadata manually, which is fragile.  This behavior is an intentional 
   69         metadata : `~lsst.daf.base.PropertyList` 
   70             Header metadata, to be modified in-place. 
   80         """Read a component held by the Exposure. 
   84         component : `str`, optional 
   85             Component to read from the file. 
   86         parameters : `dict`, optional 
   87             If specified, a dictionary of slicing parameters that 
   88             overrides those in ``fileDescriptor``. 
   92         obj : component-dependent 
   93             In-memory component object. 
   98             Raised if the requested component cannot be handled. 
  100         componentMap = {
'wcs': (
'readWcs', 
False),
 
  101                         'coaddInputs': (
'readCoaddInputs', 
False),
 
  102                         'psf': (
'readPsf', 
False),
 
  103                         'image': (
'readImage', 
True),
 
  104                         'mask': (
'readMask', 
True),
 
  105                         'variance': (
'readVariance', 
True),
 
  106                         'photoCalib': (
'readPhotoCalib', 
False),
 
  107                         'bbox': (
'readBBox', 
True),
 
  108                         'xy0': (
'readXY0', 
True),
 
  109                         'metadata': (
'readMetadata', 
False),
 
  110                         'filter': (
'readFilter', 
False),
 
  111                         'polygon': (
'readValidPolygon', 
False),
 
  112                         'apCorrMap': (
'readApCorrMap', 
False),
 
  113                         'visitInfo': (
'readVisitInfo', 
False),
 
  114                         'transmissionCurve': (
'readTransmissionCurve', 
False),
 
  115                         'detector': (
'readDetector', 
False),
 
  116                         'extras': (
'readExtraComponents', 
False),
 
  117                         'exposureInfo': (
'readExposureInfo', 
False),
 
  119         method, hasParams = componentMap.get(component, 
None)
 
  123             caller = getattr(reader, method, 
None)
 
  126                 if parameters 
is None:
 
  127                     parameters = self.fileDescriptor.parameters
 
  128                 if parameters 
is None:
 
  130                 self.fileDescriptor.storageClass.validateParameters(parameters)
 
  132                 if hasParams 
and parameters:
 
  133                     return caller(**parameters)
 
  137             raise KeyError(f
"Unknown component requested: {component}")
 
  140         """Read the full Exposure object. 
  144         parameters : `dict`, optional 
  145             If specified a dictionary of slicing parameters that overrides 
  146             those in ``fileDescriptor`. 
  150         exposure : `~lsst.afw.image.Exposure` 
  151             Complete in-memory exposure. 
  153         fileDescriptor = self.fileDescriptor
 
  154         if parameters 
is None:
 
  155             parameters = fileDescriptor.parameters
 
  156         if parameters 
is None:
 
  158         fileDescriptor.storageClass.validateParameters(parameters)
 
  160             output = fileDescriptor.storageClass.pytype(fileDescriptor.location.path, **parameters)
 
  163             output = reader.read(**parameters)
 
  166     def read(self, component=None, parameters=None):
 
  167         """Read data from a file. 
  171         component : `str`, optional 
  172             Component to read from the file. Only used if the `StorageClass` 
  173             for reading differed from the `StorageClass` used to write the 
  175         parameters : `dict`, optional 
  176             If specified, a dictionary of slicing parameters that 
  177             overrides those in ``fileDescriptor``. 
  181         inMemoryDataset : `object` 
  182             The requested data as a Python object. The type of object 
  183             is controlled by the specific formatter. 
  188             Component requested but this file does not seem to be a concrete 
  191             Raised when parameters passed with fileDescriptor are not 
  194         fileDescriptor = self.fileDescriptor
 
  195         if fileDescriptor.readStorageClass != fileDescriptor.storageClass:
 
  196             if component == 
"metadata":
 
  199             elif component 
is not None:
 
  202                 raise ValueError(
"Storage class inconsistency ({} vs {}) but no" 
  203                                  " component requested".
format(fileDescriptor.readStorageClass.name,
 
  204                                                                fileDescriptor.storageClass.name))
 
  208         """Write a Python object to a file. 
  212         inMemoryDataset : `object` 
  213             The Python object to store. 
  218             The `URI` where the primary file is stored. 
  221         self.fileDescriptor.location.updateExtension(self.
extension)
 
  222         inMemoryDataset.writeFits(self.fileDescriptor.location.path)
 
  223         return self.fileDescriptor.location.pathInStore