+++
title = "Export"
weight = 20
+++

## Export a Datafield

#### Save in various formats

A `Datafield` can be saved in various data formats
through the BornAgain Python method
```python
import bornagain as ba
simulation = ...
result = simulation.simulate()
ba.writeDatafield(result, file_name)
```
where `file_name` must have one of the extensions
- `.csv` or `.txt` --- ASCII table (see [CSV format](#csv-format) below),
- `.int` --- BornAgain native format (see [INT format](#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
```python
result = simulation.simulate()
arr_x  = result.xCenters()     # centers of x bins
arr_y  = result.yCenters()     # centers of y bins (2D data)
arr_i  = result.intensities()  # intensities
arr_di = result.errors()       # error estimates thereof
```
To save any such array, use the NumPy method
```python
numpy.savetxt("intensity.txt", arr)
```
