Skip to content

Commit 2d94d8a

Browse files
committed
fix use of height_quantile argument in integrate_profile() (#627)
1 parent 4f5b129 commit 2d94d8a

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

R/integrate_profile.R

+14-7
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ integrate_profile.vp <- function(x, alt_min = 0, alt_max = Inf, alpha = NA,
254254
denscum=cumsum(weight_densdh)
255255
denscum[is.na(denscum)]=0
256256
# 2) find lowerbound index:
257+
# NOTE: findInterval gives zero when height_quantile < first element of denscum)
257258
height_index_lower=findInterval(height_quantile, denscum)
258259
# 3) find the two height bins closest to the quantile of interest
259-
height_lower=x$data$height[height_index_lower] + interval / 2
260-
height_upper=x$data$height[min(height_index_lower+1,length(denscum))] + interval / 2
261-
height_quantile_lower <- denscum[height_index_lower]
260+
# NOTE: x$data$height indicates the lower end of a height bin.
261+
height_lower=x$data$height[min(height_index_lower+1,length(denscum))]
262+
height_upper=x$data$height[min(height_index_lower+2,length(denscum))]
263+
height_quantile_lower <- ifelse(height_index_lower==0,0,denscum[height_index_lower])
262264
height_quantile_upper <- denscum[min(height_index_lower+1,length(denscum))]
263265
# 4) do a linear interpolation to estimate the altitude at the quantile of interest
264266
delta_linear_interpolation <- (height_quantile-height_quantile_lower)*(height_upper-height_lower)/(height_quantile_upper-height_quantile_lower)
@@ -429,14 +431,19 @@ integrate_profile.vpts <- function(x, alt_min = 0, alt_max = Inf,
429431
denscum=apply(weight_densdh, 2, cumsum)
430432
denscum[is.na(denscum)]=0
431433
# 2) find lowerbound index:
432-
height_index_lower=apply(denscum,2,findInterval,x=height_quantile)
434+
height_index_lower=height_index_lower_NA=apply(denscum,2,findInterval,x=height_quantile)
435+
# change zero indices to NA (to allow subsetting of height_quantile_lower below):
436+
height_index_lower_NA[height_index_lower_NA==0]=NA
433437
# 3) find the two height bins closest to the quantile of interest
434-
height_lower=x$height[height_index_lower] + interval / 2
435-
height_upper=x$height[pmin(height_index_lower+1,nrow(denscum))] + interval / 2
436-
height_quantile_lower <- denscum[seq(0,nrow(denscum)*(ncol(denscum)-1),nrow(denscum))+height_index_lower]
438+
height_lower=x$height[pmin(height_index_lower+1,nrow(denscum))]
439+
height_upper=x$height[pmin(height_index_lower+2,nrow(denscum))]
440+
height_quantile_lower <- denscum[seq(0,nrow(denscum)*(ncol(denscum)-1),nrow(denscum))+height_index_lower_NA]
441+
# NA indicates lower end of first bin, which equals zero:
442+
height_quantile_lower[is.na(height_quantile_lower)]=0
437443
height_quantile_upper <- denscum[seq(0,nrow(denscum)*(ncol(denscum)-1),nrow(denscum))+pmin(height_index_lower+1,nrow(denscum))]
438444
# 4) do a linear interpolation to estimate the altitude at the quantile of interest
439445
delta_linear_interpolation <- (height_quantile-height_quantile_lower)*(height_upper-height_lower)/(height_quantile_upper-height_quantile_lower)
446+
# CHECK: this statement should not be necessary, can we remove?
440447
delta_linear_interpolation[is.na(delta_linear_interpolation)]=0
441448
# 5) store the quantile flight altitude as height
442449
height <- height_lower+delta_linear_interpolation

0 commit comments

Comments
 (0)