Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Isolate common code of {set,get}_{pyfits,fits} [skip appveyor]
Browse files Browse the repository at this point in the history
  • Loading branch information
montefra committed Oct 24, 2017
1 parent 65f6af0 commit fe79a2a
Showing 1 changed file with 38 additions and 22 deletions.
60 changes: 38 additions & 22 deletions pyds9/pyds9.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,40 @@ def access(self):
x = xpa.xpaaccess(string_to_bytes(self.id), None, 1)
return bytes_to_string(x[0])

def _ds9_fits_to_bytes(self):
'''Returns a ds9 FITS as a byte stream
Returns
-------
:class:`six.BytesIO`
file-like object containing the FITS data
'''
self._selftest()
imgData = self.get('fits')
return BytesIO(string_to_bytes(imgData))

def _hdulist_to_ds9_fits(self, hdul):
'''Send the input HDUList to ds9
Parameters
----------
hdul : :class:`HDUList` (astropy or pyfits)
FITS file to send
Returns
-------
success : int
1 indicates that ds9 was contacted successfully, while a return
value of 0 indicates a failure.
'''
self._selftest()
# for python2 BytesIO and StringIO are the same
with contextlib.closing(BytesIO()) as newFitsFile:
hdul.writeto(newFitsFile)
newfits = newFitsFile.getvalue()
success = self.set('fits', newfits, len(newfits))
return success

def get_fits(self):
"""Retrieve data from ds9 as an astropy FITS.
Expand All @@ -650,10 +684,7 @@ def get_fits(self):
:class:`astropy.io.fits.HDUList`
FITS object
"""
self._selftest()
imgData = self.get('fits')
imgString = BytesIO(string_to_bytes(imgData))
return fits.open(imgString)
return fits.open(self._ds9_fits_to_bytes())

def set_fits(self, hdul):
"""Display an astropy FITS in ds9.
Expand All @@ -680,15 +711,9 @@ def set_fits(self, hdul):
ValueError
if the input is not an astropy HDUList
"""
self._selftest()
if not isinstance(hdul, fits.HDUList):
raise ValueError('The input must be an astropy HDUList')
# for python2 BytesIO and StringIO are the same
with contextlib.closing(BytesIO()) as newFitsFile:
hdul.writeto(newFitsFile)
newfits = newFitsFile.getvalue()
got = self.set('fits', newfits, len(newfits))
return got
return self._hdulist_to_ds9_fits(hdul)

def get_pyfits(self):
"""Retrieve data from ds9 as an pyfits FITS.
Expand Down Expand Up @@ -720,10 +745,7 @@ def get_pyfits(self):
" 'get_pyfits'. The method is available only"
" if the package 'pyfits' is imported and"
" it's version >=2.2")
self._selftest()
imgData = self.get('fits')
imgString = BytesIO(string_to_bytes(imgData))
return pyfits.open(imgString)
return pyfits.open(self._ds9_fits_to_bytes())

def set_pyfits(self, hdul):
"""Display a pyfits FITS in ds9.
Expand Down Expand Up @@ -757,15 +779,9 @@ def set_pyfits(self, hdul):
" 'set_pyfits'. The method is available only"
" if the package 'pyfits' is imported and"
" it's version >=2.2")
self._selftest()
if not isinstance(hdul, pyfits.HDUList):
raise ValueError('The input must be a pyfits HDUList')
# for python2 BytesIO and StringIO are the same
with contextlib.closing(BytesIO()) as newFitsFile:
hdul.writeto(newFitsFile)
newfits = newFitsFile.getvalue()
got = self.set('fits', newfits, len(newfits))
return got
return self._hdulist_to_ds9_fits(hdul)

def get_arr2np(self):
"""Convert a FITS file or an array from ds9 into a numpy array.
Expand Down

0 comments on commit fe79a2a

Please sign in to comment.