1

I am trying to perform stack() in R using enhanced vegetation index (EVI) time-series maps but it doesn't include all the .tiff files in my directory. Here's the list of files when I dir(files.evi):

[1] "MOD13Q1_EVI_2019_001.tif" "MOD13Q1_EVI_2019_017.tif"

[3] "MOD13Q1_EVI_2019_033.tif" "MOD13Q1_EVI_2019_049.tif"

[5] "MOD13Q1_EVI_2019_065.tif" "MOD13Q1_EVI_2019_081.tif"

[7] "MOD13Q1_EVI_2019_097.tif" "MOD13Q1_EVI_2019_113.tif"

[9] "MOD13Q1_EVI_2019_129.tif" "MOD13Q1_EVI_2019_145.tif"

[11] "MOD13Q1_EVI_2019_161.tif" "MYD13Q1_EVI_2019_009.tif"

[13] "MYD13Q1_EVI_2019_025.tif" "MYD13Q1_EVI_2019_041.tif"

[15] "MYD13Q1_EVI_2019_057.tif" "MYD13Q1_EVI_2019_073.tif"

[17] "MYD13Q1_EVI_2019_089.tif" "MYD13Q1_EVI_2019_105.tif"

[19] "MYD13Q1_EVI_2019_121.tif" "MYD13Q1_EVI_2019_137.tif"

[21] "MYD13Q1_EVI_2019_153.tif" "MYD13Q1_EVI_2019_169.tif"

However, when I do

evi <- list.files(files.evi, pattern = glob2rx("*MYD13Q1*.tif$"), full.names = TRUE)
all_evi_st <- stack(evi)

it only stack half of the files on my list:

class : RasterStack

dimensions : 668, 1105, 738140, 11 (nrow, ncol, ncell, nlayers)

resolution : 231.6564, 231.6564 (x, y)

extent : 11251086, 11507066, 1348703, 1503450 (xmin, xmax, ymin, ymax)

crs : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs

names : MYD13Q1_EVI_2019_009, MYD13Q1_EVI_2019_025, MYD13Q1_EVI_2019_041, MYD13Q1_EVI_2019_057, MYD13Q1_EVI_2019_073, MYD13Q1_EVI_2019_089, MYD13Q1_EVI_2019_105, MYD13Q1_EVI_2019_121, MYD13Q1_EVI_2019_137, MYD13Q1_EVI_2019_153, MYD13Q1_EVI_2019_169

Can anyone tell me what's happening and what's the workaround? I am new in remote sensing and still shabby with R.

Other info I can give about the time-series images is that they are from MODIS TERRA and AQUA time series derived from the 250 m 16-day composite vegetation index products (MOD13Q1/MYD13Q1).

  • Looks like you're loading a different set of files based on the names of the raster layers. What happens when you print(files.evi) and print(evi)? – JepsonNomad Jul 16 at 23:08
  • @JepsonNomad, when I do print(files.evi), I get: "~/MODIS/VI_16Days_250m_v6/EVI" and with print(evi), it lists the 11 files in the stack(evi) above. – mand3rd Jul 17 at 0:19
  • So why not just use stack(dir(files.evi))? The file list on evi is clearly not what you want since it has not only half the layers of dir(files.evi) but also has different layers. – JepsonNomad Jul 17 at 0:24
  • @JepsonNomad that solved it! thanks so much! – mand3rd Jul 17 at 4:27
  • @JepsonNomad It would be good if you could expand your comment just a little and write it as an answer. – PolyGeo Jul 17 at 6:32
0

It isn't clear how you generated files.evi, but in this case I feel like an easy solution to the problem is using what you know works: If dir(files.evi) returns the list of files you want to work with, then use that object to create your raster stack with:

d = dir(files.evi)
myraster = raster::stack(d)

To expand on the actual problem you have, I'm trying to replicate it on my computer, and I think it somehow relates to file structure. Here, I navigate to a working directory on my computer and then make a path to a subdirectory analagous to what returns when you print(files.evi) in the comments. I have a series of .tif files in the subdirectory, with no similarly-names files in the base directory. Trying to use stack(files.evi) may because the full names are not generated with this instance of dir(), unless you set full.names = TRUE. Both your use of list.files() and sn1ks solution work, but the "solution" that works above for you in the comments (in the absence of dir(x, full.names = TRUE) actually only works when I set the working directory to the same subdirectory that contains my images of interest.

setwd('PATH/TO/DIRECTORY/')

list.files()
files.evi = "PATH/TO/DIRECTORY/SUBDIRECTORY"
dir(files.evi)

evi <- list.files(files.evi, pattern = glob2rx("*STARTNAME*.tif$"), full.names = TRUE)
evi

evi.b <- list.files(files.evi, pattern = glob2rx("*MIDNAME*.tif$"), full.names = TRUE)
evi.b

myrastera = raster::stack(evi)
myrasterb = raster::stack(evi.b)
myrasterc = raster::stack(dir(files.evi, full.names = TRUE))
myrasterd = raster::stack(dir(files.evi)) 

If line 1 = setwd("PATH/TO/DIRECTORY"),myrasterd = ras... returns an error because files don't exist in current working directory BUT if line 1 = setwd("PATH/TO/DIRECTORY/SUBDIRECTORY"), myrasterd = ras... works fine.

1

In your code example

 evi <- list.files(files.evi, pattern = glob2rx("*MYD13Q1*.tif$"), full.names = TRUE)

in the part after "pattern = ..." you specifically select only the second part of your files, starting with "MYD". the not selected files start with "MOD13Q1", so they are not selected by your code :) You need to change it to:

 evi <- list.files(files.evi, pattern = glob2rx("*13Q1*.tif$"), full.names = TRUE)

then it should work as desired :)

  • 1
    you're right, I didn't notice that I have MYD and MOD in my .tiff files. – mand3rd Jul 17 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.