[RS-ecology] Chapter 11

Vladimir Ruslan Wingate vladimir.wingate at unibas.ch
Mon Apr 10 18:51:21 CEST 2017


Hi all,
Here is an example of my code up until I get I get the following error:
> names(s) <- as.Date(modis.hdf[[1]], "A%Y%j")
Error in `names<-`(`*tmp*`, value = c(NA_real_, NA_real_)) :
  incorrect number of layer names

Many thanks for your help,
Vladimir


library(MODIS)
library(gdalUtils)
MODISoptions(gdalPath="C:/OSGeo4W64/bin")
setwd("C:/chapter 11")
getwd()

gdal_setInstallation(search_path = "C:/OSGeo4W64/bin", rescan = TRUE,ignore.full_scan = TRUE, verbose = FALSE)

MODIS:::checkTools('GDAL')
MODIS:::checkTools('MRT')

viname <- "ndvi"
product <- "MOD13Q1"
ofilename <- paste0(product, "_", viname, "_brick.grd")
ofilename

pth <- paste0(getwd(), "/raster_data/", product)
pth

fileout <- paste(pth, "/", ofilename, sep="")
fileout

if (!file.exists(fileout)) {
  if (!file.exists(pth)) {
    print("the outfolder does not exist and will be created")
    print(pth)
    dir.create(pth, recursive = TRUE)
  }
}

#dir.create() does not create nested folders by default. set "recursive = TRUE" to do so.
MODISoptions(localArcPath = pth, outDirPath =pth)
setwd(pth)
getwd()

tileH <- 19
tileV <- 10

begin <- "2015.01.01"
end <- "2015.02.01"

modis.hdf <- getHdf(product = product, begin=begin, end=end, tileH=tileH, tileV=tileV, checkIntegrity=TRUE)

print(modis.hdf)


for (i in 1:length(modis.hdf[[1]])) {

  ifl <- unlist(strsplit(unlist(strsplit(modis.hdf[[1]][i],

                                         c("[/]"))) [5], "[.]")) [1]
  ifl
  print(ifl)

  fn <- paste("cvi_", ifl, ".tif", sep="")
  print(fn)

  if (is.na(ifl) | file.exists(fn)) {

    print("file exists or is not available on the server")

  } else {

    sds <- get_subdatasets(modis.hdf[[1]][i])
  }
}



tmp <- rasterTmpFile()
extension(tmp) <- "tif"
library(gdalUtils)
gdal_translate(sds[1], dst_dataset=tmp, output_Raster=TRUE )

ndvi <- raster(tmp)/10000

writeRaster(ndvi, filename=paste0("ndvi_", ifl, ".tif", sep=""), dataType="INT2U", format="GTiff", overwrite=TRUE)

tmp2 <- rasterTmpFile()
extension(tmp2) <- "tif"
gdal_translate(sds[12], dst_dataset=tmp2, output_Raster=TRUE )

rel <- crop(x=raster(tmp2), y=raster(tmp2),
            filename=paste("rel_", ifl, ".tif", sep=""),
            dataType="INT2U", format="GTiff")
rel

f_cleanvi <- function(vi, rel) {

  i <- (rel <= 1)

  res <- matrix(NA, length(i), 1)

  if (sum(i, na.rm=TRUE) > 0) {

    i <- which(i)

    res[i] <- vi[i]
  }
  res
}

res

clvi <- overlay(ndvi, rel, fun=f_cleanvi, filename=fn, dataType="INT2U", overwrite=TRUE)
clvi

rm(ndvi, rel, clvi, tmp, tmp2)

ofl <- list.files(pattern=glob2rx("rel*.tif"))
class(ofl)
ofl
s <- do.call("brick", lapply(ofl, raster))
s


names(s) <- as.Date(modis.hdf[[1]], "A%Y%j")

s

Vladimir Wingate
PhD candidate/Doctorant/Doktorand
Physical Geography and Environmental Change
Department of Environmental Science
University of Basel
Klingelbergstrasse 27
Basel 4056

e-mail:  Vladimir.wingate at unibas.ch<mailto:Vladimir.wingate at unibas.ch>
Skype: vladwingate
Switzerland:  +41 (0) 78 613 6259
Australia:  +61 (0) 448 353 105
Namibia: +264 (0) 81 82 28 881

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.uni-wuerzburg.de/pipermail/rs-ecology/attachments/20170410/cfb44df2/attachment-0001.html>


More information about the RS-ecology mailing list