【发布时间】:2020-09-03 05:17:41
【问题描述】:
我正在尝试从中心纬度位置计算特定半径内包含的所有值。我使用的代码如下所示:
import numpy as np
import matplotlib.pylab as pl
import netCDF4 as nc
import haversine
f = nc.Dataset('air_temp.nc')
def haversine(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1 = np.deg2rad(lon1)
lon2 = np.deg2rad(lon2)
lat1 = np.deg2rad(lat1)
lat2 = np.deg2rad(lat2)
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2)**2
c = 2 * np.arcsin(np.sqrt(a))
r = 6371
return c * r
# Latitude / longitude grid
#lat = np.linspace(50,54,16)
lat = f.variables['lat'][:]
#lon = np.linspace(6,9,12)
lon = f.variables['lon'][:]
clat = 19.7
clon = 69.7
max_dist = 750 # max distance in km
# Calculate distance between center and all other lat/lon pairs
distance = haversine(lon[:,np.newaxis], lat, clon, clat)
# Mask distance array where distance > max_dist
distance_m = np.ma.masked_greater(distance, max_dist)
# Dummy data
air = f.variables['air'][0,:,:,:]
data = np.squeeze(air)
data = np.transpose(data)
#data = np.random.random(size=[lon.size, lat.size])
data_m = np.ma.masked_where(distance >max_dist, data)
# Test: set a value outside the max_dist circle to a large value:
#data[0,0] = 10
#avg = np.nanmean(data_m)-273
我使用过半正弦函数来求距离。现在我面临的问题是我需要距离中心点 2.5 度半径范围内的值,但我得到的都是公里。因此,如果有人可以通过说出我做错了什么或如何以正确的程序来帮助我,我们将不胜感激
【问题讨论】:
-
半径为 2.5 度的“圆”与半径以千米为单位的“圆”不同。度数的长度随地球表面的位置而变化。 Haversine 公式专门用于计算以公里为单位的距离。如果您需要以度为单位的距离,您可以使用 lat 和 long 偏移的平方和的根,尽管正如我所说,这可能会给您一个非常奇怪的形状,具体取决于您所在的位置。
-
@simonN thnx 浏览我的代码。我实际上并没有得到您所说的代码的哪一部分,请您详细说明。
-
代码本身并不是真正的问题。你说你想要 2.5 度内的点,但你有找到 750 公里内的点的代码。您的代码只是解决了一个与您说您感兴趣的问题不同的问题。您需要将函数“haversine”替换为生成“距离”(以度为单位)并将您的 max_dist 更改为 2.5 的函数。
-
好吧,明白你的意思了..会尝试看看..谢谢 cmets mate
-
@simmon 我搜索了以度为单位的半正弦公式,但没有得到任何具体的想法。如果你知道的话,你能帮我看看怎么做吗?
标签: python pandas numpy matplotlib