UXarray for Basic HEALPix Statistics & Visualization¶
In this section, you’ll learn:¶
- Utilizing
intake
to open a HEALPix data catalog - Using the
uxarray
package to look at basic statistics over HEALPix data - Using UXarray plotting functionality on HEALPix data
Related Documentation¶
- UXarray homepage
- Working with HEALPix data - UXarray documentation
- UXarray overview - Unstructured Grids Visualization Cookbook
- Data visualization with UXarray - Unstructured Grids Visualization Cookbook
- Subsetting - UXarray documentation
- Intake Cookbook
Prerequisites¶
Concepts | Importance | Notes |
---|---|---|
UXarray | Necessary | |
HEALPix overview | Necessary |
Time to learn: 30 minutes
import cartopy.crs as ccrs
import uxarray as ux
Open data catalog¶
Let us use the online data catalog from the WCRP’s Digital Earths Global Hackathon 2025’s catalog repository using intake
and read the output of the ICON
simulation run ngc4008
, which is stored in the HEALPix format:
import intake
# Hackathon data catalogs
cat_url = "https://digital-earths-global-hackathon.github.io/catalog/catalog.yaml"
cat = intake.open_catalog(cat_url).online
model_run = cat.icon_ngc4008
We will be looking at two resolution levels, one is the coarsest zoom level of 0, which is the default in this model run, and the other is a finer one at the zoom level of 7:
ds_coarsest = model_run().to_dask()
ds_fine = model_run(zoom=7).to_dask()
/home/runner/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/intake_xarray/base.py:21: FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`. To access a mapping from dimension names to lengths, please use `Dataset.sizes`.
'dims': dict(self._ds.dims),
/home/runner/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/intake_xarray/base.py:21: FutureWarning: The return type of `Dataset.dims` will be changed to return a set of dimension names in future, in order to be more consistent with `DataArray.dims`. To access a mapping from dimension names to lengths, please use `Dataset.sizes`.
'dims': dict(self._ds.dims),
Create UXarray Datasets from HEALPix¶
Now let us use those xarray.Dataset
s from the model run to open unstructured grid-aware uxarray.UxDataset
:
%%time
uxds_coarsest = ux.UxDataset.from_healpix(ds_coarsest)
uxds_fine = ux.UxDataset.from_healpix(ds_fine)
uxds_fine
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File <timed exec>:1
File ~/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/uxarray/core/dataset.py:328, in UxDataset.from_healpix(cls, ds, pixels_only, face_dim, **kwargs)
321 # Attach a HEALPix Grid
322 uxgrid = Grid.from_healpix(
323 zoom=get_zoom_from_cells(ds.sizes[face_dim]),
324 pixels_only=pixels_only,
325 **kwargs,
326 )
--> 328 return cls.from_xarray(ds, uxgrid, {face_dim: "n_face"})
File ~/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/uxarray/core/dataset.py:281, in UxDataset.from_xarray(cls, ds, uxgrid, ugrid_dims)
278 ugrid_dims = uxgrid._source_dims_dict
280 # map each dimension to its UGRID equivalent
--> 281 ds = _map_dims_to_ugrid(ds, ugrid_dims, uxgrid)
283 return cls(ds, uxgrid=uxgrid)
File ~/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/uxarray/core/utils.py:57, in _map_dims_to_ugrid(ds, _source_dims_dict, grid)
55 if ds.sizes[dim] == grid.n_face:
56 _source_dims_dict[dim] = "n_face"
---> 57 elif ds.sizes[dim] == grid.n_node:
58 _source_dims_dict[dim] = "n_node"
59 elif n_edge is not None:
File ~/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/uxarray/grid/grid.py:820, in Grid.n_node(self)
817 @property
818 def n_node(self) -> int:
819 """Total number of nodes."""
--> 820 return self._ds.sizes["n_node"]
File ~/micromamba/envs/healpix-cookbook-dev/lib/python3.13/site-packages/xarray/core/utils.py:452, in Frozen.__getitem__(self, key)
451 def __getitem__(self, key: K) -> V:
--> 452 return self.mapping[key]
KeyError: 'n_node'
HEALPix basic stats using UXarray¶
Let us look at the global and Boulder, CO, USA air temperature averages for the dataset. Data spans from 2020 to 2050, so let us also consider slicing it to have a 3-year interval between 2020 and 2023, which would also give us similar results to that with easy.gems in the previous section.
import matplotlib.pylab as plt
boulder_lon = -105.2747
boulder_lat = 40.0190
time_slice = slice("2020-01-02T00:00:00.000000000", "2023-01-01T00:00:00.000000000")
Mesh face containing Boulder’s coords¶
We can find face(s) containing a given point with uxarray
conveniently as follows:
%%time
boulder_face = uxds_fine.uxgrid.get_faces_containing_point(
point_lonlat=[boulder_lon, boulder_lat]
)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
File <timed exec>:1
NameError: name 'uxds_fine' is not defined
Data variables of interest¶
In order to use in the rest of the analyses, we can grab data variables, in theuxarray.UxDataArray
type, from the dataset as follows:
uxda_fine = uxds_fine.tas
uxda_coarsest = uxds_coarsest.tas
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 1
----> 1 uxda_fine = uxds_fine.tas
2 uxda_coarsest = uxds_coarsest.tas
NameError: name 'uxds_fine' is not defined
Global and Boulder’s temperature averages¶
In order to get a line plot of our UXarray.UxDataset
objects’ 1-dimensional temperature variables, we will convert them to xarray
and call the default plot
function because UXarray’s default plotting functions are all dedicated to grid-topology aware visualizations:
%%time
uxda_fine.isel(n_face=boulder_face).sel(time=time_slice).to_xarray().plot(
label="Boulder"
)
uxda_coarsest.sel(time=time_slice).mean("n_face").to_xarray().plot(label="Global mean")
plt.legend()
Data plotting with UXarray¶
UXarray provides several built-in plotting functions to visualize unstructured grids, which can also be applied to HEALPix grids in the same interface:
Let us first look into interactive plots with the bokeh
backend (i.e. UXarray’s plotting functions have a backend
parameter that defaults to “bokeh”, and it can also accept “matplotlib”)
Global plots¶
Let us first plot the global temperature (at the first timestep for simplicity), using the default backend, bokeh
, of UXarray’s visualization API to create an interactive plot:
%%time
projection = ccrs.Robinson(central_longitude=-135.5808361)
uxda_fine.isel(time=0).plot(
projection=projection,
cmap="inferno",
features=["borders", "coastline"],
title="Global temperature",
width=700,
)
Now, let us create the same plot, using matplotlib
as the backend:
%%time
uxda_fine.isel(time=0).plot(
backend="matplotlib",
projection=projection,
cmap="inferno",
features=["borders", "coastline"],
title="Global temperature",
width=1100,
)
Regional subsets (Not only for plotting but also for analysis)¶
When a region on the globe is of interest, UXarray provides subsetting functions, which return new regional grids that can then be used in the same way a global grid is plotted.
Let us look into the USA map using the Boulder, CO, USA coords we had used before for simplicity:
Subsetting uxds_fine
into a new UxDataset
using a “bounding box” around Boulder, CO first:
%%time
lon_bounds = (boulder_lon - 20, boulder_lon + 40)
lat_bounds = (boulder_lat - 20, boulder_lat + 12)
uxda_fine_subset = uxda_fine.isel(time=0).subset.bounding_box(lon_bounds, lat_bounds)
If we check the global and regional subset’s average temperature at the first timestep, we can see the difference:
print(
"Global temperature average: ", uxda_fine.isel(time=0).mean("n_face").values, " K"
)
print(
"Regional subset's temperature average: ", uxda_fine_subset.mean("n_face").values, " K"
)
Now, let us plot the regional subset UxDataset
:
%%time
projection = ccrs.Robinson(central_longitude=boulder_lon)
uxda_fine_subset.plot(
projection=projection,
cmap="inferno",
features=["borders", "coastline"],
title="Boulder temperature",
width=1100,
)
Grid topology exploration¶
Exploring the grid topology may be needed sometimes, and UXarray provides functionality to do so, both numerically and visually. Each UxDataset
or UxDataArray
has their associated Grid
object that has all the information such as spherical and cartesian coordinates, connectives, dimensions, etc. about the topology this data belongs to. This Grid
object can be explored as follows:
# uxds_fine.uxgrid # this would give the same as the below
uxda_fine.uxgrid
There might be times that the user wants to open a standalone Grid
object for a HEALPix grid (or any other unstructured grids supported by UXarray) without accessing the data yet. Let’s create the coarsest HEALPix grid as follows:
uxgrid = ux.Grid.from_healpix(zoom=0, pixels_only=False)
uxgrid
Let’s investigate how a HEALPix grid looks like over the poles. We can do things by selecting an Orthographic projection and setting a Geodetic source projection. This allows to better approximates the true HEALPix structured compared to the defaut PlateCaree projection.
projection = ccrs.Orthographic(central_latitude=90)
projection.threshold /= 100 # Smoothes out great circle arcs
uxgrid.plot(
periodic_elements="ignore", # Allow Cartopy to handle periodic elements
crs=ccrs.Geodetic(), # Enables edges to be plotted as GCAs
project=True,
projection=projection,
width=500,
title="HEALPix (Orthographic Proj), zoom=0",
)
Let’s chose another projection.
projection = ccrs.Mollweide()
projection.threshold /= 100 # Smoothes out great circle arcs
uxgrid.plot(
periodic_elements="ignore", # Allow Cartopy to handle periodic elements
crs=ccrs.Geodetic(), # Enables edges to be plotted as GCAs
project=True,
projection=projection,
width=500,
title="HEALPix (Mollweide Proj), zoom=0",
)
The grid structure here is approximated. While the boundary between each pixel is easily known in HEALPix workflows, UXarray represents the boundaries as a Great Circle Arc (GCA) due to the requirement of having explicit connectivity information, which is different than HEALPix boundaries and leads to minor differences in the computed plots and computations.
Now that we’ve looked at the grid structure, we can also apply the same principles to our data plotting.
uxda_coarsest.isel(time=0).plot(
periodic_elements="ignore", # Allow Cartopy to handle periodic elements
crs=ccrs.Geodetic(), # Enables edges to be plotted as GCAs
project=True,
projection=projection,
features=["borders", "coastline"],
cmap="inferno",
title="Temperature (Mollweide Proj), zoom=0",
width=500,
)
What is next?¶
The next section will provide an UXarray workflow that loads in and analyzes & visualizes HEALPix data.