Beispiel #1
0
def test_is_unit():
    r"""Test is_unit."""
    assert (units.is_unit('n/a'))
    assert (units.is_unit(''))
    assert (units.is_unit('cm/s**2'))
    assert (units.is_unit('cm/s^2'))
    assert (units.is_unit('umol'))
    assert (units.is_unit('mmol'))
    assert (not units.is_unit('invalid'))
Beispiel #2
0
 def test_is_unit(self):
     r"""Test is_unit."""
     assert(units.is_unit('n/a'))
     assert(units.is_unit(''))
     assert(units.is_unit('cm/s**2'))
     # Not supported by unyt
     # assert(units.is_unit('cm/s^2'))
     assert(units.is_unit('umol'))
     assert(units.is_unit('mmol'))
     assert(not units.is_unit('invalid'))
Beispiel #3
0
def parse_header(header, newline=_default_newline, lineno_format=None,
                 lineno_names=None, lineno_units=None):
    r"""Parse an ASCII table header to get information about the table.

    Args:
        header (list, str): Header lines that should be parsed.
        newline (str, optional): Newline character that should be used to split
            header if it is not already a list. Defaults to _default_newline.
        lineno_format (int, optional): Line number where formats are located.
            If not provided, an attempt will be made to locate one.
        lineno_names (int, optional): Line number where field names are located.
            If not provided, an attempt will be made to locate one.
        lineno_units (int, optional): Line number where field units are located.
            If not provided, an attempt will be made to locate one.

    Returns:
        dict: Parameters describing the information determined from the header.

    """
    out = dict()
    excl_lines = []
    if isinstance(header, backwards.bytes_type):
        header = [h + newline for h in header.split(newline)]
    # Locate format line
    if lineno_format is None:
        for i in range(len(header)):
            if len(extract_formats(header[i])) > 0:
                lineno_format = i
                break
    # Get format information from format line
    if lineno_format is not None:
        excl_lines.append(lineno_format)
        info = format2table(header[lineno_format])
        ncol = len(info['fmts'])
        out.update(**info)
        out['format_str'] = header[lineno_format].split(info['comment'])[-1]
    else:
        ncol = 0
        out.update(delimiter=_default_delimiter, comment=_default_comment,
                   fmts=[])
    out.setdefault('newline', newline)
    # Use explicit lines for names & units
    if lineno_names is not None:
        excl_lines.append(lineno_names)
        out['field_names'] = header[lineno_names].split(out['comment'])[-1].split(
            out['newline'])[0].split(out['delimiter'])
    if lineno_units is not None:
        excl_lines.append(lineno_units)
        out['field_units'] = header[lineno_units].split(out['comment'])[-1].split(
            out['newline'])[0].split(out['delimiter'])
    # Locate units & names
    for i in range(len(header)):
        if ('field_units' in out) and ('field_names' in out):
            break
        if i in excl_lines:
            continue
        cols = header[i].split(out['comment'])[-1].split(
            out['newline'])[0].split(out['delimiter'])
        if (len(cols) == ncol) or (ncol == 0):
            ncol = len(cols)
            is_unit = True
            for u in cols:
                if not units.is_unit(u):
                    is_unit = False
                    break
            if is_unit:
                if 'field_units' in out:
                    raise RuntimeError("Two lines could contain the field units.")
                out['field_units'] = cols
            else:
                if 'field_names' in out:
                    raise RuntimeError("Two lines could contain the field names.")
                out['field_names'] = cols
    return out