diff --git a/post/merge-scalar-data-arrays/README.md b/post/merge-scalar-data-arrays/README.md index 33513c1ea18b3d24f8b0e341a43ead097cc5f62a..8e713203a11a28ca837d203d1c8f2ab3d22cca64 100644 --- a/post/merge-scalar-data-arrays/README.md +++ b/post/merge-scalar-data-arrays/README.md @@ -1,5 +1,8 @@ # Merge two scalar data arrays -This script takes two scalar nodal fields from a vtu file and combines them to a +The script 'mergeDataArrays' takes two scalar nodal fields from a vtu file and combines them to a two-component vector field. +The script 'mergeDataArraysIntoOne' takes arbitrary number of scalar fields from +a vtu file and uses them as the components of a vector field that will be +created. diff --git a/post/merge-scalar-data-arrays/mergeDataArraysIntoOne.py b/post/merge-scalar-data-arrays/mergeDataArraysIntoOne.py new file mode 100755 index 0000000000000000000000000000000000000000..09945ab87713bb269ffd68839aa8605df379f7ea --- /dev/null +++ b/post/merge-scalar-data-arrays/mergeDataArraysIntoOne.py @@ -0,0 +1,62 @@ +# +# \copyright +# Copyright (c) 2012-2017, OpenGeoSys Community (http://www.opengeosys.org) +# Distributed under a Modified BSD License. +# See accompanying file LICENSE.txt or +# http://www.opengeosys.org/project/license +# + +#!/usr/bin/env python2 + +from vtk import * +from sys import argv, exit + +if len(argv) < 5: + print "Usage:", argv[0], "input.vtu scalar_array1 ... name_for_new_array output.vtu" + exit(1) + +input_file = argv[1] +output_file = argv[len(argv)-1] +print("Reading from", input_file) +print("Writing to", output_file) + +r = vtkXMLUnstructuredGridReader() +r.SetFileName(input_file) +r.Update() +m = r.GetOutput() + +cell_data = m.GetCellData() +tensor_arrays = [] + +number_of_components = len(argv) - 4 +number_of_tuples = cell_data.GetArray(argv[(2)]).GetNumberOfTuples() + +for i in range(0, number_of_components): + tensor_arrays.append(cell_data.GetArray(argv[(i)+2])) + +new_array = vtkDoubleArray() +new_array.SetNumberOfComponents(number_of_components) +new_array.SetNumberOfTuples((number_of_tuples)) +new_array.SetName(argv[len(argv)-2]) + +print "copy the particular cell data arrays to the cell data array ", argv[(len(argv)-2)] +for i in range(0, number_of_components): + new_array.CopyComponent(i, tensor_arrays[i], 0) + +print "add ", argv[(len(argv)-2)], " to the data arrays" +cell_data.AddArray(new_array) + +print "remove the not needed cell data arrays" + +for i in range(0, number_of_components): + print "remove the not needed cell data array", argv[(i)+2] + cell_data.RemoveArray(argv[(i)+2]) + +print "write result to file" + +w = vtkXMLUnstructuredGridWriter() +w.SetFileName(output_file) +w.SetInputData(m) +w.SetDataModeToAscii() +w.SetCompressorTypeToNone() +w.Update()