[英]How to write a loop in R for HDF files?
I have more than 1,000 MODIS HDF images in a folder: 我的文件夹中有1000多个MODIS HDF图像:
M:\join
Their name shows us which HDF files are belong to a certain date and must be mosaiced together. 它们的名称告诉我们哪些HDF文件属于某个日期,并且必须将它们镶嵌在一起。
For example, in the below, 2009090 means these three images are belong to the same date and must be mosaiced together: 例如,在下面, 2009090表示这三个图像属于同一日期,并且必须镶嵌在一起:
MOD05_L2.A2009090.0420.051.2010336084010
MOD05_L2.A2009090.0555.051.2010336100338
MOD05_L2.A2009090.0600.051.2010336100514
Or these two, are for the same date, 2009091 : 或这两个日期是同一日期2009091 :
MOD05_L2.A2009091.0555.051.2010336162871
MOD05_L2.A2009091.0600.051.2010336842395
Right now, I am able to successfully use mosaicHDF () to mosaic them for one date as below: 现在,我能够成功使用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)
Since, there are more than 1,000 HDF files for 1 year in the folder, how should I write a loop to use such function for all HDF files, and make a mosaic file for each date? 既然文件夹中有1年超过1,000个HDF文件,那么我应该如何编写一个循环以对所有HDF文件使用这种功能,并为每个日期制作一个镶嵌文件?
I would be very grateful if anyone could help me. 如果有人可以帮助我,我将不胜感激。
Thanks. 谢谢。
Group the files by their ID using grep
, and then use a for loop to create the mosaic for each group: 使用
grep
将文件按ID分组,然后使用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)
}
This should save all the mosaic files for each group, with the file name as the group ID. 这将保存每个组的所有镶嵌文件,并将文件名作为组ID。
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.