Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update create_regular methods to remove cellsize divisibility check #673

Merged
merged 6 commits into from
Jun 23, 2023
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
11 changes: 1 addition & 10 deletions cf/dimensioncoordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,7 @@ def create_regular(cls, args, units=None, standard_name=None, bounds=True):
:Parameters:

args: sequence of numbers
A sequence of three numeric values. The first two values in the
sequence represent the coordinate range (see the bounds
parameter for details), and the third value represents the
cellsize.
{{regular args}}

bounds: `bool`, optional
If True (the default) then the given range represents
Expand Down Expand Up @@ -311,12 +308,6 @@ def create_regular(cls, args, units=None, standard_name=None, bounds=True):
f"negative cellsize ({cellsize})"
)

if range_diff % cellsize != 0:
raise ValueError(
f"The range of the dimension ({range_diff}) must be "
f"divisible by the cellsize ({cellsize})"
)

if standard_name is not None and not isinstance(standard_name, str):
raise ValueError("standard_name must be either None or a string.")

Expand Down
12 changes: 12 additions & 0 deletions cf/docstring/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@
specified with or without the ``${...}`` syntax. For
instance, the following are equivalent: ``'base'`` and
``'${base}'``.""",
# regular args
"{{regular args}}":
"""A sequence of three numeric values. The first two values in
the sequence represent the coordinate range (see the bounds
parameter for details), and the third value represents the
cellsize.

.. note:: The cellsize does not have to explicitly divide into
the range of the given dimension. But as it follows
`numpy.arange` while creating the points, one should
verify that that the number of grid points are
returned as expected.""",
# ----------------------------------------------------------------
# Method description substitutions (4 levels of indentation)
# ----------------------------------------------------------------
Expand Down
10 changes: 2 additions & 8 deletions cf/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,10 @@ def create_regular(cls, x_args, y_args, bounds=True):
:Parameters:

x_args: sequence of numbers
A sequence of three numeric values. The first two values in the
sequence represent the coordinate range (see the bounds
parameter for details), and the third value represents the
cellsize.
{{regular args}}

y_args: sequence of numbers
A sequence of three numeric values. The first two values in the
sequence represent the coordinate range (see the bounds
parameter for details), and the third value represents the
cellsize.
{{regular args}}

bounds: `bool`, optional
If True (default), bounds will be created
Expand Down
8 changes: 0 additions & 8 deletions cf/test/test_DimensionCoordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,6 @@ def test_DimensionCoordinate_create_regular(self):
standard_name="longitude",
)

# Cellsize not divisible by range
with self.assertRaises(ValueError):
cf.DimensionCoordinate.create_regular(
(-180, 180, 50),
units="degrees_east",
standard_name="longitude",
)

# Test decreasing case
longitude_decreasing = cf.DimensionCoordinate.create_regular(
(180, -180, -1), units="degrees_east", standard_name="longitude"
Expand Down
33 changes: 29 additions & 4 deletions cf/test/test_Domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,6 @@ def test_Domain_create_regular(self):
self.assertTrue(np.allclose(longitude.array, x_points))
self.assertTrue(np.allclose(latitude.array, y_points))

# Test dx and dy not divisors of the range
with self.assertRaises(ValueError):
cf.Domain.create_regular((-180, 180, 61), (-90, 90, 46))

# Test if range difference in x_range is greater than 360
with self.assertRaises(ValueError):
cf.Domain.create_regular((-180, 190, 1), (-90, 90, 1))
Expand Down Expand Up @@ -366,6 +362,35 @@ def test_Domain_create_regular(self):
np.allclose(latitude_no_bounds.array, y_points_no_bounds)
)

# Test for the given specific domain
ymin, ymax, dy = 45.0, 90.0, 0.0083333
xmin, xmax, dx = 250.0, 360.0, 0.0083333

domain_specific = cf.Domain.create_regular(
(xmin, xmax, dx), (ymin, ymax, dy)
)
self.assertIsInstance(domain_specific, cf.Domain)

x_bounds_specific = np.arange(xmin, xmax + dx, dx)
y_bounds_specific = np.arange(ymin, ymax + dy, dy)

x_points_specific = (
x_bounds_specific[:-1] + x_bounds_specific[1:]
) / 2
y_points_specific = (
y_bounds_specific[:-1] + y_bounds_specific[1:]
) / 2

longitude_specific = domain_specific.construct("longitude")
latitude_specific = domain_specific.construct("latitude")

self.assertTrue(
np.allclose(longitude_specific.array - x_points_specific, 0)
)
self.assertTrue(
np.allclose(latitude_specific.array - y_points_specific, 0)
)


if __name__ == "__main__":
print("Run date:", datetime.datetime.now())
Expand Down