prjemian / punx

Python Utilities for NeXus HDF5 files
https://prjemian.github.io/punx
5 stars 7 forks source link

tree displays incorrectly: array of variable length strings #123

Closed prjemian closed 4 years ago

prjemian commented 4 years ago

Given a python list containing str members, each with a different length, punx tree only renders the first component as mask_names:NX_CHAR[9] = Q=0.002, not all 27 members (or some subset).

This code writes the HDF:

    nxmask.create_dataset('mask_names', data=mask_names)

This code creates the list:

    mask_names = q_based_mask_names(xpcs["ql_dyn"])

where xpcs["ql_dyn"] is a list of float and

def q_based_mask_names(q_array):
    """
    Create a text name of each mask from its Q value

    Write each Q value to 2 significant figures,
    using fixed-point notation, no trailing zeros

    Result should be a string that h5py can write 
    in a string array as a dataset, thus it must 
    be byte-encoded.
    """
    def formatter(value):
        s_value = f"{value:.2e}"        # 2 sig figs
        s_value = f"Q={float(s_value)}"   # fixed-point
        return s_value.encode()         # byte-encode

    return list(map(formatter, q_array))

See this HDFview of the data file:

Clipboard01