# ParaView struggling with negative values (need to use logarithms)

**URL:** https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211
**Category:** ParaView Support
**Tags:** vtk
**Created:** [August 29, 2022, 1:26pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211 "2022-08-29T13:26:27Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![WarinSpace\_Game](https://discourse.paraview.org/user_avatar/discourse.paraview.org/warinspace_game/32/9808_2.png) [@WarinSpace\_Game](https://discourse.paraview.org/u/WarinSpace_Game)
#### Post date: [August 29, 2022, 1:26pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211/1 "2022-08-29T13:26:27Z")

</div>

I’ve created a little Python script to produce VTK files from my data (3D spherical → XYZ). The plot looks OK when I use linear scaling:

 ![image](https://discourse.paraview.org/uploads/default/original/2X/1/1c70069395693ee27d74836639a96e9a54641d75.jpeg)

**VTK file:** [pattern\_linear.vtk](https://discourse.paraview.org/uploads/short-url/fdhvdWXcrzTSG0faPp1iuo6bQSP.vtk) (423.2 KB)

However, if I convert the values to dB (i.e. use `log` when exporting to VTK), a lot of values will be negative. For some reason, ParaView does not seem to like this, and gives a very different shape:

 ![image](https://discourse.paraview.org/uploads/default/original/2X/b/b7b25604b239e6b82acaa610b5626f18190b1dd8.jpeg)

**VTK file:** [pattern\_dB.vtk](https://discourse.paraview.org/uploads/short-url/2yPyEoWtlJ9A34pbo86LlYaTxbN.vtk) (425.6 KB)

Why is that? Is there a way to help ParaView properly parse the shape I want, and not struggle with negative values? Could I change something in the VTK files?

**Edit:** using `paraview version 5.10.0-RC1`

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [August 29, 2022, 1:27pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211/2 "2022-08-29T13:27:46Z")

</div>

ParaView only display the points where you place them, you probably have an issue in your dataset generation.

---

<div class="post-metadata">

### Author: ![WarinSpace\_Game](https://discourse.paraview.org/user_avatar/discourse.paraview.org/warinspace_game/32/9808_2.png) [@WarinSpace\_Game](https://discourse.paraview.org/u/WarinSpace_Game)
#### Post date: [August 29, 2022, 1:30pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211/3 "2022-08-29T13:30:31Z")

</div>

@mwestphal Thanks for your input! Notice anything obvious that would fail the (r, θ, φ) → (x, y, z) conversion for negative _r_ values (`farfield`) only?

```python
def DumpFF2VTK(filename, farfield, thetaRange, phiRange, scale=1):
    try:
        os.remove(filename)
    except OSError:
        pass
        
    t = thetaRange*math.pi/180
    a = phiRange*math.pi/180

    with open(filename, "w+") as fid:        
        fid.write("# vtk DataFile Version 3.0\n")
        fid.write("Structured Grid by matlab-interface of openEMS\n")
        fid.write("ASCII\n")
        fid.write("DATASET STRUCTURED_GRID\n")
        fid.write("DIMENSIONS 1 "+str(int(len(t)))+" "+str(int(len(a)))+"\n")
        fid.write("POINTS "+str(int(len(t)*len(a)))+" double\n")

        for na in range(len(phiRange)):
            for nt in range(len(thetaRange)):
                fid.write(str("{:e}".format(scale*farfield[nt,na]*sin(t[nt])*cos(a[na])))+" "+str("{:e}".format(scale*farfield[nt,na]*sin(t[nt])*sin(a[na])))+" "+str("{:e}".format(scale*farfield[nt,na]*cos(t[nt])))+"\n")

        fid.write("\n\n")
        fid.write("POINT_DATA "+str(int(len(t)*len(a)))+"\n")
        fid.write("SCALARS gain double 1\nLOOKUP_TABLE default\n")

        dumpField = farfield.flatten("F")

        for i in dumpField:
            fid.write(str("{:e}".format(i))+"\n")

```

---

<div class="post-metadata">

### Author: ![WarinSpace\_Game](https://discourse.paraview.org/user_avatar/discourse.paraview.org/warinspace_game/32/9808_2.png) [@WarinSpace\_Game](https://discourse.paraview.org/u/WarinSpace_Game)
#### Post date: [August 29, 2022, 1:55pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211/4 "2022-08-29T13:55:57Z")

</div>

Aha, looks like I got it. This fixed it - I basically add the absolute value of the minimum value of `r` to the `r` array conversion calculator, and now everything looks ok:

```python
        for na in range(len(phiRange)):
            for nt in range(len(thetaRange)):
                fid.write(str("{:e}".format(
                    (abs(np.min(farfield))+farfield[nt,na])*sin(t[nt])*cos(a[na])))+" "+
                    str("{:e}".format((abs(np.min(farfield))+farfield[nt,na])*sin(t[nt])*sin(a[na])))+" "+
                    str("{:e}".format((abs(np.min(farfield))+farfield[nt,na])*cos(t[nt])))+"\n"
                )

```

Thanks!

---

<div class="post-metadata">

### Author: ![WarinSpace\_Game](https://discourse.paraview.org/user_avatar/discourse.paraview.org/warinspace_game/32/9808_2.png) [@WarinSpace\_Game](https://discourse.paraview.org/u/WarinSpace_Game)
#### Post date: [August 29, 2022, 2:05pm UTC](https://discourse.paraview.org/t/paraview-struggling-with-negative-values-need-to-use-logarithms/10211/5 "2022-08-29T14:05:13Z")

</div>

@mwestphal Follow-up question:  
I currently write `SCALARS gain double 1` to the VTK file, and the colorbar displays that. Is it possible to include space characters in the name, like so?

```auto
SCALARS gain (dBi) double 1

```
