我一年中的每一天都有365个2d numpy阵列,显示的图像如下:

我把它们都堆成了一个3Dnumpy数组。我想要搜索前7天,或未来7天(前7层,下7层)以找到云以外的值,然后用该像素的其他可能值(相应像素在其他天/层中经历的值)替换云值。
我是python的新手,有点迷失方向。
有什么想法吗?
谢谢
发布于 2012-12-31 20:35:49
我通过以下方式解决了这个问题:
interpdata = []
j = 0
for i in stack:
try:
temp = np.where( stack[j] == 50, stack[j-1], modis[j] )
temp = np.where( temp == 50, stack[j+1], temp )
temp = np.where( temp == 50, stack[j-2], temp )
temp = np.where( temp == 50, stack[j+2], temp )
temp = np.where( temp == 50, stack[j-3], temp )
temp = np.where( temp == 50, stack[j+3], temp )
temp = np.where( temp == 50, stack[j-4], temp )
temp = np.where( temp == 50, stack[j+4], temp )
except IndexError:
print 'IndexError Passed'
pass
else:
pass
interpdata [j, :, :] = temp
j = j + 1 发布于 2012-11-29 02:36:22
您实际上是在尝试为您的数组编写一个过滤器。
在你的例子中,该函数将接受一维数组,并返回最接近中间索引的元素,而不是cloud:
import numpy as np
from scipy.ndimage.filters import generic_filter
_cloud = -1
def findNearestNonCloud(elements):
middleIndex = len(elements) / 2
if elements[middleIndex] != _cloud:
return elements[middleIndex] # middle value is not cloud
nonCloudIndices, = np.where(elements != _cloud)
if len(nonCloudIndices) == 0:
return elements[middleIndex] # all values were cloud
prevNonCloudIndex = np.where(nonCloudIndices < middleIndex,
nonCloudIndices, -1).max()
nextNonCloudIndex = -np.where(nonCloudIndices > middleIndex,
-nonCloudIndices, 1).min()
# -1 means no non-cloud index
# pick index closest to middle index
if (abs(prevNonCloudIndex - middleIndex)
<= abs(nextNonCloudIndex - middleIndex)):
return elements[prevNonCloudIndex]
else:
return elements[nextNonCloudIndex]现在,您需要将此函数应用于您感兴趣的元素。要做到这一点,您需要一个掩码来表示您对特定元素感兴趣的其他元素。
from scipy.ndimage.filters import generic_filter
# creates 5 days worth of a 3x3 plot of land
input = np.ones((5, 3, 3)) * _cloud
input[0,:,:] = 10 # set first "image" to all be 10s
input[4,0,0] = 12 # uppper left corner of fourth image is set to 12
print "input data\n", input, "\n"
mask = (5, 1, 1)
# mask represents looking at the present day, 2 days in the future and 2 days in
# the past for 5 days in total.
print "result\n", generic_filter(input, findNearestNonCloud, size=mask)
# second and third images should mirror first image,
# except upper left corner of third image should be 12发布于 2012-11-28 23:28:15
我认为你可以这样做:
data = somehow_get_your_3d_data() #indexed as [day_of_year,y,x]
for i,dat in enumerate(data):
weeks2 = data[max(i-7,i):min(i+7,len(data)), ... ]
new_value = get_new_value(weeks2) #get value from weeks2 here somehow
dat[dat == cloud_value] = new_valuehttps://stackoverflow.com/questions/13608029
复制相似问题