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

.. only:: html

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

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

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

.. _sphx_glr_gallery_06_spherical_grid_class_p01_spherical_grid_init.py:


Constructing a SphericalMesh
============================

This example demonstrates the three ways to initialise a
:class:`~pyvisual.core.mesh3d.SphericalMesh`:

1. **From an HDF file path** — pass the path directly; the constructor calls
   :func:`~psi_io.psi_io.read_hdf_by_index` internally.  Additional positional
   arguments after the path are forwarded as index arguments to the file reader.
2. **From data arrays** — read the file manually with
   :func:`~psi_io.psi_io.read_hdf_by_index`, then pass the coordinate arrays and data
   to the constructor.
3. **From an existing** :class:`~pyvisual.core.mesh3d.SphericalMesh` — pass
   another mesh instance to produce a shallow copy.

All three routes produce an equivalent mesh; the choice depends on how much
control over the read step you need.  Real coronal magnetic field data
:math:`B_r` from a PSI Thermo 2 run for Carrington Rotation 2282 (CR 2282) is
used throughout.

.. GENERATED FROM PYTHON SOURCE LINES 22-30

.. code-block:: Python


    from psi_io import read_hdf_by_index
    from pyvisual import Plot3d
    from pyvisual.core.mesh3d import SphericalMesh
    from pyvisual.utils.data import fetch_datasets

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








.. GENERATED FROM PYTHON SOURCE LINES 31-43

From an HDF File Path
---------------------

Passing a file path as the first argument triggers the file-path dispatch
path: the constructor calls :func:`~psi_io.psi_io.read_hdf_by_index` on the path,
loading both the scalar data and the three coordinate grids.  Positional
arguments after the path are forwarded to :func:`~psi_io.psi_io.read_hdf_by_index`
as index arguments, controlling which portion of the grid is loaded
(see the function documentation for details).

Here no index arguments are supplied, so the full 3-D coronal domain is loaded
(:math:`r \times \theta \times \phi`).

.. GENERATED FROM PYTHON SOURCE LINES 43-52

.. code-block:: Python


    mesh_from_path = SphericalMesh(br_file)

    print(f"dimensions : {mesh_from_path.dimensions}")
    print(f"r range    : [{mesh_from_path.r.min():.2f}, {mesh_from_path.r.max():.2f}] R_sun")
    print(f"t range    : [{mesh_from_path.t.min():.4f}, {mesh_from_path.t.max():.4f}] rad")
    print(f"p range    : [{mesh_from_path.p.min():.4f}, {mesh_from_path.p.max():.4f}] rad")
    print(f"data range : [{mesh_from_path.data.min():.4f}, {mesh_from_path.data.max():.4f}] MAS Units")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    dimensions : (255, 142, 299)
    r range    : [1.00, 30.42] R_sun
    t range    : [0.0000, 3.1416] rad
    p range    : [0.0000, 6.2832] rad
    data range : [-47.3395, 47.9679] MAS Units




.. GENERATED FROM PYTHON SOURCE LINES 53-62

From Data Arrays
----------------

When you need to pre-process the arrays before constructing the mesh —
for example to apply a coordinate transform or inspect the raw values —
call :func:`~psi_io.psi_io.read_hdf_by_index` yourself and pass the results
directly to the constructor.  The coordinate arrays go in as the first three
positional arguments (``r``, ``t``, ``p``); the scalar values are supplied
via the ``data`` keyword.

.. GENERATED FROM PYTHON SOURCE LINES 62-71

.. code-block:: Python


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

    mesh_from_arrays = SphericalMesh(r, t, p, data=data, dataid='Br')

    # Dimensions and data range are identical to the file-path route.
    print(f"dimensions match : {mesh_from_arrays.dimensions == mesh_from_path.dimensions}")
    print(f"data allclose    : {(mesh_from_arrays.data == mesh_from_path.data).all()}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    dimensions match : True
    data allclose    : True




.. GENERATED FROM PYTHON SOURCE LINES 72-78

From an Existing SphericalMesh
------------------------------

Passing an existing :class:`~pyvisual.core.mesh3d.SphericalMesh` (or any
:class:`pyvista.DataSet`) produces a shallow copy — both objects share the
same underlying data buffers.  Pass ``deep=True`` for an independent copy.

.. GENERATED FROM PYTHON SOURCE LINES 78-83

.. code-block:: Python


    mesh_from_mesh = SphericalMesh(mesh_from_path)

    print(f"dimensions match : {mesh_from_mesh.dimensions == mesh_from_path.dimensions}")





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    dimensions match : True




.. GENERATED FROM PYTHON SOURCE LINES 84-92

Visualising the Mesh
--------------------

All three meshes are equivalent.  Here the equatorial plane is extracted
by slicing the theta axis (:math:`\theta_{71} \approx \pi/2`) and rendered
as a 2-D surface colored by :math:`B_r`.  The ``MESH_FRAME`` tag stored in
``user_dict`` tells :class:`~pyvisual.core.plot3d.Plot3d` to convert
spherical coordinates to Cartesian automatically before rendering.

.. GENERATED FROM PYTHON SOURCE LINES 92-99

.. code-block:: Python


    equatorial = mesh_from_path[:, 71, :]

    plotter = Plot3d()
    plotter.show_axes()
    plotter.add_sun()
    plotter.add_mesh(equatorial, cmap='seismic', clim=(-1, 1), show_scalar_bar=True)
    plotter.show()






.. tab-set::



   .. tab-item:: Static Scene



            
     .. image-sg:: /gallery/06_spherical_grid_class/images/sphx_glr_p01_spherical_grid_init_001.png
        :alt: p01 spherical grid init
        :srcset: /gallery/06_spherical_grid_class/images/sphx_glr_p01_spherical_grid_init_001.png
        :class: sphx-glr-single-img
     


   .. tab-item:: Interactive Scene



       .. offlineviewer:: /Users/rdavidson/MHDweb/pyvisual/docs/source/gallery/06_spherical_grid_class/images/sphx_glr_p01_spherical_grid_init_001.vtksz







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

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


.. _sphx_glr_download_gallery_06_spherical_grid_class_p01_spherical_grid_init.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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