verde.make_xarray_grid

verde.make_xarray_grid(coordinates, data, data_names, dims=('northing', 'easting'), extra_coords_names=None)[source]

Create an xarray.Dataset grid from numpy arrays

This functions creates an xarray.Dataset out of 2d gridded data including easting and northing coordinates, any extra coordinates (like upward elevation, time, etc) and data arrays.

Use this to transform the outputs of verde.grid_coordinates and the predict method of a gridder into an xarray.Dataset.

Note

This is a utility function to help create 2D grids (i.e., grids with two dims coordinates). For arbitrary N-dimensional arrays, use xarray directly.

Parameters
  • coordinates (tuple of arrays) – Arrays with coordinates of each point in the grid. Each array must contain values for a dimension in the order: easting, northing, vertical, etc. All arrays must be 2d and need to have the same shape. These coordinates can be generated through verde.grid_coordinates.

  • data (array or tuple of arrays) – Array or tuple of arrays with data values on each point in the grid. Each array must contain values for a dimension in the same order as the coordinates. All arrays need to have the same shape.

  • data_names (str or list) – The name(s) of the data variables in the output grid.

  • dims (list) – The names of the northing and easting data dimensions, respectively, in the output grid. Must be defined in the following order: northing dimension, easting dimension. NOTE: This is an exception to the “easting” then “northing” pattern but is required for compatibility with xarray. The easting and northing coordinates in the xarray.Dataset will have the same names as the passed dimensions.

  • extra_coords_names (str or list) – Name or list of names for any additional coordinates besides the easting and northing ones. Ignored if coordinates has only two elements. The extra coordinates are non-index coordinates of the grid array.

Returns

grid (xarray.Dataset) – A 2D grid with one or more data variables.

Examples

>>> import numpy as np
>>> import verde as vd
>>> # Create the coordinates of the regular grid
>>> coordinates = vd.grid_coordinates((-10, -6, 8, 10), spacing=2)
>>> # And some dummy data for each point of the grid
>>> data = np.ones_like(coordinates[0])
>>> # Create the grid
>>> grid = make_xarray_grid(coordinates, data, data_names="dummy")
>>> print(grid)
<xarray.Dataset>
Dimensions:   (easting: 3, northing: 2)
Coordinates:
  * easting   (easting) float64 -10.0 -8.0 -6.0
  * northing  (northing) float64 8.0 10.0
Data variables:
    dummy     (northing, easting) float64 1.0 1.0 1.0 1.0 1.0 1.0
>>> # Create a grid with an extra coordinate
>>> coordinates = vd.grid_coordinates(
...     (-10, -6, 8, 10), spacing=2, extra_coords=5
... )
>>> # And some dummy data for each point of the grid
>>> data = np.ones_like(coordinates[0])
>>> # Create the grid
>>> grid = make_xarray_grid(
...     coordinates, data, data_names="dummy", extra_coords_names="upward"
... )
>>> print(grid)
<xarray.Dataset>
Dimensions:   (easting: 3, northing: 2)
Coordinates:
  * easting   (easting) float64 -10.0 -8.0 -6.0
  * northing  (northing) float64 8.0 10.0
    upward    (northing, easting) float64 5.0 5.0 5.0 5.0 5.0 5.0
Data variables:
    dummy     (northing, easting) float64 1.0 1.0 1.0 1.0 1.0 1.0