分批递增循环 [英] Increment Loop in Batches

查看:50
本文介绍了分批递增循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我处理我正在使用的循环功能.我已经尝试过广泛搜索Google和Stack Overflow,但是由于我不知道确切的搜索术语,我担心会丢失一些结果.考虑到这一点,如果您已经提出这个问题,我谨此表示歉意,但我希望有人可以向我指出正确的方向,以寻求解决方案.

I was hoping someone could help me with a loop function I'm working with. I have tried searching Google and Stack Overflow extensively but, as I don't know the exact search terminology, I fear I am missing some results. With that in mind, I apologise in advance if this question has already been asked but I hope that someone can point me in the right direction for a solution.

关于我的数据

我已经从NASA MODIS卫星下载了1000多个文件.由于我的学习区域很大,因此我不得不在6个图块的区域中下载数据.结果,我下载的文件列表实际上以6束为一组(尽管在资源管理器中只是文件列表而已).

I have downloaded 1000s of files from NASAs MODIS satellite. As my study area covers a large area, I've had to download the data over an area of 6 tiles. As a result, my list of downloaded files are actually 'grouped' together in bunches of 6 (despite appearing as just a list of files within explorer).

我已经在R中编写了一些简单的for循环来进行一些初始处理(在下面的示例中,这是对瓦片进行重新采样,以便可以使用raster :: mosaic将它们缝合在一起.这种处理可以分别在文件上进行在这个阶段,因此我发现以下循环非常适合我的需求:

I have written some simple for loops within R to do some initial processing (in the example below this is to resample the tiles so that they can then be stitched together using raster::mosaic. This processing can happen on the files individually at this stage so I have found the below loop to be perfect for my needs:

resampled.raster<-list()

for (l in 1:24){
  cat(l,"\n")
  resampled.raster[[l]]<-resample(rst[[l]], s, method="ngb")
}

问题

处理的下一阶段要求我将栅格分成6组,然后再移至下一批6.例如,如果我有24个文件的列表,则前6个文件需要按以下方式处理:一组文件,然后保存(在列表中),然后移至下一个6.经过4次迭代(总共达到24个文件),我需要停止循环.

The next stage of my processing requires me to mosaic the rasters in groups of 6, before moving onto the next batch of 6. For example, if I have a list of 24 files, the first 6 will need to be processed as a collection of files and then saved (in a list) before moving to the next 6. After 4 iterations of this (to make the 24 files total), I need the loop to stop.

我在互联网上找到的最接近的解决方案是此处,但这不能解决我的问题.

The closest solution I have found from searching the internet is here but this isn't a solution to my problem.

在R中使用for循环是否可以进行这种批处理"处理?预先非常感谢您的帮助.

Is this 'batch' processing possible with using a for loop within R? Many thanks in advance for your help.

推荐答案

首先请注意,使用 resample mosaic 几乎肯定是错误的.如果这些是图块,则应该可以使用 merge 进行合并.

First note that it is almost certainly wrong to use resample and mosaic. If these are tiles, you should be able to use merge to merge them.

关于需要分组的文件名,肯定有一些独特之处吗?那就是你需要使用的东西.

There surely is something unique about the names of the files that need to be grouped? That is what you need to use.

获取带有文件名的向量

# ff <- list.files()

这是一个玩具示例

ff <- c('fileA1', 'fileA2', 'fileB1', 'fileB2') 

获取唯一组

code <- gsub('file', '', ff)
code <- substr(code, 1, 1)
uc <- unique(code)

环顾群组

for (u in uc) {
    files <- ff[u == code]
    r <- lapply(files, raster)
    r$filename <- paste0("merged/", u, ".tif")
    m <- do.call(merge, r)
}

这篇关于分批递增循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