Public feed #892539

Miscellany

public text v1 · immutable
Raw Edit
·published 2008-02-05 14:58 UTC
rendered paste body
def parse_seabird(fp):
    if not hasattr(fp, 'read'): fp = open(fp)
    content = fp.read()

    header, data = content.split('*END*\r\n', 1)

    # Process headers.
    p = re.compile("""
        (?:\*{1,2}|\#)\s    # starts with "* " or "** " or "# "
        (.*?)               # everything until next token
        \s*(?::|=|$)\s*     # ":  " or " = " or EOL
        (.*)                # all the rest, if any
    """, re.VERBOSE)
    attributes = (p.match(line).groups() for line in header.split('\r\n') if line.strip())
    attributes = dict((k, lazy_eval(v.strip())) for (k, v) in attributes)

    file_type = attributes.get('file_type', 'binary').lower()
    nquan = attributes.pop('nquan')
    if file_type == 'binary':
        data = fromstring(data, 'f')
    elif file_type == 'ascii':
        data = fromstring(data, sep=' ')
    data.shape = (-1, nquan)

    return attributes, data