Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDU Header for components #1723

Merged
merged 10 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion glue/core/data_exporters/gridded_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
__all__ = []


def make_component_header(component, header):
"""
Function that extracts information from components
and adds it to the data header. The input header is
expected to come from Data.coords.header by default.
:param component: glue Component
:param header: astropy.io.fits.header.Header
:return:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm currently trying to switch over to the Numpydoc format (the same one used by astropy), so could you update this docstring accordingly?

"""

# Add units information
header["BUNIT"] = component.units

return header


@data_exporter(label='FITS (1 component/HDU)', extension=['fits', 'fit'])
def fits_writer(filename, data, components=None):
"""
Expand All @@ -28,6 +44,8 @@ def fits_writer(filename, data, components=None):
else:
mask = None

data_header = data.coords.header if hasattr(data.coords, "header") else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more robust way here would be to check if data.coords is an instance of glue.core.coordinates. WCSCoordinates (rather than just checking whether header exists).


from astropy.io import fits

hdus = fits.HDUList()
Expand All @@ -50,7 +68,8 @@ def fits_writer(filename, data, components=None):
values[~mask] = np.nan

# TODO: special behavior for PRIMARY?
hdu = fits.ImageHDU(values, name=cid.label)
header = make_component_header(comp, data_header) if data_header else None
hdu = fits.ImageHDU(values, name=cid.label, header=header)
hdus.append(hdu)

try:
Expand Down
8 changes: 6 additions & 2 deletions glue/core/data_factories/fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ def new_data(suffix=True):
if is_image_hdu(hdu):
shape = hdu.data.shape
coords = coordinates_from_header(hdu.header)
units = hdu.header["BUNIT"] if "BUNIT" in hdu.header else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can write this as hdu.header.get('BUNIT') as that will return None if the key doesn't exist.

if not auto_merge or has_wcs(coords):
data = new_data(suffix=len(hdulist) > 1)
else:
try:
data = groups[extension_by_shape[shape]]
except KeyError:
data = new_data(suffix=len(hdulist) > 1)
data.add_component(component=hdu.data,
component = Component.autotyped(hdu.data, units=units)
data.add_component(component=component,
label=hdu_name)
elif is_table_hdu(hdu):
# Loop through columns and make component list
Expand Down Expand Up @@ -205,7 +207,9 @@ def casalike_cube(filename, **kwargs):
header = hdulist[0].header
result.coords = coordinates_from_header(header)
for i in range(array.shape[0]):
result.add_component(array[[i]], label='STOKES %i' % i)
units = header["BUNIT"] if "BUNIT" in header else None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above regarding using .get

component = Component.autotyped(array[[i]], units=units)
result.add_component(component, label='STOKES %i' % i)
return result


Expand Down