Skip to content

Commit

Permalink
Merge pull request #20 from mcara/fix-deprecated-clobber
Browse files Browse the repository at this point in the history
Fix deprecated 'clobber' parameter in astropy.io.fits write functions
  • Loading branch information
pllim committed Feb 3, 2017
2 parents c44119e + 1bdde24 commit c3bbd95
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 43 deletions.
7 changes: 6 additions & 1 deletion lib/stsci/tools/asnutil.py
Expand Up @@ -14,7 +14,9 @@
from astropy.io import fits
import numpy as N
import os.path, time
from distutils.version import LooseVersion

ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

__version__ = '0.2(2015-06-23)'

Expand Down Expand Up @@ -411,7 +413,10 @@ def write(self, output=None):
cols = fits.ColDefs([memname,memtype,memprsn,xoffset,yoffset,xdelta,ydelta,rotation,scale])
hdu = fits.BinTableHDU.from_columns(cols)
fasn.append(hdu)
fasn.writeto(outfile, clobber=True)
if ASTROPY_VER_GE13:
fasn.writeto(outfile, overwrite=True)
else:
fasn.writeto(outfile, clobber=True)
fasn.close()
mem0 = self['order'][0]
refimg = self['members'][mem0]['refimage']
Expand Down
39 changes: 19 additions & 20 deletions lib/stsci/tools/convertlog.py
Expand Up @@ -7,21 +7,21 @@
Usage:
convertlog.py [OPTIONS] trailer_filename
:Options:
-h print the help (this text)
-v print version of task
-w
-w
--width Width (in chars) for trailer file table column
-o
--output Name of output FITS trailer file
If none is specified, it will convert input file
from "rootname.tra" to "rootname_trl.fits"
:Example:
If used in Pythons script, a user can, e. g.::
Expand Down Expand Up @@ -53,35 +53,35 @@
def convert(input, width=132, output=None, keep=False):

"""Input ASCII trailer file "input" will be read.
The contents will then be written out to a FITS file in the same format
as used by 'stwfits' from IRAF.
Parameters
===========
input : str
Filename of input ASCII trailer file
width : int
Number of characters wide to use for defining output FITS column
[Default: 132]
output : str
Filename to use for writing out converted FITS trailer file
If None, input filename will be converted from *.tra -> *_trl.fits
[Default: None]
keep : bool
Specifies whether or not to keep any previously written FITS files
[Default: False]
"""
# open input trailer file
trl = open(input)

# process all lines
lines = np.array([i for text in trl.readlines() for i in textwrap.wrap(text,width=width)])

# close ASCII trailer file now that we have processed all the lines
trl.close()

Expand All @@ -101,7 +101,7 @@ def convert(input, width=132, output=None, keep=False):
raise IOError
else:
os.remove(full_name)

# Build FITS table and write it out
line_fmt = "{}A".format(width)
tbhdu = fits.BinTableHDU.from_columns([fits.Column(name='TEXT_FILE',format=line_fmt,array=lines)])
Expand All @@ -110,13 +110,13 @@ def convert(input, width=132, output=None, keep=False):
print("Created output FITS filename for trailer:{} {}".format(os.linesep,full_name))

os.remove(input)

def usage():
print(__doc__)

def main():
import getopt

try:
optlist, args = getopt.getopt(sys.argv[1:], 'hvkw:o:')
except getopt.error as e:
Expand All @@ -127,9 +127,8 @@ def main():

output = None
width = 132
clobber=False
keep = False

for o, a in optlist:
if o in ("-h", "--help"):
usage()
Expand All @@ -151,7 +150,7 @@ def main():
except:
print("ERROR: Convertlog failed to convert: {}".format(trl_file))
sys.exit(2)

#-------------------------------------------------------------------------------
# special initialization when this is the main program

Expand Down
11 changes: 10 additions & 1 deletion lib/stsci/tools/convertwaiveredfits.py
Expand Up @@ -152,7 +152,12 @@
#
import os
import sys
import astropy
from astropy.io import fits
from distutils.version import LooseVersion

ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

