However, as cube files are human readable and quite easy to parse, you can always do the operations you need yourself. For example, the following Python3 script implements a class that can parse a density cube file:
import scipy as sp
class Cube:
def __init__(self, filename):
self.filename = filename
with open(self.filename, 'r') as f:
raw_data = f.readlines()
self.natoms = int(raw_data[2].split()[0])
self.nx, self.ny,
self.nz = [int(line.split()[0]) for line in raw_data[3:6]]
self.parse_density(raw_data)
print('Cube file {:s} parsed'.format(filename))
def parse_density(self, raw_data):
""" builds an nx times ny times nz array representing the spin density """
data = []
for line in raw_data[self.natoms + 6:]:
data.extend(float(x) for x in line.split())
self.density = sp.zeros((self.nx, self.ny,
self.nz))
for i in range(self.nx):
for j in range(self.ny):
for k in range(
self.nz):
self.density[i, j, k] = data[i * self.ny *
self.nz + j *
self.nz + k]
You can then calculate the average density by simply summing up the density array of each cube file.