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