Conversion of table to HDU fails depending on how column was added
See original GitHub issueWhen I add a Column
whose content is a Time
to a Table
writing the table to a FITS file fails if the data is put into a column before adding it to the table.
Code below reproduces the issue on current master (it is also present in 3.0.x):
from astropy.time import Time
from astropy.table import Table, Column
from astropy.coordinates import EarthLocation
t = Table()
t['a'] = Column(data=Time([100.0, 200.0], scale='tt', format='mjd',
location=EarthLocation(-2446354, 4237210, 4077985, unit='m')), name=['a'])
t.write('my_table.fits', overwrite=True)
# Exception traceback
If one does not create a column first the write succeeds:
from astropy.time import Time
from astropy.table import Table, Column
from astropy.coordinates import EarthLocation
t = Table()
t['a'] = Time([100.0, 200.0], scale='tt', format='mjd',
location=EarthLocation(-2446354, 4237210, 4077985, unit='m'))
t.write('my_table.fits', overwrite=True)
# Succeeds
Finally, the traceback when the exception occurs:
In [8]: from astropy.time import Time
...: from astropy.table import Table, Column
...: from astropy.coordinates import EarthLocation
...: t = Table()
...: t['a'] = Column(data=Time([100.0, 200.0], scale='tt', format='mjd',
...: location=EarthLocation(-2446354, 4237210, 4077985, unit='m')), name=['a'])
...: t.write('my_table.fits', overwrite=True)
...:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-8-23c31caf9f6c> in <module>()
5 t['a'] = Column(data=Time([100.0, 200.0], scale='tt', format='mjd',
6 location=EarthLocation(-2446354, 4237210, 4077985, unit='m')), name=['a'])
----> 7 t.write('my_table.fits', overwrite=True)
8
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/table/table.py in write(self, *args, **kwargs)
2590 serialize_method = kwargs.pop('serialize_method', None)
2591 with serialize_method_as(self, serialize_method):
-> 2592 io_registry.write(self, *args, **kwargs)
2593
2594 def copy(self, copy_data=True):
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/registry.py in write(data, format, *args, **kwargs)
558
559 writer = get_writer(format, data.__class__)
--> 560 writer(data, *args, **kwargs)
561
562
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/connect.py in write_table_fits(input, output, overwrite)
386 input = _encode_mixins(input)
387
--> 388 table_hdu = table_to_hdu(input, character_as_bytes=True)
389
390 # Check if output file already exists
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/convenience.py in table_to_hdu(table, character_as_bytes)
497 col.null = fill_value.astype(table[col.name].dtype)
498 else:
--> 499 table_hdu = BinTableHDU.from_columns(np.array(table.filled()), header=hdr, character_as_bytes=character_as_bytes)
500
501 # Set units and format display for output HDU
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/hdu/table.py in from_columns(cls, columns, header, nrows, fill, character_as_bytes, **kwargs)
123 """
124
--> 125 coldefs = cls._columns_type(columns)
126 data = FITS_rec.from_columns(coldefs, nrows=nrows, fill=fill,
127 character_as_bytes=character_as_bytes)
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/column.py in __init__(self, input, ascii)
1373 elif isinstance(input, np.ndarray) and input.dtype.fields is not None:
1374 # Construct columns from the fields of a record array
-> 1375 self._init_from_array(input)
1376 elif isiterable(input):
1377 # if the input is a list of Columns
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/column.py in _init_from_array(self, array)
1408 cname = array.dtype.names[idx]
1409 ftype = array.dtype.fields[cname][0]
-> 1410 format = self._col_format_cls.from_recformat(ftype)
1411
1412 # Determine the appropriate dimensions for items in the column
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/column.py in from_recformat(cls, recformat)
271 """Creates a column format from a Numpy record dtype format."""
272
--> 273 return cls(_convert_format(recformat, reverse=True))
274
275 @lazyproperty
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/column.py in _convert_format(format, reverse)
2398
2399 if reverse:
-> 2400 return _convert_record2fits(format)
2401 else:
2402 return _convert_fits2record(format)
~/Development/astronomy/astropy-org/astropy-bug-fixes/astropy/io/fits/column.py in _convert_record2fits(format)
2361 output_format = repeat + NUMPY2FITS[recformat]
2362 else:
-> 2363 raise ValueError('Illegal format `{}`.'.format(format))
2364
2365 return output_format
ValueError: Illegal format `object`.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 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 >Python adding a new table in fits file - Stack Overflow
I am reading a fits file with multiple tables using the astropy.table import pandas as pd from astropy.io import fits from astropy.table ......
Read more >MRDFITS - L3HarrisGeospatial.com
ASCII and BINARY tables. The data is returned as a structure with one column for each field in the table. The names of...
Read more >gammapy.utils.fits — gammapy v0.15
__init__``, i.e. if you don't pass extra options, this is equivalent to >>> hdu = fits.table_to_hdu(table) However, in this case, the column metadata...
Read more >astropy.io.fits History
BinTableHDU.from_columns to create a new binary table HDU, and the similar pyfits. ... (#160); Added initial support for table columns containing ...
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
“I really should not touch FITS” – Wise person
I’m going to close this issue as per my previous message, but if you feel that this issue should stay open, then feel free to re-open and remove the Close? label.
If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here