【问题标题】:how to make a loop for reading several nc files as a raster and then write them as envi?如何创建一个循环以将多个 nc 文件作为光栅读取,然后将它们写为 envi?
【发布时间】:2015-05-26 04:12:45
【问题描述】:

下面给出的这段代码 (developed by stackoverflow users) 将从 netcdf 文件中读取一个变量并将其写入为一个 envi 文件。
这段代码工作正常,但我有几个像这样的 netcdf 文件,我想为目录中的所有文件创建一个循环。
文件存储在路径为

的目录中
d <-  flip(t(d), direction = "x") 
rf <- writeRaster(d, filename="last.envi", overwrite=TRUE)

我试过了:

for(i in 1:length(a)){
    d <- raster(a[i], varname = "soil_moisture_c")
    rf <- writeRaster(d[[i]], filename="%d.envi", overwrite=TRUE)
    }

但出现此错误

Error in function (classes, fdef, mtable) : 
unable to find an inherited method for function ‘writeRaster’ for signature ‘"numeric", "character"’

【问题讨论】:

  • 为什么是 d[[i]]?在 writeRaster 中将 d[[i]] 更改为 d
  • @agstudy 您可以将此添加为答案...

标签: r raster netcdf rgdal envi


【解决方案1】:

2 个错误:将 d[[i]] 更改为 d 并为每个输入使用一个新的输出文件。

 fileName <- strsplit(a[i],split='\\.')[[1]][1]
 outputFile <- paste(fileName,'_amenlast','.envi',sep='')
 rf <- writeRaster(d, filename=outputFile, overwrite=TRUE)

PS : 我保持 overwrite=TRUE ,这意味着如果您下次启动循环,它会擦除​​以前生成的文件。

【讨论】:

  • fileName &lt;- strsplit(gsub("C:\\\\AMSER\\\\","",a[i]),split='\\.')[[1]][1]?
  • @agstudy 确实如此,他不必更改他的工作目录,很好。
  • 你可以使用一个输出文件来处理 30 个不同的输入文件。你明白了吗?
猜你喜欢
  • 2015-03-05
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 2014-07-03
  • 2019-08-27
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多