<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix"><br>
       another thought on this:<br>
      <br>
      I think the original question was also limited in not explaining
      why fits was needed. I could argue for pickle. Paul is right, HDF
      might be a better match, especially if you have to switch to
      another language, HDF has a more native match to that. But does it
      have to be persistent data? otherwise using a python-c/fortran
      interface is far more efficient.  (I believe HDF is actually more
      flexible than the F in FITS).<br>
      <br>
      You can't beat a native pickle:<br>
      <br>
                  import pickle<br>
                  pickle.dump(datan_raw,open("test.dat","wb"))<br>
                  ..<br>
                  new_raw = pickle.load(open("test.dat", "rb"))<br>
      <br>
      So perhaps we could return the question and ask in what situation
      you need this data structure (for).<br>
      <br>
      - peter<br>
      <br>
      On 12/04/2017 10:08 PM, Paul Kuin wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CANoQ6N3gCT1Ek91-VMgzxYc4z4+UuiK3gMu1HwigpdnKn-oxBg@mail.gmail.com">
      <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"
              moz-do-not-send="true">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" moz-do-not-send="true">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" moz-do-not-send="true">AstroPy@python.org</a><br>
            <a href="https://mail.python.org/mailman/listinfo/astropy"
              rel="noreferrer" target="_blank" moz-do-not-send="true">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/%7Enpmk/"
                                  target="_blank" moz-do-not-send="true">http://www.mssl.ucl.ac.uk/~npmk/</a>
                                * * * *<br>
                                Dr. N.P.M. Kuin      (<a
                                  href="mailto:n.kuin@ucl.ac.uk"
                                  target="_blank" moz-do-not-send="true">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>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
AstroPy mailing list
<a class="moz-txt-link-abbreviated" href="mailto:AstroPy@python.org">AstroPy@python.org</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/astropy">https://mail.python.org/mailman/listinfo/astropy</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>