Repository: sunpy/sunpy

Issue:
convert_data_to_pixel issue
This line in convert_data_to_pixel:
  pixelx = (x - crval[0]) / cdelt[0] + (crpix[1] - 1)
should be:
  pixelx = (x - crval[0]) / cdelt[0] + (crpix[0] - 1)

I found the problem using 0.6.3, but looking at the source, it persists in 0.6.4.

convert_data_to_pixel issue
This line in convert_data_to_pixel:
  pixelx = (x - crval[0]) / cdelt[0] + (crpix[1] - 1)
should be:
  pixelx = (x - crval[0]) / cdelt[0] + (crpix[0] - 1)

I found the problem using 0.6.3, but looking at the source, it persists in 0.6.4.



Discussion:
Can you provide a link to the source code please? If you click on the line number on github it will give you a link.

Thanks for the bug report.

https://github.com/sunpy/sunpy/blob/2052149cdd85e600ac4ec2724eab852e30bb255e/sunpy/wcs/wcs.py#L136

On Tue, May 10, 2016 at 6:12 AM, Stuart Mumford notifications@github.com
wrote:

> Can you provide a link to the source code please? If you click on the line
> number on github it will give you a link.
> 
> Thanks for the bug report.
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly or view it on GitHub
> https://github.com/sunpy/sunpy/issues/1763#issuecomment-218115502

yes, that is a bug. What's noteworthy is that we don't use that code in map anymore.

@thothsartre What are you using that implementation of `data_to_pixel` doing that conversion through `Map.data_to_pixel()` is better as it accounts for rotation correctly.

Thanks for the pointer to Map.data_to_pixel(); looks like it'll take a bit more code to set up, but should work well.  I'm extracting portions of data read from FITS files given data coordinates, and convert_data_to_pixel was used to get the corresponding image coordinates.

@thothsartre If you want to extract a sub section of an image from a larger image you can use [`Map.submap()`](http://docs.sunpy.org/en/stable/code_ref/map.html#sunpy.map.mapbase.GenericMap.submap) where you can specify the arguments in either data or image coordinates. HTH.

@Cadair  Many thanks!  That's very helpful.

Can you provide a link to the source code please? If you click on the line number on github it will give you a link.

Thanks for the bug report.

https://github.com/sunpy/sunpy/blob/2052149cdd85e600ac4ec2724eab852e30bb255e/sunpy/wcs/wcs.py#L136

On Tue, May 10, 2016 at 6:12 AM, Stuart Mumford notifications@github.com
wrote:

> Can you provide a link to the source code please? If you click on the line
> number on github it will give you a link.
> 
> Thanks for the bug report.
> 
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly or view it on GitHub
> https://github.com/sunpy/sunpy/issues/1763#issuecomment-218115502

yes, that is a bug. What's noteworthy is that we don't use that code in map anymore.

@thothsartre What are you using that implementation of `data_to_pixel` doing that conversion through `Map.data_to_pixel()` is better as it accounts for rotation correctly.

Thanks for the pointer to Map.data_to_pixel(); looks like it'll take a bit more code to set up, but should work well.  I'm extracting portions of data read from FITS files given data coordinates, and convert_data_to_pixel was used to get the corresponding image coordinates.

@thothsartre If you want to extract a sub section of an image from a larger image you can use [`Map.submap()`](http://docs.sunpy.org/en/stable/code_ref/map.html#sunpy.map.mapbase.GenericMap.submap) where you can specify the arguments in either data or image coordinates. HTH.

@Cadair  Many thanks!  That's very helpful.


Resolving patch:
diff --git a/sunpy/wcs/wcs.py b/sunpy/wcs/wcs.py
--- a/sunpy/wcs/wcs.py
+++ b/sunpy/wcs/wcs.py
@@ -133,7 +133,7 @@ def convert_data_to_pixel(x, y, scale, reference_pixel, reference_coordinate):
     # coord = inv_proj_tan(coord)
 
     # note that crpix[] counts pixels starting at 1
-    pixelx = (x - crval[0]) / cdelt[0] + (crpix[1] - 1)
+    pixelx = (x - crval[0]) / cdelt[0] + (crpix[0] - 1)
     pixely = (y - crval[1]) / cdelt[1] + (crpix[1] - 1)
 
     return pixelx, pixely


Test patch: