comparison 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
comparison
equal deleted inserted replaced
10:73bb35b96624 11:e411408d64ec
10 n is maximum distance of landing to some point in the cluster, default is 10. 10 n is maximum distance of landing to some point in the cluster, default is 10.
11 11
12 Feed stdin with lines from the output of cluster.py, having produced 12 Feed stdin with lines from the output of cluster.py, having produced
13 air.tsv with the same settings as the cluster input, and lava.tsv 13 air.tsv with the same settings as the cluster input, and lava.tsv
14 with a lower-bound on Y of 0""") 14 with a lower-bound on Y of 0""")
15
16 n=10.0
17 if len(sys.argv)>1:
18 if sys.argv[1]=='-d':
19 sys.argv.pop()
20 n=float(sys.argv.pop(1))
21
22 if len(sys.argv)>1:
23 infile=open(sys.argv.pop(1))
24 else:
25 infile=sys.stdin
26 15
27 class Block: 16 class Block:
28 '''Shared by Air and Lava, 17 '''Shared by Air and Lava,
29 holds points and columns. 18 holds points and columns.
30 Columns are dictionaries indexed by (x,z) coords, 19 Columns are dictionaries indexed by (x,z) coords,
46 tc[k]=[y] 35 tc[k]=[y]
47 # convert to lists of intervals 36 # convert to lists of intervals
48 for k,yy in tc.items(): 37 for k,yy in tc.items():
49 tyy=[yy] 38 tyy=[yy]
50 ii=[] 39 ii=[]
40 if k==(108.5, -45.5):
41 pass
51 while True: 42 while True:
52 clean=True 43 clean=True
53 for j,yy in enumerate(tyy): 44 for j,yy in enumerate(tyy):
54 if len(yy)==1+(yy[-1]-yy[0]): 45 if len(yy)==1+(yy[-1]-yy[0]):
55 ii.append((yy[0],yy[-1])) 46 ii.append((yy[0],yy[-1]))
56 else: 47 else:
57 clean=False 48 clean=False
58 for i in range(len(yy)-1): 49 for i in range(len(yy)-1):
59 if yy[i]+1!=yy[i+1]: 50 if yy[i]+1!=yy[i+1]:
60 ii+=(yy[0],yy[i]) 51 ii.append((yy[0],yy[i]))
61 tyy=[yy[i+1:]]+tyy[j+1:] 52 tyy=[yy[i+1:]]+tyy[j+1:]
62 break 53 break
63 if clean: 54 if clean:
64 break 55 break
65 self.columns[k]=set(ii) # so we can merge later 56 self.columns[k]=set(ii) # so we can merge later
71 Block.readHeaders=readHeaders # from util 62 Block.readHeaders=readHeaders # from util
72 63
73 class Air(Block): 64 class Air(Block):
74 pass 65 pass
75 66
76 A=Air('air.tsv')
77 67
78 class Lava(Block): 68 class Lava(Block):
79 pass 69 pass
80
81 L=Lava('lava.tsv')
82 70
83 def d(p1,p2): 71 def d(p1,p2):
84 dx=p1[0]-p2[0] 72 dx=p1[0]-p2[0]
85 dz=p1[1]-p2[1] 73 dz=p1[1]-p2[1]
86 dy=p1[2]-p2[2] 74 dy=p1[2]-p2[2]
87 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz)) 75 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))
88 76
89 def safety(p): 77 def safety(p):
90 ka=[(a[0],a[2]) for a in A.points if d(a,p)<=n] 78 ka=[((a[0],a[2]),d(a,p)) for a in A.points if d(a,p)<=n]
91 return [(k,A.columns[k],L.columns[k]) for k in ka if k in L.columns] 79 return [(k,A.columns[k[0]],L.columns.get(k[0],None)) for k in ka]
92 80
93 readHeaders(sys.modules['__main__'],infile,False) 81 if __name__=='__main__':
94 for l in infile: 82 n=10.0
95 c=eval(l) 83 if len(sys.argv)>1:
96 ss=[] 84 if sys.argv[1]=='-d':
97 hits={} 85 sys.argv.pop(1)
98 misses=[] 86 n=float(sys.argv.pop(1))
99 for p in c: 87
100 ss=safety(p) 88 if len(sys.argv)>1:
101 if ss: 89 infile=open(sys.argv.pop(1))
102 for (k,a,l) in ss: 90 else:
103 try: 91 infile=sys.stdin
104 (aa,ll)=hits[k] 92
105 aa.update(a) 93 A=Air('air.tsv')
106 ll.update(l) 94
107 except KeyError: 95 L=Lava('lava.tsv')
108 hits[k]=(a,l) 96
97 readHeaders(sys.modules['__main__'],infile,False)
98
99 for l in infile:
100 c=eval(l)
101 ss=[]
102 hits={}
103 misses=[]
104 for p in c:
105 ss=safety(p)
106 if ss:
107 for (k,a,l) in ss:
108 try:
109 (aa,ll)=hits[k]
110 aa.update(a)
111 if l is not None:
112 ll.update(l)
113 except KeyError:
114 hits[k]=(a,l)
115 else:
116 misses.append(p)
117 print(c)
118 if hits:
119 print(' %s nearby landing columns'%len(hits))
120 done={}
121 for k in sorted(hits.keys(),key=lambda k:k[1]):
122 if k[0] not in done:
123 done[k[0]]=1
124 aa,ll=hits[k]
125 laa=sorted(list(aa))
126 lowAir=laa[0][0]
127 j=-1
128 if ll is not None:
129 lll=sorted(list(ll))
130 highLava=-1
131 for i,(_,h) in enumerate(lll):
132 if h>highLava and h<lowAir:
133 highLava=h
134 if h+1==lowAir:
135 j=i
136 break
137 if j>-1:
138 lava='['+','.join(str(e) if i!=j else \
139 "(%s,%s)"%(e[0],RedFmt%e[1]) for i,e in enumerate(lll))+']'
140 lowAir=RedFmt%lowAir
141 else:
142 lava=None
143 else:
144 lava=None
145 print("%s(%s %s %s)@%0.1f: %s %s"%(' ' if j>-1 else '',
146 k[0][0],lowAir,k[0][1],k[1],laa,lava))
109 else: 147 else:
110 misses.append(p) 148 print(' No nearby landing zones')
111 print(c)
112 if hits:
113 print(' %s nearby landing columns'%len(hits))
114 for k in sorted(hits.keys(),key=lambda k:k[0]):
115 aa,ll=hits[k]
116 print("%s: %s %s"%(k,sorted(list(aa)),sorted(list(ll))))
117 else:
118 print(' No nearby landing zones')
119 149
120 if infile!=sys.stdin: 150 if infile!=sys.stdin:
121 infile.close() 151 infile.close()