perfect, thank you! On Tue, Mar 24, 2015 at 9:14 PM, Kiko <kikocorre...@gmail.com> wrote:
> > > 2015-03-24 11:02 GMT+01:00 questions anon <questions.a...@gmail.com>: > >> I would like to find the nearest coord in a netcdf from a given latitude >> and longitude. >> I found some fantastic code that does this - >> http://nbviewer.ipython.org/github/Unidata/unidata-python-workshop/blob/master/netcdf-by-coordinates.ipynb >> but I keep receiving this error - I am receiving a ValueError: need more >> than 1 value to unpack >> >> I have pasted the code and full error below. Any help will be greatly >> appreciated. >> >> >> >> >> import numpy as np >> >> import netCDF4 >> >> >> def naive_fast(latvar,lonvar,lat0,lon0): >> >> # Read latitude and longitude from file into numpy arrays >> >> latvals = latvar[:] >> >> lonvals = lonvar[:] >> >> ny,nx = latvals.shape >> >> dist_sq = (latvals-lat0)**2 + (lonvals-lon0)**2 >> >> minindex_flattened = dist_sq.argmin() # 1D index of min element >> >> iy_min,ix_min = np.unravel_index(minindex_flattened, latvals.shape) >> >> return iy_min,ix_min >> >> filename = "/Users/T_SFC.nc" >> >> ncfile = netCDF4.Dataset(filename, 'r') >> >> latvar = ncfile.variables['latitude'] >> >> lonvar = ncfile.variables['longitude'] >> >> >> iy,ix = naive_fast(latvar, lonvar, -38.009, 146.438) >> >> print 'Closest lat lon:', latvar[iy,ix], lonvar[iy,ix] >> >> ncfile.close() >> >> >> >> >> >> >> --------------------------------------------------------------------------- >> ValueError Traceback (most recent call >> last) >> /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) >> 202 else: >> 203 filename = fname >> --> 204 __builtin__.execfile(filename, *where) >> >> /Users/latlon_to_closestgrid.py in <module>() >> 22 lonvar = ncfile.variables['longitude'] >> 23 >> ---> 24 iy,ix = naive_fast(latvar, lonvar, -38.009, 146.438) >> 25 print 'Closest lat lon:', latvar[iy,ix], lonvar[iy,ix] >> 26 ncfile.close() >> >> /Users/latlon_to_closestgrid.py in naive_fast(latvar, lonvar, lat0, lon0) >> 12 latvals = latvar[:] >> 13 lonvals = lonvar[:] >> ---> 14 ny,nx = latvals.shape >> 15 dist_sq = (latvals-lat0)**2 + (lonvals-lon0)**2 >> 16 minindex_flattened = dist_sq.argmin() # 1D index of min >> element >> >> ValueError: need more than 1 value to unpack >> >> >> > It seems that latvals and lonvals should be a 2D array and you are > providing just a 1D array. > Maybe you could use numpy.meshgrid [1] to get 2D inputs from 1D arrays. > > [1] > http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html > > >> >> >> >> >> _______________________________________________ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> >> > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@scipy.org > http://mail.scipy.org/mailman/listinfo/numpy-discussion > >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion