aboutsummaryrefslogtreecommitdiff
path: root/scripts/move-entire-detector
blob: 0833845b043a29303f4ddd341a2eb999a0384f96 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python

#
# Shift the detector by a given amount
#
# Copyright (c) 2016 Deutsches Elektronen-Synchrotron DESY,
#                    a research centre of the Helmholtz Association.
#
# Author:
#    2016 Thomas White <taw@physics.org>
#

import sys
import optparse
import os
import re

op = optparse.OptionParser(usage="%prog input.geom xshift yshift")
op.add_option('', '--output', action='store', type='string', dest='ofn',
              help="Output geometry file")
op.add_option('', '--pixels', action='store_true', dest='px',
              help="Specify shifts in pixel units")
op.add_option('', '--mm', action='store_false', dest='px',
              help="Specify shifts in millimeters (this is the default)")
op.add_option('', '--beam', action='store_true', dest='beam',
              help="Specify shift of X-ray beam")
op.add_option('', '--detector', action='store_false', dest='beam',
              help="Specify shift of detector (this is the default)")
opt,args = op.parse_args(sys.argv)

if len(args) < 3:
    print 'Usage: %s input.geom xshift yshift' % args[0]
    sys.exit(1)

geom = args[1]
if not opt.ofn:
    out = os.path.splitext(geom)[0]+'-shifted.geom'
else:
    out = opt.ofn
xshift = float(args[2])
yshift = float(args[3])

if opt.px:
    units = 'px'
else:
    units = 'mm'

if opt.beam:
    bm = 'beam'
else:
    bm = 'detector'

print 'Input filename %s\nOutput filename %s' % (geom,out)
print 'Shifting %s by %f,%f %s' % (bm, xshift, yshift, units)

if opt.beam:
    xshift = -xshift
    yshift = -yshift

f = open(geom, 'r')
g = open(args[1], 'r')
h = open(out, 'w')
panel_resolutions = {}

prog1 = re.compile("^\s*res\s+=\s+([0-9\.]+)\s")
prog2 = re.compile("^\s*(.*)\/res\s+=\s+([0-9\.]+)\s")
prog3 = re.compile("^\s*(.*)\/corner_x\s+=\s+([0-9\.\-]+)\s")
prog4 = re.compile("^\s*(.*)\/corner_y\s+=\s+([0-9\.\-]+)\s")
default_res = 0
while True:

    fline = g.readline()
    if not fline:
        break

    match = prog1.match(fline)
    if match:
        default_res = float(match.group(1))
        h.write(fline)
        continue

    match = prog2.match(fline)
    if match:
        panel = match.group(1)
        panel_res = float(match.group(2))
        default_res =  panel_res
        panel_resolutions[panel] = panel_res
        h.write(fline)
        continue

    match = prog3.match(fline)
    if match:
        panel = match.group(1)
        panel_cnx = float(match.group(2))
        if panel in panel_resolutions:
            res = panel_resolutions[panel]
        else:
            res = default_res
            if not opt.px:
                print 'Using default resolution (%f px/m) for panel %s' % (res, panel)
        if opt.px:
            h.write('%s/corner_x = %f\n' % (panel,panel_cnx+xshift))
        else:
            h.write('%s/corner_x = %f\n' % (panel,panel_cnx+(xshift*res*1e-3)))
        continue

    match = prog4.match(fline)
    if match:
        panel = match.group(1)
        panel_cny = float(match.group(2))
        if panel in panel_resolutions:
            res = panel_resolutions[panel]
        else:
            res = default_res
            if not opt.px:
                print 'Using default resolution (%f px/m) for panel %s' % (res, panel)
        if opt.px:
            h.write('%s/corner_y = %f\n' % (panel,panel_cny+yshift))
        else:
            h.write('%s/corner_y = %f\n' % (panel,panel_cny+(yshift*res*1e-3)))
        continue

    h.write(fline)

g.close()
h.close()