forked from ACTCollaboration/enlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
targets.py
84 lines (77 loc) · 2.92 KB
/
targets.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
84
# This module takes a set of equatorial pointings and classifies which
# of several fixed or moving targets it belongs to. The highest priority
# target which overlaps with the pointing provided will be used.
import ephem, numpy as np
import utils
class FixedPatch:
def __init__(self, name, center, size):
self.name = name
self.center = center
self.size = size
def match(self, point, margin=0):
return self.distance(point) - margin <= 0
# What is the minimal separation between point
# the patch?
def distance(self, point):
# Normalize pointing, so that it is comparable
# with our center position. Since the patch is
# rectangular, we will use a rectangular sense of
# distance. That is, the distance between A and B
# is simply |A.x-B.x| + |A.y-B.y|. Negative distance
# means that we are inside the object.
diff = np.abs(utils.rewind(point[:,1:]-self.center, 0, 2*np.pi))
return np.min(np.sum(np.maximum(diff-self.size,0),1))
eph = {name.lower(): ephem.__dict__[name] for name in ["Sun","Moon","Mercury","Venus","Mars","Jupiter","Saturn","Uranus","Neptune"]}
class EphemObj:
def __init__(self, name, size):
self.name = name
self.eph = eph[name.lower()]()
self.size = size
def match(self, point, margin=0):
return self.distance(point) - self.size - margin < 0
def distance(self, point, exact=True):
mjd = point[0,0]
djd = mjd + 2400000.5 - 2415020
self.eph.compute(djd)
pos = np.array([float(self.eph.ra), float(self.eph.dec)])
# We assume that only points close to the object are
# relevant, so use flat sky approximation
diff = utils.rewind(point[:,1:]-pos, 0, 2*np.pi)
return np.min(np.sum(diff**2,1)**0.5)
class TargetDB:
def __init__(self, fname):
self.targets = []
self.pris = []
for line in open(fname,"r"):
if line.isspace() or line[0] == "#": continue
toks = line.split()
name = toks[0]
pri = float(toks[1])
kind = toks[2]
if kind == "fixed":
pos = np.array((float(toks[3]),float(toks[4])))*utils.degree
size = np.array((float(toks[5]),float(toks[6])))*utils.degree
self.targets.append(FixedPatch(name, pos, size))
elif kind == "ephem":
size = float(toks[3])*np.pi/180 if len(toks) > 3 else utils.degree
self.targets.append(EphemObj(name, size))
else:
class UnknownTargetKind: pass
raise UnknownTargetKind()
self.pris.append(pri)
# This find the best matching object. What is
# returned is the specific target object that
# matched. These can have various properties,
# but are guraanteed to have the property .name,
# which most users will be interested in.
# Point has shape [nsamp,{mjd,ra,dec}]
def match(self, point, margin=0):
matches = []
for pri, trg in zip(self.pris, self.targets):
if trg.match(point, margin=margin):
matches.append([pri,trg])
if len(matches) == 0: return None
matches = sorted(matches)
return matches[-1][1]
def distance(self, point):
return [t.distance(point) for t in self.targets]