67 """Read a ds9 region file, returning a ObjectMaskCatalog object
69 The files should be structured as follows:
71 # Description of catalogue as a comment
72 # CATALOG: catalog-id-string
79 circle(RA, DEC, RADIUS) # ID: 1, mag: 12.34
80 box(RA, DEC, XSIZE, YSIZE, THETA) # ID: 2, mag: 23.45
83 The ", mag: XX.YY" is optional
85 The commented lines must be present, with the relevant fields such as
86 tract patch and filter filled in. The coordinate system must be listed
87 as above. Each patch is specified as a box or circle, with RA, DEC,
88 and dimensions specified in decimal degrees (with or without an
91 Only (axis-aligned) boxes and circles are currently supported as
95 log = logging.getLogger(
"lsst.ObjectMaskCatalog")
98 checkedWcsIsFk5 =
False
99 NaN = float(
"NaN")*geom.degrees
102 with open(fileName)
as fd:
103 for lineNo, line
in enumerate(fd.readlines(), 1):
106 if re.search(
r"^\s*#", line):
115 mat = re.search(
r"^\s*#\s*([a-zA-Z][a-zA-Z0-9_]+)\s*:\s*(.*)", line)
117 key, value = mat.group(1).lower(), mat.group(2)
121 brightObjects.table.getMetadata().set(key, value)
123 line = re.sub(
r"^\s*#.*",
"", line)
127 if re.search(
r"^\s*wcs\s*;\s*fk5\s*$", line, re.IGNORECASE):
128 checkedWcsIsFk5 =
True
133 mat = re.search(
r"^\s*(box|circle)"
135 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
137 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]*)"
145 r"([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?)([d]*)"
149 r"#\s*ID:[\w\s]*(\d+)"
150 r"(?:\s*,?\s*mag:\s*([+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?))?"
153 _type, ra, raUnit, dec, decUnit, \
154 param1, param1Unit, param2, param2Unit, param3, param3Unit, \
155 _id, mag = mat.groups()
169 angle = 0.0*geom.degrees
172 width =
convertToAngle(param1, param1Unit,
"width", fileName, lineNo)
173 height =
convertToAngle(param2, param2Unit,
"height", fileName, lineNo)
174 if param3
is not None:
175 angle =
convertToAngle(param3, param3Unit,
"angle", fileName, lineNo)
178 log.warning(
"Rotated boxes are not supported: \"%s\" at %s:%d",
179 line, fileName, lineNo)
181 elif _type ==
"circle":
182 radius =
convertToAngle(param1, param1Unit,
"radius", fileName, lineNo)
184 if not (param2
is None and param3
is None):
185 log.warning(
"Extra parameters for circle: \"%s\" at %s:%d",
186 line, fileName, lineNo)
189 rec = brightObjects.addNew()
197 rec[
"height"] = height
199 rec[
"radius"] = radius
201 log.warning(
"Unexpected line \"%s\" at %s:%d", line, fileName, lineNo)
205 raise RuntimeError(
"Saw %d formatting errors in %s" % (nFormatError, fileName))
207 if not checkedWcsIsFk5:
208 raise RuntimeError(
"Expected to see a line specifying an fk5 wcs in %s" % fileName)
211 brightObjects._catalog = brightObjects._catalog.copy(
True)