#!/usr/bin/python import pandas as pd import numpy as np import regex as re from scipy import constants import argparse from datetime import datetime date = datetime.today().strftime('%y%m%d') def calculate_new_corner_positions( beam_x, beam_y ): # make df of current corner positions positions = { "current_x" : [ 607, 1646, 607, 1646, 607, 1646, 538, 1577, 538, 1577, 538, 1577, 538, 3212, 514, 3143 ], "current_y" : [ 0, 69, 550, 619, 1100, 1169, 1650, 1719, 2200, 2269, 2750, 2819, 597, 667, 1636, 1706 ] } corner_df = pd.DataFrame( positions ) # 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.000000075 poni2_p = float( poni2_m ) / 0.000000075 # 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 )*1000, 5 ) def write_new_positions( path_to_geom, beam_x, beam_y, clen, energy ): # 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 ) # 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 geom_start = path_to_geom[:-5] new_geom_name = "{0}_{1}.geom".format( geom_start, 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", "--path_to_geom", help="give the path to the cristallina 8M geom file to be updated", type=str, default="/sf/cristallina/data/p20558/work/geom/optimised_geom_file/p20558_hewl_op.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( "-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( "-p", "--poni", help="path to poni file", type=str, ) args = parser.parse_args() # run geom converter # import pdb;pdb.set_trace() if 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}".format( beam_x, beam_y, eV, clen ) ) new_geom_name = write_new_positions( args.path_to_geom, beam_x, beam_y, clen, eV ) 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.path_to_geom, args.beam_x, args.beam_y, args.clen, args.energy ) print( "updated .geom file with poni calculations\nnew .geom = {0}".format( new_geom_name ) )