【问题标题】:Loopin through directories subfolder and save result to other directory in subfolders in R循环遍历目录子文件夹并将结果保存到 R 子文件夹中的另一个目录
【发布时间】:2018-07-20 22:57:58
【问题描述】:

在 R 中,我循环浏览一个文件夹并将 netCDF 文件加载到光栅堆栈中。然后我进行计算并将结果作为多波段 tiff 保存到不同目录中的另一个文件夹中。

问题是,我有几个文件夹,里面有几个 netCDF 文件,我想让代码更有效率。 1.) 遍历目录中的子文件夹并加载 cetCDF 文件 2.) 遍历输出目录中的子文件夹并将结果保存到光盘

什么是执行此操作的有效方法?也许是一个功能? 我想它并没有那么复杂,但是我对太多的 for 循环有点困惑......

我附上了我现在所做的工作示例。

files<- list.files(path= paste(wd,"/nc_in/tmax_rcp85/", sep=""), pattern = 'nc$', full.names=TRUE)

# loop through netCDF files and create a raster stack
for (i in 1:length(files)){
  ras<- stack(files[i])
  print(ras)

  outnam<- basename(files[[i]]) # generate output name
  outnam<- sub('\\.nc$', '', outnam)

  results<- stack() # create temp stack of results

  #loop through raster stack and downscale monthly mean averages
  for (r in 1:nlayers(ras)){

    # doing some stuff with each raster in the satck

    # update results temp raster stack
    results <- stack(results, downscale)

  }

  # save downscaled rster stack to disc
  writeRaster(results, file=paste0(getwd(),"/ds_results/tmax_rcp85_ds/",sub('\\.nc$', '', outnam),"_ds",".tif", sep = ""), options="INTERLEAVE=BAND", overwrite=TRUE)
  rm(results) # clear temp stack of results

}
removeTmpFiles(0.1)

【问题讨论】:

    标签: r loops subdirectory


    【解决方案1】:

    我在list.files() 中使用recursive=T 解决了这个问题 然后我将生成的 Tiff 保存到包含一些参考信息的同一目录中...

    files<- list.files(path= paste(wd,"/nc_in/", sep=""), pattern = 'nc$', full.names=TRUE, recursive=T)
    
    # loop through netCDF files and create a raster stack
    for (i in 1:length(files)){
      ras<- stack(files[i])
      print(ras)
    
      outnam<- basename(files[[i]]) # generate output name
      outnam<- sub('\\.nc$', '', outnam)
    
      outdir <- dirname(files[[i]])
      Split <- strsplit(outdir, "//")
      lDir<- Split[[1]][length(Split[[1]])]
    
      results<- stack() # create tem stack of results
    
      #loop through raster stack and downscale monthly mean averages
      for (r in 1:nlayers(ras)){
    
        # doing some stuff with each raster in the satck
    
    
        # update results raster stack
        results <- stack(results, downscale)
    
      }
    
      print(paste("Writing multi band GeoTiff: ", outnam, "to:"))
      print(paste0(dirname(files[[i]]),'/', lDir,"_", sub('\\.nc$', '', outnam),"_ds",".tif", sep = ""))
    
      # save downscaled rster stack to disc
      writeRaster(results, file=paste0(dirname(files[[i]]),'/', lDir,"_", sub('\\.nc$', '', outnam),"_ds",".tif", sep = ""), options="INTERLEAVE=BAND", overwrite=TRUE)
      rm(results) # clear temp stack of results
    
    }
    removeTmpFiles(0.1)
    

    【讨论】:

      【解决方案2】:

      这里有一些建议

      fin <- list.files(path= "/nc_in/tmax_rcp85/", pattern = 'nc$', full.names=TRUE)
      fout <- sub("/nc_in/", "/ds_results/", sub('\\.nc$', '.tif', fin)) 
      # create output directories if they do not exist
      x <- lapply(fout, function(i) dir.create(dirname(i), FALSE, TRUE))
      
      
      downscale <- function(x) {
          # add your code here
          return(x)
      }
      
      
      for (i in 1:length(fin)){
          s <- stack(fin[i])
          for (j in 1:nlayers(s)){
              s[[j]] <- downscale(s[[j]])
          }
          writeRaster(s, file=fout[i], options="INTERLEAVE=BAND", overwrite=TRUE)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-16
        • 1970-01-01
        • 2013-07-02
        • 2020-05-08
        • 1970-01-01
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多