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.
676 {
678 int init_quantiles = 0;
679 int i = 0;
680 double h;
681 int hl;
682
683#if POSTGIS_DEBUG_LEVEL > 0
684 clock_t start, stop;
685 double elapsed = 0;
686#endif
687
689#if POSTGIS_DEBUG_LEVEL > 0
690 start = clock();
691#endif
692
693 assert(NULL != stats);
694 assert(NULL != rtn_count);
695
697 rterror(
"rt_band_get_quantiles: rt_bandstats object has no value");
698 return NULL;
699 }
700
701
702 if (NULL == quantiles) {
703
704 if (quantiles_count < 2)
705 quantiles_count = 5;
706
707 quantiles =
rtalloc(
sizeof(
double) * quantiles_count);
708 init_quantiles = 1;
709 if (NULL == quantiles) {
710 rterror(
"rt_band_get_quantiles: Could not allocate memory for quantile input");
711 return NULL;
712 }
713
714 quantiles_count--;
715 for (i = 0; i <= quantiles_count; i++)
716 quantiles[i] = ((double) i) / quantiles_count;
717 quantiles_count++;
718 }
719
720
721 for (i = 0; i < quantiles_count; i++) {
722 if (quantiles[i] < 0. || quantiles[i] > 1.) {
723 rterror(
"rt_band_get_quantiles: Quantile value not between 0 and 1");
724 if (init_quantiles)
rtdealloc(quantiles);
725 return NULL;
726 }
727 }
728 quicksort(quantiles, quantiles + quantiles_count - 1);
729
730
732 if (NULL == rtn) {
733 rterror(
"rt_band_get_quantiles: Could not allocate memory for quantile output");
734 if (init_quantiles)
rtdealloc(quantiles);
735 return NULL;
736 }
737
738
742 }
743
744
745
746
747
748
749
750 for (i = 0; i < quantiles_count; i++) {
752
753 h = ((stats->
count - 1.) * quantiles[i]) + 1.;
754 hl = floor(h);
755
756
757 if (h > hl)
759
760 else
762 }
763
764#if POSTGIS_DEBUG_LEVEL > 0
765 stop = clock();
766 elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
768#endif
769
770 *rtn_count = quantiles_count;
771 if (init_quantiles)
rtdealloc(quantiles);
773 return rtn;
774}
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)