Files
crystfel_tools/pyfai-tools/update-geom-from-lab6.py
Beale John Henry 201ead6441 updated help
2024-01-31 10:58:37 +01:00

211 lines
7.2 KiB
Python

#!/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
<no poni file>
python update-geom-from-lab6.py -g <path-to-geom-file>, -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
<with poni file>
python update-geom-from-lab6.py -g <path-to-geom-file>, -i <path-to-poni-file>,
-j name of jungfrau, -p p-group name
# output
creates an updated .geom file with a naming convention <jungfrau>_<p-group>_<date>.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
# get number of panels
panels = len( origin_y )
# 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, panels
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, panels = calculate_new_corner_positions( beam_x, beam_y, jungfrau )
# replace current corner positions with new ones
for i in range(0, panels):
# 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+\.?\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 geom file to be updated.",
type=str,
required=True
)
parser.add_argument(
"-j",
"--jungfrau",
help="SwissFEL name of jungfrau, i.e., JF17T16V01 for Cristallina 8M. Default = JF17T16V01.",
type=str,
required=True
)
parser.add_argument(
"-p",
"--p_group",
help="p-group name",
type=str,
required=True
)
parser.add_argument(
"-i",
"--poni",
help="AUTOMATIC option to path to poni file from pyFAI calculation. simply give <path-to-poni-file> all values taken from this.",
type=str,
)
parser.add_argument(
"-x",
"--beam_x",
help="manual option to add beam_x in pixels. Must be a float. You must also give following arguements.",
type=float
)
parser.add_argument(
"-y",
"--beam_y",
help="manual option to add beam_y in pixels. Must be a float. You must also give following arguements.",
type=float
)
parser.add_argument(
"-c",
"--clen",
help="manual option to add detector distance in m. Must be a float. You must also give following arguements.",
type=float
)
parser.add_argument(
"-e",
"--energy",
help="manual option to add photon energy in eV. Must be an int",
type=int
)
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} pixels\nphoton_energy = {2} eV\nclen = {3} m".format( round( beam_x, 3 ), round( beam_y, 3 ), 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\nnew .geom = {0}".format( new_geom_name ) )
print( "please check the result" )
else:
print( "manually input positions" )
print( "beam x, beam_y = {0}, {1} pixels\nphoton_energy = {2} eV\nclen = {3} m".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 ) )
print( "please check the result" )