[AstroPy] IOError: [Errno 2] No such file or directory when using satrapy fits moduel in IPython environment

Evert Rol evert.rol at gmail.com
Sun Jan 17 06:03:48 EST 2016


Michael, if I understand your problem correctly, then you shouldn't attempt to open the ...sk_corrected file (for reading). That's what you're doing now, hence Python can't find the file.

Instead, create a HDUList (or simply a PrimaryHDU, store the corrected data in that HDUList (as you do in your loop), and then use the .writeto() method as you do at the bottom, with the sk_corrected filename.
In fact, you seem to already do this, in the two lines below the faulty line.

There is no need to try and create a new FITS file first: HDUList.writeto can do that all in one go.

It's a bit unclear to me why you want an ...sk_corrected file, and then at the bottom you write three other (...new.img) files, which seem to be the actually NaN corrected files.
Perhaps you don't really need the ...sk_corrected file?

Judging from the loop, perhaps you're trying to make copies of the original file, so that they are guaranteed to have the same amount of HDUs?
Instead, you can iterate over the original HDUList and append the corrected HDUs. 
That would give something like:

with fits.open(filename) as hdulist:
  hdulist_sk = fits.HDUList([fits.PrimaryHDU(hdulist[0].header)])
  for hdu in hdulist[1:]:
    mask = np.isnan(hdu.data)
    tmphdu = fits.ImageHDU()
    replace_pix(hdu, mask, tmphdu)
    hdulist_sk.append(tmphdu.copy())
hdulist_sk.writeto(filename.replace("ex", "sk_corrected")

I'm taking a few shortcuts above, using the with statement and iterating directly over the hdulist.
Note if you let the function replace_pix() create its own, new, corrected, HDU, and let it return it, that line and the two lines around it can become:
   hdulist_sk.append(replace_pix(hdu, mask))
Now, you'll need the .copy() method.

But do check if the above code is what you're trying to achieve.

Cheers,

  Evert


> 
> Dear Astropy community,
> 
> I'm having a little problem with a script that I am using. The parts of the script which is giving me the problems are as follows:
> 
> #Function to replace all NaNs in the exposure map by 0s and to replace the corresponding pixels in the sky and large scale sensitivity map by 0s.
> def replace_nan(filename):
> 
>     
> #Print that all NaNs will be replaced by 0s in the exposure map and that the corresponding pixels in the sky and large scale sensitivity map will also be replaced by 0s.
> 
>     
> print "All NaNs will be replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map will also be replaced by 0s."
> 
>     
> #Open the exposure map, the corresponding sky and large scale sensitivity map and copy the primary headers (extension 0 of hdulist) to new hdulists.
> 
>     hdulist_ex 
> = fits.open(filename)
> 
>     new_hdu_header_ex 
> = fits.PrimaryHDU(header=hdulist_ex[0].header)
> 
>     new_hdulist_ex 
> = fits.HDUList([new_hdu_header_ex])
> 
>     hdulist_sk 
> = fits.open(filename.replace("ex","sk_corrected"))
> 
>     new_hdu_header_sk 
> = fits.PrimaryHDU(header=hdulist_sk[0].header)
> 
>     new_hdulist_sk 
> = fits.HDUList([new_hdu_header_sk])
> 
>     hdulist_lss 
> = fits.open(filename.replace("ex","lss_m"))
> 
>     new_hdu_header_lss 
> = fits.PrimaryHDU(header=hdulist_lss[0].header)
> 
>     new_hdulist_lss 
> = fits.HDUList([new_hdu_header_lss])
> 
> 
>     
> #For all frames in the image: Create the mask and run the function replace_pix.
> 
>     
> for i in range(1,len(hdulist_ex)):
> 
>         mask 
> = np.isnan(hdulist_ex[i].data)
> 
>         replace_pix
> (hdulist_ex[i],mask,new_hdulist_ex)
> 
>         replace_pix
> (hdulist_sk[i],mask,new_hdulist_sk)
> 
>         replace_pix
> (hdulist_lss[i],mask,new_hdulist_lss)
> 
> 
>     
> #Write the new hdulists to new images.
> 
>     new_hdulist_ex
> .writeto(filename.replace(".img","_new.img"))
> 
>     new_hdulist_sk
> .writeto(filename.replace("ex.img","sk_new.img"))
> 
>     new_hdulist_lss
> .writeto(filename.replace("ex.img","lss_new.img"))
> 
> 
>     
> #Print that all NaNs are replaced by 0s in the exposure map and that the corresponding pixels in the sky and large scale sensitivity map are also replaced by 0s.
> 
>     
> print "All NaNs are replaced by 0s in " + filename + " and the corresponding pixels in the sky and large scale sensitivity map are also replaced by 0s."
> 
> When running:
> 
> replace_nan("/Users/.../sw00031048001uw1_ex.img")
> (where I have dotted out my path for convenience.) it is failing on (traceback) is hdulist_sk = fits.open(filename.replace("ex","sk_corrected")) 
> 
> The error is simply 
> 
> IOError: [Errno 2] No such file or directory: '/Users/.../sw00031048001uw1_sk_corrected.img'
> But this is the file I am attempting to create by replacing '/Users/.../sw00031048001uw1_ex.img'
> 
> I'm within the iPython development environment (if that helps, or if that is relevant). I'm guessing at this stage that maybe I don't have permissions to be messing around with files from the iPython console? Or I need some extra arguments for this to work...
> 
> Any suggestions would be warmly welcomed.
> 
> Many thanks,
> 
> Michael Roberts
> 
> 
> 
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at scipy.org
> https://mail.scipy.org/mailman/listinfo/astropy




More information about the AstroPy mailing list