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