annotate safe.py @ 23:1670a33e3e6d

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