#
# -----------------------------------------------------------------------------
# Function definitions
Expand Down Expand Up @@ -516,7 +521,11 @@ def toMultiExtensionFits(waiveredObject,
head,tail = os.path.split(multiExtensionFileName)
mhdul[0].header.set('FILENAME', value=tail, after='NEXTEND')

mhdul.writeto(multiExtensionFileName,clobber=True)
if ASTROPY_VER_GE13:
mhdul.writeto(multiExtensionFileName, overwrite=True)
else:
mhdul.writeto(multiExtensionFileName, clobber=True)

verboseString = verboseString[:-1] + " and written to " + \
multiExtensionFileName + "."

Expand Down
26 changes: 21 additions & 5 deletions lib/stsci/tools/fileutil.py
Expand Up @@ -77,6 +77,7 @@
from . import numerixenv
numerixenv.check()

import astropy
from . import stpyfits as fits
from . import readgeis
from . import convertwaiveredfits
Expand All @@ -90,13 +91,16 @@

import time as _time
import numpy as np
from distutils.version import LooseVersion

PY3K = sys.version_info[0] > 2
if PY3K:
string_types = str
else:
string_types = basestring

ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

# Environment variable handling - based on iraffunctions.py
# define INDEF, yes, no, EOF, Verbose, userIrafHome

Expand Down Expand Up @@ -507,7 +511,7 @@ def buildRootname(filename, ext=None):

if rootname is not None:
break

# If we still haven't found the file, see if we have the
# info to build one...
if rootname is None and ext is not None:
Expand Down Expand Up @@ -742,10 +746,16 @@ def openImage(filename, mode='readonly', memmap=0, writefits=True,
fexists = os.path.exists(fitsname)
if (fexists and clobber) or not fexists:
print('Writing out WAIVERED as MEF to ', fitsname)
fimg.writeto(fitsname, clobber=clobber)
if ASTROPY_VER_GE13:
fimg.writeto(fitsname, overwrite=clobber)
else:
fimg.writeto(fitsname, clobber=clobber)
if dqexists:
print('Writing out WAIVERED as MEF to ', dqfitsname)
dqfile.writeto(dqfitsname, clobber=clobber)
if ASTROPY_VER_GE13:
dqfile.writeto(dqfitsname, overwrite=clobber)
else:
dqfile.writeto(dqfitsname, clobber=clobber)
# Now close input GEIS image, and open writable
# handle to output FITS image instead...
fimg.close()
Expand Down Expand Up @@ -789,10 +799,16 @@ def openImage(filename, mode='readonly', memmap=0, writefits=True,
fexists = os.path.exists(fitsname)
if (fexists and clobber) or not fexists:
print('Writing out GEIS as MEF to ', fitsname)
fimg.writeto(fitsname, clobber=clobber)
if ASTROPY_VER_GE13:
fimg.writeto(fitsname, overwrite=clobber)
else:
fimg.writeto(fitsname, clobber=clobber)
if dqexists:
print('Writing out GEIS as MEF to ', dqfitsname)
dqfile.writeto(dqfitsname, clobber=clobber)
if ASTROPY_VER_GE13:
dqfile.writeto(dqfitsname, overwrite=clobber)
else:
dqfile.writeto(dqfitsname, clobber=clobber)
# Now close input GEIS image, and open writable
# handle to output FITS image instead...
fimg.close()
Expand Down
78 changes: 62 additions & 16 deletions lib/stsci/tools/tests/testStpyfits.py
Expand Up @@ -5,14 +5,18 @@
import tempfile

import numpy as np
from distutils.version import LooseVersion

from nose.tools import assert_true, assert_false, assert_equal, assert_raises

import stsci.tools.stpyfits as stpyfits
#import pyfits
import astropy
from astropy.io import fits
#from pyfits.tests import PyfitsTestCase
from astropy.io.fits.tests import FitsTestCase

ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

class TestStpyfitsFunctions(FitsTestCase):
def setup(self):
Expand Down Expand Up @@ -144,10 +148,19 @@ def testwritetoConvienceFunction(self):
header = hdul[0].header.copy()
header['NAXIS'] = 0

stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
clobber=True)
fits.writeto(self.temp('new1.fits'), hdul1[0].data,hdul1[0].header,
clobber=True)
if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
overwrite=True)
else:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
clobber=True)

if ASTROPY_VER_GE13:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, overwrite=True)
else:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, clobber=True)

hdul.close()
hdul1.close()
Expand All @@ -171,10 +184,19 @@ def testappendConvienceFunction(self):
hdul = stpyfits.open(self.data('cdva2.fits'))
hdul1 = fits.open(self.data('cdva2.fits'))

