Skip to content

Commit

Permalink
new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
khufkens committed Feb 7, 2024
1 parent 28d5138 commit dc586fb
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(align_events)
export(merge_normalized_differences)
export(normalized_difference)
export(read_appeears)
export(read_ml_data)
57 changes: 57 additions & 0 deletions R/merge_normalized_differences.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

#' Merge / calculate difference ratios
#'
#' Calculate normalized difference ratios
#' for all possible "sur_refl" band combinations
#'
#' @param ml_df a machine learning data frame with "sur_refl" prefix column
#' names apply the band ratios to
#'
#' @return A machine learning data frame with normalized band ratios appended
#' @export

merge_normalized_differences <- function(ml_df){

# split out surface reflectance values
sur_refl <- ml_df |>
dplyr::select(
starts_with("sur_refl")
)

# calculate all normalized difference ratios
ndr <- lapply(names(sur_refl), function(band){

# split out reference band
band_1 <- sur_refl |>
dplyr::select(
all_of(band)
)

# mutate over all other collumns
df <- sur_refl |>
dplyr::select(
!all_of(band)
)

# wrangle all column names
cols <- gsub("sur_refl_", "", names(df))
ref_col <- gsub("sur_refl_", "", names(band_1))

df <- df |>
dplyr::transmute_all(
function(x){as.vector(normalized_difference(band_1, x))}
)

# add clean column names
colnames(df) <- paste(ref_col, cols, sep = "_")
return(df)
})

# bind columns in one big tibble
ndr <- dplyr::bind_cols(ndr)

# merge with original data
ml_df <- bind_cols(ml_df, ndr)
}


16 changes: 16 additions & 0 deletions R/normalized_difference.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#' Normalized difference
#'
#' Calculates normalized difference for two (arbitrary) bands. The difference
#' is taken as band 1 - band 2
#'
#' @param band_1 first band
#' @param band_2 second band
#'
#' @return normalized difference index for two bands
#' @export

normalized_difference <- function(band_1, band_2){
ndr <- (band_1 - band_2)/(band_1 + band_2)
as.matrix(ndr)
}
19 changes: 19 additions & 0 deletions man/merge_normalized_differences.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/normalized_difference.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc586fb

Please sign in to comment.