Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions src/pymca_zocalo/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,18 @@ def configure_cfg(cfg_file, peaks, energy_kev):
file.writelines(lines)


def parse_elements(energy):
def parse_elements(cutoff_energy, beamline):
"""
Filters a dictionary of element/edge combinations likely to be encountered in an
XRF experiment and returns those that are below the given photon energy.
Args:
energy (float): The energy level to filter the elements by, expressed in
electron volts (eV).
beamline (str): The beamline that the MCA spectrum was recorded on.

Returns:
str: A string representation of the filtered elements. Each element is on
a new line and is formatted as 'symbol = edge'.
dict[str]: A dictionary of the filtered elements and edges. The element and
edge form a key-value pair.
"""
elements = {
"Ti": "K",
Expand Down Expand Up @@ -177,6 +178,19 @@ def parse_elements(energy):
"Hg": "L",
"Pb": "L",
}
# Additional elements for i23
i23_elements = {
"Na": "K",
"K": "K",
"Mg": "K",
"Mo": "L",
"P": "K",
"S": "K",
"Ag": "L",
"Cl": "K",
"Ca": "K",
"Cd": "L",
}

edge_mapper = {
"K": xrl.K_SHELL,
Expand All @@ -186,7 +200,10 @@ def parse_elements(energy):
def _check_edge(symbol, edge):
Z = xrl.SymbolToAtomicNumber(symbol)
shell = edge_mapper[edge]
return float(energy) > xrl.EdgeEnergy(Z, shell) * 1000.0
return float(cutoff_energy) > xrl.EdgeEnergy(Z, shell) * 1000.0

if beamline == "i23":
elements.update(i23_elements)

valid_peaks = {}
for symbol, edge in elements.items():
Expand Down Expand Up @@ -305,6 +322,7 @@ def run_auto_pymca(
visit_dir = Path(*filepath_parts[:6])
rel_dir_path = Path(*filepath_parts[6:-1])
mca_path = src_data_dir / f"{filename_stem}.mca"
beamline = filepath_parts[2]

config_changes = {}
energy_kev = float(beam_energy) / 1000.0
Expand Down Expand Up @@ -337,7 +355,6 @@ def run_auto_pymca(
channel_counts = spectrum_data[:, 1]

if src_cfg_file is None:
beamline = filepath_parts[2]
src_cfg_file = Path("/dls_sw") / beamline / "software/pymca/pymca_new.cfg"
else:
src_cfg_file = Path(src_cfg_file)
Expand Down Expand Up @@ -365,7 +382,8 @@ def run_auto_pymca(

shutil.copyfile(src_data_file, data_file)

peaks = parse_elements(beam_energy - cutoff_offset)
cutoff_energy = beam_energy - cutoff_offset
peaks = parse_elements(cutoff_energy, beamline)

if not data_file.exists():
raise FileNotFoundError(f"File {data_file} does not exist")
Expand Down Expand Up @@ -395,16 +413,16 @@ def run_auto_pymca(
if not fit_data_file.exists():
raise FileNotFoundError(f"Results file {fit_data_file} could not be opened")

peaks = parse_spec_fit(fit_data_file)
fitted_peaks = parse_spec_fit(fit_data_file)

results_file = output_dir / f"{filename_stem}.results.dat"
results_txt = [f"{peak[0]} {peak[1]} {peak[2]}" for peak in peaks]
results_txt = [f"{peak[0]} {peak[1]} {peak[2]}" for peak in fitted_peaks]
results_txt = "\n".join(results_txt)
with open(results_file, "w") as f:
f.write(results_txt)

pymca_output = parse_raw_fluoro(
channel_energy, channel_counts, beam_energy, peaks, cutoff_offset
channel_energy, channel_counts, beam_energy, fitted_peaks, cutoff_offset
)

plot_output_file = src_data_dir / f"{filename_stem}.png"
Expand Down
Loading