view 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
line wrap: on
line source

#!/usr/bin/python3
'''Find a safe landing space (i.e. in air, not above lava) near a cluster'''
import sys, math

from util import *

def usage():
  print("""Usage:  safe.py [-d n]
  n is maximum distance of landing to some point in the cluster, default is 10.
  
  Feed stdin with lines from the output of cluster.py, having produced
  air.tsv with the same settings as the cluster input, and lava.tsv
  with a lower-bound on Y of 0""")

if len(sys.argv)>1:
  if sys.argv.pop(1)!='-d':
    usage()
    exit(1)
  n=float(sys.argv.pop(1))
else:
  n=10.0

class Block:
  pass

Block.__init__=readHeaders

class Air(Block):
  def __init__(self,filename):
    with open(filename,'r') as air:
      Block.__init__(self,air)
      air.readline()
      aa=self.aa=[[float(i) for i in l.split('\t')[2].split(',')] for l in air]
    ad=self.ad={}
    for a in aa:
      k=(a[0],a[2])
      h=ad.get(k,500.0)
      if a[1]<h:
        ad[k]=a[1]

A=Air('air.tsv')

class Lava(Block):
  def __init__(self,filename):
    with open(filename,'r') as lava:
      Block.__init__(self,lava)
      lava.readline()
      ll=self.ll={}
      for l in lava:
        x=[float(i) for i in l.split('\t')[2].split(',')]
        k=(x[0],x[2])
        kk=ll.setdefault(k,list())
        kk.append(x)
  
L=Lava('lava.tsv')

def d(p1,p2):
  dx=p1[0]-p2[0]
  dz=p1[1]-p2[1]
  dy=p1[2]-p2[2]
  return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))

def safety(p):
  ka=[(a[0],a[2]) for a in A.aa if d(a,p)<=n]
  return [(k,ll[k]) for k in ka if k in L.ll]

readHeaders(sys.modules['__main__'],sys.stdin,False)
for l in sys.stdin:
  c=eval(l)
  s=[(p,safety(p)) for p in c]
  print(s if s else 'No air',c)