Last modified: 18 December 2023

How do Sherpa 'load' commands handle ASCII files?


As of the CIAO 4.1.2 release, the 'load' functions used with ASCII files (load_ascii, load_data, load_table) have been updated to accept DM ASCII syntax and therefore recognize the column definitions in file headers.

Example syntax:

sherpa> load_data("simptab.dat")      

By default, if there is a header (i.e., the first N lines are comments, indicated by a '#' character), the last line of the header is assumed to be a list of columns, and only the first two columns are loaded into Sherpa to define x- and y-data arrays. However, if an ASCII file predates Sherpa's use of the ASCII kernel, and there are no column names contained in the last line of the header, then Sherpa will try to load the first two words in the line as columns. If only one word is contained in the last line, then by default only one column will be loaded.

If, for example, there are four columns in the ASCII file, but the last line of the header is:

# A B

then only the first two columns are read in; the first is named "A", and the second is named "B".

If the ASCII file format predates the use of the ASCII kernel, and the last line of the header does not indicate column names but is simply a comment with a single word,

# A

then by default only one column is loaded into Sherpa.

Sherpa tries to catch when only one column has been loaded, and to try again to open all columns. If enough columns are found on this try, Sherpa loads them; if the file has only one column, an error is returned.

If there really is only one column:

sherpa> load_data("onetab.dat")      
TypeError: __init__() takes at least 4 arguments (3 given)

This error indicates that for standard 1-D data sets (the default), at least two columns are needed for basic 1-D fitting. The load_data() function expects at least two columns specified with DM syntax, assuming the arrays are to be read into a Sherpa data set.

The exceptions to the rule are the functions load_table_model() and load_user_model(), which can each accept a single column of data.

For example:

sherpa> load_table_model('tbl', 'onetab.dat')
sherpa> tbl
<TableModel model instance 'tablemodel.tbl'>
sherpa> tbl._y
          array([  0.5,   1.5,   2.5,   3.5,   4.5,   5.5,   6.5,   7.5,   9.5,  10.5])
sherpa-6> print tbl
tablemodel.tbl
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   tbl.ampl     thawed            1 -3.40282e+38  3.40282e+38 

and

sherpa> def foo(p, *args, **kwargs):
            return p[0]*args[0]

sherpa> load_user_model(foo, "mdl", "onetab.dat")
sherpa> print mdl
usermodel.mdl
   Param        Type          Value          Min          Max      Units
   -----        ----          -----          ---          ---      -----
   mdl.ampl     thawed            1 -3.40282e+38  3.40282e+38