Export a Datafield

Save in various formats

A Datafield can be saved in various data formats through the BornAgain Python method

import bornagain as ba
simulation = ...
result = simulation.simulate()
ba.IOFactory.writeDatafield(result, file_name)

where file_name must have one of the extensions

  • .csv or .txt — ASCII table (see CSV format below),
  • .int — BornAgain native format (see INT format below),
  • .tif — 32-bits TIFF file,

to which one may append a second extension that specifies compression:

  • .gz,
  • .bz2.

File formats

CSV format

The CSV format (.csv, .txt, or any other extension) is a simple ASCII table suitable for import into NumPy, MATLAB, etc. For 2D data, the file contains:

# BornAgain Intensity Data
# axis-0 (columns): EquiDivision("q_y (1/nm)", 100, -1.5, 1.5)
# axis-1 (rows): EquiDivision("q_z (1/nm)", 100, 0, 3)
1.234e+00    2.345e+00    ...
...

The comment lines document the axis definitions. Data is stored row-major (each line is one row). This format does not preserve error bars.

INT format

The INT format (.int) is BornAgain’s native ASCII format with full metadata:

# BornAgain Intensity Data

# axis-0
EquiDivision("q_y (1/nm)", 100, -1.5, 1.5)

# axis-1
EquiDivision("q_z (1/nm)", 100, 0, 3)

# data
...

# errorbars
...

This format preserves axis type (EquiDivision, ListScan, etc.), labels, and error bars. Use this format for archiving simulation results.

Export to NumPy

To extract data from Datafield in form of NumPy arrays, use

result = simulation.simulate()
arr_x  = result.xCenters()  # centers of x bins
arr_i  = result.dataArray() # intensities
arr_di = result.errors()    # error estimates thereof

To save any such array, use the NumPy method

numpy.savetxt("intensity.txt", arr)