-
Notifications
You must be signed in to change notification settings - Fork 8
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
henriDierkes
wants to merge
3
commits into
davidfrantz:main
Choose a base branch
from
henriDierkes:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
# 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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])) | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?