Coverage for python/lsst/obs/sdss/convertfpM.py : 74%

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 2008, 2009, 2010 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#
23import sys
24import os
25import re
26from astropy.io import fits
28import lsst.afw.image as afwImage
29import lsst.geom as geom
32class Span(object):
34 def __init__(self, y, x1, x2):
35 self.y = y
36 self.x1 = x1
37 self.x2 = x2
40class Objmask(object):
41 nperspan = 6
43 def __init__(self, frow, cval, verbose=False):
44 self.refcntr = frow[0]
45 self.nspan = frow[1]
46 self.row0 = frow[2]
47 self.col0 = frow[3]
48 self.rmin = frow[4]
49 self.rmax = frow[5]
50 self.cmin = frow[6]
51 self.cmax = frow[7]
52 self.npix = frow[8]
53 self.span = frow[9]
54 if len(self.span) == 0: 54 ↛ 55line 54 didn't jump to line 55, because the condition on line 54 was never true
55 self.nspan = 0 # some bogus fpM files
57 self.spans = []
58 npixcheck = 0
59 for i in range(self.nspan):
60 b1 = self.span[6*i + 0]
61 b2 = self.span[6*i + 1]
62 y = (b1 << 8) + b2
63 b1 = self.span[6*i + 2]
64 b2 = self.span[6*i + 3]
65 x1 = (b1 << 8) + b2
66 b1 = self.span[6*i + 4]
67 b2 = self.span[6*i + 5]
68 x2 = (b1 << 8) + b2
69 self.spans.append(Span(y, x1, x2))
70 for i in range(x1, x2+1):
71 npixcheck += 1
73 # Some fpM files are actually wrong and this test fails!
74 # So warn, not assert
75 # 5759/40/objcs/1/fpM-005759-r1-0011.fit
76 # Plane S_MASK_NOTCHECKED
77 if self.npix != npixcheck and verbose: 77 ↛ 78line 77 didn't jump to line 78, because the condition on line 77 was never true
78 print("WARNING: npix != npixcheck (%d != %d)" % (self.npix, npixcheck))
80 self.cval = cval
82 def setMask(self, mask):
83 nrow = mask.getHeight()
84 ncol = mask.getWidth()
86 for i in range(self.nspan):
87 y = int(self.spans[i].y - self.row0)
89 if (y < 0 or y >= nrow): 89 ↛ 90line 89 didn't jump to line 90, because the condition on line 89 was never true
90 continue
92 x1 = self.spans[i].x1 - self.col0
93 x2 = self.spans[i].x2 - self.col0
95 if (x1 < 0): 95 ↛ 96line 95 didn't jump to line 96, because the condition on line 95 was never true
96 x1 = 0
98 if(x2 >= ncol): 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true
99 x2 = ncol - 1
101 mask.array[y, x1: x2 + 1] |= self.cval
104def convertfpM(infile, allPlanes=False):
105 with fits.open(infile) as hdr:
106 hdr[0].header['RUN']
107 hdr[0].header['CAMCOL']
108 hdr[0].header['FIELD']
109 nRows = hdr[0].header['MASKROWS']
110 nCols = hdr[0].header['MASKCOLS']
111 hdr[0].header['NPLANE']
113 names = hdr[-1].data.names
114 if ("attributeName" not in names) or ("Value" not in names): 114 ↛ 115line 114 didn't jump to line 115, because the condition on line 114 was never true
115 raise LookupError("Missing data in fpM header")
117 planes = hdr[-1].data.field("attributeName").tolist()
118 mask = afwImage.Mask(geom.ExtentI(nCols, nRows))
120 # Minimal sets of mask planes needed for LSST
121 interpPlane = planes.index("S_MASK_INTERP") + 1
122 satPlane = planes.index("S_MASK_SATUR") + 1
123 crPlane = planes.index("S_MASK_CR") + 1
125 interpBitMask = afwImage.Mask.getPlaneBitMask("INTRP")
126 satBitMask = afwImage.Mask.getPlaneBitMask("SAT")
127 crBitMask = afwImage.Mask.getPlaneBitMask("CR")
129 listToSet = [(interpPlane, interpBitMask),
130 (satPlane, satBitMask),
131 (crPlane, crBitMask)]
133 # Add the rest of the SDSS planes
134 if allPlanes: 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true
135 for plane in ['S_MASK_NOTCHECKED', 'S_MASK_OBJECT', 'S_MASK_BRIGHTOBJECT',
136 'S_MASK_BINOBJECT', 'S_MASK_CATOBJECT', 'S_MASK_SUBTRACTED', 'S_MASK_GHOST']:
137 idx = planes.index(plane) + 1
138 planeName = re.sub("S_MASK_", "", plane)
139 mask.addMaskPlane(planeName)
140 planeBitMask = afwImage.Mask.getPlaneBitMask(planeName)
141 listToSet.append((idx, planeBitMask))
143 for plane, bitmask in listToSet:
144 if len(hdr) < plane: 144 ↛ 145line 144 didn't jump to line 145, because the condition on line 144 was never true
145 continue
147 if hdr[plane].data is None: 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true
148 continue
150 nmask = len(hdr[plane].data)
151 for i in range(nmask):
152 frow = hdr[plane].data[i]
153 Objmask(frow, bitmask).setMask(mask)
155 return mask
158if __name__ == '__main__': 158 ↛ 159line 158 didn't jump to line 159, because the condition on line 158 was never true
159 infile = sys.argv[1]
160 outfile = sys.argv[2]
162 if not os.path.isfile(infile):
163 sys.exit(1)
165 convertfpM(infile).writeFits(outfile)
167 comparison = """ # noqa ignore the really long line
168import lsst.afw.image as afwImage
169import numpy as num
170import os
172readMask = "/astro/users/acbecker/LSST/lsst_devel/clue/Coadd/src/native/SDSS_PSFs/readAtlasImages_mod_by_Keith/src/read_mask"
173cmd = readMask + " %s INTERP /tmp/interp.fit"
174os.system(cmd)
175cmd = readMask + " %s SATUR /tmp/satur.fit"
176os.system(cmd)
177cmd = readMask + " %s CR /tmp/cr.fit"
178os.system(cmd)
180interp = afwImage.ImageU("/tmp/interp.fit")
181interp *= 2**2
183sat = afwImage.ImageU("/tmp/satur.fit")
184sat *= 2**1
186cr = afwImage.ImageU("/tmp/cr.fit")
187cr *= 2**3
189interp += sat
190interp += cr
192lsst = afwImage.ImageU("%s")
193interp -= lsst
194interp.writeFits("/tmp/mask_diff.fits")
195print len(num.where(interp.getArray() != 0)[0])
197""" % (infile, infile, infile, outfile)
198 print(comparison)