<div dir="ltr">I think that HDF does that for you. FIts is more flexible, but you have to do your own writes and retrievals. In the end you will be reinventing the wheel unless you check out how HDF does it, That's my opinion. <div><br></div><div>Cheers, </div><div><br></div><div>   Paul</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 4, 2017 at 9:02 PM, Arnon Sela <span dir="ltr"><<a href="mailto:arnon.sela@gmail.com" target="_blank">arnon.sela@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear Whom that Can Help,<br>
<br>I have nested numpy recarray structure to be stored into Fits.<br>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).<br>
<br>Code start >>>>>><br>
<br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div>import numpy as np</div><div><br></div><div>''' The following two functions are adapted from: </div><div>adopted from <a href="https://stackoverflow.com/questions/32328889/numpy-structured-array-from-arbitrary-level-nested-dictionary" target="_blank">https://stackoverflow.com/<wbr>questions/32328889/numpy-<wbr>structured-array-from-<wbr>arbitrary-level-nested-<wbr>dictionary</a></div><div>'''</div><div><br></div><div>def mkdtype(d):</div><div>    ''' Creates dtype for nested dictionary with numpy based type objects</div><div>    '''</div><div>    result = []</div><div>    for k, v in d.items():</div><div>        if isinstance(v,np.ndarray):</div><div>            result.append((k, v.dtype, v.shape))</div><div>        else:</div><div>            result.append((k, mkdtype(v)))</div><div>    return np.dtype(result)</div><div><br></div><div>def dict2recarray(data, rec=None):</div><div>    ''' Creates numpy.recarray from data (dict)</div><div>    '''</div><div>    def _dict2recarray(data, rec):</div><div>        if rec.dtype.names:</div><div>            for n in rec.dtype.names:</div><div>                _dict2recarray(data[n], rec[n])</div><div>        else:</div><div>            rec[:] = data</div><div>        return rec</div><div>    </div><div>    dtype = mkdtype(data)</div><div>    if rec is None:</div><div>        rec = np.zeros(dtype.shape, dtype)</div><div>        </div><div>    return _dict2recarray(data, rec)</div><div><br></div><div>datan_raw = {'DATA': {'D1': np.linspace( 0, 100, 8*4,).reshape(8, 4),</div><div>                      'D2': np.linspace( 0, 100, 10*5, ).reshape(10, 5), </div><div>                      'ND': {'D1': np.linspace( 0, 100, 10*5, ).reshape(10, 5), </div><div>                             'D2': np.linspace( 0, 100, 8*4,).reshape(8, 4), }}}</div><div><br></div><div>dtype = mkdtype(datan_raw)</div><div><b><i>data_for_fits</i></b> = dict2recarray(datan_raw)</div></blockquote>
<br>>>>>>> Code ends<br>
<br>I couldn't find documentation on how to build such a FITS structure (nested recarrays). <br>
<br>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.<br>
<br>The better option is to build FITS is such a manner that would retrieve the structure correctly on FITS load().<br>
<br>Thank you for your help,<br>
<br>Best regards.<br></div>
<br>______________________________<wbr>_________________<br>
AstroPy mailing list<br>
<a href="mailto:AstroPy@python.org">AstroPy@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/astropy" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/astropy</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><br>* * * * * * * * <a href="http://www.mssl.ucl.ac.uk/~npmk/" target="_blank">http://www.mssl.ucl.ac.uk/~npmk/</a> * * * *<br>Dr. N.P.M. Kuin      (<a href="mailto:n.kuin@ucl.ac.uk" target="_blank">n.kuin@ucl.ac.uk</a>)      <br>phone +44-(0)1483 (prefix) -204111 (work) <br>mobile +44(0)7908715953  skype ID: npkuin<br>Mullard Space Science Laboratory  – University College London  –<br>Holmbury St Mary – Dorking – Surrey RH5 6NT–  U.K.</div></div></div></div></div></div></div></div></div></div></div></div>
</div>