aboutsummaryrefslogtreecommitdiff
path: root/scripts/move-entire-detector
blob: ffd6120f34a40fc4e3b630bbfb0c2fc4a868749d (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
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Shift the detector by a given amount
#
# Copyright © 2016-2017 Deutsches Elektronen-Synchrotron DESY,
#                       a research centre of the Helmholtz Association.
#
# Author:
#    2016-2017 Thomas White <taw@physics.org>
#

import sys
import argparse
import os
import re

op = argparse.ArgumentParser()

op.add_argument('--output', dest='ofn', metavar='output.geom',
              help="Output geometry file")

op.add_argument('--pixels', action='store_true', dest='px',
              help="Indicates that the shifts are in pixel units")

op.add_argument('--mm', action='store_false', dest='px',
              help="Indicates that the shifts are in millimeters (this is the default)")

op.add_argument('--beam', action='store_true', dest='beam',
              help="Shift the X-ray beam rather than the detector")

op.add_argument('--detector', action='store_false', dest='beam',
              help="Shift the detector rather than the X-ray beam.  This is the default.")

op.add_argument('ifn', metavar='input.geom',
               help="The name of the geometry file to shift")

op.add_argument('xshift', type=float, metavar='XXX',
               help="The shift in the x direction")

op.add_argument('yshift', type=float, metavar='YYY',
               help="The shift in the y direction")

args = op.parse_args()

if not args.ofn:
    out = os.path.splitext(args.ifn)[0]+'-shifted.geom'
else:
    out = args.ofn

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

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

print('Input filename {}\nOutput filename {}'.format(args.ifn,out))
print('Shifting {} by {},{} {}'.format(bm, args.xshift, args.yshift, units))

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

f = open(args.ifn, '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 = f.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 args.px:
                print('Using default resolution ({} px/m) for panel {}'.format(res, panel))
        if args.px:
            h.write('%s/corner_x = %f\n' % (panel,panel_cnx+args.xshift))
        else:
            h.write('%s/corner_x = %f\n' % (panel,panel_cnx+(args.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 args.px:
                print('Using default resolution ({} px/m) for panel {}'.format(res, panel))
        if args.px:
            h.write('%s/corner_y = %f\n' % (panel,panel_cny+args.yshift))
        else:
            h.write('%s/corner_y = %f\n' % (panel,panel_cny+(args.yshift*res*1e-3)))
        continue

    h.write(fline)

f.close()
h.close()