在R中通过Akima插值获得NA

时间:2018-06-26 15:05:06

标签: r interpolation

我正在尝试使用akima包interp函数将一些网格化值插值到路段上的某个点。数据点并不总是在那里,因为那时候数据丢失了。例如,请参见下图。

![Gridded Values

显示的经/纬度点是那些具有我的数据集中的dBz值的点(如果没有值,则我们的数据库不会记录条目)。我的目标是使用此网格将dBz值插值到特定的经度/纬度点(即纬度= 39.95045和经度= -86.3574)

我的数据如下...

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

如果我在特定时间子集数据(因为每个时间戳有多个条目。我的数据如下)。

        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

对数据进行子集设置后,我运行以下命令...

interpolated <- interp(x = mrms_sub$longitude, y = mrms_sub$latitude, z = mrms_sub$dBz, xo = road$longitude, yo = road$latitude, linear = TRUE)

产生x = -86.4,y = num 40,z = num NA

我不知道为什么interp会不断产生NA值。当我通过csv文件读取所有数据时,我曾经使用过这种方法。我已经检查过csv方法的数据类型,而不是将数据从SQL Server提取到数据帧中,并且它们是相同的。任何想法或建议都将不胜感激!

更新

require(tidyverse)
require(RODBC)
require(akima)

##### Road Details #####
cnx_wx <- odbcConnect("dbName", uid = "", pwd = "")
cnx_rd <- odbcConnect("dbName", uid = "", pwd = "")

queryString <- 
  "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 <- sqlQuery(cnx_rd, queryString)

#Massage road center lat long points
road$roadCenter <- gsub("POINT|\\(|\\)", "", road$roadCenter)
road = separate(road, roadCenter, into = c("junk", "longitude", "latitude"), sep = " ")
road$longitude <- as.numeric(road$longitude)
road$latitude <- as.numeric(road$latitude)


##### Speed Data #####
queryString <- 
  "SELECT
      CAST(tstamp as smalldatetime) as tstamp,
      xdid,
      speed,
      score,
      cvalue
    FROM itsdb1.inrix_xd.dbo.xdspeeds
    WHERE tstamp >= '2018-06-09 15:00:00' AND tstamp <= '2018-06-09 18:00:00'
    AND xdid = 1363484955
    ORDER BY tstamp"

speed <- sqlQuery(cnx_rd, queryString)
#attr(speed$tstamp, "tzone") <- "GMT"
odbcClose(cnx_rd)


##### MRMS Data #####
queryString <- 
  "SELECT
      CAST(tstamp as smalldatetime) as tstamp,
      dBz,
      latitude,
      longitude
    FROM weather_mrms.dbo.v_seamless_hsr
    WHERE tstamp >= '2018-06-09 15:00:00' AND tstamp <= '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 <- sqlQuery(cnx_wx, queryString)
odbcClose(cnx_wx)


##### Combined Data #####
numOfwxLevels <- nlevels(as.factor(mrms$tstamp))

i <- 1
obsTime <- mrms$tstamp[2]

output <- data.frame(0,0)
colnames(output) <- c("tstamp", "Interpolated dBz")

while (i <= numOfwxLevels) {
  mrms_sub <- filter(mrms, mrms$tstamp == obsTime)

  interpolated <- akima::interp(x = mrms_sub$longitude, y = mrms_sub$latitude, z = mrms_sub$dBz,
                         xo = road$longitude, yo = road$latitude, linear = TRUE)

  val <- c(obsTime, interpolated$z)
  newTable <- rbind(output, val)
  obsTime <- obsTime + 120
  i <- i + 1

}

1 个答案:

答案 0 :(得分:0)

阅读help file for it会带来两件事:1)推荐colinear分; 2)无法使用linear interpolation来推断,这就是为什么NA位于您的数据之外。

如果您使用其他插值,它将起作用,但是我怀疑那是您想要的。您可能需要看看另一种方法。

df1terp2 <- interp(x = mrms_sub$longitude, y = mrms_sub$latitude, z = mrms_sub$dBz, xo = 39.95045, yo = -86.3574, linear = FALSE, extrap = TRUE)

$`x`
[1] 39.95045
$y
[1] -86.3574
$z
    [,1]
[1,] -78789.37