def getStationIndices(longitude,latitude,st_lon,st_lat,numberOfPoints):
"""
This is a function that takes longitude and latitude as
decimal input, and returns the index values closest to
the longitude and latitude. This is an iterative process for finding the best
index pair.
"""
if st_lon<0: st_lon=st_lon+360.0; NEG=True
else: NEG=False
"""Input longitude should go from 0-360"""
longitude=np.where(longitude<0,longitude+360,longitude)
distance = np.zeros((longitude.shape),dtype=np.float64)
listd=[]
"""First, create a list of distances from the station of interest, while
also save the matrix of distances that contains the info to get the index pair that the distance of interest corresponds to"""
for eta in range(len(latitude[:,0])):
for xi in range(len(latitude[0,:])):
distance[eta,xi] = np.sqrt( (latitude[eta,xi]-st_lat)**2.0 + (longitude[eta, xi] - st_lon)**2.0 )
listd.append(distance[eta,xi])
listsIndexes=[]
listd.sort()
"""Now find the closest point to the station. When that point is found, remove the
closests pooint and find the next closests point, until you have found numberOfPoints
closests to station.
"""
for i in range(numberOfPoints):
value=listd[0]
itemindex=np.where(distance==value)
listsIndexes.append(itemindex)
listd.pop(0)
print ''
print '=====getStationIndices======'
if NEG is True:
print 'Looking for longitude [%3.3f] and latitude [%3.3f]'%(st_lon-360,st_lat)
else:
print 'Looking for longitude [%3.3f] and latitude [%3.3f]'%(st_lon,st_lat)
print 'Result ===>'
for i in range(numberOfPoints):
print 'Found index pair in gridfile',listsIndexes[i]
if NEG is True:
print 'Index corresponds to longitude [%3.3f] and latitude [%3.3f]'%(longitude[listsIndexes[i][0],listsIndexes[i][1]]-360,latitude[listsIndexes[i][0],listsIndexes[i][1]])
else:
print 'Index corresponds to longitude [%3.3f] and latitude [%3.3f]'%(longitude[listsIndexes[i][0],listsIndexes[i][1]],latitude[listsIndexes[i][0],listsIndexes[i][1]])
"""
We want to use data interpolated from the 4 surrounding points to get appropriate values at station point.
We do this by using relative weights determined by relative distance to total distance from all 4 points.
"""
dis=[]
for i in range(numberOfPoints):
dis.append(np.sqrt( (latitude[listsIndexes[i][0],listsIndexes[i][1]]-st_lat)**2.0 + (longitude[listsIndexes[i][0],listsIndexes[i][1]] - st_lon)**2.0 ))
return listsIndexes, dis
关于您的要求有点不清楚,但是如果您有一个经度、纬度对数组,并且希望从网格数据集中找到最接近的值,可以使用下面的方法。这是一个Python函数,用于从网格数据集(矩形数据)中提取站点数据,其中每个站点都是经纬度对。可以指定要提取其索引的距桩号最近的点的数量。一旦有了周围点的索引列表,就可以将值插值到桩号。在
这里,
^{pr2}$longitude
和latitude
是二维数组,其中包含保存数据的矩形网格的地理信息。如果您的数据是一维的(例如lat=0-90N,lon=0-360E),您可以使用以下方法创建二维阵列:要使用函数,这些数据必须是正的(0-360),或者您必须编辑函数。要调用该方法,请提供站点的地理位置(例如st_lon=30.0,st_lat=55.2),然后调用:
这里,
numberOfPoints
是围绕(st_lon,st_lat)
的网格单元数。下一步,从您标识的网格单元格中提取数据。在在这里,我假设您的数据存储在维度
(time,latitude,longitude)
的数组中。您可以进一步使用dis
对站点数据进行加权以进行加权插值。你可以在here中查找我如何使用它的更多信息。希望这有帮助。在首先,将数据加载到数组中(尤其是要多次加载时):
然后求出(平方)距离:
^{pr2}$得到SNC:
相关问题 更多 >
编程相关推荐