Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ValueError to mapbase.submap() when Helioprojection coordinate contains NaN values. #7543

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/7543.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated :meth:`sunpy.map.Map.submap` to check if it is about to work on locations with NaNs now errors and informs the user that they likely want to use :meth:`~sunpy.coordinates.Helioprojective.assume_spherical_screen` so that the off-disk 2D coordinate can be converted to a 3D coordinate.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as below. Should be updated to use SphericalScreen once that PR is merged.

14 changes: 13 additions & 1 deletion sunpy/map/mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sunpy.coordinates
import sunpy.visualization.colormaps
from sunpy import config, log
from sunpy.coordinates import HeliographicCarrington, get_earth, sun
from sunpy.coordinates import HeliographicCarrington, Helioprojective, get_earth, sun
from sunpy.coordinates.utils import get_rectangle_coordinates
from sunpy.image.resample import resample as sunpy_image_resample
from sunpy.image.resample import reshape_image_to_4d_superpixel
Expand Down Expand Up @@ -1994,6 +1994,18 @@ def _parse_submap_quantity_input(self, bottom_left, top_right, width, height):
@_parse_submap_input.register(SkyCoord)
@_parse_submap_input.register(BaseCoordinateFrame)
def _parse_submap_coord_input(self, bottom_left, top_right, width, height):
msg = (
"The {quadrant} input contains NaN values. "
"It is possible the coordinates are off-disk. "
"Consider using Helioprojective.assume_spherical_screen"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we merge #7115, this should change to SphericalScreen

Comment on lines +1998 to +2000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads more like a warning than an exception. My issue with it is it doesn't really say why an exception is being raised. Why am I not being allowed to crop my map when one of my coordinates is off disk?

)
if isinstance(bottom_left, Helioprojective):
if np.isnan(bottom_left.distance.to_value()).any():
raise ValueError(msg.format(quadrant="bottom_left"))
if top_right is not None and isinstance(top_right, Helioprojective):
if np.isnan(top_right.distance.to_value()).any():
raise ValueError(msg.format(quadrant="top_right"))

# Use helper function to get top_right as a SkyCoord
bottom_left, top_right = get_rectangle_coordinates(bottom_left,
top_right=top_right,
Expand Down
18 changes: 17 additions & 1 deletion sunpy/map/tests/test_mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import sunpy.coordinates
import sunpy.map
import sunpy.sun
from sunpy.coordinates import HeliographicCarrington, HeliographicStonyhurst, sun
from sunpy.coordinates import HeliographicCarrington, HeliographicStonyhurst, Helioprojective, sun
from sunpy.data.test import get_dummy_map_from_header, get_test_filepath
from sunpy.image.transform import _rotation_registry
from sunpy.map.mapbase import GenericMap
Expand Down Expand Up @@ -1815,3 +1815,19 @@ def test_plot_deprecated_positional_args(aia171_test_map):
with pytest.warns(SunpyDeprecationWarning, match=r"Pass annotate=interpolation, axes=True as keyword args."):
with pytest.raises(TypeError, match="non-boolean value"):
aia171_test_map.plot('interpolation', True)


def test_submap_nan_error_bottom_left(aia171_test_map):
h = Helioprojective([319, 2233]*u.arcsec, 0*u.arcsec, observer='earth', obstime='2020-04-08')
h_3d = h.make_3d()
with pytest.raises(ValueError, match="The bottom_left input contains NaN values."):
aia171_test_map.submap(h_3d)

def test_submap_nan_error_top_right(aia171_test_map):
h = Helioprojective(319*u.arcsec, 0*u.arcsec, observer='earth', obstime='2020-04-08')
h2 = Helioprojective(2233*u.arcsec, 0*u.arcsec, observer='earth', obstime='2020-04-08')
h_3d = h.make_3d()
with pytest.warns(SunpyUserWarning, match="The conversion of these 2D helioprojective coordinates to 3D is all NaNs"):
h2_3d = h2.make_3d()
with pytest.raises(ValueError, match="The top_right input contains NaN values."):
aia171_test_map.submap(h_3d, top_right=h2_3d)