[AstroPy] AstroPy Digest: HDU to Table

Derek Homeier derek at astro.physik.uni-goettingen.de
Thu Apr 18 08:35:11 EDT 2019


Hi Rick,
> 
> I agree - this is the way to go for now.  THAT thin a wrapper for a single HDU->Table conversion would be elegant but is otherwise unnecessary.  However,  mutiple table in a multiple HDU list require more work and it would be nice to have a wrapper for that.  It would be good to have this mentioned in the docs, something like
> 
> 	----------------------------------------------------------------------------------------------------------------------------------------
> 	The equivalent reverse conversion for complex HDUList's (multiple tables in one FITS binary table file)
> 
> 		tables = hdulist_to_tables (hdul)
> 
> 	is easily achieved by explicitly passing both the data and the metadata to Table():
> 
for the docs it would probably be best to recommend the “trick” using Table.read() below.
> 
> OK, so I wrote the test script
> 
> 	http://www.astro.physik.uni-goettingen.de/~hessman/misc/HDUList_to_Table_test.py
> 
> which creates different multiple table HDULists from scratch, writes them to files (for a Table.read({filename}) comparison), and reads the last table into a Table using
> 
>    t = Table.read (hdul,hdu=n)
> 
> (hdul is the HDUList, n is the total number of HDU's; hdu=0 is the PrimaryHDU) and this appears to work: the correct table is converted.  However, I get 
> 
> 	FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
>  from ._conv import register_converters as _register_converters
> 
> the first time and
> 
Hmm,  don’t see that FutureWarning with Astropy 3.1.2 + Numpy 1.14.5 - in fact I could not find that
code line anywhere in the astropy or numpy tree.

> 	WARNING: hdu= was not specified but multiple tables are present, reading in first available table (hdu=1) [astropy.io.fits.connect]
> 
> for n > 2 (i.e. more than one table), even though "hdu=" _was_ specified and the correct HDU is converted; the "first available table" is NOT read.    A small bug in the irrelevant warning.
> 
But the ‘hdu= was not specified’ warning (correctly) follows the reading from file, and that is is opening the
first extension - looks all correct to me. I tested with all extensions in hdul (extending your last lines a bit):
...
	t = Table.read ('test.fits',format='fits')
	t.info
	print ('  * y-sum of table:',np.sum(t['y']))

	print (' * Creating a table from default (first) table in HDUList …')
	t = Table.read (hdul)
	t.info
	print ('  * y-sum of table:',np.sum(t['y']))

	print ('  * Creating a table from the each table in HDUList using hdu= argument ...')
	for i in range(1, n+1) :
		t = Table.read (hdul,hdu=i)
		t.info
		print ('  * HDU {:d} - {:s}: y-sum of table: {:f}'.format(i, t.meta['OBJNAME'], np.sum(t['y'])))

…
  * Creating a table from the file...
WARNING: hdu= was not specified but multiple tables are present, reading in first available table (hdu=1) [astropy.io.fits.connect]
  * y-sum of table: 55.0
  * Creating a table from default (first) table in HDUList ...
  * y-sum of table: 55.0
  * Creating a table from the each table in HDUList using hdu= argument ...
  * HDU 1 - data_0: y-sum of table: 55.000000
  * HDU 2 - data_1: y-sum of table: 550.000000

* Test using 3 HDUs...
  * y-sum of table: 55.0
  * y-sum of table: 550.0
  * y-sum of table: 5500.0
  * Storing in file...
  * Creating a table from the file...
  * y-sum of table: 55.0
  * Creating a table from default (first) table in HDUList ...
  * y-sum of table: 55.0
  * Creating a table from the each table in HDUList using hdu= argument ...
  * HDU 1 - data_0: y-sum of table: 55.000000
  * HDU 2 - data_1: y-sum of table: 550.000000
  * HDU 3 - data_2: y-sum of table: 5500.000000

Creating lists of table is thus straightforward; and as `table_to_hdu()` does not provide any more
specific functionality here either, I rather don’t think it needs to be further discussed in the docs.
At a minimum probably a paragraph should be added to the end of the `Reading` section noting
that in addition to the file-like objects mentioned in http://docs.astropy.org/en/stable/io/unified.html
Table.read() accepts an HDU or HDUList object as input “file” (with the ‘hdu=‘ kwarg functioning
accordingly in the latter case).

Cheers,
					Derek



More information about the AstroPy mailing list