Problem with reading multi extension .fits files

Viewed 83

I am trying to read/open some multi-extension .fits files. But I have a problem opening them. Here is the part of the cod I am using to open .fits files located in the same folder:

imgs = sorted(glob.glob('location_of_the_files/*.fits'))


for location in imgs:   
    hdul = fits.open(imgs) 
    original = hdul[1].data
    model = hdul[2].data
    residual = hdul[3].data

When running this I am getting this:

OSError: File-like object does not have a 'write' method, required for mode 'ostream'.

I try to check on the internet, but I do not understand what is going on. Any help on how to solve this?

Maybe it is important to mention when trying to open single .fits file with this code everything works without any problem:

hdul = fits.open("location_of_the_files/image_data.fits")

original = hdul[1].data 
model = hdul[2].data
residual = hdul[3].data

If needed let me know and I can upload .fits files (in that case, please just tell me how to do this here).

Thanks.

1 Answers

I suppose that you were trying to do something like this:

for location in imgs:   
    with fits.open(location) as hdul:
       original = hdul[1].data
       model = hdul[2].data
       residual = hdul[3].data
   ...

Note the location instead of imgs as an argument of open method.

Related