Created
February 1, 2011 17:00
-
-
Save astrofrog/806152 to your computer and use it in GitHub Desktop.
Create an ATpy table in IRAF hselect style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
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.
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!
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
I don't know if this make a huge difference, but can't we avoid the
if i == 0condition usingin the second iteration over
header?