【问题标题】:Calculating percentile across netcdf files fails跨 netcdf 文件计算百分位数失败
【发布时间】:2015-01-12 16:56:26
【问题描述】:

我有五个 netcdf 文件,每个文件都包含一个时间段的数据。我想单独计算每个单元格的整个时间跨度的第 98 个百分位数。 netcdf 文件的累积文件大小约为 250 MB。

我的做法是这样的:

library(raster)

  fileType="\\.nc$"
  filenameList <- list.files(path=getwd(), pattern=fileType, full.names=F, recursive=FALSE)
  #rasterStack for all layers
  rasterStack <- stack()

  #stack all data
  for(i in 1:length(filenameList)){

    filename <- filenameList[i]
    stack.temp<-stack(filename)
    rasterStack<-stack(rasterStack, stack.temp)

  }

  #calculate raster containing the 98th percentiles
  result <- calc(rasterStack,  fun = function(x) {quantile(x,probs = .98,na.rm=TRUE)} )

但是,我收到此错误:

Error in ncdf4::nc_close(x@file@con) : 
  no slot of name "con" for this object of class ".RasterFile"

我的代码的堆叠部分有效,崩溃发生在 calc 函数期间。 你知道这可能来自哪里吗?这可能是数据存储位置(内存/磁盘)的问题吗?

【问题讨论】:

  • 您是否尝试使用 getValues / getValuesBlock 并在那里应用您的百分位函数?

标签: r gis r-raster


【解决方案1】:

奇怪,我生成了一些虚拟数据,它似乎工作得很好,这似乎不是你的方法。 250MB 不算太大。我会剪下每个光栅的一小块并测试它是否有效。

dat<-matrix(rnorm(16), 4, 4)
r1<-raster(dat)
r2<-r1*2
r3<-r2+1
r4<-r3+4
rStack <- stack(r1,r2,r3,r4)
result <- calc(rStack,  fun = function(x) {quantile(x,probs = .98)} )

【讨论】:

    【解决方案2】:

    也许这与您创建 RasterStack 的奇怪方式有关。你应该这样做:

      filenames <- list.files(pattern="\\.nc$")
      rasterStack <- stack(filenames)
    

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 2013-06-20
      • 2016-02-17
      • 2016-07-28
      • 2017-08-29
      • 2012-10-28
      • 2021-02-26
      • 1970-01-01
      • 2016-03-11
      相关资源
      最近更新 更多