diff --git a/CHANGES.txt b/CHANGES.txt index ac013be2..e742e12c 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,5 @@ +* add Gridder1D.SetResolution and use it more consistently in all Gridder + v1.8.0, 2026-07-21 * replace deprecated scipy.odr usage with direct odrpack calls diff --git a/lib/xrayutilities/gridder.py b/lib/xrayutilities/gridder.py index bf198757..435dab24 100644 --- a/lib/xrayutilities/gridder.py +++ b/lib/xrayutilities/gridder.py @@ -131,6 +131,15 @@ def __call__(self): override """ + def SetResolution(self, *resolution): + """ + Reset the grid resolution and discard currently gridded data. + + The number of resolution arguments depends on the dimensionality of the + concrete gridder. + """ + raise NotImplementedError + def Normalize(self, bool): """ set or unset the normalization flag. Normalization needs to be done to @@ -187,14 +196,16 @@ def Clear(self): class Gridder1D(Gridder): def __init__(self, nx): Gridder.__init__(self) - if nx <= 0: - raise InputError("nx must be a positiv integer!") - - self.nx = nx self.xmin = 0 self.xmax = 0 - self._gdata = numpy.zeros(nx, dtype=numpy.double) - self._gnorm = numpy.zeros(nx, dtype=numpy.double) + self.SetResolution(nx) + + def _allocate_memory(self): + """ + Allocate data and normalization arrays for the current resolution. + """ + self._gdata = numpy.zeros(self.nx, dtype=numpy.double) + self._gnorm = numpy.zeros(self.nx, dtype=numpy.double) def savetxt(self, filename, header=""): """ @@ -215,6 +226,22 @@ def savetxt(self, filename, header=""): fmt="%.6g %.4g", ) + def SetResolution(self, nx): + """ + Reset the 1D grid resolution and discard currently gridded data. + + Parameters + ---------- + nx : int + Number of points in x-direction. + """ + if nx <= 0: + raise InputError("nx must be a positive integer!") + + self.nx = nx + + self._allocate_memory() + def __get_xaxis(self): """ Returns the xaxis of the gridder diff --git a/lib/xrayutilities/gridder2d.py b/lib/xrayutilities/gridder2d.py index 920f16e1..c145cd86 100644 --- a/lib/xrayutilities/gridder2d.py +++ b/lib/xrayutilities/gridder2d.py @@ -28,26 +28,16 @@ class Gridder2D(Gridder): def __init__(self, nx, ny): Gridder.__init__(self) - # check input - if nx <= 0 or ny <= 0: - raise exception.InputError( - "Neither nx nor ny can be smallerthan 1!" - ) - self.xmin = None self.ymin = None self.xmax = None self.ymax = None - self.nx = nx - self.ny = ny - - self._allocate_memory() + self.SetResolution(nx, ny) def _allocate_memory(self): """ - Class method to allocate memory for the gridder based on the nx, ny - class attributes. + Allocate data and normalization arrays for the current resolution. """ self._gdata = numpy.zeros((self.nx, self.ny), dtype=numpy.double) @@ -77,16 +67,20 @@ def savetxt(self, filename, header=""): def SetResolution(self, nx, ny): """ - Reset the resolution of the gridder. In this case the original data - stored in the object will be deleted. + Reset the 2D grid resolution and discard currently gridded data. Parameters ---------- - nx : int - number of points in x-direction - ny : int - number of points in y-direction + nx : int + Number of points in x-direction. + ny : int + Number of points in y-direction. """ + if nx <= 0 or ny <= 0: + raise exception.InputError( + "Neither nx nor ny can be smaller than 1!" + ) + self.nx = nx self.ny = ny diff --git a/lib/xrayutilities/gridder3d.py b/lib/xrayutilities/gridder3d.py index 1a89847d..70827e4e 100644 --- a/lib/xrayutilities/gridder3d.py +++ b/lib/xrayutilities/gridder3d.py @@ -28,12 +28,6 @@ class Gridder3D(Gridder): def __init__(self, nx, ny, nz): Gridder.__init__(self) - # check input - if nx <= 0 or ny <= 0 or nz <= 0: - raise exception.InputError( - "None of nx, ny and nz can be smaller than 1!" - ) - self.xmin = 0 self.xmax = 0 self.ymin = 0 @@ -41,16 +35,11 @@ def __init__(self, nx, ny, nz): self.zmin = 0 self.zmax = 0 - self.nx = nx - self.nz = nz - self.ny = ny - - self._allocate_memory() + self.SetResolution(nx, ny, nz) def _allocate_memory(self): """ - Class method to allocate memory for the gridder based on the nx, ny - class attributes. + Allocate data and normalization arrays for the current resolution. """ self._gdata = numpy.zeros( (self.nx, self.ny, self.nz), dtype=numpy.double @@ -60,6 +49,23 @@ class attributes. ) def SetResolution(self, nx, ny, nz): + """ + Reset the 3D grid resolution and discard currently gridded data. + + Parameters + ---------- + nx : int + Number of points in x-direction. + ny : int + Number of points in y-direction. + nz : int + Number of points in z-direction. + """ + if nx <= 0 or ny <= 0 or nz <= 0: + raise exception.InputError( + "None of nx, ny and nz can be smaller than 1!" + ) + self.nx = nx self.ny = ny self.nz = nz diff --git a/tests/test_gridder1d.py b/tests/test_gridder1d.py index 7ac872e9..5b3f0d15 100644 --- a/tests/test_gridder1d.py +++ b/tests/test_gridder1d.py @@ -19,6 +19,7 @@ import numpy import xrayutilities as xu +from xrayutilities.exception import InputError class TestGridder1D(unittest.TestCase): @@ -48,6 +49,21 @@ def test_gridder1d_data(self): self.gridder.data[i], self.data[i], places=12 ) + def test_gridder1d_set_resolution(self): + gridder = xu.Gridder1D(self.num) + gridder(self.x, self.data) + + new_num = self.num + 7 + gridder.SetResolution(new_num) + + self.assertEqual(gridder.nx, new_num) + self.assertEqual(gridder.data.shape, (new_num,)) + self.assertTrue(numpy.all(gridder.data == 0)) + + def test_gridder1d_set_resolution_invalid(self): + with self.assertRaises(InputError): + xu.Gridder1D(self.num).SetResolution(0) + if __name__ == "__main__": unittest.main()