Skip to content

Commit

Permalink
raise or warn instead of log unit errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Sep 20, 2023
1 parent aeb2ec2 commit d263ba7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
13 changes: 9 additions & 4 deletions py/desiutil/annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os
import sys
from argparse import ArgumentParser
from warnings import warn
import yaml
from astropy.io import fits
from astropy.table import Table, QTable
Expand All @@ -19,6 +20,12 @@
from .log import get_logger, DEBUG


class FITSUnitWarning(UserWarning):
"""Warnings related to invalid FITS units.
"""
pass


def find_column_name(columns, prefix=('unit', )):
"""Find the column that contains unit descriptions, or comments.
Expand Down Expand Up @@ -95,11 +102,10 @@ def validate_unit(unit, error=False):
Raises
------
:exc:`ValueError`
If `error` is set and the unit can't be parsed.
If `error` is set and `unit` can't be parsed.
"""
if unit is None:
return None
log = get_logger()
acceptable_units = ('maggie', 'maggy', 'mgy',
'electron/Angstrom',
'log(Angstrom)')
Expand All @@ -112,10 +118,9 @@ def validate_unit(unit, error=False):
return bad_unit
else:
if error:
log.critical(str(e))
raise
else:
log.warning(str(e))
warn(m, FITSUnitWarning)
return None


Expand Down
15 changes: 8 additions & 7 deletions py/desiutil/test/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from astropy.io import fits
from astropy.table import Table, QTable
from ..annotate import (annotate_fits, annotate_table, find_column_name,
from ..annotate import (FITSUnitWarning, annotate_fits, annotate_table, find_column_name,
find_key_name, validate_unit, load_csv_units,
load_yml_units, _options)

Expand Down Expand Up @@ -97,8 +97,7 @@ def test_find_key_name(self):
k = find_key_name({'RA': 'deg', 'DEC': 'deg', 'COLUMN': None}, prefix=('comment', 'description'))
self.assertEqual(e.exception.args[0], "No key matching 'comment' found!")

@patch('desiutil.annotate.get_logger')
def test_validate_unit(self, mock_log):
def test_validate_unit(self):
"""Test function to validate units.
"""
m = "'ergs' did not parse as fits unit: At col 0, Unit 'ergs' not supported by the FITS standard. Did you mean erg?"
Expand All @@ -109,9 +108,13 @@ def test_validate_unit(self, mock_log):
self.assertIsNone(c)
c = validate_unit('erg')
self.assertIsNone(c)
c = validate_unit('ergs', error=False)
with self.assertWarns(FITSUnitWarning) as w:
c = validate_unit('ergs', error=False)
self.assertEqual(str(w.warning), m + l)
self.assertIsNone(c)
c = validate_unit('1/deg^2')
with self.assertWarns(FITSUnitWarning) as w:
c = validate_unit('1/deg^2')
self.assertEqual(str(w.warning), d + l)
self.assertIsNone(c)
c = validate_unit('nanomaggies', error=True)
self.assertEqual(c, "'nanomaggies'")
Expand All @@ -121,8 +124,6 @@ def test_validate_unit(self, mock_log):
with self.assertRaises(ValueError) as e:
c = validate_unit('1/nanomaggy^2', error=True)
self.assertEqual(str(e.exception), f + l)
mock_log().warning.assert_has_calls([call(m + l), call(d + l)])
mock_log().critical.assert_has_calls([call(m + l), call(f + l)])

@patch('desiutil.annotate.get_logger')
def test_load_csv_units(self, mock_log):
Expand Down

0 comments on commit d263ba7

Please sign in to comment.