Skip to content

Commit

Permalink
Ignore the bounding box ordering warning when reading files (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Nadia Dencheva <nadia.dencheva@gmail.com>
  • Loading branch information
WilliamJamieson and nden authored Dec 2, 2024
1 parent 86dc56c commit 9f23a66
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

- Force ``bounding_box`` to always be returned as a ``F`` ordered box. [#522]

- Add warning filter to asdf extension to prevent the ``bounding_box`` order warning for gwcs
objects originating from a file. [#526]

0.21.0 (2024-03-10)
-------------------

Expand Down
11 changes: 10 additions & 1 deletion gwcs/converters/wcs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

import warnings
from contextlib import suppress
from asdf.extension import Converter


Expand All @@ -14,10 +16,17 @@ class WCSConverter(Converter):
types = ["gwcs.wcs.WCS"]

def from_yaml_tree(self, node, tag, ctx):
from ..wcs import WCS
from ..wcs import WCS, GwcsBoundingBoxWarning
gwcsobj = WCS(node['steps'], name=node['name'])
if 'pixel_shape' in node:
gwcsobj.pixel_shape = node['pixel_shape']

# Ignore the warning about the bounding box order for data read from a
# file. This is causing issues with files from MAST.
with suppress(AttributeError), warnings.catch_warnings():
warnings.filterwarnings('ignore', category=GwcsBoundingBoxWarning)
_ = gwcsobj.bounding_box

return gwcsobj

def to_yaml_tree(self, gwcsobj, tag, ctx):
Expand Down
11 changes: 11 additions & 0 deletions gwcs/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def gwcs_2d_shift_scale():
pipe = [(DETECTOR_2D_FRAME, m3), (ICRC_SKY_FRAME, None)]
return wcs.WCS(pipe)

def gwcs_2d_bad_bounding_box_order():
m1 = models.Shift(1) & models.Shift(2)
m2 = models.Scale(5) & models.Scale(10)
m3 = m1 | m2

# Purposefully set the bounding box in the wrong order
m3.bounding_box = ((1, 2), (3, 4))

pipe = [(DETECTOR_2D_FRAME, m3), (ICRC_SKY_FRAME, None)]
return wcs.WCS(pipe)


def gwcs_1d_freq_quantity():

Expand Down
27 changes: 27 additions & 0 deletions gwcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ..utils import CoordinateFrameError
from .utils import _gwcs_from_hst_fits_wcs
from . import data
from ..examples import gwcs_2d_bad_bounding_box_order


data_path = os.path.split(os.path.abspath(data.__file__))[0]
Expand Down Expand Up @@ -1443,3 +1444,29 @@ def test_bounding_box_is_returned_F():

# Show the the bounding box is different between the two WCS objects
assert gwcs_object_after.bounding_box != gwcs_object_before.bounding_box


def test_no_bounding_box_if_read_from_file(tmp_path):
bad_wcs = gwcs_2d_bad_bounding_box_order()

# Check the waring is issued for the bounding box of this WCS object
with pytest.warns(wcs.GwcsBoundingBoxWarning):
bad_wcs.bounding_box

# Check that the warning is not issued again the second time
with warnings.catch_warnings():
warnings.simplefilter("error")
bad_wcs.bounding_box

# Write a bad wcs bounding box to an asdf file
asdf_file = tmp_path / "bad_wcs.asdf"
af = asdf.AsdfFile({"wcs": gwcs_2d_bad_bounding_box_order()}) # re-create the bad wcs object
af.write_to(asdf_file)

with asdf.open(asdf_file) as af:
wcs_from_file = af["wcs"]

# Check that no warning is issued for the bounding box of this WCS object
with warnings.catch_warnings():
warnings.simplefilter("error")
wcs_from_file.bounding_box

0 comments on commit 9f23a66

Please sign in to comment.