Skip to content

Commit

Permalink
create path2df() and utils.R
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafnuss committed Oct 31, 2022
1 parent 0f706d6 commit 70dfa6e
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 17 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(light2mat)
export(pam_classify)
export(pam_read)
export(pam_sta)
export(path2df)
export(refracted)
export(solar)
export(trainset_read)
Expand Down
6 changes: 6 additions & 0 deletions R/geopressure.R
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,12 @@ geopressure_ts_path <- function(path,
#' stationary period id (`sta_id`) as column. Optionally, if indexes were requested, it will be
#' return. You will need to use `which.max(as.matrix(raster))` and not `which.max(raster)` to get
#' the correct location.
#' @examples
#' # See `geopressure_prob_map()` for generating pressure_prob
#' pressure_prob_1 <- readRDS(system.file("extdata/1_pressure/", "18LX_pressure_prob_1.rda",
#' package = "GeoPressureR"
#' ))
#' geopressure_map2path(list(pressure_prob_1))
#' @seealso [`geopressure_prob_map()`], [`geopressure_ts_path()`], [GeoPressureManual | Pressure Map
#' ](https://raphaelnussbaumer.com/GeoPressureManual/pressure-map.html#compute-altitude)
#' @export
Expand Down
17 changes: 0 additions & 17 deletions R/progress_bar.R

This file was deleted.

79 changes: 79 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#' Progress bar function
#' @param x value of the current status
#' @param max maximum value that x will reach
#' @param text text to display
#' @noRd

progress_bar <- function(x, max = 100, text = "") {
percent <- x / max * 100
cat(sprintf(
"\r[%-50s] %d / %d %s",
paste(rep("=", percent / 2), collapse = ""),
x, max, text
))
if (x == max) {
cat("\n")
}
}

#' Path to data.frame
#'
#' This function convert a GeoPressureR path to a data.frame which can be read in [movevis](
#' https://movevis.org/index.html) for instance.
#'
#' The function basically duplicate location position at the start and end time of each stationary
#' period.
#'
#' @param pam pam logger dataset list with `pam$sta` computed (see `pam_sta`)
#' @param path data.frame containtings the path(s) of the bird with column `lat`, `lon` and `sta_id`
#' at least. Path can be generated with `geopressure_map2path`, `graph_simulation`, `geopressureviz`
#' .
#'
#' @examples
#' pam <- pam_read(
#' pathname = system.file("extdata/0_PAM/18LX", package = "GeoPressureR")
#' )
#' pam <- trainset_read(pam,
#' pathname = system.file("extdata/1_pressure/labels", package = "GeoPressureR")
#' )
#' pam <- pam_sta(pam)
#' pressure_prob_1 <- readRDS(system.file("extdata/1_pressure/", "18LX_pressure_prob_1.rda",
#' package = "GeoPressureR"
#' ))
#' path <- geopressure_map2path(list(pressure_prob_1))
#' path2df(pam, path)
#' @seealso [`movevis::df2move`](https://movevis.org/reference/df2move.html),
#' [`geopressure_map2path`], [`graph_simulation`], [`geopressureviz`]
#' @export
path2df <- function(pam, path) {

# Check input variable
assertthat::assert_that(is.list(pam))
assertthat::assert_that(assertthat::has_name(pam, "sta"))
assertthat::assert_that(is.data.frame(pam$sta))
assertthat::assert_that(assertthat::has_name(pam$sta, c("sta_id", "start", "end")))

assertthat::assert_that(is.list(path))
assertthat::assert_that(assertthat::has_name(path, c("lat", "lon", "sta_id")))

if (is.matrix(path$lat)) {
df0 <- data.frame(
lat = utils::stack(as.data.frame(t(path$lat)))$values,
lon = stack(as.data.frame(t(path$lon)))$values,
sta_id = rep(path$sta_id, dim(path$lat)[1]),
track_id = paste0(pam$id, "_", rep(seq(1, dim(path$lat)[1]), dim(path$lat)[2]))
)
df0 <- merge(df0, pam$sta, by = "sta_id")
} else {
df0 <- merge(as.data.frame(path), pam$sta, by = "sta_id")
df0$track_id <- pam$id
}

df1 <- df0
df1$time <- df1$start
df2 <- df0
df2$time <- df2$end

df <- rbind(df1, df2)
df[, names(df) %in% c("time", "track_id", "lat", "lon")]
}
4 changes: 4 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ reference:
desc: Vizualize trajectory with a shiny app
contents:
- geopressureviz
- title: Utility
desc: Short functions
contents:
- path2df
navbar:
left:
- text: "Reference"
Expand Down
7 changes: 7 additions & 0 deletions man/geopressure_map2path.Rd

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

40 changes: 40 additions & 0 deletions man/path2df.Rd

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

0 comments on commit 70dfa6e

Please # to comment.