Repository: sunpy/ndcube

Issue:
Patch wcs setting in NDCube
<!--
We know asking good questions takes effort, and we appreciate your time.
Thank you.

Please be aware that everyone has to follow our code of conduct:
https://sunpy.org/coc

These comments are hidden when you submit this github issue.

Please have a search on our GitHub repository to see if a similar issue has already been posted.
If a similar issue is closed, have a quick look to see if you are satisfied by the resolution.
If not please go ahead and open an issue!
-->

### Description

<!--
Provide a general description of the bug.
-->

Because of a bug in astropy we need to explicitly set the WCS in `NDCube` by adding this line after calling the [`super().__init__`](https://github.com/sunpy/ndcube/blob/main/ndcube/ndcube.py#L242)

`self.wcs = wcs`

Discussion:
pep8speaks: Hello @DanRyanIrish! Thanks for opening this PR. 

* In the file [`ndcube/ndcube.py`](https://github.com/sunpy/ndcube/blob/7e43a8ba6ff7184bf1713057af750531387da528/ndcube/ndcube.py):

> [Line 658:38](https://github.com/sunpy/ndcube/blob/7e43a8ba6ff7184bf1713057af750531387da528/ndcube/ndcube.py#L658): [E741](https://duckduckgo.com/?q=pep8%20E741) ambiguous variable name 'l'

Do see the [Hitchhiker's guide to code style](https://goo.gl/hqbW4r)

Patch:
diff --git a/changelog/447.bugfix.rst b/changelog/447.bugfix.rst
new file mode 100644
index 0000000..7d3b54a
--- /dev/null
+++ b/changelog/447.bugfix.rst
@@ -0,0 +1,3 @@
+Ensure `NDCube` init forces WCS to become high level.
+
+This patches a bug in astropy.
diff --git a/ndcube/ndcube.py b/ndcube/ndcube.py
index cf19433..3dc1f97 100644
--- a/ndcube/ndcube.py
+++ b/ndcube/ndcube.py
@@ -239,8 +239,12 @@ class NDCubeBase(NDCubeSlicingMixin, NDCubeABC):
     def __init__(self, data, wcs=None, uncertainty=None, mask=None, meta=None,
                  unit=None, copy=False, **kwargs):
 
-        super().__init__(data, wcs=wcs, uncertainty=uncertainty, mask=mask,
+        super().__init__(data, uncertainty=uncertainty, mask=mask,
                          meta=meta, unit=unit, copy=copy, **kwargs)
+        if not self.wcs:
+            self.wcs = wcs  # This line is required as a patch for an astropy bug.
+        # Above line is in if statement to prevent WCS being overwritten with None
+        # if we are instantiating from an NDCube.
 
         # Enforce that the WCS object is not None
         if self.wcs is None:


Test patch:
diff --git a/ndcube/tests/test_ndcube.py b/ndcube/tests/test_ndcube.py
index b7dd8c7..708fbc8 100644
--- a/ndcube/tests/test_ndcube.py
+++ b/ndcube/tests/test_ndcube.py
@@ -620,3 +620,13 @@ def test_reproject_shape_out(ndcube_4d_ln_l_t_lt, wcs_4d_lt_t_l_ln):
     # should not raise an exception when target_wcs has pixel_shape or array_shape attribute
     wcs_4d_lt_t_l_ln.array_shape = shape
     _ = ndcube_4d_ln_l_t_lt.reproject_to(wcs_4d_lt_t_l_ln, shape_out=shape)
+
+
+def test_wcs_type_after_init(ndcube_3d_ln_lt_l, wcs_3d_l_lt_ln):
+    # Generate a low level WCS
+    slices = np.s_[:, :, 0]
+    low_level_wcs = SlicedLowLevelWCS(wcs_3d_l_lt_ln, slices)
+    # Generate an NDCube using the low level WCS
+    cube = NDCube(ndcube_3d_ln_lt_l.data[slices], low_level_wcs)
+    # Check the WCS has been converted to high level but NDCube init.
+    assert isinstance(cube.wcs, BaseHighLevelWCS)