"IndexError: too many indices" using HDF5 file with 8760 - every hour for the year
<p><strong>Background:</strong> I am trying to pull out the daily max windspeed from an 8760 hdf5 dataset using a kdtree to lat/lons from a point file and then pull it all into a csv. Things go smoothly, until I try to make an empty array to store the max values. I am having a hard time wrapping my head around the logic needed here. </p>

<p>I have browsed other questions and from other answers think I may have a one-dimensional array and I'm trying to slice it with two dimensions. But not sure. Any help would be appreciated.</p>

<pre><code># Store X / Y coordinates
x_vector = []
y_vector = []

# Open dummy HDF5 file
hf = h5py.File(dir + 'h5/wspd_2014.h5', 'r')

# Break out attributes of the data
ws = hf['wspd'][...]
meta = hf['meta'][...]

# Get coordinates of HDF5 file
coords = np.vstack([meta['latitude'], meta['longitude']]).T

# Open the vector data
with fiona.open(dir + 'shapefiles/soil_winderode4.shp', 'r') as vector:
    # create array to store xy coordinates
    coords_arr = []
    # get the coordinates from the shapefile
    for feature in vector:
        coords = feature['geometry']['coordinates']
        coords_arr.append(coords)

# Run a loop using the coordinates and split the x and y
for x, y in coords_arr:
    x_vector.append(x)
    y_vector.append(y)

# Run the kdtree to match nearest values
tree = cKDTree(np.vstack([x_vector, y_vector]).T)
kdtree_indices = tree.query(coords)[1]

# ------ Match WSPD, lat/lon &amp; Save to CSV -------
ws_sites = ws[:, kdtree_indices]
indices = range(0, 8760, 24)
indices.append(8759)
</code></pre>

<p><strong>Issue:</strong> This is where things get wonky, I make an empty numpy array using days (365) and the shape of windspeed (194940) in the <code>for</code>loop and then my intent is to get the max value for every day (ind).</p>

<pre><code>out = np.empty(shape=(365, ws.shape[1]))

for ind in range(len(indices)-1):
    out[ind, :] = ws_sites[indices[ind]: indices[ind + 1], :].max(axis=0)
</code></pre>

<p><strong>As per request here is the traceback:</strong></p>

<pre><code>Traceback (most recent call last):
194940
  File "meera_extract.py", line 56, in &lt;module&gt;
out[ind, :] = ws_sites[indices[ind]: indices[ind + 1], :].max(axis=0)
IndexError: too many indices for array
</code></pre>

<p>First, there was some indentation missing below the <code>with ... a vector:</code>.  I added my guess on that.  But I'm guessing all of that <code>hdf5</code> stuff is unimportant - at least for your problem.</p>

<p>The problem is all at the end - if I read your question right:</p>

<pre><code>ws_sites = ws[:, kdtree_indices]
indices = range(0, 8760, 24)
indices.append(8759)
#.... 
out = np.empty(shape=(365, ws.shape[1]))

for ind in range(len(indices)-1):
    out[ind, :] = ws_sites[indices[ind]: indices[ind + 1], :].max(axis=0)
</code></pre>

<p>From this I deduce that <code>indices</code> is a list with values in the range 0 8760, 366 values in all.  <code>out</code> is 2d, so indexing it with <code>ind</code> should be fine.</p>

<p>But what about <code>ws_sites</code>?  What is its shape?  Any time you have indexing errors, check the shapes.  Often they aren't what you might guess.  You are indexing it as though it were 2d, eg.</p>

<pre><code>ws_sites[24:48, :]
</code></pre>

<p>It looks like <code>ws</code> is 2d.  But if <code>kdtree_indices</code> is a single number, <code>ws_sites = ws[:, kdtree_indices]</code> will be 1d, producing the <code>too many indices</code>.  </p>

<p>The error has to do with the number of dimensions, not their size.  Trying to index with too high a number will produce a different error.</p>

python arrays numpy