Skip to content

Commit

Permalink
Added appropriate raster comparisons for trend tif's to test.R
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix-Kummer committed May 30, 2024
1 parent 4ff325a commit 665e862
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions bin/test.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ dinp <- args[1]
# load package
require(terra)

# function to compare raster values with tolerance
compare_rasters <- function(r1, r2, tolerance = 1e-5) {

diff <- abs(values(r1) - values(r2))
max_diff <- max(diff, na.rm = TRUE)

if (max_diff > tolerance) {
return(paste("Max difference", max_diff, "exceeds tolerance", tolerance))
}

return(TRUE)
}


# LOAD REFERENCE
Expand Down Expand Up @@ -85,7 +97,7 @@ peak_year_of_change <- peak_rast["YEAR-OF-CHANGE"]
#######################################################################
failure <- FALSE

woody_cover_changes_result <- all.equal(woody_cover_changes, woody_cover_changes_ref)
woody_cover_changes_result <- compare_rasters(woody_cover_changes, woody_cover_changes_ref)
if (is.character(woody_cover_changes_result)) {
print(paste0("Error: ", woody_cover_changes_result, " for woody cover changes."))
failure <- TRUE
Expand All @@ -102,7 +114,7 @@ if (is.character(woody_cover_year_of_change_result)) {
}


herbaceous_cover_changes_result <- all.equal(herbaceous_cover_changes, herbaceous_cover_changes_ref)
herbaceous_cover_changes_result <- compare_rasters(herbaceous_cover_changes, herbaceous_cover_changes_ref)
if (is.character(herbaceous_cover_changes_result)) {
print(paste0("Error: ",herbaceous_cover_changes_result, " for herbaceous cover changes."))
failure <- TRUE
Expand All @@ -119,7 +131,7 @@ if (is.character(herbaceous_cover_year_of_change_result)) {
}


peak_changes_result <- all.equal(peak_changes, peak_changes_ref)
peak_changes_result <- compare_rasters(peak_changes, peak_changes_ref)
if (is.character(peak_changes_result)) {
print(paste0("Error: ", peak_changes_result, " for peak changes."))
failure <- TRUE
Expand Down

0 comments on commit 665e862

Please sign in to comment.