main.c 7.0 KB

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