Skip to content

Commit

Permalink
Merge 531454c into d205d6c
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Jul 26, 2019
2 parents d205d6c + 531454c commit ae05385
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 154 deletions.
98 changes: 43 additions & 55 deletions ccdproc/image_collection.py
Expand Up @@ -48,18 +48,16 @@ class ImageFileCollection(object):
columns. Default value is '*' unless ``info_file`` is specified.
Default is ``None``.
info_file : str or None, optional
Path to file that contains a table of information about FITS files.
In this case the keywords are set to the names of the columns of the
``info_file`` unless ``keywords`` is explicitly set to a different
list.
Default is ``None``.
.. deprecated:: 1.3
find_fits_by_reading: bool, optional
If ``True``, read each file in location to check whether the file is a
FITS file and include it in the collection based on that, rather than
by file name. Compressed files, e.g. image.fits.gz, will **NOT** be
properly detected. *Will be ignored if `filenames` is not ``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``.
The filenames may either be in ``location`` or the name can be a
relative or absolute path to the file.
Default is ``None``.
glob_include: str or None, optional
Expand All @@ -74,23 +72,20 @@ class ImageFileCollection(object):
easily select subsets of files in the target directory.
Default is ``None``.
ext: str or int, optional
The extension from which the header and data will be read in all files.
Default is ``0``.
ext: str or int, optional
The extension from which the header and data will be read in all
files.Default is ``0``.
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,
filenames=None, glob_include=None, glob_exclude=None, ext=0):

if info_file is not None:
warnings.warn("The 'info_file' argument is deprecated and will be "
"removed in a future version", DeprecationWarning)

def __init__(self, location=None, keywords=None,
find_fits_by_reading=False,
filenames=None, glob_include=None, glob_exclude=None, ext=0):
# Include or exclude files from the collection based on glob pattern
# matching - has to go above call to _get_files()
if glob_exclude is not None:
Expand All @@ -101,41 +96,24 @@ def __init__(self, location=None, keywords=None, info_file=None,
glob_include = str(glob_include)
self._glob_include = glob_include

self._location = location
if location is not None:
self._location = location
else:
self._location = ''

self._find_fits_by_reading = find_fits_by_reading

self._filenames = filenames
self._files = []
self._info_file = info_file
if location:
self._files = self._get_files()
self._files = self._get_files()

if self._files == []:
warnings.warn("no FITS files in the collection.",
AstropyUserWarning)
self._summary = {}
if keywords is None:
if info_file is not None:
# Default to empty list so that keywords will be populated
# from table columns names.
keywords = []
else:
# Otherwise use all keywords.
keywords = '*'
if info_file is not None:
try:
info_path = path.join(self.location, info_file)
except (AttributeError, TypeError):
info_path = info_file
try:
self._summary = Table.read(info_path, format='ascii',
delimiter=',')
self._summary = Table(self._summary, masked=True)
except IOError:
if location:
logger.warning('unable to open table file %s, will try '
'initializing from location instead.',
info_path)
else:
raise
# Use all keywords.
keywords = '*'

# Used internally to keep track of whether the user asked for all
# keywords or a specific list. The keywords setter takes care of
Expand All @@ -159,11 +137,6 @@ def __repr__(self):
else:
kw = "keywords={!r}".format(self.keywords[1:])

if self._info_file is None:
infofile = ''
else:
infofile = "info_file={!r}".format(self._info_file)

if self.glob_exclude is None:
glob_exclude = ''
else:
Expand All @@ -184,7 +157,7 @@ def __repr__(self):
else:
filenames = "filenames={}".format(self._filenames)

params = [location, kw, infofile, filenames, glob_include, glob_exclude, ext]
params = [location, kw, filenames, glob_include, glob_exclude, ext]
params = ', '.join([p for p in params if p])

str_repr = "{self.__class__.__name__}({params})".format(
Expand Down Expand Up @@ -491,12 +464,19 @@ def _add_val_to_dict(key, value, tbl_dict, n_previous, missing_marker):

assert 'file' not in h

if self.location:
# We have a location and can reconstruct the path using it
name_for_file_column = path.basename(file_name)
else:
# No location, so use whatever path the user passed in
name_for_file_column = file_name

# Try opening header before this so that file name is only added if
# file is valid FITS
try:
summary['file'].append(path.basename(file_name))
summary['file'].append(name_for_file_column)
except KeyError:
summary['file'] = [path.basename(file_name)]
summary['file'] = [name_for_file_column]

missing_in_this_file = [k for k in summary if (k not in h and
k != 'file')]
Expand Down Expand Up @@ -738,8 +718,16 @@ def _fits_files_in_directory(self, extensions=None,

all_files = listdir(self.location)
files = []
for extension in full_extensions:
files.extend(fnmatch.filter(all_files, '*' + extension))
if not self._find_fits_by_reading:
for extension in full_extensions:
files.extend(fnmatch.filter(all_files, '*' + extension))
else:
for file in all_files:
with open(file, 'rb') as fp:
# Hmm, first argument to is_fits is not actually used in
# that function. *shrug*
if fits.connect.is_fits('just some junk', file, fp):
files.append(file)

files.sort()
return files
Expand Down

0 comments on commit ae05385

Please sign in to comment.