organised tools into directories - made 16M pyfai script work

This commit is contained in:
Beale John Henry
2023-03-22 14:03:00 +01:00
parent cfcc9b5941
commit b1ce7a2215
7 changed files with 899 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env python3
# author J.Beale
"""
# aim
- -16M=varient for large detectors
make image file to input into pyFAI for initial detector beam-centre and detector distance calibration
refer to Cristallina8M-calibration for complete protocol
https://docs.google.com/document/d/1RoeUUogvRxX4M6uqGwkjf3dVJBabiMUx4ZxwcA5e9Dc/edit#
# protocol
take scan of LaB6
## IMPORTANT ##
- save image as photon-counts - in slic/run_control scale=beam energy
- detector_geometry=TRUE - saves detector panels in their correct orientation
## scan inputs ##
- <0.01 trans
- motor scan > 10 um per step
- 10 images per step, 100 steps
- use scan.json as input for this script
# usage
python make-tiff.py -j <jugfrau-name> -s <path to scan file> -n <name of output file>
# output
creates a .npy file that can be loaded directly into pyFAI
"""
# modules
from matplotlib import pyplot as plt
import numpy as np
from sfdata import SFScanInfo
from tqdm import tqdm
import argparse
def convert_image( path_to_json, jungfrau, name ):
# opens scan
print( "opening scane" )
scan = SFScanInfo( path_to_json )
# steps in scane
nsteps = len(scan)
# define step ch and im_shape
step = scan[0]
ch = step[jungfrau]
img_shape = ch[0].shape
print("stepping through scan and averaging images at each step")
# step through scan and average files from each positions
imgs_shape = (nsteps, *img_shape)
imgs = np.empty(imgs_shape)
for i, subset in tqdm(enumerate(scan)):
# go through data in_batches so you don't run out of memory
ch = subset[jungfrau]
mean = np.zeros(img_shape)
for _indices, batch in ch.in_batches(size=2):
mean += np.mean(batch, axis=0)
# take mean of means for batch opened data
imgs[i] = mean
print( "done" )
# sum averaged imaged
print( "final average" )
mean_image = imgs.mean(axis=0)
print("done")
# output to file
print( "saving to .npy = {0}".format( name ) )
np.save( "{0}.npy".format( name ), mean_image )
print( "done" )
# create plot of summed, averaged scan
fig, ax = plt.subplots()
ax.imshow(mean_image, vmin=0, vmax=1000)
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-j",
"--jungfrau",
help="name of the jungfrau used, i.e., JF17T16V01 for Cristallina MX",
type=str,
default="JF17T16V01"
)
parser.add_argument(
"-s",
"--scan",
help="path to json scan file",
type=str,
default="/sf/cristallina/data/p20590/raw/run0003/meta/scan.json"
)
parser.add_argument(
"-n",
"--name",
help="name of output file",
type=str,
default="mean_scan"
)
args = parser.parse_args()
convert_image( args.scan, args.jungfrau, args.name )

View File

@@ -0,0 +1,85 @@
#!/usr/bin/env python3
# author J.Beale
"""
# aim
make image file to input into pyFAI for initial detector beam-centre and detector distance calibration
refer to Cristallina8M-calibration for complete protocol
https://docs.google.com/document/d/1RoeUUogvRxX4M6uqGwkjf3dVJBabiMUx4ZxwcA5e9Dc/edit#
# protocol
take scan of LaB6
## IMPORTANT ##
- save image as photon-counts - in slic/run_control scale=beam energy
- detector_geometry=TRUE - saves detector panels in their correct orientation
## scan inputs ##
- <0.01 trans
- motor scan > 10 um per step
- 10 images per step, 100 steps
- use scan.json as input for this script
# usage
python make-tiff.py -j <jugfrau-name> -s <path to scan file> -n <name of output file>
# output
creates a .npy file that can be loaded directly into pyFAI
"""
# modules
from matplotlib import pyplot as plt
import numpy as np
from sfdata import SFScanInfo
from tqdm import tqdm
import argparse
def convert_image( path_to_json, jungfrau, name ):
# opens scan
scan = SFScanInfo( path_to_json )
# step through scan and average files from each positions
mean_image = []
for step in tqdm( enumerate(scan) ):
# step is a SFDataFiles object
subset = step[1]
mean = np.mean( subset[ jungfrau ].data, axis=0 )
mean_image.append(mean)
# sum averaged imaged
sum_image = np.sum( mean_image, axis=0 )
# output to file
np.save( "{0}.npy".format( name ), sum_image )
# create plot of summed, averaged scan
fig, ax = plt.subplots()
ax.imshow(sum_image, vmin=0, vmax=1000)
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-j",
"--jungfrau",
help="name of the jungfrau used, i.e., JF17T16V01 for Cristallina MX",
type=str,
default="JF17T16V01"
)
parser.add_argument(
"-s",
"--scan",
help="path to json scan file",
type=str,
default="/sf/cristallina/data/p20590/raw/run0003/meta/scan.json"
)
parser.add_argument(
"-n",
"--name",
help="name of output file",
type=str,
default="sum_mean_scan"
)
args = parser.parse_args()
convert_image( args.scan, args.jungfrau, args.name )

View File

@@ -0,0 +1,12 @@
# Nota: C-Order, 1 refers to the Y axis, 2 to the X axis
# Calibration done at Thu Mar 16 16:56:31 2023
poni_version: 2
Detector: Detector
Detector_config: {"pixel1": 7.5e-08, "pixel2": 7.5e-08, "max_shape": [3333, 3212]}
Distance: 0.00012198044363190573
Poni1: 0.0001245625996392014
Poni2: 0.00012028397296269736
Rot1: -0.0013017934228372007
Rot2: 0.0011373661117621663
Rot3: -0.00014487988888865335
Wavelength: 1.0088217935980494e-10

View File

@@ -0,0 +1,168 @@
#!/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",
"--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(
"-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.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.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.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 ) )