简体   繁体   English

如何对纬度/经度进行分类以找到最近的城市

[英]how to categorize lat/lon to find nearest city

I am playing with iris (really neat.) and I have a list of cities lat/lons I am interested to see average temperature over time.我正在玩虹膜(真的很整洁。)并且我有一个城市纬度/经度列表,我有兴趣查看一段时间内的平均温度。 I have.netcdf files with air temperatures covering entire country, I would like to tag data points in a cube with lat/lons closest to my cities so then I can easily get values I need just for these cities.我有覆盖整个国家的气温的 .netcdf 文件,我想用最靠近我的城市的纬度/经度标记立方体中的数据点,这样我就可以轻松获得这些城市所需的值。 or export data just for these cities somewhere.或仅在某处导出这些城市的数据。

I imagine I need to use add_categorised_coord somehow?我想我需要以某种方式使用 add_categorised_coord 吗? https://scitools.org.uk/iris/docs/latest/iris/iris/coord_categorisation.html#iris.coord_categorisation.add_categorised_coord https://scitools.org.uk/iris/docs/latest/iris/iris/coord_categorisation.html#iris.coord_categorisation.add_categorised_coord

I will appreciate an example.我会很感激一个例子。 Thanks!谢谢!

Assuming you have a gridded dataset of air temperature, a better solution would be to interpolate the data to given coordinate points, instead of "tagging" data points in a cube.假设您有一个空气温度的网格化数据集,更好的解决方案是将数据插值到给定的坐标点,而不是在立方体中“标记”数据点。

This can be done by looping over cities and their coordinates and using cube.interpolate() method.这可以通过遍历城市及其坐标并使用cube.interpolate()方法来完成。 See https://scitools.org.uk/iris/docs/latest/userguide/interpolation_and_regridding.html#cube-interpolation-and-regridding for examples.有关示例,请参见https://scitools.org.uk/iris/docs/latest/userguide/interpolation_and_regridding.html#cube-interpolation-and-regridding

A more optimised solution would be to interpolate the data to all city points at once using the trajectory module:一个更优化的解决方案是使用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")

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM