从输入数组中选择方格数据python

2024-09-28 21:45:36 发布

您现在位置:Python中文网/ 问答频道 /正文

有人有过使用数组从输入数组中选择网格数据的经验吗?如果我有一个数组,如下面的lat/long坐标:

 x = np.array([[66.39, -58.74], [66.47, -58.83], [66.55, -58.93]])

我想从下表格式中选择最近的snc数据:

^{pr2}$

我可以使用哪个numpy工具来选择输入数组中坐标最接近的snc值?任何帮助都将不胜感激。在


Tags: 工具数据numpy网格格式np数组经验
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

这里,longitudelatitude是二维数组,其中包含保存数据的矩形网格的地理信息。如果您的数据是一维的(例如lat=0-90N,lon=0-360E),您可以使用以下方法创建二维阵列:

^{pr2}$

要使用函数,这些数据必须是正的(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中查找我如何使用它的更多信息。希望这有帮助。在

首先,将数据加载到数组中(尤其是要多次加载时):

lat,lon,snc = np.loadtxt('inputfile',unpack=True...)

然后求出(平方)距离:

^{pr2}$

得到SNC:

snc[argmin(R)]

相关问题 更多 >