0

I have around 184 .img image files per year. Their name is like these:

mod11a1_2009-03-30.lst_day_1km.img
mod11a1_2009-03-31.lst_day_1km.img
mod11a1_2009-04-01.lst_day_1km.img

. . .

I am going to put all of those .img files, into 1 stack image with the same format:

mod11a1_2009_lst_day_1km.img

And generate the final stack image as an .img file in my working directory.

I wonder if someone could help me to do so, using R programming language. As I am new in R, I would be very grateful if you could help me in detail.

FYI: I am using R-Studio. My working directory is 'M:/2009_stack'

Canada2015
  • 65
  • 1
  • 9

1 Answers1

3

You first have to install and load the raster package:

install.packages("raster", dependencies=TRUE)
library(raster)

Then change your working directory to the file location:

setwd("M:/2009_stack")

Create a raster stack out of your files:

myStack <- stack(list.files(pattern="\\.img$"))

If you want to export an .IMG file into your working directory:

writeRaster(myStack,"mod11a1_2009_lst_day_1km.img", format="HFA")

For more information on the used raster functions have a look here or here.

maRtin
  • 6,336
  • 11
  • 43
  • 66
  • 1
    You may want to edit your `list.files()` call to include a wildcard (e.g. `list.files(pattern="\\.img$")`) to weed out any non-image files (such as pyramid, statistics or metadata files) that are often present. Otherwise exactly the solution I'd have proposed. – Forrest R. Stevens May 12 '15 at 22:34
  • Thank you so much maRtin. Great help. Now, I can save my time, incredibly. Thanks to others for their comments, as well. – Canada2015 May 12 '15 at 23:22
  • Hi maRtin, Could you please have a look to this question, too. Thanks. [How to mosaic the same HDF files using R?](http://stackoverflow.com/questions/30628501/how-to-mosaic-the-same-hdf-files-using-r) – Canada2015 Jun 04 '15 at 18:40
  • @maRtin . Hi maRtin, Could you please have a look to this question, too. Thanks. [How to mosaic the same HDF files using R?](http://stackoverflow.com/questions/30628501/how-to-mosaic-the-same-hdf-files-using-r) – Canada2015 Jun 04 '15 at 18:48