df_percentile_real Function

public function df_percentile_real(df, col_index, percentile) result(perc)

Calculate percentile of real column

Arguments

Type IntentOptional Attributes Name
type(data_frame), intent(in) :: df
integer, intent(in) :: col_index
real(kind=rk), intent(in) :: percentile

Return Value real(kind=rk)


Source Code

    function df_percentile_real(df, col_index, percentile) result(perc)
        type(data_frame), intent(in) :: df
        integer, intent(in) :: col_index
        real(rk), intent(in) :: percentile
        real(rk) :: perc

        real(rk), dimension(:), allocatable :: col, sorted_col
        integer :: n, idx
        real(rk) :: pos
        type(column) :: data_col

        if (percentile < 0.0_rk .or. percentile > 100.0_rk) error stop "percentile must be between 0 and 100"

        if (col_index < 1 .or. col_index > df % ncols()) error stop "column index out of range"

        data_col = df % get_data_col(col_index)
        if (data_col % get_type() /= REAL_NUM) error stop "column is not real type"

        col = data_col % getr()
        n = size(col)
        sorted_col = col

        call quick_sort_real(sorted_col, 1, n)

        pos = (percentile / 100.0_rk) * real(n - 1, rk) + 1.0_rk
        idx = int(pos)

        if (idx >= n) then
            perc = sorted_col(n)
        else if (idx < 1) then
            perc = sorted_col(1)
        else
            ! Linear interpolation
            perc = sorted_col(idx) + (pos - real(idx, rk)) * (sorted_col(idx + 1) - sorted_col(idx))
        end if
    end function df_percentile_real