main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /****************************************************************************
  2. *
  3. * MODULE: r.quantile
  4. * AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor),
  5. * PURPOSE: Compute quantiles using two passes
  6. *
  7. * This program is free software under the GNU General Public
  8. * License (>=v2). Read the file COPYING that comes with GRASS
  9. * for details.
  10. *
  11. *****************************************************************************/
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. #include <grass/glocale.h>
  18. struct bin
  19. {
  20. unsigned long origin;
  21. DCELL min, max;
  22. int base, count;
  23. };
  24. static int rows, cols;
  25. static DCELL min, max;
  26. static int num_quants;
  27. static DCELL *quants;
  28. static int num_slots;
  29. static unsigned int *slots;
  30. static DCELL slot_size;
  31. static unsigned long total;
  32. static int num_values;
  33. static unsigned short *slot_bins;
  34. static int num_bins;
  35. static struct bin *bins;
  36. static DCELL *values;
  37. static inline int get_slot(DCELL c)
  38. {
  39. int i = (int)floor((c - min) / slot_size);
  40. if (i < 0)
  41. i = 0;
  42. if (i > num_slots - 1)
  43. i = num_slots - 1;
  44. return i;
  45. }
  46. static inline double get_quantile(int n)
  47. {
  48. return (double)total * quants[n];
  49. }
  50. static void get_slot_counts(int infile)
  51. {
  52. DCELL *inbuf = Rast_allocate_d_buf();
  53. int row, col;
  54. G_message(_("Computing histogram"));
  55. total = 0;
  56. for (row = 0; row < rows; row++) {
  57. Rast_get_d_row(infile, inbuf, row);
  58. for (col = 0; col < cols; col++) {
  59. int i;
  60. if (Rast_is_d_null_value(&inbuf[col]))
  61. continue;
  62. i = get_slot(inbuf[col]);
  63. slots[i]++;
  64. total++;
  65. }
  66. G_percent(row, rows, 2);
  67. }
  68. G_percent(rows, rows, 2);
  69. G_free(inbuf);
  70. }
  71. static void initialize_bins(void)
  72. {
  73. int slot;
  74. double next;
  75. int bin = 0;
  76. unsigned long accum = 0;
  77. int quant = 0;
  78. G_message(_("Computing bins"));
  79. num_values = 0;
  80. next = get_quantile(quant);
  81. for (slot = 0; slot < num_slots; slot++) {
  82. unsigned int count = slots[slot];
  83. unsigned long accum2 = accum + count;
  84. if (accum2 > next) {
  85. struct bin *b = &bins[bin];
  86. slot_bins[slot] = ++bin;
  87. b->origin = accum;
  88. b->base = num_values;
  89. b->count = 0;
  90. b->min = min + slot_size * slot;
  91. b->max = min + slot_size * (slot + 1);
  92. while (accum2 > next)
  93. next = get_quantile(++quant);
  94. num_values += count;
  95. }
  96. accum = accum2;
  97. }
  98. num_bins = bin;
  99. }
  100. static void fill_bins(int infile)
  101. {
  102. DCELL *inbuf = Rast_allocate_d_buf();
  103. int row, col;
  104. G_message(_("Binning data"));
  105. for (row = 0; row < rows; row++) {
  106. Rast_get_d_row(infile, inbuf, row);
  107. for (col = 0; col < cols; col++) {
  108. int i, bin;
  109. struct bin *b;
  110. if (Rast_is_d_null_value(&inbuf[col]))
  111. continue;
  112. i = get_slot(inbuf[col]);
  113. if (!slot_bins[i])
  114. continue;
  115. bin = slot_bins[i] - 1;
  116. b = &bins[bin];
  117. values[b->base + b->count++] = inbuf[col];
  118. }
  119. G_percent(row, rows, 2);
  120. }
  121. G_percent(rows, rows, 2);
  122. G_free(inbuf);
  123. }
  124. static int compare_dcell(const void *aa, const void *bb)
  125. {
  126. DCELL a = *(const DCELL *)aa;
  127. DCELL b = *(const DCELL *)bb;
  128. if (a < b)
  129. return -1;
  130. if (a > b)
  131. return 1;
  132. return 0;
  133. }
  134. static void sort_bins(void)
  135. {
  136. int bin;
  137. G_message(_("Sorting bins"));
  138. for (bin = 0; bin < num_bins; bin++) {
  139. struct bin *b = &bins[bin];
  140. qsort(&values[b->base], b->count, sizeof(DCELL), compare_dcell);
  141. G_percent(bin, num_bins, 2);
  142. }
  143. G_percent(bin, num_bins, 2);
  144. G_message(_("Computing quantiles"));
  145. }
  146. static void compute_quantiles(int recode)
  147. {
  148. int bin = 0;
  149. double prev_v = min;
  150. int quant;
  151. for (quant = 0; quant < num_quants; quant++) {
  152. struct bin *b;
  153. double next = get_quantile(quant);
  154. double k, v;
  155. int i0, i1;
  156. for (; bin < num_bins; bin++) {
  157. b = &bins[bin];
  158. if (b->origin + b->count >= next)
  159. break;
  160. }
  161. if (bin < num_bins) {
  162. k = next - b->origin;
  163. i0 = (int)floor(k);
  164. i1 = (int)ceil(k);
  165. if (i1 > b->count - 1)
  166. i1 = b->count - 1;
  167. v = (i0 == i1)
  168. ? values[b->base + i0]
  169. : values[b->base + i0] * (i1 - k) +
  170. values[b->base + i1] * (k - i0);
  171. }
  172. else
  173. v = max;
  174. if (recode)
  175. printf("%f:%f:%i\n", prev_v, v, quant + 1);
  176. else
  177. printf("%d:%f:%f\n", quant, 100 * quants[quant], v);
  178. prev_v = v;
  179. }
  180. if (recode)
  181. printf("%f:%f:%i\n", prev_v, max, num_quants + 1);
  182. }
  183. int main(int argc, char *argv[])
  184. {
  185. struct GModule *module;
  186. struct
  187. {
  188. struct Option *input, *quant, *perc, *slots;
  189. } opt;
  190. struct {
  191. struct Flag *r;
  192. } flag;
  193. int recode;
  194. int infile;
  195. struct FPRange range;
  196. G_gisinit(argv[0]);
  197. module = G_define_module();
  198. G_add_keyword(_("raster"));
  199. G_add_keyword(_("algebra"));
  200. G_add_keyword(_("statistics"));
  201. module->description = _("Compute quantiles using two passes.");
  202. opt.input = G_define_standard_option(G_OPT_R_INPUT);
  203. opt.quant = G_define_option();
  204. opt.quant->key = "quantiles";
  205. opt.quant->type = TYPE_INTEGER;
  206. opt.quant->required = NO;
  207. opt.quant->description = _("Number of quantiles");
  208. opt.quant->answer = "4";
  209. opt.perc = G_define_option();
  210. opt.perc->key = "percentiles";
  211. opt.perc->type = TYPE_DOUBLE;
  212. opt.perc->required = NO;
  213. opt.perc->multiple = YES;
  214. opt.perc->description = _("List of percentiles");
  215. opt.slots = G_define_option();
  216. opt.slots->key = "bins";
  217. opt.slots->type = TYPE_INTEGER;
  218. opt.slots->required = NO;
  219. opt.slots->description = _("Number of bins to use");
  220. opt.slots->answer = "1000000";
  221. flag.r = G_define_flag();
  222. flag.r->key = 'r';
  223. flag.r->description = _("Generate recode rules based on quantile-defined intervals.");
  224. if (G_parser(argc, argv))
  225. exit(EXIT_FAILURE);
  226. num_slots = atoi(opt.slots->answer);
  227. recode = flag.r->answer;
  228. if (opt.perc->answer) {
  229. int i;
  230. for (i = 0; opt.perc->answers[i]; i++) ;
  231. num_quants = i;
  232. quants = G_calloc(num_quants, sizeof(DCELL));
  233. for (i = 0; i < num_quants; i++)
  234. quants[i] = atof(opt.perc->answers[i]) / 100;
  235. qsort(quants, num_quants, sizeof(DCELL), compare_dcell);
  236. }
  237. else {
  238. int i;
  239. num_quants = atoi(opt.quant->answer) - 1;
  240. quants = G_calloc(num_quants, sizeof(DCELL));
  241. for (i = 0; i < num_quants; i++)
  242. quants[i] = 1.0 * (i + 1) / (num_quants + 1);
  243. }
  244. if (num_quants > 65535)
  245. G_fatal_error(_("Too many quantiles"));
  246. infile = Rast_open_old(opt.input->answer, "");
  247. Rast_read_fp_range(opt.input->answer, "", &range);
  248. Rast_get_fp_range_min_max(&range, &min, &max);
  249. slots = G_calloc(num_slots, sizeof(unsigned int));
  250. slot_bins = G_calloc(num_slots, sizeof(unsigned short));
  251. slot_size = (max - min) / num_slots;
  252. rows = Rast_window_rows();
  253. cols = Rast_window_cols();
  254. get_slot_counts(infile);
  255. bins = G_calloc(num_quants, sizeof(struct bin));
  256. initialize_bins();
  257. G_free(slots);
  258. values = G_calloc(num_values, sizeof(DCELL));
  259. fill_bins(infile);
  260. Rast_close(infile);
  261. G_free(slot_bins);
  262. sort_bins();
  263. compute_quantiles(recode);
  264. return (EXIT_SUCCESS);
  265. }