init.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* init.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <grass/gis.h>
  8. #include <grass/raster.h>
  9. #include <grass/glocale.h>
  10. #include "ransurf.h"
  11. #include "local_proto.h"
  12. void Init(void)
  13. {
  14. struct Cell_head Region;
  15. int row, col, i, j, NumWeight, NumDist, NumExp;
  16. char *Name, *Number, String[80];
  17. char msg[128], msg2[64];
  18. double MinRes;
  19. G_debug(2, "Init");
  20. Rs = Rast_window_rows();
  21. Cs = Rast_window_cols();
  22. Surface = (double **)G_malloc(Rs * sizeof(double *));
  23. for (row = 0; row < Rs; row++)
  24. Surface[row] = (double *)G_malloc(Cs * sizeof(double));
  25. G_get_set_window(&Region);
  26. EW = Region.ew_res;
  27. NS = Region.ns_res;
  28. if (EW < NS)
  29. MinRes = EW;
  30. else
  31. MinRes = NS;
  32. if (NULL == G_find_file("cell", "MASK", G_mapset())) {
  33. MapCount = Rs * Cs;
  34. FDM = -1;
  35. }
  36. else {
  37. FDM = Rast_open_old("MASK", G_mapset());
  38. {
  39. MapCount = 0;
  40. CellBuffer = Rast_allocate_c_buf();
  41. for (row = 0; row < Rs; row++) {
  42. Rast_get_c_row_nomask(FDM, CellBuffer, row);
  43. for (col = 0; col < Cs; col++) {
  44. if (CellBuffer[col])
  45. MapCount++;
  46. }
  47. }
  48. }
  49. }
  50. if (Uniform->answer)
  51. sprintf(Buf, "Uni. R. S.");
  52. else
  53. sprintf(Buf, "Dist. R. S.");
  54. if (!range_high_stuff->answer)
  55. High = 255;
  56. else {
  57. High = atoi(range_high_stuff->answer);
  58. sprintf(String, " high=%d", High);
  59. strcat(Buf, String);
  60. }
  61. if (1 >= High)
  62. G_fatal_error(_("High (%d) must be greater than 1"), High);
  63. CatInfo.NumCat = High;
  64. NumMaps = 0;
  65. OutNames = (char **)G_malloc(sizeof(char *));
  66. for (i = 0; (Name = Output->answers[i]); i++) {
  67. for (j = i - 1; j >= 0; j--) {
  68. if (strcmp(OutNames[j], Name) == 0)
  69. G_fatal_error
  70. (_("Rastar map <%s> repeated, maps must be unique"),
  71. Name);
  72. }
  73. OutNames = (char **)G_realloc(OutNames, (i + 1) * sizeof(char *));
  74. OutNames[i] = (char *)G_malloc(sizeof(char) * (strlen(Name) + 1));
  75. strcpy(OutNames[i], Name);
  76. NumMaps++;
  77. }
  78. if (NumMaps == 0)
  79. G_fatal_error(_("Output raster map required"));
  80. Theory = 0;
  81. NumSeeds = 0;
  82. Seeds = (int *)G_malloc(NumMaps * sizeof(int));
  83. if (!SeedStuff->answers) {
  84. for (i = 0; i < NumMaps; i++) {
  85. Seeds[i] = SEED_MIN - 1;
  86. Seed = (int)getpid();
  87. }
  88. }
  89. else {
  90. for (i = 0; (Number = SeedStuff->answers[i]) && i < NumMaps; i++) {
  91. sscanf(Number, "%d", &(Seeds[i]));
  92. if (Seeds[i] > SEED_MAX) {
  93. sprintf(msg, _("Seed (%d) larger than maximum (%d)"),
  94. Seeds[i], SEED_MAX);
  95. Seeds[i] = Seeds[i] % SEED_MAX;
  96. sprintf(msg2, _(" seed is set to %d"), Seeds[i]);
  97. strcat(msg, msg2);
  98. G_warning("%s", msg);
  99. }
  100. else if (Seeds[i] < SEED_MIN) {
  101. sprintf(msg, _("Seed (%d) smaller than minimum (%d)"),
  102. Seeds[i], SEED_MIN);
  103. while (Seeds[i] < SEED_MIN)
  104. Seeds[i] += SEED_MAX - SEED_MIN;
  105. sprintf(msg2, _(" seed is set to %d"), Seeds[i]);
  106. strcat(msg, msg2);
  107. G_warning("%s", msg);
  108. }
  109. } /* /for */
  110. } /* /else */
  111. CellBuffer = Rast_allocate_c_buf();
  112. CatInfo.NumValue = (int *)G_malloc(CatInfo.NumCat * sizeof(int));
  113. CatInfo.Average = (double *)G_malloc(CatInfo.NumCat * sizeof(double));
  114. CatInfo.Min = (double *)G_malloc(CatInfo.NumCat * sizeof(double));
  115. CatInfo.Max = (double *)G_malloc(CatInfo.NumCat * sizeof(double));
  116. NumFilters = 1;
  117. AllFilters = (FILTER *) G_malloc(sizeof(FILTER));
  118. /*
  119. if( ! Distance->answers) {
  120. NumDist = 1;
  121. AllFilters[ 0].MaxDist = MinRes / 2.0;
  122. } else {
  123. NumDist = 0;
  124. for (i = 0; Number = Distance->answers[i]; i++) {
  125. if(NumDist == NumFilters) {
  126. AllFilters = (FILTER *) G_realloc( AllFilters,
  127. ++NumFilters * sizeof( FILTER));
  128. }
  129. sscanf( Number, "%lf", &(AllFilters[NumDist].MaxDist));
  130. if (AllFilters[NumDist].MaxDist < 0.0)
  131. G_fatal_error("%s: distance value[%d]: [%lf] must be >= 0.0",
  132. G_program_name(), NumDist,
  133. AllFilters[NumDist].MaxDist);
  134. NumDist++;
  135. }
  136. }
  137. */
  138. NumDist = 0;
  139. if (Distance->answer) {
  140. sscanf(Distance->answer, "%lf", &(AllFilters[NumDist].MaxDist));
  141. if (AllFilters[NumDist].MaxDist < 0.0)
  142. G_fatal_error(_("Distance value (%d): %lf must be >= 0.0"),
  143. NumDist, AllFilters[NumDist].MaxDist);
  144. NumDist++;
  145. }
  146. /*
  147. NumExp = 0;
  148. if( Exponent->answers) {
  149. for (i = 0; Number = Exponent->answers[i]; i++) {
  150. if(NumExp == NumFilters)
  151. AllFilters = (FILTER *) G_realloc(
  152. AllFilters, ++NumFilters * sizeof( FILTER));
  153. sscanf( Number, "%lf", &(AllFilters[NumExp].Exp));
  154. DOUBLE(AllFilters[NumExp].Exp);
  155. if (AllFilters[NumExp].Exp <= 0.0)
  156. G_fatal_error("%s: exponent value [%lf] must be > 0.0",
  157. G_program_name(), AllFilters[NumExp].Exp);
  158. AllFilters[ NumExp].Exp = 1.0 / AllFilters[ NumExp].Exp;
  159. NumExp++;
  160. }
  161. }
  162. INT(NumExp);
  163. */
  164. NumExp = 0;
  165. if (Exponent->answer) {
  166. sscanf(Exponent->answer, "%lf", &(AllFilters[NumExp].Exp));
  167. if (AllFilters[NumExp].Exp <= 0.0)
  168. G_fatal_error(_("Exponent value (%lf) must be > 0.0"),
  169. AllFilters[NumExp].Exp);
  170. NumExp++;
  171. }
  172. NumWeight = 0;
  173. /*
  174. if( Weight->answers) {
  175. for (i = 0; Number = Weight->answers[i]; i++) {
  176. if(NumWeight == NumFilters)
  177. AllFilters = (FILTER *) G_realloc(
  178. AllFilters, ++NumFilters * sizeof( FILTER));
  179. sscanf( Numbers, "%lf", &(AllFilters[NumWeight].Mult));
  180. if (AllFilters[NumWeight].Mult == 0.0)
  181. G_fatal_error("%s: weight value [%lf] must not be 0.0",
  182. G_program_name(), AllFilters[NumWeight].Mult);
  183. NumWeight++;
  184. }
  185. }
  186. */
  187. if (Weight->answer) {
  188. sscanf(Weight->answer, "%lf", &(AllFilters[NumWeight].Mult));
  189. if (AllFilters[NumWeight].Mult > AllFilters[NumWeight].MaxDist)
  190. G_fatal_error(_("Flat value (%lf) must be less than distance value (%lf)"),
  191. AllFilters[NumWeight].Mult,
  192. AllFilters[NumWeight].MaxDist);
  193. NumWeight++;
  194. }
  195. if (NumDist > 0) {
  196. sprintf(String, " dist=");
  197. strcat(Buf, String);
  198. for (i = 0; i < NumDist - 1; i++) {
  199. sprintf(String, "%.*lf,",
  200. Digits(AllFilters[i].MaxDist, 6), AllFilters[i].MaxDist);
  201. strcat(Buf, String);
  202. }
  203. sprintf(String, "%.*lf",
  204. Digits(AllFilters[i].MaxDist, 6), AllFilters[i].MaxDist);
  205. strcat(Buf, String);
  206. }
  207. if (NumDist > 1 && NumDist < NumFilters)
  208. G_fatal_error(_("Must have a distance value for each filter"));
  209. if (NumDist == 0) {
  210. AllFilters[0].MaxDist = MinRes / 4.0;
  211. }
  212. if (NumDist < NumFilters) {
  213. for (NumDist = 1; NumDist < NumFilters; NumDist++)
  214. AllFilters[NumDist].MaxDist = AllFilters[0].MaxDist;
  215. }
  216. for (NumDist = 0; NumDist < NumFilters; NumDist++) {
  217. if (AllFilters[NumDist].MaxDist < MinRes)
  218. AllFilters[NumDist].MaxDist = MinRes * 0.5;
  219. else
  220. AllFilters[NumDist].MaxDist *= 0.5;
  221. }
  222. if (NumExp > 0) {
  223. sprintf(String, " exp=");
  224. strcat(Buf, String);
  225. for (i = 0; i < NumExp - 1; i++) {
  226. sprintf(String, "%.*lf,",
  227. Digits(AllFilters[i].Exp, 6), AllFilters[i].Exp);
  228. strcat(Buf, String);
  229. }
  230. sprintf(String, "%.*lf",
  231. Digits(AllFilters[i].Exp, 6), AllFilters[i].Exp);
  232. strcat(Buf, String);
  233. }
  234. if (NumExp > 1 && NumExp < NumFilters)
  235. G_fatal_error(_("Must have a exponent value for each filter"));
  236. if (NumWeight > 0) {
  237. sprintf(String, " flat=");
  238. strcat(Buf, String);
  239. for (i = 0; i < NumWeight - 1; i++) {
  240. sprintf(String, "%.*lf,",
  241. Digits(AllFilters[i].Mult, 6), AllFilters[i].Mult);
  242. strcat(Buf, String);
  243. G_debug(3, "(AllFilters[i].Mult):%.12lf", AllFilters[i].Mult);
  244. }
  245. sprintf(String, "%.*lf",
  246. Digits(AllFilters[i].Mult, 6), AllFilters[i].Mult);
  247. strcat(Buf, String);
  248. }
  249. if (NumWeight > 1 && NumWeight < NumFilters)
  250. G_fatal_error(_("Must have a weight value for each filter"));
  251. if (NumExp == 1) {
  252. for (NumExp = 1; NumExp < NumFilters; NumExp++)
  253. AllFilters[NumExp].Exp = AllFilters[0].Exp;
  254. }
  255. if (NumExp == 0) {
  256. for (NumExp = 0; NumExp < NumFilters; NumExp++)
  257. AllFilters[NumExp].Exp = 1.0;
  258. }
  259. if (NumWeight == 0) {
  260. for (NumWeight = 0; NumWeight < NumFilters; NumWeight++)
  261. AllFilters[NumWeight].Mult = 0.0;
  262. }
  263. AllMaxDist = 0.0;
  264. for (i = 0; i < NumFilters; i++) {
  265. if (AllMaxDist < AllFilters[i].MaxDist)
  266. AllMaxDist = AllFilters[i].MaxDist;
  267. AllFilters[i].MaxSq = AllFilters[i].MaxDist * AllFilters[i].MaxDist;
  268. G_debug(3, "(i):%d", i);
  269. G_debug(3, "(AllFilters[i].Mult):%.12lf", AllFilters[i].Mult);
  270. G_debug(3, "(AllFilters[i].MaxDist):%.12lf", AllFilters[i].MaxDist);
  271. G_debug(3, "(AllFilters[i].MaxSq):%.12lf", AllFilters[i].MaxSq);
  272. G_debug(3, "(AllFilters[i].Exp):%.12lf", AllFilters[i].Exp);
  273. }
  274. BigF.RowPlus = AllMaxDist / NS;
  275. BigF.ColPlus = AllMaxDist / EW;
  276. BigF.NumR = BigF.RowPlus * 2 + 1;
  277. BigF.NumC = BigF.ColPlus * 2 + 1;
  278. BigF.LowBF = (int *)G_malloc(BigF.NumR * sizeof(int));
  279. BigF.HihBF = (int *)G_malloc(BigF.NumR * sizeof(int));
  280. BigF.F = (double **)G_malloc(BigF.NumR * sizeof(double *));
  281. for (i = 0; i < BigF.NumR; i++)
  282. BigF.F[i] = (double *)G_malloc(BigF.NumC * sizeof(double));
  283. AllMaxDist *= 2.0;
  284. }