Skip to content

WCS handles negative slice index incorrectly #8771

Open

Description

@mwcraig

WCS objects can be sliced, with some restrictions (e.g. no negative steps). Currently, wcs.slice returns nonsensical results if the start of the slice is negative.

I don't know that WCS should be able to handle negative start since those require knowing the number of dimensions along each axis, and discussion of that topic has been vigorous in the past (see #4669 and #5461). My particular use case is with CCDData (well, really, NDSlicingMixin) where there will always image dimensions and a negative index can be converted to positive.

The example below demonstrates the issue.

from astropy.wcs import WCS

wcs = WCS(naxis=2)
wcs.wcs.crpix = [5, 5]
wcs.wcs.crval = [0.0, 1.0]
wcs.wcs.cdelt = [1.0, 1.0]

wcs_good_slice = wcs[4:, 4:]
# The slice below would, for an array, give the same thing
# as the slice above.
wcs_bad_slice = wcs[-6:, -6:]

print('crval: ', wcs.wcs.crval)
print('coordinate value at pixel [0, 0] (should be crval): ', wcs_good_slice.all_pix2world([[0, 0]], 0))
print('coordinate value at pixel [0, 0] (should be crval): ', wcs_bad_slice.all_pix2world([[0, 0]], 0))

Activity

  1. mwcraig commented on May 28, 2019

    @mwcraig
    MemberAuthor

    I'm happy to implement any of these solutions:

    1. Modify the NDData slicing mechanism to turn negative indices positive before passing along to other slicers.
    2. Modify wcs.slice to raise an exception if it gets a negative index.
    3. Modify wcs.slice to use NAXISj to convert a negative slice to a positive one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions