-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from lpsinger/from_header
Add HEALPix.from_header
- Loading branch information
Showing
4 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import numpy as np | ||
|
||
import pytest | ||
from astropy.coordinates import FK5, Galactic | ||
from astropy.io import fits | ||
|
||
from ..utils import parse_coord_system, parse_input_healpix_data | ||
|
||
|
||
def test_parse_coord_system(): | ||
|
||
frame = parse_coord_system(Galactic()) | ||
assert isinstance(frame, Galactic) | ||
|
||
frame = parse_coord_system('fk5') | ||
assert isinstance(frame, FK5) | ||
|
||
with pytest.raises(ValueError) as exc: | ||
frame = parse_coord_system('e') | ||
assert exc.value.args[0] == "Ecliptic coordinate frame not yet supported" | ||
|
||
frame = parse_coord_system('g') | ||
assert isinstance(frame, Galactic) | ||
|
||
with pytest.raises(ValueError) as exc: | ||
frame = parse_coord_system('spam') | ||
assert exc.value.args[0] == "Could not determine frame for system=spam" | ||
|
||
|
||
def test_parse_input_healpix_data(tmpdir): | ||
|
||
data = np.arange(3072) | ||
|
||
col = fits.Column(array=data, name='flux', format="E") | ||
hdu = fits.BinTableHDU.from_columns([col]) | ||
hdu.header['NSIDE'] = 512 | ||
hdu.header['COORDSYS'] = "G" | ||
|
||
# As HDU | ||
array, coordinate_system, nested = parse_input_healpix_data(hdu) | ||
np.testing.assert_allclose(array, data) | ||
|
||
# As filename | ||
filename = tmpdir.join('test.fits').strpath | ||
hdu.writeto(filename) | ||
array, coordinate_system, nested = parse_input_healpix_data(filename) | ||
np.testing.assert_allclose(array, data) | ||
|
||
# As array | ||
array, coordinate_system, nested = parse_input_healpix_data((data, "galactic")) | ||
np.testing.assert_allclose(array, data) | ||
|
||
# Invalid | ||
with pytest.raises(TypeError) as exc: | ||
parse_input_healpix_data(data) | ||
assert exc.value.args[0] == "input_data should either be an HDU object or a tuple of (array, frame)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import numpy as np | ||
|
||
from astropy.io import fits | ||
from astropy.io.fits import TableHDU, BinTableHDU | ||
from astropy.extern import six | ||
from astropy.coordinates import BaseCoordinateFrame, frame_transform_graph, Galactic, ICRS | ||
|
||
FRAMES = { | ||
'g': Galactic(), | ||
'c': ICRS() | ||
} | ||
|
||
|
||
def parse_coord_system(system): | ||
if isinstance(system, BaseCoordinateFrame): | ||
return system | ||
elif isinstance(system, six.string_types): | ||
system = system.lower() | ||
if system == 'e': | ||
raise ValueError("Ecliptic coordinate frame not yet supported") | ||
elif system in FRAMES: | ||
return FRAMES[system] | ||
else: | ||
system_new = frame_transform_graph.lookup_name(system) | ||
if system_new is None: | ||
raise ValueError("Could not determine frame for system={0}".format(system)) | ||
else: | ||
return system_new() | ||
|
||
|
||
def parse_input_healpix_data(input_data, field=0, hdu_in=None, nested=None): | ||
""" | ||
Parse input HEALPIX data to return a Numpy array and coordinate frame object. | ||
""" | ||
|
||
if isinstance(input_data, (TableHDU, BinTableHDU)): | ||
data = input_data.data | ||
header = input_data.header | ||
coordinate_system_in = parse_coord_system(header['COORDSYS']) | ||
array_in = data[data.columns[field].name].ravel() | ||
if 'ORDERING' in header: | ||
nested = header['ORDERING'].lower() == 'nested' | ||
elif isinstance(input_data, six.string_types): | ||
hdu = fits.open(input_data)[hdu_in or 1] | ||
return parse_input_healpix_data(hdu, field=field) | ||
elif isinstance(input_data, tuple) and isinstance(input_data[0], np.ndarray): | ||
array_in = input_data[0] | ||
coordinate_system_in = parse_coord_system(input_data[1]) | ||
else: | ||
raise TypeError("input_data should either be an HDU object or a tuple of (array, frame)") | ||
|
||
return array_in, coordinate_system_in, nested |