Coverage for python/lsst/obs/decam/ingest.py: 20%

101 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-11 03:43 -0700

1# 

2# LSST Data Management System 

3# Copyright 2012,2015 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# 

22import os 

23import re 

24 

25from deprecated.sphinx import deprecated 

26 

27from astro_metadata_translator import fix_header, DecamTranslator 

28from lsst.afw.fits import readMetadata 

29from lsst.pipe.tasks.ingest import ParseTask, IngestTask, IngestArgumentParser 

30import lsst.obs.base 

31 

32__all__ = ["DecamRawIngestTask", "DecamIngestArgumentParser", "DecamIngestTask", "DecamParseTask"] 

33 

34 

35@deprecated(reason="DECam no longer requires a specialist gen3 ingest task. Please use the default." 

36 " Will be removed after v23.", 

37 category=FutureWarning, 

38 version="23.0") 

39class DecamRawIngestTask(lsst.obs.base.RawIngestTask): 

40 """Task for ingesting raw DECam data into a Gen3 Butler repository. 

41 """ 

42 

43 

44class DecamIngestArgumentParser(IngestArgumentParser): 

45 """Gen2 DECam ingest additional arguments. 

46 """ 

47 

48 def __init__(self, *args, **kwargs): 

49 super(DecamIngestArgumentParser, self).__init__(*args, **kwargs) 

50 self.add_argument("--filetype", default="raw", choices=["instcal", "raw"], 

51 help="Data processing level of the files to be ingested") 

52 

53 

54class DecamIngestTask(IngestTask): 

55 """Gen2 DECam file ingest task. 

56 """ 

57 ArgumentParser = DecamIngestArgumentParser 

58 

59 def __init__(self, *args, **kwargs): 

60 super(DecamIngestTask, self).__init__(*args, **kwargs) 

61 

62 def run(self, args): 

63 """Ingest all specified files and add them to the registry 

64 """ 

65 if args.filetype == "instcal": 

66 root = args.input 

67 with self.register.openRegistry(root, create=args.create, dryrun=args.dryrun) as registry: 

68 for infile in args.files: 

69 fileInfo, hduInfoList = self.parse.getInfo(infile, args.filetype) 

70 if len(hduInfoList) > 0: 

71 outfileInstcal = os.path.join(root, self.parse.getDestination(args.butler, 

72 hduInfoList[0], 

73 infile, "instcal")) 

74 outfileDqmask = os.path.join(root, self.parse.getDestination(args.butler, 

75 hduInfoList[0], infile, 

76 "dqmask")) 

77 outfileWtmap = os.path.join(root, self.parse.getDestination(args.butler, 

78 hduInfoList[0], infile, 

79 "wtmap")) 

80 

81 ingestedInstcal = self.ingest(fileInfo["instcal"], outfileInstcal, 

82 mode=args.mode, dryrun=args.dryrun) 

83 ingestedDqmask = self.ingest(fileInfo["dqmask"], outfileDqmask, 

84 mode=args.mode, dryrun=args.dryrun) 

85 ingestedWtmap = self.ingest(fileInfo["wtmap"], outfileWtmap, 

86 mode=args.mode, dryrun=args.dryrun) 

87 

88 if not (ingestedInstcal or ingestedDqmask or ingestedWtmap): 

89 continue 

90 

91 for info in hduInfoList: 

92 self.register.addRow(registry, info, dryrun=args.dryrun, create=args.create) 

93 

94 elif args.filetype == "raw": 

95 IngestTask.run(self, args) 

96 

97 

98class DecamParseTask(ParseTask): 

99 """Parse an image filename to get the required information to 

100 put the file in the correct location and populate the registry. 

101 """ 

102 

103 _translatorClass = DecamTranslator 

104 

105 def __init__(self, *args, **kwargs): 

106 super(ParseTask, self).__init__(*args, **kwargs) 

107 

108 self.expnumMapper = None 

109 

110 # Note that these should be syncronized with the fields in 

111 # root.register.columns defined in config/ingest.py 

112 self.instcalPrefix = "instcal" 

113 self.dqmaskPrefix = "dqmask" 

114 self.wtmapPrefix = "wtmap" 

115 

116 def _listdir(self, path, prefix): 

117 for file in os.listdir(path): 

118 fileName = os.path.join(path, file) 

119 md = readMetadata(fileName) 

120 fix_header(md, translator_class=self._translatorClass) 

121 if "EXPNUM" not in md: 

122 return 

123 expnum = md["EXPNUM"] 

124 if expnum not in self.expnumMapper: 

125 self.expnumMapper[expnum] = {self.instcalPrefix: None, 

126 self.wtmapPrefix: None, 

127 self.dqmaskPrefix: None} 

128 self.expnumMapper[expnum][prefix] = fileName 

129 

130 def buildExpnumMapper(self, basepath): 

