From 3499fdfb4212aaa98a2a49efa72a390b174c3834 Mon Sep 17 00:00:00 2001 From: Benjamin Alan Weaver Date: Fri, 10 Jan 2025 09:44:59 -0700 Subject: [PATCH] don't raise a warning for identical units --- py/desiutil/annotate.py | 4 ++-- py/desiutil/test/test_annotate.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/py/desiutil/annotate.py b/py/desiutil/annotate.py index 206cf48..8f49177 100644 --- a/py/desiutil/annotate.py +++ b/py/desiutil/annotate.py @@ -426,14 +426,14 @@ def annotate_fits(filename, extension, output, units=None, comments=None, for colname in column_index: ttype = f"TTYPE{column_index[colname]:d}" if colname in column_comments: - if hdu.header.comments[ttype].strip(): + if hdu.header.comments[ttype].strip() and column_comments[colname] != hdu.header.comments[ttype].strip(): log.warning("Overriding comment on column '%s': '%s' -> '%s'.", colname, hdu.header.comments[ttype].strip(), column_comments[colname]) hdu.header[ttype] = (colname, column_comments[colname]) log.debug('Set %s comment to "%s"', colname, column_comments[colname]) if colname in column_units: tunit = f"TUNIT{column_index[colname]:d}" - if tunit in hdu.header and hdu.header[tunit].strip(): + if tunit in hdu.header and hdu.header[tunit].strip() and column_units[colname] != hdu.header[tunit].strip(): log.warning("Overriding units for column '%s': '%s' -> '%s'.", colname, hdu.header[tunit].strip(), column_units[colname]) hdu.header[tunit] = (column_units[colname], colname+' units') diff --git a/py/desiutil/test/test_annotate.py b/py/desiutil/test/test_annotate.py index 444eef6..5d87314 100644 --- a/py/desiutil/test/test_annotate.py +++ b/py/desiutil/test/test_annotate.py @@ -386,6 +386,16 @@ def test_annotate_fits(self, mock_log): self.assertEqual(new_hdulist[2].header['TUNIT2'], 'deg') self.assertEqual(new_hdulist[2].header['TUNIT3'], 'deg') new_hdulist_name = os.path.join(self.TMP, 'test_annotate_update2.fits') + # + # Changing the unit to the same unit should not raise a warning. + # + new_hdulist = annotate_fits(self.fits_file, 2, new_hdulist_name, units={'MAG': 'mag'}, overwrite=True) + self.assertIn('TUNIT4', new_hdulist[2].header) + self.assertEqual(new_hdulist[2].header['TUNIT4'], 'mag') + mock_log().warning.assert_not_called() + # + # Actually change the units. + # new_hdulist = annotate_fits(self.fits_file, 2, new_hdulist_name, units={'MAG': 'nJy'}, overwrite=True) self.assertIn('TUNIT4', new_hdulist[2].header) self.assertEqual(new_hdulist[2].header['TUNIT4'], 'nJy')