def appendHeader(self, dtype, nelem, ncomp): """ This function only writes the size of the data block that will be appended. The data itself must be written immediately after calling this function. PARAMETERS: dtype: string with data type representation (same as numpy). For example, 'float64' nelem: number of elements. ncomp: number of components, 1 (=scalar) or 3 (=vector). """ self.openAppendedData() dsize = np_to_vtk[dtype].size block_size = dsize * ncomp * nelem if self.largeFile == False: writeBlockSize(self.xml.stream, block_size) else: writeBlockSize64Bit(self.xml.stream, block_size)
def appendData(self, data): """ Append data to binary section. This function writes the header section and the data to the binary file. PARAMETERS: data: one numpy array or a tuple with 3 numpy arrays. If a tuple, the individual arrays must represent the components of a vector field. All arrays must be one dimensional or three-dimensional. The order of the arrays must coincide with the numbering scheme of the grid. RETURNS: This VtkFile to allow chained calls TODO: Extend this function to accept contiguous C order arrays. """ self.openAppendedData() if type(data).__name__ == 'tuple': # 3 numpy arrays ncomp = len(data) assert (ncomp == 3) dsize = data[0].dtype.itemsize nelem = data[0].size block_size = ncomp * nelem * dsize #if self.largeFile == False: writeBlockSize(self.xml.stream, block_size) #else: # writeBlockSize64Bit(self.xml.stream, block_size) x, y, z = data[0], data[1], data[2] writeArraysToFile(self.xml.stream, x, y, z) elif type(data).__name__ == 'ndarray' and (data.ndim == 1 or data.ndim == 3): # single numpy array ncomp = 1 dsize = data.dtype.itemsize nelem = data.size block_size = ncomp * nelem * dsize #if self.largeFile == False: writeBlockSize(self.xml.stream, block_size) #else: # writeBlockSize64Bit(self.xml.stream, block_size) writeArrayToFile(self.xml.stream, data) else: assert False return self