Skip to content

Commit 6d695f6

Browse files
mdboomdhomeier
authored andcommitted
Fix astropy#3991: FITS unit format fixes
- Accept power-of-10 scales in FITS units (and only power-of-10 scales) - Accept * as a separator between the scale factor and the units
1 parent 342deb4 commit 6d695f6

5 files changed

Lines changed: 142 additions & 64 deletions

File tree

‎astropy/units/format/fits.py‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
unicode_literals)
99
from ...extern.six.moves import zip
1010

11+
import numpy as np
12+
1113
import copy
1214
import keyword
1315

@@ -114,16 +116,25 @@ def to_string(cls, unit):
114116
# Remove units that aren't known to the format
115117
unit = utils.decompose_to_known_units(unit, cls._get_unit_name)
116118

119+
parts = []
120+
117121
if isinstance(unit, core.CompositeUnit):
118-
if unit.scale != 1:
122+
base = np.log10(unit.scale)
123+
124+
if base % 1.0 != 0.0:
119125
raise core.UnitScaleError(
120-
"The FITS unit format is not able to represent scale. "
121-
"Multiply your data by {0:e}.".format(unit.scale))
126+
"The FITS unit format is not able to scales that "
127+
"are not powers of 10. Multiply your data by "
128+
"{0:e}.".format(unit.scale))
129+
elif unit.scale != 1.0:
130+
parts.append('10**{0}'.format(int(base)))
122131

123132
pairs = list(zip(unit.bases, unit.powers))
124-
pairs.sort(key=lambda x: x[1], reverse=True)
133+
if len(pairs):
134+
pairs.sort(key=lambda x: x[1], reverse=True)
135+
parts.append(cls._format_unit_list(pairs))
125136

126-
s = cls._format_unit_list(pairs)
137+
s = ' '.join(parts)
127138
elif isinstance(unit, core.NamedUnit):
128139
s = cls._get_unit_name(unit)
129140

‎astropy/units/format/generic.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,22 @@ def p_main(p):
157157
'''
158158
main : product_of_units
159159
| factor product_of_units
160+
| factor product product_of_units
160161
| division_product_of_units
161162
| factor division_product_of_units
163+
| factor product division_product_of_units
162164
| inverse_unit
163165
| factor inverse_unit
166+
| factor product inverse_unit
164167
| factor
165168
'''
166169
from ..core import Unit
167170
if len(p) == 2:
168171
p[0] = Unit(p[1])
169-
else:
172+
elif len(p) == 3:
170173
p[0] = Unit(p[1] * p[2])
174+
elif len(p) == 4:
175+
p[0] = Unit(p[1] * p[3])
171176

