Skip to content

Commit

Permalink
Merge pull request #113 from juliotux/header_double_keyword
Browse files Browse the repository at this point in the history
framedata: to_hdu fix for None history or comment
  • Loading branch information
juliotux committed Jul 9, 2023
2 parents e042ee6 + 86a32fa commit f6cb6f4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 26 deletions.
10 changes: 6 additions & 4 deletions astropop/framedata/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ def _to_hdu(frame, hdu_uncertainty=_HDU_UNCERT, hdu_flags=_HDU_FLAGS,
unit_string = u.format.Fits.to_string(frame.unit)
header[unit_key] = unit_string

for i in frame.history:
header['history'] = i
for i in frame.comment:
header['comment'] = i
if frame.history is not None:
for i in frame.history:
header['history'] = i
if frame.comment is not None:
for i in frame.comment:
header['comment'] = i
hdul = fits.HDUList(fits.PrimaryHDU(data, header=header))

if hdu_uncertainty and frame._unct is not None:
Expand Down
4 changes: 4 additions & 0 deletions astropop/framedata/framedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ def history(self):

@history.setter
def history(self, value):
if value is None:
return
if not np.isscalar(value):
self._history = self._history + list(value)
else:
Expand All @@ -351,6 +353,8 @@ def comment(self):

@comment.setter
def comment(self, value):
if value is None:
return
if not np.isscalar(value):
self._comments = self._comments + list(value)
else:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_framedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ def test_framedata_meta_history(self):
assert_not_in('history', frame.meta)
assert_equal(frame.history, ['testing history'])

def test_framedata_no_history(self):
frame = FrameData([[1]])
assert_is_instance(frame.history, list)
assert_equal(frame.history, [])

def test_framedata_no_history_meta(self):
frame = FrameData([[1]], meta={'test': 'testing no history'})
assert_is_instance(frame.history, list)
assert_equal(frame.history, [])

def test_framedata_no_comment(self):
frame = FrameData([[1]])
assert_is_instance(frame.comment, list)
assert_equal(frame.comment, [])

def test_framedata_no_comment_meta(self):
frame = FrameData([[1]], meta={'test': 'testing no comment'})
assert_is_instance(frame.comment, list)
assert_equal(frame.comment, [])


class TestFrameDataCreationData:
def test_framedata_empty(self):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_framedata_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ def test_to_hdu_comment(self):
assert_is_instance(hdul, fits.HDUList)
assert_equal(hdul[0].header['comment'], ['test 1', 'test 2', 'test'])

def test_to_hdu_none_history(self):
frame = create_framedata()
frame._history = None

hdul = frame.to_hdu()
assert_is_instance(hdul, fits.HDUList)
assert_not_in('HISTORY', hdul[0].header)

def test_to_hdu_none_comment(self):
frame = create_framedata()
frame._comment = None

hdul = frame.to_hdu()
assert_is_instance(hdul, fits.HDUList)
assert_not_in('COMMENT', hdul[0].header)

def test_write_unit_to_hdu(self):
frame = create_framedata()
ccd_unit = frame.unit
Expand Down
70 changes: 48 additions & 22 deletions tests/test_framedata_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,60 @@


class Test_FrameData_History:
def test_framedata_set_history(self):
hist = ['once upon a time', 'a small fits file',
'got read', 'by astropop',
'and stay in', 'computer memory.']

def test_framedata_set_history_string(self):
frame = create_framedata()
frame.history = 'once upon a time'
frame.history = 'a small fits file'
frame.history = ['got read', 'by astropop']
frame.history = ('and stay in', 'computer memory.')
for i in self.hist:
frame.history = i
assert_equal(len(frame.history), 6)
assert_equal(frame.history, self.hist)

def test_framedata_set_history_none(self):
frame = create_framedata()
frame.history = None
assert_equal(frame.history, [])

def test_framedata_set_history_list(self):
frame = create_framedata()
frame.history = list(self.hist)
assert_equal(len(frame.history), 6)
assert_equal(frame.history[0], 'once upon a time')
assert_equal(frame.history[1], 'a small fits file')
assert_equal(frame.history[2], 'got read')
assert_equal(frame.history[3], 'by astropop')
assert_equal(frame.history[4], 'and stay in')
assert_equal(frame.history[5], 'computer memory.')
assert_equal(frame.history, self.hist)

def test_framedata_set_history_tuple(self):
frame = create_framedata()
frame.history = tuple(self.hist)
assert_equal(len(frame.history), 6)
assert_equal(frame.history, self.hist)


class Test_FrameData_Comment:
def test_framedata_set_comment(self):
comm = ['this is a test', 'to make commenst in astropop', 'that can',
'be lists', 'or also', 'tuples.']

def test_framedata_set_comment_string(self):
frame = create_framedata()
frame.comment = 'this is a test'
frame.comment = 'to make commenst in astropop'
frame.comment = ['that can', 'be lists']
frame.comment = ('or also', 'tuples.')
for i in self.comm:
frame.comment = i

assert_equal(len(frame.comment), 6)
assert_equal(frame.comment[0], 'this is a test')
assert_equal(frame.comment[1], 'to make commenst in astropop')
assert_equal(frame.comment[2], 'that can')
assert_equal(frame.comment[3], 'be lists')
assert_equal(frame.comment[4], 'or also')
assert_equal(frame.comment[5], 'tuples.')
assert_equal(frame.comment, self.comm)

def test_frame_set_comment_none(self):
frame = create_framedata()
frame.comment = None
assert_equal(frame.comment, [])

def test_frame_set_comment_list(self):
frame = create_framedata()
frame.comment = list(self.comm)
assert_equal(len(frame.comment), 6)
assert_equal(frame.comment, self.comm)

def test_frame_set_comment_tuple(self):
frame = create_framedata()
frame.comment = tuple(self.comm)
assert_equal(len(frame.comment), 6)
assert_equal(frame.comment, self.comm)

0 comments on commit f6cb6f4

Please sign in to comment.