annotate python/safe.py @ 11:e411408d64ec

from python repo
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sat, 11 Dec 2021 16:13:04 -0500
parents 73bb35b96624
children 1e42c0147a49
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''Find a safe landing space (i.e. in air, not above lava) near a cluster'''
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 import sys, math
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 from util import *
10
73bb35b96624 show good hit output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
6 from bisect import insort
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 def usage():
10
73bb35b96624 show good hit output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
9 print("""Usage: safe.py [-d n] [filename]
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 n is maximum distance of landing to some point in the cluster, default is 10.
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 Feed stdin with lines from the output of cluster.py, having produced
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 air.tsv with the same settings as the cluster input, and lava.tsv
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 with a lower-bound on Y of 0""")
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 class Block:
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
17 '''Shared by Air and Lava,
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
18 holds points and columns.
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
19 Columns are dictionaries indexed by (x,z) coords,
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
20 value is upward-ordered list of pairs, closed intervals of y'''
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
21 def __init__(self,filename):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
22 with open(filename,'r') as file:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
23 self.readHeaders(file)
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
24 self.readPoints(file)
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
26 cc=self.columns={}
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
27 tc={}
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
28 for p in self.points:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
29 k=(p[0],p[2])
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
30 y=p[1]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
31 try:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
32 yy=tc[k]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
33 insort(yy,y)
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
34 except KeyError:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
35 tc[k]=[y]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
36 # convert to lists of intervals
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
37 for k,yy in tc.items():
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
38 tyy=[yy]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
39 ii=[]
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
40 if k==(108.5, -45.5):
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
41 pass
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
42 while True:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
43 clean=True
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
44 for j,yy in enumerate(tyy):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
45 if len(yy)==1+(yy[-1]-yy[0]):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
46 ii.append((yy[0],yy[-1]))
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
47 else:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
48 clean=False
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
49 for i in range(len(yy)-1):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
50 if yy[i]+1!=yy[i+1]:
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
51 ii.append((yy[0],yy[i]))
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
52 tyy=[yy[i+1:]]+tyy[j+1:]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
53 break
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
54 if clean:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
55 break
10
73bb35b96624 show good hit output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
56 self.columns[k]=set(ii) # so we can merge later
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
57
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
58 def readPoints(self,file):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
59 file.readline()
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
60 self.points=[[float(i) for i in l.split('\t')[2].split(',')] for l in file]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
61
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
62 Block.readHeaders=readHeaders # from util
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 class Air(Block):
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
65 pass
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 class Lava(Block):
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
69 pass
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
70
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 def d(p1,p2):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 dx=p1[0]-p2[0]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 dz=p1[1]-p2[1]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 dy=p1[2]-p2[2]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76
9
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
77 def safety(p):
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
78 ka=[((a[0],a[2]),d(a,p)) for a in A.points if d(a,p)<=n]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
79 return [(k,A.columns[k[0]],L.columns.get(k[0],None)) for k in ka]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
80
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
81 if __name__=='__main__':
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
82 n=10.0
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
83 if len(sys.argv)>1:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
84 if sys.argv[1]=='-d':
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
85 sys.argv.pop(1)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
86 n=float(sys.argv.pop(1))
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
87
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
88 if len(sys.argv)>1:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
89 infile=open(sys.argv.pop(1))
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
90 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
91 infile=sys.stdin
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
92
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
93 A=Air('air.tsv')
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
94
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
95 L=Lava('lava.tsv')
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
96
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
97 readHeaders(sys.modules['__main__'],infile,False)
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
98
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
99 for l in infile:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
100 c=eval(l)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
101 ss=[]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
102 hits={}
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
103 misses=[]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
104 for p in c:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
105 ss=safety(p)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
106 if ss:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
107 for (k,a,l) in ss:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
108 try:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
109 (aa,ll)=hits[k]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
110 aa.update(a)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
111 if l is not None:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
112 ll.update(l)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
113 except KeyError:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
114 hits[k]=(a,l)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
115 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
116 misses.append(p)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
117 print(c)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
118 if hits:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
119 print(' %s nearby landing columns'%len(hits))
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
120 done={}
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
121 for k in sorted(hits.keys(),key=lambda k:k[1]):
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
122 if k[0] not in done:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
123 done[k[0]]=1
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
124 aa,ll=hits[k]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
125 laa=sorted(list(aa))
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
126 lowAir=laa[0][0]
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
127 j=-1
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
128 if ll is not None:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
129 lll=sorted(list(ll))
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
130 highLava=-1
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
131 for i,(_,h) in enumerate(lll):
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
132 if h>highLava and h<lowAir:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
133 highLava=h
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
134 if h+1==lowAir:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
135 j=i
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
136 break
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
137 if j>-1:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
138 lava='['+','.join(str(e) if i!=j else \
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
139 "(%s,%s)"%(e[0],RedFmt%e[1]) for i,e in enumerate(lll))+']'
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
140 lowAir=RedFmt%lowAir
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
141 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
142 lava=None
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
143 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
144 lava=None
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
145 print("%s(%s %s %s)@%0.1f: %s %s"%(' ' if j>-1 else '',
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
146 k[0][0],lowAir,k[0][1],k[1],laa,lava))
9
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
147 else:
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
148 print(' No nearby landing zones')
10
73bb35b96624 show good hit output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 9
diff changeset
149
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
150 if infile!=sys.stdin:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 10
diff changeset
151 infile.close()