#!/usr/bin/python # author J.Beale """ # aim use .poni file from pyFAI or pixel x, y, clen inputs and energy to convert geom file ready for initial hewl processing # usage python update-geom-from-lab6.py -g , -x beam x position (in pixels), -y beam y postion (in pixels), -c camera length (in m), -e pulse energy (in eV), -j name of jungfrau, -p p-group name python update-geom-from-lab6.py -g , -i , -j name of jungfrau, -p p-group name # output creates an updated .geom file with a naming convention __.geom """ from runpy import run_path import pandas as pd import numpy as np import regex as re from scipy import constants from jungfrau_utils import geometry import argparse from datetime import datetime date = datetime.today().strftime('%y%m%d') def calculate_new_corner_positions( beam_x, beam_y, jungfrau ): # import psi detectors from jungfrau_utils detectors = geometry.detector_geometry # get x and y detector corners for the specifc jungfrau detector origin_y = detectors[ jungfrau ].origin_y origin_x = detectors[ jungfrau ].origin_x # make df of current corner positions x_df = pd.DataFrame( origin_x, columns=[ "current_x" ] ) y_df = pd.DataFrame( origin_y, columns=[ "current_y" ] ) corner_df = pd.concat( ( x_df, y_df ), axis=1 ) # calculate new corner positions corner_df[ "new_x" ] = corner_df.current_x.subtract( beam_x ) corner_df[ "new_y" ] = corner_df.current_y.subtract( beam_y ) # drop old positions corner_df = corner_df[[ "new_x", "new_y" ]] return corner_df def scrub_poni( path_to_poni_file ): # open poni file poni_file = open( path_to_poni_file, "r" ).read() # regex patterns to scrub poni data clen_m_pattern = r"Distance:\s(\d\.\d*)" poni1_m_pattern = r"Poni1:\s(\d\.\d*)" poni2_m_pattern = r"Poni2:\s(\d\.\d*)" wave_pattern = r"Wavelength:\s(\d\.\d*)e(-\d+)" # regex seach clen = re.search( clen_m_pattern, poni_file ).group( 1 ) poni1_m = re.search( poni1_m_pattern, poni_file ).group( 1 ) poni2_m = re.search( poni2_m_pattern, poni_file ).group( 1 ) wave = re.search( wave_pattern, poni_file ).group( 1, 2 ) # calulate proper wavelength wave = float(wave[0]) * np.float_power( 10, int( wave[1]) ) # calculate beam_centre poni1_p = float( poni1_m ) / 0.000075 poni2_p = float( poni2_m ) / 0.000075 # calculate beam energy in eV eV = ( ( constants.c * constants.h ) / wave ) / constants.electron_volt # return poni1 = y, poni2 = x and energy return poni1_p, poni2_p, eV, round( float( clen ), 5 ) def write_new_positions( path_to_geom, beam_x, beam_y, clen, energy, jungfrau, p_group ): # open current geometry file current_geom_file = open( path_to_geom, "r" ).read() # calculate new corner positions corner_df = calculate_new_corner_positions( beam_x, beam_y, jungfrau ) # replace current corner positions with new ones for i in range(0, 16): # x and y positions new_x, new_y = round( corner_df.new_x[i], 3 ), round( corner_df.new_y[i], 3 ) # input new x position current_pattern_x = r"p" + re.escape( str(i) ) + r"/corner_x = -?\d+\.\d+" new_pattern_x = r"p" + re.escape( str(i) ) + r"/corner_x = " + str( new_x ) current_geom_file = re.sub( current_pattern_x, new_pattern_x, current_geom_file ) # input new y position current_pattern_y = r"p" + re.escape( str(i) ) + r"/corner_y = -?\d+\.\d+" new_pattern_y = r"p" + re.escape( str(i) ) + r"/corner_y = " + str( new_y ) current_geom_file = re.sub( current_pattern_y, new_pattern_y, current_geom_file ) # input new clen current_clen = r"clen = \d\.\d+" new_clen = r"clen = " + str( clen ) current_geom_file = re.sub( current_clen, new_clen, current_geom_file ) # input new energy current_energy = r"photon_energy = \d+" new_energy = r"photon_energy = " + str( energy ) current_geom_file = re.sub( current_energy, new_energy, current_geom_file ) # create geom new file new_geom_name = "{0}_{1}_{2}.geom".format( jungfrau, p_group, date ) # write new geom file f = open( new_geom_name, "w" ) f.write( current_geom_file ) f.close() # return new_geom_name return new_geom_name if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "-g", "--geom", help="give the path to the cristallina 8M geom file to be updated", type=str, default="/sf/cristallina/applications/mx/crmx-tools/geom_files/crmx8M_230322.geom", ) parser.add_argument( "-x", "--beam_x", help="beam_x in pixels", type=float, default=1603.73 ) parser.add_argument( "-y", "--beam_y", help="beam_y in pixels", type=float, default=1661.99 ) parser.add_argument( "-j", "--jungfrau", help="name of jungfrau, i.e., JF17T16V01 for Cristallina 8M", type=str, default="JF17T16V01" ) parser.add_argument( "-c", "--clen", help="detector distance in m", type=int, default=0.111 ) parser.add_argument( "-e", "--energy", help="photon energy", type=int, default=12400 ) parser.add_argument( "-i", "--poni", help="path to poni file", type=str, ) parser.add_argument( "-p", "--p_group", help="p-group name", type=str, ) args = parser.parse_args() # run geom converter if args.p_group is None: print( "you must specify a p-group that the .geom file is associated with\n\nfor example -p p20560" ) elif args.poni is not None: print( "reading poni file" ) beam_y, beam_x, eV, clen = scrub_poni( args.poni ) print( "beam x, beam_y = {0}, {1}\nphoton_energy = {2}\nclen = {3} m".format( beam_x, beam_y, eV, clen ) ) new_geom_name = write_new_positions( args.geom, beam_x, beam_y, clen, eV, args.jungfrau, args.p_group ) print( "updated .geom file with poni calculations\n new .geom = {0}".format( new_geom_name ) ) else: print( "manually input positions" ) print( "beam x, beam_y = {0}, {1}\nphoton_energy = {2}\nclen = {3}".format( args.beam_x, args.beam_y, args.energy, args.clen ) ) new_geom_name = write_new_positions( args.geom, args.beam_x, args.beam_y, args.clen, args.energy, args.jungfrau, args.p_group ) print( "updated .geom file with poni calculations\nnew .geom = {0}".format( new_geom_name ) )