131 """Extract exposure numbers from filenames to set self.expnumMapper 

132 

133 Parameters 

134 ---------- 

135 basepath : `str` 

136 Location on disk of instcal, dqmask, and wtmap subdirectories. 

137 """ 

138 self.expnumMapper = {} 

139 

140 instcalPath = basepath 

141 dqmaskPath = re.sub(self.instcalPrefix, self.dqmaskPrefix, instcalPath) 

142 wtmapPath = re.sub(self.instcalPrefix, self.wtmapPrefix, instcalPath) 

143 if instcalPath == dqmaskPath: 

144 raise RuntimeError("instcal and mask directories are the same") 

145 if instcalPath == wtmapPath: 

146 raise RuntimeError("instcal and weight map directories are the same") 

147 

148 if not os.path.isdir(dqmaskPath): 

149 raise OSError("Directory %s does not exist" % (dqmaskPath)) 

150 if not os.path.isdir(wtmapPath): 

151 raise OSError("Directory %s does not exist" % (wtmapPath)) 

152 

153 # Traverse each directory and extract the expnums 

154 for path, prefix in zip((instcalPath, dqmaskPath, wtmapPath), 

155 (self.instcalPrefix, self.dqmaskPrefix, self.wtmapPrefix)): 

156 self._listdir(path, prefix) 

157 

158 def getInfo(self, filename, filetype="raw"): 

159 """Get metadata header info from multi-extension FITS decam image file. 

160 

161 The science pixels, mask, and weight (inverse variance) are 

162 stored in separate files each with a unique name but with a 

163 common unique identifier EXPNUM in the FITS header. We have 

164 to aggregate the 3 filenames for a given EXPNUM and return 

165 this information along with that returned by the base class. 

166 

167 Parameters 

168 ---------- 

169 filename : `str` 

170 Image file to retrieve info from. 

171 filetype : `str` 

172 One of "raw" or "instcal". 

173 

174 Returns 

175 ------- 

176 phuInfo : `dict` 

177 Primary header unit info. 

178 infoList : `list` of `dict` 

179 Info for the other HDUs. 

180 

181 Notes 

182 ----- 

183 For filetype="instcal", we expect a directory structure that looks 

184 like the following: 

185 

186 .. code-block:: none 

187 

188 dqmask/ 

189 instcal/ 

190 wtmap/ 

191 

192 The user creates the registry by running: 

193 

194 .. code-block:: none 

195 

196 ingestImagesDecam.py outputRepository --filetype=instcal --mode=link instcal/*fits 

197 """ 

198 if filetype == "instcal": 

199 if self.expnumMapper is None: 

200 self.buildExpnumMapper(os.path.dirname(os.path.abspath(filename))) 

201 

202 # Note that phuInfo will have 

203 # 'side': 'X', 'ccd': 0 

204 phuInfo, infoList = super(DecamParseTask, self).getInfo(filename) 

205 expnum = phuInfo["visit"] 

206 phuInfo[self.instcalPrefix] = self.expnumMapper[expnum][self.instcalPrefix] 

207 phuInfo[self.dqmaskPrefix] = self.expnumMapper[expnum][self.dqmaskPrefix] 

208 phuInfo[self.wtmapPrefix] = self.expnumMapper[expnum][self.wtmapPrefix] 

209 for info in infoList: 

210 expnum = info["visit"] 

211 info[self.instcalPrefix] = self.expnumMapper[expnum][self.instcalPrefix] 

212 info[self.dqmaskPrefix] = self.expnumMapper[expnum][self.dqmaskPrefix] 

213 info[self.wtmapPrefix] = self.expnumMapper[expnum][self.wtmapPrefix] 

214 

215 elif filetype == "raw": 

216 phuInfo, infoList = super(DecamParseTask, self).getInfo(filename) 

217 for info in infoList: 

218 info[self.instcalPrefix] = "" 

219 info[self.dqmaskPrefix] = "" 

220 info[self.wtmapPrefix] = "" 

221 

222 # Some data IDs can not be extracted from the zeroth extension 

223 # of the MEF. Add them so Butler does not try to find them 

224 # in the registry which may still yet to be created. 

225 for key in ("ccdnum", "hdu", "ccd", "calib_hdu"): 

226 if key not in phuInfo: 

227 phuInfo[key] = 0 

228 

229 return phuInfo, infoList 

230 

231 def getDestination(self, butler, info, filename, filetype="raw"): 

232 """Get destination for the file 

233 

234 Parameters 

235 ---------- 

236 butler : `lsst.daf.persistence.Butler` 

237 Data butler. 

238 info : data ID 

239 File properties, used as dataId for the butler. 

240 filename : `str` 

241 Input filename. 

242 

243 Returns 

244 ------- 

245 raw : `str` 

246 Destination filename. 

247 """ 

248 raw = butler.get("%s_filename"%(filetype), info)[0] 

249 # Ensure filename is devoid of cfitsio directions about HDUs 

250 c = raw.find("[") 

251 if c > 0: 

252 raw = raw[:c] 

253 return raw