Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 33 additions & 6 deletions lib/xrayutilities/gridder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=""):
"""
Expand All @@ -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
Expand Down
30 changes: 12 additions & 18 deletions lib/xrayutilities/gridder2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
32 changes: 19 additions & 13 deletions lib/xrayutilities/gridder3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,18 @@ 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
self.ymax = 0
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
Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_gridder1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import numpy
import xrayutilities as xu
from xrayutilities.exception import InputError


class TestGridder1D(unittest.TestCase):
Expand Down Expand Up @@ -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()
Loading