Compute the default set of or requested quantiles for a set of data the quantile formula used is same as Excel and R default method.
677 {
679 int init_quantiles = 0;
680 int i = 0;
681 double h;
682 int hl;
683
684#if POSTGIS_DEBUG_LEVEL > 0
685 clock_t start, stop;
686 double elapsed = 0;
687#endif
688
690#if POSTGIS_DEBUG_LEVEL > 0
691 start = clock();
692#endif
693
694 assert(NULL != stats);
695 assert(NULL != rtn_count);
696
698 rterror(
"rt_band_get_quantiles: rt_bandstats object has no value");
699 return NULL;
700 }
701
702
703 if (NULL == quantiles) {
704
705 if (quantiles_count < 2)
706 quantiles_count = 5;
707
708 quantiles =
rtalloc(
sizeof(
double) * quantiles_count);
709 init_quantiles = 1;
710 if (NULL == quantiles) {
711 rterror(
"rt_band_get_quantiles: Could not allocate memory for quantile input");
712 return NULL;
713 }
714
715 quantiles_count--;
716 for (i = 0; i <= quantiles_count; i++)
717 quantiles[i] = ((double) i) / quantiles_count;
718 quantiles_count++;
719 }
720
721
722 for (i = 0; i < quantiles_count; i++) {
723 if (quantiles[i] < 0. || quantiles[i] > 1.) {
724 rterror(
"rt_band_get_quantiles: Quantile value not between 0 and 1");
725 if (init_quantiles)
rtdealloc(quantiles);
726 return NULL;
727 }
728 }
729 quicksort(quantiles, quantiles + quantiles_count - 1);
730
731
733 if (NULL == rtn) {
734 rterror(
"rt_band_get_quantiles: Could not allocate memory for quantile output");
735 if (init_quantiles)
rtdealloc(quantiles);
736 return NULL;
737 }
738
739
743 }
744
745
746
747
748
749
750
751 for (i = 0; i < quantiles_count; i++) {
753
754 h = ((stats->
count - 1.) * quantiles[i]) + 1.;
755 hl = floor(h);
756
757
758 if (h > hl)
760
761 else
763 }
764
765#if POSTGIS_DEBUG_LEVEL > 0
766 stop = clock();
767 elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
769#endif
770
771 *rtn_count = quantiles_count;
772 if (init_quantiles)
rtdealloc(quantiles);
774 return rtn;
775}
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void rtdealloc(void *mem)
static void quicksort(double *left, double *right)