\(\renewcommand\AA{\unicode{x212B}}\)

SaveAscii v2

Summary

Saves a 2D workspace to a ascii file.

See Also

LoadAscii, SaveCSV, SaveReflectometryAscii, SaveOpenGenieAscii, SaveGSS, SaveFocusedXYE

Properties

Name

Direction

Type

Default

Description

InputWorkspace

Input

Workspace

Mandatory

The name of the workspace containing the data you want to save to a Ascii file.

Filename

Input

string

Mandatory

The filename of the output Ascii file. Allowed extensions: [‘.dat’, ‘.txt’, ‘.csv’]

WorkspaceIndexMin

Input

number

Optional

The starting workspace index. Ignored for Table Workspaces.

WorkspaceIndexMax

Input

number

Optional

The ending workspace index. Ignored for Table Workspaces.

SpectrumList

Input

int list

List of workspace indices to save. Ignored for Table Workspaces.

Precision

Input

number

Optional

Precision of output double values.

ScientificFormat

Input

boolean

False

If true, the values will be written to the file in scientific notation.

WriteXError

Input

boolean

False

If true, the error on X will be written as the fourth column. Ignored for Table Workspaces.

WriteSpectrumID

Input

boolean

True

If false, the spectrum No will not be written for single-spectrum workspaces. It is always written for workspaces with multiple spectra, unless spectrum axis value is written. Ignored for Table Workspaces.

CommentIndicator

Input

string

#

Character(s) to put in front of comment lines.

Separator

Input

string

CSV

The separator between data columns in the data file. The possible values are “CSV”, “Tab”, “Space”, “SemiColon”, “Colon” or “UserDefined”. Allowed values: [‘CSV’, ‘Tab’, ‘Space’, ‘Colon’, ‘SemiColon’, ‘UserDefined’]

CustomSeparator

Input

string

If present, will override any specified choice given to Separator.

ColumnHeader

Input

boolean

True

If true, put column headers into file. Even if false, a headeris automatically added if the workspace is Distribution = true.

SpectrumMetaData

Input

string

A comma separated list that defines data that describes each spectrum in a workspace. The valid options for this are: SpectrumNumber,Q,Angle. Ignored for Table Workspaces.

AppendToFile

Input

boolean

False

If true, don’t overwrite the file. Append to the end of it.

RaggedWorkspace

Input

boolean

True

If true, ensure that more than one xspectra is used. Ignored for Table Workspaces.

WriteSpectrumAxisValue

Input

boolean

False

Write the spectrum axis value if requested. Ignored for Table Workspaces.

LogList

Input

str list

List of logs to write to the file header. Ignored for Table Workspaces.

OneSpectrumPerFile

Input

boolean

False

If true, each spectrum will be saved to an individual file

Description

The format used differs based on the type of workspace being saved. For a table workspace the data will contain an optional row of column headers, followed by the row values, with each individual column value seperated by the defined seperator.

For a matrix workspace the data are stored in the file in columns: the first column contains the X-values, followed by pairs of Y and E values. Columns are separated by commas. The resulting file can normally be loaded into a workspace by the LoadAscii v2 algorithm.

There is limited support for MDHistoWorkspaces, and the algorithm only allows for the saving of 1D MDHisto Workspaces. This functionality is specifically used to save cuts made using sliceviewr. The saved format consists of a header comment that contains all of the workspace properties, followed by three separate columns corresponding to X, Y, and E, respectively.

It is possible to create a header for the ASCII file containing selected sample log entries and its unit, specified through LogList property. In case the requested log does not exist, it will be printed as Not defined. This feature is not enabled for TableWorkspace.

If the workspace (matrix workspace) contains several spectra, two options are available:

  • if OneSpectrumPerFile if false (default value), all spectra will be appended into the same file

  • if OneSpectrumPerFile is true, each spectrum will be written in a separate file. The name of the file will be created as follows: <Filename property>_ <spectrum index>_<axis value><axis unit>.<extension>

Limitations

The algorithm assumes that the workspace has common X values for all spectra (i.e. is not a ragged workspace )

Usage

Example - Save a workspace in CSV format

#import the os path libraries for directory functions
import os

# create histogram workspace
dataX1 = [0,1,2,3,4,5,6,7,8,9] # or use dataX1=range(0,10)
dataY1 = [0,1,2,3,4,5,6,7,8] # or use dataY1=range(0,9)
dataE1 = [1,1,1,1,1,1,1,1,1] # or use dataE1=[1]*9

ws1 = CreateWorkspace(dataX1, dataY1, dataE1)

#Create an absolute path by joining the proposed filename to a directory
#os.path.expanduser("~") used in this case returns the home directory of the current user
savefile = os.path.join(os.path.expanduser("~"), "AsciiFile.txt")

# perform the algorithm
SaveAscii(InputWorkspace=ws1,Filename=savefile)

print("File Exists: {}".format(os.path.exists(savefile)))

Output:

File Exists: True

Example - Save a workspace as ASCII with a different delimiter

#import the os path libraries for directory functions
import os

# create histogram workspace
dataX1 = [0,1,2,3,4,5,6,7,8,9] # or use dataX1=range(0,10)
dataY1 = [0,1,2,3,4,5,6,7,8] # or use dataY1=range(0,9)
dataE1 = [1,1,1,1,1,1,1,1,1] # or use dataE1=[1]*9

ws1 = CreateWorkspace(dataX1, dataY1, dataE1)

#Create an absolute path by joining the proposed filename to a directory
#os.path.expanduser("~") used in this case returns the home directory of the current user
savefile = os.path.join(os.path.expanduser("~"), "AsciiFile.txt")

# perform the algorithm
SaveAscii(InputWorkspace=ws1,Filename=savefile,Separator="Space")

print("File Exists: {}".format(os.path.exists(savefile)))

Output:

File Exists: True

Example - Save a workspace as ASCII with a different comment indicator

#import the os path libraries for directory functions
import os

# create histogram workspace
dataX1 = [0,1,2,3,4,5,6,7,8,9] # or use dataX1=range(0,10)
dataY1 = [0,1,2,3,4,5,6,7,8] # or use dataY1=range(0,9)
dataE1 = [1,1,1,1,1,1,1,1,1] # or use dataE1=[1]*9

ws1 = CreateWorkspace(dataX1, dataY1, dataE1)

#Create an absolute path by joining the proposed filename to a directory
#os.path.expanduser("~") used in this case returns the home directory of the current user
savefile = os.path.join(os.path.expanduser("~"), "AsciiFile.txt")

# perform the algorithm
# CommentIndicator can be changed, but when read back in must be specified
SaveAscii(InputWorkspace=ws1,Filename=savefile,CommentIndicator="!")

print("File Exists: {}".format(os.path.exists(savefile)))

Output:

File Exists: True

Categories: AlgorithmIndex | DataHandling\Text

Source

C++ header: SaveAscii2.h

C++ source: SaveAscii2.cpp