changeset 4:56508a6033a9

minutor chunk hacking
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 25 May 2021 14:01:26 -0400
parents 870e13483642
children 672621ab4db4
files python/cluster.py python/safe.py python/util.py
diffstat 3 files changed, 181 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/cluster.py	Tue May 25 14:01:26 2021 -0400
@@ -0,0 +1,84 @@
+#!/usr/bin/python3
+'''Read a minutor block lockation tsv and resort it to show clusters'''
+import sys
+
+from util import *
+
+if len(sys.argv)==1 or sys.argv[1]=='-h':
+  print("""Usage: cluster.py [-h] [-c n] infile.tsv [outfile.tsv]
+           n is cluster diameter, default is 5
+           default outfile is [infile]_c[n].tsv""")
+  exit(1)
+if sys.argv[1]=='-c':
+  sys.argv.pop(1)
+  n=float(sys.argv.pop(1))
+else:
+  n=5.0
+
+infile_name=sys.argv.pop(1)
+if len(sys.argv)>1:
+  outfile_name=sys.argv.pop(1)
+else:
+  outfile_name="%s_c%s.tsv"%(infile_name.split('.')[0],n)
+
+cc=[]
+
+with open(infile_name,'r') as infile:
+  with open(outfile_name,'w') as outfile:
+    l=infile.readline().rstrip()
+    print(l,file=outfile)
+    ff=PPAT.split(l)
+    (nr,ox,oy,oz)=intsMaybe(ff)
+    et=ff[9]
+    l=infile.readline().rstrip()
+    print(l,file=outfile)
+    (orad,ymin,ymax)=intsMaybe(PPAT.split(l))
+    print(nr,ox,oy,oz,et,orad,ymin,ymax)
+    _=infile.readline()
+    for l in infile:
+      found=False
+      q=[float(i) for i in l.split('\t')[2].split(',')]
+      for c in cc:
+        for p in c:
+          if d(p,q)<=n:
+            c.append(q)
+            found=True
+            break
+        if found:
+          break
+      if not found:
+        cc.append([q])
+    oc=cc
+    cc=[] # lose
+    w=0
+    ow=-1
+    nc=[] # win
+    while True:
+      for i,c in enumerate(oc):
+        win=False
+        for p in c:
+          for g in oc[i+1:]:
+            for q in g:
+              if d(p,q)<=n:
+                win=True
+                w+=1
+                nc.append(c+g)
+                break
+            if win:
+              break
+          if win:
+            break
+        if not win:
+          cc.append(c)
+      print(len(cc),len(nc),ow,w,file=sys.stderr)
+      if ow==w:
+        break
+      ow=w
+      oc=nc
+      nc=[]
+    for c in sorted(cc,reverse=True,key=lambda x:len(x)):
+      print(c,file=outfile)
+    
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/safe.py	Tue May 25 14:01:26 2021 -0400
@@ -0,0 +1,73 @@
+#!/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.get(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)
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/util.py	Tue May 25 14:01:26 2021 -0400
@@ -0,0 +1,24 @@
+'''Utility fns for dealing with block .tsv files'''
+import math,re
+
+IPAT=re.compile('-?[0-9][0-9]*$')
+PPAT=re.compile('[ ,;\[\]]')
+
+def intsMaybe(ii):
+  return (int(i) for i in ii if IPAT.match(i))
+
+def readHeaders(host,infile,skipColHdrs=True):
+    l=infile.readline().rstrip()
+    ff=PPAT.split(l)
+    (host.nr,host.ox,host.oy,host.oz)=intsMaybe(ff)
+    host.et=ff[9]
+    l=infile.readline().rstrip()
+    (host.orad,host.ymin,host.ymax)=intsMaybe(PPAT.split(l))
+    if skipColHdrs:
+      _=infile.readline()
+
+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))