0

我在一个文件夹中有 1,000 多个 MODIS HDF 图像:

M:\join

他们的名字向我们展示了哪些 HDF 文件属于某个日期并且必须镶嵌在一起。

例如,在下面,2009090表示这三张图片属于同一日期,必须拼接在一起:

MOD05_L2.A2009090.0420.051.2010336084010
MOD05_L2.A2009090.0555.051.2010336100338
MOD05_L2.A2009090.0600.051.2010336100514

或者这两个,是同一日期,2009091

MOD05_L2.A2009091.0555.051.2010336162871
MOD05_L2.A2009091.0600.051.2010336842395

现在,我可以成功地使用mosaicHDF () 将它们拼接到一个日期,如下所示:

hdfs <- c('MOD05_L2.A2009090.0420.051.2010336084010.hdf',
          'MOD05_L2.A2009090.0555.051.2010336100338.hdf',
          'MOD05_L2.A2009090.0600.051.2010336100514.hdf')

mosaicHDF(hdfNames=hdfs, filename='newhdf.hdf', MRTpath='C:/MRT/bin',bands_subset="1 0 0 0", delete=FALSE) 

由于文件夹中有 1,000 多个 HDF 文件 1 年,我应该如何编写一个循环以对所有 HDF 文件使用此功能,并为每个日期制作一个马赛克文件?

如果有人能帮助我,我将不胜感激。

谢谢。

4

2 回答 2

2

使用 将文件按 ID 分组grep,然后使用 for 循环为每个组创建马赛克:

fnames <- c("MOD05_L2.A2009090.0420.051.2010336084010", "MOD05_L2.A2009090.0555.051.2010336100338", "MOD05_L2.A2009090.0600.051.2010336100514", "MOD05_L2.A2009091.0555.051.2010336162871", "MOD05_L2.A2009091.0600.051.2010336842395")
#or
fnames <- list.files(path = "M:/join/", pattern = "*.hdf") #Credits do @Gregor for noticing this

ids <- unique(substr(fnames, 10, 17))

groups <- sapply(ids, grep, fnames, value=TRUE)

for (gr in seq_along(groups)) {
  mosaicHDF(hdfNames=groups[[gr]], filename=paste0(names(groups)[gr], '.hdf'), 
            MRTpath='C:/MRT/bin',bands_subset="1 0 0 0", delete=FALSE)
}

这应该保存每个组的所有马赛克文件,文件名作为组 ID。

于 2015-06-04T23:29:11.943 回答
0
    fnames <- c("MOD05_L2.A2009090.0420.051.2010336084010", "MOD05_L2.A2009090.0555.051.2010336100338", "MOD05_L2.A2009090.0600.051.2010336100514", "MOD05_L2.A2009091.0555.051.2010336162871", "MOD05_L2.A2009091.0600.051.2010336842395")
    #or
    fnames <- list.files(path = "M:/join/", pattern = "*.hdf") #Credits do @Gregor for noticing this

    ids <- unique(substr(fnames, 10, 17))

    groups <- lapply(ids, grep, fnames, value=TRUE)  # This line should be lapply instead of sapply
    ref.num = substr(groups[[j]][1], 10, 17) #Give output different names


for (gr in seq_along(groups)) {
ref.num = substr(groups[[gr]][1], 10, 17) 
      mosaicHDF(hdfNames=groups[[gr]], filename=paste0("MOD05_",ref.num, ".hdf"), 
                MRTpath='C:/MRT/bin',bands_subset="1 0 0 0", delete=FALSE)
    }
于 2017-03-08T20:47:43.623 回答