bjlittle / geovista

Cartographic rendering and mesh analytics powered by PyVista
https://geovista.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
163 stars 27 forks source link

Update scalars on existing mesh #449

Closed jbusecke closed 4 months ago

jbusecke commented 1 year ago

📰 Custom Issue

I recently produced this and am wondering if I could improve performance with these sorts of high resolution climate outputs. The movie shows two 'animated' features: a) evolving scalars (time dependent velocity and salinity fields) with time b) A camera move

I wrote the model out in a fashing like this (pseudocode):

import geovista as gv

ds = load_xarray_dataset
frames = range(n)
camera_path = define_camera_path(frames)

p = gv.GeoPlotter()
p.open_movie(f'something.mp4')

for frame in frames:
    # select timestep
    da = ds.sel(time=frame).YOUR_VARIABLE

   # create new mesh for each timestep (point a)
    mesh = gv.Transform.from_2d(da.lon, da.lat, data=da.data)
    mesh = mesh.threshold()
    actor = p.add_mesh(mesh)

   # Animate camera position (point b)
    p.camera_position = camera_path.points[frame,:]

    # write movie frame and remove actor
    p.write_frame()
    p.remove_actor(actor)

p.close()

This seems super inefficent to me. I actually do not need to redraw the mesh, but instead the whole point a) is currently a change in the scalar values. I saw that there is a possibility to update just the scalars in pyvista. I wonder if there is an internal/external function that would allow me to 'map' my n dimensional array of scalar data with geovista to be in the proper format to use pyvista.Plotter.update_scalars() instead of removing/rebuilding the mesh.

bjlittle commented 1 year ago

Hey @jbusecke!

Great to hear from you.

Okay, I can see what you're doing, and your intuition is right ...

Given your example above, you should be able to do "something" like this:

import geovista as gv

ds = load_xarray_dataset
frames = range(n)
camera_path = define_camera_path(frames)

p = gv.GeoPlotter()
p.open_movie(f'something.mp4')

# create the mesh with no data payload
mesh = gv.Transform.from_2d(da.lon, da.lat)

# this is the name of the active scalar that contains your frame data
# which can be anything, let's go with ...
name = "data-frame"

for frame in frames:
    # select timestep
    da = ds.sel(time=frame).YOUR_VARIABLE

    # set the active scalar with the data payload for the frame
    mesh[name] = da.data

    # threshold the mesh - this can change the mesh geometry each frame,
    # so create a separate "frame" mesh
    frame = mesh.threshold()

    # this will add the named "data-frame" mesh to the plotter or replace it
    actor = p.add_mesh(frame, name=name)

    # Animate camera position (point b)
    p.camera_position = camera_path.points[frame, :]

    # write movie frame
    p.write_frame()

p.close()

The above assumes that the relationship between the data and the original mesh geometry doesn't change.

Give it a whirl and let me know if that works. It's difficult to give solid advice without the data to play with, so I may hunt around for some at this end to confirm a working pattern.

Also, you may have already seen this, but this may be of help.

HTH

🍻

github-actions[bot] commented 8 months ago

In order to maintain a backlog of relevant issues, we automatically label them as stale after 180 days of inactivity.

If this issue is still important to you, then please comment on this issue and the stale label will be removed.

Otherwise this issue will be automatically closed in 28 days time.

bjlittle commented 4 months ago

@all-contributors please add @jbusecke for promotion

allcontributors[bot] commented 4 months ago

@bjlittle

I've put up a pull request to add @jbusecke! :tada:

bjlittle commented 4 months ago

@jbusecke I'm going to close this issue, but if you have any further questions then please don't hesitate to ask in another issue ... or feel free to reopen this issue :+1:

Thanks :beers:

bjlittle commented 4 months ago

Reference https://vimeo.com/863996198

jbusecke commented 4 months ago

Thanks @bjlittle. I have not had a lot of time for pushing viz work recently but I am at a meetibg today and the rotating earth continues to impress people. I will definitely return to this soon.

bjlittle commented 4 months ago

@jbusecke Cool. I'll be here if you need me 👍

Are you at SciPy 2024 BTW?

I'm here in Tacoma if you wanted to hangout and catch-up

jbusecke commented 4 months ago

Unfortunately not, super bummed to miss it this year but maybe this sort of thing would be a good target to rally some pangeo folks either for next year or some other occasion?

bjlittle commented 4 months ago

@jbusecke Sorry you're not here. SciPy is missing you ... and Tacoma is lovely BTW (that certainly doesn't help you feeling bummed 🤣)

It's odd not being in Austin, but exciting to be somewhere new at the same time 👍

Okay, thanks for this. I'll have a think and perhaps organise something more structured for next year.

Also, I was helping teach at the PyVista tutorial on Tuesday morning to introduce geovista . I wrapped up my segment at the end by sharing with them your amazing animation. Hope you don't mind?

There was definitely a collective sound of jaw dropping from the audience ... It's the gift that keeps giving, so thanks again for sharing it with me 💚

Cheers 🍻

jbusecke commented 3 months ago

Oh I absolutely do not mind. Love when my work is shared (and even more when it drops jaws 😂). I honestly think this sort of stuff is only scratching the surface of what would be possible using high resolution earth system model output 🤗. Very keen to advance this in the future!