{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load data using a Reader {#reader_example}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To have more control over reading data files, use a class based reader.\nThis class allows for more fine-grained control over reading datasets\nfrom files. See `pyvista.get_reader`{.interpreted-text role=\"func\"} for\na list of file types supported.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from tempfile import NamedTemporaryFile\n\nimport numpy as np\n\nimport pyvista\nfrom pyvista import examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An XML PolyData file in `.vtp` format is created. It will be saved in a\ntemporary file for this example.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "temp_file = NamedTemporaryFile('w', suffix=\".vtp\")\ntemp_file.name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`pyvista.Sphere`{.interpreted-text role=\"class\"} already includes\n`Normals` point data. Additionally `height` point data and `id` cell\ndata is added.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mesh = pyvista.Sphere()\nmesh['height'] = mesh.points[:, 1]\nmesh['id'] = np.arange(mesh.n_cells)\nmesh.save(temp_file.name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`pyvista.read`{.interpreted-text role=\"func\"} function reads all the\ndata in the file. This provides a quick and easy one-liner to read data\nfrom files.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "new_mesh = pyvista.read(temp_file.name)\nprint(f\"All arrays: {mesh.array_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using `pyvista.get_reader`{.interpreted-text role=\"func\"} enables more\nfine-grained control of reading data files. Reading in a `.vtp`[ file\nuses the :class:\\`pyvista.XMLPolyDataReader]{.title-ref}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader = pyvista.get_reader(temp_file.name)\nreader\n# Alternative method: reader = pyvista.XMLPolyDataReader(temp_file.name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some reader classes, including this one, offer the ability to inspect\nthe data file before loading all the data. For example, we can access\nthe number and names of point and cell arrays.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(f\"Number of point arrays: {reader.number_point_arrays}\")\nprint(f\"Available point data: {reader.point_array_names}\")\nprint(f\"Number of cell arrays: {reader.number_cell_arrays}\")\nprint(f\"Available cell data: {reader.cell_array_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can select which data to read by selectively disabling or enabling\nspecific arrays or all arrays. Here we disable all the cell arrays and\nthe `Normals` point array to leave only the `height` point array. The\ndata is finally read into a pyvista object that only has the `height`\npoint array.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader.disable_all_cell_arrays()\nreader.disable_point_array('Normals')\nprint(f\"Point array status: {reader.all_point_arrays_status}\")\nprint(f\"Cell array status: {reader.all_cell_arrays_status}\")\nreader_mesh = reader.read()\nprint(f\"Read arrays: {reader_mesh.array_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can reuse the reader object to choose different variables if needed.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader.enable_all_cell_arrays()\nreader_mesh_2 = reader.read()\nprint(f\"New read arrays: {reader_mesh_2.array_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some Readers support setting different time points or iterations. In\nboth cases, this is done using the time point functionality. The NACA\ndataset has two such points with density. This dataset is in EnSight\nformat, which uses the `pyvista.EnSightReader`{.interpreted-text\nrole=\"class\"} class.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filename = examples.download_naca(load=False)\nreader = pyvista.get_reader(filename)\ntime_values = reader.time_values\nprint(reader)\nprint(f\"Available time points: {time_values}\")\nprint(f\"Available point arrays: {reader.point_array_names}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First both time points are read in, and then the difference in density\nis calculated and saved on the second mesh. The read method of\n`pyvista.EnSightReader`{.interpreted-text role=\"class\"} returns a\n`pyvista.MultiBlock`{.interpreted-text role=\"class\"} instance. In this\ndataset, there are 3 blocks and the new scalar must be applied on each\nblock.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "reader.set_active_time_value(time_values[0])\nmesh_0 = reader.read()\nreader.set_active_time_value(time_values[1])\nmesh_1 = reader.read()\n\nfor block_0, block_1 in zip(mesh_0, mesh_1):\n block_1['DENS_DIFF'] = block_1['DENS'] - block_0['DENS']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The value of [DENS]{.title-ref} is plotted on the left column for both\ntime points, and the difference on the right.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plotter = pyvista.Plotter(shape='2|1')\n\nplotter.subplot(0)\nplotter.add_mesh(mesh_0, scalars='DENS', show_scalar_bar=False)\nplotter.add_text(f\"{time_values[0]}\")\n\nplotter.subplot(1)\nplotter.add_mesh(mesh_1, scalars='DENS', show_scalar_bar=False)\nplotter.add_text(f\"{time_values[1]}\")\n\n# pyvista currently cannot plot the same mesh twice with different scalars\nplotter.subplot(2)\nplotter.add_mesh(mesh_1.copy(), scalars='DENS_DIFF', show_scalar_bar=False)\nplotter.add_text(\"DENS Difference\")\n\nplotter.link_views()\nplotter.camera_position = ((0.5, 0, 8), (0.5, 0, 0), (0, 1, 0))\n\nplotter.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reading time points or iterations can also be utilized to make a movie.\nCompare to `gif_movie_example`{.interpreted-text role=\"ref\"}, but here a\nset of files are read in through a ParaView Data format file. This file\nformat and reader also return a `pyvista.MultiBlock`{.interpreted-text\nrole=\"class\"} mesh.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filename = examples.download_wavy(load=False)\nreader = pyvista.get_reader(filename)\nprint(reader)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For each time point, plot the mesh colored by the height. Put iteration\nvalue in top left\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plotter = pyvista.Plotter(notebook=False, off_screen=True)\n# Open a gif\nplotter.open_gif(\"wave_pvd.gif\")\n\nfor time_value in reader.time_values:\n reader.set_active_time_value(time_value)\n mesh = reader.read()[0] # This dataset only has 1 block\n plotter.add_mesh(mesh, scalars='z', show_scalar_bar=False, lighting=False)\n plotter.add_text(f\"Time: {time_value:.0f}\", color=\"black\")\n plotter.write_frame()\n plotter.clear()\n\nplotter.close()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }