Search code examples
rrasterr-rasterterrar-modis

Fill pixel gaps in MYD13Q1 EVI with matching pixel from MOD13Q1 EVI raster in R


I'm working with monthly aggregates of MODIS EVI data from an area with a lot of cloud cover. Not many pixels are left after parsing for QA.

To improve the number of pixels, I want to combine pixels from MYD13Q1.061 Aqua Vegetation Indices 16-Day Global 250m with those from MOD13Q1.061 Terra Vegetation Indices 16-Day Global 250m. All images have been subjected to the same process in Google Earth Engine and importated in R. Sample files can me gotten here (files are not large)

MOD13Q1 EVI

#Set working directory
setwd("C:/...sample/mod")

##load EVI tiffs
pathtif_evi='C:/...sample/mod/'
tiflist_evi=list.files(path = pathtif_evi,'*.tif')

##read in EVI tifs
all_tiffs_evi <- lapply(tiflist_evi, raster)

MYD13Q1 EVI

#Set working directory
setwd("C:/...sample/myd")

##load EVI tiffs
pathtif_myd='C:/...sample/myd/'
tiflist_myd=list.files(path = pathtif_myd,'*.tif')

I have tried a solution from @Forrest R. Stevens using the code below it doesn't work on pixels. It seems suited for numeric values.

Create indices for pixels that are NA in B and not NA in A:

A <- all_tiffs_evi
B <- all_tiffs_myd
    
indices <- is.na(B)[] & !is.na(A)[]

B[indices] <- A[indices]

Please, I need help with a method for replacing NA pixels in one image with pixels from another (where a valid pixel exist). I have monthly data for 22 years. Thank you


Solution

  • You can use terra::cover like this

    library(terra)
    fmod <- list.files(path="mod", pattern='tif$', full=TRUE)
    mod <- rast(fmod)
    fmyd <- list.files(path="myd", pattern='tif$', full=TRUE)
    myd <- rast(fmyd)
    
    x <- cover(mod, myd)
    

    If there is no reason to prefer mod over myd then it could make more sense to take the mean values where they both have a value.

    m <- mean(mod, myd, na.rm=TRUE)