简体   繁体   中英

PyFITS: File already exists

I'm really close to completing a large code, but the final segment of it seems to be failing and I don't know why. What I'm trying to do here is take an image-array, compare it to a different image array, and wherever the initial image array equals 1, I want to mask that portion out in the second image array. However, I'm getting a strange error:

Code:

maskimg='omask'+str(inimgs)[5:16]+'.fits'
newmaskimg=pf.getdata(maskimg)
oimg=pf.getdata(inimgs)
for i in range (newmaskimg.shape[0]):
    for j in range (newmaskimg.shape[1]):
        if newmaskimg[i,j]==1:
            oimg[i,j]=0
pf.writeto('newestmask'+str(inimgs)[5:16]+'.fits',newmaskimg)

Error:

/home/vidur/se_files/fetch_swarp10.py in objmask(inimgs, inwhts, thresh1, thresh2, tfdel, xceng, yceng, outdir, tmpdir)
    122             if newmaskimg[i,j]==1:
    123                 oimg[i,j]=0
--> 124     pf.writeto('newestmask'+str(inimgs)[5:16]+'.fits',newmaskimg)
    125 
    126 

/usr/local/lib/python2.7/dist-packages/pyfits/convenience.pyc in writeto(filename, data, header, output_verify, clobber, checksum)
    396         hdu = PrimaryHDU(data, header=header)
    397     hdu.writeto(filename, clobber=clobber, output_verify=output_verify,
--> 398                 checksum=checksum)
    399 
    400 

/usr/local/lib/python2.7/dist-packages/pyfits/hdu/base.pyc in writeto(self, name, output_verify, clobber, checksum)
    348         hdulist = HDUList([self])
    349         hdulist.writeto(name, output_verify, clobber=clobber,
--> 350                         checksum=checksum)
    351 
    352     def _get_raw_data(self, shape, code, offset):

/usr/local/lib/python2.7/dist-packages/pyfits/hdu/hdulist.pyc in writeto(self, fileobj, output_verify, clobber, checksum)
    651                     os.remove(filename)
    652                 else:
--> 653                     raise IOError("File '%s' already exists." % filename)
    654         elif (hasattr(fileobj, 'len') and fileobj.len > 0):
    655             if clobber:

IOError: File 'newestmaskPHOTOf105w0.fits' already exists.

If you don't care about overwriting the existing file, pyfits.writeto accepts a clobber argument to automatically overwrite existing files (it will still output a warning):

pyfits.writeto(..., clobber=True)

As an aside, let me be very emphatic that the code you posted above is very much not the right way to use Numpy. The loop in your code can be written in one line and will be orders of magnitude faster. For example, one of many possibilities is to write it like this:

oimg[newmaskimg == 1] = 0

Yes, add clobber = True . I've used this in my codes before and it works just fine. Or, simply go and sudo rm path/to/file and get rid of them so you can run it again.

I had the same issue and as it turns out using the argument clobber still works but won't be supported in future versions of AstroPy.

The argument overwrite does the same thing and doesn't put out an error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM