Skip to content

Commit

Permalink
Refactor file extensions to make it a property
Browse files Browse the repository at this point in the history
This ensures that a refresh picks up the correct files, and avoids having
`extension=` arguments all over the place.
  • Loading branch information
mwcraig committed Mar 15, 2021
1 parent 8b07299 commit f1d6064
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions ccdproc/image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ def __init__(self, location=None, keywords=None,
else:
self._location = ''

# Set file name extensions
# Do our best to keep the file extensions immutable
if extensions is not None:
if isinstance(extensions, str):
# Comma at the end to force it to be a tuple
self._file_extensions = (extensions,)
else:
self._file_extensions = tuple(extensions)
else:
self._file_extensions = tuple(_recognized_fits_file_extensions)

self._find_fits_by_reading = find_fits_by_reading

self._filenames = filenames
self._files = []
self._files = self._get_files(extensions=extensions)
self._files = self._get_files()

if self._files == []:
warnings.warn("no FITS files in the collection.",
Expand Down Expand Up @@ -292,11 +303,19 @@ def glob_exclude(self):
@property
def ext(self):
"""
str or int, The extension from which the header and data will
str or int, The FITS extension from which the header and data will
be read in all files.
"""
return self._ext

@property
def file_extensions(self):
"""
List of file name extensions to match when populating or refreshing
the ``ImageFileCollection``.
"""
return self._file_extensions

def values(self, keyword, unique=False):
"""
List of values for a keyword.
Expand Down Expand Up @@ -437,7 +456,7 @@ def filter(self, **kwd):
return ImageFileCollection(filenames=files,
keywords=self.keywords)

def _get_files(self, extensions=None):
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.
Expand Down Expand Up @@ -752,18 +771,13 @@ def _find_keywords_by_values(self, **kwd):
self.summary['file'].mask = ma.nomask
self.summary['file'].mask[~matches] = True

def _fits_files_in_directory(self, extensions=None,
def _fits_files_in_directory(self,
compressed=True):
"""
Get names of FITS files in directory, based on filename extension.
Parameters
----------
extensions : list of str or None, optional
List of filename extensions that are FITS files. Default is
``['fit', 'fits', 'fts']``.
Default is ``None``.
compressed : bool, optional
If ``True``, compressed files should be included in the list
(e.g. `.fits.gz`).
Expand All @@ -774,10 +788,12 @@ def _fits_files_in_directory(self, extensions=None,
list
*Names* of the files (with extension), not the full pathname.
"""
# Force a copy of the extensions to avoid endless combinations of
# compression extensions.
full_extensions = list(self.file_extensions)

full_extensions = extensions or list(_recognized_fits_file_extensions)

# The common compressed fits image .fz is supported using ext=1 when calling ImageFileCollection
# The common compressed fits image .fz is supported using ext=1 when
# calling ImageFileCollection
if compressed:
for comp in ['.gz', '.bz2', '.Z', '.zip', '.fz']:
with_comp = [extension + comp for extension in full_extensions]
Expand Down

0 comments on commit f1d6064

Please sign in to comment.