API¶
A Python package to interpolate data from a NetCFD file onto a VTU file.
netcdf2vtu provides a the Mapper class to read in a NetCFD input file, a destination VTU file and to set some specifics for data interpolation.
- class netcdf2vtu.Mapper(nc_path, vtu_path, data_var_names, map_func_type, nc_crs, vtu_crs, nullvalue, **kwargs_nc)¶
Bases:
object
A class for interpolating data from a NetCFD file on a VTU file.
- Parameters:
nc_path (str) – The path to the NetCFD input file.
vtu_path (str) – The path to the destination VTU file.
data_var_names (list of str) – A list of variable names to read from the NetCFD file.
map_func_type (int) – The type of mapping function to use for interpolation.
nc_crs (str) – The coordinate reference system of the NetCFD file.
vtu_crs (str) – The coordinate reference system of the VTU file.
nullvalue (float) – The value to use for missing data points during interpolation.
kwargs_nc (dict) –
- Additional keyword arguments to pass to the NetCFD4 library when
reading the NetCFD file.
- get_nc_variables()¶
Returns a numpy.ndarray variable data of the NetCFD dataset.
- interpolate()¶
Interpolates the data from the NetCFD file onto the VTU file. It creates a vtkPolyData instance from self.nc[“dataset”] and interpolates this on a copy of an instance of vtkUnstructuredGrid created from the input VTU file.
- map(out_path, lat_name, lon_name, time_name=None)¶
A wrapper for simple interpolation.
- read_nc_coords()¶
Reads the latitude, longitude, and time coordinates from the NetCFD dataset in self.nc[“dataset”].
- read_nc_data()¶
Reads the data variables specified in self.nc[“data_names”] from the NetCFD dataset.
- set_nc_coord_names(lat_name, lon_name, time_name=None)¶
Sets the names of the latitude, longitude, and time variables to be read from the dataset in self.nc[“dataset”].
- set_nc_data_names(var_names)¶
Sets the names of the variables to be read from the NetCFD dataset in self.nc[“dataset”].
- Parameters:
var_names (list of str) – A list of variable names to read from the NetCFD file.
- write_out_vtu(path)¶
Writes the updated instance of vtkUnstructuredGrid to the specified path as VTU file.
- Parameters:
path (str) – The path of the VTU file to be written.
- write_vtp(path)¶
Writes the vtkPolyData instance created to the specified path as VTP file.
- Parameters:
path (str) – The path of the VTP file to be written.
- netcdf2vtu.create_vtp(lon_dat, lat_dat, nc_crs, vtu_crs)¶
- Initializes a vtkPolyData object and sets its points and cells based
on input latitude and longitude coordinates.
The point coordinates are transformed to the specified destination coordinate reference system.
initialize src_poly as vtkPolyData set points and cells for src_poly (vtkPolyData object where NetCFD data goes into) cells are vertice cells based on points point coordinates are transformed
- Parameters:
lon_dat (numpy.ndarray) – A numpy array of longitude coordinates.
lat_dat (numpy.ndarray) – A numpy array of latitude coordinates.
nc_crs (str) – The coordinate reference system of the input latitude and longitude coordinates.
vtu_crs (str) – The destination coordinate reference system to transform the point coordinates to.
- Returns:
A vtkPolyData object with points and cells set based on input coordinates.
- Return type:
vtk.vtkPolyData
- netcdf2vtu.gaussian_kernel()¶
Gaussian filter of point cloud defined by radius around a point.
- Returns:
The Gaussian filter interpolation kernel.
- Return type:
vtk.vtkGaussianKernel
- netcdf2vtu.get_nc_data(nc, data_var_names)¶
Extract variable data out of a netcdf4 dataset.
- Parameters:
nc (netCDF4.Dataset) – The NetCFD dataset to extract data from.
data_var_names (list of str) – A list of variable names to extract from nc.
- Returns:
A list of tuples, where each tuple contains a variable name and the corresponding data as a numpy array.
- Return type:
list
- netcdf2vtu.interpolate_vtp_data_on_vtu(vtp, vtu, map_func_type, nullvalue)¶
Maps the data of a vtkPolyData object onto a vtkUnstructuredGrid object using a specified interpolation algorithm.
- Parameters:
vtp (vtk.vtkPolyData) – The object that contains the data to be interpolated.
vtu (vtk.vtkUnstructuredGrid) – The object that the data will be interpolated onto.
map_func_type (int) – The type of mapping function to use for interpolation.
nullvalue (float) – The value to use for missing data points during interpolation.
- Returns:
An object with the interpolated data.
- Return type:
vtk.vtkUnstructuredGrid
- netcdf2vtu.nc_data_to_vtp(vtp, nc_data, time=None)¶
Adds extracted variable data from a NetCFD file to a vtkPolyData object.
- Parameters:
vtp (vtk.vtkPolyData) – The vtkPolyData object to add the NetCFD data to.
nc_data (list) – A list of tuples, where each tuple contains a variable name and its corresponding data as a numpy array.
time (numpy.ndarray, optional) – An array of time values for the NetCFD data. If not provided, the time value will be set to “FALSE”.
- netcdf2vtu.read_vtu(in_filepath)¶
Reads an OGS mesh file and returns it as a vtkUnstructuredGrid object.
- Parameters:
in_filepath (str) – The path of the VTU file to read.
- Returns:
The VTU file as a vtkUnstructuredGrid instance.
- Return type:
vtk.vtkUnstructuredGrid
- netcdf2vtu.set_map_function(map_func_type)¶
Returns the specified interpolation/mapping algorithm.
- Parameters:
map_func_type (int) – The type of mapping function to use for interpolation. 1: voronoi_kernel 2: gaussian_kernel 3: shepard_kernel
- Returns:
An object of the specified interpolation/mapping algorithm.
- Return type:
vtk.vtkInterpolationKernel
- netcdf2vtu.shepard_kernel()¶
Interpolation of a point cloud object defined by power function of the radius around a point.
- Returns:
The Shepard interpolation kernel.
- Return type:
vtk.vtkShepardKernel
- netcdf2vtu.voronoi_kernel()¶
Preferred for categorical data. Takes value of the closest point.
- Returns:
The voronoi interpolation kernel.
- Return type:
vtk.vtkVoronoiKernel
- netcdf2vtu.write_vtp(vtp, outputfile_name)¶
Writes a vtkPolyData object to a file.
- Parameters:
vtp (vtk.vtkPolyData) – The vtkPolyData object to write to a file.
outputfile_name (str) – The path of the file to write the vtkPolyData object to.
- netcdf2vtu.write_vtu(vtu, path)¶
Writes a vtkUnstructuredGrid object to a file.
- Parameters:
vtp (vtk.vtkUnstructuredGrid) – The vtkUnstructuredGrid object to write to a file.
outputfile_name (str) – The path of the VTU file to be written.