[Numpy-discussion] netcdf lat lon to coord - ValueError: need more than 1 value to unpack

questions anon questions.anon at gmail.com
Tue Mar 24 06:02:31 EDT 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150324/f1d54132/attachment.html>


More information about the NumPy-Discussion mailing list