-1

I have an array of filenames, and I want to open each file with pyfits. So my code should be something like:

import pyfits
files=array(["1131lc.fits+2","1132lc.fits+2","1134lc.fits+2","1136lc.fits+2","1137lc.fits+2","1138lc.fits+2"])
for file in files:
    data = pyfits.open(files)

print data
print len(data)

But in this way, the last two commands, print out: "[]" and "0". Where is the problem?

Py-ser
  • 1,860
  • 9
  • 32
  • 58
  • Have you checked that the last file is correct and not empty? Just incase its not intentional, you are assigning the data var to the last entry in the array (data =) , should it perhaps be data +=? – Steve Jul 05 '13 at 12:25
  • Does it work now with my solution? – usethedeathstar Jul 08 '13 at 06:35
  • getheader only returns the header, whereas I want the specific value of a keyword. I added an answer of mine, that was the best solution in my case. Thank you anyway. – Py-ser Jul 08 '13 at 14:26

3 Answers3

2

change

data=pyfits.open(files)

to

data=pyfits.getdata(file)

that should work

edit: if you want your printstatements to work too, you should do:

data=[]
for file in files:
    data.append(pyfits.getdata(file))
print len(data)

and than to get each bit of data separately, just get data[i], to get the i-th item in the list,

edit2: Are you certain that you execute it in the right path? (that your files are not stored somewhere in a different path? - not sure if fits+2 is a correct extension either)

Edit3: if you get an IOError, it probably is related to your filenames or so, fits+2 ? if your .py file with your code you wrote is not in the same directory as your data, you need the full path so that it can find the file

usethedeathstar
  • 2,219
  • 1
  • 19
  • 30
  • since i still need more rep to be able to comment on NADH solution: you do not store the data somewhere, you overwrite the variable "data" in your for loop each time, – usethedeathstar Jul 05 '13 at 12:33
  • Did you check the filenames and the total path? because if those fitsfiles are not in the same path as your .py file, it wont work, with that IOError – usethedeathstar Jul 05 '13 at 12:51
  • The path is correct. If I remove the "+2" from the filenames, I obtain an output that I cannot understand: `[ ('BINTABLE', 'ISGR-SRC.-LCR', 1, 3, '', '', 'J130117.2-613607', 'GX 304-1', 195.32167, -61.601944, 18.0, 80.0)] 1` – Py-ser Jul 05 '13 at 13:12
  • I guess that has to do with what is inside your fitsfile, ('J130117.2-613607'i know is some coordinate of astronomy objects, so i assume that is header-info you got there.) The thing you expect to see, what dimensions should it have? – usethedeathstar Jul 05 '13 at 13:23
  • http://simbad.u-strasbg.fr/simbad/sim-id?Ident=GX+304-1 you are looking at a high-mass xray binary ;-) you should know the J-number and the GX tag,... this is some header info which is stored in the file, If your value you need to know is a value (magnitude or so), it might be in there, so that definately means you are in the file now, finding info – usethedeathstar Jul 05 '13 at 13:27
  • Yes, but I need to surf the info in the header of a defined unit, not in the table. That is what I meant by "I cannot understand the output". I am in the file, but not where I am supposed/want to be. – Py-ser Jul 05 '13 at 13:42
  • What does it give if you try pyfits.getheader(file)? and if you do pyfits.info(file) ?? – usethedeathstar Jul 05 '13 at 14:11
  • did you check if the fits+2 files exist? because i found out a little more about that stuff you are doing (lc stands for lightcurve ;-)), and i find mention of .fits+1 somewhere too, and i assume you are working on a large database with many many fitsfiles, so maybe the .fits exists, and the .fits+2 exists too, (and in that case, i suggest checking what pyfits.info and pyfits.getheader give you) Also interesting to know would be how large the fitsfiles are, to know if its not a mistake of how the data were put into the fitsfile – usethedeathstar Jul 05 '13 at 14:27
1

Your files array was an array within an array. Also, you were trying passing the wrong parameter to open() (it should be getdata() either way).

import pyfits
files=["1131lc.fits+2","1132lc.fits+2","1134lc.fits+2","1136lc.fits+2","1137lc.fits+2","1138lc.fits+2"]
for file in files:
    data = pyfits.getdata(file)

    print data
    print len(data)
Nadh
  • 6,987
  • 2
  • 21
  • 21
  • Unfortunately, it does not work: `Traceback (most recent call last): File "plot_pp.py", line 42, in data = pyfits.getdata(file) ... IOError: [Errno 2] No such file or directory: '1131lc.fits+2'` – Py-ser Jul 05 '13 at 12:37
0

The first thing is that pyfits does not like to specify the unit of the fits file with the format: "+2". The unit can be specified in a different way, according to the data we need to retrieve. In my case, the right thig was:

data=[]
for file in files
    data.append(pyfits.getval(file, 'TSTART', 2))

where files is an array where the name of the fits files are specified (again, without the "+2"). In this way, the data array will store the six values of the 'TSTART' keyword in the header of the second unit of each file. I hope this is useful.

Py-ser
  • 1,860
  • 9
  • 32
  • 58
  • Filename suffixes like "+2" are particular to IRAF and are not used in most Python software I'm aware of (though some packages like multidrizzle that do provide IRAF-like interfaces have this, but it's not something you'll find in general. – Iguananaut Feb 16 '15 at 22:38