Skip to content

Commit

Permalink
bug fix on calc_flux: be careful of approx(), it can flip the order o…
Browse files Browse the repository at this point in the history
…f xycoord and cause bug. missing scal added in. inherit dots arguments in more explicit way.
  • Loading branch information
jinyung committed Dec 11, 2018
1 parent b62af09 commit 319f565
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions R/calc_flux.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
calc_flux <- function(FUN = get_fluxln, vf_path,
side = c('right', 'left'),
refv = 0, width, ...) {
refv = 0, width, scal, ...) {
side <- match.arg(side)
fluxlnpts <- FUN(...)
projected_v <- proj_v(fluxlnpts, vf_path = vf_path, side = side)

# return sum of vectors * width they represent, after adjustment by relative
# velocity
if (as.character(substitute(FUN)) == 'get_fluxln')
width <- ln_width*scal*ln_width_scal
if (as.character(substitute(FUN)) == 'get_fluxln') {
dot_args <- list(...)
width <- dot_args$ln_width*scal*dot_args$ln_width_scal
}
return((mean(projected_v)+refv) * width)
}

Expand All @@ -32,7 +34,7 @@ tcalc_flux <-function(body_land_array, vf_dir, id, vect_scal,
refv <- calc_refv(body_land_array, type = type, scal = scal,
t_stamp = t_stamp)
} else {
refv = 0
refv = rep(0, length(id))
}

# calc flux
Expand All @@ -42,9 +44,9 @@ tcalc_flux <-function(body_land_array, vf_dir, id, vect_scal,
vf_path = file.path(vf_dir, get_file(id[i])),
refv = refv[i],
vect_scal = vect_scal,
ln_width, ln_width_scal = ln_width_scal,
ln_width = ln_width, ln_width_scal = ln_width_scal,
side = side,
body_land = body_land_array[, , i])
body_land = body_land_array[, , i], scal = scal)
}

return(result)
Expand All @@ -61,7 +63,7 @@ proj_v <- function(xycoord, vf_path, nseg = 8, side = c('right', 'left')) {
# get the xycoords of these 17 points
approx_out <- approx(xycoord, n = nseg*2+1) # in/output in xy.coords format
# from the indices, get the center points of each segment
flux_ln <- lapply(approx_out, `[`, cell_idx)
flux_ln <- lapply(approx_out, `[`, cell_idx)

## interpolate vector field on flux line
# get the vector field
Expand All @@ -77,8 +79,8 @@ proj_v <- function(xycoord, vf_path, nseg = 8, side = c('right', 'left')) {
## projection
# let's make flux line a vector
flux_ln_mat <- xycoords2mat(flux_ln) # just change format
flux_ln_vec <- unitv(flux_ln_mat[2, ] - flux_ln_mat[1, ])

flux_ln_vec <- unitv(xycoords2mat(xycoord)[2, ] -xycoords2mat(xycoord)[1, ] )
# unit normal vector, have two in either direction
# find normal vector by swapping x and y, and minus to either one
unormv1 <- c(flux_ln_vec[2], -flux_ln_vec[1]) # clockwise (right)
Expand Down
2 changes: 1 addition & 1 deletion man/calc_flux.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ proj_v(xycoord, vf_path, nseg = 8, side = c('right', 'left'))
\item{ln_width}{passed to \code{\link{get_fluxln}}, single value}
\item{ln_width_scal}{passed to \code{\link{get_fluxln}}, single value}
\item{t_stamp}{passed to \code{\link{calc_refv}}}
\item{scal}{passed to \code{\link{get_fluxln}}}
\item{scal}{scale in length/ pixel to scale \code{width}}
\item{xycoord}{xy coordinates of two points defining the line segment to
calculate flux, should in \code{R \link{xy.coords}} format}
\item{nseg}{(int) how many segments the flux line consists of, arbitrarily set}
Expand Down

0 comments on commit 319f565

Please sign in to comment.