Coverage for python/lsst/obs/cfht/ingest.py : 22%

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# LSST Data Management System
3# Copyright 2012 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23__all__ = ["MegacamParseTask", "MegaPrimeRawIngestTask"]
25import re
27import lsst.obs.base
28from lsst.obs.base.ingest import RawFileData
29from astro_metadata_translator import fix_header
31from lsst.pipe.tasks.ingest import ParseTask
32import lsst.pex.exceptions
33from ._instrument import MegaPrime
35filters = {'u.MP9301': 'u',
36 'u.MP9302': 'u2',
37 'g.MP9401': 'g',
38 'g.MP9402': 'g2',
39 'r.MP9601': 'r',
40 'r.MP9602': 'r2',
41 'i.MP9701': 'i',
42 'i.MP9702': 'i2',
43 'i.MP9703': 'i3',
44 'z.MP9801': 'z',
45 'z.MP9901': 'z2',
46 }
49class MegaPrimeRawIngestTask(lsst.obs.base.RawIngestTask):
50 """Task for ingesting raw MegaPrime multi-extension FITS data into Gen3.
51 """
52 def extractMetadata(self, filename: str) -> RawFileData:
53 datasets = []
54 fitsData = lsst.afw.fits.Fits(filename, "r")
56 # NOTE: The primary header (HDU=0) does not contain detector data.
57 for i in range(1, fitsData.countHdus()):
58 fitsData.setHdu(i)
59 header = fitsData.readMetadata()
60 if not header["EXTNAME"].startswith("ccd"):
61 continue
62 fix_header(header)
63 datasets.append(self._calculate_dataset_info(header, filename))
65 # The data model currently assumes that whilst multiple datasets
66 # can be associated with a single file, they must all share the
67 # same formatter.
68 instrument = MegaPrime()
69 FormatterClass = instrument.getRawFormatter(datasets[0].dataId)
71 self.log.info(f"Found images for {len(datasets)} detectors in {filename}")
72 return RawFileData(datasets=datasets, filename=filename,
73 FormatterClass=FormatterClass,
74 instrumentClass=type(instrument))
77class MegacamParseTask(ParseTask):
79 def translate_ccd(self, md):
80 try:
81 extname = self.getExtensionName(md)
82 return int(extname[3:]) # chop off "ccd"
83 except LookupError:
84 # Dummy value, intended for PHU (need something to get filename)
85 return 99
87 def translate_filter(self, md):
88 filtName = md.getScalar("FILTER").strip()
89 if filtName not in filters:
90 return "UNKNOWN"
91 return filters[filtName]
93 def translate_taiObs(self, md):
94 # Field name is "taiObs" but we're giving it UTC; shouldn't matter so
95 # long as we're consistent
96 (yr, month, day) = (md.getScalar("DATE-OBS").strip()).split("-")
97 (hr, min, sec) = (md.getScalar("UTC-OBS").strip()).split(":")
98 (sec1, sec2) = sec.split('.')
99 return "%04d-%02d-%02dT%02d:%02d:%02d.%02d"%(int(yr), int(month), int(day),
100 int(hr), int(min), int(sec1), int(sec2))
102 def translate_defects(self, md):
103 maskName = md.getScalar("IMRED_MK").strip()
104 maskName, ccd = maskName.split(".fits")
105 filter = md.getScalar("FILTER").strip().split('.')[0]
106 if filter in ["i", "i2", "i3", "z", "z2"]:
107 maskName = maskName+"_enlarged"
108 maskFile = maskName+".nn/"+ccd[1:6]+".fits"
109 return maskFile
111 def getInfo(self, filename):
112 phuInfo, infoList = super(MegacamParseTask, self).getInfo(filename)
113 match = re.search(r"\d+(?P<state>o|p)\.fits.*", filename)
114 if not match:
115 raise RuntimeError("Unable to parse filename: %s" % filename)
116 phuInfo['state'] = match.group('state')
117 phuInfo['extension'] = 0
118 for num, info in enumerate(infoList):
119 info['state'] = match.group('state')
120 info['extension'] = num + 1
121 return phuInfo, infoList
123 def getExtensionName(self, md):
124 """Get the name of an extension.
126 Parameters
127 ----------
128 md : `PropertySet`
129 Metadata to get the name from.
131 Returns
132 -------
133 name : `str` or None
134 Name of the extension if it exists. None otherwise.
135 """
136 # We have to overwrite this method because some (mostly recent) Megacam
137 # images have a different header where the keword "EXTNAME" appears one
138 # time instead of two. In the later case ext is a tuple while in the
139 # other case it is a single value
140 try:
141 # This returns a tuple
142 ext = md.getScalar("EXTNAME")
143 # Most of the time the EXTNAME keyword appears 2 times in the
144 # header (1st time to specify that the image is compressed) but
145 # sometimes it appears only once even if the image is compressed
146 if type(ext) == tuple or type(ext) == list:
147 return ext[1]
148 else:
149 return ext
150 except lsst.pex.exceptions.Exception:
151 return None