Raise RuntimeWarning instead of an exception for irregular grid spacing#1530
Raise RuntimeWarning instead of an exception for irregular grid spacing#1530
Conversation
1b58645 to
17ca994
Compare
| @@ -95,9 +97,12 @@ def dataarray_to_matrix(grid): | |||
| coord_incs = coord[1:] - coord[0:-1] | |||
| coord_inc = coord_incs[0] | |||
| if not np.allclose(coord_incs, coord_inc): | |||
There was a problem hiding this comment.
Could we try to work out the np.assert_allclose atol or rtol argument values which won't matter for GMT? I remember reading somewhere that GMT uses single-precision (4-byte/32-bit) floating point to store the grids (see https://github.com/GenericMappingTools/gmt/blob/32296e512b173cfdd2c3e8da1d2735e49b79f02a/doc/rst/source/explain_float.rst_#L5-L7). So how many decimal places is that?
There was a problem hiding this comment.
Although GMT grids are stored using 4-bytes, the grid metadata (range, inc, and others) are stored using double-precision floating points (8 bytes) (see https://github.com/GenericMappingTools/gmt/blob/8ee9060d8620a7d0e599e3f4990303b1517aed02/src/gmt_resources.h#L413).
I'm looking at @leouieda's example data in #1468. The "longitude" dimension has the following values:
>>> grid.coords["longitude"].values
array([-180. , -179.8333, -179.6667, ..., 179.6667, 179.8333,
180. ], dtype=float32)As you can see, the grid spacings can be either 0.1666 or 0.1667 even if we ignore the floating-point precision issue.
| coord_incs = coord[1:] - coord[0:-1] | ||
| coord_inc = coord_incs[0] |
There was a problem hiding this comment.
If the coordinate increments can be slightly different, is it safe to just take the first one, or should we take the mean/average value (inside the if not np.allclose(coord_incs, coord_inc): block)?
There was a problem hiding this comment.
Yes, using the mean spacing sounds better.
There was a problem hiding this comment.
Or we can calculate the increment from the minimum, maximum and length of the coordinates? Take the coordinates in #1530 (comment) as an example, the array grid.coords["longitude"].values has a size of 2161, and the increment is (180-(-180))/(2161-1) = 0.1666666...
There was a problem hiding this comment.
Or we can calculate the increment from the minimum, maximum and length of the coordinates? Take the coordinates in #1530 (comment) as an example, the array
grid.coords["longitude"].valueshas a size of 2161, and the increment is (180-(-180))/(2161-1) = 0.1666666...
Implemented in d5d98c8.
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
…ng (GenericMappingTools#1530) * Raise a warning intsead an exception for irregular grid spacing * Calcuate grid spacing if irregular spacing is detected * Update pygmt/tests/test_clib.py Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Description of proposed changes
GMT only supports regular grid spacing. So when passing a grid to GMT API, it's always assumed that the grid has regular spacings in x and y dimensions.
When irregular grid spacings are detected, we raised an exception, but the detection may be too strict (see #1468 for the issue report). In this PR, a warning is raised, instead of an exception.
Fixes #1468
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.Slash Commands
You can write slash commands (
/command) in the first line of a comment to performspecific operations. Supported slash commands are:
/format: automatically format and lint the code/test-gmt-dev: run full tests on the latest GMT development version