Skip to content

Commit

Permalink
remove ind
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafnuss committed Jan 27, 2024
1 parent 6d43645 commit f7b38e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
7 changes: 3 additions & 4 deletions R/ind2path.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#' @return A path data.frame
#' - `stap_id` stationary period identification
#' - `j` identification for each trajectory (1 to $nj$).
#' - `ind` indices of the coordinate in the 2D grid. Useful to retrieve map or graph information.
#' - `lat` Latitude,
#' - `lon` longitude
#' - `...` other columns from `stap`
Expand Down Expand Up @@ -84,9 +83,9 @@ ind2path <- function(ind,
path$lon[path$known] <- path$known_lon[path$known]
path$lat[path$known] <- path$known_lat[path$known]

lon_ind_known <- sapply(path$known_lon[path$known], \(x) which.min(abs(g$lon - x)))
lat_ind_known <- sapply(path$known_lat[path$known], \(x) which.min(abs(g$lat - x)))
path$ind[path$known] <- (lon_ind_known - 1) * g$dim[1] + lat_ind_known
# lon_ind_known <- sapply(path$known_lon[path$known], \(x) which.min(abs(g$lon - x)))
# lat_ind_known <- sapply(path$known_lat[path$known], \(x) which.min(abs(g$lat - x)))
# path$ind[path$known] <- (lon_ind_known - 1) * g$dim[1] + lat_ind_known
}

# Remove known_lat and known_lon
Expand Down
24 changes: 16 additions & 8 deletions R/path2edge.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,35 @@
#' @export
path2edge <- function(path, tag_graph) {
assertthat::assert_that(is.data.frame(path))
assertthat::assert_that(assertthat::has_name(path, c("stap_id", "ind")))
assertthat::assert_that(assertthat::has_name(path, c("stap_id")))

assertthat::assert_that(inherits(tag_graph, "tag") | inherits(tag_graph, "graph"))
stap <- tag_graph$stap
assertthat::assert_that(all(unique(path$stap_id) == stap$stap_id))

g <- map_expand(tag_graph$param$extent, tag_graph$param$scale)
nll <- prod(g$dim)

# Number of paths
nj <- length(unique(path$j))
if ("j" %in% names(path)){
nj <- length(unique(path$j))
} else {
nj = 1
}


# Find stap included
stap_id_included <- unique(path$stap_id[!is.na(path$ind)])
stap_id_included <- unique(path$stap_id[!is.na(path$lat) & !is.na(path$lon)])
# should be similar to stap$include in most case.

ind2d <- matrix(path$ind[path$stap_id %in% stap_id_included], nrow = nj)
lat <- matrix(path$lat[path$stap_id %in% stap_id_included], nrow = nj)
lon <- matrix(path$lon[path$stap_id %in% stap_id_included], nrow = nj)

ind3d <- ind2d + t(replicate(nj, nll * (seq_len(ncol(ind2d)) - 1)))
ind_lat <- sapply(lat, \(l) which.min(abs(l - g$lat)) )
ind_lon <- sapply(lon, \(l) which.min(abs(l - g$lon)) )

ind2d <- matrix(ind_lat + (ind_lon-1) * g$dim[1], nrow = nj)

ind3d <- ind2d + t(replicate(nj, prod(g$dim) * (seq_len(ncol(ind2d)) - 1)))

stap3d <- t(replicate(nj, stap_id_included))

Expand Down Expand Up @@ -86,10 +94,10 @@ path2edge <- function(path, tag_graph) {
if (inherits(tag_graph, "graph")) {
# Check that all sources and target exist in the graph
assertthat::assert_that(all(edge$s %in% tag_graph$s),
msg = "path$ind is not compatible with the graph$s."
msg = "path is not compatible with the graph."
)
assertthat::assert_that(all(edge$t %in% tag_graph$t),
msg = "path$ind is not compatible with the graph$t."
msg = "path is not compatible with the graph."
)

# Build data.frame of the graph
Expand Down
1 change: 0 additions & 1 deletion R/pressurepath_create.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
#' - `pressure_tag` same as `pressure$value`
#' - `label` same as `pressure$label`
#' - `j` same as `path$j`
#' - `ind` same as `path$ind`
#' - `lat` same as `path$lat`
#' - `lon` same as `path$lon`
#' - `include` same as `path$include`
Expand Down

0 comments on commit f7b38e1

Please sign in to comment.