indexing issues when extracting specified latitude and longitude
<p>I want to extract a specified latitude and longitude from a <code>netCDF</code> file. In the past, I have never had issues with extracting the data. I am assuming that the reason it is not working this time is because I read in my data differently (see below)</p>

<pre><code>data = netCDF4.Dataset('/home/eburrows/metr173/regional_cm/Lab1/air.mon.mean.nc', mode = 'r')
lat = data.variables['lat'][:] #90 through -90
lon = data.variables['lon'][:] #0 through 360
air_temp = data.variables['air'][:] #degrees C
air_temp[air_temp&gt;10000] = n.NaN
</code></pre>

<p>Previously I have been able to do the following:</p>

<pre><code>us_lat = n.ravel(n.where((lat&gt;=___)&amp;(lat&lt;=___)))
us_lon = n.ravel(n.where((lon&gt;=___)&amp;(lon&lt;=___)))
us_annual_temp = n.nanmean(air_temp[:,us_lat, us_lon],0)
</code></pre>

<p>This time however, it is returning a <code>Type Error</code> stating that <code>list indices must be integers, not tuple</code>.</p>

<p>I then forced the <code>tuple</code> into a <code>list</code> by changing <code>us_lat</code> and <code>us_lon</code> to have <code>list(n.ravel(n.where(...))</code>, but it still returns the same error. In the past I have been able to index this way and am not entirely sure why it is not working this time around.</p>

<p>The results of <code>lat_us</code> from the <code>where</code> command are a tuple of indices, not the actual indices that are needed for slicing <code>air_temp</code>.  To fix this, you need to index the first result from <code>lat_us</code> to access the array of latitude indices.  </p>

<p>For instance,</p>

<pre><code>&gt;&gt;&gt; import numpy as np
&gt;&gt;&gt; lat = np.arange(-90,91,10)
&gt;&gt;&gt; lat
array([-90, -80, -70, -60, -50, -40, -30, -20, -10,   0,  10,  20,  30,
    40,  50,  60,  70,  80,  90])
&gt;&gt;&gt; lat_us = np.where((lat &gt;= -30) &amp; (lat &lt;= 30))
&gt;&gt;&gt; lat_us
(array([ 6,  7,  8,  9, 10, 11, 12]),)
&gt;&gt;&gt; lat_us[0]
array([ 6,  7,  8,  9, 10, 11, 12])
</code></pre>

<p>So the line </p>

<pre><code>us_lat = n.ravel(n.where((lat&gt;=___)&amp;(lat&lt;=___)))
</code></pre>

<p>Should be modified to (note: I don't think you need to ravel this either):</p>

<pre><code>us_lat = n.where((lat&gt;=___) &amp; (lat&lt;=___))[0]  
</code></pre>

<p>Also, you are currently reading in only one dimensional for the variable <code>air_temp</code>, but it appears to be 3D (time x lat x lon).  So you need to modify the read in of this variable to include all three dimensions:</p>

<pre><code>air_temp = data.variables['air'][:,:,:] 
</code></pre>

python list indexing tuples typeerror