Skip to content

redplanet.Mag.depth.get_grid

get_grid(
    lon: float | numpy.ndarray,
    lat: float | numpy.ndarray,
    col: str,
) -> ndarray

Similar to get_nearest, but significantly more optimized for accessing data over large areas by using a pre-computed grid of nearest dipole locations.

Parameters:

Name Type Description Default
lon float | np.ndarray

Longitude coordinate(s) in range [-180, 360].

required
lat float | np.ndarray

Latitude coordinate(s) in range [-90, 90].

required
col str

Name of dataset column to return. See redplanet.Mag.depth.get_dataset for options/explanations.

required

Returns:

Type Description
np.ndarray

Data values at the input coordinates, with shape (num_lats, num_lons). For columns with three values (e.g. 'depth_km'), the shape will be (3, num_lats, num_lons).

Raises:

Type Description
ValueError

If the specified column is not found in the dataset.

Source code in src/redplanet/Mag/depth/getter.py
@substitute_docstrings
def get_grid(
    lon : float | np.ndarray,
    lat : float | np.ndarray,
    col : str,
) -> np.ndarray:
    """
    Similar to `get_nearest`, but significantly more optimized for accessing data over large areas by using a pre-computed grid of nearest dipole locations.

    Parameters
    ----------
    {param.lon}
    {param.lat}
    col : str
        Name of dataset column to return. See `redplanet.Mag.depth.get_dataset` for options/explanations.

    Returns
    -------
    np.ndarray
        Data values at the input coordinates, with shape `(num_lats, num_lons)`. For columns with three values (e.g. `'depth_km'`), the shape will be `(3, num_lats, num_lons)`.

    Raises
    ------
    ValueError
        If the specified column is not found in the dataset.
    """

    df_depths, dat_nearest_dipole = get_dataset(_extras=True)

    if col not in df_depths.columns:
        raise ValueError(f"Column '{col}' not found in dataset. Available columns are: {df_depths.columns.tolist()}")

    col_values = np.stack(df_depths[col])
    dat_idx = dat_nearest_dipole.get_values(lon, lat, 'dat')

    dat_return = col_values[dat_idx]

    if dat_return.ndim == 3:
        dat_return = np.moveaxis(dat_return, 2, 0)

    return dat_return