title: Getting NA with Akima Interpolation in R
body: <p>I'm attempting to interpolate some gridded values I have to a point on a road segment using the akima packages interp function. The data points are not always all there due to the data being missing at those times. For example, see the graph below.</p>
  
  <p><a href="https://i.stack.imgur.com/OCOjI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OCOjI.png" alt="![Gridded Values"></a></p>
  
  <p>The lat/lng points shown are those that have dBz values from my dataset (if there is no value, our database doesn't record an entry). My goal is to use this grid to interpolate a dBz value to a specific lat/lng point (i.e. latitude = 39.95045 and longitude = -86.3574)</p>
  
  <p>My data is as follows...</p>
  
  <pre><code>mrms
     tstamp: POSIXct: format: "2018-06-09 16:56:00" ...
     dBz: int 27 26 50 ...
     latitude: num 39.9 39.9 39.9 ...
     longitude: num -86.4 -86.4 -86.4
  </code></pre>
  
  <p>If I subset my data for a specific time (because there are multiple entries per time stamp. My data looks like the following.</p>
  
  <pre><code>        tstamp     dBz  latitude    longitude
  
  1   6/9/2018 16:58  50  39.92501    -86.37501
  2   6/9/2018 16:58  52  39.93498    -86.36501
  3   6/9/2018 16:58  29  39.93498    -86.355
  4   6/9/2018 16:58  38  39.91499    -86.37501
  5   6/9/2018 16:58  44  39.92501    -86.36501
  6   6/9/2018 16:58  23  39.92501    -86.355
  7   6/9/2018 16:58  52  39.93498    -86.37501
  8   6/9/2018 16:58  50  39.94498    -86.36501
  9   6/9/2018 16:58  23  39.90501    -86.36501
  10  6/9/2018 16:58  51  39.94498    -86.37501
  11  6/9/2018 16:58  18  39.90501    -86.37501
  12  6/9/2018 16:58  37  39.91499    -86.36501
  </code></pre>
  
  <p>After subsetting the data, I run the following command ...</p>
  
  <pre><code>interpolated &lt;- interp(x = mrms_sub$longitude, y = mrms_sub$latitude, z = mrms_sub$dBz, xo = road$longitude, yo = road$latitude, linear = TRUE)
  </code></pre>
  
  <p>Which produces x = num -86.4, y = num 40, and z = num NA</p>
  
  <p>I have no clue why interp keeps producing an NA value. I had this method working previously when I read all of the data in through a csv file. I've checked over datatypes for my csv method vs pulling the data in from SQL Server into a dataframe and they're the same. Any thoughts or suggestions are greatly appreciated!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>require(tidyverse)
  require(RODBC)
  require(akima)
  
  ##### Road Details #####
  cnx_wx &lt;- odbcConnect("dbName", uid = "", pwd = "")
  cnx_rd &lt;- odbcConnect("dbName", uid = "", pwd = "")
  
  queryString &lt;- 
    "SELECT
            XDSegID,
        RoadNumber,
        RoadName,
        Bearing,
        geog.EnvelopeCenter().STAsText() as roadCenter
      FROM [itsdb1].[inrix_xd].[dbo].[__xd] as inrix_xd
      WHERE XDSegID = 1363484955 AND version = '2018-04-25'"
  
  road &lt;- sqlQuery(cnx_rd, queryString)
  
  #Massage road center lat long points
  road$roadCenter &lt;- gsub("POINT|\\(|\\)", "", road$roadCenter)
  road = separate(road, roadCenter, into = c("junk", "longitude", "latitude"), sep = " ")
  road$longitude &lt;- as.numeric(road$longitude)
  road$latitude &lt;- as.numeric(road$latitude)
  
  
  ##### Speed Data #####
  queryString &lt;- 
    "SELECT
        CAST(tstamp as smalldatetime) as tstamp,
        xdid,
        speed,
        score,
        cvalue
      FROM itsdb1.inrix_xd.dbo.xdspeeds
      WHERE tstamp &gt;= '2018-06-09 15:00:00' AND tstamp &lt;= '2018-06-09 18:00:00'
      AND xdid = 1363484955
      ORDER BY tstamp"
  
  speed &lt;- sqlQuery(cnx_rd, queryString)
  #attr(speed$tstamp, "tzone") &lt;- "GMT"
  odbcClose(cnx_rd)
  
  
  ##### MRMS Data #####
  queryString &lt;- 
    "SELECT
        CAST(tstamp as smalldatetime) as tstamp,
        dBz,
        latitude,
        longitude
      FROM weather_mrms.dbo.v_seamless_hsr
      WHERE tstamp &gt;= '2018-06-09 15:00:00' AND tstamp &lt;= '2018-06-09 18:00:00'
      AND latitude BETWEEN 39.90 AND 39.95 AND longitude BETWEEN -86.38 AND -86.29
      ORDER BY tstamp"
  
  mrms &lt;- sqlQuery(cnx_wx, queryString)
  odbcClose(cnx_wx)
  
  
  ##### Combined Data #####
  numOfwxLevels &lt;- nlevels(as.factor(mrms$tstamp))
  
  i &lt;- 1
  obsTime &lt;- mrms$tstamp[2]
  
  output &lt;- data.frame(0,0)
  colnames(output) &lt;- c("tstamp", "Interpolated dBz")
  
  while (i &lt;= numOfwxLevels) {
    mrms_sub &lt;- filter(mrms, mrms$tstamp == obsTime)
  
    interpolated &lt;- akima::interp(x = mrms_sub$longitude, y = mrms_sub$latitude, z = mrms_sub$dBz,
                           xo = road$longitude, yo = road$latitude, linear = TRUE)
  
    val &lt;- c(obsTime, interpolated$z)
    newTable &lt;- rbind(output, val)
    obsTime &lt;- obsTime + 120
    i &lt;- i + 1
  
  }
  </code></pre>