Skip to content

Commit 4bd5325

Browse files
authored
Merge 4601903 into 55144ce
2 parents 55144ce + 4601903 commit 4bd5325

File tree

4 files changed

+56
-14
lines changed

4 files changed

+56
-14
lines changed
Binary file not shown.

pydatalab/src/pydatalab/apps/xrd/blocks.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
from pydatalab.logger import LOGGER
1313
from pydatalab.mongo import flask_mongo
1414

15-
from .utils import parse_xrdml
15+
from .utils import parse_rasx_zip, parse_xrdml
1616

1717

1818
class XRDBlock(DataBlock):
1919
blocktype = "xrd"
2020
name = "Powder XRD"
2121
description = "Visualize XRD patterns and perform simple baseline corrections."
22-
accepted_file_extensions = (".xrdml", ".xy", ".dat", ".xye")
22+
accepted_file_extensions = (".xrdml", ".xy", ".dat", ".xye", ".rasx")
2323

2424
defaults = {"wavelength": 1.54060}
2525

@@ -38,6 +38,8 @@ def load_pattern(
3838

3939
if ext == ".xrdml":
4040
df = parse_xrdml(location)
41+
elif ext == ".rasx":
42+
df = parse_rasx_zip(location)
4143

4244
else:
4345
columns = ["twotheta", "intensity", "error"]

pydatalab/src/pydatalab/apps/xrd/utils.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22
import re
33
import warnings
4+
import zipfile
5+
from pathlib import Path
46
from typing import List, Tuple
57

68
import numpy as np
@@ -146,3 +148,39 @@ def toXY(intensities: List[float], start: float, end: float) -> str:
146148
angles = np.linspace(start, end, num=len(intensities))
147149
xylines = ["{:.5f} {:.3f}\r\n".format(a, i) for a, i in zip(angles, intensities)]
148150
return "".join(xylines)
151+
152+
153+
def parse_rasx_zip(filename: str) -> pd.DataFrame:
154+
"""Parses an RASX zip file and returns a pandas DataFrame with columns
155+
twotheta and intensity.
156+
157+
Parameters:
158+
filename: The file to parse.
159+
160+
"""
161+
# Unzip the file
162+
zip_path = Path(filename)
163+
destination = zip_path.parent / "extracted"
164+
with zipfile.ZipFile(zip_path, "r") as zip_ref:
165+
zip_ref.extractall(destination)
166+
167+
# Find the .txt data file inside the unzipped .rasx archive
168+
# Seems to normally unzip to a folder called "Data0" with one .txt file inside
169+
data_file = None
170+
for file in destination.glob("Data*/*.txt"):
171+
data_file = file
172+
break
173+
174+
if data_file is None:
175+
raise FileNotFoundError("No .txt file found in the .rasx archive.")
176+
177+
xrd_data = pd.read_csv(data_file, sep="\t", header=None)
178+
xrd_data.columns = ["twotheta", "intensity", "imnotsure"]
179+
# Extract the data
180+
# twotheta, intensities = getTwoThetaIntensities(s)
181+
return pd.DataFrame(
182+
{
183+
"twotheta": xrd_data["twotheta"],
184+
"intensity": xrd_data["intensity"],
185+
}
186+
)

pydatalab/tests/apps/test_xrd_block.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ def data_files():
1313

1414
def test_load(data_files):
1515
for f in data_files:
16-
df, y_options = XRDBlock.load_pattern(f)
17-
assert all(y in df.columns for y in y_options)
16+
if f.suffix in XRDBlock.accepted_file_extensions:
17+
df, y_options = XRDBlock.load_pattern(f)
18+
assert all(y in df.columns for y in y_options)
1819

1920

2021
def test_plot(data_files):
2122
f = next(data_files)
22-
df, y_options = XRDBlock.load_pattern(f)
23-
p = selectable_axes_plot(
24-
[df],
25-
x_options=["2θ (°)", "Q (Å⁻¹)", "d (Å)"],
26-
y_options=y_options,
27-
plot_line=True,
28-
plot_points=True,
29-
point_size=3,
30-
)
31-
assert p
23+
if f.suffix in XRDBlock.accepted_file_extensions:
24+
df, y_options = XRDBlock.load_pattern(f)
25+
p = selectable_axes_plot(
26+
[df],
27+
x_options=["2θ (°)", "Q (Å⁻¹)", "d (Å)"],
28+
y_options=y_options,
29+
plot_line=True,
30+
plot_points=True,
31+
point_size=3,
32+
)
33+
assert p

0 commit comments

Comments
 (0)