0

Overview: I am struggling to understand where I am going wrong with this nested mask using np.where. What I am hoping for is- if snow is true, assign 1, if false evaluate the second np.where, which tests if no_snow is true, assign 0, if false (meaning that snow is false and no_snow if false) then assign 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, :])

Error:

    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

Help me, stackoverflow. You're my only hope.

5
  • @Jérôme ims is an attribute from an hdf file that has a shape of (6574, 2005409) and represents snow cover. It is called as a global variable. Commented Oct 20, 2016 at 15:37
  • PyCharm is suggesting I use if cond is True: or if cond: How would that apply in this line of code and would the result be the same? Commented Oct 20, 2016 at 15:52
  • Isn't no_snow an empty array? Commented Oct 20, 2016 at 16:43
  • @Mahdi I don't think so?... It's length is 2005409 and printing to the console renders [False False False ..., False False False] Commented Oct 20, 2016 at 16:56
  • 1
    Isolate the problem expression, and test it with some small arrays. Make sure you understand what where is doing. The purpose of that where expression is not obvious. Commented Oct 20, 2016 at 18:10

1 Answer 1

2

From help(np.where):

where(condition, [x, y])

Return elements, either from `x` or `y`, depending on `condition`.

If only `condition` is given, return ``condition.nonzero()``.

Parameters
----------
condition : array_like, bool
    When True, yield `x`, otherwise yield `y`.
x, y : array_like, optional
    Values from which to choose. `x` and `y` need to have the same
    shape as `condition`.

I doubt that np.where(no_snow...) has the same shape as snow == True.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.