Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 10 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
Commits on Aug 15, 2012
@astrofrog astrofrog Fixed astropy.wcs import
The WCS package in astropy is at astropy.wcs not astropy.pywcs. Since astropy.wcs is guaranteed to be a very recent version of pywcs, I thought it would make more sense to try that first. For example, if someone has an old installation of pywcs and astropy, astropy should be preferred (to have the 'sub' attribute).
893ed0a
@astrofrog astrofrog Always try using Astropy before PyFITS/PyWCS. If astropy.wcs is used,…
… always use astropy.io.fits to ensure compatibility
249d79a
@astrofrog astrofrog Improve astropy.wcs import 72887e2
@astrofrog astrofrog Fixed PyFITS imports in examples 5934fad
@astrofrog astrofrog Revert examples to use PyFITS 9040074
@astrofrog astrofrog Make wcs_helper.py code insensitive to whether we are using PyWCS/PyF…
…ITS, Astropy, or a mix of the two.
daf971b
@astrofrog astrofrog More changes to make code insensitive to PyFITS vs Astropy 66316ce
@astrofrog astrofrog Whitespace 629d072
@astrofrog astrofrog Whitespace c432690
Commits on Aug 29, 2012
@leejjoon Merge pull request #12 from astrofrog/patch-2
Fixed astropy.wcs import
9602506
View
9 lib/__init__.py
@@ -116,8 +116,13 @@ def filterwcs(h):
"""
select wcs related cards
"""
- import pyfits
- l = [card for card in h.ascardlist() if _wcs_key_pattern.match(card.key)]
+ try:
+ from astropy.io import fits as pyfits
+ except ImportError:
+ import pyfits
+ # We have to re-instantiate the cards since we don't know if the original
+ # header was from PyFITS or Astropy.
+ l = [pyfits.Card(card.key, card.value, card.comment) for card in h.ascardlist() if _wcs_key_pattern.match(card.key)]
return pyfits.Header(l)
View
6 lib/allsky_axes.py
@@ -1,5 +1,9 @@
import matplotlib.pyplot as plt
-import pyfits
+
+try:
+ from astropy.io import fits as pyfits
+except ImportError:
+ import pyfits
from mpl_toolkits.axisartist.floating_axes import floatingaxes_class_factory
View
7 lib/healpix_helper.py
@@ -1,10 +1,11 @@
-import pyfits
import numpy as np
try:
- import pywcs
-except ImportError:
+ from astropy.io import fits as pyfits
import astropy.pywcs as pywcs
+except ImportError:
+ import pyfits
+ import pywcs
import healpy
import warnings
View
99 lib/wcs_helper.py
@@ -3,19 +3,20 @@
from kapteyn_celestial import skymatrix, longlat2xyz, dotrans, xyz2longlat
import kapteyn_celestial
-import pyfits
-
_wcs_module_import_log = []
_pywcs_installed = True
try:
- import pywcs
+ from astropy import wcs as pywcs
except ImportError:
try:
- import astropy.pywcs as pywcs
+ import pywcs
except ImportError:
_pywcs_installed = False
_wcs_module_import_log.append("Failed to import the pywcs")
+ import pyfits
+else:
+ from astropy.io import fits as pyfits
if _pywcs_installed:
if not hasattr(pywcs.WCS, "sub"):
@@ -26,7 +27,7 @@
try:
import kapteyn.wcs
except ImportError:
- _wcs_module_import_log.append("Failed to import the kpateyn.wcs")
+ _wcs_module_import_log.append("Failed to import the kapteyn.wcs")
else:
_kapteyn_installed = True
@@ -183,7 +184,9 @@ def fix_header(header):
if c.key.startswith("CUNIT") and c.value.lower().startswith("deg"):
c = pyfits.Card(c.key, "deg")
- cards.append(c)
+ # We re-instantiate the card so that it doesn't matter if the
+ # original Card object is from PyFITS or Astropy
+ cards.append(pyfits.Card(c.key, c.value, c.comment))
h = pyfits.Header(cards)
return h
@@ -259,12 +262,18 @@ class ProjectionKapteyn(ProjectionBase):
A wrapper for kapteyn.projection
"""
def __init__(self, header):
- ProjectionBase.__init__(self)
- if isinstance(header, pyfits.Header):
+
+ # Kapteyn doesn't care whether the header is from PyFITS or Astropy,
+ # but we can't check using isinstance, so instead we check if it has
+ # the 'ascard' attribute that both header classes define.
+
+ if hasattr(header, 'ascard'):
self._proj = kapteyn.wcs.Projection(header)
else:
self._proj = header
+ ProjectionBase.__init__(self)
+
def _get_ctypes(self):
return self._proj.ctype
@@ -310,15 +319,27 @@ class ProjectionPywcsNd(_ProjectionSubInterface, ProjectionBase):
A wrapper for pywcs
"""
def __init__(self, header):
- if isinstance(header, pyfits.Header):
+
+ # We can't check the header type using isinstance since we don't know
+ # if it comes from PyFITS or Astropy, so instead we check if it has
+ # the 'ascard' attribute that both header classes define.
+
+ if hasattr(header, 'ascard'):
+
header = fix_header(header)
- # pywcs.WCS internally uses `repr(header.ascard)` which
- # returns str, but expecte byte in py3.
- import sys
- if sys.version_info >= (3,0):
- header = repr(header.ascard).encode("ascii")
+
+ # Since we don't know if PyFITS or PyWCS are from Astropy, and the
+ # WCS object in PyWCS and Astropy both accept a string
+ # representation of the header, we use this instead (both
+ # internally use `repr(header.ascard)` which returns str,
+ # and is compatible with Python 3
+
+ header = repr(header.ascard).encode('latin1')
+
self._pywcs = pywcs.WCS(header=header)
+
else:
+
self._pywcs = header
ProjectionBase.__init__(self)
@@ -479,12 +500,31 @@ class ProjectionPywcs(ProjectionBase):
A wrapper for pywcs
"""
def __init__(self, header):
- ProjectionBase.__init__(self)
- if isinstance(header, pyfits.Header):
+
+ # We can't check the header type using isinstance since we don't know
+ # if it comes from PyFITS or Astropy, so instead we check if it has
+ # the 'ascard' attribute that both header classes define.
+
+ if hasattr(header, 'ascard'):
+
+ header = fix_header(header)
+
+ # Since we don't know if PyFITS or PyWCS are from Astropy, and the
+ # WCS object in PyWCS and Astropy both accept a string
+ # representation of the header, we use this instead (both
+ # internally use `repr(header.ascard)` which returns str,
+ # and is compatible with Python 3
+
+ header = repr(header.ascard).encode('latin1')
+
self._pywcs = pywcs.WCS(header=header)
+
else:
+
self._pywcs = header
+ ProjectionBase.__init__(self)
+
def _get_ctypes(self):
return tuple(self._pywcs.wcs.ctype)
@@ -517,10 +557,23 @@ class ProjectionSimple(ProjectionBase):
A wrapper for pywcs
"""
def __init__(self, header):
+
ProjectionBase.__init__(self)
- self._pywcs = pywcs.WCS(header=header)
+
+ # This projection assumes that `header` is a Header object, and
+ # doesn't care whether it comes from PyFITS or Astropy
+
self._simple_init(header)
+ # Since we don't know if PyFITS or PyWCS are from Astropy, and the
+ # WCS object in PyWCS and Astropy both accept a string
+ # representation of the header, we use this instead (both
+ # internally use `repr(header.ascard)` which returns str
+
+ header = repr(header.ascard).encode('latin1')
+
+ self._pywcs = pywcs.WCS(header=header)
+
def _get_ctypes(self):
return tuple(self._pywcs.wcs.ctype)
@@ -601,9 +654,15 @@ def sub(self, axis_nums):
ProjectionDefault = ProjectionKapteyn
def get_kapteyn_projection(header):
+
+ # For checking against PyWCS, we can't use isinstance(..., pywcs.WCS)
+ # because it could come from PyWCS or Astropy. But what really matters is
+ # that the WCS object has a wcs_sky2pix method, so we check for that
+ # instead.
+
if _kapteyn_installed and isinstance(header, kapteyn.wcs.Projection):
projection = ProjectionKapteynNd(header)
- elif _pywcs_installed and isinstance(header, pywcs.WCS):
+ elif _pywcs_installed and hasattr(header, 'wcs_sky2pix'):
projection = ProjectionPywcsNd(header)
elif isinstance(header, ProjectionBase):
projection = header
@@ -667,7 +726,3 @@ def estimate_angle(transSky2Pix, x0, y0):
#print fk5_to_fk4([47.37], [6.32])
#print fk5_to_fk4([47.37, 47.37], [6.32, 6.32])
#print sky2sky("fk5", "FK4")([47.37, 47.37], [6.32, 6.32])
-
-
-
-

No commit comments for this range

Something went wrong with that request. Please try again.