From 5b64b3dd7d846fbb26f79ab993994cf1570fe187 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 3 Aug 2016 17:54:42 +0200 Subject: Add scripts/move-entire-detector and scripts/split-by-mask --- scripts/move-entire-detector | 127 +++++++++++++++++++++++++++++++++++++++++++ scripts/split-by-mask | 87 +++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100755 scripts/move-entire-detector create mode 100755 scripts/split-by-mask (limited to 'scripts') diff --git a/scripts/move-entire-detector b/scripts/move-entire-detector new file mode 100755 index 00000000..0833845b --- /dev/null +++ b/scripts/move-entire-detector @@ -0,0 +1,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 +# + +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() + diff --git a/scripts/split-by-mask b/scripts/split-by-mask new file mode 100755 index 00000000..aede3f43 --- /dev/null +++ b/scripts/split-by-mask @@ -0,0 +1,87 @@ +#!/usr/bin/env python + +# +# Split stream according to an external mask +# +# Copyright (c) 2014-2016 Deutsches Elektronen-Synchrotron DESY, +# a research centre of the Helmholtz Association. +# +# Author: +# 2014-2016 Thomas White +# + +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? %s' % 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() + -- cgit v1.2.3