-
Notifications
You must be signed in to change notification settings - Fork 0
/
straytest.py
83 lines (70 loc) · 2.51 KB
/
straytest.py
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
"""
This is a testing script for combining different region files for use
in straycats.py. This is done using the shapely geometry package since
the python `regions` package has not yet supported operations to
combine regions.
"""
from regions import Regions
from shapely.geometry import Point
from shapely.plotting import plot_polygon
import matplotlib.pyplot as plt
# Read in a regions file and record its contents
filepath = "/Volumes/data_ssd_1/bifrost_data/straycat/40014024001A_StrayCatsI_260.reg"
region_list = Regions.read(filepath, format='ds9')
with open(filepath, 'r') as region_file:
reg_info = region_file.readlines()
# Initialize sequence of operations
construct = []
flag = False
for line in reg_info:
if flag:
if line[0] == '-':
construct.append('-')
else:
construct.append('+')
if line == "image\n":
flag = True
# If only one shape is in region file, create a simple circular
# mask to represent the region file.
if len(region_list) == 1:
pos = region_list[0].center.xy
radius = region_list[0].radius
src_x = pos[0]
src_y = pos[1]
mask = Point(src_x, src_y).buffer(radius, resolution=1000)
# Routine for combining multiple regions using the shapely package
if len(region_list) > 1:
pos = region_list[0].center.xy
radius = region_list[0].radius
src_x = pos[0]
src_y = pos[1]
mask = Point(src_x, src_y).buffer(radius, resolution=1000)
for idx, region in enumerate(region_list):
if idx == 0:
continue
# Combine two regions via a union
if construct[idx] == '+':
pos = region.center.xy
radius = region.radius
src_x = pos[0]
src_y = pos[1]
mask_add = Point(src_x, src_y).buffer(radius, resolution=1000)
mask = mask.union(mask_add)
# Subtract two regions via a difference
if construct[idx] == '-':
pos = region.center.xy
radius = region.radius
src_x = pos[0]
src_y = pos[1]
mask_subtract = Point(src_x, src_y).buffer(radius, resolution=1000)
mask = mask.difference(mask_subtract)
# Create a test plot to show the polygon in DET1 coordinates.
fig, ax = plt.subplots()
plot_polygon(mask, ax=ax, add_points=False)
plt.xlim(0, 300)
plt.ylim(0, 300)
plt.show()
# Test line for seeing if points are located within the plotted
# StrayCat region.
test_point = Point(22, ).buffer(1, resolution=1000)
print(mask.contains(test_point))