【问题标题】:Extraction of data from multiple netcdf files at five coordinates files and writing them to five separate csv files从五个坐标文件的多个 netcdf 文件中提取数据并将它们写入五个单独的 csv 文件
【发布时间】:2020-10-14 09:12:56
【问题描述】:

我有 365 个 .nc 文件位于包含每日土壤水分信息的文件夹中。我想提取全年五个坐标位置的数据,并将它们写入五个单独的 csv 文件。我的代码附在下面。但是,我在该行之后收到此错误:

s <- stack(ff)

>Error in if (is.na(get("has_proj_def.dat", envir = .RGDAL_CACHE))) { : argument is of length zero In addition: Warning message: In .varName(nc, varname, warn = warn) : varname used is: sm If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor 

不知道如何继续。

library(raster)
library(ncdf4)
ptf <- "D://SMOS_ECV_SM//SMOS_ECV_SM//ECV_SM_Data_1978_2010//1978"
ff <- list.files(path=ptf, pattern="[.]nc$", full.names=TRUE)

s <- stack(ff)
points <- rbind(c(0,1), c(100,120), c(80,5), c(85,4), c(82,4))
v <- extract(s, points)

for (i in 1:ncol(v)) {
  write.csv(v[,i,drop=FALSE], paste0("file", i, ".csv"))
}

【问题讨论】:

  • 请更改您的问题以显示实际错误发生的位置。我想它在s &lt;- stack(ff) 线上——但是你需要解压它。尝试单个文件。如果失败,只需显示并让文件可用。另请报告R和rgdal的版本(并首先升级到最新版本)。

标签: r csv netcdf r-raster


【解决方案1】:
library(raster)
#Loading required package: sp
f <- list.files("try", full=T)

首先尝试单个文件

r <- raster(f[1])
#Loading required namespace: ncdf4
#Warning message:
#In .varName(nc, varname, warn = warn) : varname used is: sm
#If that is not correct, you can set it to one of: sm, sm_noise, flag, sensor

摆脱警告:

r <- raster(f[1], varname="sm")

现在所有文件

s <- stack(f, varname="sm")
s
#class      : RasterStack 
#dimensions : 720, 1440, 1036800, 2  (nrow, ncol, ncell, nlayers)
#resolution : 0.25, 0.25  (x, y)
#extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +no_defs 
#names      : Soil.Moisture.1, Soil.Moisture.2 

提取值

points <- rbind(c(-96.7, 47), c(34.55, 54.85))
v <- extract(s, points)

v
#     Soil.Moisture.1 Soil.Moisture.2
#[1,]          0.3254          0.3018
#[2,]          0.3386          0.3386
 

【讨论】:

  • 堆栈操作期间出现了一些其他错误。但是,重新安装光栅包后问题得到了解决。
猜你喜欢
  • 2015-05-23
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
相关资源
最近更新 更多