Skip to content

Instantly share code, notes, and snippets.

@astrofrog
Created February 1, 2011 17:00
Show Gist options
  • Select an option

  • Save astrofrog/806152 to your computer and use it in GitHub Desktop.

Select an option

Save astrofrog/806152 to your computer and use it in GitHub Desktop.
Create an ATpy table in IRAF hselect style
# Create an ATpy table in IRAF hselect style
# Example:
#
# >>> files = glob.glob(os.path.join('.', '*.fits'))
# >>> t = hselect(files)
import glob
import os
import pyfits
import atpy
from atpy.odict import odict
def hselect(files):
# Initialize column holder
cols = odict()
# Loop over files
for i, f in enumerate(files):
# Read in header
header = pyfits.getheader(f)
# Add values
for key in header:
if key.strip() != "":
if key not in cols:
cols[key] = []
cols[key].append(header[key])
# Create ATpy table
t = atpy.Table()
for col in cols:
t.add_column(col, cols[col])
return t
@phn
Copy link
Copy Markdown

phn commented Feb 2, 2011

I don't know if this make a huge difference, but can't we avoid the if i == 0 condition using

cols.setdefault(key, []).append(header[key])

in the second iteration over header?

@astrofrog
Copy link
Copy Markdown
Author

Thanks for the suggestion! However, this would not work here at the moment, as the setdefault method isn't implemented in the odict() class.

@phn
Copy link
Copy Markdown

phn commented Feb 8, 2011

Hello,

I thought that odict() was a subclass of dict. I looked at the code only now and see that it is not.

But one could still do something like:

for key in header:
    if key.strip() != "":
        if key not in cols:
            cols[key] = []
        cols[key].append(header[key])

I am sure that you must have thought of these; I am just saying it loud!

@astrofrog
Copy link
Copy Markdown
Author

I updated the code with your suggestion :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment