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

Allow ImageFileCollections to take a list of file names #403

Merged
merged 11 commits into from Oct 17, 2016
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,9 @@
New Features
^^^^^^^^^^^^

- Add an optional attribute named ``filenames`` to ``ImageFileCollection``,
so that users can pass a list of FITS files to the collection. [#374]

Other Changes and Additions
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
36 changes: 32 additions & 4 deletions ccdproc/image_collection.py
Expand Up @@ -34,7 +34,7 @@ class ImageFileCollection(object):
Parameters
----------
location : str or None, optional
path to directory containing FITS files.
Path to directory containing FITS files.
Default is ``None``.

keywords : list of str, '*' or None, optional
Expand All @@ -51,17 +51,25 @@ class ImageFileCollection(object):
list.
Default is ``None``.

filenames: str, list of str, or None, optional
List of the names of FITS files which will be added to the collection.
The filenames are assumed to be in ``location``.
Default is ``None``.

Raises
------
ValueError
Raised if keywords are set to a combination of '*' and any other
value.
"""
def __init__(self, location=None, keywords=None, info_file=None):
def __init__(self, location=None, keywords=None, info_file=None,
filenames=None):
self._location = location
self._filenames = filenames
self._files = []
if location:
self._files = self._fits_files_in_directory()
self._files = self._get_files()

if self._files == []:
warnings.warn("no FITS files in the collection.",
AstropyUserWarning)
Expand Down Expand Up @@ -271,7 +279,7 @@ def refresh(self):
"""
keywords = '*' if self._all_keywords else self.keywords
# Re-load list of files
self._files = self._fits_files_in_directory()
self._files = self._get_files()
self._summary_info = self._fits_summary(header_keywords=keywords)

def sort(self, keys=None):
Expand All @@ -290,6 +298,26 @@ def sort(self, keys=None):
self._summary_info.sort(keys)
self._files = list(self.summary_info['file'])

def _get_files(self):
""" Helper method which checks whether ``files`` should be set
to a subset of file names or to all file names in a directory.

Returns
-------
files : list or str
List of file names which will be added to the collection.
"""
files = []
if self._filenames:
if isinstance(self._filenames, six.string_types):
files.append(self._filenames)
else:
files = self._filenames
else:
files = self._fits_files_in_directory()

return files

def _dict_from_fits_header(self, file_name, input_summary=None,
missing_marker=None):
"""
Expand Down
14 changes: 14 additions & 0 deletions ccdproc/tests/test_image_collection.py
Expand Up @@ -85,6 +85,20 @@ def test_summary_is_summary_info(self, triage_setup):
location=triage_setup.test_dir, keywords=['imagetyp', 'filter'])
assert img_collection.summary is img_collection.summary_info

def test_filenames_are_set_properly(self, triage_setup):
fn = ['filter_no_object_bias.fit', 'filter_object_light_foo.fit']
img_collection = image_collection.ImageFileCollection(
location=triage_setup.test_dir, filenames=fn, keywords=['filter'])
assert img_collection.files == fn

img_collection.refresh()
assert img_collection.files == fn

fn = 'filter_no_object_bias.fit'
img_collection = image_collection.ImageFileCollection(
location=triage_setup.test_dir, filenames=fn, keywords=['filter'])
assert img_collection.files == [fn]

def test_files_with_compressed(self, triage_setup):
collection = image_collection.ImageFileCollection(
location=triage_setup.test_dir)
Expand Down