-1

Once a beginner, always a beginner! I'm using python 2.7.5, OSX 10.8

You may have the solution to my question even if you don't know pyfits, since I believe it's a problem in my algorithm! I use the following code

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pyfits
from PySide import QtGui, QtCore
import os
import fonctions
print('\n\n')

directory = raw_input("file path : ")

hdulist=pyfits.open(directory)
print('\n\n')
print('--------------------------------fits informations :')
hdulist.info()
print ('\n\n')


print("\n")
j=0 #PyFITS is using zero-based indexing when referring to HDUs
k=0  
while True: #runs through all the HDUs
    try:    
        hdulist[j].header[k] is None
    except IndexError:  #errors handling when coming to the last HDU
        print("--------------------------------No more HDU! \n\n\n\n\n\n")
        break

    while True: #runs through all the headers
        try:
            hdulist[j].header[k] is None
        except IndexError:  #errors handling when coming to the last header
            i=0
            break
        header = hdulist[j].header[k]
        print (hdulist[j].header.ascardlist())
        k=k+1
    j=j+1

It "works" since it displays hdulist[j].header.ascardlist(), but it prints it k times, before going to the next HDU... Any advice?

Geo
  • 119
  • 1
  • 10
  • Downvoted as this doesn't really explain what the code is trying to do, and the issue in question is too specific to this code. – Iguananaut Nov 30 '13 at 04:26

3 Answers3

1

I have never work with Pyfits, but I had a look at the documentation. I would say this loop construct, should be more suitable

for hdu in hdulist:
    for hdu_header in hdu.header.itervalues():
        print( hdu_header.ascardlist() )

Hopefully i could help you.

  • This code wouldn't work: `hdu.header.itervalues()` just returns an iterator over keyword values in the header. – Iguananaut Nov 30 '13 at 04:22
0

I am convinced the assignment k=0 needs to go inside the outer 'while True' loop because it needs to be reset for each new j.

0

You didn't really explain what this is supposed to do, but it looks like you're just trying to display all the headers in an HDU. None of the while statements or try/excepts are necessary. This should suffice:

with pyfits.open(filename) as hdulist:
    for hdu in hdulist:
        print hdu.header
Iguananaut
  • 21,810
  • 5
  • 50
  • 63