# How to Isosurface for nc/netcdf Data?

**URL:** https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448
**Category:** Support
**Created:** [January 17, 2020, 3:20pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448 "2020-01-17T15:20:27Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [January 17, 2020, 3:20pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/1 "2020-01-17T15:20:27Z")

</div>

Hi all,  
I am trying to read a .nc file with the vtkNetCDFCFReader and use a vtkContourFilter on it. But as soon as I use the vtkContourFilter I see nothing but a black window. What am I doing wrong?  
My Code:

```
string inputFilename = "C:\\snapshot_ql_00081.nc";

vtkSmartPointer<vtkNetCDFCFReader> cdfCFReader = vtkSmartPointer<vtkNetCDFCFReader>::New();
cdfCFReader->SetFileName(inputFilename.c_str());
cdfCFReader->UpdateInformation();
cdfCFReader->SetDimensions("z,y,x");
cdfCFReader->SetVariableArrayStatus("ql_3D", 1);

cdfCFReader->Update();

vtkSmartPointer<vtkContourFilter> contours = vtkSmartPointer<vtkContourFilter>::New();
contours->SetInputConnection(cdfCFReader->GetOutputPort());
contours->GenerateValues(10, 0, 0.257);
contours->Update();

vtkSmartPointer<vtkDataSetMapper> dataMapper = vtkSmartPointer<vtkDataSetMapper>::New();
dataMapper->SetInputConnection(contours->GetOutputPort());

vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(dataMapper);

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderer->AddActor(actor);
renderer->ResetCamera();
renWin->AddRenderer(renderer);

interactor->SetRenderWindow(renWin);

renWin->Render();
interactor->Start();

```

Here is the rendered Image with and without the vtkContoutFilter:

Without: ![withoutContour](https://discourse.vtk.org/uploads/default/original/2X/7/70d0ffaf8ca26ed0d4a7da994492e189dfa545d8.png)  
With: ![withContour](https://discourse.vtk.org/uploads/default/original/2X/f/f16de34b4884983b61cc2c4d7935fda6569fdd39.png)

If I open the same file in ParaView with Contours I get a different result:

 ![paraViewContours](https://discourse.vtk.org/uploads/default/original/2X/d/d22d664502024420e111129d3f70dda5d87628f2.png)

I can provide the test file if needed.  
Thanks for your help in advance.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 18, 2020, 10:49pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/2 "2020-01-18T22:49:32Z")

</div>

Hello, Alex,

According to the documentation of `vtkContourFilter`: [https://vtk.org/doc/nightly/html/classvtkContourFilter.html#details](https://vtk.org/doc/nightly/html/classvtkContourFilter.html#details), you need to compute the normals (with [vtkPolyDataNormals](https://vtk.org/doc/nightly/html/classvtkPolyDataNormals.html)) for the resulting `vtkPolyData`, assuming the output of `vtkNetCDFCFReader` is either a `vtkUnstructuredGrid` or a `vtkStructuredGrid`. Incorrect or null normals usually lead to problems with rendering.

all the best,

Paulo

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [January 21, 2020, 8:15am UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/3 "2020-01-21T08:15:43Z")

</div>

Hello Paulo,  
I get vtkImageDaten from the vtkCDFCFReader. I have tried to change the data type with  
`vtkCDFCFReader() -> SetOutputTypeToStructured();`  
and then calculating the normals with vtkPolyDateNormals, but unfortunately that didn’t work.  
Do you have an idea how it could work with the vtkImageDate and vtkContourFilter?  
Thanks for your help in advance.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 21, 2020, 10:41am UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/4 "2020-01-21T10:41:11Z")

</div>

Can you, please, post the strings returned by `cdfCFReader->GetAllDimensions()`?

According to the [documentation](https://vtk.org/doc/nightly/html/classvtkNetCDFReader.html#ae5ee7e14a911a9129366fbee87936cad), you could:

- Remove the call to `SetVariableArrayStatus()`;
- Change the call to `SetDimensions()` to `cdfCFReader->SetDimensions("z,y,x,ql_3D")`.

Alternativelly, try:

- Remove the call to `SetDimensions()`;
- Add a call to `SetDimensions()` for each variable:

```auto
cdfCFReader->SetVariableArrayStatus("x", 1);
cdfCFReader->SetVariableArrayStatus("y", 1);
cdfCFReader->SetVariableArrayStatus("z", 1);
cdfCFReader->SetVariableArrayStatus("ql_3D", 1);

```

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [January 21, 2020, 11:45am UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/5 "2020-01-21T11:45:31Z")

</div>

With `cdfCFReader->GetAllDimensions()`:

```
vtkSmartPointer<vtkStringArray> stringallDims = vtkSmartPointer<vtkStringArray>::New();
stringallDims = cdfCFReader->GetAllDimensions();

for (int i = 0; i < stringallDims->GetNumberOfValues(); i++) {
	cout << "This is Dim: "<< i << " " << stringallDims->GetValue(i) << endl;
}

```

I get the following output:  
“This is Dim: 0 (z,y,x)”

If I use `cdfCFReader->SetDimensions("z,y,x,ql_3D")` I get the same result with the black window like in the first post.  
But if I use

```auto
cdfCFReader->SetVariableArrayStatus("x", 1);
cdfCFReader->SetVariableArrayStatus("y", 1);
cdfCFReader->SetVariableArrayStatus("z", 1);
cdfCFReader->SetVariableArrayStatus("ql_3D", 1);

```

i get the following error messages:

```auto
Warning: In G:\VTK-8.2.0\src\IO\NetCDF\vtkNetCDFReader.cxx, line 823
vtkNetCDFCFReader (000000985E5EC5A0): Variable x dimensions (x) are different than the other variable dimensions (z y x). Skipping

Warning: In G:\VTK-8.2.0\src\IO\NetCDF\vtkNetCDFReader.cxx, line 823
vtkNetCDFCFReader (000000985E5EC5A0): Variable y dimensions (y) are different than the other variable dimensions (z y x). Skipping

Warning: In G:\VTK-8.2.0\src\IO\NetCDF\vtkNetCDFReader.cxx, line 823
vtkNetCDFCFReader (000000985E5EC5A0): Variable z dimensions (z) are different than the other variable dimensions (z y x). Skipping

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 21, 2020, 12:33pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/6 "2020-01-21T12:33:43Z")

</div>

Ok… can you try this:

```
cdfCFReader->SetVariableArrayStatus("z,y,x", 1);
cdfCFReader->SetVariableArrayStatus("ql_3D", 1);

```

?

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [January 21, 2020, 1:13pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/7 "2020-01-21T13:13:27Z")

</div>

If i do this I get a new error:

```
ERROR: In G:\VTK-8.2.0\src\IO\NetCDF\vtkNetCDFReader.cxx, line 202
vtkNetCDFCFReader (000000D3BD9EC5A0): netCDF Error: NetCDF: Variable not found

ERROR: In G:\VTK-8.2.0\src\Common\ExecutionModel\vtkExecutive.cxx, line 782
vtkCompositeDataPipeline (000000D3BD9ECC30): Algorithm vtkNetCDFCFReader(000000D3BD9EC5A0) returned failure for request: vtkInformation (000000D3BDE3D820)
  Debug: Off
  Modified Time: 1404
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_INFORMATION
  FORWARD_DIRECTION: 0
  ALGORITHM_AFTER_FORWARD: 1

ERROR: In G:\VTK-8.2.0\src\IO\NetCDF\vtkNetCDFReader.cxx, line 761
vtkNetCDFCFReader (000000D3BD9EC5A0): netCDF Error: NetCDF: Variable not found

ERROR: In G:\VTK-8.2.0\src\Common\ExecutionModel\vtkExecutive.cxx, line 782
vtkCompositeDataPipeline (000000D3BD9ECC30): Algorithm vtkNetCDFCFReader(000000D3BD9EC5A0) returned failure for request: vtkInformation (000000D3BDE3F2A0)
  Debug: Off
  Modified Time: 1660
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_DATA
  FORWARD_DIRECTION: 0
  ALGORITHM_AFTER_FORWARD: 1
  FROM_OUTPUT_PORT: 0
```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 21, 2020, 2:06pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/8 "2020-01-21T14:06:37Z")

</div>

It seems that your original code is correct afterall, based on the almost absent documentation of the `vtkNetCDFCFReader` class. In that case, I’d report a bug: [https://gitlab.kitware.com/vtk/vtk/issues](https://gitlab.kitware.com/vtk/vtk/issues)

I suspect it’s a bug because ParaView uses a custom class: [https://github.com/Kitware/ParaView/blob/df2aad3888107a6d61cd28bc755b535c7adcf471/Plugins/CDIReader/Reader/vtkCDIReader.h](https://github.com/Kitware/ParaView/blob/df2aad3888107a6d61cd28bc755b535c7adcf471/Plugins/CDIReader/Reader/vtkCDIReader.h)

Perhaps you could copy that over to your project (possibly along with some other source files) and give it a try while you wait for a feedback.

---

<div class="post-metadata">

### Author: ![Alex1](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/a/898d66/32.png) [@Alex1](https://discourse.vtk.org/u/Alex1)
#### Post date: [March 5, 2020, 6:52pm UTC](https://discourse.vtk.org/t/how-to-isosurface-for-nc-netcdf-data/2448/9 "2020-03-05T18:52:52Z")

</div>

I have a solution for my Problem to use `vtkAssignAttribute` and `vtkDataSetSurfaceFilter`  
Here the full code:

````auto
    vtkSmartPointer<vtkNetCDFCFReader> cdfCFReader = vtkSmartPointer<vtkNetCDFCFReader>::New();
    cdfCFReader->SetFileName(filenameNC.c_str());
    cdfCFReader->UpdateMetaData();
    cdfCFReader->SetVariableArrayStatus("ql_3D", 1);
    cdfCFReader->Update();

    vtkSmartPointer<vtkAssignAttribute> assignAttribute = vtkSmartPointer<vtkAssignAttribute>::New();
    assignAttribute->SetInputConnection(cdfCFReader->GetOutputPort());
    assignAttribute->Assign("ql_3D", "SCALARS", "POINT_DATA");
    
 
    vtkSmartPointer<vtkContourFilter> contour = vtkSmartPointer<vtkContourFilter>::New();
    contour->SetInputConnection(assignAttribute->GetOutputPort());
    contour->GenerateValues(10, 0.0, 0.257);

    vtkSmartPointer<vtkDataSetSurfaceFilter> surfaceFilter = vtkSmartPointer<vtkDataSetSurfaceFilter>::New();
    surfaceFilter->SetInputConnection(contour->GetOutputPort());

    vtkSmartPointer<vtkPolyDataMapper> polyMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    polyMapper->SetInputConnection(surfaceFilter->GetOutputPort());
    //polyMapper->SetScalarRange(0, 0.257);

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(polyMapper);

    vtkSmartPointer<vtkRenderer> = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
	
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(renderer);
    renWin->Render();```

![grafik|506x500](upload://nD7XXQUXjxx2xS9h2UN1uolBE4A.png)
````
