1#!/usr/local/bin/python3.8
2from gi.repository import v_sim
3import traceback
4
5def parseCNT(format, filename, data, iset, cancel):
6  print "test file: %s for VisuData: %s(%d)" % (filename, data, iset)
7  valid = False
8  try:
9    # Read the given file.
10    f = open(filename, "r")
11    desc = f.readline()
12    f.readline()
13    box = map(float, f.readline().split()) + \
14          map(float, f.readline().split()) + \
15          map(float, f.readline().split())
16    if len(box) != 9:
17      raise ValueError
18    elements = f.readline().split()
19    nElements = map(int, f.readline().split())
20    # From here, we consider that this file is indeed a VASP file.
21    valid = True
22    f.readline()
23    f.readline()
24    coords = []
25    for line in f.xreadlines():
26      coords.append(map(float, line.split()[:3]))
27    f.close()
28    allElements = []
29    for (ele, n) in zip(elements, nElements):
30      allElements += [ele, ] * n
31
32    # Pass the variables to V_Sim.
33    # Declare one set of data for this file.
34    data.setNSubset(1)
35    # Setup the box geomtry.
36    box = v_sim.Box.new_full(box, v_sim.BoxBoundaries.PERIODIC)
37    # Setting up the box geometry here make available to use the
38    # converting routines to go from reduced coordinates to cartesian.
39    data.setBox(box)
40    # Allocate the internal arrays for these elemnts and number of nodes.
41    data.allocateByNames(nElements, elements)
42    for (coord, ele) in zip(coords, allElements):
43      # For each node, add it with its coordinates
44      data.addNodeFromElementName(ele, coord, False);
45    # Set a commentry for this subset of data.
46    data.setDescription(desc.strip(), 0)
47
48    return True
49  except Exception as error:
50    traceback.print_exc()
51    return valid
52  return False
53
54# Declare this new fileformat to V_Sim.
55fmt = v_sim.DataLoader.new("VASP CNT files", ("*.cnt", "cnt.*"), False, parseCNT, 95)
56v_sim.DataAtomicClass.addLoader(fmt)
57