Skip to content

Commit

Permalink
Merge pull request #143 from keflavich/header_nowcs
Browse files Browse the repository at this point in the history
Strip the WCS out of the copied header
  • Loading branch information
keflavich committed Sep 10, 2014
2 parents 8e1b8fe + d7f1e1c commit 75bb3e7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
15 changes: 8 additions & 7 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from astropy import units as u
from astropy.extern import six
from astropy.io.fits import PrimaryHDU, ImageHDU, Header
from astropy.io.fits import PrimaryHDU, ImageHDU, Header, Card
from astropy import log
from astropy import wcs

Expand Down Expand Up @@ -1514,7 +1514,8 @@ def to_yt(self, spectral_factor=1.0, nprocs=None, **kwargs):

import yt

if StrictVersion(yt.__version__) >= StrictVersion('3.0'):
if ('dev' in yt.__version__ or
StrictVersion(yt.__version__) >= StrictVersion('3.0')):

from yt.frontends.fits.api import FITSDataset
from astropy.io import fits
Expand Down Expand Up @@ -1552,13 +1553,13 @@ def to_yt(self, spectral_factor=1.0, nprocs=None, **kwargs):
@property
def header(self):
# Preserve non-WCS information from previous header iteration
header = self._header.copy()
header = wcs_utils.strip_wcs_from_header(self._header)
header.update(self.wcs.to_header())
header['BUNIT'] = self.unit.to_string(format='fits')
header['NAXIS'] = self._data.ndim
header['NAXIS1'] = self.shape[2]
header['NAXIS2'] = self.shape[1]
header['NAXIS3'] = self.shape[0]
header.insert(2, Card(keyword='NAXIS', value=self._data.ndim))
header.insert(3, Card(keyword='NAXIS1', value=self.shape[2]))
header.insert(4, Card(keyword='NAXIS2', value=self.shape[1]))
header.insert(5, Card(keyword='NAXIS3', value=self.shape[0]))
# TODO: incorporate other relevant metadata here
return header

Expand Down
1 change: 1 addition & 0 deletions spectral_cube/tests/test_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ def test_header_naxis():
assert cube.header['NAXIS1'] == data.shape[3]
assert cube.header['NAXIS2'] == data.shape[2]
assert cube.header['NAXIS3'] == data.shape[1]
assert 'NAXIS4' not in cube.header

def test_slicing():

Expand Down
28 changes: 28 additions & 0 deletions spectral_cube/wcs_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,31 @@ def check_equality(wcs1, wcs2, warn_missing=False, ignore_keywords=['MJD-OBS',
OK = False

return OK

def strip_wcs_from_header(header):
"""
Given a header with WCS information, remove ALL WCS information from that
header
"""

hwcs = WCS(header)
wcsh = hwcs.to_header()

keys_to_keep = [k for k in header
if (k and k not in wcsh and 'NAXIS' not in k)]

newheader = header.copy()
for kw in newheader.keys():
if kw not in keys_to_keep:
del newheader[kw]

for kw in ('CRPIX{ii}', 'CRVAL{ii}', 'CDELT{ii}', 'CUNIT{ii}',
'CTYPE{ii}', 'PC0{ii}_0{jj}', 'CD{ii}_{jj}',):
for ii in range(5):
for jj in range(5):
k = kw.format(ii=ii,jj=jj)
if k in newheader.keys():
del newheader[k]


return newheader

0 comments on commit 75bb3e7

Please sign in to comment.