Skip to content

Commit

Permalink
add line and annunlus to parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joleroi committed May 9, 2017
1 parent 2d37fd3 commit 66a6cf2
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions regions/io/read_ds9.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from astropy import log
from astropy.utils.exceptions import AstropyUserWarning
from warnings import warn
from ..shapes import circle, rectangle, polygon, ellipse, point
from ..shapes import circle, rectangle, polygon, ellipse, point, line, annulus
from ..core import PixCoord

__all__ = [
Expand Down Expand Up @@ -123,6 +123,8 @@ def parse_angular_length_quantity(string_rep):
'ellipse': itertools.chain((coordinate, coordinate), itertools.cycle((radius,))),
'box': (coordinate, coordinate, width, height, angle),
'polygon': itertools.cycle((coordinate,)),
'line': (coordinate, coordinate, coordinate, coordinate),
'annulus': itertools.chain((coordinate, coordinate), itertools.cycle((radius,))),
}

coordinate_systems = ['fk5', 'fk4', 'icrs', 'galactic', 'wcs', 'physical', 'image', 'ecliptic']
Expand Down Expand Up @@ -214,6 +216,23 @@ def ds9_region_list_to_objects(region_list):
reg = point.PointPixelRegion(coord_list[0])
else:
raise DS9RegionParserError("No central coordinate")
elif region_type == 'line':
if isinstance(coord_list[0], BaseCoordinateFrame):
reg = line.LineSkyRegion(coord_list[0], coord_list[1])
elif isinstance(coord_list[0], PixCoord):
reg = line.LinePixelRegion(coord_list[0], coord_list[1])
else:
raise DS9RegionParserError("No central coordinate")
elif region_type == 'annnulus':
# Do not read more than one annulus for now
if len(coord_list) > 3:
continue
if isinstance(coord_list[0], BaseCoordinateFrame):
reg = line.AnnulusSkyRegion(coord_list[0], coord_list[1], coord_list[2])
elif isinstance(coord_list[0], PixCoord):
reg = line.AnnulusPixelRegion(coord_list[0], coord_list[1], coord_list[3])
else:
raise DS9RegionParserError("No central coordinate")
else:
# Note: this should effectively never happen, because it would
# imply that the line_parser found a region that didn't match the
Expand Down Expand Up @@ -401,8 +420,8 @@ def line_parser(line, coordsys=None, errors='strict'):
parsed = type_parser(coords_etc, language_spec[region_type],
coordsys_name_mapping[coordsys])

# Reset iterator for ellipse annulus
if region_type == 'ellipse':
# Reset iterator for ellipse annulus and annulus
if region_type in ['ellipse', 'annulus']:
language_spec[region_type] = itertools.chain((coordinate, coordinate), itertools.cycle((radius,)))

parsed_angles = [(x, y)
Expand All @@ -416,12 +435,14 @@ def line_parser(line, coordsys=None, errors='strict'):
if hasattr(lon, '__len__') and hasattr(lon, '__lat__') and len(lon) == 1 and len(lat==1):
# force entries to be scalar if they are length-1
lon, lat = u.Quantity(lon[0]), u.Quantity(lat[0])
elif region_type == 'line':

# line constructor expectes two scalar coordinates
else:
# otherwise, they are vector quantitites
lon, lat = u.Quantity(lon), u.Quantity(lat)
sphcoords = coordinates.UnitSphericalRepresentation(lon, lat)
coords = frame(sphcoords)

return region_type, [coords] + parsed[len(coords) * 2:], parsed_meta, composite, include
else:
parsed = type_parser(coords_etc, language_spec[region_type],
Expand All @@ -431,13 +452,18 @@ def line_parser(line, coordsys=None, errors='strict'):
# b/c can't typecheck when iterating as in sky coord case
coord = PixCoord(parsed[0::2], parsed[1::2])
parsed_return = [coord]
elif region_type == 'line':
# special case line because it contains two coordinate points
coord1 = PixCoord(parsed[0], parsed[1])
coord2 = PixCoord(parsed[2], parsed[3])
parsed_return = [coord1, coord2] + parsed[4:]
else:
parsed = [_.value for _ in parsed]
coord = PixCoord(parsed[0], parsed[1])
parsed_return = [coord] + parsed[2:]

# Reset iterator for ellipse annulus
if region_type == 'ellipse':
# Reset iterator for ellipse annulus, annulus
if region_type in ['ellipse', 'annulus']:
language_spec[region_type] = itertools.chain((coordinate, coordinate), itertools.cycle((radius,)))

return region_type, parsed_return, parsed_meta, composite, include
Expand Down

0 comments on commit 66a6cf2

Please sign in to comment.