Cutout2D behaviour for non-rectangular pixel sizes
See original GitHub issueDescription
When specifying a non-rectangular pixel size for the WCS provided to the Cutout2D
class, the cutout does not return the expected cutout size and data shape.
Steps to Reproduce
from astropy.nddata.utils import Cutout2D
from astropy.wcs.utils import celestial_frame_to_wcs, proj_plane_pixel_scales
from astropy.coordinates import SkyCoord
import numpy as np
center = SkyCoord("0d", "0d", frame="icrs")
wcs = celestial_frame_to_wcs(center.frame, projection="CAR")
wcs.wcs.crval = (0, 0)
wcs.wcs.cdelt = (-5, 5)
wcs.wcs.crpix = (36.5, 18.5)
data = np.ones((36, 72))
print(data.shape)
size = [90, 180] * u.deg
c2d = Cutout2D(data=data, position=center, size=size, wcs=wcs)
print(c2d.data.shape)
Prints:
(36, 72)
(18, 36)
So it behaves as expected and returns a data array with half the input shape in all axes.
In case of specifying an un-even pixel size:
from astropy.nddata.utils import Cutout2D
from astropy.wcs.utils import celestial_frame_to_wcs, proj_plane_pixel_scales
from astropy.coordinates import SkyCoord
import numpy as np
center = SkyCoord("0d", "0d", frame="icrs")
wcs = celestial_frame_to_wcs(center.frame, projection="CAR")
wcs.wcs.crval = (0, 0)
wcs.wcs.cdelt = (-10, 5)
wcs.wcs.crpix = (18.5, 18.5)
data = np.ones((36, 36))
print(data.shape)
size = [90, 180] * u.deg
c2d = Cutout2D(data=data, position=center, size=size, wcs=wcs)
print(c2d.data.shape)
It prints:
(36, 36)
(9, 36)
While I would have expected (18, 18). Maybe it seems the lon and lat axes are switched in this case? cc @LauraOlivera
System Details
macOS-10.14.6-x86_64-i386-64bit
Python 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 23:22:12)
[Clang 11.0.1 ]
Numpy 1.20.0
astropy 4.1
Scipy 1.6.0
Matplotlib 3.3.4
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
Cutout2D — Astropy v5.2
If size is in angular units, the cutout size is converted to pixels using the pixel scales along each axis of the image...
Read more >python-astropy-4.0.2-bp153.1.10 - SUSE Package Hub
Changed the behavior when slicing a table (either in rows or with a list of column names) so now the sliced output gets...
Read more >astropy/CHANGES.rst at main - GitHub
The pixel argument to astropy.visualization.wcsaxes.ticklabels. ... Fixed Cutout2D output WCS NAXIS values to reflect the cutout image size. [#7552] ...
Read more >Cutout2D not centering on galaxies - Stack Overflow
Cutout2D not centering on galaxies ... y_pix) size = (200, 200) #size I want the image to be in pixels cut_test = Cutout2D(data,...
Read more >Cutout2D not updated wcs properly (?) - Google Groups
So the pixels in both images seem to have the same coordinates, but the images don't align when ... cutout = Cutout2D(scidata, position,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
From a quick look at the code, I think I already found the bug:
size
argument of theCutout2D
class is given in (lat, lon) order, as documented in the docstringsize
to pixel in the loop hereproj_plane_pixel_scales(wcs)
returns the pixel size in (lon, lat) orderIf someone confirms the bug, I could possibly implement a fix myself.
“axis of the image” refers to the axis of data, e.g. (x, y) not WCS axis. In general (lat, lon) (in whichever order) aren’t going to be aligned along the (x, y) axes, so I don’t know another way to do this other than to use
proj_plane_pixel_scales
(the data cutouts are always rectangles in (x, y) space). We should update the documentation if that is confusing. Perhaps even explicitly by stating the cutout pixel size will be:where the x and y pixel scales are computed using
proj_plane_pixel_scales
Would that help?