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# 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# 

22 

23__all__ = ["MegacamParseTask"] 

24 

25import re 

26 

27from lsst.pipe.tasks.ingest import ParseTask 

28import lsst.pex.exceptions 

29 

30filters = {'u.MP9301': 'u', 

31 'u.MP9302': 'u2', 

32 'g.MP9401': 'g', 

33 'g.MP9402': 'g2', 

34 'r.MP9601': 'r', 

35 'r.MP9602': 'r2', 

36 'i.MP9701': 'i', 

37 'i.MP9702': 'i2', 

38 'i.MP9703': 'i3', 

39 'z.MP9801': 'z', 

40 'z.MP9901': 'z2', 

41 } 

42 

43 

44class MegacamParseTask(ParseTask): 

45 

46 def translate_ccd(self, md): 

47 try: 

48 extname = self.getExtensionName(md) 

49 return int(extname[3:]) # chop off "ccd" 

50 except LookupError: 

51 # Dummy value, intended for PHU (need something to get filename) 

52 return 99 

53 

54 def translate_filter(self, md): 

55 filtName = md.getScalar("FILTER").strip() 

56 if filtName not in filters: 

57 return "UNKNOWN" 

58 return filters[filtName] 

59 

60 def translate_taiObs(self, md): 

61 # Field name is "taiObs" but we're giving it UTC; shouldn't matter so 

62 # long as we're consistent 

63 (yr, month, day) = (md.getScalar("DATE-OBS").strip()).split("-") 

64 (hr, min, sec) = (md.getScalar("UTC-OBS").strip()).split(":") 

65 (sec1, sec2) = sec.split('.') 

66 return "%04d-%02d-%02dT%02d:%02d:%02d.%02d"%(int(yr), int(month), int(day), 

67 int(hr), int(min), int(sec1), int(sec2)) 

68 

69 def translate_defects(self, md): 

70 maskName = md.getScalar("IMRED_MK").strip() 

71 maskName, ccd = maskName.split(".fits") 

72 filter = md.getScalar("FILTER").strip().split('.')[0] 

73 if filter in ["i", "i2", "i3", "z", "z2"]: 

74 maskName = maskName+"_enlarged" 

75 maskFile = maskName+".nn/"+ccd[1:6]+".fits" 

76 return maskFile 

77 

78 def getInfo(self, filename): 

79 phuInfo, infoList = super(MegacamParseTask, self).getInfo(filename) 

80 match = re.search(r"\d+(?P<state>o|p)\.fits.*", filename) 

81 if not match: 

82 raise RuntimeError("Unable to parse filename: %s" % filename) 

83 phuInfo['state'] = match.group('state') 

84 phuInfo['extension'] = 0 

85 for num, info in enumerate(infoList): 

86 info['state'] = match.group('state') 

87 info['extension'] = num + 1 

88 return phuInfo, infoList 

89 

90 def getExtensionName(self, md): 

91 """Get the name of an extension. 

92 

93 Parameters 

94 ---------- 

95 md : `PropertySet` 

96 Metadata to get the name from. 

97 

98 Returns 

99 ------- 

100 name : `str` or None 

101 Name of the extension if it exists. None otherwise. 

102 """ 

103 # We have to overwrite this method because some (mostly recent) Megacam 

104 # images have a different header where the keword "EXTNAME" appears one 

105 # time instead of two. In the later case ext is a tuple while in the 

106 # other case it is a single value 

107 try: 

108 # This returns a tuple 

109 ext = md.getScalar("EXTNAME") 

110 # Most of the time the EXTNAME keyword appears 2 times in the 

111 # header (1st time to specify that the image is compressed) but 

112 # sometimes it appears only once even if the image is compressed 

113 if type(ext) == tuple or type(ext) == list: 

114 return ext[1] 

115 else: 

116 return ext 

117 except lsst.pex.exceptions.Exception: 

118 return None