From a0887c3aebabaf6dd96f48ca2eaa0240b159ac10 Mon Sep 17 00:00:00 2001
From: Ask Hjorth Larsen <asklarsen@gmail.com>
Date: Wed, 18 Apr 2018 09:54:04 +0200
Subject: [PATCH] add shortcut functions to add metadata directly from ase
 Atoms objects or cube files

---
 common/python/nomadcore/atoms2nomad.py | 27 +++++++++++++++++
 common/python/nomadcore/cube_reader.py | 40 ++++++++++++++++++++++++++
 2 files changed, 67 insertions(+)
 create mode 100644 common/python/nomadcore/atoms2nomad.py
 create mode 100644 common/python/nomadcore/cube_reader.py

diff --git a/common/python/nomadcore/atoms2nomad.py b/common/python/nomadcore/atoms2nomad.py
new file mode 100644
index 0000000..95fc97f
--- /dev/null
+++ b/common/python/nomadcore/atoms2nomad.py
@@ -0,0 +1,27 @@
+import numpy as np
+
+from nomadcore.unit_conversion.unit_conversion import convert_unit
+
+
+def ase_atoms_to_section_system(backend, atoms, new_section=True):
+    """Add ASE Atoms object as metainfo to section_system.
+
+    If new_section is True, open and close a new section_system,
+    returning its gIndex."""
+
+    if new_section:
+        gIndex = backend.openSection('section_system')
+
+    backend.addArrayValues('atom_labels',
+                           np.array(atoms.get_chemical_symbols()))
+    backend.addArrayValues('atom_positions',
+                           convert_unit(atoms.positions, 'angstrom'))
+    backend.addArrayValues('simulation_cell',
+                           convert_unit(atoms.cell, 'angstrom'))
+    backend.addArrayValues('configuration_periodic_dimensions',
+                           np.array(atoms.pbc))
+
+    # Return system ref if we opened it, else None:
+    if new_section:
+        backend.closeSection('section_system', gIndex)
+        return gIndex
diff --git a/common/python/nomadcore/cube_reader.py b/common/python/nomadcore/cube_reader.py
new file mode 100644
index 0000000..5aa48c6
--- /dev/null
+++ b/common/python/nomadcore/cube_reader.py
@@ -0,0 +1,40 @@
+import numpy as np
+from ase.io import read
+
+from nomadcore.atoms2nomad import ase_atoms_to_section_system
+
+class CubeError(OSError):
+    pass
+
+
+def read_cube_file(backend, file_name):
+    try:
+        d = read(file_name, format = 'cube', full_output = True)
+
+    except Exception as err:
+        raise CubeError(err)
+
+    data = d['data']
+    atoms = d['atoms']
+    origin = d['origin']
+    nx, ny, nz = data.shape
+    displacements = np.array([atoms.cell[i]/data.shape[i] for i in range(3)])
+
+    system = ase_atoms_to_section_system(backend, atoms)
+
+    singleconfig = backend.openSection('section_single_configuration_calculation')
+    volumetric = backend.openSection('section_volumetric_data')
+
+    backend.addValue('volumetric_data_nx', nx)
+    backend.addValue('volumetric_data_ny', ny)
+    backend.addValue('volumetric_data_nz', nz)
+
+    backend.addArrayValues('volumetric_data_origin', origin)
+    backend.addArrayValues('volumetric_data_displacements', displacements)
+
+    backend.addValue('volumetric_data_multiplicity', 1)
+    backend.addArrayValues('volumetric_data_values', data[None])
+    backend.closeSection('section_volumetric_data', volumetric)
+
+    backend.addValue('single_configuration_calculation_to_system_ref', system)
+    backend.closeSection('section_single_configuration_calculation', singleconfig)
-- 
GitLab