diff --git a/astropy/io/fits/connect.py b/astropy/io/fits/connect.py index 90fd62b1760..cdb6fcb7289 100644 --- a/astropy/io/fits/connect.py +++ b/astropy/io/fits/connect.py @@ -15,6 +15,7 @@ from ...table import Table from ...utils import OrderedDict from ...utils.exceptions import AstropyUserWarning +from astropy.units.format.fits import UnitScaleError from . import HDUList, TableHDU, BinTableHDU, GroupsHDU from .hdu.hdulist import fitsopen as fits_open @@ -238,7 +239,15 @@ def write_table_fits(input, output, overwrite=False): # Set units for output HDU for col in table_hdu.columns: if input[col.name].unit is not None: - col.unit = input[col.name].unit.to_string(format='fits') + try: + col.unit = input[col.name].unit.to_string(format='fits') + except UnitScaleError: + scale = input[col.name].unit.scale + raise UnitScaleError( + "The column '{0}' could not be stored in FITS format " + "because it has a scale '({1})' that " + "is not recognized by the FITS standard. Either scale " + "the data or change the units.".format(col.name, str(scale))) for key, value in input.meta.items(): diff --git a/astropy/io/fits/tests/test_connect.py b/astropy/io/fits/tests/test_connect.py index 7e766f179ae..0f2f4d58b20 100644 --- a/astropy/io/fits/tests/test_connect.py +++ b/astropy/io/fits/tests/test_connect.py @@ -10,6 +10,7 @@ from .... import units as u from .... import log from ....tests.helper import pytest, catch_warnings +from astropy.units.format.fits import UnitScaleError PY3 = sys.version_info[0] >= 3 DATA = os.path.join(os.path.dirname(__file__), 'data') @@ -221,3 +222,15 @@ def test_masking_regression_1795(): assert np.all(t['c2'].data == np.array(['abc', 'xy '])) assert_allclose(t['c3'].data, np.array([3.70000007153, 6.6999997139])) assert np.all(t['c4'].data == np.array([False, True])) + +def test_scale_error(): + from astropy.table import Table + a = [1, 4, 5] + b = [2.0, 5.0, 8.2] + c = ['x', 'y', 'z'] + t = Table([a, b, c], names=('a', 'b', 'c'), meta={'name': 'first table'}) + t['a'].unit='percent' + with pytest.raises(UnitScaleError) as exc: + t.write('t.fits',format='fits', overwrite=True) + assert exc.value.args[0]=="The column 'a' could not be stored in FITS format because it has a scale '(1.0)' that is not recognized by the FITS standard. Either scale the data or change the units." + diff --git a/astropy/units/format/fits.py b/astropy/units/format/fits.py index da31ea54d0b..2cc6a7eba1b 100644 --- a/astropy/units/format/fits.py +++ b/astropy/units/format/fits.py @@ -17,6 +17,11 @@ from . import utils from ...utils.misc import did_you_mean +class UnitScaleError(ValueError): + """ + Used to catch the errors involving scaled units, + which are not recognized by FITS format. + """ class Fits(generic.Generic): """ @@ -119,7 +124,7 @@ def to_string(self, unit): if isinstance(unit, core.CompositeUnit): if unit.scale != 1: - raise ValueError( + raise UnitScaleError( "The FITS unit format is not able to represent scale. " "Multiply your data by {0:e}.".format(unit.scale))