annotate bin/by_interval.py @ 226:19bdf3368678

rebuild to match triple fig line colour
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Wed, 28 Feb 2024 15:27:00 +0000
parents d2c4fec1ed21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
2 '''Split stamped data by time interval, e.g. year or month
215
d2c4fec1ed21 correct Usage
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 172
diff changeset
3 Usage: by_interval.py origin-interval output-dir interval-file sorted-ks-file.tsv [field]
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 If field is given, ks-file is tsv and stamp is in that field,
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
5 which, as for cut, is 1-origin. Interval IDs are assumed to be sequential numbers.
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
6 '''
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 import sys, os, os.path
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 if len(sys.argv)>5:
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
11 (origin, outdir, intv_file, in_file, field) = sys.argv[1:]
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 field=int(field)-1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 else:
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
14 (origin, outdir, intv_file, in_file) = sys.argv[1:]
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 field=3
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 if not os.access(outdir,os.F_OK):
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 os.mkdir(outdir)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19
168
f95858689037 fix output year
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 167
diff changeset
20 origin = int(origin)
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 current = origin
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 is_first = True
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
23 with open(intv_file,'r') as intvs, open(in_file,'r') as stamped:
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
24 y = intvs.readline()
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25 l = stamped.readline()
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 while y:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 y = float(y)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 if is_first:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 ysuf = 'prev'
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 else:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 ysuf = str(current-1)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 with open(os.path.join(outdir,'ks_%s.tsv'%ysuf),'w') as y_file:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 while l:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 if float(l.split('\t')[field]) < y:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35 y_file.write(l)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 else:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 break
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 l = stamped.readline()
172
bc66c6098e5e should work for months also now
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 168
diff changeset
39 y = intvs.readline()
167
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 current += 1
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 is_first = False
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 else:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 with open(os.path.join(outdir,'ks_post.tsv'),'w') as y_file:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 while l:
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 y_file.write(l)
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 l = stamped.readline()