嵌套的np.where和布尔数组索引问题

时间:2016-10-20 15:32:32

标签: python python-2.7 numpy

概述:我正在努力通过np.where了解这个嵌套蒙版出错的地方。我希望的是 - 如果雪是真的,分配1,如果是假,则评估第二个np.where,如果no_snow为真则测试,分配0,如果为false(意味着雪是假,no_snow,如果为假)则分配2

# open IMS & pull necessary keys.
hf = h5py.File(ims_dir + 'ims_daily_snow_cover.h5', 'r')
ims = hf['snow_cover'][...]


# create an empty parameter to be later written to new hdf file as gap_fill_flag.
dataset_fill = np.zeros(ims.shape)

# loop through fill - branch based on temporal fill or merra fill.
for day in range(len(fill)):
    # print len(day)
    print day
    fill[day] == 2
    year = days[day][:4]
    # merra fill - more than one consecutive day missing.
    if (fill[day-1] == 2) | (fill[day+1] == 2):
        # run merra_fill function
        # fill with a 2 to signify data are filled from merra.
        ims[day, :] = merra_fill(days[day], ims[day, :])
        dataset_fill[day, :] = 2
    else:
        # temporal_fill - less than one consecutive day missing.
        snow = ((ims[day - 1:day+2, :] == 1).sum(axis=0)) == 2
        no_snow = ((ims[day - 1:day+2, :] == 0).sum(axis=0)) == 2
        # nested np.where.
        ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2))

        dataset_fill[day, :][ims[day, :] < 2] = 1
        dataset_fill[day, :][ims[day, :] == 2] = 2

        ims[day, :][ims[day, :] == 2] = merra_fill(days[day], ims[day, :])

错误:

    ims[day, :] = np.where(snow == True, 1, np.where(no_snow == True, 0, 2))
ValueError: NumPy boolean array indexing assignment cannot assign 2005409 input values to the 0 output values where the mask is true

帮帮我,stackoverflow。你是我唯一的希望。

1 个答案:

答案 0 :(得分:2)

来自帮助(np.where):

np.where(no_snow...)

我怀疑snow == True的形状与{{1}}相同。