annotate util.py @ 53:91d71e9760e8

forgot what this is about
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 29 May 2023 22:02:52 +0100
parents 668d788cac47
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
1 '''Utility fns for dealing with block .tsv files'''
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
2 import math,re
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
3
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
4 IPAT=re.compile('-?[0-9][0-9]*$')
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
5 PPAT=re.compile('[ ,;\[\]]')
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
6
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
7 Red=''
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
8 eRed=''
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
9 RedFmt=Red+'%s'+eRed
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
10
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
11 def intsMaybe(ii):
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
12 return (int(i) for i in ii if IPAT.match(i))
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
13
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
14 def readHeaders(host,infile,skipColHdrs=True):
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
15 l=infile.readline().rstrip()
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
16 ff=PPAT.split(l)
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
17 (host.nr,host.ox,host.oy,host.oz)=intsMaybe(ff)
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
18 host.et=ff[9]
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
19 l=infile.readline().rstrip()
34
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
20 l2=PPAT.split(l)
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
21 if l2[-1]=='bounded':
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
22 host.orad=int(l2[1])
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
23 host.ymin=host.ymax=-1
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
24 else:
668d788cac47 handle unbounded Y, landing below target elevation
Henry Thompson <ht@markup.co.uk>
parents: 26
diff changeset
25 (host.orad,host.ymin,host.ymax)=intsMaybe(PPAT.split(l))
26
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
26 if skipColHdrs:
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
27 _=infile.readline()
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
28
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
29 def d(p1,p2):
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
30 dx=p1[0]-p2[0]
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
31 dz=p1[1]-p2[1]
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
32 dy=p1[2]-p2[2]
5488b5d3ba10 use indentation and colour to differentiate safe from unsafe landing place
Henry Thompson <ht@markup.co.uk>
parents:
diff changeset
33 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))