Skip to content

Commit

Permalink
allow DN as a unit on Map
Browse files Browse the repository at this point in the history
  • Loading branch information
wtbarnes committed Apr 23, 2024
1 parent 9529ee0 commit 266115c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
21 changes: 12 additions & 9 deletions sunpy/map/mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ def _new_instance_from_op(self, new_data):
operations.
"""
new_meta = copy.deepcopy(self.meta)
new_meta['bunit'] = new_data.unit.to_string('fits')
new_meta['bunit'] = new_data.unit.to_string()
return self._new_instance(new_data.value, new_meta, plot_settings=self.plot_settings)

def __neg__(self):
Expand Down Expand Up @@ -727,19 +727,22 @@ def max(self, *args, **kwargs):
@staticmethod
def _parse_fits_unit(unit_str):
replacements = {'gauss': 'G',
'dn': 'ct',
'dn/s': 'ct/s',
'counts / pixel': 'ct/pix',}
if unit_str.lower() in replacements:
unit_str = replacements[unit_str.lower()]
unit = u.Unit(unit_str, format='fits', parse_strict='silent')
if isinstance(unit, u.UnrecognizedUnit):
warn_metadata(f'Could not parse unit string "{unit_str}" as a valid FITS unit.\n'
f'See {_META_FIX_URL} for how to fix metadata before loading it '
'with sunpy.map.Map.\n'
'See https://fits.gsfc.nasa.gov/fits_standard.html for '
'the FITS unit standards.')
unit = None
# NOTE: Special case DN here as it is not part of the FITS standard, but
# is widely used and is also a recognized astropy unit
if 'DN' in str(unit):
unit = u.Unit(unit_str)
else:
warn_metadata(f'Could not parse unit string "{unit_str}" as a valid FITS unit.\n'
f'See {_META_FIX_URL} for how to fix metadata before loading it '
'with sunpy.map.Map.\n'
'See https://fits.gsfc.nasa.gov/fits_standard.html for '
'the FITS unit standards.')
unit = None
return unit

@property
Expand Down
2 changes: 1 addition & 1 deletion sunpy/map/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def generic_map():
'detector': 'bar',
'wavelnth': 10,
'waveunit': 'm',
'bunit': 'ct/s',
'bunit': 'DN/s',
}
return sunpy.map.Map((data, header))

Expand Down
38 changes: 19 additions & 19 deletions sunpy/map/tests/test_mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def test_std(generic_map):


def test_unit(generic_map):
assert generic_map.unit == u.ct / u.s
assert generic_map.unit == u.DN / u.s
generic_map.meta['bunit'] = 'not a unit'
with pytest.warns(SunpyMetadataWarning, match='Could not parse unit string "not a unit"'):
assert generic_map.unit is None
Expand Down Expand Up @@ -1682,12 +1682,10 @@ def check_arithmetic_value_and_units(map_new, data_expected):


@pytest.mark.parametrize('value', [
10 * u.ct,
10 * u.mct,
u.Quantity([10], u.ct),
u.Quantity(np.random.rand(128), u.ct),
u.Quantity(np.random.rand(128, 128), u.ct),
u.Quantity(np.random.rand(128, 128), u.mct),
10 * u.DN,
u.Quantity([10], u.DN),
u.Quantity(np.random.rand(128), u.DN),
u.Quantity(np.random.rand(128, 128), u.DN),
])
def test_map_arithmetic_addition_subtraction(aia171_test_map, value):
new_map = aia171_test_map + value
Expand All @@ -1701,10 +1699,10 @@ def test_map_arithmetic_addition_subtraction(aia171_test_map, value):


@pytest.mark.parametrize('value', [
10 * u.ct,
u.Quantity([10], u.ct),
u.Quantity(np.random.rand(128), u.ct),
u.Quantity(np.random.rand(128, 128), u.ct),
10 * u.s,
u.Quantity([10], u.s),
u.Quantity(np.random.rand(128), u.s),
u.Quantity(np.random.rand(128, 128), u.s),
10.0,
np.random.rand(128),
np.random.rand(128, 128),
Expand Down Expand Up @@ -1741,14 +1739,16 @@ def test_map_arithmetic_operations_raise_exceptions(aia171_test_map, value):
with pytest.raises(TypeError):
_ = value / aia171_test_map


def test_parse_fits_units():
# Check that we parse a BUNIT of G correctly.
out_unit = GenericMap._parse_fits_unit("Gauss")
assert out_unit == u.G

out_unit = GenericMap._parse_fits_unit("G")
assert out_unit == u.G
@pytest.mark.parametrize(('units_string','expected_unit'),[
('Gauss', u.G),
('G', u.G),
('DN', u.DN),
('DN/s', u.DN/u.s),
('counts / pixel', u.ct/u.pix),
])
def test_parse_fits_units(units_string, expected_unit):
out_unit = GenericMap._parse_fits_unit(units_string)
assert out_unit == expected_unit


def test_only_cd():
Expand Down

0 comments on commit 266115c

Please sign in to comment.