<div dir="ltr">perfect, thank you!<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 24, 2015 at 9:14 PM, Kiko <span dir="ltr"><<a href="mailto:kikocorreoso@gmail.com" target="_blank">kikocorreoso@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2015-03-24 11:02 GMT+01:00 questions anon <span dir="ltr"><<a href="mailto:questions.anon@gmail.com" target="_blank">questions.anon@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div>I would like to find the nearest coord in a netcdf from a given latitude and longitude.<br>I found some fantastic code that does this - <a href="http://nbviewer.ipython.org/github/Unidata/unidata-python-workshop/blob/master/netcdf-by-coordinates.ipynb" target="_blank">http://nbviewer.ipython.org/github/Unidata/unidata-python-workshop/blob/master/netcdf-by-coordinates.ipynb</a><br>but I keep receiving this error - I am receiving a ValueError: need more than 1 value to unpack </div><p style="margin:0px;text-indent:0px">I have pasted the code and full error below. Any help will be greatly appreciated. <br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">

</p><p style="margin:0px;text-indent:0px">import numpy as np</p>
<p style="margin:0px;text-indent:0px">import netCDF4</p>
<p style="margin:0px;text-indent:0px"><br></p>
<p style="margin:0px;text-indent:0px">def naive_fast(latvar,lonvar,lat0,lon0):</p>
<p style="margin:0px;text-indent:0px">      # Read latitude and longitude from file into numpy arrays</p>
<p style="margin:0px;text-indent:0px">      latvals = latvar[:]</p>
<p style="margin:0px;text-indent:0px">      lonvals = lonvar[:]</p>
<p style="margin:0px;text-indent:0px">      ny,nx = latvals.shape</p>
<p style="margin:0px;text-indent:0px">      dist_sq = (latvals-lat0)**2 + (lonvals-lon0)**2</p>
<p style="margin:0px;text-indent:0px">   minindex_flattened = dist_sq.argmin()  # 1D index of min element</p>
<p style="margin:0px;text-indent:0px">      iy_min,ix_min = np.unravel_index(minindex_flattened, latvals.shape)</p>
<p style="margin:0px;text-indent:0px">      return iy_min,ix_min</p>
<p style="margin:0px;text-indent:0px">filename = "/Users/T_SFC.nc"</p>
<p style="margin:0px;text-indent:0px">ncfile = netCDF4.Dataset(filename, 'r')</p>
<p style="margin:0px;text-indent:0px">latvar = ncfile.variables['latitude']</p>
<p style="margin:0px;text-indent:0px">lonvar = ncfile.variables['longitude']</p>
<p style="margin:0px;text-indent:0px"><br></p>
<p style="margin:0px;text-indent:0px">iy,ix = naive_fast(latvar, lonvar, -38.009, 146.438)</p>
<p style="margin:0px;text-indent:0px">print 'Closest lat lon:', latvar[iy,ix], lonvar[iy,ix]</p>
<p style="margin:0px;text-indent:0px">ncfile.close()</p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px"><br></p><p style="margin:0px;text-indent:0px">---------------------------------------------------------------------------<br>ValueError                                Traceback (most recent call last)<br>/Applications/Canopy.app/appdata/canopy-1.3.0.1715.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)<br>    202             else:<br>    203                 filename = fname<br>--> 204             __builtin__.execfile(filename, *where)<br><br>/Users/latlon_to_closestgrid.py in <module>()<br>     22 lonvar = ncfile.variables['longitude']<br>     23 <br>---> 24 iy,ix = naive_fast(latvar, lonvar, -38.009, 146.438)<br>     25 print 'Closest lat lon:', latvar[iy,ix], lonvar[iy,ix]<br>     26 ncfile.close()<br><br>/Users/latlon_to_closestgrid.py in naive_fast(latvar, lonvar, lat0, lon0)<br>     12     latvals = latvar[:]<br>     13     lonvals = lonvar[:]<br>---> 14     ny,nx = latvals.shape<br>     15     dist_sq = (latvals-lat0)**2 + (lonvals-lon0)**2<br>     16     minindex_flattened = dist_sq.argmin()  # 1D index of min element<br><br>ValueError: need more than 1 value to unpack <br><br></p>
<p style="margin:0px;text-indent:0px"><br></p></div></blockquote><div><br></div></div></div><div>It seems that latvals and lonvals should be a 2D array and you are providing just a 1D array.</div><div>Maybe you could use numpy.meshgrid [1] to get 2D inputs from 1D arrays.</div><div><br></div><div>[1] <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html" target="_blank">http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html</a><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><p style="margin:0px;text-indent:0px"></p>
<p style="margin:0px;text-indent:0px"><br></p>
<p style="margin:0px;text-indent:0px"><br></p>
<p style="margin:0px;text-indent:0px"><br></p></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org" target="_blank">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div></div>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br></div>