Files
JFCalibration2/jfcal/helpers.py
T
2026-07-22 16:46:36 +02:00

54 lines
1.6 KiB
Python

import hdf5plugin
import h5py
import numpy as np
import uproot
from pathlib import Path
def row_col_to_file_and_pixel_index(row, col):
shape = (512, 1024)
if row>= shape[0] or col >=shape[1]:
raise ValueError(f'Coordinate {row, col} is outside image shape {shape}')
flat = np.ravel_multi_index((row, col), shape)
file_index = flat//256**2
pixel_index = flat-file_index*256**2
return file_index, pixel_index
def flat_index_to_row_col(flat_idx):
shape = (512, 1024) # 3 rows, 4 columns
row, col = np.unravel_index(flat_idx, shape)
return row, col
def save_hist_data(fname, hist):
with h5py.File(fname, 'w') as f:
f.create_dataset(
"pixel_data",
data=hist.values(),
chunks=(512, 1024, 5),
**hdf5plugin.Bitshuffle(cname="lz4")
)
f['x_center'] = hist.bin_centers()
f['x_edge'] = hist.bin_edges()
def load_hist_data(fname):
with h5py.File(fname) as f:
pixel_data = f['pixel_data'][()]
x_center = f['x_center'][()]
x_edge = f['x_edge'][()]
return pixel_data, x_center, x_edge
def load_gain_from_root_file(fname):
with uproot.open(fname) as f:
gain_hist = f['gain_ADUper1keV_2d'].to_boost()
gain = gain_hist.values()
gain = gain.swapaxes(0,1)
return gain
def get_fname(path, gain, label = 'CuFluo', index = -1):
path = Path(path)
file_sets = [fname for fname in path.glob(f'{label}{gain}*000000.dat')]
file_sets.sort(key = lambda f: str(f).rsplit('_',2)[1])
return file_sets[index] #