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

Read image unit #113

Merged
merged 2 commits into from
Jun 9, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 18 additions & 4 deletions ccdproc/ccddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from astropy.io import fits, registry
from astropy.utils.compat.odict import OrderedDict
from astropy import units as u
import astropy
from astropy import log

from .utils.collections import CaseInsensitiveOrderedDict

Expand Down Expand Up @@ -262,8 +262,10 @@ def fits_ccddata_reader(filename, hdu=0, unit=None, **kwd):
hdu : int, optional
FITS extension from which CCDData should be initialized.

unit : astropy.units.Unit
Units of the image data
unit : astropy.units.Unit, optional
Units of the image data. If this argument is provided and there is a
unit for the image in the FITS header (the keyword ``BUNIT`` is used
as the unit, if present), this argument is used for the unit.

kwd :
Any additional keyword parameters are passed through to the FITS reader
Expand All @@ -286,7 +288,19 @@ def fits_ccddata_reader(filename, hdu=0, unit=None, **kwd):
prefix = 'Unsupported keyword: {0}.'.format(key)
raise TypeError(' '.join([prefix, msg]))
hdus = fits.open(filename, **kwd)
ccd_data = CCDData(hdus[hdu].data, meta=hdus[hdu].header, unit=unit)
hdr = hdus[hdu].header

try:
fits_unit_string = hdr['bunit']
except KeyError:
fits_unit_string = None

if unit is not None and fits_unit_string:
log.info("Using the unit {0} passed to the FITS reader instead of "
"the unit {1} in the FITS file.", unit, fits_unit_string)

use_unit = unit or fits_unit_string
ccd_data = CCDData(hdus[hdu].data, meta=hdus[hdu].header, unit=use_unit)
hdus.close()
return ccd_data

Expand Down
15 changes: 15 additions & 0 deletions ccdproc/tests/test_ccddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ def test_initialize_from_FITS(ccd_data, tmpdir):
assert cd.meta[k] == v


def test_initialize_from_fits_with_unit_in_header(tmpdir):
fake_img = np.random.random(size=(100, 100))
hdu = fits.PrimaryHDU(fake_img)
hdu.header['bunit'] = u.adu.to_string()
filename = tmpdir.join('afile.fits').strpath
hdu.writeto(filename)
ccd = CCDData.read(filename)
# ccd should pick up the unit adu from the fits header...did it?
assert ccd.unit is u.adu

# An explicit unit in the read overrides any unit in the FITS file
ccd2 = CCDData.read(filename, unit="photon")
assert ccd2.unit is u.photon


def test_initialize_from_FITS_bad_keyword_raises_error(ccd_data, tmpdir):
# There are two fits.open keywords that are not permitted in ccdpro:
# do_not_scale_image_data and scale_back
Expand Down
5 changes: 3 additions & 2 deletions docs/ccdproc/ccddata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ A `~ccdproc.ccddata.CCDData` object can also be initialized from a FITS file:

>>> ccd = ccdproc.CCDData.read('my_file.fits', unit="adu") # doctest: +SKIP

but for the moment you need to set the unit explicitly, even if it is in the
FITS header.
If there is a unit in the FITS file (in the ``BUNIT`` keyword), that will be
used, but a unit explicitly provided in ``read`` will override any unit in the
FITS file.

There is no restriction at all on what the unit can be -- any unit in
`astropy.units` or that you create yourself will work.
Expand Down