Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bfastlite as force-udf in R #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions rstats/bfastlite.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#load libraries
lib <- "../../rlib"

.libPaths(c(.libPaths(), lib))

sfLibrary(zoo, lib.loc = lib)
sfLibrary(sandwich, lib.loc = lib)
sfLibrary(strucchangeRcpp, lib.loc = lib)
sfLibrary(bfast, lib.loc = lib)


# dates: vector with dates of input data (class: Date)
# sensors: vector with sensors of input data (class: character)
# bandnames: vector with bandnames of input data (class: character)
force_rstats_init <- function(dates, sensors, bandnames){
return(c("Breakdate1", "Breakdate2", "Breakdate3", "NR", "RMSD1",
"RMSD2", "RMSD3", "Trend1", "Trend2", "Trend3", "Trend4"))
}

# inarray: 2D-array with dim = c(length(dates), length(bandnames))
# No-Data values are encoded as NA. (class: Integer)
# dates: vector with dates of input data (class: Date)
# sensors: vector with sensors of input data (class: character)
# bandnames: vector with bandnames of input data (class: character)
# nproc: number of CPUs the UDF may use. Always 1 for pixel functions (class: Integer)

force_rstats_pixel <- function(inarray, dates, sensors, bandnames, nproc){

# check for missing and zero values
if (all(inarray == 0) | all (is.na(inarray))){

return(rep(-99999, 11))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you change this to -9999, please?

}

# extract year and doy from date information of inarray
years <- dates |>
format("%Y") |>
as.numeric()

doy <- dates |>
format("%j") |>
as.numeric()

# create year identifier for the time series
x <- years + doy/365

# check for non-unique values and create mean value if two observations per time from different sensors appear
y <- split(inarray[,1], x) |> sapply(mean, na.rm = TRUE)

# check the uniqueness of x, otherwise zoo() will return an error
x <- unique(x)

# create time series
ts <- zoo(y, x, frequency = 365) |>
as.ts()

#save(list = ls(), file = "workspace.RData", envir = environment())

# apply bfast with 3 breaks
bf = bfastlite(ts, breaks = 3)

# get trend, breaks and break dates
breakpoints_table <- strucchangeRcpp::breakpoints(bf[["breakpoints"]])
breakpoints <- breakpoints_table$breakpoints
break_dates <- x[breakpoints]

# if breakpoints are determined, retrieve magnitude, rmsd, and number of breakpoints
if (any(!is.na(breakpoints))){
mag <- magnitude(bf$breakpoints)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please fix the indendation in this block, as well as add a space before "else" in l. 79?

rmsd <- as.numeric(unlist(mag$Mag[,4]))
nr <- as.numeric(length(breakpoints))

# calculate trend
trendtable <- data.frame(coef(bf$breakpoints))
trendtable$trend_recl <- with(trendtable, ifelse(trend < 0, 1, 2))
trend <- as.numeric(trendtable[,9])

# otherwise return 0
}else {
break_dates <- rep(0,3)
rmsd <- rep(0,3)
nr <- 0
trend <- rep(0,4)
}

return(c(break_dates[1], break_dates[2], break_dates[3],nr, rmsd[1], rmsd[2], rmsd[3], trend[1], trend[2], trend[3], trend[4]))

}