Reputation:
It seems that for a script that I am attempting to use I am returning an error which is a little baffling, that is, in attempting to create a new file from an old file extension it is saying that the file that it is attempting to create does not exist? Could anyone explain why this is happening and perhaps a potential fix?
Here is the script I am using:
#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")
I get the following error:
IOError: [Errno 2] No such file or directory: '/Users/.../sw00031048001uw1_sk_corrected.img'
Full traceback:
---------------------------------------------------------------------------
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
Upvotes: 0
Views: 449
Reputation: 23306
Look at the first line of your traceback:
----> 9 hdulist_sk = fits.open(filename.replace("ex","sk_corrected"))
You're trying to open a file with the "corrected" filename before that file exists. fits.open
is only for opening existing FITS files.
Upvotes: 1