Mercurial > hg > lib > markup
annotate python/safe.py @ 6:a01ff74f9fd7
fix accumulator bug in Lava.__init__
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Tue, 25 May 2021 17:20:04 -0400 |
parents | 56508a6033a9 |
children | 3ee329b129c6 |
rev | line source |
---|---|
4 | 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 | |
7 def usage(): | |
8 print("""Usage: safe.py [-d n] | |
9 n is maximum distance of landing to some point in the cluster, default is 10. | |
10 | |
11 Feed stdin with lines from the output of cluster.py, having produced | |
12 air.tsv with the same settings as the cluster input, and lava.tsv | |
13 with a lower-bound on Y of 0""") | |
14 | |
15 if len(sys.argv)>1: | |
16 if sys.argv.pop(1)!='-d': | |
17 usage() | |
18 exit(1) | |
19 n=float(sys.argv.pop(1)) | |
20 else: | |
21 n=10.0 | |
22 | |
23 class Block: | |
24 pass | |
25 | |
26 Block.__init__=readHeaders | |
27 | |
28 class Air(Block): | |
29 def __init__(self,filename): | |
30 with open(filename,'r') as air: | |
31 Block.__init__(self,air) | |
32 air.readline() | |
33 aa=self.aa=[[float(i) for i in l.split('\t')[2].split(',')] for l in air] | |
34 ad=self.ad={} | |
35 for a in aa: | |
36 k=(a[0],a[2]) | |
37 h=ad.get(k,500.0) | |
38 if a[1]<h: | |
39 ad[k]=a[1] | |
40 | |
41 A=Air('air.tsv') | |
42 | |
43 class Lava(Block): | |
44 def __init__(self,filename): | |
45 with open(filename,'r') as lava: | |
46 Block.__init__(self,lava) | |
47 lava.readline() | |
48 ll=self.ll={} | |
49 for l in lava: | |
50 x=[float(i) for i in l.split('\t')[2].split(',')] | |
51 k=(x[0],x[2]) | |
6
a01ff74f9fd7
fix accumulator bug in Lava.__init__
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
4
diff
changeset
|
52 kk=ll.setdefault(k,list()) |
4 | 53 kk.append(x) |
54 | |
55 L=Lava('lava.tsv') | |
56 | |
57 def d(p1,p2): | |
58 dx=p1[0]-p2[0] | |
59 dz=p1[1]-p2[1] | |
60 dy=p1[2]-p2[2] | |
61 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz)) | |
62 | |
63 def safety(p): | |
64 ka=[(a[0],a[2]) for a in A.aa if d(a,p)<=n] | |
65 return [(k,ll[k]) for k in ka if k in L.ll] | |
66 | |
67 readHeaders(sys.modules['__main__'],sys.stdin,False) | |
68 for l in sys.stdin: | |
69 c=eval(l) | |
70 s=[(p,safety(p)) for p in c] | |
71 print(s if s else 'No air',c) | |
72 | |
73 |