Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What is happening is that the lengths of each of your index (<code>where</code>) arrays is going to be different, and so the shape of your output array is ambiguous, hence the error. To force the broadcasting to the proper shape, you'll have to reshape your arrays to broadcast to a proper shape, something like this:</p> <pre><code>Windspeedsplains = Defaultwindspeed[goodtime[0][:, None, None, None],:,goodlat[0][:,None],plainslon[0]] </code></pre> <p>The <code>[0]</code> are there becuase <code>np.where(a)</code> returns a tuple of length <code>a.ndim</code> with each element of the tuple being an array of indices that fit your condition. I'm assuming all of your boolean arrays are 1d, so all the <code>where</code> outputs are going to be tuples of length 1, so we just want the one array from it, hence <code>[0]</code>.</p> <p>After we get the array, we want to reshape it to match the shape that you want your output array to have. Presumable, the shape your output should have is <code>(goodtime.size, Defaultwindspeed.shape[1], goodlat.size, plainslon.size)</code>, so you must make each of your index arrays have the shape that matches the axis along which the output array should vary for that variable. For example, for <code>goodtime</code>, you want <code>Windspeedplains</code> to vary with respect to time along axis <code>0</code> of <code>4</code> axes. So, <code>goodtime</code> itself must also vary only along axis <code>0</code> of four axes, so you force the index array to have shape <code>(N, 1, 1, 1)</code> which is what <code>[:, None, None, None]</code> does.</p> <p>So, you could make the above line more readable with:</p> <pre><code>goodtime = n.where((Time&gt;=1948)&amp;(Time&lt;2013))[0][:, None, None, None] goodlat = n.where((Lat&gt;=35)&amp;(Lat&lt;=50))[0][:, None] plainslon = n.where((Lon&gt;=275)&amp;(Lon&lt;=285))[0] Windspeedsplains=Defaultwindspeed[goodtime, :, goodlat, plainslon] </code></pre> <p>Or actually, since you can index with the boolean arrays directly:</p> <pre><code>goodtime = ((Time&gt;=1948)&amp;(Time&lt;2013))[:, None, None, None] goodlat = ((Lat&gt;=35)&amp;(Lat&lt;=50))[:, None] plainslon = ((Lon&gt;=275)&amp;(Lon&lt;=285)) Windspeedsplains=Defaultwindspeed[goodtime, :, goodlat, plainslon] </code></pre> <p>Here is a somewhat simpler example:</p> <pre><code>In [52]: a = np.arange(3*3*3).reshape(3,3,3) In [53]: a Out[53]: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) In [54]: mask0 = np.where(a[:,0,0] &gt;= 9) In [55]: mask0 Out[55]: (array([1, 2]),) # &lt;-- this is the length 1 tuple I was talking about. we want the array inside. In [56]: mask1 = np.where(a[0,:,0]%2 == 0) In [57]: mask1 Out[57]: (array([0, 2]),) In [62]: mask2 = np.where(a[0,0,:] &lt; 1) In [63]: mask2 Out[63]: (array([0]),) In [67]: b = a[mask0[0][:, None, None], mask1[0][:, None], mask2[0]] In [68]: b Out[68]: array([[[ 9], [15]], [[18], [24]]]) In [69]: b.shape Out[69]: (2, 2, 1) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload