1 year ago

#76369

test-img

Yogi

Read specific characters in .hdf files and then convert them to .tif files

I have two MODIS images over a decade which are distinguished by "h11" and "h12" in the filename. How can I specify to list files with "h11" in one variable and then with "12" in another variable, which will then be converted to .tif files. The current R-code converts both "h11" and "h12" files in the folder but "h11" are overwritten so I only have "h12" .tif files.

In the dir() function, can I specify more than one pattern?

I have over 3000 files in the folder, separating it manually would be a mess.

library(raster)
library(gdalUtils)
setwd("D:/MODIS")

shp_path<-"D:/MODIS"
boundary<-"boundary.shp"
outline<-shapefile(file.path(shp_path,boundary))

x<-crs(outline)

hdf_file <- dir(pattern = ".hdf")
h11_hdffile <- dir(pattern = ".hdf") #get h11 files?
filename <- substr(hdf_file,1,8)

filename <- paste0(filename,".tif")

i <- 1
n <- length(hdf_file)

for (i in 1:n){
  sds <- get_subdatasets(hdf_file[i])
  gdal_translate(sds[2],dst_dataset = filename[i])
}

Expected results:

MODIS2006.1_h11.tif, MODIS2006.1_h12.tif, ..., MODIS2016.1_h11.tiff, MODIS2016.1_h12.tiff

r

gdal

geotiff

hdf

0 Answers

Your Answer

Accepted video resources