Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self-contained runnable sample code for basic mesh plotting. #5501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified docs/src/further_topics/ugrid/images/plotting_basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 37 additions & 42 deletions docs/src/further_topics/ugrid/operations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,32 @@ earlier:

.. code-block:: python

>>> import iris
>>> from iris.experimental.ugrid import PARSE_UGRID_ON_LOAD
>>>
>>> from geovista import GeoPlotter, Transform
>>> from geovista.common import to_xyz


# We'll re-use this to plot some real global data later.
>>> from geovista.common import to_cartesian as to_xyz
Copy link
Member

@bjlittle bjlittle Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really?

You should adopt the use of to_cartesian and not rebrand it. The reason why you're doing this is already lost in the mists of time, and it's not recommending a good pattern to adopt to the reader moving forwards 👎

>>>
>>> testdata_path = iris.sample_data_path("mesh_C4_synthetic_float.nc")
>>> with PARSE_UGRID_ON_LOAD.context():
... cube = iris.load_cube(testdata_path)
...
>>> print(cube)
synthetic / (1) (-- : 96)
Mesh coordinates:
latitude x
longitude x
Mesh:
name Topology data of 2D unstructured mesh
location face
Attributes:
NCO 'netCDF Operators version 4.7.5 (Homepage = http://nco.sf.net, Code = h ...'
history 'Mon Apr 12 01:44:41 2021: ncap2 -s synthetic=float(synthetic) mesh_C4_synthetic.nc ...'
nco_openmp_thread_number 1
>>> def cube_faces_to_polydata(cube):
... lons, lats = cube.mesh.node_coords
... face_node = cube.mesh.face_node_connectivity
... indices = face_node.indices_by_location()
...
... mesh = Transform.from_unstructured(
... lons.points,
... lats.points,
Expand All @@ -502,50 +518,29 @@ earlier:
... start_index=face_node.start_index,
... )
... return mesh

>>> print(face_cube)
face_data / (K) (-- : 2; height: 3)
Dimension coordinates:
height - x
Mesh coordinates:
latitude x -
longitude x -
Attributes:
Conventions 'CF-1.7'

# Convert our mesh+data to a PolyData object.
# Just plotting a single height level.
>>> face_polydata = cube_faces_to_polydata(face_cube[:, 0])
...
Copy link
Member

@bjlittle bjlittle Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pp-mo You can now simplify the call to Transform.from_unstructured as follows:

mesh = Transform.from_unstructured(
    lons.points,
    lats.points,
    connectivity=indices,
    data=cube.data,
    name=f"{cube.name()} / {cube.units}",
)

There is now no need to provide the start_index as geovista will detect this, and it's better to name the indices with the connectivity kwarg for clarity.

This is just a suggestion, and not strictly necessary.

>>> face_polydata = cube_faces_to_polydata(cube)
>>> print(face_polydata)
PolyData (0x7ff4861ff4c0)
N Cells: 2
N Points: 5
X Bounds: 9.903e-01, 1.000e+00
Y Bounds: 0.000e+00, 1.392e-01
Z Bounds: 6.123e-17, 5.234e-02
N Arrays: 2

# Create the GeoVista plotter and add our mesh+data to it.
PolyData (0x7f179a6b2800)
N Cells: 96
N Points: 98
N Strips: 0
X Bounds: -1.000e+00, 1.000e+00
Y Bounds: -1.000e+00, 1.000e+00
Z Bounds: -1.000e+00, 1.000e+00
N Arrays: 4
>>>
>>> my_plotter = GeoPlotter()
>>> my_plotter.add_coastlines(color="black")
>>> my_plotter.add_base_layer(color="grey")
>>> my_plotter.add_mesh(face_polydata)

# Centre the camera on the data.
>>> camera_region = to_xyz(
... face_cube.coord("longitude").points,
... face_cube.coord("latitude").points,
... radius=3,
... )
>>> camera_pos = camera_region.mean(axis=0)
>>> my_plotter.camera.position = camera_pos

>>> my_plotter.background_color = "#502020ff"
Copy link
Member

@bjlittle bjlittle Sep 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use import geovista.theme there's need to set the background_color

>>> _ = my_plotter.add_coastlines(color="white", radius=1.01)
>>> _ = my_plotter.add_mesh(face_polydata, radius=1.0)
Comment on lines +535 to +536
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no real need here to set the radius, it's not adding anything of great value to "basic" plotting... it's just a distraction.

If you're concern is that the mesh is so low resolution that it's markedly different from the spherical coastlines, then only specify the radius for the coastlines. Setting the radius=1.0 for the face_polydata is a nop.

>>>
>>> my_plotter.show()

.. image:: images/plotting_basic.png
:alt: A GeoVista plot of the basic example Mesh.

This artificial data makes West Africa rather chilly!
This artificial data is very low resolution !

Here's another example using a global cubed-sphere data set:

Expand Down
Loading