main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /***************************************************************
  2. *
  3. * MODULE: v.normal
  4. *
  5. * AUTHOR(S): James Darrell McCauley darrell@mccauley-usa.com
  6. * OGR support by Martin Landa <landa.martin gmail.com> (2009)
  7. *
  8. * PURPOSE: GRASS program for distributional testing (based on s.normal)
  9. *
  10. * COPYRIGHT: (C) 2001-2014 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Public License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. * Modification History:
  16. * <23 Jan 2001> - added field parameter, fixed reading of sites (MN)
  17. * <27 Aug 1994> - began coding. Adapted cdh.f from statlib (jdm)
  18. * <30 Sep 1994> - finished alpha version of cdh-c (jdm)
  19. * <10 Oct 1994> - announced version 0.1B on pasture.ecn.purdue.edu (jdm)
  20. * <02 Jan 1995> - cleaned man page, added html page (jdm)
  21. * <25 Feb 1995> - cleaned 'gcc -Wall' warnings (jdm)
  22. * <21 Jun 1995> - adapted to use new sites API (jdm)
  23. * <13 Sep 2000> - released under GPL
  24. *
  25. **************************************************************/
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <grass/gis.h>
  29. #include <grass/dbmi.h>
  30. #include <grass/vector.h>
  31. #include <grass/cdhc.h>
  32. #include <grass/glocale.h>
  33. int scan_cats(char *, long *, long *);
  34. int main(int argc, char **argv)
  35. {
  36. int i, nsites, warn_once = 0;
  37. int all;
  38. long x, y;
  39. struct Cell_head window;
  40. struct GModule *module;
  41. struct
  42. {
  43. struct Option *input, *tests, *dfield, *layer;
  44. } parm;
  45. struct
  46. {
  47. struct Flag *q, *l, *region;
  48. } flag;
  49. double *w, *z;
  50. struct Map_info Map;
  51. int line, nlines, npoints;
  52. int field;
  53. struct line_pnts *Points;
  54. struct line_cats *Cats;
  55. struct bound_box box;
  56. /* Attributes */
  57. int nrecords;
  58. int ctype;
  59. struct field_info *Fi;
  60. dbDriver *Driver;
  61. dbCatValArray cvarr;
  62. G_gisinit(argv[0]);
  63. module = G_define_module();
  64. G_add_keyword(_("vector"));
  65. G_add_keyword(_("statistics"));
  66. G_add_keyword(_("points"));
  67. G_add_keyword(_("point pattern"));
  68. module->description = _("Tests for normality for vector points.");
  69. parm.input = G_define_standard_option(G_OPT_V_MAP);
  70. parm.layer = G_define_standard_option(G_OPT_V_FIELD);
  71. parm.tests = G_define_option();
  72. parm.tests->key = "tests";
  73. parm.tests->key_desc = "range";
  74. parm.tests->type = TYPE_STRING;
  75. parm.tests->multiple = YES;
  76. parm.tests->required = YES;
  77. parm.tests->label = _("Lists of tests (1-15)");
  78. parm.tests->description = _("E.g. 1,3-8,13");
  79. parm.dfield = G_define_standard_option(G_OPT_DB_COLUMN);
  80. parm.dfield->required = YES;
  81. flag.region = G_define_flag();
  82. flag.region->key = 'r';
  83. flag.region->description = _("Use only points in current region");
  84. flag.l = G_define_flag();
  85. flag.l->key = 'l';
  86. flag.l->description = _("Lognormality instead of normality");
  87. if (G_parser(argc, argv))
  88. exit(EXIT_FAILURE);
  89. all = flag.region->answer ? 0 : 1;
  90. /* Open input */
  91. Vect_set_open_level(2);
  92. if (Vect_open_old2(&Map, parm.input->answer, "", parm.layer->answer) < 0)
  93. G_fatal_error(_("Unable to open vector map <%s>"), parm.input->answer);
  94. field = Vect_get_field_number(&Map, parm.layer->answer);
  95. /* Read attributes */
  96. Fi = Vect_get_field(&Map, field);
  97. if (Fi == NULL) {
  98. G_fatal_error("Database connection not defined for layer %d", field);
  99. }
  100. Driver = db_start_driver_open_database(Fi->driver, Fi->database);
  101. if (Driver == NULL)
  102. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  103. Fi->database, Fi->driver);
  104. nrecords = db_select_CatValArray(Driver, Fi->table, Fi->key, parm.dfield->answer,
  105. NULL, &cvarr);
  106. G_debug(1, "nrecords = %d", nrecords);
  107. ctype = cvarr.ctype;
  108. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
  109. G_fatal_error(_("Only numeric column type supported"));
  110. if (nrecords < 0)
  111. G_fatal_error(_("Unable to select data from table"));
  112. G_verbose_message(_("%d records selected from table"), nrecords);
  113. db_close_database_shutdown_driver(Driver);
  114. /* Read points */
  115. npoints = Vect_get_num_primitives(&Map, GV_POINT);
  116. z = (double *)G_malloc(npoints * sizeof(double));
  117. G_get_window(&window);
  118. Vect_region_box(&window, &box);
  119. Points = Vect_new_line_struct();
  120. Cats = Vect_new_cats_struct();
  121. nlines = Vect_get_num_lines(&Map);
  122. nsites = 0;
  123. for (line = 1; line <= nlines; line++) {
  124. int type, cat, ret, cval;
  125. double dval;
  126. G_debug(3, "line = %d", line);
  127. type = Vect_read_line(&Map, Points, Cats, line);
  128. if (!(type & GV_POINT))
  129. continue;
  130. if (!all) {
  131. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &box))
  132. continue;
  133. }
  134. Vect_cat_get(Cats, 1, &cat);
  135. G_debug(3, "cat = %d", cat);
  136. /* find actual value */
  137. if (ctype == DB_C_TYPE_INT) {
  138. ret = db_CatValArray_get_value_int(&cvarr, cat, &cval);
  139. if (ret != DB_OK) {
  140. G_warning(_("No record for cat %d"), cat);
  141. continue;
  142. }
  143. dval = cval;
  144. }
  145. else if (ctype == DB_C_TYPE_DOUBLE) {
  146. ret = db_CatValArray_get_value_double(&cvarr, cat, &dval);
  147. if (ret != DB_OK) {
  148. G_warning(_("No record for cat %d"), cat);
  149. continue;
  150. }
  151. }
  152. G_debug(3, "dval = %e", dval);
  153. z[nsites] = dval;
  154. nsites++;
  155. }
  156. G_verbose_message(_("Number of points: %d"), nsites);
  157. if (nsites <= 0)
  158. G_fatal_error(_("No points found"));
  159. if (nsites < 4)
  160. G_warning(_("Too small sample"));
  161. if (flag.l->answer) {
  162. warn_once = 0;
  163. for (i = 0; i < nsites; ++i) {
  164. if (z[i] > 1.0e-10)
  165. z[i] = log10(z[i]);
  166. else if (!warn_once) {
  167. G_warning(_("Negative or very small point values set to -10.0"));
  168. z[i] = -10.0;
  169. warn_once = 1;
  170. }
  171. }
  172. }
  173. for (i = 0; parm.tests->answers[i]; i++)
  174. if (!scan_cats(parm.tests->answers[i], &x, &y)) {
  175. G_usage();
  176. exit(EXIT_FAILURE);
  177. }
  178. for (i = 0; parm.tests->answers[i]; i++) {
  179. scan_cats(parm.tests->answers[i], &x, &y);
  180. while (x <= y)
  181. switch (x++) {
  182. case 1: /* moments */
  183. fprintf(stdout, _("Moments \\sqrt{b_1} and b_2: "));
  184. w = Cdhc_omnibus_moments(z, nsites);
  185. fprintf(stdout, "%g %g\n", w[0], w[1]);
  186. break;
  187. case 2: /* geary */
  188. fprintf(stdout, _("Geary's a-statistic & an approx. normal: "));
  189. w = Cdhc_geary_test(z, nsites);
  190. fprintf(stdout, "%g %g\n", w[0], w[1]);
  191. break;
  192. case 3: /* extreme deviates */
  193. fprintf(stdout, _("Extreme normal deviates: "));
  194. w = Cdhc_extreme(z, nsites);
  195. fprintf(stdout, "%g %g\n", w[0], w[1]);
  196. break;
  197. case 4: /* D'Agostino */
  198. fprintf(stdout, _("D'Agostino's D & an approx. normal: "));
  199. w = Cdhc_dagostino_d(z, nsites);
  200. fprintf(stdout, "%g %g\n", w[0], w[1]);
  201. break;
  202. case 5: /* Kuiper */
  203. fprintf(stdout,
  204. _("Kuiper's V (regular & modified for normality): "));
  205. w = Cdhc_kuipers_v(z, nsites);
  206. fprintf(stdout, "%g %g\n", w[1], w[0]);
  207. break;
  208. case 6: /* Watson */
  209. fprintf(stdout,
  210. _("Watson's U^2 (regular & modified for normality): "));
  211. w = Cdhc_watson_u2(z, nsites);
  212. fprintf(stdout, "%g %g\n", w[1], w[0]);
  213. break;
  214. case 7: /* Durbin */
  215. fprintf(stdout,
  216. _("Durbin's Exact Test (modified Kolmogorov): "));
  217. w = Cdhc_durbins_exact(z, nsites);
  218. fprintf(stdout, "%g\n", w[0]);
  219. break;
  220. case 8: /* Anderson-Darling */
  221. fprintf(stdout,
  222. _("Anderson-Darling's A^2 (regular & modified for normality): "));
  223. w = Cdhc_anderson_darling(z, nsites);
  224. fprintf(stdout, "%g %g\n", w[1], w[0]);
  225. break;
  226. case 9: /* Cramer-Von Mises */
  227. fprintf(stdout,
  228. _("Cramer-Von Mises W^2(regular & modified for normality): "));
  229. w = Cdhc_cramer_von_mises(z, nsites);
  230. fprintf(stdout, "%g %g\n", w[1], w[0]);
  231. break;
  232. case 10: /* Kolmogorov-Smirnov */
  233. fprintf(stdout,
  234. _("Kolmogorov-Smirnov's D (regular & modified for normality): "));
  235. w = Cdhc_kolmogorov_smirnov(z, nsites);
  236. fprintf(stdout, "%g %g\n", w[1], w[0]);
  237. break;
  238. case 11: /* chi-square */
  239. fprintf(stdout,
  240. _("Chi-Square stat (equal probability classes) and d.f.: "));
  241. w = Cdhc_chi_square(z, nsites);
  242. fprintf(stdout, "%g %d\n", w[0], (int)w[1]);
  243. break;
  244. case 12: /* Shapiro-Wilk */
  245. if (nsites > 50) {
  246. G_warning(_("Shapiro-Wilk's W cannot be used for n > 50"));
  247. if (nsites < 99)
  248. G_message(_("Use Weisberg-Binghams's W''"));
  249. }
  250. else {
  251. fprintf(stdout, _("Shapiro-Wilk W: "));
  252. w = Cdhc_shapiro_wilk(z, nsites);
  253. fprintf(stdout, "%g\n", w[0]);
  254. }
  255. break;
  256. case 13: /* Weisberg-Bingham */
  257. if (nsites > 99 || nsites < 50)
  258. G_warning(_("Weisberg-Bingham's W'' cannot be used for n < 50 or n > 99"));
  259. else {
  260. fprintf(stdout, _("Weisberg-Bingham's W'': "));
  261. w = Cdhc_weisberg_bingham(z, nsites);
  262. fprintf(stdout, "%g\n", w[0]);
  263. }
  264. break;
  265. case 14: /* Royston */
  266. if (nsites > 2000)
  267. G_warning(_("Royston only extended Shapiro-Wilk's W up to n = 2000"));
  268. else {
  269. fprintf(stdout, _("Shapiro-Wilk W'': "));
  270. w = Cdhc_royston(z, nsites);
  271. fprintf(stdout, "%g\n", w[0]);
  272. }
  273. break;
  274. case 15: /* Kotz */
  275. fprintf(stdout, _("Kotz' T'_f (Lognormality vs. Normality): "));
  276. w = Cdhc_kotz_families(z, nsites);
  277. fprintf(stdout, "%g\n", w[0]);
  278. break;
  279. default:
  280. break;
  281. }
  282. }
  283. exit(EXIT_SUCCESS);
  284. }