title: Python: passing coordinates from list to function
body: <p>I am using some code from a <a href="https://github.com/Unidata/unidata-python-workshop/blob/a56daa50d7b343c7debe93968683613642d6b9f7/notebooks/netcdf-by-coordinates.ipynb" rel="nofollow noreferrer">workshop</a> to extract data from netCDF files by the coordinates closest to my specified coordinates. When using just one set of coordinates I am able to extract the values I need without trouble as below:</p>
  
  <pre><code>import numpy as np
  import netCDF4
  from math import pi
  from numpy import cos, sin
  
  def tunnel_fast(latvar,lonvar,lat0,lon0):
      '''
      Find closest point in a set of (lat,lon) points to specified point
      latvar - 2D latitude variable from an open netCDF dataset
      lonvar - 2D longitude variable from an open netCDF dataset
      lat0,lon0 - query point
      Returns iy,ix such that the square of the tunnel distance
      between (latval[it,ix],lonval[iy,ix]) and (lat0,lon0)
      is minimum.
      '''
  
      rad_factor = pi/180.0 # for trignometry, need angles in radians
      # Read latitude and longitude from file into numpy arrays
      latvals = latvar[:] * rad_factor
      lonvals = lonvar[:] * rad_factor
      ny,nx = latvals.shape
      lat0_rad = lat0 * rad_factor
      lon0_rad = lon0 * rad_factor
      # Compute numpy arrays for all values, no loops
      clat,clon = cos(latvals),cos(lonvals)
      slat,slon = sin(latvals),sin(lonvals)
      delX = cos(lat0_rad)*cos(lon0_rad) - clat*clon
      delY = cos(lat0_rad)*sin(lon0_rad) - clat*slon
      delZ = sin(lat0_rad) - slat;
      dist_sq = delX**2 + delY**2 + delZ**2
      minindex_1d = dist_sq.argmin()  # 1D index of minimum element
      iy_min,ix_min = np.unravel_index(minindex_1d, latvals.shape)
      return iy_min,ix_min
  
  ncfile = netCDF4.Dataset('E:\wind_level2_1.nc', 'r')
  latvar = ncfile.variables['latitude']
  lonvar = ncfile.variables['longitude']
  
  #_________GG turbine_________GAD10  Latitude    51.735516, GAD10    Longitude   1.942656
  
  iy,ix = tunnel_fast(latvar, lonvar, 51.735516, 1.942656)
  print('Closest lat lon:', latvar[iy,ix], lonvar[iy,ix])
  
  refLAT=latvar[iy,ix]
  refLON = lonvar[iy,ix]
  #try to find the data for this location
  
  SARwind = ncfile.variables['sar_wind'][:,:]
  ModelWind = ncfile.variables['model_speed'][:,:]
  
  print 'iy,ix' #appears to be the index of the value of Lat,lon
  print SARwind[iy,ix]
  
  
  ncfile.close()
  </code></pre>
  
  <p>Now I am trying to loop through a text file containing coordinates <code>coord_list</code> to extract sets of coordinates, find the data then move to the next set of coordinates in the list. This code works on it's own as below:</p>
  
  <pre><code>import csv
  from decimal import Decimal
  with open('Turbine_locs_no_header.csv','rb') as f:
      reader = csv.reader(f)
      #coord_list = list(reader)
      coord_list = [reader]
      end_row = len(coord_list)
  
      lon_ind=1
      lat_ind=2
  
  for row in range(0, end_row-1):#end_row - 1 due to the 0 index
      turbine_lat = coord_list[row][lat_ind]
      turbine_lon = coord_list[row][lon_ind]
      turbine_lat = [Decimal(turbine_lat)]
      print 'lat',turbine_lat, 'lon',turbine_lon, row
  </code></pre>
  
  <p>However, I want to pass coordinates from the text file to this part of the original code <code>iy,ix = tunnel_fast(latvar, lonvar, 51.94341, 1.922094888)</code>, replacing the numbers with variables <code>iy, ix = tunnel_fast(latvar, lonvar, turbine_lat, turbine_lon)</code>. I try to combine the two codes by creating a function <code>get_coordinates</code>, I get the following errors</p>
  
  <pre><code>  File "C:/Users/mm/test_nc_bycoords_GG_turbines_AGW.py", line 65, in &lt;module&gt;
      get_coordinates(coord_list, latvar, lonvar)
    File "C:/Users/mm/test_nc_bycoords_GG_turbines_AGW.py", line 51, in get_coordinates
      iy, ix = tunnel_fast(latvar, lonvar, turbine_lat, turbine_lon)
    File "C:/Users/mm/test_nc_bycoords_GG_turbines_AGW.py", line 27, in tunnel_fast
      lat0_rad = lat0 * rad_factor
  TypeError: can't multiply sequence by non-int of type 'float'
  </code></pre>
  
  <p>I thought this is because the <code>turbine_lat</code> and <code>turbine_lon</code> are list items so cannot be used, but this doesn't seem to be connected to the errors. I know this code needs more work anyway, but if anyone could help me spot where I am going wrong that would be very helpful. My attempt to combine the two codes is below.</p>
  
  <pre><code>import numpy as np
  import netCDF4
  from math import pi
  from numpy import cos, sin
  import csv
  
  # edited from https://github.com/Unidata/unidata-python-workshop/blob/a56daa50d7b343c7debe93968683613642d6b9f7/notebooks/netcdf-by-coordinates.ipynb
  
  def tunnel_fast(latvar,lonvar,lat0,lon0):
      '''
      Find closest point in a set of (lat,lon) points to specified point
      latvar - 2D latitude variable from an open netCDF dataset
      lonvar - 2D longitude variable from an open netCDF dataset
      lat0,lon0 - query point
      Returns iy,ix such that the square of the tunnel distance
      between (latval[it,ix],lonval[iy,ix]) and (lat0,lon0)
      is minimum.
      '''
  
      rad_factor = pi/180.0 # for trignometry, need angles in radians
      # Read latitude and longitude from file into numpy arrays
      latvals = latvar[:] * rad_factor
      lonvals = lonvar[:] * rad_factor
      ny,nx = latvals.shape
      lat0_rad = lat0 * rad_factor
      lon0_rad = lon0 * rad_factor
      # Compute numpy arrays for all values, no loops
      clat,clon = cos(latvals),cos(lonvals)
      slat,slon = sin(latvals),sin(lonvals)
      delX = cos(lat0_rad)*cos(lon0_rad) - clat*clon
      delY = cos(lat0_rad)*sin(lon0_rad) - clat*slon
      delZ = sin(lat0_rad) - slat;
      dist_sq = delX**2 + delY**2 + delZ**2
      minindex_1d = dist_sq.argmin()  # 1D index of minimum element
      iy_min,ix_min = np.unravel_index(minindex_1d, latvals.shape)
      return iy_min,ix_min
  #________________my edits___________________________________________________
  def get_coordinates(coord_list, latvar, lonvar):
      "this takes coordinates from a .csv and assigns them to variables"
  
      end_row = len(coord_list)
  
      lon_ind=1
      lat_ind=2
  
      for row in range(0, end_row-1):#end_row - 1 due to the 0 index
          turbine_lat = coord_list[row][lat_ind]
          turbine_lon = coord_list[row][lon_ind]
          iy, ix = tunnel_fast(latvar, lonvar, turbine_lat, turbine_lon)
          print('Closest lat lon:', latvar[iy, ix], lonvar[iy, ix])
  
  #________________________________________________________________________________________________________________________
  ncfile = netCDF4.Dataset('NOGAPS_wind_level2_1.nc', 'r')
  latvar = ncfile.variables['latitude']
  lonvar = ncfile.variables['longitude']
  
  #____added in to pass to get coordinates function
  with open('Turbine_locs_no_header.csv','rb') as f:
      reader = csv.reader(f)
      coord_list = list(reader)
  #_________take latitude from coordinateas function
  
  get_coordinates(coord_list, latvar, lonvar)
  
  #iy,ix = tunnel_fast(latvar, lonvar, turbine_lat, turbine_lon)#get these from the 'assign_coordinates_fromlist.py
  #print('Closest lat lon:', latvar[iy,ix], lonvar[iy,ix])
  
  SARwind = ncfile.variables['sar_wind'][:,:]
  ModelWind = ncfile.variables['model_speed'][:,:]
  
  print 'iy,ix' #appears to be the index of the value of Lat,lon
  print SARwind[iy,ix]
  
  
  ncfile.close()
  </code></pre>
  
  <p>When I try to convert </p>