[AstroPy] Convert FITS header into astropy Table entries?

Jens Melinder jens at astro.su.se
Wed Aug 19 10:59:59 EDT 2015

>> If you don’t need the HISTORY and COMMENT fields a simple fix maybe something
>> like this:
>> keys_cl=[]
>> vals_cl=[]
>> for ii in range(len(h0.keys())):
>> if (h0.keys()[ii] != ’’)&(h0.keys()[ii] != ’HISTORY’)&(h0.keys()[ii] != ’COMMENT’):
>> keys_cl.append(h0.keys()[ii])
>> vals_cl.append(h0.values()[ii])
>> tab = Table(rows=[vals_cl],names=keys_cl)
> This example has a couple issues--for one header.keys() returns a generator so 
> you can't just index it directly.  
Ah yes of course, I was trying it out with lists generated from the h0.keys() method. Thanks for pointing it out.

> I think a simpler approach is:
>>>> unique_keys = sorted(set(header))
>>>> t = Table(rows=[[header[k] for k in unique_keys]], names=unique_keys)
> If you wanted to exclude commentary keywords like '', 'COMMENT', and 'HISTORY' 
> you might instead do:
>>>> unique_keys = sorted(key for key in header
> ...                      if key not in ('', 'COMMENT', 'HISTORY'))
> This all also assumed that all the headers you're processing have the exact same 
> set of keywords in them.  If that weren't the case you might have do take a 
> subtly different approach to determining what columns your table should have 
> (for example by looping over all headers first and adding their header keywords 
> to a set of all known keywords).

Much simpler indeed.


Jens Melinder
Department of Astronomy
Stockholm University
jens at astro.su.se
+46 706471856

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20150819/290a8536/attachment.html>

More information about the AstroPy mailing list