[docs]defget_selected_array_name(algorithm,idx):"""Gets the name of the input array for a given index on a VTK algorithm Args: algorithm (vtkAlgorithm): A vtkAlgorithm class instantiation idx (int): the input array index Return: str : the name of the input array for the given index """info=algorithm.GetInputArrayInformation(idx)returninfo.Get(vtk.vtkDataObject.FIELD_NAME())
[docs]defget_selected_array_field(algorithm,idx):"""Gets the field of the input array for a given index on a VTK algorithm Args: algorithm (vtkAlgorithm) : A vtkAlgorithm class instantiation idx (int) : the input array index Return: int : the field type of the input array for the given index """info=algorithm.GetInputArrayInformation(idx)returninfo.Get(vtk.vtkDataObject.FIELD_ASSOCIATION())
defget_field_id_by_name(field):"""Get the field ID by name."""fields=dict(point=0,pt=0,p=0,cell=1,c=1,field=2,f=2,row=6,r=6,)field=field.lower()try:returnfields[field]exceptKeyError:raise_helpers.PVGeoError('Field association not defined. Try inputing `point`, `cell`, `field`, or `row`.')
[docs]defcopy_arrays_to_point_data(pdi,pdo,field):"""Copies arrays from an input to an ouput's point data. Args: pdi (vtkDataObject) : The input data object to copy from pdo (vtkDataObject) : The output data object to copy over to field (int or str) : the field type id or name Return: vtkDataObject : returns the output data object parameter """ifisinstance(field,str):field=get_field_id_by_name(field)# Point Dataiffield==0:foriinrange(pdi.GetPointData().GetNumberOfArrays()):arr=pdi.GetPointData().GetArray(i)pdo.GetPointData().AddArray(arr)# Cell Data: DO NOT USEeliffield==1:foriinrange(pdi.GetCellData().GetNumberOfArrays()):arr=pdi.GetCellData().GetArray(i)pdo.GetPointData().AddArray(arr)# Field Data:eliffield==2:foriinrange(pdi.GetFieldData().GetNumberOfArrays()):arr=pdi.GetFieldData().GetArray(i)pdo.GetPointData().AddArray(arr)# Row Data:eliffield==6:foriinrange(pdi.GetRowData().GetNumberOfArrays()):arr=pdi.GetRowData().GetArray(i)pdo.GetPointData().AddArray(arr)else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))# Field Datareturnpdo
[docs]defget_numpy_array(wpdi,field,name):"""Grabs an array from vtkDataObject given its name and field association. Args: wpdi (wrapped vtkDataObject) : the input data object wrapped using vtk dataset adapter field (int or str) : the field type id or name name (str) : the name of the input array for the given index Return: numpy.array : a wrapped ``vtkDataArray`` for NumPy """ifisinstance(field,str):field=get_field_id_by_name(field)ifnotisinstance(wpdi,vtk.numpy_interface.dataset_adapter.DataObject):wpdi=dsa.WrapDataObject(wpdi)# Point Dataiffield==0:arr=wpdi.PointData[name]# Cell Data:eliffield==1:arr=wpdi.CellData[name]# Field Data:eliffield==2:arr=wpdi.FieldData[name]# Row Data:eliffield==6:arr=wpdi.RowData[name]else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))returnarr
[docs]defget_vtk_array(pdi,field,name):"""Grabs an array from vtkDataObject given its name and field association. Args: pdi (vtkDataObject) : the input data object field (int or str) : the field type id or name name (str) : the name of the input array for the given index Return: vtkDataArray : the array from input field and name """ifisinstance(field,str):field=get_field_id_by_name(field)# Point Dataiffield==0:arr=pdi.GetPointData().GetArray(name)# Cell Data:eliffield==1:arr=pdi.GetCellData().GetArray(name)# Field Data:eliffield==2:arr=pdi.GetFieldData().GetArray(name)# Row Data:eliffield==6:arr=pdi.GetRowData().GetArray(name)else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))returnarr
[docs]defget_selected_array(algorithm,wpdi,idx):"""Gets selectected array at index idx wrapped for NumPy Args: algorithm (vtkAlgorithm) : A vtkAlgorithm class instantiation wpdi (wrapped vtkDataObject) : the input data object wrapped using vtk dataset adapter idx (int) : the input array index Return: numpy.array : a wrapped ``vtkDataArray`` for NumPy """name=get_selected_array_name(algorithm,idx)field=get_selected_array_field(algorithm,idx)returnget_array(wpdi,field,name)
[docs]defadd_array(pdo,field,vtkArray):"""Adds an array to a vtkDataObject given its field association. Args: pdo (vtkDataObject) : the output data object field (int or str) : the field type id or name vtkArray (vtkDataArray) : the data array to add to the output Return: vtkDataObject : the output data object with the data array added """ifisinstance(field,str):field=get_field_id_by_name(field)# Point Dataiffield==0:pdo.GetPointData().AddArray(vtkArray)# Cell Data:eliffield==1:pdo.GetCellData().AddArray(vtkArray)# Field Data:eliffield==2:pdo.GetFieldData().AddArray(vtkArray)# Row Data:eliffield==6:pdo.GetRowData().AddArray(vtkArray)else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))returnpdo
def_get_data(pdi,field):"""Gets data field from input vtkDataObject"""data=Noneifisinstance(field,str):field=get_field_id_by_name(field)try:# Point Dataiffield==0:data=pdi.GetPointData()# Cell Data:eliffield==1:data=pdi.GetCellData()# Field Data:eliffield==2:data=pdi.GetFieldData()# Row Data:eliffield==6:data=pdi.GetRowData()else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))exceptAttributeError:raise_helpers.PVGeoError('Input data does not have field type `{}`.'.format(field))returndatadefget_array(pdi,field,name):"""Gets an array from a vtkDataObject given its field association and name. Notes: - Point Data: 0 - Cell Data: 1 - Field Data: 2 - Row Data: 6 Args: pdi (vtkDataObject) : the input data object field (int or str) : the field type id or name name (str) : the data array name Return: vtkDataObject: the output data object """ifisinstance(field,str):field=get_field_id_by_name(field)data=_get_data(pdi,field)returndata.GetArray(name)
[docs]defsearch_for_array(pdi,name):def_search_field(field):data=_get_data(pdi,field)foriinrange(data.GetNumberOfArrays()):ifdata.GetArrayName(i)==name:returndata.GetArray(i)returnNonefields=[0,1,2,6]forfieldinfields:try:arr=_search_field(field)except_helpers.PVGeoError:continueifarrisnotNone:# We found it!returnarr,fieldraise_helpers.PVGeoError('Array `{}` not found in input data.'.format(name))returnNone
[docs]defget_all_array_names(dataset,field):ifisinstance(field,str):field=get_field_id_by_name(field)ifnotisinstance(dataset,vtk.numpy_interface.dataset_adapter.DataObject):wpdi=dsa.WrapDataObject(dataset)else:wpdi=dataset# Point Dataiffield==0:returnwpdi.PointData.keys()# Cell Data:eliffield==1:returnwpdi.CellData.keys()# Field Data:eliffield==2:returnwpdi.FieldData.keys()# Row Data:eliffield==6:returnwpdi.RowData.keys()else:raise_helpers.PVGeoError('Field association ({}) not defined. Try inputing Point, Cell, Field, or Row data.'.format(field))returnNone