Skip to content

Commit

Permalink
v0.3 Feature Additions (#3)
Browse files Browse the repository at this point in the history
* Added `getArrayList()` to `VTKGrid`.
* Added `getNX()`, `getNY()`, `getNZ()` to `VTKGrid`
  • Loading branch information
dgaylo authored Dec 5, 2023
1 parent 8c8e5c4 commit 524b87a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions tests/test_vtkgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ def myVTKGrid(myVtkRectGrid) -> VTKGrid:

def test_getDimensions(myVTKGrid, myGrid):
assert myVTKGrid.getDimensions(0) == myGrid.nx()
assert myVTKGrid.getNX() == myGrid.nx()

assert myVTKGrid.getDimensions(1) == myGrid.ny()
assert myVTKGrid.getNY() == myGrid.ny()

assert myVTKGrid.getDimensions(2) == myGrid.nz()
assert myVTKGrid.getNZ() == myGrid.nz()

assert myVTKGrid.getDimensions() == [myGrid.nx(),myGrid.ny(),myGrid.nz()]

# For getting coordinates
Expand Down Expand Up @@ -67,6 +73,10 @@ def test_getExtentsZ(myVTKGrid, myGrid):
def test_getArrayNone(myVTKGrid):
assert myVTKGrid.getArray(None) is None

# test getting list of arrays
def test_getArrayList(myVtkRectGrid_V):
assert VTKGrid(myVtkRectGrid_V).getArrayList() == ["V"]

# test reading scalar data
def test_getArrayScalar(myVtkRectGrid_S,myScalarData):
s=VTKGrid(myVtkRectGrid_S).getArray("S")
Expand Down
30 changes: 30 additions & 0 deletions vtktonumpy/vtkgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def getDimensions(self, d=None):
else:
return self.dims[d]

def getNX(self) -> int:
"""
$N_x$, the number of cells in $x$ direction
"""
return self.dims[0]

def getNY(self) -> int:
"""
$N_y$, the number of cells in $y$ direction
"""
return self.dims[1]

def getNZ(self) -> int:
"""
$N_z$, the number of cells in $z$ direction
"""
return self.dims[2]

# For getting coordinates
def getXCoordinates(self) -> np.ndarray:
"""
Expand Down Expand Up @@ -173,6 +191,18 @@ def getExtentsZ(self) -> tuple:
vtk_to_numpy(self.vtk_grid.GetZCoordinates())
)

# For data arrays
def getArrayList(self):
"""
list[str]: A list of cell arrays present in the VTK data
"""
out = []
cell_data = self.vtk_grid.GetCellData()
for i in range(cell_data.GetNumberOfArrays()):
out.append(cell_data.GetArrayName(i))

return out

def getArray(self, name: str) -> np.ndarray:
r"""
Get VTK array as a numpy array. If no such array exists, return :obj:`None`
Expand Down
8 changes: 1 addition & 7 deletions vtktonumpy/vtkreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,7 @@ def removeArray(self, array) -> None:

def getArrayList(self):
"""
Returns a list of arrays present in the VTK data
Parameters:
none
Returns:
list[str]: a list of available cell arrays
list[str]: A list of cell arrays present in the VTK data
"""
out = []
for i in range(self.vtk_reader.GetNumberOfCellArrays()):
Expand Down

0 comments on commit 524b87a

Please sign in to comment.