Mercurial > hg > cc > cirrus_work
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 |
rev | line source |
---|---|
167 | 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 | 3 Usage: by_interval.py origin-interval output-dir interval-file sorted-ks-file.tsv [field] |
167 | 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 | 7 |
8 import sys, os, os.path | |
9 | |
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 | 12 field=int(field)-1 |
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 | 15 field=3 |
16 | |
17 if not os.access(outdir,os.F_OK): | |
18 os.mkdir(outdir) | |
19 | |
168 | 20 origin = int(origin) |
167 | 21 current = origin |
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 | 25 l = stamped.readline() |
26 while y: | |
27 y = float(y) | |
28 if is_first: | |
29 ysuf = 'prev' | |
30 else: | |
31 ysuf = str(current-1) | |
32 with open(os.path.join(outdir,'ks_%s.tsv'%ysuf),'w') as y_file: | |
33 while l: | |
34 if float(l.split('\t')[field]) < y: | |
35 y_file.write(l) | |
36 else: | |
37 break | |
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 | 40 current += 1 |
41 is_first = False | |
42 else: | |
43 with open(os.path.join(outdir,'ks_post.tsv'),'w') as y_file: | |
44 while l: | |
45 y_file.write(l) | |
46 l = stamped.readline() |