Skip to content

Reproject

calc_latlon(ds)

Calculate the latitude and longitude coordinates for the given dataset

Parameters:

Name Type Description Default
ds Dataset

Xarray Dataset to calculate the lat/lon coordinates for, with x and y coordinates

required

Returns:

Type Description
Dataset

Xarray Dataset with the latitude and longitude coordinates added

Source code in rs_tools/_src/geoprocessing/reproject.py
def calc_latlon(ds: xr.Dataset) -> xr.Dataset:
    """
    Calculate the latitude and longitude coordinates for the given dataset

    Args:
        ds: Xarray Dataset to calculate the lat/lon coordinates for, with x and y coordinates

    Returns:
        Xarray Dataset with the latitude and longitude coordinates added
    """
    XX, YY = np.meshgrid(ds.x.data, ds.y.data)
    lons, lats = convert_x_y_to_lat_lon(ds.rio.crs, XX, YY)
    # Check if lons and lons_trans are close in value
    # Set inf to NaN values
    lons[lons == np.inf] = np.nan
    lats[lats == np.inf] = np.nan

    ds = ds.assign_coords({"latitude": (["y", "x"], lats), "longitude": (["y", "x"], lons)})
    ds.latitude.attrs["units"] = "degrees_north"
    ds.longitude.attrs["units"] = "degrees_east"
    return ds