aboutsummaryrefslogtreecommitdiff
path: root/scripts/split-by-mask
blob: a261d75807a21e0b47f3da142b412e0e636c1122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Split stream according to an external mask
#
# Copyright © 2014-2020 Deutsches Elektronen-Synchrotron DESY,
#                       a research centre of the Helmholtz Association.
#
# Author:
#    2014-2017 Thomas White <taw@physics.org>
#

import sys
import optparse
from collections import deque
import h5py
import re

prog = re.compile("^\s+([\d-]+)\s+([\d-]+)\s+([\d-]+)\s+[\d\.\-]+\s+[\d\.\-]+\s+[\d\.\-]+\s+[\d\.\-]+\s+([\d\.]+)\s+([\d\.]+)")

def process_refln(fline, g, h, mask):
    match = prog.match(fline)
    if not match:
        print('Line not understood, WTF? {}'.format(fline))
	return
    fs = int(round(float(match.group(4))))
    ss = int(round(float(match.group(5))))
    m = mask[ss][fs]
    if m:
        g.write(fline)
    else:
        h.write(fline)

op = optparse.OptionParser()

op.add_option('', '--input', action='store', type='string', dest='ifn',
              help="Input stream")

op.add_option('', '--output1', action='store', type='string', dest='ofn1',
              help="Output stream", default='mask-split.stream1')

op.add_option('', '--output2', action='store', type='string', dest='ofn2',
              help="Output stream", default='mask-split.stream2')

op.add_option('', '--mask', action='store', type='string', dest='mask_fn',
              help="Filename of mask", default='mask.h5')

opt,arg = op.parse_args(sys.argv)

if not opt.ifn:
    print("You need at least --input")
    exit(1)

f = open(opt.ifn, 'r')
g = open(opt.ofn1, 'w')
h = open(opt.ofn2, 'w')

fh = h5py.File(opt.mask_fn, 'r')
mask = fh['/data/data'].value
fh.close()

in_refl = 0
while True:

    fline = f.readline()
    if not fline:
        break
    if fline.find("End of reflections") != -1:
        in_refl = 0

    if in_refl == 1:
        process_refln(fline, g, h, mask)
    else:
        g.write(fline)
        h.write(fline)
	if in_refl == 2:
	    in_refl = 1

    if fline.find("Reflections measured after indexing") != -1:
        in_refl = 2




f.close()
g.close()