Open nbren12 opened 1 year ago
Having some kind of cache for updating image data without needing to transform every time seems like a good improvement! But in your example, you are then putting that into pcolormesh()
rather than imshow()
which will be much faster. For pcolormesh()
we actually subclass the typical QuadMesh
object into a GeoQuadMesh
, https://github.com/SciTools/cartopy/blob/main/lib/cartopy/mpl/geocollection.py to handle this exact updating during a loop. https://scitools.org.uk/cartopy/docs/latest/gallery/miscellanea/animate_surface.html#sphx-glr-gallery-miscellanea-animate-surface-py
It seems like doing something similar for AxesImage
turning into a GeoAxesImage
where the array mapping is cached on that object would be the way to go here. Then your above example would be:
image = ax.imshow(arr, transform=target_crs)
for i in range(100):
arr = get_frame(i)
# almost instantaneous
image.set_array(arr)
I agree imshow makes more sense than quash-mesh here. Perhaps having the caching in a matplotlib subclass makes sense too for the movie application.
That said, regrid
is nicely general purpose and could be useful beyond plotting. For example, for batch conversion of data between CRSs. So it could make sense to add both GeoAxesImage
and img_transform.Regrid
. GeoAxesImage could use the latter. Thoughts?
I agree you'll need to do some kind of refactor to cache the needed quantities for re-use, so that may be a good option.
Description
A common use-case for cartopy is to generate a series of maps which are then compiled into a movie (e.g. with ffmpeg).
This is slow. E.g. to transform, 0.25 deg global data to Mercator on my laptop takes about 2 seconds per frame. Most of this time is spent on a nearest-neighbors search to map between the target and source projections, but this computation only needs to happen a single time if the target and source grids are fixed.
I am proposing that
img_transform.regrid
be refactored into an objectRegrid
to achieve this. It can then be used like this:xesmf uses the same pattern to cache the expensive weight matrix computation.
I went ahead and implemented this (see below). Now I can make movies about 100x faster :) Would you be interested in a PR? Sorry for not following the template too closely...it's a feature request rather than a bug.