Writing quantity with user-defined unit to fits table breaks if unit is registered
See original GitHub issueFrom #8883: one can write a user defined unit to a fits table, and generally this works though one gets an appropriate warning:
import numpy as np
from astropy import units as u
from astropy.table import QTable
bandpass_sol_lum = u.def_unit('bandpass_sol_lum')
lum_array = np.ones((5,5)) * bandpass_sol_lum
qt = QTable()
qt['Luminosity'] = lum_array
qt.write('output_lum.fits',overwrite=True,format='fits')
# WARNING: The unit 'bandpass_sol_lum' could not be saved to FITS format [astropy.io.fits.convenience]
u.add_enabled_units(bandpass_sol_lum)
QTable.read('output_lum.fits', format='fits')
<QTable length=5>
Luminosity [5]
bandpass_sol_lum
float64
----------------
1.0 .. 1.0
1.0 .. 1.0
1.0 .. 1.0
1.0 .. 1.0
1.0 .. 1.0
So far, so good. But when one then tries to write the fits file again, it fails:
qt.write('output_lum.fits',overwrite=True,format='fits')
WARNING: The unit 'bandpass_sol_lum' could not be saved to FITS format [astropy.io.fits.convenience]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-c1bb5ecc6cf2> in <module>()
----> 1 qt.write('output_lum.fits',overwrite=True,format='fits')
/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/table/connect.py in __call__(self, *args, **kwargs)
112 instance = self._instance
113 with serialize_method_as(instance, serialize_method):
--> 114 registry.write(instance, *args, **kwargs)
/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/registry.py in write(data, format, *args, **kwargs)
564
565 writer = get_writer(format, data.__class__)
--> 566 writer(data, *args, **kwargs)
567
568
/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/fits/connect.py in write_table_fits(input, output, overwrite)
385 input = _encode_mixins(input)
386
--> 387 table_hdu = table_to_hdu(input, character_as_bytes=True)
388
389 # Check if output file already exists
/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/io/fits/convenience.py in table_to_hdu(table, character_as_bytes)
530
531 # Try creating a Unit to issue a warning if the unit is not FITS compliant
--> 532 Unit(col.unit, format='fits', parse_strict='warn')
533
534 # Column-specific override keywords for coordinate columns
/data/mhvk/venv/numpy-dev/lib/python3.7/site-packages/numpy-1.17.0.dev0+0dcd300-py3.7-linux-x86_64.egg/astropy/units/core.py in __call__(self, s, represents, format, namespace, doc, parse_strict)
1843
1844 elif s is None:
-> 1845 raise TypeError("None is not a valid Unit")
1846
1847 else:
TypeError: None is not a valid Unit
Anyone any ideas? (cc @taldcroft, @astrofrog, @MSeifert04)
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Table Data — Astropy v5.2
The data in a FITS table HDU is basically a record array with added attributes. The metadata (i.e., information about the table data)...
Read more >New FITS table column attributes break existing code #7145
So, sad to say, but the best solution I see is to fix the pipeline, using the column attributes (something like fits.Column(name="RAWX", format= ......
Read more >TOPCAT - Tool for OPerations on Catalogues And Tables
TOPCAT is an interactive graphical program which can examine, analyse, combine, edit and write out tables. A table is, roughly, something with ...
Read more >Updating, Editing and Deleting Indexes and Tables of Contents
Place the cursor in the index or table of contents. If you cannot place your cursor in the index or table of contents,...
Read more >Excel IF AND OR Functions Explained - My Online Training Hub
Must know Excel formula writing tips, tricks and tools to make you an ... 4 $44.05 (base cost of $40.00 + 4.05 for...
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
Possible fix for the fits case in #9015
Yes, tried that: it breaks the same way. Looking at the traceback, somehow the column looses its unit altogether, which is very weird. But I haven’t had time to look at the actual code.