Python 从输入数组中选择网格化数据

Python 从输入数组中选择网格化数据,python,arrays,numpy,Python,Arrays,Numpy,有没有人有使用数组从输入数组中选择网格数据的经验?如果我有一个数组,如以下lat/long坐标: 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 我可以使用哪个n

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

 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值?非常感谢您的帮助。

首先,将数据加载到阵列中(特别是在需要多次加载的情况下):

然后找到(平方)距离:

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)]

首先,将数据加载到数组中(特别是在需要多次加载的情况下):

然后找到(平方)距离:

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)]

首先,将数据加载到数组中(特别是在需要多次加载的情况下):

然后找到(平方)距离:

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)]

首先,将数据加载到数组中(特别是在需要多次加载的情况下):

然后找到(平方)距离:

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)]

关于您要问什么,现在还不清楚,但是如果您有一个经度、纬度对数组,并且希望从网格数据集中找到最接近的值,那么可以使用下面的方法。这是一个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
要使用该函数,这些数据必须为正值(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]

在这里,我假设您的数据存储在维度
(时间、纬度、经度)
的数组中。您可以进一步使用
dis
对站点数据进行加权,以进行加权插值。您可以查看有关我如何使用此功能的更多信息。希望这能有所帮助。

关于您的要求有点不清楚,但是如果您有一个经度、纬度对数组,并且希望从网格数据集中找到最接近的值,您可以使用下面的方法。这是一个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
要使用该函数,这些数据必须为正值(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]

在这里,我假设您的数据存储在维度
(时间、纬度、经度)
的数组中。您可以进一步使用
dis
对站点数据进行加权,以进行加权插值。您可以查看有关我如何使用此功能的更多信息。希望这能有所帮助。

关于您的要求有点不清楚,但是如果您有一个经度、纬度对数组,并且希望从网格数据集中找到最接近的值,您可以使用下面的方法。这是一个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
要使用该函数,这些数据必须为正值(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]

在这里,我假设您的数据存储在维度
(时间、纬度、经度)
的数组中。您可以进一步使用
dis
对站点数据进行加权,以进行加权插值。您可以查看有关我如何使用此功能的更多信息。希望这能有所帮助。

关于您的要求有点不清楚,但是如果您有一个经度、纬度对数组,并且希望从网格数据集中找到最接近的值,您可以使用下面的方法。这是一个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
要使用该函数,这些数据必须为正值(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]
在这里,我假设您的数据存储在维度
(时间、纬度、经度)
的数组中。您还可以进一步使用
dis
对站点数据进行加权,以进行加权