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

world_to_array_index_values does not round to integers as APE 14 and astropy.wcs indicate it should #524

Open
WilliamJamieson opened this issue Nov 11, 2024 · 0 comments · May be fixed by #525

Comments

@WilliamJamieson
Copy link
Collaborator

Currently,

gwcs/gwcs/api.py

Lines 141 to 153 in 7f92615

def world_to_array_index_values(self, *world_arrays):
"""
Convert world coordinates to array indices.
This is the same as `~BaseLowLevelWCS.world_to_pixel_values` except that
the indices should be returned in ``(i, j)`` order, where for an image
``i`` is the row and ``j`` is the column (i.e. the opposite order to
`~BaseLowLevelWCS.pixel_to_world_values`). The indices should be
returned as rounded integers.
"""
result = self.world_to_pixel_values(*world_arrays)
if self.pixel_n_dim != 1:
result = result[::-1]
return result
does not round to integer values like its docstring indicates. This behavior is present in astropy.wcs and listed as part of the general API by APE 14.

The clear solution is to apply

gwcs/gwcs/utils.py

Lines 54 to 74 in 7f92615

def _toindex(value):
"""
Convert value to an int or an int array.
Input coordinates converted to integers
corresponding to the center of the pixel.
The convention is that the center of the pixel is
(0, 0), while the lower left corner is (-0.5, -0.5).
The outputs are used to index the mask.
Examples
--------
>>> _toindex(np.array([-0.5, 0.49999]))
array([0, 0])
>>> _toindex(np.array([0.5, 1.49999]))
array([1, 1])
>>> _toindex(np.array([1.5, 2.49999]))
array([2, 2])
"""
indx = np.asarray(np.floor(np.asarray(value) + 0.5), dtype=int)
return indx
before returning the value.

@WilliamJamieson WilliamJamieson linked a pull request Nov 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant