Skip to content

Commit

Permalink
Add Defects method for reading defects from text file
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Apr 23, 2019
1 parent 2035cac commit ca728a5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
39 changes: 39 additions & 0 deletions python/lsst/meas/algorithms/defects.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,42 @@ def readFits(cls, *args):
"""
table = lsst.afw.table.BaseCatalog.readFits(*args)
return cls.fromTable(table)

@classmethod
def readLsstDefectsFile(cls, filename):
"""Read defects information from an LSST format text file.
Parameters
----------
filename : `str`
Name of text file containing the defect information.
Returns
-------
defects : `Defects`
The defects.
Notes
-----
These defect text files are used as the human readable definitions
of defects in calibration data definition repositories. The format
is to use four columns defined as follows:
x0 : `int`
X coordinate of bottom left corner of box.
y0 : `int`
Y coordinate of bottom left corner of box.
width : `int`
X extent of the box.
height : `int`
Y extent of the box.
"""
# Use loadtxt so that ValueError is thrown if the file contains a
# non-integer value. genfromtxt converts bad values to -1.
defect_array = np.loadtxt(filename,
dtype=[("x0", "int"), ("y0", "int"),
('x_extent', '<i8'), ('y_extent', '<i8')])

return Defects(lsst.geom.Box2I(lsst.geom.Point2I(row["x0"], row["y0"]),
lsst.geom.Extent2I(row["x_extent"], row["y_extent"]))
for row in defect_array)
22 changes: 22 additions & 0 deletions tests/test_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ def test_defects(self):
with self.assertRaises(ValueError):
defects.append("defect")

def testLsstTextfile(self):
with lsst.utils.tests.getTempFilePath(".txt") as tmpFile:
with open(tmpFile, "w") as fh:
print("""# X0 Y0 width height
996 0 56 24
0 4156 2048 20
0 0 17 4176
1998 4035 50 141
1023 0 2 4176
2027 0 21 4176
0 4047 37 129
# Some rows without fixed column widths
14 20 2000 50
10 10 10 10
""", file=fh)

defects = algorithms.Defects.readLsstDefectsFile(tmpFile)

self.assertEqual(len(defects), 9)
self.assertEqual(defects[3].getBBox(), lsst.geom.Box2I(lsst.geom.Point2I(1998, 4035),
lsst.geom.Extent2I(50, 141)))


class InterpolationTestCase(lsst.utils.tests.TestCase):
"""A test case for interpolation."""
Expand Down

0 comments on commit ca728a5

Please sign in to comment.