Last active
December 20, 2015 16:39
-
-
Save jdswinbank/6163501 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import abc | |
class DataAccessor(object): | |
__metaclass__ = abc.ABCMeta | |
@abc.abstractproperty | |
def wcs(self): | |
pass | |
@abc.abstractproperty | |
def data(self): | |
pass | |
@abc.abstractproperty | |
def url(self): | |
pass | |
def extract_metadata(self): | |
return { | |
'url': self.url | |
} | |
class FitsImage(DataAccessor): | |
def __init__(self, url, plane, beam, hdu=0): | |
self._plane = plane | |
self._hdu = hdu | |
self._url = url | |
self._beam = beam | |
# We must provide all the abstract properties, or we can't instantiate | |
# the class. We may also provide whatever extra properties we like: | |
if 'TELESCOP' in self.header: | |
self.telescope = self.header['TELESCOP'] | |
# But Trap code is explicitly not allowed to depend on their existence | |
# (ie, if the attribute doesn't exist, Trap code must gracefully | |
# degrade rather than throw an exception). | |
# Optional properties, to match current FitsImage class | |
@property | |
def hdu(self): | |
return pyfits.open(self.url)[self._hdu] | |
@property | |
def header(self): | |
return self.hdu.header | |
# Properties required to comply with interface | |
@property | |
def wcs(self): | |
if not hasattr(self, "_wcs"): | |
self._wcs = parse_coordinates(self.header) | |
return self._wcs | |
@property | |
def data(self): | |
return read_data(self.hdu, self._plane) | |
@property | |
def url(self): | |
return self._url | |
f = FitsImage('url', 'plane', 'beam') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment