[AstroPy] Inheriting HDUList

Erik Bray embray at stsci.edu
Mon Feb 3 14:32:30 EST 2014


On 02/03/2014 02:30 PM, Erik Bray wrote:
> On 02/02/2014 09:10 AM, Joe Philip Ninan wrote:
>> Hi,
>> Is there any online example of how to create a new child object from HDUList of
>> astropy.io.fits ?  (The object astropy.io.fits.open() returns).
>> I want to create an object class which inherits everything from HDUList, and
>> then add some additional functions.
>> The part i cannot figure out is how to initialize the HDUList inside the new class.
>>
>> class MyFitsObject(HDUList):
>>       def __init__(self,filename):
>>       # How do i initialise HDUList here, when all i have is the Fits File name?
>
> Hi,
>
> It depends on exactly what you want to do--you have to be careful as HDUList has
> a bit of internal state that needs to be managed, but for the most part if you
> just want to extend it with some new methods it's safe enough.  But it's hard to
> help without knowing exactly what you need to do.
>
> One thing I would emphatically *not* recommend is to pass a filename to your
> __init__, but again it depends on exactly what you need the subclass to do.
> Normally HDUList is treated as a more abstract datastructure that may or may not
> have actually come from a file on disk.  HDUList.fromfile should be used to read
> an HDUList from a file (in fact it's basically what pyfits.open() wraps).
>
> The base HDUList.__init__ simply accepts a list of HDU objects to add to the list.

I should add, the example John sent from the yt project is a good example of how 
to extend the HDUList class.  +1 to it.

Erik




More information about the AstroPy mailing list