diff --git a/cfel_geom.py b/cfel_geom.py index 0f5937e69ceded764be1da63bd84ad6a8e9f6acf..c1370d3390b5afcf3ba8b70fecfa262d159fa7da 100644 --- a/cfel_geom.py +++ b/cfel_geom.py @@ -25,6 +25,7 @@ from __future__ import print_function from __future__ import unicode_literals import numpy +from cfel_crystfel import load_crystfel_geometry def apply_geometry_from_file(data_as_slab, geometry_filename): @@ -123,38 +124,6 @@ def pixel_maps_for_image_view(geometry_filename): return yx, slab_shape, img_shape -def parse_xy(string): - """Extracts x and y values from strings in a geometry file. - - Parse the x, y values from strings in that have the format: '1x + 2.0y'. - - Args: - - string (str): the string to be parsed. - - Returns: - - x, y (float, float): the values of x and y. - """ - - x = y = 0 - - if string.find('x') is not -1: - xs = string.split('x')[0].split(' ')[-1] - if len(xs) > 0: - x = float(xs) - else: - x = 1. - - if string.find('y') is not -1: - ys = string.split('y')[0].split(' ')[-1] - if len(ys) > 0: - y = float(ys) - else: - y = 1. - return x, y - - def pixel_maps_from_geometry_file(fnam): """Parses a geometry file and creates pixel maps. @@ -172,122 +141,34 @@ def pixel_maps_from_geometry_file(fnam): respectively x, y coordinates of the pixel and distance of the pixel from the center of the reference system. """ - f = open(fnam, 'r') - f_lines = f.readlines() - f.close() + detector = load_crystfel_geometry(fnam) - keyword_list = ['min_fs', 'min_ss', 'max_fs', 'max_ss', 'fs', 'ss', 'corner_x', 'corner_y'] - - detector_dict = {} - - panel_lines = [x for x in f_lines if '/' in x and - len(x.split('/')) == 2 and x.split('/')[1].split('=')[0].strip() in keyword_list and - 'bad_' not in x.split('/')[0].strip()] - - for pline in panel_lines: - items = pline.split('=')[0].split('/') - panel = items[0].strip() - prop = items[1].strip() - if prop in keyword_list: - if panel not in detector_dict.keys(): - detector_dict[panel] = {} - detector_dict[panel][prop] = pline.split('=')[1].split(';')[0] - - parsed_detector_dict = {} - - for p in detector_dict.keys(): - - parsed_detector_dict[p] = {} - - parsed_detector_dict[p]['min_fs'] = int(detector_dict[p]['min_fs']) - parsed_detector_dict[p]['max_fs'] = int(detector_dict[p]['max_fs']) - parsed_detector_dict[p]['min_ss'] = int(detector_dict[p]['min_ss']) - parsed_detector_dict[p]['max_ss'] = int(detector_dict[p]['max_ss']) - parsed_detector_dict[p]['fs'] = list(parse_xy(detector_dict[p]['fs'])) - parsed_detector_dict[p]['ss'] = list(parse_xy(detector_dict[p]['ss'])) - parsed_detector_dict[p]['corner_x'] = float(detector_dict[p]['corner_x']) - parsed_detector_dict[p]['corner_y'] = float(detector_dict[p]['corner_y']) - - max_slab_fs = numpy.array([parsed_detector_dict[k]['max_fs'] for k in parsed_detector_dict.keys()]).max() - max_slab_ss = numpy.array([parsed_detector_dict[k]['max_ss'] for k in parsed_detector_dict.keys()]).max() + max_slab_fs = numpy.array([detector['panels'][k]['max_fs'] for k in detector['panels']]).max() + max_slab_ss = numpy.array([detector['panels'][k]['max_ss'] for k in detector['panels']]).max() x = numpy.zeros((max_slab_ss+1, max_slab_fs+1), dtype=numpy.float32) y = numpy.zeros((max_slab_ss+1, max_slab_fs+1), dtype=numpy.float32) - for p in parsed_detector_dict.keys(): + for p in detector['panels']: # get the pixel coords for this asic - i, j = numpy.meshgrid(numpy.arange(parsed_detector_dict[p]['max_ss'] - parsed_detector_dict[p]['min_ss'] + 1), - numpy.arange(parsed_detector_dict[p]['max_fs'] - parsed_detector_dict[p]['min_fs'] + 1), + i, j = numpy.meshgrid(numpy.arange(detector['panels'][p]['max_ss'] - detector['panels'][p]['min_ss'] + 1), + numpy.arange(detector['panels'][p]['max_fs'] - detector['panels'][p]['min_fs'] + 1), indexing='ij') - # # make the y-x ( ss, fs ) vectors, using complex notation - dx = parsed_detector_dict[p]['fs'][1] + 1J * parsed_detector_dict[p]['fs'][0] - dy = parsed_detector_dict[p]['ss'][1] + 1J * parsed_detector_dict[p]['ss'][0] - r_0 = parsed_detector_dict[p]['corner_y'] + 1J * parsed_detector_dict[p]['corner_x'] + dx = detector['panels'][p]['fsy'] + 1J * detector['panels'][p]['fsx'] + dy = detector['panels'][p]['ssy'] + 1J * detector['panels'][p]['ssx'] + r_0 = detector['panels'][p]['cny'] + 1J * detector['panels'][p]['cnx'] # r = i * dy + j * dx + r_0 # - y[parsed_detector_dict[p]['min_ss']: parsed_detector_dict[p]['max_ss'] + 1, - parsed_detector_dict[p]['min_fs']: parsed_detector_dict[p]['max_fs'] + 1] = r.real + y[detector['panels'][p]['min_ss']: detector['panels'][p]['max_ss'] + 1, + detector['panels'][p]['min_fs']: detector['panels'][p]['max_fs'] + 1] = r.real - x[parsed_detector_dict[p]['min_ss']: parsed_detector_dict[p]['max_ss'] + 1, - parsed_detector_dict[p]['min_fs']: parsed_detector_dict[p]['max_fs'] + 1] = r.imag + x[detector['panels'][p]['min_ss']: detector['panels'][p]['max_ss'] + 1, + detector['panels'][p]['min_fs']: detector['panels'][p]['max_fs'] + 1] = r.imag r = numpy.sqrt(numpy.square(x) + numpy.square(y)) return x, y, r - -def coffset_from_geometry_file(fnam): - """Extracts detector distance information from a geometry file. - - Extracts detector distance offset information from a CrystFEL-style geometry file. - - Args: - - fnam (str): geometry filename. - - Returns: - - coffset (float): the detector distance offset - """ - - f = open(fnam, 'r') - f_lines = f.readlines() - f.close() - - coffset = 0.0 - - for line in f_lines: - if line.startswith('coffset'): - coffset = float(line.split('=')[1].split(';')[0]) - - return coffset - - -def res_from_geometry_file(fnam): - """Extracts pixel resolution information from a geometry file. - - Extracts pixel resolution information from a CrystFEL-style geometry file. - - Args: - - fnam (str): geometry filename. - - Returns: - - res (float): the pixel resolution - """ - - f = open(fnam, 'r') - f_lines = f.readlines() - f.close() - - res = None - - for line in f_lines: - if line.startswith('res'): - res = float(line.split('=')[1].split(';')[0]) - - return res