Debian Codegraph @

raster/r.li/r.li.renyi/renyi.c

raw
1 /****************************************************************************
2 *
3 * MODULE: r.li.renyi
4 *
5 * PURPOSE: calculates renyi's diversity index
6 *
7 * AUTHOR(S): Luca Delucchi, Fondazione Edmund Mach
8 * Serena Pallecchi student of Computer Science
9 * University of Pisa (Italy)
10 * Rewrite: Markus Metz
11 *
12 * COPYRIGHT:
13 * This program is free software under the GPL (>=v2)
14 * Read the COPYING file that comes with GRASS for details.
15 *
16 ***************************************************************************/
17
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <math.h>
21
22 #include <grass/gis.h>
23 #include <grass/raster.h>
24 #include <grass/glocale.h>
25
26 #include "../r.li.daemon/daemon.h"
27 #include "../r.li.daemon/avlDefs.h"
28 #include "../r.li.daemon/avl.h"
29
30 /* template is shannon */
31
32 rli_func renyi;
33 int calculate(int fd, struct area_entry *ad, char **par, double *result);
34 int calculateD(int fd, struct area_entry *ad, char **par, double *result);
35 int calculateF(int fd, struct area_entry *ad, char **par, double *result);
36
37 int main(int argc, char *argv[])
38 {
39 struct Option *raster, *conf, *output, *alpha;
40 struct GModule *module;
41 char **par = NULL;
42
43 G_gisinit(argv[0]);
44 module = G_define_module();
45 module->description =
46 _("Calculates Renyi's diversity index on a raster map");
47 G_add_keyword(_("raster"));
48 G_add_keyword(_("landscape structure analysis"));
49 G_add_keyword(_("diversity index"));
50
51 /* define options */
52
53 raster = G_define_standard_option(G_OPT_R_INPUT);
54
55 conf = G_define_standard_option(G_OPT_F_INPUT);
56 conf->key = "config";
57 conf->description = _("Configuration file");
58 conf->required = YES;
59
60 alpha = G_define_option();
61 alpha->key = "alpha";
62 alpha->description =
63 _("Alpha value is the order of the generalized entropy");
64 alpha->type = TYPE_STRING;
65 alpha->required = YES;
66
67 output = G_define_standard_option(G_OPT_R_OUTPUT);
68
69 if (G_parser(argc, argv))
70 exit(EXIT_FAILURE);
71
72 if (atof(alpha->answer) == 1) {
73 G_fatal_error("If alpha = 1 Renyi index is not defined. (Ricotta et "
74 "al., 2003, Environ. Model. Softw.)");
75 exit(RLI_ERRORE);
76 }
77 else if (atof(alpha->answer) < 0.) {
78 G_fatal_error("Alpha must be > 0 otherwise Renyi index is not defined. "
79 "(Ricotta et al., 2003, Environ. Model. Softw.)");
80 exit(RLI_ERRORE);
81 }
82 else {
83 par = &alpha->answer;
84 }
85 return calculateIndex(conf->answer, renyi, par, raster->answer,
86 output->answer);
87 }
88
89 int renyi(int fd, char **par, struct area_entry *ad, double *result)
90 {
91 int ris = RLI_OK;
92 double indice = 0;
93
94 if (!par)
95 G_fatal_error("par is NULL");
96
97 switch (ad->data_type) {
98 case CELL_TYPE: {
99 ris = calculate(fd, ad, par, &indice);
100 break;
101 }
102 case DCELL_TYPE: {
103 ris = calculateD(fd, ad, par, &indice);
104 break;
105 }
106 case FCELL_TYPE: {
107 ris = calculateF(fd, ad, par, &indice);
108 break;
109 }
110 default: {
111 G_fatal_error("data type unknown");
112 }
113 }
114
115 if (ris != RLI_OK) {
116 return RLI_ERRORE;
117 }
118
119 *result = indice;
120
121 return RLI_OK;
122 }
123
124 int calculate(int fd, struct area_entry *ad, char **par, double *result)
125 {
126 CELL *buf;
127 CELL corrCell;
128 CELL precCell;
129
130 int i, j;
131 int mask_fd = -1, *mask_buf = NULL;
132 int ris = 0;
133 int masked = FALSE;
134 int res = RLI_OK;
135
136 long m = 0;
137 long tot = 0;
138 long zero = 0;
139 long totCorr = 1;
140
141 long area = 0;
142
143 avl_tree albero = NULL;
144 AVL_table array;
145 generic_cell uc;
146
147 uc.t = CELL_TYPE;
148
149 /* open mask if needed */
150 if (ad->mask == 1) {
151 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
152 return RLI_ERRORE;
153 mask_buf = G_malloc(ad->cl * sizeof(int));
154 if (mask_buf == NULL) {
155 close(mask_fd);
156 G_fatal_error("malloc mask_buf failed");
157 }
158 masked = TRUE;
159 }
160
161 Rast_set_c_null_value(&precCell, 1);
162 for (j = 0; j < ad->rl; j++) { /* for each row */
163 if (masked) {
164 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
165 close(mask_fd);
166 G_fatal_error("mask read failed");
167 }
168 }
169
170 buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
171
172 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
173 corrCell = buf[i + ad->x];
174
175 if ((masked) && (mask_buf[i] == 0)) {
176 Rast_set_c_null_value(&corrCell, 1);
177 }
178
179 if (!(Rast_is_null_value(&corrCell, uc.t))) {
180 /* total patch area */
181 area++;
182 }
183
184 if (!(Rast_is_null_value(&precCell, uc.t)) &&
185 corrCell == precCell) {
186
187 totCorr++;
188 }
189 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
190 corrCell != precCell) {
191
192 /* add precCell to search tree */
193 if (albero == NULL) {
194 uc.val.c = precCell;
195 albero = avl_make(uc, totCorr);
196 if (albero == NULL) {
197 if (masked)
198 close(mask_fd);
199 G_fatal_error("avl_make error");
200 }
201 m++;
202 }
203 else {
204 uc.val.c = precCell;
205 ris = avl_add(&albero, uc, totCorr);
206 switch (ris) {
207 case AVL_ERR: {
208 if (masked)
209 close(mask_fd);
210 G_fatal_error("avl_add error");
211 }
212 case AVL_ADD: {
213 m++;
214 break;
215 }
216 case AVL_PRES: {
217 break;
218 }
219 default: {
220 if (masked)
221 close(mask_fd);
222 G_fatal_error("avl_make unknown error");
223 }
224 }
225 }
226 totCorr = 1;
227 } /* endif not equal cells */
228 precCell = corrCell;
229 }
230 }
231
232 /* last closing */
233 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
234 if (albero == NULL) {
235 uc.val.c = precCell;
236 albero = avl_make(uc, totCorr);
237 if (albero == NULL) {
238 if (masked)
239 close(mask_fd);
240 G_fatal_error("avl_make error");
241 }
242 m++;
243 }
244 else {
245 uc.val.c = precCell;
246 ris = avl_add(&albero, uc, totCorr);
247 switch (ris) {
248 case AVL_ERR: {
249 if (masked)
250 close(mask_fd);
251 G_fatal_error("avl_add error");
252 }
253 case AVL_ADD: {
254 m++;
255 break;
256 }
257 case AVL_PRES: {
258 break;
259 }
260 default: {
261 if (masked)
262 close(mask_fd);
263 G_fatal_error("avl_add unknown error");
264 }
265 }
266 }
267 }
268
269 if (area > 0) {
270 double alpha;
271 double pi;
272 double t;
273 double sum, sum2;
274
275 alpha = atof(par[0]);
276
277 array = G_malloc(m * sizeof(AVL_tableRow));
278 if (array == NULL) {
279 if (masked)
280 close(mask_fd);
281 G_fatal_error("malloc array failed");
282 }
283 tot = avl_to_array(albero, zero, array);
284 if (tot != m) {
285 G_warning(
286 "avl_to_array unexpected value. the result could be wrong");
287 res = RLI_ERRORE;
288 G_free(array);
289 goto free_exit;
290 }
291
292 /* calculate renyi index */
293 sum = 0;
294 sum2 = 0;
295 for (i = 0; i < m; i++) {
296 t = array[i].tot;
297 pi = t / area;
298 sum += pow(pi, alpha);
299 sum2 += pi;
300 }
301 G_free(array);
302
303 /* correction for numerical instability */
304 if (sum2 != 1)
305 sum += 1 - sum2;
306
307 if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
308 G_warning("Renyi index calculation reached numerical instability. "
309 "This can happen with alpha close to 1. The result will "
310 "be set to zero.");
311 sum = 1;
312 }
313
314 *result = (1 / (1 - alpha)) * log(sum);
315 }
316 else
317 Rast_set_d_null_value(result, 1);
318
319 free_exit:
320 avl_destroy(albero);
321 if (masked) {
322 close(mask_fd);
323 G_free(mask_buf);
324 }
325
326 return res;
327 }
328
329 int calculateD(int fd, struct area_entry *ad, char **par, double *result)
330 {
331 DCELL *buf;
332 DCELL corrCell;
333 DCELL precCell;
334
335 int i, j;
336 int mask_fd = -1, *mask_buf = NULL;
337 int ris = 0;
338 int masked = FALSE;
339 int res = RLI_OK;
340
341 long m = 0;
342 long tot = 0;
343 long zero = 0;
344 long totCorr = 1;
345
346 long area = 0;
347
348 avl_tree albero = NULL;
349 AVL_table array;
350 generic_cell uc;
351
352 uc.t = DCELL_TYPE;
353
354 /* open mask if needed */
355 if (ad->mask == 1) {
356 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
357 return RLI_ERRORE;
358 mask_buf = G_malloc(ad->cl * sizeof(int));
359 if (mask_buf == NULL) {
360 close(mask_fd);
361 G_fatal_error("malloc mask_buf failed");
362 }
363 masked = TRUE;
364 }
365
366 Rast_set_d_null_value(&precCell, 1);
367 for (j = 0; j < ad->rl; j++) { /* for each row */
368 if (masked) {
369 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
370 close(mask_fd);
371 G_fatal_error("mask read failed");
372 }
373 }
374
375 buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
376
377 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
378 corrCell = buf[i + ad->x];
379
380 if ((masked) && (mask_buf[i] == 0)) {
381 Rast_set_d_null_value(&corrCell, 1);
382 }
383
384 if (!(Rast_is_null_value(&corrCell, uc.t))) {
385 /* total patch area */
386 area++;
387 }
388
389 if (!(Rast_is_null_value(&precCell, uc.t)) &&
390 corrCell == precCell) {
391
392 totCorr++;
393 }
394 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
395 corrCell != precCell) {
396
397 /* add precCell to search tree */
398 if (albero == NULL) {
399 uc.val.dc = precCell;
400 albero = avl_make(uc, totCorr);
401 if (albero == NULL) {
402 if (masked)
403 close(mask_fd);
404 G_fatal_error("avl_make error");
405 }
406 m++;
407 }
408 else {
409 uc.val.dc = precCell;
410 ris = avl_add(&albero, uc, totCorr);
411 switch (ris) {
412 case AVL_ERR: {
413 if (masked)
414 close(mask_fd);
415 G_fatal_error("avl_add error");
416 }
417 case AVL_ADD: {
418 m++;
419 break;
420 }
421 case AVL_PRES: {
422 break;
423 }
424 default: {
425 if (masked)
426 close(mask_fd);
427 G_fatal_error("avl_make unknown error");
428 }
429 }
430 }
431 totCorr = 1;
432 } /* endif not equal cells */
433 precCell = corrCell;
434 }
435 }
436
437 /* last closing */
438 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
439 if (albero == NULL) {
440 uc.val.dc = precCell;
441 albero = avl_make(uc, totCorr);
442 if (albero == NULL) {
443 if (masked)
444 close(mask_fd);
445 G_fatal_error("avl_make error");
446 }
447 m++;
448 }
449 else {
450 uc.val.dc = precCell;
451 ris = avl_add(&albero, uc, totCorr);
452 switch (ris) {
453 case AVL_ERR: {
454 if (masked)
455 close(mask_fd);
456 G_fatal_error("avl_add error");
457 }
458 case AVL_ADD: {
459 m++;
460 break;
461 }
462 case AVL_PRES: {
463 break;
464 }
465 default: {
466 if (masked)
467 close(mask_fd);
468 G_fatal_error("avl_add unknown error");
469 }
470 }
471 }
472 }
473
474 if (area > 0) {
475 double alpha;
476 double pi;
477 double t;
478 double sum, sum2;
479
480 alpha = atof(par[0]);
481
482 array = G_malloc(m * sizeof(AVL_tableRow));
483 if (array == NULL) {
484 if (masked)
485 close(mask_fd);
486 G_fatal_error("malloc array failed");
487 }
488 tot = avl_to_array(albero, zero, array);
489 if (tot != m) {
490 G_warning(
491 "avl_to_array unexpected value. the result could be wrong");
492 res = RLI_ERRORE;
493 G_free(array);
494 goto free_exit;
495 }
496
497 /* calculate renyi index */
498 sum = 0;
499 sum2 = 0;
500 for (i = 0; i < m; i++) {
501 t = array[i].tot;
502 pi = t / area;
503 sum += pow(pi, alpha);
504 sum += pi;
505 }
506 G_free(array);
507
508 /* correction for numerical instability */
509 if (sum2 != 1)
510 sum += 1 - sum2;
511
512 if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
513 G_warning("Renyi index calculation reached numerical instability. "
514 "This can happen with alpha close to 1. The result will "
515 "be set to zero.");
516 sum = 1;
517 }
518
519 *result = (1 / (1 - alpha)) * log(sum);
520 }
521 else
522 Rast_set_d_null_value(result, 1);
523
524 free_exit:
525 avl_destroy(albero);
526 if (masked) {
527 close(mask_fd);
528 G_free(mask_buf);
529 }
530
531 return res;
532 }
533
534 int calculateF(int fd, struct area_entry *ad, char **par, double *result)
535 {
536 FCELL *buf;
537 FCELL corrCell;
538 FCELL precCell;
539
540 int i, j;
541 int mask_fd = -1, *mask_buf = NULL;
542 int ris = 0;
543 int masked = FALSE;
544 int res = RLI_OK;
545
546 long m = 0;
547 long tot = 0;
548 long zero = 0;
549 long totCorr = 1;
550
551 long area = 0;
552
553 avl_tree albero = NULL;
554 AVL_table array;
555 generic_cell uc;
556
557 uc.t = FCELL_TYPE;
558
559 /* open mask if needed */
560 if (ad->mask == 1) {
561 if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
562 return RLI_ERRORE;
563 mask_buf = G_malloc(ad->cl * sizeof(int));
564 if (mask_buf == NULL) {
565 close(mask_fd);
566 G_fatal_error("malloc mask_buf failed");
567 }
568 masked = TRUE;
569 }
570
571 Rast_set_f_null_value(&precCell, 1);
572 for (j = 0; j < ad->rl; j++) { /* for each row */
573 if (masked) {
574 if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
575 close(mask_fd);
576 G_fatal_error("mask read failed");
577 }
578 }
579
580 buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
581
582 for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
583 corrCell = buf[i + ad->x];
584
585 if ((masked) && (mask_buf[i] == 0)) {
586 Rast_set_f_null_value(&corrCell, 1);
587 }
588
589 if (!(Rast_is_null_value(&corrCell, uc.t))) {
590 /* total patch area */
591 area++;
592 }
593
594 if (!(Rast_is_null_value(&precCell, uc.t)) &&
595 corrCell == precCell) {
596
597 totCorr++;
598 }
599 else if (!(Rast_is_null_value(&precCell, uc.t)) &&
600 corrCell != precCell) {
601
602 /* add precCell to search tree */
603 if (albero == NULL) {
604 uc.val.fc = precCell;
605 albero = avl_make(uc, totCorr);
606 if (albero == NULL) {
607 if (masked)
608 close(mask_fd);
609 G_fatal_error("avl_make error");
610 }
611 m++;
612 }
613 else {
614 uc.val.fc = precCell;
615 ris = avl_add(&albero, uc, totCorr);
616 switch (ris) {
617 case AVL_ERR: {
618 if (masked)
619 close(mask_fd);
620 G_fatal_error("avl_add error");
621 }
622 case AVL_ADD: {
623 m++;
624 break;
625 }
626 case AVL_PRES: {
627 break;
628 }
629 default: {
630 if (masked)
631 close(mask_fd);
632 G_fatal_error("avl_make unknown error");
633 }
634 }
635 }
636 totCorr = 1;
637 } /* endif not equal cells */
638 precCell = corrCell;
639 }
640 }
641
642 /* last closing */
643 if (area > 0 && !(Rast_is_null_value(&precCell, uc.t))) {
644 if (albero == NULL) {
645 uc.val.fc = precCell;
646 albero = avl_make(uc, totCorr);
647 if (albero == NULL) {
648 if (masked)
649 close(mask_fd);
650 G_fatal_error("avl_make error");
651 }
652 m++;
653 }
654 else {
655 uc.val.fc = precCell;
656 ris = avl_add(&albero, uc, totCorr);
657 switch (ris) {
658 case AVL_ERR: {
659 if (masked)
660 close(mask_fd);
661 G_fatal_error("avl_add error");
662 }
663 case AVL_ADD: {
664 m++;
665 break;
666 }
667 case AVL_PRES: {
668 break;
669 }
670 default: {
671 if (masked)
672 close(mask_fd);
673 G_fatal_error("avl_add unknown error");
674 }
675 }
676 }
677 }
678
679 if (area > 0) {
680 double alpha;
681 double pi;
682 double t;
683 double sum, sum2;
684
685 alpha = atof(par[0]);
686
687 array = G_malloc(m * sizeof(AVL_tableRow));
688 if (array == NULL) {
689 if (masked)
690 close(mask_fd);
691 G_fatal_error("malloc array failed");
692 }
693 tot = avl_to_array(albero, zero, array);
694 if (tot != m) {
695 G_warning(
696 "avl_to_array unexpected value. the result could be wrong");
697 res = RLI_ERRORE;
698 G_free(array);
699 goto free_exit;
700 }
701
702 /* calculate renyi index */
703 sum = 0;
704 sum2 = 0;
705 for (i = 0; i < m; i++) {
706 t = array[i].tot;
707 pi = t / area;
708 sum += pow(pi, alpha);
709 sum2 += pi;
710 }
711 G_free(array);
712
713 /* correction for numerical instability */
714 if (sum2 != 1)
715 sum += 1 - sum2;
716
717 if ((alpha < 1 && sum < 1) || (alpha > 1 && sum > 1)) {
718 G_warning("Renyi index calculation reached numerical instability. "
719 "This can happen with alpha close to 1. The result will "
720 "be set to zero.");
721 sum = 1;
722 }
723
724 *result = (1 / (1 - alpha)) * log(sum);
725 }
726 else
727 Rast_set_d_null_value(result, 1);
728
729 free_exit:
730 avl_destroy(albero);
731 if (masked) {
732 close(mask_fd);
733 G_free(mask_buf);
734 }
735
736 return res;
737 }