172177
def p_division_product_of_units(p):
173178
'''
@@ -188,7 +193,8 @@ def p_inverse_unit(p):
188193

189194
def p_factor(p):
190195
'''
191-
factor : factor_float
196+
factor : factor_fits
197+
| factor_float
192198
| factor_int
193199
'''
194200
p[0] = p[1]
@@ -199,6 +205,8 @@ def p_factor_float(p):
199205
| signed_float UINT signed_int
200206
| signed_float UINT power numeric_power
201207
'''
208+
if cls.name == 'fits':
209+
raise ValueError("Numeric factor not supported by FITS")
202210
if len(p) == 4:
203211
p[0] = p[1] * p[2] ** float(p[3])
204212
elif len(p) == 5:
@@ -214,6 +222,8 @@ def p_factor_int(p):
214222
| UINT UINT signed_int
215223
| UINT UINT power numeric_power
216224
'''
225+
if cls.name == 'fits':
226+
raise ValueError("Numeric factor not supported by FITS")
217227
if len(p) == 2:
218228
p[0] = p[1]
219229
elif len(p) == 3:
@@ -226,6 +236,28 @@ def p_factor_int(p):
226236
elif len(p) == 5:
227237
p[0] = p[1] * p[2] ** p[4]
228238

239+
def p_factor_fits(p):
240+
'''
241+
factor_fits : UINT power OPEN_PAREN signed_int CLOSE_PAREN
242+
| UINT power signed_int
243+
| UINT SIGN UINT
244+
| UINT OPEN_PAREN signed_int CLOSE_PAREN
245+
'''
246+
if p[1] != 10:
247+
if cls.name == 'fits':
248+
raise ValueError("Base must be 10")
249+
else:
250+
return
251+
if len(p) == 4:
252+
if p[2] in ('**', '^'):
253+
p[0] = 10 ** p[3]
254+
else:
255+
p[0] = 10 ** (p[2] * p[3])
256+
elif len(p) == 5:
257+
p[0] = 10 ** p[3]
258+
elif len(p) == 6:
259+
p[0] = 10 ** p[4]
260+
229261
def p_product_of_units(p):
230262
'''
231263
product_of_units : unit_expression product product_of_units
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# generic_lextab.py. This file automatically created by PLY (version 3.4). Don't edit!
22
_tabversion = '3.4'
3-
_lextokens = {'DOUBLE_STAR': 1, 'UINT': 1, 'SOLIDUS': 1, 'OPEN_PAREN': 1, 'UNIT': 1, 'PERIOD': 1, 'STAR': 1, 'FUNCNAME': 1, 'UFLOAT': 1, 'CLOSE_PAREN': 1, 'SIGN': 1, 'CARET': 1}
3+
_lextokens = {'CARET': 1, 'SOLIDUS': 1, 'STAR': 1, 'DOUBLE_STAR': 1, 'PERIOD': 1, 'FUNCNAME': 1, 'OPEN_PAREN': 1, 'UINT': 1, 'UNIT': 1, 'SIGN': 1, 'CLOSE_PAREN': 1, 'UFLOAT': 1}
44
_lexreflags = 32
55
_lexliterals = ''
66
_lexstateinfo = {'INITIAL': 'inclusive'}
7-
_lexstatere = {'INITIAL': [("(?P<t_UFLOAT>((\\d+\\.?\\d*)|(\\.\\d+))([eE][+-]?\\d+)?)|(?P<t_UINT>\\d+)|(?P<t_SIGN>[+-](?=\\d))|(?P<t_FUNCNAME>((sqrt)|(ln)|(exp)|(log)|(mag)|(dB)|(dex))(?=\\ *\\())|(?P<t_UNIT>%|([YZEPTGMkhdcmunpfazy]?'((?!\\d)\\w)+')|((?!\\d)\\w)+)|(?P<t_DOUBLE_STAR>\\*\\*)|(?P<t_STAR>\\*)|(?P<t_OPEN_PAREN>\\()|(?P<t_CLOSE_PAREN>\\))|(?P<t_CARET>\\^)|(?P<t_PERIOD>\\.)|(?P<t_SOLIDUS>/)", [None, ('t_UFLOAT', 'UFLOAT'), None, None, None, None, ('t_UINT', 'UINT'), ('t_SIGN', 'SIGN'), ('t_FUNCNAME', 'FUNCNAME'), None, None, None, None, None, None, None, None, ('t_UNIT', 'UNIT'), None, None, None, (None, 'DOUBLE_STAR'), (None, 'STAR'), (None, 'OPEN_PAREN'), (None, 'CLOSE_PAREN'), (None, 'CARET'), (None, 'PERIOD'), (None, 'SOLIDUS')])]}
7+
_lexstatere = {'INITIAL': [("(?P<t_UFLOAT>((\\d+\\.?\\d*)|(\\.\\d+))([eE][+-]?\\d+)?)|(?P<t_UINT>\\d+)|(?P<t_SIGN>[+-](?=\\d))|(?P<t_FUNCNAME>((sqrt)|(ln)|(exp)|(log)|(mag)|(dB)|(dex))(?=\\ *\\())|(?P<t_UNIT>%|([YZEPTGMkhdcmunpfazy]?'((?!\\d)\\w)+')|((?!\\d)\\w)+)|(?P<t_DOUBLE_STAR>\\*\\*)|(?P<t_CARET>\\^)|(?P<t_OPEN_PAREN>\\()|(?P<t_CLOSE_PAREN>\\))|(?P<t_PERIOD>\\.)|(?P<t_STAR>\\*)|(?P<t_SOLIDUS>/)", [None, ('t_UFLOAT', 'UFLOAT'), None, None, None, None, ('t_UINT', 'UINT'), ('t_SIGN', 'SIGN'), ('t_FUNCNAME', 'FUNCNAME'), None, None, None, None, None, None, None, None, ('t_UNIT', 'UNIT'), None, None, None, (None, 'DOUBLE_STAR'), (None, 'CARET'), (None, 'OPEN_PAREN'), (None, 'CLOSE_PAREN'), (None, 'PERIOD'), (None, 'STAR'), (None, 'SOLIDUS')])]}
88
_lexstateignore = {'INITIAL': ' '}
99
_lexstateerrorf = {'INITIAL': 't_error'}

0 commit comments

Comments
 (0)