[AstroPy] Nested recarrays in FITS

Arnon Sela arnon.sela at gmail.com
Mon Dec 4 16:02:03 EST 2017

Dear Whom that Can Help,

I have nested numpy recarray structure to be stored into Fits.
The following code is a just a test I used to build a nested structure
(data_for_fits variable in the last line of the code).

Code start >>>>>>

import numpy as np

''' The following two functions are adapted from:
adopted from

def mkdtype(d):
    ''' Creates dtype for nested dictionary with numpy based type objects
    result = []
    for k, v in d.items():
        if isinstance(v,np.ndarray):
            result.append((k, v.dtype, v.shape))
            result.append((k, mkdtype(v)))
    return np.dtype(result)

def dict2recarray(data, rec=None):
    ''' Creates numpy.recarray from data (dict)
    def _dict2recarray(data, rec):
        if rec.dtype.names:
            for n in rec.dtype.names:
                _dict2recarray(data[n], rec[n])
            rec[:] = data
        return rec

    dtype = mkdtype(data)
    if rec is None:
        rec = np.zeros(dtype.shape, dtype)

    return _dict2recarray(data, rec)

datan_raw = {'DATA': {'D1': np.linspace( 0, 100, 8*4,).reshape(8, 4),
                      'D2': np.linspace( 0, 100, 10*5, ).reshape(10, 5),
                      'ND': {'D1': np.linspace( 0, 100, 10*5, ).reshape(10,
                             'D2': np.linspace( 0, 100, 8*4,).reshape(8,
4), }}}

dtype = mkdtype(datan_raw)
*data_for_fits* = dict2recarray(datan_raw)

>>>>>> Code ends

I couldn't find documentation on how to build such a FITS structure (nested

One option is to build sub-recarrays into different BIN tables with a
header that would correspond to a nested key in the recarray. But that
would require creating another function to reconstruct the recarray
structure after reading the BIN tables from the FITS file.

The better option is to build FITS is such a manner that would retrieve the
structure correctly on FITS load().

Thank you for your help,

Best regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20171204/fd098c9d/attachment-0001.html>

More information about the AstroPy mailing list