Skip to content

Commit

Permalink
Merge pull request #306 from MSeifert04/arit_with_quantities_fix
Browse files Browse the repository at this point in the history
fixes add/subtract arithmetics with quantities of different unit.
  • Loading branch information
crawfordsm committed Feb 16, 2016
2 parents 8aa38e5 + 8a8a51a commit 9d26428
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Other Changes and Additions
Bug Fixes
^^^^^^^^^

- Adding/Subtracting a CCDData instance with a Quantity with a different unit
produced wrong results. [#291]


0.3.2 (unreleased)
------------------

Expand Down
9 changes: 8 additions & 1 deletion ccdproc/ccddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,14 @@ def _ccddata_arithmetic(self, other, operation, scale_uncertainty=False):
# and a Quantity is currently broken, but it works with two Quantity
# arguments.
if isinstance(other, u.Quantity):
other_value = other.value
if (operation.__name__ in ['add', 'subtract'] and
self.unit != other.unit):
# For addition and subtraction we need to convert the unit
# to the same unit otherwise operating on the values alone will
# give wrong results (#291)
other_value = other.to(self.unit).value
else:
other_value = other.value
elif isinstance(other, numbers.Number):
other_value = other
else:
Expand Down
23 changes: 23 additions & 0 deletions ccdproc/tests/test_ccddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,29 @@ def test_arithmetic_overload_ccddata_operand(ccd_data):
expected_uncertainty)


def test_arithmetic_overload_differing_units():
a = np.array([1, 2, 3]) * u.m
b = np.array([1, 2, 3]) * u.cm
ccddata = CCDData(a)

# TODO: Could also be parametrized.
res = ccddata.add(b)
np.testing.assert_array_almost_equal(res.data, np.add(a, b).value)
assert res.unit == np.add(a, b).unit

res = ccddata.subtract(b)
np.testing.assert_array_almost_equal(res.data, np.subtract(a, b).value)
assert res.unit == np.subtract(a, b).unit

res = ccddata.multiply(b)
np.testing.assert_array_almost_equal(res.data, np.multiply(a, b).value)
assert res.unit == np.multiply(a, b).unit

res = ccddata.divide(b)
np.testing.assert_array_almost_equal(res.data, np.divide(a, b).value)
assert res.unit == np.divide(a, b).unit


def test_ccddata_header_does_not_corrupt_fits(ccd_data, tmpdir):
# This test is for the problem described in astropy/ccdproc#165
# The issue comes up when a long FITS keyword value is in a header
Expand Down

0 comments on commit 9d26428

Please sign in to comment.