我已经很接近使我的代码生效,但是似乎无法弄清楚如何获得动态文件名。这是Ivve得到的:
require(ncdf)
require(raster)
require(rgdal)
## For multiple files, use a for loop
## Input directory
dir.nc <- 'inputdirectoy'
files.nc <- list.files(dir.nc, full.names = T, recursive = T)
## Output directory
dir.output <- 'outputdirectory'
## For simplicity, I use "i" as the file name, but would like to have a dynamic one
for (i in 1:length(files.nc)) {
r.nc <- raster(files.nc[i], varname = "precipitation")
writeRaster(r.nc, paste(dir.output, i, '.tiff', sep = ''), format = 'GTiff', prj = T, overwrite = T)
}
## END
感谢您的帮助。太近了!
答案 0 :(得分:0)
您可以用不同的方式执行此操作,但是我认为通常最简单的方法是先创建所有输出文件名(并检查它们是否正确),然后在循环中使用它们。
是这样的:
Paint paint = new Paint ();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
为确保您正在写入现有文件夹,可以先创建它。
library(raster)
infiles <- list.files('inputpath', full.names=TRUE)
ff <- extension(basename(infiles), '.tif')
outpath <- 'outputpath'
outfiles <- file.path(outpath, ff)
然后遍历文件
dir.create(outpath, showWarnings=FALSE, recursive=TRUE)
您也可以沿这些方式使用
for (i in 1:length(infiles)) {
r <- raster(infiles[i])
writeRaster(r, paste(outfiles[i], overwrite = TRUE)
}
答案 1 :(得分:0)
这是最终起作用的代码:
# Imports
library(raster)
#Set source file
infiles <- list.files('infilepath', full.names=TRUE)
#create dynamic file names and choose outfiles to view list
ff <- extension(basename(infiles), '.tif')
outpath <- 'outfilepath'
outfiles <- file.path(outpath, ff)
#run da loop
for (i in 1:length(infiles)) {
r <- raster(infiles[i])
writeRaster(r, paste(outfiles[i]), format ='GTiff', overwrite = T)
}
## END