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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Shift the detector by a given amount
#
# Copyright © 2016-2020 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 x and y shifts are in pixel units. "
"The z shift is always in millimeters")
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 (reverses x and y shifts)")
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")
op.add_argument('zshift', type=float, metavar='ZZZ',
help="The shift in the z 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, args.zshift, units))
if args.beam:
args.xshift = -args.xshift
args.yshift = -args.yshift
f = open(args.ifn, 'r')
h = open(out, 'w')
panel_resolutions = {}
panels = []
panels_have_coffset = []
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")
prog5 = re.compile("^\s*(.*)\/coffset\s+=\s+([0-9\.\-]+)\s")
prog6 = re.compile("^\s*coffset\s+=\s+([0-9\.]+)\s")
default_res = 0
default_coffset = None
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 = prog6.match(fline)
if match:
default_coffset = float(match.group(1))
h.write("coffset = %f\n" % (default_coffset+args.zshift))
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)
if panel not in panels:
panels.append(panel)
# If we have a default coffset, this panel has a coffset
# (which has already been adjusted)
if default_coffset is not None:
panels_have_coffset.append(panel)
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
match = prog5.match(fline)
if match:
panel = match.group(1)
panel_coffset = float(match.group(2))
h.write('%s/coffset = %f\n' % (panel,panel_coffset+args.zshift*1e-3))
panels_have_coffset.append(panel)
continue
h.write(fline)
h.write("\n")
for p in [n for n in panels if n not in panels_have_coffset]:
h.write("%s/coffset = %f\n" % (p, args.zshift*1e-3))
f.close()
h.close()
|