1

There are more than 1,000 MODIS HDF images in a folder:

M:\join

Their names show us which files must be mosaiced together.

For example, in the below files, 2009090 means these three images must be mosaiced together:

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:

MOD05_L2.A2009091.0555.051.2010336162871
MOD05_L2.A2009091.0600.051.2010336842395

I am going to mosaic them using this function (source of function ):

mosaicHDF(hdfNames, filename, MRTpath, bands_subset, delete=FALSE)

How should I introduce my HDF files to hdfNames?

And what should I write in filename?

I tried to find a manual for this function, but there was not anything.

Thanks for your help.

Canada2015
  • 65
  • 1
  • 9
  • Do all files have a name with the pattern `MOD05_L2.GroupID.NotImport`? Or, are the group identifiders always between the first two `.` of the file name? Or even, are all the file names the same length, with the same length for `XXXX.ID`? With this info it's possible to use string functions to identify and group the names. – Molx Jun 03 '15 at 19:09
  • Thanks Molx, Yes, they all have the same pattern as MOD05_L2.GroupID.NotImport . It means we could recognize the HDF files which are belong to the same date. – Canada2015 Jun 03 '15 at 19:25
  • Do you know how to load and manipulate HDF files in R? I can show you how to group the file names, but I've never used HDF before. – Molx Jun 03 '15 at 20:01
  • In the below R script, there is a function for mosaic HDF files, but since I am new in R, I was not able to use it. This is the function: `mosaicHDF (hdfNames, filename, MRTpath, bands_subset, delete=FALSE)` . But there is not info about how to use that. If you do me a favour, and learn me how to group the file name, I would appropriate it. [R- Script](http://r-gis.net/ModisDownload/ModisDownload.R) – Canada2015 Jun 03 '15 at 20:13
  • Have you tried following [this guide](http://spatial-analyst.net/wiki/index.php?title=Download_and_resampling_of_MODIS_images#Download_of_MODIS_HDF_tiles)? It seems to be very detailed with step by step instructions. – Molx Jun 04 '15 at 00:56
  • I won't post as an answer because it would be incomplete, but check this for a simple example on how you can group file names based on your criteria, starting with all of them in a vector. http://www.r-fiddle.org/#/fiddle?id=WOF77Jo0 – Molx Jun 04 '15 at 01:07
  • Thank you @Molx. I did your method, but the problem is that we have thousands of HDF files and could not copy and paste their name in the script. Undoubtedly, there is a easier method to do so, but I do not know, yet. Thanks again for your comment. – Canada2015 Jun 04 '15 at 19:03
  • To get the name of the files, use `fnames <- list.files(pattern="MOD05")`. You can switch the pattern for whatever works better, like the extension, for example. – Molx Jun 04 '15 at 22:14
  • Using fnames <- list.files(pattern="MOD05") puts all HDF files into one mosaic file !! – Canada2015 Jun 04 '15 at 22:49

2 Answers2

1

This question is quite old but I figured I'd post up the R code Canada2015 asked for. So for a loop over years 2000 to 2016, assuming the file names still have the A < YEAR > < MONTH >< DAY >. format. This code mosaics all tiles together to produce a new file for each year present. If you had to mosaic many tiles from a single year, you could just change the pattern= argument to something general like '.hdf'

for(i in 2000:2016){
  HDFs <- list.files(path = "F:/PATHTOFILES/HDFs/", pattern = paste("A",i,sep = ""))
  mosaicHDF(hdfNames = HDFs, filename = paste('newhdf',i,'.hdf',sep = ""), MRTpath = 'C:/MRT/bin',bands_subset="1 0 0 0", delete=FALSE)
}
HaplessEcologist
  • 375
  • 3
  • 11
0

I did find the answer, fortunately. Thanks for your help.

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) 

But I have a new problem :-)

Since there are thousands of HDF files in the folder,

How could I write a loop to introduce all HDF files to the function?

FYI: I am quite new to R.

Canada2015
  • 65
  • 1
  • 9
  • If you have another question, please ask it by clicking the [Ask Question](//stackoverflow.com/questions/ask) button. – NathanOliver Jun 04 '15 at 21:27