
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/01_getting_started/p03_loading_data.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_01_getting_started_p03_loading_data.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_01_getting_started_p03_loading_data.py:


Loading and Plotting MHD Data
==============================

This example introduces the two steps needed to go from a PSI data file to a
rendered scene:

1. **Fetching a dataset** — :func:`~pyvisual.utils.data.fetch_datasets`
   downloads (or retrieves from cache) a version-pinned HDF5 file from the
   PSI asset server and returns its local path.
2. **Reading the data** — :func:`~psi_io.psi_io.read_hdf_by_index` loads the array
   values and the three coordinate grids :math:`(r, \theta, \phi)` from the
   file.  Passing ``None`` for a dimension selects its full extent; passing an
   integer index fixes that dimension to a single grid point.

The dataset used here is the radial magnetic field :math:`B_r` from a
Thermo 2 steady-state coronal simulation for Carrington Rotation 2282
(CR 2282), covering the domain :math:`r \in [1,\,30]\,R_\odot`.

.. GENERATED FROM PYTHON SOURCE LINES 20-25

.. code-block:: Python


    from psi_io import read_hdf_by_index
    from pyvisual import Plot3d
    from pyvisual.utils.data import fetch_datasets








.. GENERATED FROM PYTHON SOURCE LINES 26-35

Fetching a Dataset
------------------

:func:`~pyvisual.utils.data.fetch_datasets` accepts a *domain* identifier
(``'cor'`` for the coronal domain, ``'hel'`` for heliospheric) and a
*variable* name.  It returns a :func:`~collections.namedtuple` whose fields
are named ``"{domain}_{variable}"``.  The first call downloads the file to
the local cache; subsequent calls return the cached copy immediately without
hitting the network.

.. GENERATED FROM PYTHON SOURCE LINES 35-39

.. code-block:: Python


    datasets = fetch_datasets("cor", "br")
    br_file = datasets.cor_br








.. GENERATED FROM PYTHON SOURCE LINES 40-55

Reading a 2-D Radial Slice
--------------------------

:func:`~psi_io.psi_io.read_hdf_by_index` reads the HDF5 file and returns
``(data, r, t, p)`` — the scalar array followed by the three coordinate
vectors.  Index arguments control which portion of the grid is loaded:

- ``None`` — load the full extent of that dimension.
- An integer ``i`` — fix that dimension to the ``i``-th grid point
  (1-based), collapsing it to a length-1 array.

Here the colatitude is fixed at index 71 (the equatorial plane,
:math:`\theta_{71} \approx \pi/2`), while :math:`r` and :math:`\phi`
span their full extents.  The result is a 2-D surface in the equatorial
plane colored by :math:`B_r`.

.. GENERATED FROM PYTHON SOURCE LINES 55-65

.. code-block:: Python


    data, r, t, p = read_hdf_by_index(br_file, None, 71, None)

    plotter = Plot3d()
    plotter.show_axes()
    plotter.add_sun()
    plotter.add_2d_slice(r, t, p, data, cmap='seismic', clim=(-1, 1),
                         show_scalar_bar=True)
    plotter.show()








.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /gallery/01_getting_started/images/sphx_glr_p03_loading_data_001.png
        :alt: p03 loading data
        :srcset: /gallery/01_getting_started/images/sphx_glr_p03_loading_data_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /Users/rdavidson/MHDweb/pyvisual/docs/source/gallery/01_getting_started/images/sphx_glr_p03_loading_data_001.vtksz






.. GENERATED FROM PYTHON SOURCE LINES 66-74

Scaling by :math:`r^2`
-----------------------

The radial magnetic field falls off geometrically as :math:`1/r^2` with
distance.  Multiplying by :math:`r^2` removes this trend and reveals the
longitudinal structure of open-field regions at all radii — a common
diagnostic in solar wind modeling.  Because ``r`` is a plain NumPy array,
the scaling is a single element-wise operation before passing to the plotter.

.. GENERATED FROM PYTHON SOURCE LINES 74-80

.. code-block:: Python


    plotter = Plot3d()
    plotter.show_axes()
    plotter.add_sun()
    plotter.add_2d_slice(r, t, p, data * r ** 2, cmap='seismic', clim=(-1, 1),
                         show_scalar_bar=True)
    plotter.show()






.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /gallery/01_getting_started/images/sphx_glr_p03_loading_data_002.png
        :alt: p03 loading data
        :srcset: /gallery/01_getting_started/images/sphx_glr_p03_loading_data_002.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /Users/rdavidson/MHDweb/pyvisual/docs/source/gallery/01_getting_started/images/sphx_glr_p03_loading_data_002.vtksz







.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 1.603 seconds)


.. _sphx_glr_download_gallery_01_getting_started_p03_loading_data.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: p03_loading_data.ipynb <p03_loading_data.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: p03_loading_data.py <p03_loading_data.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: p03_loading_data.zip <p03_loading_data.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
