driver.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  1. /*
  2. ************************************************************
  3. * MODULE: r.le.pixel/driver.c *
  4. * Version 5.0 Nov. 1, 2001 *
  5. * *
  6. * AUTHOR: W.L. Baker, University of Wyoming *
  7. * BAKERWL@UWYO.EDU *
  8. * *
  9. * PURPOSE: To analyze pixel-scale landscape properties *
  10. * driver.c opens the input and output files and *
  11. * then calls the moving window, unit, and whole *
  12. * map drivers *
  13. * *
  14. * COPYRIGHT: (C) 2001 by W.L. Baker *
  15. * *
  16. * This program is free software under the GNU General *
  17. * Public License(>=v2). Read the file COPYING that comes *
  18. * with GRASS for details *
  19. * *
  20. ************************************************************/
  21. #include <grass/gis.h>
  22. #include "pixel.h"
  23. #include <grass/config.h>
  24. #include <grass/raster.h>
  25. int finput, g_scale = 1, g_unit = 1;
  26. extern struct CHOICE *choice;
  27. char cmdbuf[100];
  28. RASTER_MAP_TYPE data_type;
  29. /*
  30. Variables:
  31. GLOBAL:
  32. finput = the raster map to be analyzed
  33. g_scale = the number of sampling scales
  34. g_unit = the number of sampling units
  35. data_type = the type of raster map: integer, floating point, or double
  36. */
  37. /* RUN R.LE.PIXEL IN FOREGROUND */
  38. void texture_fore()
  39. {
  40. FILE *fp0, *fp1, *fp2, *fp3, *fp4, *fp5;
  41. fprintf(stdout, "\nR.LE.PIXEL IS WORKING....;\n\n");
  42. /* check for input raster map */
  43. if (0 > (finput = Rast_open_old(choice->fn, G_mapset()))) {
  44. fprintf(stdout, "\n");
  45. fprintf(stdout,
  46. " ********************************************************\n");
  47. fprintf(stdout,
  48. " The raster map you specified with the 'map=' parameter \n");
  49. fprintf(stdout,
  50. " was not found in your mapset. \n");
  51. fprintf(stdout,
  52. " ********************************************************\n");
  53. exit(1);
  54. }
  55. /* determine whether the raster map is integer
  56. (CELL_TYPE),floating point (FCELL_TYPE), or
  57. double (DCELL_TYPE) and make globally available */
  58. else
  59. data_type = Rast_map_type(choice->fn, G_mapset());
  60. /* if using a moving window, get the parameters,
  61. and start the moving window driver */
  62. if (choice->wrum == 'm')
  63. mv_driver();
  64. /* if using the whole raster map as the sampling
  65. area or sampling with units or regions, open
  66. the output files */
  67. else {
  68. if (choice->att[0]) {
  69. fp0 = fopen0("r.le.out/b1-4.out", "w");
  70. fprintf(fp0,
  71. " MEAN ST. DEV. MINIMUM MAXIMUM\n");
  72. fprintf(fp0,
  73. "Scale Unit PIXEL ATT. PIXEL ATT. PIXEL ATT. PIXEL ATT.\n");
  74. fclose(fp0);
  75. }
  76. if (choice->div[0]) {
  77. fp1 = fopen0("r.le.out/d1-4.out", "w");
  78. fprintf(fp1,
  79. " INVERSE\n");
  80. fprintf(fp1,
  81. "Scale Unit RICHNESS SHANNON DOMINANCE SIMPSON\n");
  82. fclose(fp1);
  83. }
  84. if (choice->te2[0]) {
  85. fp2 = fopen0("r.le.out/t1-5.out", "w");
  86. fprintf(fp2,
  87. " ANGULAR INVERSE\n");
  88. fprintf(fp2,
  89. "Scale Unit CONTAGION SEC. MOM. DIFF. MOM. ENTROPY CONTRAST\n");
  90. fclose(fp2);
  91. }
  92. if (choice->jux[0]) {
  93. fp3 = fopen0("r.le.out/j1-2.out", "w");
  94. fprintf(fp3, " MEAN ST. DEV..\n");
  95. fprintf(fp3, "Scale Unit JUXTAPOS. JUXTAPOS.\n");
  96. fclose(fp3);
  97. }
  98. if (choice->edg[0]) {
  99. if (choice->edg[1]) {
  100. fp4 = fopen0("r.le.out/e1.out", "w");
  101. fprintf(fp4, " SUM\n");
  102. fprintf(fp4, "Scale Unit OF EDGES\n");
  103. fclose(fp4);
  104. }
  105. if (choice->edg[2]) {
  106. fp5 = fopen0("r.le.out/e2.out", "w");
  107. fprintf(fp5, " SUM\n");
  108. fprintf(fp5, "Scale Unit OF EDGES\n");
  109. fclose(fp5);
  110. }
  111. }
  112. if (choice->wrum == 'w' || choice->wrum == 'r')
  113. whole_reg_driver();
  114. else if (choice->wrum == 'u')
  115. unit_driver();
  116. }
  117. Rast_close(finput);
  118. fputs("R.LE.PIXEL IS DONE; ", stderr);
  119. if (choice->wrum != 'm')
  120. fputs("OUTPUT FILES IN SUBDIRECTORY \"r.le.out\"\n", stderr);
  121. return;
  122. }
  123. /* MOVING WINDOW DRIVER */
  124. void mv_driver()
  125. {
  126. register int i, j;
  127. int nr, nc, u_w, u_l, x0, y0, d, fmask, m, p, cntwhole = 0, b, *row_buf;
  128. char *nul_buf, *nulltmp;
  129. int *tmp;
  130. float *ftmp;
  131. double *dtmp;
  132. double *tmp_buf, *tmp_buf2, **buff = NULL, *richwhole;
  133. int b1, b2, b3, b4, d1, d2, d3, d4, t1, t2, t3, t4, t5, j1, j2, e1, e2;
  134. long finished_time;
  135. float radius;
  136. struct Cell_head wind;
  137. /* variables:
  138. nc = #cols. in search area minus (1/2 width of mov. wind. + 1) =
  139. number of cols with values in out map
  140. nr = #rows in search area minus (1/2 height of mov. wind. + 1) =
  141. number of rows with values in out map
  142. x0 = starting column for upper L corner of search area
  143. y0 = starting row for upper L corner of search area
  144. u_w = width of mov. wind. in cells
  145. u_l = width of mov. wind. in cells
  146. x0 = starting column for upper L corner of mov. wind.
  147. y0 = starting row for upper L corner of mov. wind.
  148. row = row for moving-window center
  149. col = column for moving-window center
  150. *row_buf = temporary array that holds one row of the
  151. MASK if there is one
  152. *tmp_buf = temporary array that holds one moving wind.
  153. measure for a single row
  154. **buff = temporary array that holds the set of chosen
  155. measures for a row
  156. radius = radius of the sampling unit, if circles are used
  157. */
  158. /* open the appropriate output
  159. maps */
  160. if (choice->att[1]) {
  161. if (G_find_raster("b1", G_mapset()) != NULL) {
  162. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=b1,b1bak");
  163. system(cmdbuf);
  164. }
  165. b1 = Rast_open_new("b1", DCELL_TYPE);
  166. }
  167. if (choice->att[2]) {
  168. if (G_find_raster("b2", G_mapset()) != NULL) {
  169. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=b2,b2bak");
  170. system(cmdbuf);
  171. }
  172. b2 = Rast_open_new("b2", DCELL_TYPE);
  173. }
  174. if (choice->att[3]) {
  175. if (G_find_raster("b3", G_mapset()) != NULL) {
  176. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=b3,b3bak");
  177. system(cmdbuf);
  178. }
  179. b3 = Rast_open_new("b3", DCELL_TYPE);
  180. }
  181. if (choice->att[4]) {
  182. if (G_find_raster("b4", G_mapset()) != NULL) {
  183. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=b4,b4bak");
  184. system(cmdbuf);
  185. }
  186. b4 = Rast_open_new("b4", DCELL_TYPE);
  187. }
  188. if (choice->div[1]) {
  189. if (G_find_raster("d1", G_mapset()) != NULL) {
  190. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=d1,d1bak");
  191. system(cmdbuf);
  192. }
  193. d1 = Rast_open_new("d1", DCELL_TYPE);
  194. }
  195. if (choice->div[2]) {
  196. if (G_find_raster("d2", G_mapset()) != NULL) {
  197. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=d2,d2bak");
  198. system(cmdbuf);
  199. }
  200. d2 = Rast_open_new("d2", DCELL_TYPE);
  201. }
  202. if (choice->div[3]) {
  203. if (G_find_raster("d3", G_mapset()) != NULL) {
  204. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=d3,d3bak");
  205. system(cmdbuf);
  206. }
  207. d3 = Rast_open_new("d3", DCELL_TYPE);
  208. }
  209. if (choice->div[4]) {
  210. if (G_find_raster("d4", G_mapset()) != NULL) {
  211. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=d4,d4bak");
  212. system(cmdbuf);
  213. }
  214. d4 = Rast_open_new("d4", DCELL_TYPE);
  215. }
  216. if (choice->te2[1]) {
  217. if (G_find_raster("t1", G_mapset()) != NULL) {
  218. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=t1,t1bak");
  219. system(cmdbuf);
  220. }
  221. t1 = Rast_open_new("t1", DCELL_TYPE);
  222. }
  223. if (choice->te2[2]) {
  224. if (G_find_raster("t2", G_mapset()) != NULL) {
  225. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=t2,t2bak");
  226. system(cmdbuf);
  227. }
  228. t2 = Rast_open_new("t2", DCELL_TYPE);
  229. }
  230. if (choice->te2[3]) {
  231. if (G_find_raster("t3", G_mapset()) != NULL) {
  232. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=t3,t3bak");
  233. system(cmdbuf);
  234. }
  235. t3 = Rast_open_new("t3", DCELL_TYPE);
  236. }
  237. if (choice->te2[4]) {
  238. if (G_find_raster("t4", G_mapset()) != NULL) {
  239. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=t4,t4bak");
  240. system(cmdbuf);
  241. }
  242. t4 = Rast_open_new("t4", DCELL_TYPE);
  243. }
  244. if (choice->te2[5]) {
  245. if (G_find_raster("t5", G_mapset()) != NULL) {
  246. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=t5,t5bak");
  247. system(cmdbuf);
  248. }
  249. t5 = Rast_open_new("t5", DCELL_TYPE);
  250. }
  251. if (choice->jux[1]) {
  252. if (G_find_raster("j1", G_mapset()) != NULL) {
  253. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=j1,j1bak");
  254. system(cmdbuf);
  255. }
  256. j1 = Rast_open_new("j1", DCELL_TYPE);
  257. }
  258. if (choice->jux[2]) {
  259. if (G_find_raster("j2", G_mapset()) != NULL) {
  260. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=j2,j2bak");
  261. system(cmdbuf);
  262. }
  263. j2 = Rast_open_new("j2", DCELL_TYPE);
  264. }
  265. if (choice->edg[1]) {
  266. if (G_find_raster("e1", G_mapset()) != NULL) {
  267. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=e1,e1bak");
  268. system(cmdbuf);
  269. }
  270. e1 = Rast_open_new("e1", DCELL_TYPE);
  271. }
  272. if (choice->edg[2]) {
  273. if (G_find_raster("e2", G_mapset()) != NULL) {
  274. sprintf(cmdbuf, "%s %s", "g.rename -o", "rast=e2,e2bak");
  275. system(cmdbuf);
  276. }
  277. e2 = Rast_open_new("e2", DCELL_TYPE);
  278. }
  279. /* get the moving window parameters */
  280. read_mwind(&u_w, &u_l, &nc, &nr, &x0, &y0, &radius);
  281. /* check for an unacceptable
  282. moving-window size */
  283. if (nc < 1 || nr < 1) {
  284. fprintf(stdout, "\n");
  285. fprintf(stdout,
  286. " *******************************************************\n");
  287. fprintf(stdout,
  288. " The moving window size specified in file r.le.para/ \n");
  289. fprintf(stdout,
  290. " move_wind is less than 1 row or column. Check this \n");
  291. fprintf(stdout,
  292. " file or redefine the moving window using r.le.setup. \n");
  293. fprintf(stdout,
  294. " *******************************************************\n");
  295. exit(1);
  296. }
  297. /* check for an unacceptable
  298. search area */
  299. G_get_set_window(&wind);
  300. if (wind.rows < nr + y0 || wind.cols < nc + x0) {
  301. fprintf(stdout, "\n");
  302. fprintf(stdout,
  303. " *******************************************************\n");
  304. fprintf(stdout,
  305. " Moving window search area in file r.le.para/move_wind \n");
  306. fprintf(stdout,
  307. " does not match the dimensions of the current region. \n");
  308. fprintf(stdout,
  309. " You must either rerun r.le.setup to make a new \n");
  310. fprintf(stdout,
  311. " r.le.para/move_wind file or reset the region to match \n");
  312. fprintf(stdout,
  313. " the r.le.para/move_wind file \n");
  314. fprintf(stdout,
  315. " *******************************************************\n");
  316. exit(1);
  317. }
  318. /* begin main moving window loop
  319. section */
  320. /* set the d parameter for the
  321. performance meter */
  322. if (nr * nc > 10000)
  323. d = nr * nc / 1000;
  324. else if (nr * nc > 2500)
  325. d = nr * nc / 100;
  326. else
  327. d = 10;
  328. /* return a value > 0 to fmask if
  329. there is a MASK present */
  330. fprintf(stdout,
  331. "If a MASK is not present (see r.mask) a beep may sound and a\n");
  332. fprintf(stdout,
  333. " warning may be printed or appear in a window; ignore this warning.\n");
  334. fprintf(stdout, "If a MASK is present there will be no warning.\n");
  335. fmask = Rast_open_old("MASK", G_mapset());
  336. fprintf(stdout, "\n");
  337. /* allocate memory for the buffer */
  338. buff = (double **)G_calloc(nc + 1, sizeof(double *));
  339. /* allocate memory for each of 17 measures */
  340. for (p = 0; p < nc + 1; p++)
  341. buff[p] = (double *)G_calloc(17, sizeof(double));
  342. /* allocate memory for a row buffer if
  343. there is a mask */
  344. if (fmask > 0)
  345. row_buf = Rast_allocate_buf(CELL_TYPE);
  346. if (choice->edg[2] || choice->jux[0]) {
  347. /* dynamically allocate memory for
  348. the richness array */
  349. richwhole = (double *)G_calloc(MAX, sizeof(double));
  350. /* initialize the richness array
  351. elements with the value -999 */
  352. for (i = 0; i < MAX; i++)
  353. richwhole[i] = -999.0;
  354. /* allocate memory to store a row of the
  355. raster map, depending on the type of
  356. input raster map; keep track of the
  357. name of the buffer for each raster type */
  358. switch (data_type) {
  359. case CELL_TYPE:
  360. tmp = Rast_allocate_buf(CELL_TYPE);
  361. break;
  362. case FCELL_TYPE:
  363. ftmp = Rast_allocate_buf(FCELL_TYPE);
  364. break;
  365. case DCELL_TYPE:
  366. dtmp = Rast_allocate_buf(DCELL_TYPE);
  367. break;
  368. }
  369. nul_buf = Rast_allocate_null_buf();
  370. /* go through the search area
  371. pixel by pixel */
  372. for (i = 0; i < nr; i++) {
  373. switch (data_type) {
  374. case (CELL_TYPE):
  375. Rast_zero_buf(tmp, CELL_TYPE);
  376. Rast_get_row(finput, tmp, i, CELL_TYPE);
  377. break;
  378. case (FCELL_TYPE):
  379. Rast_zero_buf(ftmp, FCELL_TYPE);
  380. Rast_get_row(finput, ftmp, i, FCELL_TYPE);
  381. break;
  382. case (DCELL_TYPE):
  383. Rast_zero_buf(dtmp, DCELL_TYPE);
  384. Rast_get_row(finput, dtmp, i, DCELL_TYPE);
  385. break;
  386. }
  387. Rast_get_null_value_row(finput, nul_buf, i);
  388. for (j = 0; j < nc; j++) {
  389. /* if the raster value is not null,
  390. call get_rich_whole to tally up the
  391. number of different attributes
  392. in the search area and fill
  393. the richness array with those
  394. attributes */
  395. switch (data_type) {
  396. case (CELL_TYPE):
  397. if ((*(tmp + j) ||
  398. *(tmp + j) == 0) && *(nul_buf + j) == 0.0)
  399. get_rich_whole((double)*(tmp + j), richwhole,
  400. &cntwhole);
  401. break;
  402. case (FCELL_TYPE):
  403. if ((*(ftmp + j) ||
  404. *(ftmp + j) == 0) && *(nul_buf + j) == 0.0)
  405. get_rich_whole((double)*(ftmp + j), richwhole,
  406. &cntwhole);
  407. break;
  408. case (DCELL_TYPE):
  409. if ((*(dtmp + j) ||
  410. *(dtmp + j) == 0) && *(nul_buf + j) == 0.0)
  411. get_rich_whole(*(dtmp + j), richwhole, &cntwhole);
  412. break;
  413. }
  414. }
  415. }
  416. switch (data_type) {
  417. case (CELL_TYPE):
  418. G_free(tmp);
  419. break;
  420. case (FCELL_TYPE):
  421. G_free(ftmp);
  422. break;
  423. case (DCELL_TYPE):
  424. G_free(dtmp);
  425. break;
  426. }
  427. G_free(nul_buf);
  428. G_free(richwhole);
  429. }
  430. /* main loop for clipping & measuring
  431. using the moving-window; index i
  432. refers to which moving window, not
  433. the row of the original map */
  434. for (i = 0; i < nr; i++) {
  435. /* zero the buffer before filling it
  436. again */
  437. for (m = 0; m < nc + 1; m++) {
  438. for (p = 0; p < 17; p++)
  439. *(*(buff + m) + p) = 0.0;
  440. }
  441. /* if there is a MASK, then read in
  442. a row of MASK - this part skips
  443. cells with the value "0" in the
  444. MASK to speed up the moving window
  445. process */
  446. if (fmask > 0) {
  447. Rast_zero_buf(row_buf, CELL_TYPE);
  448. Rast_get_row_nomask(fmask, row_buf, y0 + i + u_l / 2,
  449. CELL_TYPE);
  450. /* for each cell whose value is "1"
  451. in MASK */
  452. for (j = 0; j < nc; j++) {
  453. /* display #cells left to do */
  454. if (i == 0 && j == 0)
  455. fprintf(stdout, "TOTAL WINDOWS = %8d\n", nr * nc);
  456. meter2(nr * nc, (i * nc + (j + 1)), d);
  457. /* call the cell clip driver */
  458. if (row_buf[x0 + j + u_w / 2])
  459. cell_clip_drv(x0 + j, y0 + i, u_w, u_l, buff, j, cntwhole,
  460. radius);
  461. }
  462. }
  463. /* if there is no MASK, then clip
  464. and measure at every cell */
  465. else {
  466. for (j = 0; j < nc; j++) {
  467. /* display #cells left to do */
  468. if (i == 0 && j == 0)
  469. fprintf(stdout, "TOTAL WINDOWS = %8d\n", nr * nc);
  470. meter2(nr * nc, (i * nc + (j + 1)), d);
  471. /* call the cell clip driver. This routine will clip
  472. the rectangle at x0 + j, y0 + i and u_w X u_l wide
  473. (or in a circle with radius), and put the results
  474. for each chosen moving windown measure in buff;
  475. note that the center of the moving window is not
  476. at x0 + j, y0 + i, but at x0 + j + u_w/2, y0 + i +
  477. u_l/2 */
  478. cell_clip_drv(x0 + j, y0 + i, u_w, u_l, buff, j, cntwhole,
  479. radius);
  480. }
  481. }
  482. /* copy the chosen measures into a temporary row
  483. buffer which is then fed into the chosen output
  484. maps; the map location is adjusted to the center
  485. of the moving window */
  486. tmp_buf = Rast_allocate_buf(DCELL_TYPE);
  487. nulltmp = Rast_allocate_null_buf();
  488. if (choice->att[1]) {
  489. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  490. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  491. if (i == 0) {
  492. for (b = 0; b < u_l / 2; b++)
  493. Rast_put_d_row(b1, tmp_buf);
  494. }
  495. if (i < nr) {
  496. for (m = 0; m < nc; m++) {
  497. if (*(*(buff + m) + 0) > -BIG)
  498. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 0);
  499. }
  500. Rast_put_d_row(b1, tmp_buf);
  501. }
  502. if (i == nr - 1) {
  503. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  504. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  505. for (b = 0; b < u_l / 2; b++)
  506. Rast_put_d_row(b1, tmp_buf2);
  507. G_free(tmp_buf2);
  508. }
  509. }
  510. if (choice->att[2]) {
  511. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  512. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  513. if (i == 0) {
  514. for (b = 0; b < u_l / 2; b++)
  515. Rast_put_d_row(b2, tmp_buf);
  516. }
  517. if (i < nr) {
  518. for (m = 0; m < nc; m++) {
  519. if (*(*(buff + m) + 1) > -BIG)
  520. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 1);
  521. }
  522. Rast_put_d_row(b2, tmp_buf);
  523. }
  524. if (i == nr - 1) {
  525. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  526. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  527. for (b = 0; b < u_l / 2; b++)
  528. Rast_put_d_row(b2, tmp_buf2);
  529. G_free(tmp_buf2);
  530. }
  531. }
  532. if (choice->att[3]) {
  533. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  534. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  535. if (i == 0) {
  536. for (b = 0; b < u_l / 2; b++)
  537. Rast_put_d_row(b3, tmp_buf);
  538. }
  539. if (i < nr) {
  540. for (m = 0; m < nc; m++) {
  541. if (*(*(buff + m) + 2) > -BIG)
  542. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 2);
  543. }
  544. Rast_put_d_row(b3, tmp_buf);
  545. }
  546. if (i == nr - 1) {
  547. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  548. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  549. for (b = 0; b < u_l / 2; b++)
  550. Rast_put_d_row(b3, tmp_buf2);
  551. G_free(tmp_buf2);
  552. }
  553. }
  554. if (choice->att[4]) {
  555. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  556. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  557. if (i == 0) {
  558. for (b = 0; b < u_l / 2; b++)
  559. Rast_put_d_row(b4, tmp_buf);
  560. }
  561. if (i < nr) {
  562. for (m = 0; m < nc; m++) {
  563. if (*(*(buff + m) + 3) > -BIG)
  564. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 3);
  565. }
  566. Rast_put_d_row(b4, tmp_buf);
  567. }
  568. if (i == nr - 1) {
  569. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  570. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  571. for (b = 0; b < u_l / 2; b++)
  572. Rast_put_d_row(b4, tmp_buf2);
  573. G_free(tmp_buf2);
  574. }
  575. }
  576. if (choice->div[1]) {
  577. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  578. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  579. if (i == 0) {
  580. for (b = 0; b < u_l / 2; b++)
  581. Rast_put_d_row(d1, tmp_buf);
  582. }
  583. if (i < nr) {
  584. for (m = 0; m < nc; m++) {
  585. if (*(*(buff + m) + 4) > -BIG)
  586. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 4);
  587. }
  588. Rast_put_d_row(d1, tmp_buf);
  589. }
  590. if (i == nr - 1) {
  591. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  592. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  593. for (b = 0; b < u_l / 2; b++)
  594. Rast_put_d_row(d1, tmp_buf2);
  595. G_free(tmp_buf2);
  596. }
  597. }
  598. if (choice->div[2]) {
  599. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  600. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  601. if (i == 0) {
  602. for (b = 0; b < u_l / 2; b++)
  603. Rast_put_d_row(d2, tmp_buf);
  604. }
  605. if (i < nr) {
  606. for (m = 0; m < nc; m++) {
  607. if (*(*(buff + m) + 5) > -BIG)
  608. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 5);
  609. }
  610. Rast_put_d_row(d2, tmp_buf);
  611. }
  612. if (i == nr - 1) {
  613. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  614. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  615. for (b = 0; b < u_l / 2; b++)
  616. Rast_put_d_row(d2, tmp_buf2);
  617. G_free(tmp_buf2);
  618. }
  619. }
  620. if (choice->div[3]) {
  621. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  622. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  623. if (i == 0) {
  624. for (b = 0; b < u_l / 2; b++)
  625. Rast_put_d_row(d3, tmp_buf);
  626. }
  627. if (i < nr) {
  628. for (m = 0; m < nc; m++) {
  629. if (*(*(buff + m) + 6) > -BIG)
  630. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 6);
  631. }
  632. Rast_put_d_row(d3, tmp_buf);
  633. }
  634. if (i == nr - 1) {
  635. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  636. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  637. for (b = 0; b < u_l / 2; b++)
  638. Rast_put_d_row(d3, tmp_buf2);
  639. G_free(tmp_buf2);
  640. }
  641. }
  642. if (choice->div[4]) {
  643. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  644. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  645. if (i == 0) {
  646. for (b = 0; b < u_l / 2; b++)
  647. Rast_put_d_row(d4, tmp_buf);
  648. }
  649. if (i < nr) {
  650. for (m = 0; m < nc; m++) {
  651. if (*(*(buff + m) + 7) > -BIG)
  652. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 7);
  653. }
  654. Rast_put_d_row(d4, tmp_buf);
  655. }
  656. if (i == nr - 1) {
  657. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  658. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  659. for (b = 0; b < u_l / 2; b++)
  660. Rast_put_d_row(d4, tmp_buf2);
  661. G_free(tmp_buf2);
  662. }
  663. }
  664. if (choice->te2[1]) {
  665. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  666. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  667. if (i == 0) {
  668. for (b = 0; b < u_l / 2; b++)
  669. Rast_put_d_row(t1, tmp_buf);
  670. }
  671. if (i < nr) {
  672. for (m = 0; m < nc; m++) {
  673. if (*(*(buff + m) + 8) > -BIG)
  674. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 8);
  675. }
  676. Rast_put_d_row(t1, tmp_buf);
  677. }
  678. if (i == nr - 1) {
  679. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  680. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  681. for (b = 0; b < u_l / 2; b++)
  682. Rast_put_d_row(t1, tmp_buf2);
  683. G_free(tmp_buf2);
  684. }
  685. }
  686. if (choice->te2[2]) {
  687. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  688. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  689. if (i == 0) {
  690. for (b = 0; b < u_l / 2; b++)
  691. Rast_put_d_row(t2, tmp_buf);
  692. }
  693. if (i < nr) {
  694. for (m = 0; m < nc; m++) {
  695. if (*(*(buff + m) + 9) > -BIG)
  696. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 9);
  697. }
  698. Rast_put_d_row(t2, tmp_buf);
  699. }
  700. if (i == nr - 1) {
  701. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  702. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  703. for (b = 0; b < u_l / 2; b++)
  704. Rast_put_d_row(t2, tmp_buf2);
  705. G_free(tmp_buf2);
  706. }
  707. }
  708. if (choice->te2[3]) {
  709. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  710. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  711. if (i == 0) {
  712. for (b = 0; b < u_l / 2; b++)
  713. Rast_put_d_row(t3, tmp_buf);
  714. }
  715. if (i < nr) {
  716. for (m = 0; m < nc; m++) {
  717. if (*(*(buff + m) + 10) > -BIG)
  718. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 10);
  719. }
  720. Rast_put_d_row(t3, tmp_buf);
  721. }
  722. if (i == nr - 1) {
  723. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  724. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  725. for (b = 0; b < u_l / 2; b++)
  726. Rast_put_d_row(t3, tmp_buf2);
  727. G_free(tmp_buf2);
  728. }
  729. }
  730. if (choice->te2[4]) {
  731. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  732. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  733. if (i == 0) {
  734. for (b = 0; b < u_l / 2; b++)
  735. Rast_put_d_row(t4, tmp_buf);
  736. }
  737. if (i < nr) {
  738. for (m = 0; m < nc; m++) {
  739. if (*(*(buff + m) + 11) > -BIG)
  740. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 11);
  741. }
  742. Rast_put_d_row(t4, tmp_buf);
  743. }
  744. if (i == nr - 1) {
  745. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  746. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  747. for (b = 0; b < u_l / 2; b++)
  748. Rast_put_d_row(t4, tmp_buf2);
  749. G_free(tmp_buf2);
  750. }
  751. }
  752. if (choice->te2[5]) {
  753. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  754. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  755. if (i == 0) {
  756. for (b = 0; b < u_l / 2; b++)
  757. Rast_put_d_row(t5, tmp_buf);
  758. }
  759. if (i < nr) {
  760. for (m = 0; m < nc; m++) {
  761. if (*(*(buff + m) + 12) > -BIG)
  762. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 12);
  763. }
  764. Rast_put_d_row(t5, tmp_buf);
  765. }
  766. if (i == nr - 1) {
  767. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  768. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  769. for (b = 0; b < u_l / 2; b++)
  770. Rast_put_d_row(t5, tmp_buf2);
  771. G_free(tmp_buf2);
  772. }
  773. }
  774. if (choice->jux[1]) {
  775. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  776. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  777. if (i == 0) {
  778. for (b = 0; b < u_l / 2; b++)
  779. Rast_put_d_row(j1, tmp_buf);
  780. }
  781. if (i < nr) {
  782. for (m = 0; m < nc; m++) {
  783. if (*(*(buff + m) + 13) > -BIG)
  784. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 13);
  785. }
  786. Rast_put_d_row(j1, tmp_buf);
  787. }
  788. if (i == nr - 1) {
  789. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  790. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  791. for (b = 0; b < u_l / 2; b++)
  792. Rast_put_d_row(j1, tmp_buf2);
  793. G_free(tmp_buf2);
  794. }
  795. }
  796. if (choice->jux[2]) {
  797. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  798. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  799. if (i == 0) {
  800. for (b = 0; b < u_l / 2; b++)
  801. Rast_put_d_row(j2, tmp_buf);
  802. }
  803. if (i < nr) {
  804. for (m = 0; m < nc; m++) {
  805. if (*(*(buff + m) + 14) > -BIG)
  806. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 14);
  807. }
  808. Rast_put_d_row(j2, tmp_buf);
  809. }
  810. if (i == nr - 1) {
  811. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  812. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  813. for (b = 0; b < u_l / 2; b++)
  814. Rast_put_d_row(j2, tmp_buf2);
  815. G_free(tmp_buf2);
  816. }
  817. }
  818. if (choice->edg[1]) {
  819. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  820. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  821. if (i == 0) {
  822. for (b = 0; b < u_l / 2; b++)
  823. Rast_put_d_row(e1, tmp_buf);
  824. }
  825. if (i < nr) {
  826. for (m = 0; m < nc; m++) {
  827. if (*(*(buff + m) + 15) > -BIG)
  828. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 15);
  829. }
  830. Rast_put_d_row(e1, tmp_buf);
  831. }
  832. if (i == nr - 1) {
  833. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  834. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  835. for (b = 0; b < u_l / 2; b++)
  836. Rast_put_d_row(e1, tmp_buf2);
  837. G_free(tmp_buf2);
  838. }
  839. }
  840. if (choice->edg[2]) {
  841. Rast_zero_buf(tmp_buf, DCELL_TYPE);
  842. Rast_set_null_value(tmp_buf, x0 + nc + u_w, DCELL_TYPE);
  843. if (i == 0) {
  844. for (b = 0; b < u_l / 2; b++)
  845. Rast_put_d_row(e2, tmp_buf);
  846. }
  847. if (i < nr) {
  848. for (m = 0; m < nc; m++) {
  849. if (*(*(buff + m) + 16) > -BIG)
  850. *(tmp_buf + (x0 + m + u_w / 2)) = *(*(buff + m) + 16);
  851. }
  852. Rast_put_d_row(e2, tmp_buf);
  853. }
  854. if (i == nr - 1) {
  855. tmp_buf2 = Rast_allocate_buf(DCELL_TYPE);
  856. Rast_set_null_value(tmp_buf2, x0 + nc + u_w, DCELL_TYPE);
  857. for (b = 0; b < u_l / 2; b++)
  858. Rast_put_d_row(e2, tmp_buf2);
  859. G_free(tmp_buf2);
  860. }
  861. }
  862. G_free(tmp_buf);
  863. }
  864. time(&finished_time);
  865. fprintf(stdout, "\nACTUAL COMPLETION = %s",
  866. asctime(localtime(&finished_time)));
  867. fflush(stdout);
  868. /* free the memory allocated for the
  869. mask and other buffer */
  870. if (fmask > 0)
  871. G_free(row_buf);
  872. for (p = 0; p < nc + 1; p++)
  873. G_free(buff[p]);
  874. G_free(buff);
  875. /* close the raster maps, set the
  876. color table for the new raster
  877. map and compress the map */
  878. if (choice->att[1]) {
  879. Rast_close(b1);
  880. set_colors("b1");
  881. sprintf(cmdbuf, "%s %s", "r.compress", "b1");
  882. system(cmdbuf);
  883. }
  884. if (choice->att[2]) {
  885. Rast_close(b2);
  886. set_colors("b2");
  887. sprintf(cmdbuf, "%s %s", "r.compress", "b2");
  888. system(cmdbuf);
  889. }
  890. if (choice->att[3]) {
  891. Rast_close(b3);
  892. set_colors("b3");
  893. sprintf(cmdbuf, "%s %s", "r.compress", "b3");
  894. system(cmdbuf);
  895. }
  896. if (choice->att[4]) {
  897. Rast_close(b4);
  898. set_colors("b4");
  899. sprintf(cmdbuf, "%s %s", "r.compress", "b4");
  900. system(cmdbuf);
  901. }
  902. if (choice->div[1]) {
  903. Rast_close(d1);
  904. set_colors("d1");
  905. sprintf(cmdbuf, "%s %s", "r.compress", "d1");
  906. system(cmdbuf);
  907. }
  908. if (choice->div[2]) {
  909. Rast_close(d2);
  910. set_colors("d2");
  911. sprintf(cmdbuf, "%s %s", "r.compress", "d2");
  912. system(cmdbuf);
  913. }
  914. if (choice->div[3]) {
  915. Rast_close(d3);
  916. set_colors("d3");
  917. sprintf(cmdbuf, "%s %s", "r.compress", "d3");
  918. system(cmdbuf);
  919. }
  920. if (choice->div[4]) {
  921. Rast_close(d4);
  922. set_colors("d4");
  923. sprintf(cmdbuf, "%s %s", "r.compress", "d4");
  924. system(cmdbuf);
  925. }
  926. if (choice->te2[1]) {
  927. Rast_close(t1);
  928. set_colors("t1");
  929. sprintf(cmdbuf, "%s %s", "r.compress", "t1");
  930. system(cmdbuf);
  931. }
  932. if (choice->te2[2]) {
  933. Rast_close(t2);
  934. set_colors("t2");
  935. sprintf(cmdbuf, "%s %s", "r.compress", "t2");
  936. system(cmdbuf);
  937. }
  938. if (choice->te2[3]) {
  939. Rast_close(t3);
  940. set_colors("t3");
  941. sprintf(cmdbuf, "%s %s", "r.compress", "t3");
  942. system(cmdbuf);
  943. }
  944. if (choice->te2[4]) {
  945. Rast_close(t4);
  946. set_colors("t4");
  947. sprintf(cmdbuf, "%s %s", "r.compress", "t4");
  948. system(cmdbuf);
  949. }
  950. if (choice->te2[5]) {
  951. Rast_close(t5);
  952. set_colors("t5");
  953. sprintf(cmdbuf, "%s %s", "r.compress", "t5");
  954. system(cmdbuf);
  955. }
  956. if (choice->jux[1]) {
  957. Rast_close(j1);
  958. set_colors("j1");
  959. sprintf(cmdbuf, "%s %s", "r.compress", "j1");
  960. system(cmdbuf);
  961. }
  962. if (choice->jux[2]) {
  963. Rast_close(j2);
  964. set_colors("j2");
  965. sprintf(cmdbuf, "%s %s", "r.compress", "j2");
  966. system(cmdbuf);
  967. }
  968. if (choice->edg[1]) {
  969. Rast_close(e1);
  970. set_colors("e1");
  971. sprintf(cmdbuf, "%s %s", "r.compress", "e1");
  972. system(cmdbuf);
  973. }
  974. if (choice->edg[2]) {
  975. Rast_close(e2);
  976. set_colors("e2");
  977. sprintf(cmdbuf, "%s %s", "r.compress", "e2");
  978. system(cmdbuf);
  979. }
  980. Rast_close(fmask);
  981. return;
  982. }
  983. /* SET "OUT" RASTER FILE COLOR
  984. TABLE TO G-Y-R */
  985. void set_colors(char *name)
  986. {
  987. struct Colors colors;
  988. struct FPRange fprange;
  989. Rast_read_fp_range(name, G_mapset(), &fprange);
  990. Rast_make_gyr_fp_colors(&colors, fprange.min, fprange.max);
  991. Rast_write_colors(name, G_mapset(), &colors);
  992. return;
  993. }
  994. /* OPEN OUTPUT FILE WITH ERROR TRAP */
  995. FILE *fopen0(char *name, char *flag)
  996. {
  997. FILE *fp;
  998. if (!(fp = fopen(name, flag))) {
  999. fprintf(stdout, "\n");
  1000. fprintf(stdout, " ******************************************\n");
  1001. fprintf(stdout, " Can't open output file \"%s\" \n",
  1002. name);
  1003. fprintf(stdout, " Do you have write permission in r.le.out \n");
  1004. fprintf(stdout, " subdirectory? \n");
  1005. fprintf(stdout, " ******************************************\n");
  1006. exit(1);
  1007. }
  1008. return fp;
  1009. }
  1010. /* OPEN INPUT FILE WITH ERROR TRAP */
  1011. FILE *fopen1(char *name, char *flag)
  1012. {
  1013. FILE *fp;
  1014. if (!(fp = fopen(name, flag))) {
  1015. fprintf(stdout, "\n");
  1016. fprintf(stdout,
  1017. " ******************************************************\n");
  1018. fprintf(stdout,
  1019. " You chose a moving window or sampling units analysis \n");
  1020. fprintf(stdout,
  1021. " but r.le.pixel can't find file \"%s\" \n",
  1022. name);
  1023. fprintf(stdout,
  1024. " which defines the moving window or sampling units \n");
  1025. fprintf(stdout,
  1026. " First use r.le.setup for to setup a moving window or \n");
  1027. fprintf(stdout,
  1028. " sampling units to make this file \n");
  1029. fprintf(stdout,
  1030. " ******************************************************\n");
  1031. exit(1);
  1032. }
  1033. return fp;
  1034. }
  1035. /* OPEN WEIGHT FILE WITH ERROR TRAP */
  1036. FILE *fopen2(char *name, char *flag)
  1037. {
  1038. FILE *fp;
  1039. if (!(fp = fopen(name, flag))) {
  1040. fprintf(stdout, "\n");
  1041. fprintf(stdout,
  1042. " ***************************************************\n");
  1043. fprintf(stdout,
  1044. " You chose a juxtaposition measure, but r.le.pixel \n");
  1045. fprintf(stdout,
  1046. " can't find file \"%s\" \n",
  1047. name);
  1048. fprintf(stdout,
  1049. " which defines the weights for different edges \n");
  1050. fprintf(stdout,
  1051. " First use a text editor to make this file \n");
  1052. fprintf(stdout,
  1053. " ***************************************************\n");
  1054. exit(1);
  1055. }
  1056. return fp;
  1057. }
  1058. /* OPEN EDGE FILE WITH ERROR TRAP */
  1059. FILE *fopen3(char *name, char *flag)
  1060. {
  1061. FILE *fp;
  1062. if (!(fp = fopen(name, flag))) {
  1063. fprintf(stdout, "\n");
  1064. fprintf(stdout,
  1065. " ***************************************************\n");
  1066. fprintf(stdout,
  1067. " You chose an edge measure, but r.le.pixel \n");
  1068. fprintf(stdout,
  1069. " can't find file \"%s\" \n",
  1070. name);
  1071. fprintf(stdout,
  1072. " which defines the types of edges to be counted \n");
  1073. fprintf(stdout,
  1074. " First use a text editor to make this file \n");
  1075. fprintf(stdout,
  1076. " ***************************************************\n");
  1077. exit(1);
  1078. }
  1079. return fp;
  1080. }
  1081. /* PERFORMANCE METER - DISPLAYS
  1082. THE PROGRESS OF THE MOVING WINDOW
  1083. AS A COUNT AND ESTIMATED COMPLETION
  1084. TIME WHILE THE PROGRAM RUNS */
  1085. void meter2(int n, int i, int div)
  1086. {
  1087. long current_time, time_left, elapsed, complete;
  1088. static long start;
  1089. float window_time;
  1090. static int k = 0;
  1091. char done2[30];
  1092. int d;
  1093. if (i <= 1) {
  1094. time(&start);
  1095. }
  1096. if (i < 10)
  1097. d = 1;
  1098. else
  1099. d = div;
  1100. if (k > 2000) {
  1101. if (G_fseek(stdout, 0L, 0))
  1102. G_fatal_error("Can't reset the \"stdout\", exit.\n");
  1103. k = 0;
  1104. }
  1105. if ((n - i) % d == 0) {
  1106. time(&current_time);
  1107. elapsed = current_time - start;
  1108. window_time = ((float)elapsed) / (i + 1);
  1109. time_left = (long)((n - i) * window_time);
  1110. complete = current_time + time_left;
  1111. strncpy(done2, asctime(localtime(&complete)), 24);
  1112. done2[24] = '\0';
  1113. fprintf(stdout, "WINDOWS LEFT = %8d EST. COMPLETION = %s\r",
  1114. (n - i), done2);
  1115. fflush(stdout);
  1116. k++;
  1117. }
  1118. return;
  1119. }
  1120. /* READ IN THE MOVING WINDOW
  1121. PARAMETERS */
  1122. void read_mwind(int *uw, int *ul, int *nc, int *nr, int *x0, int *y0,
  1123. float *radius)
  1124. {
  1125. FILE *fp;
  1126. int ww, wl;
  1127. char *buf;
  1128. fp = fopen1("r.le.para/move_wind", "r");
  1129. buf = G_malloc(513);
  1130. fgets(buf, 512, fp);
  1131. sscanf(buf, "%d%d", uw, ul);
  1132. fgets(buf, 512, fp);
  1133. sscanf(buf, "%f", radius);
  1134. fgets(buf, 512, fp);
  1135. sscanf(buf, "%d%d", &ww, &wl);
  1136. fgets(buf, 512, fp);
  1137. sscanf(buf, "%d%d", x0, y0);
  1138. *nc = ww - *uw + 1;
  1139. *nr = wl - *ul + 1;
  1140. G_free(buf);
  1141. fclose(fp);
  1142. return;
  1143. }
  1144. /* READ IN THE SAMPLING UNIT
  1145. PARAMETERS AND RUN R.LE.PIXEL */
  1146. void unit_driver()
  1147. {
  1148. register int i, j, k, m;
  1149. int top, left, u_w, u_l, nscl, nu, fd, *tmp;
  1150. float *ftmp;
  1151. double *richwhole, *dtmp;
  1152. char *nul_buf;
  1153. char *buf, unitname[10], istr[3];
  1154. int cntwhole = 0;
  1155. FILE *fp;
  1156. CELL **units, *unit_buf;
  1157. float radius = 0.0;
  1158. struct Cell_head wind;
  1159. G_get_set_window(&wind);
  1160. fp = fopen1("r.le.para/units", "r");
  1161. buf = G_malloc(513);
  1162. /* get the number of scales */
  1163. fgets(buf, 512, fp);
  1164. sscanf(buf, "%d", &nscl);
  1165. if (choice->edg[2] || choice->jux[0]) {
  1166. /* dynamically allocate memory for
  1167. the richness array */
  1168. richwhole = (double *)G_calloc(MAX, sizeof(double));
  1169. /* initialize the richness array
  1170. elements with the value -999 */
  1171. for (i = 0; i < MAX; i++)
  1172. richwhole[i] = -999.0;
  1173. switch (data_type) {
  1174. case CELL_TYPE:
  1175. tmp = Rast_allocate_buf(CELL_TYPE);
  1176. break;
  1177. case FCELL_TYPE:
  1178. ftmp = Rast_allocate_buf(FCELL_TYPE);
  1179. break;
  1180. case DCELL_TYPE:
  1181. dtmp = Rast_allocate_buf(DCELL_TYPE);
  1182. break;
  1183. }
  1184. nul_buf = Rast_allocate_null_buf();
  1185. /* go through the search area
  1186. pixel by pixel */
  1187. for (i = 0; i < wind.rows; i++) {
  1188. switch (data_type) {
  1189. case (CELL_TYPE):
  1190. Rast_zero_buf(tmp, CELL_TYPE);
  1191. Rast_get_row(finput, tmp, i, CELL_TYPE);
  1192. break;
  1193. case (FCELL_TYPE):
  1194. Rast_zero_buf(ftmp, FCELL_TYPE);
  1195. Rast_get_row(finput, ftmp, i, FCELL_TYPE);
  1196. break;
  1197. case (DCELL_TYPE):
  1198. Rast_zero_buf(dtmp, DCELL_TYPE);
  1199. Rast_get_row(finput, dtmp, i, DCELL_TYPE);
  1200. break;
  1201. }
  1202. Rast_get_null_value_row(finput, nul_buf, i);
  1203. for (j = 0; j < wind.cols; j++) {
  1204. /* if the raster value is not null,
  1205. call get_rich_whole to tally up the
  1206. number of different attributes
  1207. in the search area and fill
  1208. the richness array with those
  1209. attributes */
  1210. switch (data_type) {
  1211. case (CELL_TYPE):
  1212. if ((*(tmp + j) ||
  1213. *(tmp + j) == 0) && *(nul_buf + j) == 0.0)
  1214. get_rich_whole((double)*(tmp + j), richwhole,
  1215. &cntwhole);
  1216. break;
  1217. case (FCELL_TYPE):
  1218. if ((*(ftmp + j) ||
  1219. *(ftmp + j) == 0) && *(nul_buf + j) == 0.0)
  1220. get_rich_whole((double)*(ftmp + j), richwhole,
  1221. &cntwhole);
  1222. break;
  1223. case (DCELL_TYPE):
  1224. if ((*(dtmp + j) ||
  1225. *(dtmp + j) == 0) && *(nul_buf + j) == 0.0)
  1226. get_rich_whole(*(dtmp + j), richwhole, &cntwhole);
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. switch (data_type) {
  1232. case (CELL_TYPE):
  1233. G_free(tmp);
  1234. break;
  1235. case (FCELL_TYPE):
  1236. G_free(ftmp);
  1237. break;
  1238. case (DCELL_TYPE):
  1239. G_free(dtmp);
  1240. break;
  1241. }
  1242. G_free(nul_buf);
  1243. G_free(richwhole);
  1244. }
  1245. /* dynamically allocate storage for the
  1246. buffer that will hold the map of the
  1247. sampling units */
  1248. if (choice->units) {
  1249. units = (CELL **) G_calloc(wind.rows + 3, sizeof(CELL *));
  1250. for (i = 0; i < wind.rows + 3; i++)
  1251. units[i] = (CELL *) G_calloc(wind.cols + 3, sizeof(CELL));
  1252. }
  1253. /* for each scale */
  1254. for (i = 0; i < nscl; i++) {
  1255. g_scale = i + 1;
  1256. fgets(buf, 512, fp);
  1257. sscanf(buf, "%d", &nu);
  1258. /* get the width and length */
  1259. fgets(buf, 512, fp);
  1260. sscanf(buf, "%d%d", &u_w, &u_l);
  1261. /* get the radius to see if sampling
  1262. units are circles */
  1263. fgets(buf, 512, fp);
  1264. sscanf(buf, "%f", &radius);
  1265. /* if units map was chosen, zero it,
  1266. then copy the number of the map
  1267. to the end of the word "units" */
  1268. if (choice->units) {
  1269. for (k = 0; k < wind.rows + 3; k++) {
  1270. for (m = 0; m < wind.cols + 3; m++)
  1271. *(*(units + k) + m) = 0;
  1272. }
  1273. if (i == 0)
  1274. strcpy(istr, "1");
  1275. else if (i == 1)
  1276. strcpy(istr, "2");
  1277. else if (i == 2)
  1278. strcpy(istr, "3");
  1279. else if (i == 3)
  1280. strcpy(istr, "4");
  1281. else if (i == 4)
  1282. strcpy(istr, "5");
  1283. else if (i == 5)
  1284. strcpy(istr, "6");
  1285. else if (i == 6)
  1286. strcpy(istr, "7");
  1287. else if (i == 7)
  1288. strcpy(istr, "8");
  1289. else if (i == 8)
  1290. strcpy(istr, "9");
  1291. else if (i == 9)
  1292. strcpy(istr, "10");
  1293. else if (i == 10)
  1294. strcpy(istr, "11");
  1295. else if (i == 11)
  1296. strcpy(istr, "12");
  1297. else if (i == 12)
  1298. strcpy(istr, "13");
  1299. else if (i == 13)
  1300. strcpy(istr, "14");
  1301. else if (i == 14)
  1302. strcpy(istr, "15");
  1303. else if (i > 14) {
  1304. fprintf(stdout, "\n");
  1305. fprintf(stdout,
  1306. " ***************************************************\n");
  1307. fprintf(stdout,
  1308. " You cannot choose more than 15 scales \n");
  1309. fprintf(stdout,
  1310. " ***************************************************\n");
  1311. exit(0);
  1312. }
  1313. }
  1314. /* for each unit */
  1315. for (j = 0; j < nu; j++) {
  1316. g_unit = j + 1;
  1317. fgets(buf, 512, fp);
  1318. sscanf(buf, "%d%d", &left, &top);
  1319. /* call cell_clip driver */
  1320. run_clip(wind.cols, wind.rows, u_w, u_l, left, top, units, j,
  1321. cntwhole, radius);
  1322. }
  1323. /* if a map of the sampling units
  1324. was requested */
  1325. if (choice->units) {
  1326. strcpy(unitname, "units_");
  1327. strcat(unitname, istr);
  1328. fd = Rast_open_new(unitname, CELL_TYPE);
  1329. unit_buf = Rast_allocate_buf(CELL_TYPE);
  1330. for (k = 1; k < wind.rows + 1; k++) {
  1331. Rast_zero_buf(unit_buf, CELL_TYPE);
  1332. Rast_set_null_value(unit_buf, wind.cols + 1, CELL_TYPE);
  1333. for (m = 1; m < wind.cols + 3; m++) {
  1334. if (*(*(units + k) + m))
  1335. *(unit_buf + m - 1) = *(*(units + k) + m);
  1336. }
  1337. Rast_put_row(fd, unit_buf, CELL_TYPE);
  1338. }
  1339. Rast_close(fd);
  1340. G_free(unit_buf);
  1341. }
  1342. }
  1343. if (choice->units) {
  1344. for (m = 0; m < wind.rows + 3; m++)
  1345. G_free(units[m]);
  1346. G_free(units);
  1347. }
  1348. G_free(buf);
  1349. fclose(fp);
  1350. return;
  1351. }
  1352. /* CHECK FOR OUT-OF MAP UNIT, THEN
  1353. CALL CELL CLIP DRIVER */
  1354. void run_clip(int ncols, int nrows, int u_w, int u_l, int left, int top,
  1355. CELL ** units, int id, int cntwhole, float radius)
  1356. {
  1357. int i, j;
  1358. double center_row, center_col;
  1359. double dist;
  1360. G_sleep_on_error(0);
  1361. /* check unit */
  1362. if (ncols < left + u_w || nrows < top + u_l) {
  1363. fprintf(stdout, "\n");
  1364. fprintf(stdout,
  1365. " ******************************************************\n");
  1366. fprintf(stdout,
  1367. " Sampling units do not fit within the current region. \n");
  1368. fprintf(stdout,
  1369. " Either correct the region or redo the sampling unit \n");
  1370. fprintf(stdout,
  1371. " selection using r.le.setup. This error message came \n");
  1372. fprintf(stdout,
  1373. " from an analysis of the r.le.para/units file and the \n");
  1374. fprintf(stdout,
  1375. " current region setting. \n");
  1376. fprintf(stdout,
  1377. " ******************************************************\n");
  1378. exit(1);
  1379. }
  1380. if (choice->units) {
  1381. if (radius) {
  1382. center_row = ((double)(top + 1) + ((double)u_l - 1) / 2);
  1383. center_col = ((double)(left + 1) + ((double)u_w - 1) / 2);
  1384. for (i = top + 1; i < top + 1 + u_l; i++) {
  1385. for (j = left + 1; j < left + 1 + u_w; j++) {
  1386. dist =
  1387. sqrt(((double)i - center_row) * ((double)i -
  1388. center_row) +
  1389. ((double)j - center_col) * ((double)j -
  1390. center_col));
  1391. if (dist < radius) {
  1392. *(*(units + i) + j) = id + 1;
  1393. /*printf("units[%d][%d]=%d id=%d\n",i,j, *(*(units + i) + j),id + 1); */
  1394. }
  1395. }
  1396. }
  1397. }
  1398. else {
  1399. for (i = top + 1; i < top + 1 + u_l; i++) {
  1400. for (j = left + 1; j < left + 1 + u_w; j++)
  1401. *(*(units + i) + j) = id + 1;
  1402. }
  1403. }
  1404. }
  1405. cell_clip_drv(left, top, u_w, u_l, NULL, 0, cntwhole, radius);
  1406. return;
  1407. }
  1408. /* CLIP THE REGION, THEN
  1409. RUN R.LE.PIXEL */
  1410. void whole_reg_driver()
  1411. {
  1412. register int i, j;
  1413. int *row_buf, regcnt, found, fr, nrows, ncols, cntwhole = 0, *tmp;
  1414. float *ftmp;
  1415. double *richwhole, *dtmp;
  1416. char *nul_buf;
  1417. REGLIST *ptrfirst, *ptrthis, *ptrnew;
  1418. RASTER_MAP_TYPE data_type;
  1419. data_type = Rast_map_type(choice->fn, G_mapset());
  1420. nrows = Rast_window_rows();
  1421. ncols = Rast_window_cols();
  1422. g_scale = 1;
  1423. if (choice->edg[2] || choice->jux[0]) {
  1424. /* dynamically allocate memory for
  1425. the richness array */
  1426. richwhole = (double *)G_calloc(MAX, sizeof(double));
  1427. /* initialize the richness array
  1428. elements with the value -999 */
  1429. for (i = 0; i < MAX; i++)
  1430. richwhole[i] = -999.0;
  1431. /* allocate memory to store a row of the
  1432. raster map, depending on the type of
  1433. input raster map; keep track of the
  1434. name of the buffer for each raster type */
  1435. switch (data_type) {
  1436. case CELL_TYPE:
  1437. tmp = Rast_allocate_buf(CELL_TYPE);
  1438. break;
  1439. case FCELL_TYPE:
  1440. ftmp = Rast_allocate_buf(FCELL_TYPE);
  1441. break;
  1442. case DCELL_TYPE:
  1443. dtmp = Rast_allocate_buf(DCELL_TYPE);
  1444. break;
  1445. }
  1446. nul_buf = Rast_allocate_null_buf();
  1447. /* go through the search area
  1448. pixel by pixel */
  1449. for (i = 0; i < nrows; i++) {
  1450. switch (data_type) {
  1451. case (CELL_TYPE):
  1452. Rast_zero_buf(tmp, CELL_TYPE);
  1453. Rast_get_row(finput, tmp, i, CELL_TYPE);
  1454. break;
  1455. case (FCELL_TYPE):
  1456. Rast_zero_buf(ftmp, FCELL_TYPE);
  1457. Rast_get_row(finput, ftmp, i, FCELL_TYPE);
  1458. break;
  1459. case (DCELL_TYPE):
  1460. Rast_zero_buf(dtmp, DCELL_TYPE);
  1461. Rast_get_row(finput, dtmp, i, DCELL_TYPE);
  1462. break;
  1463. }
  1464. Rast_get_null_value_row(finput, nul_buf, i);
  1465. for (j = 0; j < ncols; j++) {
  1466. /* if the raster value is not null,
  1467. call get_rich_whole to tally up the
  1468. number of different attributes
  1469. in the search area and fill
  1470. the richness array with those
  1471. attributes */
  1472. switch (data_type) {
  1473. case (CELL_TYPE):
  1474. if ((*(tmp + j) ||
  1475. *(tmp + j) == 0) && *(nul_buf + j) == 0.0)
  1476. get_rich_whole((double)*(tmp + j), richwhole,
  1477. &cntwhole);
  1478. break;
  1479. case (FCELL_TYPE):
  1480. if ((*(ftmp + j) ||
  1481. *(ftmp + j) == 0.0) && *(nul_buf + j) == 0.0)
  1482. get_rich_whole((double)*(ftmp + j), richwhole,
  1483. &cntwhole);
  1484. break;
  1485. case (DCELL_TYPE):
  1486. if ((*(dtmp + j) ||
  1487. *(dtmp + j) == 0.0) && *(nul_buf + j) == 0.0)
  1488. get_rich_whole(*(dtmp + j), richwhole, &cntwhole);
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. switch (data_type) {
  1494. case (CELL_TYPE):
  1495. G_free(tmp);
  1496. break;
  1497. case (FCELL_TYPE):
  1498. G_free(ftmp);
  1499. break;
  1500. case (DCELL_TYPE):
  1501. G_free(dtmp);
  1502. break;
  1503. }
  1504. G_free(nul_buf);
  1505. G_free(richwhole);
  1506. }
  1507. if (choice->wrum != 'r') {
  1508. cell_clip_drv(0, 0, ncols, nrows, NULL, 0, cntwhole, 0.0);
  1509. }
  1510. else {
  1511. regcnt = 0;
  1512. fr = Rast_open_old(choice->reg, G_mapset());
  1513. row_buf = Rast_allocate_buf(CELL_TYPE);
  1514. for (i = 0; i < nrows; i++) {
  1515. Rast_zero_buf(row_buf, CELL_TYPE);
  1516. Rast_get_row(fr, row_buf, i, CELL_TYPE);
  1517. for (j = 0; j < ncols; j++) {
  1518. if (*(row_buf + j)) {
  1519. if (regcnt == 0)
  1520. ptrfirst = (REGLIST *) NULL;
  1521. ptrthis = ptrfirst;
  1522. found = 0;
  1523. while (ptrthis) {
  1524. if (*(row_buf + j) == ptrthis->att) {
  1525. if (j < ptrthis->w)
  1526. ptrthis->w = j;
  1527. if (j > ptrthis->e)
  1528. ptrthis->e = j;
  1529. if (i < ptrthis->n)
  1530. ptrthis->n = i;
  1531. if (i > ptrthis->s)
  1532. ptrthis->s = i;
  1533. found = 1;
  1534. }
  1535. ptrthis = ptrthis->next;
  1536. }
  1537. if (!found) {
  1538. ptrnew = (REGLIST *) G_calloc(1, sizeof(REGLIST));
  1539. if (ptrfirst == (REGLIST *) NULL)
  1540. ptrfirst = ptrthis = ptrnew;
  1541. else {
  1542. ptrthis = ptrfirst;
  1543. while (ptrthis->next != (REGLIST *) NULL)
  1544. ptrthis = ptrthis->next;
  1545. ptrthis->next = ptrnew;
  1546. ptrthis = ptrnew;
  1547. }
  1548. ptrthis->att = *(row_buf + j);
  1549. ptrthis->n = i;
  1550. ptrthis->s = i;
  1551. ptrthis->e = j;
  1552. ptrthis->w = j;
  1553. regcnt++;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. g_unit = 0;
  1559. ptrthis = ptrfirst;
  1560. while (ptrthis) {
  1561. g_unit = ptrthis->att;
  1562. cell_clip_drv(ptrthis->w, ptrthis->n, ptrthis->e - ptrthis->w + 1,
  1563. ptrthis->s - ptrthis->n + 1, NULL, ptrthis->att,
  1564. cntwhole, 0.0);
  1565. ptrthis = ptrthis->next;
  1566. }
  1567. Rast_close(fr);
  1568. G_free(row_buf);
  1569. G_free(ptrnew);
  1570. }
  1571. return;
  1572. }
  1573. /* FIND UNCOUNTED ATTRIBUTES,
  1574. COUNT THEM UP, AND ADD THEM TO
  1575. THE RICHNESS ARRAY IN UNSORTED
  1576. ORDER */
  1577. void get_rich_whole(double att, double rich[], int *cnt)
  1578. {
  1579. register int i;
  1580. /* if this attribute is already
  1581. in the richness array, then
  1582. return */
  1583. for (i = 0; i < *cnt; i++)
  1584. if (att == rich[i])
  1585. break;
  1586. /* if this attribute is not already
  1587. in the richness array, then make
  1588. it the "cnt" element of the
  1589. array, then increment the cnt */
  1590. if (i >= *cnt) {
  1591. rich[*cnt] = att;
  1592. ++(*cnt);
  1593. /*printf("rich[%d]=%f\n",*cnt,att); */
  1594. }
  1595. return;
  1596. }