如何分类 lat/lon 以找到最近的城市

how to categorize lat/lon to find nearest city

我正在玩鸢尾花(真的很漂亮!)并且我有一个城市列表 lat/lons 我有兴趣查看一段时间内的平均温度。我有覆盖整个国家的气温的 netcdf 文件。我想在离我的城市最近的 lat/lons 立方体中标记数据点,这样我就可以轻松获得我需要的这些城市的值,或者只为这些城市的某个地方导出数据。

我想我需要以某种方式使用 add_categorised_coord? https://scitools.org.uk/iris/docs/latest/iris/iris/coord_categorisation.html#iris.coord_categorisation.add_categorised_coord

我会欣赏一个例子。谢谢!

假设您有一个空气温度的网格化数据集,更好的解决方案是将数据插值到给定的坐标点,而不是立方体中的 "tagging" 个数据点。

这可以通过遍历城市及其坐标并使用 cube.interpolate() 方法来完成。有关示例,请参阅 https://scitools.org.uk/iris/docs/latest/userguide/interpolation_and_regridding.html#cube-interpolation-and-regridding

一个更优化的解决方案是使用 trajectory 模块将数据一次性插入所有城市点:

import iris
import iris.analysis.trajectory as itraj
import numpy as np

# Create some dummy data
nx = 10
ny = 20

lonc = iris.coords.DimCoord(
    np.linspace(-5, 10, nx), units="degrees", standard_name="longitude"
)
latc = iris.coords.DimCoord(
    np.linspace(45, 55, ny), units="degrees", standard_name="latitude"
)
cube = iris.cube.Cube(
    np.random.rand(ny, nx),
    dim_coords_and_dims=((latc, 0), (lonc, 1)),
    standard_name="x_wind",
    units="m s^-1",
    attributes=dict(title="dummy_data"),
)

# Create a Nx2 array of city coordinates
city_coords = np.array([[50.7184, -3.5339], [48.8566, 2.3522], [52.6401898, 1.2517445]])

# Interpolate the data to the given points
sample_points = [("latitude", city_coords[:, 0]), ("longitude", city_coords[:, 1])]
cube_values_in_cities = itraj.interpolate(cube, sample_points, "linear")

希望对您有所帮助。