Skip to content

Commit

Permalink
Merge 91af3ed into 9775509
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed Jan 20, 2015
2 parents 9775509 + 91af3ed commit f139294
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
42 changes: 31 additions & 11 deletions msumastro/image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def __init__(self, location=None, keywords=None, info_file=None):
info_path)
else:
raise

# Used internally to keep track of whether the user asked for all
# keywords or a specific list. The keywords setter takes care of
# actually setting the correct value, this just ensure that there
# is always *some* value.
self._all_keywords = False

if keywords:
self.keywords = keywords

Expand Down Expand Up @@ -132,6 +139,11 @@ def keywords(self, keywords=None):
self._summary_info = []
return

if keywords == '*':
self._all_keywords = True
else:
self._all_keywords = False

logging.debug('keywords in setter before pruning: %s', keywords)

# remove duplicates and force a copy
Expand Down Expand Up @@ -212,10 +224,18 @@ def files_filtered(self, **kwd):
self.summary_info['file'].mask = current_file_mask
return filtered_files

def _table_from_fits_header(self, file_name, input_summary=None,
missing_marker=None):
def refresh(self):
"""
Refresh the collection by re-reading headers.
"""
keywords = '*' if self._all_keywords else self.keywords
self._summary_info = self._fits_summary(header_keywords=keywords)

def _dict_from_fits_header(self, file_name, input_summary=None,
missing_marker=None):
"""
Construct a table whose columns are the header keywords
Construct a dictionary whose keys are the header keywords and values
are a list of the values from this file and the input dictionary.
Parameters
----------
Expand All @@ -233,12 +253,12 @@ def _table_from_fits_header(self, file_name, input_summary=None,
"""
from collections import OrderedDict

def _add_val_to_table(key, value, table, n_prev):
def _add_val_to_dict(key, value, tbl_dict, n_prev):
try:
table[key.lower()].append(value)
tbl_dict[key.lower()].append(value)
except KeyError:
table[key.lower()] = [missing_marker] * n_previous
table[key.lower()].append(value)
tbl_dict[key.lower()] = [missing_marker] * n_previous
tbl_dict[key.lower()].append(value)

if input_summary is None:
summary = OrderedDict()
Expand Down Expand Up @@ -269,18 +289,18 @@ def _add_val_to_table(key, value, table, n_prev):

if k.lower() in ['comment', 'history']:
multi_entry_keys[k.lower()].append(str(v))
# Accumulate these is a separate dictionary until the
# Accumulate these in a separate dictionary until the
# end to avoid adding multiple entries to summary.
continue
else:
val = v

_add_val_to_table(k, val, summary, n_previous)
_add_val_to_dict(k, val, summary, n_previous)

for k, v in multi_entry_keys.iteritems():
if v:
joined = ','.join(v)
_add_val_to_table(k, joined, summary, n_previous)
_add_val_to_dict(k, joined, summary, n_previous)

for missing in missing_in_this_file:
summary[missing].append(missing_marker)
Expand Down Expand Up @@ -331,7 +351,7 @@ def _fits_summary(self, header_keywords=None):
for file_name in file_name_column:
file_path = path.join(self.location, file_name)
try:
summary_dict = self._table_from_fits_header(
summary_dict = self._dict_from_fits_header(
file_path, input_summary=summary_dict,
missing_marker=missing_marker)
except IOError as e:
Expand Down
14 changes: 14 additions & 0 deletions msumastro/tests/test_image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,17 @@ def test_header_with_long_history_roundtrips_to_disk(self, triage_setup):
ic.summary_info.write('test_table.txt', format='ascii.csv')
table_disk = Table.read('test_table.txt', format='ascii.csv')
assert len(table_disk) == len(ic.summary_info)

def test_refresh_method(self, triage_setup):
ic = tff.ImageFileCollection(triage_setup.test_dir, keywords='*')
# Add a keyword I know isn't already in the header to each file.
not_in_header = 'BARKARK'
for h in ic.headers(overwrite=True):
h[not_in_header] = True
print(h)
assert not_in_header not in ic.summary_info.colnames

ic.refresh()
# After refreshing the odd keyword should be present.
print(ic.keywords)
assert not_in_header.lower() in ic.summary_info.colnames

0 comments on commit f139294

Please sign in to comment.