Skip to content

Commit acfc00d

Browse files
committed
dump method for diffraction_objects with test
1 parent 14ad3d9 commit acfc00d

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: src/diffpy/utils/scattering_objects/diffraction_objects.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Diffraction_object():
1414
def __init__(self, name='', wavelength=None):
1515
self.name = name
1616
self.wavelength = wavelength
17-
self.scat_quantities = {}
17+
self.scat_quantity = ""
1818
self.on_q = [np.empty(1), np.empty(1)]
1919
self.on_tth = [np.empty(1), np.empty(1)]
2020
self.on_d = [np.empty(1), np.empty(1)]
@@ -401,3 +401,21 @@ def on_xtype(self, xtype):
401401
elif xtype.lower() in DQUANTITIES:
402402
return self.on_d
403403
pass
404+
405+
def dump(self, filepath, xtype=None):
406+
if xtype is None:
407+
xtype = " q"
408+
if xtype == "q":
409+
data_to_save = np.column_stack((self.on_q[0], self.on_q[1]))
410+
elif xtype == "tth":
411+
data_to_save = np.column_stack((self.on_tth[0], self.on_tth[1]))
412+
else:
413+
print(f"WARNING: cannot handle the xtype '{xtype}'")
414+
415+
with open(filepath, 'w') as f:
416+
f.write(f"[Diffraction_object]\nname = {self.name}\nwavelength = {self.wavelength}\n"
417+
f"scat_quantity = {self.scat_quantity}\n")
418+
for key, value in self.metadata.items():
419+
f.write(f"{key} = {value}\n")
420+
f.write("\n#### start data\n")
421+
np.savetxt(f, data_to_save, delimiter=" ")

Diff for: src/diffpy/utils/tests/test_diffraction_objects.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
from pathlib import Path
3+
from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object
4+
5+
def test_dump(tmp_path):
6+
x, y = np.linspace(0, 10, 11), np.linspace(0, 10, 11)
7+
directory = Path(tmp_path)
8+
file = directory / "testfile"
9+
test = Diffraction_object()
10+
test.wavelength = 1.54
11+
test.name = "test"
12+
test.scat_quantity = "x-ray"
13+
test.insert_scattering_quantity(x, y, "q", metadata={"thing1": 1, "thing2": "thing2"})
14+
test.dump(file, "q")
15+
with open(file, "r") as f:
16+
actual = f.read()
17+
expected = ("[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n"
18+
"thing2 = thing2\n\n#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n"
19+
"1.000000000000000000e+00 1.000000000000000000e+00\n2.000000000000000000e+00 2.000000000000000000e+00\n"
20+
"3.000000000000000000e+00 3.000000000000000000e+00\n4.000000000000000000e+00 4.000000000000000000e+00\n"
21+
"5.000000000000000000e+00 5.000000000000000000e+00\n"
22+
"6.000000000000000000e+00 6.000000000000000000e+00\n7.000000000000000000e+00 7.000000000000000000e+00\n"
23+
"8.000000000000000000e+00 8.000000000000000000e+00\n9.000000000000000000e+00 9.000000000000000000e+00\n"
24+
"1.000000000000000000e+01 1.000000000000000000e+01\n")
25+
assert actual == expected

0 commit comments

Comments
 (0)