annotate safe.py @ 24:6df2f6dcc809

fixed interval creation bug, made result better for clipping
author Henry Thompson <ht@markup.co.uk>
date Sat, 29 May 2021 21:29:08 +0100
parents 1670a33e3e6d
children 5488b5d3ba10
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=[]
24
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
51 if k==(108.5, -45.5):
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
52 pass
23
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
53 while True:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
54 clean=True
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
55 for j,yy in enumerate(tyy):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
56 if len(yy)==1+(yy[-1]-yy[0]):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
57 ii.append((yy[0],yy[-1]))
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
58 else:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
59 clean=False
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
60 for i in range(len(yy)-1):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
61 if yy[i]+1!=yy[i+1]:
24
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
62 ii.append((yy[0],yy[i]))
23
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
63 tyy=[yy[i+1:]]+tyy[j+1:]
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 if clean:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
66 break
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
67 self.columns[k]=set(ii) # so we can merge later
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
68
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
69 def readPoints(self,file):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
70 file.readline()
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
71 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
72
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
73 Block.readHeaders=readHeaders # from util
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
74
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
75 class Air(Block):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
76 pass
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 A=Air('air.tsv')
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
79
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
80 class Lava(Block):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
81 pass
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 L=Lava('lava.tsv')
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
84
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
85 def d(p1,p2):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
86 dx=p1[0]-p2[0]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
87 dz=p1[1]-p2[1]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
88 dy=p1[2]-p2[2]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
89 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
90
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
91 def safety(p):
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
92 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
93 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
94
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
95 readHeaders(sys.modules['__main__'],infile,False)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
96 for l in infile:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
97 c=eval(l)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
98 ss=[]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
99 hits={}
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
100 misses=[]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
101 for p in c:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
102 ss=safety(p)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
103 if ss:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
104 for (k,a,l) in ss:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
105 try:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
106 (aa,ll)=hits[k]
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
107 aa.update(a)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
108 ll.update(l)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
109 except KeyError:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
110 hits[k]=(a,l)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
111 else:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
112 misses.append(p)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
113 print(c)
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
114 if hits:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
115 print(' %s nearby landing columns'%len(hits))
24
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
116 done={}
23
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
117 for k in sorted(hits.keys(),key=lambda k:k[1]):
24
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
118 if k[0] not in done:
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
119 done[k[0]]=1
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
120 aa,ll=hits[k]
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
121 laa=sorted(list(aa))
6df2f6dcc809 fixed interval creation bug, made result better for clipping
Henry Thompson <ht@markup.co.uk>
parents: 23
diff changeset
122 print("(%s %s %s)@%0.1f: %s %s"%(k[0][0],laa[0][0],k[0][1],k[1],laa,sorted(list(ll))))
23
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
123 else:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
124 print(' No nearby landing zones')
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
125
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
126 if infile!=sys.stdin:
1670a33e3e6d from markup
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
127 infile.close()