aboutsummaryrefslogtreecommitdiffstats
path: root/acispy/region.py
blob: dd98d20986a20bdda30252bf981e2fa69a6b9460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright (c) 2017 Weitian LI <liweitianux@live.com>
# MIT license

"""
Region utilities.

NOTE:
Only support the commonly used region shapes in CIAO format.
While more complex shapes and DS9 format are not considered
at the moment.
"""

import os
import re


class Point:
    """
    Point region: point(xc,yc)
    """
    shape = "point"

    def __init__(self, xc=None, yc=None):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])

    def __str__(self):
        return "%s(%.4f,%.4f)" % (self.shape, self.xc, self.yc)


class Circle:
    """
    Circle region: circle(xc,yc,radius)
    """
    shape = "circle"

    def __init__(self, xc=None, yc=None, radius=None):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc
            self.radius = radius

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])
        self.radius = float(g[3])

    def __str__(self):
        return "%s(%.4f,%.4f,%.4f)" % (self.shape, self.xc, self.yc,
                                       self.radius)


class Annulus:
    """
    Annulus region:
        annulus(xc,yc,radius,radius2)
        + xc, yc : center
        + radius, radius2 : inner, outer radius
    """
    shape = "annulus"

    def __init__(self, xc=None, yc=None, radius=None, radius2=None):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc
            self.radius = radius
            self.radius2 = radius2

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])
        self.radius = float(g[3])
        self.radius2 = float(g[4])

    def __str__(self):
        return "%s(%.4f,%.4f,%.4f,%.4f)" % (self.shape, self.xc, self.yc,
                                            self.radius, self.radius2)


class Pie:
    """
    Pie region:
        pie(xc,yc,radius,radius2,angle1,angle2)
        + xc, yc : center
        + radius, radius2 : inner, outer radius
        + angle1, angle2: start, end angle [0, 360)
    """
    shape = "pie"

    def __init__(self, xc=None, yc=None, radius=None, radius2=None,
                 angle1=0.0, angle2=360.0):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc
            self.radius = radius
            self.radius2 = radius2
            self.angle1 = angle1
            self.angle2 = angle2

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])
        self.radius = float(g[3])
        self.radius2 = float(g[4])
        self.angle1 = float(g[5])
        self.angle2 = float(g[6])

    def __str__(self):
        return "%s(%.4f,%.4f,%.4f,%.4f,%.4f,%.4f)" % (
            self.shape, self.xc, self.yc, self.radius, self.radius2,
            self.angle1, self.angle2)


class Ellipse:
    """
    Ellipse region:
        ellipse(xc,yc,radius,radius2,rotation)
        + xc, yc : center
        + radius, radius2 : semi-major / semi-minor axis
        + rotation: rotation angle [0, 360)
    """
    shape = "ellipse"

    def __init__(self, xc=None, yc=None, radius=None, radius2=None,
                 rotation=0.0):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc
            self.radius = radius
            self.radius2 = radius2
            self.rotation = rotation

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])
        self.radius = float(g[3])
        self.radius2 = float(g[4])
        self.rotation = float(g[5])

    def __str__(self):
        return "%s(%.4f,%.4f,%.4f,%.4f,%.4f)" % (
            self.shape, self.xc, self.yc, self.radius,
            self.radius2, self.rotation)


class Box:
    """
    Box region: box(xc,yc,width,height,rotation)
    """
    shape = "box"

    def __init__(self, xc=None, yc=None, width=None, height=None,
                 rotation=0.0):
        if isinstance(xc, str):
            self.parse(regstr=xc)
        else:
            self.xc = xc
            self.yc = yc
            self.width = width
            self.height = height
            self.rotation = rotation

    def parse(self, regstr):
        g = re.split(r"[(),]", regstr)
        self.xc = float(g[1])
        self.yc = float(g[2])
        self.width = float(g[3])
        self.height = float(g[4])
        self.rotation = float(g[5])

    def __str__(self):
        return "%s(%.4f,%.4f,%.4f,%.4f,%.4f)" % (
            self.shape, self.xc, self.yc, self.width,
            self.height, self.rotation)


class Regions:
    """
    Manipulate the region files (CIAO format), as well as parse regions
    from strings.
    """
    REGION_SHAPES = {
        "point": Point,
        "circle": Circle,
        "annulus": Annulus,
        "pie": Pie,
        "ellipse": Ellipse,
        "box": Box,
    }

    def __init__(self, regfile=None):
        if regfile:
            self.load(regfile)
        else:
            self.regions = []

    def load(self, infile):
        regstr = []
        for line in open(infile):
            if not (re.match(r"^\s*#.*$", line) or re.match(r"^\s*$", line)):
                regstr.append(line.strip())
        self.regions = self.parse(regstr)

    def save(self, outfile, clobber=False):
        if (not clobber) and os.path.exists(outfile):
            raise OSError("output file already exists: %s" % outfile)
        regstr = [str(reg) for reg in self.regions]
        open(outfile, "w").write("\n".join(regstr) + "\n")

    @classmethod
    def parse(self, regstr):
        """
        Parse the given (list of) region string(s), and return the
        corresponding parsed region objects.
        """
        if isinstance(regstr, list):
            return [self.parse_single(reg) for reg in regstr]
        else:
            return self.parse_single(regstr)

    @classmethod
    def parse_single(self, regstr):
        """
        Parse the given single region string to its corresponding
        region object.
        """
        shape = regstr.strip().split('(')[0].lower()
        if shape in self.REGION_SHAPES:
            return self.REGION_SHAPES[shape](regstr)
        else:
            raise ValueError("unknown region shape: %s" % shape)