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

Roberts, Michael michael.roberts.15 at ucl.ac.uk
Sun Jan 17 05:43:52 EST 2016


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




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20160117/f7852ad0/attachment.html>


More information about the AstroPy mailing list