是否有人使用数组从输入数组中选择网格数据?如果我有一个数组,如下面的纬度/经度坐标:
x = np.array([[66.39, -58.74], [66.47, -58.83], [66.55, -58.93]])
我想从下表格式中选择最近的snc数据:
lat, lon, snc
53.45, 25.45, 80
66.20, -57.45, 45
66.10, -58.90, 75
我可以使用哪个numpy工具用输入数组中的坐标选择最接近的snc值?非常感谢任何帮助。
答案 0 :(得分:2)
您要问的是有点不清楚,但是如果您有一系列经度,纬度对,并且您想要从网格化数据集中找到最接近的值,您可以使用下面的方法。这是我用来从网格化数据集(矩形数据)中提取站点数据的Python函数,其中每个站点都是经度 - 纬度对。您可以指定要为您的工作站提取索引的最近点数。获得周围点的索引列表后,可以将值插值到工作站。
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
此处,longitude
和latitude
是二维数组,包含保存数据的矩形网格的地理信息。如果您的数据是1维(例如lat = 0-90N,lon = 0-360E),您可以使用以下方法创建2D数组:
import numpy as np
lon=np.arange(0,360,1)
lat=np.arange(0,90,1)
longitude, latitude = np.meshgrid(lon,lat)
要使用此功能,这些数据必须为正数(0-360),否则您必须编辑该功能。要拨打该方法,请提供您的电台的地理位置(例如st_lon = 30.0,st_lat = 55.2)并致电:
gridIndexes, dis = getStationIndices(longitude,latitude,st_lon,st_lat,numberOfPoints)
此处,numberOfPoints
是您要提取的(st_lon,st_lat)
周围的网格单元数。接下来,您将从您标识的网格单元中提取数据。
for i in xrange(numberOfPoints):
latindex=int(gridIndexes[i][0])
lonindex=int(gridIndexes[i][1])
result = TEMP[time,latindex,lonindex]
在这里,我假设您的数据存储在维度(time,latitude,longitude)
的数组中。您可以进一步使用dis
来加权您的电台数据以进行加权插值。您可以查找有关我如何使用此信息的更多信息here。希望这会有所帮助。
答案 1 :(得分:1)
首先,将数据加载到数组中(特别是如果要多次执行此操作):
lat,lon,snc = np.loadtxt('inputfile',unpack=True...)
然后找到(平方)距离:
R = ((x[:,0]-lat)**2 + (x[:,1]-lon)**2 * np.cos(x[:,0])**2 )
#note cos term; this may not be needed for you
并获得SNC:
snc[argmin(R)]