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

# This file is part of obs_decam. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://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 <http://www.gnu.org/licenses/>. 

 

"""Gen3 Butler Formatters for Dark Energy Camera raw data. 

""" 

 

import astro_metadata_translator 

 

import lsst.afw.fits 

import lsst.afw.image 

import lsst.log 

from lsst.obs.base.fitsRawFormatterBase import FitsRawFormatterBase 

 

from . import DarkEnergyCamera 

 

__all__ = ("DarkEnergyCameraRawFormatter",) 

 

 

# The mapping of detector id to HDU in raw files for "most" DECam data. 

# We try this first before scaning the HDUs manually. 

detector_to_hdu = {25: 1, 26: 2, 27: 3, 32: 4, 33: 5, 34: 6, 19: 7, 20: 8, 13: 9, 

14: 10, 8: 11, 4: 12, 39: 13, 40: 14, 45: 15, 46: 16, 51: 17, 56: 18, 21: 19, 

22: 20, 23: 21, 24: 22, 17: 23, 18: 24, 15: 25, 16: 26, 9: 27, 10: 28, 11: 29, 

12: 30, 5: 31, 6: 32, 7: 33, 1: 34, 2: 35, 3: 36, 35: 37, 36: 38, 37: 39, 

38: 40, 28: 41, 29: 42, 30: 43, 31: 44, 41: 45, 42: 46, 43: 47, 44: 48, 49: 49, 

50: 50, 47: 51, 48: 52, 52: 53, 53: 54, 54: 55, 55: 56, 57: 57, 58: 58, 59: 59, 

60: 60, 61: 61, 62: 62, 72: 63, 71: 64, 64: 65, 63: 66, 73: 67, 74: 68, 70: 69, 

69: 70 

} 

 

 

class DarkEnergyCameraRawFormatter(FitsRawFormatterBase): 

translatorClass = astro_metadata_translator.DecamTranslator 

filterDefinitions = DarkEnergyCamera.filterDefinitions 

 

def getDetector(self, id): 

return DarkEnergyCamera().getCamera()[id] 

 

def _scanHdus(self, filename, detectorId): 

"""Scan through a file for the HDU containing data from one detector. 

 

Parameters 

---------- 

filename : `str` 

The file to search through. 

detectorId : `int` 

The detector id to search for. 

 

Returns 

------- 

index : `int` 

The index of the HDU with the requested data. 

metadata: `lsst.daf.base.PropertyList` 

The metadata read from the header for that detector id. 

 

Raises 

------ 

ValueError 

Raised if detectorId is not found in any of the file HDUs 

""" 

log = lsst.log.Log.getLogger("DarkEnergyCameraRawFormatter") 

log.debug("Did not find detector=%s at expected HDU=%s in %s: scanning through all HDUs.", 

detectorId, detector_to_hdu[detectorId], filename) 

 

fitsData = lsst.afw.fits.Fits(filename, 'r') 

# NOTE: The primary header (HDU=0) does not contain detector data. 

for i in range(1, fitsData.countHdus()): 

fitsData.setHdu(i) 

metadata = fitsData.readMetadata() 

if metadata['CCDNUM'] == detectorId: 

return i, metadata 

else: 

raise ValueError(f"Did not find detectorId={detectorId} as CCDNUM in any HDU of {filename}.") 

 

def _determineHDU(self, detectorId): 

"""Determine the correct HDU number for a given detector id. 

 

Parameters 

---------- 

detectorId : `int` 

The detector id to search for. 

 

Returns 

------- 

index : `int` 

The index of the HDU with the requested data. 

metadata : `lsst.daf.base.PropertyList` 

The metadata read from the header for that detector id. 

 

Raises 

------ 

ValueError 

Raised if detectorId is not found in any of the file HDUs 

""" 

filename = self.fileDescriptor.location.path 

try: 

index = detector_to_hdu[detectorId] 

metadata = lsst.afw.image.readMetadata(filename, index) 

if metadata['CCDNUM'] != detectorId: 

# the detector->HDU mapping is different in this file: try scanning 

return self._scanHdus(filename, detectorId) 

else: 

fitsData = lsst.afw.fits.Fits(filename, 'r') 

fitsData.setHdu(index) 

return index, metadata 

except lsst.afw.fits.FitsError: 

# if the file doesn't contain all the HDUs of "normal" files, try scanning 

return self._scanHdus(filename, detectorId) 

 

def readMetadata(self): 

index, metadata = self._determineHDU(self.dataId['detector']) 

astro_metadata_translator.fix_header(metadata) 

return metadata 

 

def readImage(self): 

index, metadata = self._determineHDU(self.dataId['detector']) 

return lsst.afw.image.ImageI(self.fileDescriptor.location.path, index)