0

私が使用しようとしているスクリプトでは、少し不可解なエラーが返されているようです。つまり、古いファイル拡張子から新しいファイルを作成しようとすると、作成しようとしているファイルが存在しません?なぜこれが起こっているのか、そしておそらく修正の可能性を説明できる人はいますか?

私が使用しているスクリプトは次のとおりです。

#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."

実行時:

replace_nan("/Users/.../sw00031048001uw1_ex.img")

次のエラーが表示されます。

IOError: [Errno 2] No such file or directory: '/Users/.../sw00031048001uw1_sk_corrected.img'

完全なトレースバック:

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-10-af5110b477a1> in <module>()
----> 1 replace_nan('/Users/UCL_Astronomy/Documents/UCL/PHASG199/M33_SWIFT_corrected_usnob1_spec/sw00031048001uw1_ex.img')

<ipython-input-8-ca837d8e11f7> in replace_nan(filename)
      7     new_hdu_header_ex = fits.PrimaryHDU(header=hdulist_ex[0].header)
      8     new_hdulist_ex = fits.HDUList([new_hdu_header_ex])
----> 9     hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
     10     new_hdu_header_sk = fits.PrimaryHDU(header=hdulist_sk[0].header)
     11     new_hdulist_sk = fits.HDUList([new_hdu_header_sk])

//anaconda/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in fitsopen(name, mode, memmap, save_backup, cache, **kwargs)
    127         raise ValueError('Empty filename: %s' % repr(name))
    128 
--> 129     return HDUList.fromfile(name, mode, memmap, save_backup, cache, **kwargs)
    130 
    131 

//anaconda/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in fromfile(cls, fileobj, mode, memmap, save_backup, cache, **kwargs)
    269 
    270         return cls._readfrom(fileobj=fileobj, mode=mode, memmap=memmap,
--> 271                              save_backup=save_backup, cache=cache, **kwargs)
    272 
    273     @classmethod

//anaconda/lib/python2.7/site-packages/astropy/io/fits/hdu/hdulist.pyc in _readfrom(cls, fileobj, data, mode, memmap, save_backup, cache, **kwargs)
    790             if not isinstance(fileobj, _File):
    791                 # instantiate a FITS file object (ffo)
--> 792                 ffo = _File(fileobj, mode=mode, memmap=memmap, cache=cache)
    793             else:
    794                 ffo = fileobj

//anaconda/lib/python2.7/site-packages/astropy/io/fits/file.pyc in __init__(self, fileobj, mode, memmap, clobber, cache)
    135             self._open_fileobj(fileobj, mode, clobber)
    136         elif isinstance(fileobj, string_types):
--> 137             self._open_filename(fileobj, mode, clobber)
    138         else:
    139             self._open_filelike(fileobj, mode, clobber)

//anaconda/lib/python2.7/site-packages/astropy/io/fits/file.pyc in _open_filename(self, filename, mode, clobber)
    438             self._open_zipfile(self.name, mode)
    439         else:
--> 440             self._file = fileobj_open(self.name, PYFITS_MODES[mode])
    441             # Make certain we're back at the beginning of the file
    442         self._file.seek(0)

//anaconda/lib/python2.7/site-packages/astropy/io/fits/util.pyc in fileobj_open(filename, mode)
    412         """
    413 
--> 414         return open(filename, mode)
    415 
    416 
4

1 に答える 1

1

トレースバックの最初の行を見てください:

----> 9     hdulist_sk =     fits.open(filename.replace("ex","sk_corrected"))

そのファイルが存在する前に、「修正された」ファイル名でファイルを開こうとしています。fits.open既存の FITS ファイルを開くためだけのものです。

于 2016-01-17T14:41:55.130 に答える