main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************************
  2. *
  3. * MODULE: r.random.surface
  4. * AUTHOR(S): Charles Ehlschlaeger, Michael Goodchild, and Chih-chang Lin;
  5. * (National Center for Geographic Information and
  6. * Analysis, University of California, Santa Barbara)
  7. * (original contributors)
  8. * Markus Neteler <neteler itc.it>,
  9. * Bernhard Reiter <bernhard intevation.de>,
  10. * Brad Douglas <rez touchofmadness.com>,
  11. * Glynn Clements <glynn gclements.plus.com>,
  12. * Jachym Cepicky <jachym les-ejk.cz>,
  13. * Jan-Oliver Wagner <jan intevation.de>
  14. * PURPOSE: generates a spatially dependent random surface
  15. * COPYRIGHT: (C) 2000-2008 by the GRASS Development Team
  16. *
  17. * This program is free software under the GNU General Public
  18. * License (>=v2). Read the file COPYING that comes with GRASS
  19. * for details.
  20. *
  21. *****************************************************************************/
  22. /* main.c */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include <grass/glocale.h>
  28. #include "ransurf.h"
  29. #include "local_proto.h"
  30. BIGF BigF;
  31. double **Surface, NS, EW, FilterSD, AllMaxDist, *Norm;
  32. int MapCount, FDM, Rs, Cs, Theory;
  33. CELL *CellBuffer;
  34. FILTER *AllFilters, Filter;
  35. CATINFO CatInfo;
  36. int *Seeds, Seed, NumSeeds, Low, High, NumMaps, NumFilters, OutFD;
  37. char Buf[240], **OutNames, *TheoryName, *Mapset;
  38. struct Flag *Uniform;
  39. struct Option *Distance, *Exponent, *Weight;
  40. struct Option *Output;
  41. struct Option *range_high_stuff;
  42. struct Option *SeedStuff;
  43. int main(int argc, char **argv)
  44. {
  45. struct GModule *module;
  46. int DoMap, DoFilter, MapSeed;
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("raster"));
  50. G_add_keyword(_("surface"));
  51. G_add_keyword(_("random"));
  52. module->description =
  53. _("Generates random surface(s) with spatial dependence.");
  54. Output = G_define_option();
  55. Output->key = "output";
  56. Output->type = TYPE_STRING;
  57. Output->required = YES;
  58. Output->multiple = YES;
  59. Output->description = _("Name for output raster map(s)");
  60. Output->gisprompt = "new,cell,raster";
  61. Distance = G_define_option();
  62. Distance->key = "distance";
  63. Distance->type = TYPE_DOUBLE;
  64. Distance->required = NO;
  65. Distance->multiple = NO;
  66. Distance->description =
  67. _("Maximum distance of spatial correlation (value >= 0.0)");
  68. Distance->answer = "0.0";
  69. Exponent = G_define_option();
  70. Exponent->key = "exponent";
  71. Exponent->type = TYPE_DOUBLE;
  72. Exponent->multiple = NO;
  73. Exponent->required = NO;
  74. Exponent->description = _("Distance decay exponent (value > 0.0)");
  75. Exponent->answer = "1.0";
  76. Weight = G_define_option();
  77. Weight->key = "flat";
  78. Weight->type = TYPE_DOUBLE;
  79. Weight->multiple = NO;
  80. Weight->required = NO;
  81. Weight->description =
  82. _("Distance filter remains flat before beginning exponent");
  83. Weight->answer = "0.0";
  84. SeedStuff = G_define_option();
  85. SeedStuff->key = "seed";
  86. SeedStuff->type = TYPE_INTEGER;
  87. SeedStuff->required = NO;
  88. SeedStuff->description =
  89. _("Random seed (SEED_MIN >= value >= SEED_MAX), default [random]");
  90. range_high_stuff = G_define_option();
  91. range_high_stuff->key = "high";
  92. range_high_stuff->type = TYPE_INTEGER;
  93. range_high_stuff->required = NO;
  94. range_high_stuff->description = _("Maximum cell value of distribution");
  95. range_high_stuff->answer = "255";
  96. Uniform = G_define_flag();
  97. Uniform->key = 'u';
  98. Uniform->description = _("Uniformly distributed cell values");
  99. if (G_parser(argc, argv))
  100. exit(EXIT_FAILURE);
  101. Init();
  102. if (Uniform->answer)
  103. GenNorm();
  104. CalcSD();
  105. for (DoMap = 0; DoMap < NumMaps; DoMap++) {
  106. OutFD = Rast_open_c_new(OutNames[DoMap]);
  107. G_message(_("Generating raster map <%s>..."), OutNames[DoMap]);
  108. if (Seeds[DoMap] == SEED_MIN - 1)
  109. Seeds[DoMap] = (int)(ran1() * SEED_MAX);
  110. MapSeed = Seed = Seeds[DoMap];
  111. ZeroMapCells();
  112. for (DoFilter = 0; DoFilter < NumFilters; DoFilter++) {
  113. CopyFilter(&Filter, AllFilters[DoFilter]);
  114. G_debug(1,
  115. "Starting filter #%d, distance: %.*lf, exponent: %.*lf, flat: %.*lf",
  116. DoFilter, Digits(2.0 * Filter.MaxDist, 6),
  117. 2.0 * Filter.MaxDist, Digits(1.0 / Filter.Exp, 6),
  118. 1.0 / Filter.Exp, Digits(Filter.Mult, 6), Filter.Mult);
  119. MakeBigF();
  120. CalcSurface();
  121. }
  122. SaveMap(DoMap, MapSeed);
  123. }
  124. G_done_msg(" ");
  125. exit(EXIT_SUCCESS);
  126. }