Step 01: Resampling Rasters

##############################################################
######## Project Name : SATSURE Credit Risk Product
######## File Usage : This script resamples rasters based on maximum possible extent for all rasters across years
######## Date : November 20, 2018
######## Author : Actify Data Labs
##############################################################

#How to run the script
# Rscript resample_rasters.R <veg_index> <district>

# rm(list = ls())
source(“~/codes/credit_risk_product/utils.R”)
source(“~/codes/credit_risk_product/settings.R”)
import_libraries()

args <- commandArgs(TRUE)

# Parameters
veg_index <- toupper(args[1])
district <- tools::toTitleCase(args[2])
data_dir <- glue::glue(“~/{district}”)

# veg_index <- “NDWI”
# (list_of_raster_files_1 <- return_list_of_files(dir = data_dir,
# years_and_pattern = years_and_pattern,
# vegetation_index = veg_index))

# Doing the resampling for year 2017 only as this was given the latest
(list_of_raster_files <- lapply(years, function(year){
(path <- glue::glue(“{data_dir}/{year}/{veg_index}”))
list.files(path = path, pattern = “img|tif”, full.names = TRUE)

}) %>% unlist)

raster_list <- lapply(list_of_raster_files, raster)

print(raster_list)

# Get the best extent from all raster files for all years
best_extent <- compute_best_extent(raster_list)

print(best_extent)

resample_rasters(file_list = list_of_raster_files, extent = best_extent, reso = c(10,10))

# for (arg in arg_list){
#
# dir <- arg[1]
# file_pattern <- arg[2]
#
#
#
#
#
# print(paste0(“Resampling rasters for year “, substr(dir, 8 ,11), “…………”))
# resample_rasters(dir = dir,
# file_pattern = file_pattern
# )
#
#

# ndvi_brick <- make_brick_of_clipped_raster(dir = dir ,
# file_pattern = file_pattern
# )

# print(paste0(“Writing Brick Files as raster files for year “, substr(dir,start = 8, stop = 11), “…..”))
# filename <- paste0(dir, “brickFile.”,file_pattern )
# writeRaster(ndvi_brick,
# filename,
# overwrite = TRUE)

# }