9 """Class to support bright object masks 11 N.b. I/O is done by providing a readFits method which fools the butler. 15 schema = afwTable.SimpleTable.makeMinimalSchema()
16 schema.addField(
"type", str,
"type of region (e.g. box, circle)", size=10)
17 schema.addField(
"radius",
"Angle",
"radius of mask (if type == circle")
18 schema.addField(
"height",
"Angle",
"height of mask (if type == box)")
19 schema.addField(
"width",
"Angle",
"width of mask (if type == box)")
20 schema.addField(
"angle",
"Angle",
"rotation of mask (if type == box)")
21 schema.addField(
"mag", float,
"object's magnitude")
23 self.
_catalog = afwTable.SimpleCatalog(schema)
24 self.
_catalog.table.setMetadata(dafBase.PropertyList())
43 """Read a ds9 region file, returning a ObjectMaskCatalog object 45 This method is called "readFits" to fool the butler. The corresponding mapper entry looks like 47 template: "deepCoadd/BrightObjectMasks/%(tract)d/BrightObjectMask-%(tract)d-%(patch)s-%(filter)s.reg" # noqa E501 48 python: "lsst.obs.subaru.objectMasks.ObjectMaskCatalog" 49 persistable: "PurePythonClass" 50 storage: "FitsCatalogStorage" 52 and this is the only way I know to get it to read a random file type, in this case a ds9 region file. 54 This method expects to find files named as BrightObjectMask-%(tract)d-%(patch)s-%(filter)s.reg 55 The files should be structured as follows: 57 # Description of catalogue as a comment 58 # CATALOG: catalog-id-string 65 circle(RA, DEC, RADIUS) # ID: 1, mag: 12.34 66 box(RA, DEC, XSIZE, YSIZE, THETA) # ID: 2, mag: 23.45 69 The ", mag: XX.YY" is optional 71 The commented lines must be present, with the relevant fields such as tract patch and filter filled 72 in. The coordinate system must be listed as above. Each patch is specified as a box or circle, with 73 RA, DEC, and dimensions specified in decimal degrees (with or without an explicit "d"). 75 Only (axis-aligned) boxes and circles are currently supported as region definitions. 78 log = Log.getLogger(
"ObjectMaskCatalog")
81 checkedWcsIsFk5 =
False 82 NaN = float(
"NaN")*afwGeom.degrees
85 with open(fileName)
as fd:
86 for lineNo, line
in enumerate(fd.readlines(), 1):
89 if re.search(
r"^\s*#", line):
98 mat = re.search(
r"^\s*#\s*([a-zA-Z][a-zA-Z0-9_]+)\s*:\s*(.*)", line)
100 key, value = mat.group(1).lower(), mat.group(2)
104 brightObjects.table.getMetadata().set(key, value)
106 line = re.sub(
r"^\s*#.*",
"", line)
110 if re.search(
r"^\s*wcs\s*;\s*fk5\s*$", line, re.IGNORECASE):
111 checkedWcsIsFk5 =
True 116 mat = re.search(
r"^\s*(box|circle)" 118 "(\d+(?:\.\d*)?([d]*))" "(?:\s+|\s*,\s*)" 119 "([+-]?\d+(?:\.\d*)?)([d]*)" "(?:\s+|\s*,\s*)" 120 "([+-]?\d+(?:\.\d*)?)([d]*)" "(?:\s+|\s*,\s*)?" 121 "(?:([+-]?\d+(?:\.\d*)?)([d]*)" 123 "([+-]?\d+(?:\.\d*)?)([d]*)" 127 "(?:\s*,\s*mag:\s*(\d+\.\d*))?" 130 _type, ra, raUnit, dec, decUnit, \
131 param1, param1Unit, param2, param2Unit, param3, param3Unit, \
132 _id, mag = mat.groups()
149 width =
convertToAngle(param1, param1Unit,
"width", fileName, lineNo)
150 height =
convertToAngle(param2, param2Unit,
"height", fileName, lineNo)
151 angle =
convertToAngle(param3, param3Unit,
"angle", fileName, lineNo)
154 log.warn(
"Rotated boxes are not supported: \"%s\" at %s:%d" % (
155 line, fileName, lineNo))
157 elif _type ==
"circle":
158 radius =
convertToAngle(param1, param1Unit,
"radius", fileName, lineNo)
160 if not (param2
is None and param3
is None):
161 log.warn(
"Extra parameters for circle: \"%s\" at %s:%d" % (
162 line, fileName, lineNo))
165 rec = brightObjects.addNew()
170 rec.setCoord(afwGeom.SpherePoint(ra, dec))
173 rec[
"height"] = height
175 rec[
"radius"] = radius
177 log.warn(
"Unexpected line \"%s\" at %s:%d" % (line, fileName, lineNo))
181 raise RuntimeError(
"Saw %d formatting errors in %s" % (nFormatError, fileName))
183 if not checkedWcsIsFk5:
184 raise RuntimeError(
"Expected to see a line specifying an fk5 wcs in %s" % fileName)
187 brightObjects._catalog = brightObjects._catalog.copy(
True)
193 """Given a variable and its units, return an afwGeom.Angle 195 what, fileName, and lineNo are used to generate helpful error messages 199 if varUnit
in (
"d",
"",
None):
206 raise RuntimeError(
"unsupported unit \"%s\" for %s at %s:%d" %
207 (varUnit, what, fileName, lineNo))
209 return var*afwGeom.degrees
def convertToAngle(var, varUnit, what, fileName, lineNo)
def readFits(fileName, hdu=0, flags=0)
def __setitem__(self, i, v)