Cannot include Quantities in FITS headers
See original GitHub issueIt looks like one cannot put an astropy Quantity into a FITS header (astropy version=3.1.2
):
Edit: same on 4.0.dev25972
import astropy.units as u
from astropy.io import fits
temperature = 10*u.deg_C
header = fits.Header()
header['CCDTEMP'] = temperature
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-3ce5059bc40e> in <module>
----> 1 header['CCDTEMP'] = temperature
~/lsst/lsstsw/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/io/fits/header.py in __setitem__(self, key, value)
187 # If we get an IndexError that should be raised; we don't allow
188 # assignment to non-existing indices
--> 189 self._update((key, value, comment))
190
191 def __delitem__(self, key):
~/lsst/lsstsw/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/io/fits/header.py in _update(self, card)
1607 else:
1608 # A new keyword! self.append() will handle updating _modified
-> 1609 self.append(card)
1610
1611 def _cardindex(self, key):
~/lsst/lsstsw/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/io/fits/header.py in append(self, card, useblanks, bottom, end)
1139 card = Card(card)
1140 elif isinstance(card, tuple):
-> 1141 card = Card(*card)
1142 elif card is None:
1143 card = Card()
~/lsst/lsstsw/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/io/fits/card.py in __init__(self, keyword, value, comment, **kwargs)
189 self.keyword = keyword
190 if value is not None:
--> 191 self.value = value
192
193 if comment is not None:
~/lsst/lsstsw/miniconda/envs/lsst-scipipe/lib/python3.7/site-packages/astropy/io/fits/card.py in value(self, value)
309 np.floating, np.integer, np.complexfloating,
310 np.bool_)):
--> 311 raise ValueError('Illegal value: {!r}.'.format(value))
312
313 if isinstance(value, float) and (np.isnan(value) or np.isinf(value)):
ValueError: Illegal value: <Quantity 10. deg_C>.
I would have expected to be able to insert a Quantity into the header and have the units round-trip. The FITS standard document suggests putting units in the comment field for that header key (see section 4.3.2 of the FITS Standard document); since astropy Quantities have well defined stringifications, we could just use those?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Batch edit fits headers | Page 3 | PixInsight Forum
The FITS value format standard is ambiguous here: "1" could be integer or float, but "1." is definitely float. While it is not...
Read more >FITS File Handling (astropy.io.fits)
For those unfamiliar with FITS headers, they consist of a list of 80 byte “cards”, where a card contains a keyword, a value,...
Read more >FITS File Headers
The main headers are parsed into sections, and all science data files (RAW, CAL, IDF, ALL, ANO, and NVO) contain the same sections....
Read more >Copying fits-file data and/or header into a new fits-file
... Files contain different numbers of HDUs: a: 3 b: 2 Primary HDU: Headers contain differences: Headers have different number of cards: a:...
Read more >FITS File handling (astropy.io.fits)
Working with FITS Headers¶ ... As mentioned earlier, each element of an HDUList is an HDU object with .header and .data attributes, which...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Something like:
(modulo me getting the spacing right and remembering how FITS was Celsius to be written). The unit should be inserted at the start of any existing comment (overwriting unit if one is already defined). Parsing the units out on when extracting values from a header would also be wonderful.
@pllim : I don’t think so, no. #9197 is about writing to a FITS BinTableHDU (which is already partly supported via mixins). This one is about writing to the FITS headers.