stpyfits.writeto(self.temp('new.fits'), hdul[0].data, hdul[0].header,
clobber=True)
fits.writeto(self.temp('new1.fits'), hdul1[0].data, hdul1[0].header,
clobber=True)
if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data,
hdul[0].header, overwrite=True)
else:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data,
hdul[0].header, clobber=True)

if ASTROPY_VER_GE13:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, overwrite=True)
else:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, clobber=True)

hdu = stpyfits.ImageHDU()
hdu1 = fits.ImageHDU()
Expand Down Expand Up @@ -243,8 +265,14 @@ def testupdateConvienceFunction(self):

header = hdul[0].header.copy()
header['NAXIS'] = 0
stpyfits.writeto(self.temp('new.fits'), hdul[0].data, header,
clobber=True)

if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data,
header, overwrite=True)
else:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data,
header, clobber=True)


hdu = stpyfits.ImageHDU()
hdu1 = fits.ImageHDU()
Expand Down Expand Up @@ -324,8 +352,14 @@ def testPrimaryHDUConstructor(self):
hdu = stpyfits.PrimaryHDU(n)
hdu.header.set('PIXVALUE', 1.0, 'Constant pixel value', after='EXTEND')
hdu.header.set('NAXIS', 0)
stpyfits.writeto(self.temp('new.fits'), hdu.data, hdu.header,
clobber=True)

if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdu.data, hdu.header,
overwrite=True)
else:
stpyfits.writeto(self.temp('new.fits'), hdu.data, hdu.header,
clobber=True)

hdul = stpyfits.open(self.temp('new.fits'))
hdul1 = fits.open(self.temp('new.fits'))

Expand Down Expand Up @@ -371,7 +405,10 @@ def testHDUListWritetoMethod(self):
hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
after='NAXIS1')
hdul = stpyfits.HDUList([hdu,hdu1])
hdul.writeto(self.temp('new.fits'), clobber=True)
if ASTROPY_VER_GE13:
hdul.writeto(self.temp('new.fits'), overwrite=True)
else:
hdul.writeto(self.temp('new.fits'), clobber=True)

assert_equal(stpyfits.info(self.temp('new.fits'), output=False),
[(0, 'PRIMARY', 'PrimaryHDU', 7, (10, 10), 'int32', ''),
Expand Down Expand Up @@ -437,7 +474,10 @@ def testHDUList__getitem__Method(self):
hdu = stpyfits.PrimaryHDU(n)
hdu.header.set('PIXVALUE', 1., 'constant pixel value', after='EXTEND')

hdu.writeto(self.temp('new.fits'), clobber=True)
if ASTROPY_VER_GE13:
hdu.writeto(self.temp('new.fits'), overwrite=True)
else:
hdu.writeto(self.temp('new.fits'), clobber=True)

hdul = stpyfits.open(self.temp('new.fits'))
hdul1 = fits.open(self.temp('new.fits'))
Expand Down Expand Up @@ -486,7 +526,10 @@ def testHDUListFlushMethod(self):
hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
after='NAXIS1')
hdul = stpyfits.HDUList([hdu, hdu1])
hdul.writeto(self.temp('new.fits'), clobber=True)
if ASTROPY_VER_GE13:
hdul.writeto(self.temp('new.fits'), overwrite=True)
else:
hdul.writeto(self.temp('new.fits'), clobber=True)

hdul = stpyfits.open(self.temp('new.fits'), 'update')
d = np.arange(10, dtype=np.int32)
Expand Down Expand Up @@ -589,7 +632,10 @@ def testImageBaseHDUWriteToMethod(self):
hdu = stpyfits.PrimaryHDU(n)
hdu.header.set('PIXVALUE', 1., 'constant pixel value', after='EXTEND')

hdu.writeto(self.temp('new.fits'), clobber=True)
if ASTROPY_VER_GE13:
hdu.writeto(self.temp('new.fits'), overwrite=True)
else:
hdu.writeto(self.temp('new.fits'), clobber=True)

hdul = stpyfits.open(self.temp('new.fits'))
hdul1 = fits.open(self.temp('new.fits'))
Expand Down

0 comments on commit c3bbd95

Please sign in to comment.