Python and the fits module (operating system interfaces): "IOError: [Errno 2] No such file or directory"
Python and the fits module (operating system interfaces): "IOError: [Errno 2] No such file or directory"
似乎对于我尝试使用的脚本,我返回了一个有点莫名其妙的错误,也就是说,在尝试从旧文件扩展名创建新文件时,它说该文件它试图创建不存在?任何人都可以解释为什么会发生这种情况以及可能的解决方法吗?
这是我正在使用的脚本:
#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
查看回溯的第一行:
----> 9 hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
您正在尝试打开文件名 "corrected" 之前的文件。 fits.open
仅用于打开现有的 FITS 文件。
似乎对于我尝试使用的脚本,我返回了一个有点莫名其妙的错误,也就是说,在尝试从旧文件扩展名创建新文件时,它说该文件它试图创建不存在?任何人都可以解释为什么会发生这种情况以及可能的解决方法吗?
这是我正在使用的脚本:
#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
查看回溯的第一行:
----> 9 hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
您正在尝试打开文件名 "corrected" 之前的文件。 fits.open
仅用于打开现有的 FITS 文件。