diff --git a/news/dump.rst b/news/dump.rst new file mode 100644 index 00000000..48e83182 --- /dev/null +++ b/news/dump.rst @@ -0,0 +1,23 @@ +**Added:** + +* dump method to diffraction_object + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/pyproject.toml b/pyproject.toml index 53b7d419..87f8b86a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,8 +40,8 @@ Issues = "https://github.com/diffpy/diffpy.utils/issues" [tool.setuptools-git-versioning] enabled = true template = "{tag}" -dev_template = "{tag}.post{ccount}" -dirty_template = "{tag}.post{ccount}" +dev_template = "{tag}" +dirty_template = "{tag}" [tool.setuptools.packages.find] where = ["src"] # list of folders that contain the packages (["."] by default) diff --git a/src/diffpy/utils/scattering_objects/diffraction_objects.py b/src/diffpy/utils/scattering_objects/diffraction_objects.py index 3cf1a808..b02c5225 100644 --- a/src/diffpy/utils/scattering_objects/diffraction_objects.py +++ b/src/diffpy/utils/scattering_objects/diffraction_objects.py @@ -14,7 +14,7 @@ class Diffraction_object(): def __init__(self, name='', wavelength=None): self.name = name self.wavelength = wavelength - self.scat_quantities = {} + self.scat_quantity = "" self.on_q = [np.empty(1), np.empty(1)] self.on_tth = [np.empty(1), np.empty(1)] self.on_d = [np.empty(1), np.empty(1)] @@ -401,3 +401,21 @@ def on_xtype(self, xtype): elif xtype.lower() in DQUANTITIES: return self.on_d pass + + def dump(self, filepath, xtype=None): + if xtype is None: + xtype = " q" + if xtype == "q": + data_to_save = np.column_stack((self.on_q[0], self.on_q[1])) + elif xtype == "tth": + data_to_save = np.column_stack((self.on_tth[0], self.on_tth[1])) + else: + print(f"WARNING: cannot handle the xtype '{xtype}'") + + with open(filepath, 'w') as f: + f.write(f"[Diffraction_object]\nname = {self.name}\nwavelength = {self.wavelength}\n" + f"scat_quantity = {self.scat_quantity}\n") + for key, value in self.metadata.items(): + f.write(f"{key} = {value}\n") + f.write("\n#### start data\n") + np.savetxt(f, data_to_save, delimiter=" ") diff --git a/src/diffpy/utils/tests/test_diffraction_objects.py b/src/diffpy/utils/tests/test_diffraction_objects.py new file mode 100644 index 00000000..333718e7 --- /dev/null +++ b/src/diffpy/utils/tests/test_diffraction_objects.py @@ -0,0 +1,25 @@ +import numpy as np +from pathlib import Path +from diffpy.utils.scattering_objects.diffraction_objects import Diffraction_object + +def test_dump(tmp_path): + x, y = np.linspace(0, 10, 11), np.linspace(0, 10, 11) + directory = Path(tmp_path) + file = directory / "testfile" + test = Diffraction_object() + test.wavelength = 1.54 + test.name = "test" + test.scat_quantity = "x-ray" + test.insert_scattering_quantity(x, y, "q", metadata={"thing1": 1, "thing2": "thing2"}) + test.dump(file, "q") + with open(file, "r") as f: + actual = f.read() + expected = ("[Diffraction_object]\nname = test\nwavelength = 1.54\nscat_quantity = x-ray\nthing1 = 1\n" + "thing2 = thing2\n\n#### start data\n0.000000000000000000e+00 0.000000000000000000e+00\n" + "1.000000000000000000e+00 1.000000000000000000e+00\n2.000000000000000000e+00 2.000000000000000000e+00\n" + "3.000000000000000000e+00 3.000000000000000000e+00\n4.000000000000000000e+00 4.000000000000000000e+00\n" + "5.000000000000000000e+00 5.000000000000000000e+00\n" + "6.000000000000000000e+00 6.000000000000000000e+00\n7.000000000000000000e+00 7.000000000000000000e+00\n" + "8.000000000000000000e+00 8.000000000000000000e+00\n9.000000000000000000e+00 9.000000000000000000e+00\n" + "1.000000000000000000e+01 1.000000000000000000e+01\n") + assert actual == expected