Skip to content

Commit

Permalink
Replace USE_FITS_OVERWRITE with ASTROPY_VER_GE13 and distutils-based …
Browse files Browse the repository at this point in the history
…version comparison
  • Loading branch information
mcara committed Feb 3, 2017
1 parent e40e6bf commit 1bdde24
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
8 changes: 3 additions & 5 deletions lib/stsci/tools/asnutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
from astropy.io import fits
import numpy as N
import os.path, time
from distutils.version import LooseVersion

# USE_FITS_OVERWRITE is necessary as long as we support astropy versions < 1.3
USE_FITS_OVERWRITE = ((astropy.version.major == 1 and
astropy.version.minor >= 3) or
astropy.version.major >= 2)
ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

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

Expand Down Expand Up @@ -415,7 +413,7 @@ 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)
if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
fasn.writeto(outfile, overwrite=True)
else:
fasn.writeto(outfile, clobber=True)
Expand Down
6 changes: 3 additions & 3 deletions lib/stsci/tools/convertwaiveredfits.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@
import sys
import astropy
from astropy.io import fits
from distutils.version import LooseVersion

# USE_FITS_OVERWRITE is necessary as long as we support astropy versions < 1.3
USE_FITS_OVERWRITE = astropy.version.major >= 1 and astropy.version.minor >=3
ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

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

if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
mhdul.writeto(multiExtensionFileName, overwrite=True)
else:
mhdul.writeto(multiExtensionFileName, clobber=True)
Expand Down
14 changes: 6 additions & 8 deletions lib/stsci/tools/fileutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,15 @@

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

# USE_FITS_OVERWRITE is necessary as long as we support astropy versions < 1.3
USE_FITS_OVERWRITE = ((astropy.version.major == 1 and
astropy.version.minor >= 3) or
astropy.version.major >= 2)
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 @@ -748,13 +746,13 @@ 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)
if USE_FITS_OVERWRITE:
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)
if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
dqfile.writeto(dqfitsname, overwrite=clobber)
else:
dqfile.writeto(dqfitsname, clobber=clobber)
Expand Down Expand Up @@ -801,13 +799,13 @@ 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)
if USE_FITS_OVERWRITE:
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)
if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
dqfile.writeto(dqfitsname, overwrite=clobber)
else:
dqfile.writeto(dqfitsname, clobber=clobber)
Expand Down
27 changes: 13 additions & 14 deletions lib/stsci/tools/tests/testStpyfits.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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
Expand All @@ -14,10 +16,7 @@
#from pyfits.tests import PyfitsTestCase
from astropy.io.fits.tests import FitsTestCase

# USE_FITS_OVERWRITE is necessary as long as we support astropy versions < 1.3
USE_FITS_OVERWRITE = ((astropy.version.major == 1 and
astropy.version.minor >= 3) or
astropy.version.major >= 2)
ASTROPY_VER_GE13 = LooseVersion(astropy.__version__) >= LooseVersion('1.3')

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

if USE_FITS_OVERWRITE:
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 USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, overwrite=True)
else:
Expand Down Expand Up @@ -185,14 +184,14 @@ def testappendConvienceFunction(self):
hdul = stpyfits.open(self.data('cdva2.fits'))
hdul1 = fits.open(self.data('cdva2.fits'))

if USE_FITS_OVERWRITE:
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 USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
fits.writeto(self.temp('new1.fits'), hdul1[0].data,
hdul1[0].header, overwrite=True)
else:
Expand Down Expand Up @@ -267,7 +266,7 @@ def testupdateConvienceFunction(self):
header = hdul[0].header.copy()
header['NAXIS'] = 0

if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdul[0].data,
header, overwrite=True)
else:
Expand Down Expand Up @@ -354,7 +353,7 @@ def testPrimaryHDUConstructor(self):
hdu.header.set('PIXVALUE', 1.0, 'Constant pixel value', after='EXTEND')
hdu.header.set('NAXIS', 0)

if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
stpyfits.writeto(self.temp('new.fits'), hdu.data, hdu.header,
overwrite=True)
else:
Expand Down Expand Up @@ -406,7 +405,7 @@ def testHDUListWritetoMethod(self):
hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
after='NAXIS1')
hdul = stpyfits.HDUList([hdu,hdu1])
if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
hdul.writeto(self.temp('new.fits'), overwrite=True)
else:
hdul.writeto(self.temp('new.fits'), clobber=True)
Expand Down Expand Up @@ -475,7 +474,7 @@ def testHDUList__getitem__Method(self):
hdu = stpyfits.PrimaryHDU(n)
hdu.header.set('PIXVALUE', 1., 'constant pixel value', after='EXTEND')

if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
hdu.writeto(self.temp('new.fits'), overwrite=True)
else:
hdu.writeto(self.temp('new.fits'), clobber=True)
Expand Down Expand Up @@ -527,7 +526,7 @@ def testHDUListFlushMethod(self):
hdu1.header.set('NAXIS2', 10, 'length of constant array axis 2',
after='NAXIS1')
hdul = stpyfits.HDUList([hdu, hdu1])
if USE_FITS_OVERWRITE:
if ASTROPY_VER_GE13:
hdul.writeto(self.temp('new.fits'), overwrite=True)
else:
hdul.writeto(self.temp('new.fits'), clobber=True)
Expand Down Expand Up @@ -633,7 +632,7 @@ def testImageBaseHDUWriteToMethod(self):
hdu = stpyfits.PrimaryHDU(n)
hdu.header.set('PIXVALUE', 1., 'constant pixel value', after='EXTEND')

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

0 comments on commit 1bdde24

Please sign in to comment.