# verde.grid_to_table¶

verde.grid_to_table(grid)[source]

Convert a grid to a table with the values and coordinates of each point.

Takes a 2D grid as input, extracts the coordinates and runs them through numpy.meshgrid to create a 2D table. Works for 2D grids and any number of variables. Use cases includes passing gridded data to functions that expect data in XYZ format, such as verde.BlockReduce

Parameters: grid : xarray.Dataset A 2D grid with one or more data variables. table : pandas.DataFrame Table with coordinates and variable values for each point in the grid.

Examples

>>> import xarray as xr
>>> import numpy as np
>>> # Create a sample grid with a single data variable
>>> temperature = xr.DataArray(
...     np.arange(20).reshape((4, 5)),
...     coords=(np.arange(4), np.arange(5, 10)),
...     dims=['northing', 'easting']
... )
>>> grid = xr.Dataset({"temperature": temperature})
>>> table  = grid_to_table(grid)
>>> list(sorted(table.columns))
['easting', 'northing', 'temperature']
>>> print(table.northing.values)
[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
>>> print(table.easting.values)
[5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9]
>>> print(table.temperature.values)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
>>> # Grids with multiple data variables will have more columns.
>>> wind_speed = xr.DataArray(
...     np.arange(20, 40).reshape((4, 5)),
...     coords=(np.arange(4), np.arange(5, 10)),
...     dims=['northing', 'easting']
... )
>>> grid['wind_speed'] = wind_speed
>>> table = grid_to_table(grid)
>>> list(sorted(table.columns))
['easting', 'northing', 'temperature', 'wind_speed']
>>> print(table.northing.values)
[0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3]
>>> print(table.easting.values)
[5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9]
>>> print(table.temperature.values)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
>>> print(table.wind_speed.values)
[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]