- sncosmo.read_snana_ascii(fname, default_tablename=None)[source]¶
Read an SNANA-format ascii file.
Such files may contain metadata lines and one or more tables. See Notes for a summary of the format.
Filename of object to read.
- default_tablenamestr, optional
Default tablename, or the string that indicates a table row, when a table starts with ‘NVAR:’ rather than ‘NVAR_TABLENAME:’.
- arraybool, optional
If True, each table is converted to a numpy array. If False, each table is a dictionary of lists (each list is a column). Default is True.
Metadata from keywords.
- tablesdict of
Tables, indexed by table name.
The file can contain one or more tables, as well as optional metadata. Here is an example of the expected format:
META1: a META2: 6 NVAR_SN: 3 VARNAMES: A B C SN: 1 2.0 x SN: 4 5.0 y
Any strings ending in a colon (:) are treated as keywords.
The start of a new table is indicated by a keyword starting with ‘NVAR’.
If the ‘NVAR’ is followed by an underscore (e.g., ‘NVAR_TABLENAME’), then ‘TABLENAME’ is taken to be the name of the table. Otherwise the user must specify a
default_tablename. This is because data rows are identified by the tablename.
After a keyword starting with ‘NVAR’, the next keyword must be ‘VARNAMES’. The strings following give the column names.
Any other keywords anywhere in the file are treated as metadata. The first string after the keyword is treated as the value for that keyword.
Note: Newlines are treated as equivalent to spaces; they do not indicate a new row. This is necessary because some SNANA-format files have multiple metadata on a single row or single table rows split over multiple lines, making newline characters meaningless.
>>> from io import StringIO # StringIO behaves like a file >>> f = StringIO('META1: a\n' ... 'META2: 6\n' ... 'NVAR_SN: 3\n' ... 'VARNAMES: A B C\n' ... 'SN: 1 2.0 x\n' ... 'SN: 4 5.0 y\n') ... >>> meta, tables = read_snana_ascii(f)
The first object is a dictionary of metadata:
>>> meta OrderedDict([('META1', 'a'), ('META2', 6)])
The second is a dictionary of all the tables in the file:
>>> tables['SN'] <Table rows=2 names=('A','B','C')> array([(1, 2.0, 'x'), (4, 5.0, 'y')], dtype=[('A', '<i8'), ('B', '<f8'), ('C', 'S1')])
If the file had an ‘NVAR’ keyword rather than ‘NVAR_SN’, for example:
NVAR: 3 VARNAMES: A B C SN: 1 2.0 x SN: 4 5.0 y SN: 5 8.2 z
it can be read by supplying a default table name:
>>> meta, tables = read_snana_ascii(f, default_tablename='SN')