@@ -254,11 +254,13 @@ integrate_profile.vp <- function(x, alt_min = 0, alt_max = Inf, alpha = NA,
254
254
denscum = cumsum(weight_densdh )
255
255
denscum [is.na(denscum )]= 0
256
256
# 2) find lowerbound index:
257
+ # NOTE: findInterval gives zero when height_quantile < first element of denscum)
257
258
height_index_lower = findInterval(height_quantile , denscum )
258
259
# 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 ])
262
264
height_quantile_upper <- denscum [min(height_index_lower + 1 ,length(denscum ))]
263
265
# 4) do a linear interpolation to estimate the altitude at the quantile of interest
264
266
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,
429
431
denscum = apply(weight_densdh , 2 , cumsum )
430
432
denscum [is.na(denscum )]= 0
431
433
# 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
433
437
# 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
437
443
height_quantile_upper <- denscum [seq(0 ,nrow(denscum )* (ncol(denscum )- 1 ),nrow(denscum ))+ pmin(height_index_lower + 1 ,nrow(denscum ))]
438
444
# 4) do a linear interpolation to estimate the altitude at the quantile of interest
439
445
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?
440
447
delta_linear_interpolation [is.na(delta_linear_interpolation )]= 0
441
448
# 5) store the quantile flight altitude as height
442
449
height <- height_lower + delta_linear_interpolation
0 commit comments