【问题标题】:Extract time series of a point ( lon, lat) from netCDF in R从 R 中的 netCDF 提取点的时间序列(lon,lat)
【发布时间】:2014-01-04 10:48:48
【问题描述】:

我对 R 比较陌生。 我正在尝试从 netCDF 文件中获取温度数据的不同点(纬度、经度)的时间序列。 我的示例数据文件是 herehere 是小文件。我已经尝试过 netCDF 包和我目前使用的代码

library(ncdf)
obsdata = open.ncdf("obs.nc")

print.ncdf(obsdata) 

obsdatadates = obsdata$dim$time$vals
obsdatadates = as.Date(obsdatadates,origin = '1950-01-01') 
obsdatadates
obsoutput = get.var.ncdf(obsdata, varid = 'tasmin', start = c(1,1,1),
                         count = c(1,1,22280))
dim(obsoutput)
datafinal=merge(obsdatadates,obsoutput)

谁能帮我获取时间序列的数据框(第一列)和另一个数据的特定点(纬度,经度)的数据值。 在这种情况下,我正在寻找时间序列( 1950-01-01 到 2010-12-31 的数据)对于特定的纬度点(并重复许多兴趣点)和给定的变量(在这种情况下塔斯敏)。 您的帮助将不胜感激。 谢谢, 好像

【问题讨论】:

  • 您希望我们下载一个 14.71 MB 的数据文件!?!?听起来这是一个“为我做我的项目”的请求。
  • 请查看here,了解如何创建一个最小可重现的示例,例如使用dput
  • @DWin 现在第二个文件比较小,只有 2.4 MB

标签: r extract netcdf


【解决方案1】:

也许使用 raster 包,这不适用于所有 NetCDF 文件,但它适用于您的文件:

library(raster)
## brick reads all 22280 layers
r <- brick("obs.nc", varname = "tasmin")
## extract works for all time steps
vals <- extract(r, matrix(c(-120, 52.5), ncol = 2))

dim(vals)
## [1]     1 22280

请注意,它给出了一个 1 行、多列的矩阵,因为我只给了 extract() 一个点。

(提取很简单,直接从最近的单元格复制,使用 method = "bilinear" 进行插值)。有关其他选项,请参阅 ?extract

【讨论】:

  • 感谢您的建议,它很有帮助。谢谢:)
【解决方案2】:

以下是我将如何继续使用ncdf

library(ncdf)
obsdata = open.ncdf("obs1.nc")
obsdatadates = as.Date(obsdata$dim$time$vals,origin = '1950-01-01')
#Get the whole data first
obsoutput = get.var.ncdf(obsdata, varid = 'tasmin')
#Prepare your points of interest
points_of_interest = data.frame(lat=seq(1,8,by=2),lon=c(1,5,3,6))
#Subset your data accordingly
data_at_point = apply(points_of_interest,1,function(x)obsoutput[x[1],x[2],])
#Turn it into a dataframe
data_at_point = as.data.frame(data_at_point)
#Add the dates to the dataframe
data_at_point$Date = obsdatadates

【讨论】:

    【解决方案3】:

    “ncdf”包已弃用:http://cirrus.ucsd.edu/~pierce/ncdf/

    使用 ncdf4 包更新:

    library(ncdf4)
    obsdata <- nc_open("obs1.nc")
    print(obsdata) # check that dims are lon-lat-time
    
    # location of interest
    lon <- 6  # longitude of location
    lat <- 51 # latitude  of location
    
    # get dates
    obsdatadates <- as.Date(obsdata$dim$time$vals, origin = '1950-01-01')
    
    # get values at location lonlat
    obsoutput <- ncvar_get(obsdata, varid = 'tasmin',
                      start= c(which.min(abs(obsdata$dim$longitude$vals - lon)), # look for closest long
                               which.min(abs(obsdata$dim$latitude$vals - lat)),  # look for closest lat
                               1),
                      count = c(1,1,-1)) #count '-1' means 'all values along that dimension'that dimension'
    # create dataframe
    datafinal <- data.frame(dates= obsdatadates, obs = obsoutput)
    

    【讨论】:

      猜你喜欢
      • 2017-06-25
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2020-03-10
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多