annotate python/safe.py @ 9:0d1670ab37df

towards useful output
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 26 May 2021 13:52:53 -0400
parents fcef94b6324c
children 73bb35b96624
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 *
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
6 from bisect import insort_left as 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():
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 print("""Usage: safe.py [-d n]
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 if len(sys.argv)>1:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 if sys.argv.pop(1)!='-d':
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 usage()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 exit(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 n=float(sys.argv.pop(1))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 else:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 n=10.0
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 class Block:
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
25 '''Shared by Air and Lava,
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
26 holds points and columns.
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
27 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
28 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
29 def __init__(self,filename):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
30 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
31 self.readHeaders(file)
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
32 self.readPoints(file)
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
34 cc=self.columns={}
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
35 tc={}
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
36 for p in self.points:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
37 k=(p[0],p[2])
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
38 y=p[1]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
39 try:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
40 yy=tc[k]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
41 insort(yy,y)
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
42 except KeyError:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
43 tc[k]=[y]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
44 # convert to lists of intervals
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
45 # Simplest case first
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
46 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
47 tyy=[yy]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
48 ii=[]
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
49 while True:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
50 clean=True
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
51 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
52 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
53 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
54 else:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
55 clean=False
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
56 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
57 if yy[i]+1!=yy[i+1]:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
58 ii+=(yy[0],yy[i])
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
59 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
60 break
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
61 if clean:
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
62 break
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
63 self.columns[k]=ii
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
64
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
65 def readPoints(self,file):
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
66 file.readline()
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
67 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
68
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
69 Block.readHeaders=readHeaders # from util
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 class Air(Block):
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
72 pass
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 A=Air('air.tsv')
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 class Lava(Block):
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
77 pass
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
78
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 L=Lava('lava.tsv')
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81 def d(p1,p2):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82 dx=p1[0]-p2[0]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83 dz=p1[1]-p2[1]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84 dy=p1[2]-p2[2]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
85 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
86
9
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
87 def safety(p):
8
fcef94b6324c move all the action to Block
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 7
diff changeset
88 ka=[(a[0],a[2]) for a in A.points if d(a,p)<=n]
7
3ee329b129c6 rename block properties, slightly revise result format
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 6
diff changeset
89 return [(k,A.columns[k],L.columns[k]) for k in ka if k in L.columns]
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
90
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
91 readHeaders(sys.modules['__main__'],sys.stdin,False)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
92 for l in sys.stdin:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
93 c=eval(l)
9
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
94 ss=[]
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
95 hits={}
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
96 misses=[]
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
97 for p in c:
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
98 s=safety(p)
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
99 if s:
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
100 pass
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
101 else:
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
102 misses.append(p)
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
103 print(c)
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
104 print(' %s nearby landing zones'%(
0d1670ab37df towards useful output
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 8
diff changeset
105 (lambda nm,nc:'No' if nm==nc else nc-nm)(len(misses),len(c))))