Skip to content

WCS.world_to_array_index_values ignores units of inputs #11173

Open

Description

@DanRyanIrish

Description

When equivalent inputs are given to WCS.world_to_array_index_values but in different units, different array indices are returned. Presumably this is because the unit information is being ignored and it is being assumed that the inputs are in the same unit as stored in the WCS.

Expected behavior

Equivalent inputs in different units produce same output.

Actual behavior

Steps to Reproduce

>>> import astropy.units as u
>>> import numpy as np
>>> from astropy.wcs import WCS

>>> wcs = WCS()
>>> wcs.wcs.ctype = 'HPLN-TAN', 'HPLT-TAN'
>>> wcs.wcs.crval = 0.00089530541880571, 0.00038493926472938695
>>> wcs.wcs.crpix = 512.5, 512.5
>>> wcs.wcs.pc = np.array([[ 0.99999706,  0.00242302], [-0.00242302,  0.99999706]])
>>> wcs.wcs.cdelt = 0.0006674422222222221, 0.0006674422222222221
>>> wcs.wcs.cunit = ['deg', 'deg']

>>> # Define inputs
>>> a = [-900., -900.] * u.arcsec
>>> b = [0., -200.] *u.arcsec

>>> # The following two commands should return the same result, but don't.
>>> wcs.world_to_array_index_values(a.to(u.deg), b.to(u.deg))
(array([511, 428]), array([136, 136]))

>>> wcs.world_to_array_index_values(a.to(u.arcsec), b.to(u.arcsec))
(array([-9223372036854775808,                31755]),
 array([-9223372036854775808,                  510]))

System Details

Activity

  1. pllim commented on Dec 17, 2020

    @pllim
    Member

    Are you sure it takes Quantity? I can't tell from the API doc. On https://docs.astropy.org/en/latest/api/astropy.wcs.wcsapi.BaseWCSWrapper.html , it says world_to_array_index_values is the same as world_to_pixel_values, and then world_to_pixel_values says it "takes world_n_dim scalars or arrays as input in units given by world_axis_units," which is a separate attribute. 🤷

  2. DanRyanIrish commented on Dec 17, 2020

    @DanRyanIrish
    ContributorAuthor

    Ah. So you're suggesting this is deliberate behavior, not a bug? Is there a deliberate reason behind that? Without knowing the history and context I would think it should take a quantity and convert to the relevant unit. If there is a reason it shouldn't however, then perhaps an error or warning should be raised if the user enters a quantity. Otherwise it's very confusing for a user who understandably might expect the function to convert the quantity before the operation.

  3. pllim commented on Dec 17, 2020

    @pllim
    Member

    I will have to defer to the authors of APE 14 -- @astrofrog @eteq @Cadair @keflavich

  4. keflavich commented on Dec 17, 2020

    @keflavich
    Contributor

    I suspect there is a bug here, in that this low-level API function being called (world_to_array_index_values) should not accept quantities at all. Low-level WCS should (probably) only accept floats and should explicitly declare that the floats should have deg units. Or they should do the unit conversion, but that to me seems to be in scope for the high-level API.

    @DanRyanIrish you may want to use world_to_array_index, leaving off the _values, to use the high-level API instead.

  5. DanRyanIrish commented on Dec 17, 2020

    @DanRyanIrish
    ContributorAuthor

    Hi @keflavich. Thanks for your thoughts. My specific use case in within ndcube and the _values method is the correct one to use. So it's a matter of deciding where the unit handling should be done, in astropy or ndcube. Whichever direction astropy wants to go (perform unit conversion or explicitly not accept Quantities) is fine so long as there's clarity. That way ndcube can proceed with confidence.

  6. keflavich commented on Dec 17, 2020

    @keflavich
    Contributor

    Because the low-level WCS is the FITS WCS here, I suspect it can only handle values in deg units, so I suggest going with that for ndcube development. However, we still should leave this issue open, as the _values method needs to either raise an error or properly treat quantities.

  7. Cadair commented on Dec 17, 2020

    @Cadair
    Member

    I suspect it can only handle values in deg units

    Surely this is only true for astropy.wcs.WCS and not a general APE 14 low level WCS.

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