28from lsst.daf.butler.formatters.file
import FileFormatter
32 """Class to support bright object masks
36 schema = afwTable.SimpleTable.makeMinimalSchema()
37 schema.addField(
"type", str,
"type of region (e.g. box, circle)", size=10)
38 schema.addField(
"radius",
"Angle",
"radius of mask (if type == circle")
39 schema.addField(
"height",
"Angle",
"height of mask (if type == box)")
40 schema.addField(
"width",
"Angle",
"width of mask (if type == box)")
41 schema.addField(
"angle",
"Angle",
"rotation of mask (if type == box)")
42 schema.addField(
"mag", float,
"object's magnitude")
44 self.
_catalog = afwTable.SimpleCatalog(schema)
45 self.
_catalog.table.setMetadata(dafBase.PropertyList())
64 """Read a ds9 region file, returning a ObjectMaskCatalog object
66 The files should be structured as follows:
76 circle(RA, DEC, RADIUS)
77 box(RA, DEC, XSIZE, YSIZE, THETA)
80 The
", mag: XX.YY" is optional
82 The commented lines must be present,
with the relevant fields such
as
83 tract patch
and filter filled
in. The coordinate system must be listed
84 as above. Each patch
is specified
as a box
or circle,
with RA, DEC,
85 and dimensions specified
in decimal degrees (
with or without an
88 Only (axis-aligned) boxes
and circles are currently supported
as
92 log = logging.getLogger("lsst.ObjectMaskCatalog")
95 checkedWcsIsFk5 =
False
96 NaN = float(
"NaN")*geom.degrees
99 with open(fileName)
as fd:
100 for lineNo, line
in enumerate(fd.readlines(), 1):
103 if re.search(
r"^\s*#", line):
112 mat = re.search(
r"^\s*#\s*([a-zA-Z][a-zA-Z0-9_]+)\s*:\s*(.*)", line)
114 key, value = mat.group(1).lower(), mat.group(2)
118 brightObjects.table.getMetadata().set(key, value)
120 line = re.sub(
r"^\s*#.*",
"", line)
124 if re.search(
r"^\s*wcs\s*;\s*fk5\s*$", line, re.IGNORECASE):
125 checkedWcsIsFk5 =
True
130 mat = re.search(
r"^\s*(box|circle)"
132 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
134 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
136 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
139 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
142 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
146 r"#\s*ID:[\w\s]*(\d+)"
147 r"(?:\s*,?\s*mag:\s*([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?))?"
150 _type, ra, raUnit, dec, decUnit, \
151 param1, param1Unit, param2, param2Unit, param3, param3Unit, \
152 _id, mag = mat.groups()
166 angle = 0.0*geom.degrees
169 width =
convertToAngle(param1, param1Unit,
"width", fileName, lineNo)
170 height =
convertToAngle(param2, param2Unit,
"height", fileName, lineNo)
171 if param3
is not None:
172 angle =
convertToAngle(param3, param3Unit,
"angle", fileName, lineNo)
175 log.warning(
"Rotated boxes are not supported: \"%s\" at %s:%d",
176 line, fileName, lineNo)
178 elif _type ==
"circle":
179 radius =
convertToAngle(param1, param1Unit,
"radius", fileName, lineNo)
181 if not (param2
is None and param3
is None):
182 log.warning(
"Extra parameters for circle: \"%s\" at %s:%d",
183 line, fileName, lineNo)
186 rec = brightObjects.addNew()
194 rec[
"height"] = height
196 rec[
"radius"] = radius
198 log.warning(
"Unexpected line \"%s\" at %s:%d", line, fileName, lineNo)
202 raise RuntimeError(
"Saw %d formatting errors in %s" % (nFormatError, fileName))
204 if not checkedWcsIsFk5:
205 raise RuntimeError(
"Expected to see a line specifying an fk5 wcs in %s" % fileName)
208 brightObjects._catalog = brightObjects._catalog.copy(
True)
214 """Given a variable and its units, return an geom.Angle
216 what, fileName, and lineNo are used to generate helpful error messages
220 if varUnit
in (
"d",
"",
None):
227 raise RuntimeError(
"unsupported unit \"%s\" for %s at %s:%d" %
228 (varUnit, what, fileName, lineNo))
230 return var*geom.degrees
234 """Plugin for reading DS9 region file catalogs with Gen3 Butler.
238 def _readFile(self, path, pytype):
240 if not os.path.exists(path):
243 return pytype.read(path)
245 def _writeFile(self, inMemoryDataset, fileDescriptor):
247 raise NotImplementedError(
"Write not implemented.")
def __setitem__(self, i, v)
def convertToAngle(var, varUnit, what, fileName, lineNo)