hello i have 4 folders containing modis images each folder have time series images named the same way e.g:
Folder 1 :
MCD19A2_Optical_Depth_047_2019_032.tif
MCD19A2_Optical_Depth_047_2019_033.tif
MCD19A2_Optical_Depth_047_2019_034.tif
MCD19A2_Optical_Depth_047_2019_035.tif
MCD19A2_Optical_Depth_047_2019_036.tif
MCD19A2_Optical_Depth_047_2019_037.tif
...
Folder 2 :
MCD19A2_Optical_Depth_047_2019_032.tif
MCD19A2_Optical_Depth_047_2019_033.tif
MCD19A2_Optical_Depth_047_2019_034.tif
MCD19A2_Optical_Depth_047_2019_035.tif
MCD19A2_Optical_Depth_047_2019_036.tif
MCD19A2_Optical_Depth_047_2019_037.tif
...
Folder 3 :
MCD19A2_Optical_Depth_047_2019_032.tif
MCD19A2_Optical_Depth_047_2019_033.tif
MCD19A2_Optical_Depth_047_2019_034.tif
MCD19A2_Optical_Depth_047_2019_035.tif
MCD19A2_Optical_Depth_047_2019_036.tif
MCD19A2_Optical_Depth_047_2019_037.tif
...
Folder 4 :
MCD19A2_Optical_Depth_047_2019_032.tif
MCD19A2_Optical_Depth_047_2019_033.tif
MCD19A2_Optical_Depth_047_2019_034.tif
MCD19A2_Optical_Depth_047_2019_035.tif
MCD19A2_Optical_Depth_047_2019_036.tif
MCD19A2_Optical_Depth_047_2019_037.tif
...
i would like to know how to mosaic the 4 modis images MCD19A2_Optical_Depth_047_2019_032.tif together and the 4 MCD19A2_Optical_Depth_047_2019_033.tif together in R ...
with the normal gregorian calendar dates in the names not as above in the julian day
It is better if you post some code you have tried so one could make a better suggestion. I think you could try something like:
dirNames <- c(paste('Folder', 1:4))
filesNames <- c("MCD19A2_Optical_Depth_047_2019_032.tif",
"MCD19A2_Optical_Depth_047_2019_033.tif",
"MCD19A2_Optical_Depth_047_2019_034.tif",
"MCD19A2_Optical_Depth_047_2019_035.tif",
"MCD19A2_Optical_Depth_047_2019_036.tif",
"MCD19A2_Optical_Depth_047_2019_037.tif",
"MCD19A2_Optical_Depth_047_2019_038.tif")
filesRast <- stringr::str_c(rep(dirNames, length(filesNames)),
'/', filesNames)
filesFolders <-lapply(dirNames,
function(x) {
y <- filesRast[stringr::str_detect(pattern = x, string = filesRast)]
y <- sort(y)
})
for(i in 1:length(dirNames)){
name <- lapply(filesFolders, function(x) x[i])
dateOrig <- gsub('_', '',gsub(paste0('.*MCD19A2_Optical_Depth_047_|.tif'), '', name[1]))
dateGreg <- as.Date(dateOrig, "%y%j")
r <- raster::raster(name[1])
for(j in 2:length(name)){
r <- raster::mosaic(r, raster::raster(name[i]))
}
raster::writeRaster(r, paste0('out/folder/', 'MCD19A2_mosaic_', dateGreg, '.tif'))
}
Note that filesNames
is a character vector I copied from your example. Buy you should use something like list.files('/folder/where/tif/are', pattern='.tif$')
, to extract all the names in your folders.