列表作为列表列表或多维列表作为网格

时间:2013-05-16 13:34:50

标签: python list numpy

我正在尝试创建一个lat / lon网格,其中包含已找到索引的数组,其中lat / lon组合满足两个条件。这种方法可能过于复杂,但使用meshgrid或numpy广播也失败了。如果有更好的方法,请随时分享您的知识。 : - )

舍入lat / lon值到格子化分辨率为1°但保留数组的全长:

x = np.around(lon, decimals=0) 
y = np.around(lat, decimals=0) 

数组由-180到180和-82°到82°的经度/纬度值组成;多个可能的douplets

检查lat / lon的每个组合可用于1°/ 1°网格点的测量数量:

a = arange(-180,181)    
b = arange(-82,83)
totalgrid = [ [ 0 for i in range(len(b)) ] for j in range(len(a)) ]
for d1 in range(len(a)):
    for d2 in range(len(b)):
        totalgrid[d1][d2]=np.where((x==a[d1])&(y==b[d2]))[0]

此方法失败,仅返回包含空数组的列表列表。我无法弄清楚为什么它不能正常工作。 用以下代替最后一行:

totalgrid[d1][d2]=np.where((x==a[0])&(y==b[0]))[0] 

返回lon / lat中所有找到的指数,它们出现在-180°/ -82°。不幸的是需要一段时间。我在某个地方错过了一个for循环吗?!

问题更详细: @askewchan 不幸的是,这个并没有解决我原来的问题。 正如预期的那样,结果很好地代表了地面轨道。 Groundtrack as result of histogram 但除了我需要每个网格点的总点数这一事实外,我还需要lat / lon数组中每个lat / lon组合的单个索引以进行进一步的计算。 我们假设我有一个数组

lat(100000L,), lon(100000L,) and a third one array(100000L,)

对应于每个点的测量。我需要lat / lon中所有1°/ 1°组合的每个索引,如果满足条件,则检查数组中的该索引(100000L,)。现在假设lat / lon的索引[10000,10001,10002,...,10025]在同一网格点上。对于那些索引,我需要检查数组[10000,10001,10002,...,10025]现在是否满足条件,即np.where(array == 0)。使用cts.nonzero()我只得到直方图中的索引。但是,导致直方图值的每个点的所有信息都会丢失。希望你得到我最初的问题。

1 个答案:

答案 0 :(得分:1)

不确定我是否理解这里的目标,但您想计算每个1°部分中有多少纬度/经度对?这是直方图的作用:

lon = np.random.random(5000)*2*180 - 180
lat = np.random.random(5000)*2*82 - 82

a = np.arange(-180,181)
b = np.arange(-82,83)

np.histogram2d(lon, lat, (a,b))
#(array([[ 0.,  0.,  1., ...,  0.,  0.,  0.],
#        [ 0.,  2.,  0., ...,  0.,  0.,  1.],
#        [ 0.,  0.,  0., ...,  0.,  1.,  0.],
#        ..., 
#        [ 0.,  1.,  0., ...,  0.,  0.,  0.],
#        [ 0.,  0.,  0., ...,  0.,  0.,  0.],
#        [ 0.,  0.,  0., ...,  0.,  0.,  0.]]),

您有非零数的索引位于:

cts.nonzero()
#(array([  0,   0,   0, ..., 359, 359, 359]),
# array([  2,  23,  25, ..., 126, 140, 155]))

您也可以绘制它:

cts, xs, ys = np.histogram2d(lon, lat, (a,b))

pyplot.imshow(cts, extent=(-82,82,-180,180))

latlonghist