# Is it possible to export to XYZ trajectory file from TrajectoryData?

**URL:** <https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 5, 2023, 2:54pm UTC](https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73 "2023-09-05T14:54:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bastonero](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/bastonero/32/32_2.png) [@bastonero](https://aiida.discourse.group/u/bastonero)\
**Post date:** [September 5, 2023, 2:54pm UTC](https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73/1 "2023-09-05T14:54:30Z")

</div>

I am trying to understand whether there is a built-in aiida function that exports TrajectoryData info to XYZ format (or similar). By far, I haven’t found much. Any idea or pointer, or shall one write a custom function?  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![sphuber](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/sphuber/32/6_2.png) [@sphuber](https://aiida.discourse.group/u/sphuber)\
**Post date:** [September 6, 2023, 10:58am UTC](https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73/2 "2023-09-06T10:58:53Z")

</div>

I see two options:

- `TrajectoryData.get_positions()` together with `TrajectoryData.symbols` gives you all information and then you format it manually
- `TrajectoryData.get_structure()` to get a `StructureData` which comes with `_prepare_xyz` to return it in XYZ format.

Think latter is easier as you don’t reimplement formatting yourself, but not sure about performance of having to go to `StructureData` as intermediate.

---

<div class="post-metadata">

**Author:** ![bastonero](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/bastonero/32/32_2.png) [@bastonero](https://aiida.discourse.group/u/bastonero)\
**Post date:** [September 6, 2023, 11:33am UTC](https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73/3 "2023-09-06T11:33:34Z")

</div>

Thanks Sebastian!  
Since I needed energy, forces, and stresse for training such data, I found this solution:

```python
from qe_tools import CONSTANTS as C
from ase.io import write
from ase import units
from ase.calculators.singlepoint import SinglePointCalculator

filename = './dataset.xyz'
stress_units = (-1 * units.Ry / units.Bohr**3)/(C.ry_si / C.bohr_si**3 / 10**9) # convention as in ASE (sign and eV/Ang^3)

index = 0
atoms = traj.get_step_structure(index).get_ase()
energy = traj.get_array('energy')[index]
s = traj.get_array('stress')[index]

calc = SinglePointCalculator(atoms)

calc.results = {
    'energy': energy,
    'free_energy': energy,
    'forces': traj.get_array('forces')[index],
    'stress': stress_units*np.array([s[0,0],s[1,1],s[2,2],s[1,2],s[0,2],s[0,1]]),
}

atoms.calc = calc

write(filename, atoms, format='extxyz', append=True)

```

I think one can also collect in a single calculator more than one “Atoms”, and then write it once instead of looping each index. Happy to receive suggestions here on how to optimize (:

---

<div class="post-metadata">

**Author:** ![danielhollas](https://yyz2.discourse-cdn.com/free1/user_avatar/aiida.discourse.group/danielhollas/32/4_2.png) [@danielhollas](https://aiida.discourse.group/u/danielhollas)\
**Post date:** [September 6, 2023, 4:52pm UTC](https://aiida.discourse.group/t/is-it-possible-to-export-to-xyz-trajectory-file-from-trajectorydata/73/4 "2023-09-06T16:52:56Z")

</div>

I struggled with this as well, and in the end used the `get_step_structure`. It might be nice if `TrajectoryData` had the `get_ase()` method similarly to `StructureData`.

Another quality-of-life improvement would be if `verdi data core.trajectory export` supported the extended XYZ format.
