# Vector field from three scalar components

**URL:** https://discourse.vtk.org/t/vector-field-from-three-scalar-components/4959
**Category:** Support
**Created:** [January 6, 2021, 7:20am UTC](https://discourse.vtk.org/t/vector-field-from-three-scalar-components/4959 "2021-01-06T07:20:23Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![razoumov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/razoumov/32/2825_2.png) [@razoumov](https://discourse.vtk.org/u/razoumov)
#### Post date: [January 6, 2021, 7:20am UTC](https://discourse.vtk.org/t/vector-field-from-three-scalar-components/4959/1 "2021-01-06T07:20:23Z")

</div>

Hello,

I have three vtkDoubleArray 3D scalar fields in my Python VTK pipeline:

```auto
bx = resample.GetOutput().GetPointData().GetArray("Bx")
by = resample.GetOutput().GetPointData().GetArray("By")
bz = resample.GetOutput().GetPointData().GetArray("Bz")

```

How do I create a single vector field from these three components, without cycling through all data points explicitly (these arrays are large)? I assume I would start with

```auto
vector = vtk.vtkFloatArray()
vector.SetName("B")
vector.SetNumberOfComponents(3)

```

How do I specify the components?

Thank you.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 6, 2021, 2:39pm UTC](https://discourse.vtk.org/t/vector-field-from-three-scalar-components/4959/2 "2021-01-06T14:39:00Z")

</div>

Probably you can use [vtkImageAppendComponents](https://vtk.org/doc/nightly/html/classvtkImageAppendComponents.html).

You can also do this using numpy: get the 3 arrays as numpy arrays, concatenate them using numpy, and then use that to update a VTK array.

These vector operations are generally about 100x faster than iterating through elements one by one in Python.

---

<div class="post-metadata">

### Author: ![razoumov](https://discourse.vtk.org/user_avatar/discourse.vtk.org/razoumov/32/2825_2.png) [@razoumov](https://discourse.vtk.org/u/razoumov)
#### Post date: [January 7, 2021, 1:50am UTC](https://discourse.vtk.org/t/vector-field-from-three-scalar-components/4959/3 "2021-01-07T01:50:53Z")

</div>

Thank you, Andras. I went the numpy route:

```auto
... some code to read data from file ...

data = tableToPoints.GetOutput().GetPointData()
bx = data.GetArray("Bx")
by = data.GetArray("By")
bz = data.GetArray("Bz")

ax = vtk_to_numpy(bx).reshape(nx, ny, nz, order='F')
ay = vtk_to_numpy(by).reshape(nx, ny, nz, order='F')
az = vtk_to_numpy(bz).reshape(nx, ny, nz, order='F')

vtype = vtk.util.numpy_support.get_vtk_array_type(ax.dtype)
vcomponents = 3

imageData = vtk.vtkImageData()
imageData.SetSpacing(1.0, 1.0, 1.0)
imageData.SetOrigin(0.0, 0.0, 0.0)
imageData.SetDimensions(nz, ny, nx)
imageData.AllocateScalars(vtype, vcomponents)

aaa = np.array([ax,ay,az]).transpose()
vtk_data_array = numpy_to_vtk(num_array=aaa.ravel(), deep=True, array_type=vtype)
vtk_data_array.SetNumberOfComponents(3)
vtk_data_array.SetName("B")
imageData.GetPointData().SetScalars(vtk_data_array)

writer = vtk.vtkXMLImageDataWriter()
writer.SetInputData(imageData)
writer.SetFileName(fileout)
writer.Write()

```
