Skip to content

Commit

Permalink
fix #134
Browse files Browse the repository at this point in the history
  • Loading branch information
joleroi committed May 18, 2017
1 parent 46a0743 commit e3afa19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
39 changes: 19 additions & 20 deletions regions/io/ds9/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ def parse_line(self, line):
"the known region types.".format(region_type))
return
else:
# Found region specification
self.parse_region(include, region_type, line)
# Found region specification,
region_end = region_type_search.span()[1]
self.parse_region(include, region_type, region_end, line)

def _raise_error(self, msg):
if self.errors == 'warn':
Expand Down Expand Up @@ -264,14 +265,18 @@ def parse_meta(meta_str):

return result

def parse_region(self, include, region_type, line):
def parse_region(self, include, region_type, region_end, line):
"""Extract a Shape from a region string"""
if self.coordsys is None:
raise DS9RegionParserError("No coordinate system specified and a"
" region has been found.")
else:
helper = DS9RegionParser(self.coordsys, region_type,
self.global_meta, line)
helper = DS9RegionParser(coordsys=self.coordsys,
include=include,
region_type=region_type,
region_end=region_end,
global_meta=self.global_meta,
line=line)
helper.parse()
self.shapes.append(helper.shape)

Expand All @@ -297,6 +302,9 @@ class DS9RegionParser(object):
Flag at the beginning of the line
region_type : str
Region type
region_end : int
Coordinate of the end of the regions name, this is passed in order to
handle whitespaces correctly
global_meta : dict
Global meta data
line : str
Expand Down Expand Up @@ -328,16 +336,17 @@ class DS9RegionParser(object):
}
"""DS9 language specification. This defines how a certain region is read"""

def __init__(self, coordsys, region_type, global_meta, line):
def __init__(self, coordsys, include, region_type, region_end, global_meta, line):

self.coordsys = coordsys
self.include = include
self.region_type = region_type
self.region_end = region_end
self.global_meta = global_meta
self.line = line

self.meta_str = None
self.coord_str = None
self.include = None
self.composite = None
self.coord = None
self.meta = None
Expand All @@ -346,6 +355,7 @@ def __init__(self, coordsys, region_type, global_meta, line):
def __str__(self):
ss = self.__class__.__name__
ss += '\nLine : {}'.format(self.line)
ss += '\nRegion end : {}'.format(self.region_end)
ss += '\nMeta string : {}'.format(self.meta_str)
ss += '\nCoord string: {}'.format(self.coord_str)
ss += '\nShape: {}'.format(self.shape)
Expand All @@ -357,7 +367,6 @@ def parse(self):
log.debug(self)

self.parse_composite()
self.parse_include()
self.split_line()
self.convert_coordinates()
self.convert_meta()
Expand All @@ -368,21 +377,11 @@ def parse_composite(self):
"""Determine whether the region is composite"""
self.composite = "||" in self.line

def parse_include(self):
"""Determine wether to include/exclude the region"""
if self.line[0] == '-':
self.include = False
else:
self.include = True

def split_line(self):
"""Split line into coordinates and meta string"""
end_of_region_name = len(self.region_type)
if not self.include:
end_of_region_name += 1
# coordinate of the # symbol or end of the line (-1) if not found
hash_or_end = self.line.find("#")
temp = self.line[end_of_region_name:hash_or_end].strip(" |")
temp = self.line[self.region_end:hash_or_end].strip(" |")
self.coord_str = regex_paren.sub("", temp)
self.meta_str = self.line[hash_or_end:]

Expand Down Expand Up @@ -423,5 +422,5 @@ def make_shape(self):
coord=self.coord,
meta=self.meta,
composite=self.composite,
include=self.include
include=self.include != '-',
)
10 changes: 2 additions & 8 deletions regions/io/ds9/tests/test_ds9_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,10 @@ def test_ds9_string_to_objects():
def test_ds9_string_to_objects_whitespace():
"""Simple test case for ds9_string_to_objects
"""
ds9_str = '# Region file format: DS9 astropy/regions\nfk5\n circle(42.0000,43.0000,3.0000)\n'
ds9_str = '# Region file format: DS9 astropy/regions\nfk5\n -circle(42.0000,43.0000,3.0000)\n'
parser = DS9Parser(ds9_str)
parser.run()
regions = parser.shapes.to_region()
reg = regions[0]

assert_allclose(reg.center.ra.deg, 42)
assert_allclose(reg.center.dec.deg, 43)
assert_allclose(reg.radius.value, 3)

assert parser.shapes[0].include == False

def test_ds9_io(tmpdir):
"""Simple test case for write_ds9 and read_ds9.
Expand Down

0 comments on commit e3afa19

Please sign in to comment.