astropy/astropy — pull request #659 by astrofrog (2013-01-19 21:06:48+00:00)
Title: Refactor I/O framework

Astropy has two main data containers, ``Table``, and ``NDData``, which are equivalent to structured and normal arrays in Numpy. It makes sense for these two objects to behave in a similar way - yet, the current I/O functionality is only implemented for ``Table``.

This PR takes the I/O code out of ``Table`` and instead puts it in ``astropy.io.registry``. A ``DataIO`` class is defined, and both ``NDData`` and ``Table`` then inherit from it (as @taldcroft suggested off list, I think we need a better name than ``DataIO``). This made it very easy to extend the I/O stuff to ``NDData``, although no readers/writers are defined inside Astropy currently. Here's an example of defining a (dumb) FITS reader:

```
import numpy as np

from astropy.nddata import NDData
from astropy.io import fits, registry

def read_fits_data(filename, hdu=0):
hdu = fits.open(filename)[hdu]
return NDData(hdu.data, meta=hdu.header)

registry.register_reader('fits', NDData, read_fits_data)

d = NDData.read('MSX_E.fits', format='fits')

print(np.mean(d))
print(d.shape)
print(d.meta['TELESCOP'])
```

with the result:

```
1.09424543196e-05
(599, 599)
MSX
```

The documentation still needs to be updated (the I/O page would be moved out of Table and moved to ``io/registry.rst``) but I wanted to first get some feedback on this.

@eteq @taldcroft @iguananaut @mdboom - what do you think?

@iguananaut - I've tagged this 0.3. Of course, if it gets finalized soon, I don't see a reason it can't be put in 0.2 instead.