loicdtx / bfastSpatial

Set of utilities and wrappers to perform change detection on satellite image time-series (Landsat and MODIS). Includes pre-processing steps and functions for spatial implementation of bfastmonitor change detection and post processing of the results.
93 stars 39 forks source link

Problem with cleanMODIS(), Cannot create a RasterLayer object from this file. (file does not exist) #57

Open TimWeatherman opened 8 years ago

TimWeatherman commented 8 years ago

In cleanMODIS.r line 56, the call to raster() is on the subset string rather than the filename that is returned from readGDAL(), like on line 55. This causes an error, file does not exist.

Error in .local(.Object, ...) : 
  `/home/tim/geoscripting/Project/data/HDF4_EOS:EOS_GRID:MCD15A2H.A2002193.h19v03.006.2015149105839.hdf:MOD_Grid_MOD15A2H:FparLai_QC' does not exist in the file system,
and is not recognised as a supported dataset name.

Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  : 
  Cannot create a RasterLayer object from this file. (file does not exist)

A reproducible case is:

library(bfastSpatial)
download.file("http://e4ftl01.cr.usgs.gov//MODV6_Cmp_A/MOTA/MCD15A2H.006/2002.07.04/MCD15A2H.A2002185.h19v03.006.2015149105834.hdf", method = "wget", destfile = "MCD15A2H.A2002193.h19v03.006.2015149105839.hdf")

QC <- raster(get_subdatasets("MCD15A2H.A2002193.h19v03.006.2015149105839.hdf")[3])
QC <- raster(readGDAL(get_subdatasets("MCD15A2H.A2002193.h19v03.006.2015149105839.hdf")[3]))

The first line fails, the second line works correctly.

loicdtx commented 8 years ago

It works if you use full path.

GreatEmerald commented 8 years ago

Yes, looks like it. It also works if the file is in a subdirectory, like this:

QC <- raster(get_subdatasets("data/MCD15A2H.A2002193.h19v03.006.2015149105839.hdf")[3])

In order to make it work correctly with files in the working directory, this seems to be the solution:

QCG <- raster(get_subdatasets(normalizePath("MCD15A2H.A2002193.h19v03.006.2015149105839.hdf"))[3])

So perhaps https://github.com/dutri001/bfastSpatial/blob/master/R/cleanMODIS.R#L54 should be changed to:

sds <- get_subdatasets(normalizePath(x))