LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
rawFormatter.py
Go to the documentation of this file.
1 # This file is part of obs_decam.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 """Gen3 Butler Formatters for Dark Energy Camera raw data.
23 """
24 
25 import astro_metadata_translator
26 
27 import lsst.afw.fits
28 import lsst.afw.image
29 import lsst.log
30 from lsst.obs.base import FitsRawFormatterBase
31 
32 from . import DarkEnergyCamera
33 
34 __all__ = ("DarkEnergyCameraRawFormatter", "DarkEnergyCameraCPCalibFormatter")
35 
36 
37 # The mapping of detector id to HDU in raw files for "most" DECam data.
38 # We try this first before scaning the HDUs manually.
39 detector_to_hdu = {25: 1, 26: 2, 27: 3, 32: 4, 33: 5, 34: 6, 19: 7, 20: 8, 13: 9,
40  14: 10, 8: 11, 4: 12, 39: 13, 40: 14, 45: 15, 46: 16, 51: 17, 56: 18, 21: 19,
41  22: 20, 23: 21, 24: 22, 17: 23, 18: 24, 15: 25, 16: 26, 9: 27, 10: 28, 11: 29,
42  12: 30, 5: 31, 6: 32, 7: 33, 1: 34, 2: 35, 3: 36, 35: 37, 36: 38, 37: 39,
43  38: 40, 28: 41, 29: 42, 30: 43, 31: 44, 41: 45, 42: 46, 43: 47, 44: 48, 49: 49,
44  50: 50, 47: 51, 48: 52, 52: 53, 53: 54, 54: 55, 55: 56, 57: 57, 58: 58, 59: 59,
45  60: 60, 61: 61, 62: 62, 72: 63, 71: 64, 64: 65, 63: 66, 73: 67, 74: 68, 70: 69,
46  69: 70
47  }
48 
49 
51  translatorClass = astro_metadata_translator.DecamTranslator
52  filterDefinitions = DarkEnergyCamera.filterDefinitions
53 
54  # DECam has a coordinate system flipped on X with respect to our
55  # VisitInfo definition of the field angle orientation.
56  # We have to specify the flip explicitly until DM-20746 is implemented.
57  wcsFlipX = True
58 
59  def getDetector(self, id):
60  return DarkEnergyCamera().getCamera()[id]
61 
62  def _scanHdus(self, filename, detectorId):
63  """Scan through a file for the HDU containing data from one detector.
64 
65  Parameters
66  ----------
67  filename : `str`
68  The file to search through.
69  detectorId : `int`
70  The detector id to search for.
71 
72  Returns
73  -------
74  index : `int`
75  The index of the HDU with the requested data.
76  metadata: `lsst.daf.base.PropertyList`
77  The metadata read from the header for that detector id.
78 
79  Raises
80  ------
81  ValueError
82  Raised if detectorId is not found in any of the file HDUs
83  """
84  log = lsst.log.Log.getLogger("DarkEnergyCameraRawFormatter")
85  log.debug("Did not find detector=%s at expected HDU=%s in %s: scanning through all HDUs.",
86  detectorId, detector_to_hdu[detectorId], filename)
87 
88  fitsData = lsst.afw.fits.Fits(filename, 'r')
89  # NOTE: The primary header (HDU=0) does not contain detector data.
90  for i in range(1, fitsData.countHdus()):
91  fitsData.setHdu(i)
92  metadata = fitsData.readMetadata()
93  if metadata['CCDNUM'] == detectorId:
94  return i, metadata
95  else:
96  raise ValueError(f"Did not find detectorId={detectorId} as CCDNUM in any HDU of {filename}.")
97 
98  def _determineHDU(self, detectorId):
99  """Determine the correct HDU number for a given detector id.
100 
101  Parameters
102  ----------
103  detectorId : `int`
104  The detector id to search for.
105 
106  Returns
107  -------
108  index : `int`
109  The index of the HDU with the requested data.
110  metadata : `lsst.daf.base.PropertyList`
111  The metadata read from the header for that detector id.
112 
113  Raises
114  ------
115  ValueError
116  Raised if detectorId is not found in any of the file HDUs
117  """
118  filename = self.fileDescriptor.location.path
119  try:
120  index = detector_to_hdu[detectorId]
121  metadata = lsst.afw.fits.readMetadata(filename, index)
122  if metadata['CCDNUM'] != detectorId:
123  # the detector->HDU mapping is different in this file: try scanning
124  return self._scanHdus(filename, detectorId)
125  else:
126  fitsData = lsst.afw.fits.Fits(filename, 'r')
127  fitsData.setHdu(index)
128  return index, metadata
130  # if the file doesn't contain all the HDUs of "normal" files, try scanning
131  return self._scanHdus(filename, detectorId)
132 
133  def readMetadata(self):
134  index, metadata = self._determineHDU(self.dataId['detector'])
135  astro_metadata_translator.fix_header(metadata)
136  return metadata
137 
138  def readImage(self):
139  index, metadata = self._determineHDU(self.dataId['detector'])
140  return lsst.afw.image.ImageI(self.fileDescriptor.location.path, index)
141 
142 
144  """DECam Community Pipeline calibrations (bias, dark, flat, fringe) are
145  multi-extension FITS files with detector=index+1.
146  """
147 
148  def _determineHDU(self, detectorId):
149  """The HDU to read is the same as the detector number."""
150  filename = self.fileDescriptor.location.path
151  metadata = lsst.afw.fits.readMetadata(filename, detectorId)
152  if metadata['CCDNUM'] != detectorId:
153  msg = f"Found CCDNUM={metadata['CCDNUM']} instead of {detectorId} in {filename} HDU={detectorId}."
154  raise ValueError(msg)
155  return detectorId, metadata
156 
157  def readImage(self):
158  index, metadata = self._determineHDU(self.dataId['detector'])
159  return lsst.afw.image.ImageF(self.fileDescriptor.location.path, index)
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter.getDetector
def getDetector(self, id)
Definition: rawFormatter.py:59
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter._determineHDU
def _determineHDU(self, detectorId)
Definition: rawFormatter.py:98
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter._scanHdus
def _scanHdus(self, filename, detectorId)
Definition: rawFormatter.py:62
lsst::afw::fits::FitsError
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:36
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
lsst::log::Log::getLogger
static Log getLogger(Log const &logger)
Definition: Log.h:760
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter.readMetadata
def readMetadata(self)
Definition: rawFormatter.py:133
lsst.obs.decam.rawFormatter.DarkEnergyCameraCPCalibFormatter.readImage
def readImage(self)
Definition: rawFormatter.py:157
lsst::log
Definition: Log.h:706
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter.readImage
def readImage(self)
Definition: rawFormatter.py:138
lsst.obs.base._fitsRawFormatterBase.FitsRawFormatterBase
Definition: _fitsRawFormatterBase.py:39
lsst::afw::fits::readMetadata
std::shared_ptr< daf::base::PropertyList > readMetadata(fits::Fits &fitsfile, bool strip=false)
Read FITS header.
Definition: fits.cc:1669
lsst.obs.decam.rawFormatter.DarkEnergyCameraCPCalibFormatter
Definition: rawFormatter.py:143
lsst::afw::fits
Definition: fits.h:31
lsst.obs.base
Definition: __init__.py:1
lsst.obs.decam.rawFormatter.DarkEnergyCameraRawFormatter
Definition: rawFormatter.py:50