Repository: sunpy/sunpy

Issue:
make_fitswcs_header has an off by one error in the default crpix calculation
This needs double checking but it looks like we  are +1 off in the default array shape halving


Discussion:

Resolving patch:
diff --git a/sunpy/map/header_helper.py b/sunpy/map/header_helper.py
--- a/sunpy/map/header_helper.py
+++ b/sunpy/map/header_helper.py
@@ -134,8 +134,8 @@ def make_fitswcs_header(data, coordinate,
     meta_wcs['crval1'], meta_wcs['crval2'] = (coordinate.spherical.lat.to_value(meta_wcs['cunit1']),
                                               coordinate.spherical.lon.to_value(meta_wcs['cunit2']))
 
-    meta_wcs['crpix1'], meta_wcs['crpix2'] = (reference_pixel[0].to_value(u.pixel) + 1,
-                                              reference_pixel[1].to_value(u.pixel) + 1)
+    meta_wcs['crpix1'], meta_wcs['crpix2'] = (reference_pixel[0].to_value(u.pixel),
+                                              reference_pixel[1].to_value(u.pixel))
 
     meta_wcs['cdelt1'], meta_wcs['cdelt2'] = (scale[0].to_value(meta_wcs['cunit1']/u.pixel),
                                               scale[1].to_value(meta_wcs['cunit2']/u.pixel))


Test patch:
diff --git a/sunpy/map/tests/test_header_helper.py b/sunpy/map/tests/test_header_helper.py
--- a/sunpy/map/tests/test_header_helper.py
+++ b/sunpy/map/tests/test_header_helper.py
@@ -87,7 +87,7 @@ def test_make_fits_header(map_data, hpc_test_header, hgc_test_header,
     # Check that correct information is in header MetaDict including observer for HPC
     header = sunpy.map.make_fitswcs_header(map_data, hpc_test_header)
     assert header['crval1'] == 0
-    assert header['crpix1'] == 6.5
+    assert header['crpix1'] == 5.5
     assert header['ctype1'] == 'HPLN-TAN'
     assert header['dsun_obs'] == hpc_test_header.frame.observer.radius.to_value(u.m)
     assert isinstance(WCS(header), WCS)
@@ -125,8 +125,8 @@ def test_HGS_CAR_header():
     new_data = np.empty((72, 144))
     new_frame = SkyCoord(0*u.deg, 0*u.deg, obstime="2019-06-16", frame="heliographic_stonyhurst")
     new_header = sunpy.map.make_fitswcs_header(new_data, new_frame,
-                                            scale=[2.5, 2.5]*u.deg/u.pix,
-                                            projection_code="CAR")
+                                               scale=[2.5, 2.5]*u.deg/u.pix,
+                                               projection_code="CAR")
 
     assert new_header['ctype1'] == "HGLN-CAR"
     assert new_header['ctype2'] == "HGLT-CAR"
@@ -136,3 +136,15 @@ def test_HGS_CAR_header():
     assert "hgln_obs" not in new_header
     assert "hglt_obs" not in new_header
     assert "dsun_obs" not in new_header
+
+    wcs = WCS(new_header)
+    pix_coords = np.mgrid[0:new_data.shape[1], 0:new_data.shape[0]]
+    world_coords = wcs.pixel_to_world(*pix_coords)
+
+    # In this projection the coordinate system is symmetric around the
+    # reference pixel. because the reference pixel should be in the center of
+    # the array it means the array should be symmetric apart from the sign
+    # flip. This is therefore testing that the reference pixel is correctly in
+    # the center of the array.
+    assert u.allclose(world_coords.lon, world_coords.lon[::-1, ::-1]*-1)
+    assert u.allclose(world_coords.lat, world_coords.lat[::-1, ::-1]*-1)