From 87223a5e260593acc113c26f299ce646fe1729b3 Mon Sep 17 00:00:00 2001 From: Beale John Henry Date: Thu, 16 Jan 2025 13:06:47 +0100 Subject: [PATCH] copies of swissmx related tools --- beamline_tools/edge_scan.py | 104 +++++++++++++++++++++ beamline_tools/swissmx.sh | 7 ++ clen_tools/detector-distance-refinement.py | 3 +- 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 beamline_tools/edge_scan.py create mode 100644 beamline_tools/swissmx.sh diff --git a/beamline_tools/edge_scan.py b/beamline_tools/edge_scan.py new file mode 100644 index 0000000..650997c --- /dev/null +++ b/beamline_tools/edge_scan.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 + +# authors M.Appleby + some code donated by J.Beale + +""" +# aim +Calculate FWHM of x-ray beam from an edge scan + +# protocol +complete edge scan +## IMPORTANT ## +- save data as .txt file + +# usage +python convert-scan-for-pyfai.py -j -s -n + +# output +creates a .png of fit including FWHM title +""" + +import numpy as np +import pandas as pd +import matplotlib.pyplot as plt +from scipy import stats +from scipy.optimize import curve_fit +from scipy import asarray as ar,exp +from math import sqrt, pi +import argparse + +def gaus(x, C, A, x0, sigma): + return C + A*np.exp(-(x-x0)**2/(2*sigma**2)) + + +def getFWHM(filename, output_name): + df = pd.read_csv(filename, sep='\t', index_col=0) + print(df) + #print(df.columns) + + x_label=df.index.name + y_label=df.columns[0] + x_vals = df.index.tolist() + y_vals = df[y_label].tolist() + + plt.plot(x_vals, y_vals, label = 'edge_scan') + plt.ylabel('Intensity (counts)') + plt.xlabel('Motor position (mm)') + + dydx = np.gradient(y_vals, x_vals) + + mean = sum((x_vals)*dydx)/sum(dydx) + sigma = sum(dydx*(mean)**2)/sum(dydx) + print(mean, sigma) + + A= 1/(sigma*2*sqrt(pi)) + print(x_vals, '\\', y_vals) + print(dydx) + dydx[np.isnan(dydx)] = 0 + print(dydx) + + param_optimised,param_covariance_matrix = curve_fit(gaus,x_vals,dydx,p0=[min(y_vals), A, mean,sigma],maxfev=5000) + offset_op, amp_op, x0_op, sigma_op = param_optimised[0], param_optimised[1], param_optimised[2], param_optimised[3] + + print(param_optimised) + + gauss_y = gaus(x_vals,*param_optimised) + FWHM_x = np.abs(2*np.sqrt(2*np.log(2))*sigma_op) + + plt.plot(x_vals, dydx, label = 'derivative') + plt.plot(x_vals,gauss_y,label='Gaussian fit',color ='orange') + #plt.fill_between(x_vals,gauss_y,color='orange',alpha=0.5) + plt.axvspan(x0_op+FWHM_x/2,x0_op-FWHM_x/2, color='green', alpha=0.75, lw=0, label='FWHM = {0}'.format(FWHM_x)) + print(FWHM_x) + #plt.plot(x_label, y_label, data=df) + plt.show() + plt.savefig(output_name+filename.split('/')[-1]+'FWHM_{0}.png'.format(FWHM_x)) + return + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument( + "-p", + "--path", + help="path of input and output file, not currently in use", + type=str, + default="/sf/cristallina/data/p21224/res/pshell/edge_scans/" + ) + parser.add_argument( + "-i", + "--input", + help="location of input file", + type=str, + default="/sf/cristallina/data/p21224/res/pshell/edge_scans/0.5_x/0.5_x" + ) + parser.add_argument( + "-o", + "--output", + help="output path to save figure", + type=str, + default="/sf/cristallina/data/p21224/res/pshell/edge_scans/" + ) + args = parser.parse_args() + getFWHM(args.input, args.output) + + diff --git a/beamline_tools/swissmx.sh b/beamline_tools/swissmx.sh new file mode 100644 index 0000000..2f4d8d7 --- /dev/null +++ b/beamline_tools/swissmx.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +source /sf/cristallina/applications/mx/conda/miniconda/bin/activate +conda activate crmx38 + +cd /sf/cristallina/applications/mx/zamofing_t/ESB_MX/python/SwissMX/ +python swissmx.py diff --git a/clen_tools/detector-distance-refinement.py b/clen_tools/detector-distance-refinement.py index 35a1002..b4f8531 100644 --- a/clen_tools/detector-distance-refinement.py +++ b/clen_tools/detector-distance-refinement.py @@ -87,8 +87,7 @@ def write_crystfel_run( clen, sample_h5_file, clen_geom_file, cell_file, thresho run_sh = open( cryst_run_file, "w" ) run_sh.write( "#!/bin/sh\n\n" ) run_sh.write( "module purge\n" ) - run_sh.write( "module use MX unstable\n" ) - run_sh.write( "module load crystfel/0.10.2-rhel8\n" ) + run_sh.write( "module load crystfel/0.11.1\n" ) run_sh.write( "indexamajig -i {0} \\\n".format( sample_h5_file ) ) run_sh.write( " --output={0}.stream \\\n".format( clen ) ) run_sh.write( " --geometry={0}\\\n".format( clen_geom_file ) )