[GRASS-SVN] r59042 - grass/trunk/raster/r.li/r.li.pielou
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Feb 14 02:27:43 PST 2014
Author: mmetz
Date: 2014-02-14 02:27:42 -0800 (Fri, 14 Feb 2014)
New Revision: 59042
Modified:
grass/trunk/raster/r.li/r.li.pielou/pielou.c
Log:
r.li.pielou: sync to r.li.shannon
Modified: grass/trunk/raster/r.li/r.li.pielou/pielou.c
===================================================================
--- grass/trunk/raster/r.li/r.li.pielou/pielou.c 2014-02-14 10:27:03 UTC (rev 59041)
+++ grass/trunk/raster/r.li/r.li.pielou/pielou.c 2014-02-14 10:27:42 UTC (rev 59042)
@@ -35,8 +35,8 @@
/*This function is used to sort the values in the moving window */
static int cmp(const void *pa, const void *pb)
{
- int *p1 = (int *)pa;
- int *p2 = (int *)pb;
+ double *p1 = (double *)pa;
+ double *p2 = (double *)pb;
if (*p1 < *p2)
return -1;
@@ -123,13 +123,11 @@
int calculate(int fd, struct area_entry *ad, double *result)
{
-
CELL *buf;
CELL corrCell;
- CELL precCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int a = 0; /* a=0 if all cells are null */
@@ -137,7 +135,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -147,25 +144,29 @@
double logaritmo;
/* add to compute the number of class */
- long *array_copy;
+ double *array_copy;
int totNumClass = 1;
double logTotNumClass;
generic_cell cc;
-
generic_cell cc_passage; /*add to compute the number of class */
-
avl_tree albero = NULL;
-
AVL_table *array;
cc.t = CELL_TYPE;
+ if (ad->y + ad->rl > Rast_window_rows())
+ G_fatal_error("Region dimensions are wrong: n rows %d > %d",
+ ad->y + ad->rl, Rast_window_rows());
+ if (ad->x + ad->cl > Rast_window_cols())
+ G_fatal_error("Region dimensions are wrong; n cols %d > %d",
+ ad->x + ad->cl, Rast_window_cols());
+
/* open mask if needed */
if (ad->mask == 1) {
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
return RLI_ERRORE;
- mask_buf = G_malloc(ad->cl * sizeof(int));
+ mask_buf = G_malloc(Rast_window_cols() * sizeof(int));
if (mask_buf == NULL) {
G_fatal_error("malloc mask_buf failed");
return RLI_ERRORE;
@@ -173,15 +174,10 @@
masked = TRUE;
}
-
- Rast_set_c_null_value(&precCell, 1);
-
-
/*for each row */
for (j = 0; j < ad->rl; j++) {
buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
-
if (masked) {
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
G_fatal_error("mask read failed");
@@ -189,7 +185,6 @@
}
}
-
for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
area++;
corrCell = buf[i + ad->x];
@@ -199,170 +194,117 @@
area--;
}
- if (!(Rast_is_null_value(&corrCell, cc.t))) {
+ if (!(Rast_is_null_value(&corrCell, CELL_TYPE))) {
a = 1;
- if (Rast_is_null_value(&precCell, cc.t)) {
- precCell = corrCell;
- }
- if (corrCell != precCell) {
- if (albero == NULL) {
- cc.val.c = precCell;
- albero = avl_make(cc, totCorr);
+ if (albero == NULL) {
+ cc.val.c = corrCell;
+ albero = avl_make(cc, 1);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
+ if (albero == NULL) {
+ G_fatal_error("avl_make error");
+ return RLI_ERRORE;
+ }
+ m++;
+ }
+ else {
+ cc.val.c = corrCell;
+ ris = avl_add(&albero, cc, 1);
+ switch (ris) {
+ case AVL_ERR:
+ {
+ G_fatal_error("avl_add error");
return RLI_ERRORE;
}
- else
+ case AVL_ADD:
+ {
m++;
- }
- else {
- cc.val.c = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
+ break;
}
+ case AVL_PRES:
+ {
+ break;
+ }
+ default:
+ {
+ G_fatal_error("avl_add unknown error");
+ return RLI_ERRORE;
+ }
}
- totCorr = 1;
}
- else {
- totCorr++;
- }
- precCell = corrCell;
}
-
} /* end for */
} /* end for */
-
/*last closing */
- if (a != 0) {
- if (albero == NULL) {
- cc.val.c = precCell;
- albero = avl_make(cc, totCorr);
+ if (a != 0 && albero != NULL) {
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
+ array = G_malloc(m * sizeof(AVL_tableRow *));
+ if (array == NULL) {
+ G_fatal_error("malloc array failed");
+ return RLI_ERRORE;
}
- else {
- cc.val.c = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
+ tot = avl_to_array(albero, zero, array);
+
+ if (tot != m) {
+ G_warning("avl_to_array unaspected value. the result could be wrong");
+ return RLI_ERRORE;
}
- }
- array = G_malloc(m * sizeof(AVL_tableRow));
- if (array == NULL) {
- G_fatal_error("malloc array failed");
- return RLI_ERRORE;
- }
- tot = avl_to_array(albero, zero, array);
+ /* calculate summary */
+ for (i = 0; i < m; i++) {
+ t = (double)array[i]->tot;
+ percentuale = (double)(t / area);
+ logaritmo = (double)log(percentuale);
+ somma = somma + (percentuale * logaritmo);
+ }
- if (tot != m) {
- G_warning("avl_to_array unaspected value. the result could be wrong");
- return RLI_ERRORE;
- }
+ array_copy = G_malloc(m * sizeof(double));
+ for (i = 0; i < m; i++) {
+ cc_passage = array[i]->k;
+ array_copy[i] = cc_passage.val.c;
+ }
- array_copy = G_malloc(m * sizeof(AVL_tableRow));
- for (i = 0; i < m; i++) {
- cc_passage = array[i]->k;
- array_copy[i] = cc_passage.val.c;
- }
+ qsort(array_copy, m, sizeof(double), cmp);
- qsort(array_copy, m, sizeof(int), cmp);
-
- for (i = 1; i < m; i++) {
- if (array_copy[i - 1] != array_copy[i]) {
- totNumClass++;
+ for (i = 1; i < m; i++) {
+ if (array_copy[i - 1] != array_copy[i]) {
+ totNumClass++;
+ }
}
- }
- /* calculate summary */
- for (i = 0; i < m; i++) {
- t = (double)array[i]->tot;
- percentuale = (double)(t / area);
- logaritmo = (double)log(percentuale);
- somma = somma + (percentuale * logaritmo);
- }
+ G_free(array);
+ G_free(array_copy);
- logTotNumClass = (double)log(totNumClass);
- /*if a is 0, that is all cell are null, i put index=-1 */
- if (a != 0) {
+ logTotNumClass = (double)log(totNumClass);
indice = ((-1) * somma) / logTotNumClass;
+
+ G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+ somma, logTotNumClass, indice);
}
- else {
+ else
+ /*if a is 0, that is all cell are null, i put index=-1 */
indice = (double)(-1);
- }
- if (isnan(indice) || isinf(indice)) {
- indice = -1;
- }
- G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
- somma, logTotNumClass, indice);
-
*result = indice;
- G_free(array);
if (masked) {
+ close(mask_fd);
G_free(mask_buf);
}
+ avl_destroy(albero);
return RLI_OK;
}
-
int calculateD(int fd, struct area_entry *ad, double *result)
{
-
DCELL *buf;
DCELL corrCell;
- DCELL precCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int a = 0; /* a=0 if all cells are null */
@@ -370,7 +312,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -379,20 +320,25 @@
double t;
double logaritmo;
- /*add to compute the number of class */
- long *array_copy;
+ /* add to compute the number of class */
+ double *array_copy;
int totNumClass = 1;
double logTotNumClass;
+ generic_cell cc;
+ generic_cell cc_passage; /*add to compute the number of class */
avl_tree albero = NULL;
-
AVL_table *array;
- generic_cell cc;
- generic_cell cc_passage; /*add to compute the number of class */
-
cc.t = DCELL_TYPE;
+ if (ad->y + ad->rl > Rast_window_rows())
+ G_fatal_error("Region dimensions are wrong: n rows %d > %d",
+ ad->y + ad->rl, Rast_window_rows());
+ if (ad->x + ad->cl > Rast_window_cols())
+ G_fatal_error("Region dimensions are wrong; n cols %d > %d",
+ ad->x + ad->cl, Rast_window_cols());
+
/* open mask if needed */
if (ad->mask == 1) {
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
@@ -405,14 +351,10 @@
masked = TRUE;
}
-
- Rast_set_d_null_value(&precCell, 1);
-
/*for each row */
for (j = 0; j < ad->rl; j++) {
buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-
if (masked) {
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
G_fatal_error("mask read failed");
@@ -420,7 +362,6 @@
}
}
-
for (i = 0; i < ad->cl; i++) { /* for each dcell in the row */
area++;
corrCell = buf[i + ad->x];
@@ -432,98 +373,48 @@
if (!(Rast_is_null_value(&corrCell, DCELL_TYPE))) {
a = 1;
- if (Rast_is_null_value(&precCell, DCELL_TYPE)) {
- precCell = corrCell;
- }
- if (corrCell != precCell) {
+
+ if (albero == NULL) {
+ cc.val.dc = corrCell;
+ albero = avl_make(cc, 1);
if (albero == NULL) {
- cc.val.dc = precCell;
- albero = avl_make(cc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
+ G_fatal_error("avl_make error");
+ return RLI_ERRORE;
+ }
+ m++;
+ }
+ else {
+ cc.val.dc = corrCell;
+ ris = avl_add(&albero, cc, 1);
+ switch (ris) {
+ case AVL_ERR:
+ {
+ G_fatal_error("avl_add error");
return RLI_ERRORE;
}
- m++;
- }
- else {
- cc.val.dc = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
+ case AVL_ADD:
+ {
+ m++;
+ break;
}
+ case AVL_PRES:
+ {
+ break;
+ }
+ default:
+ {
+ G_fatal_error("avl_add unknown error");
+ return RLI_ERRORE;
+ }
}
- totCorr = 1;
}
- else {
- totCorr++;
- }
- precCell = corrCell;
}
-
} /*close for */
} /*close for */
- if (masked) {
- G_free(mask_buf);
- }
-
/*last closing */
- if (a != 0) {
- if (albero == NULL) {
- cc.val.dc = precCell;
- albero = avl_make(cc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
- }
- else {
- cc.val.fc = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
- }
-
-
- array = G_malloc(m * sizeof(AVL_tableRow));
+ if (a != 0 && albero != NULL) {
+ array = G_malloc(m * sizeof(AVL_tableRow *));
if (array == NULL) {
G_fatal_error("malloc array failed");
return RLI_ERRORE;
@@ -536,14 +427,21 @@
return RLI_ERRORE;
}
- array_copy = G_malloc(m * sizeof(AVL_tableRow));
+ /* calculate summary */
+ for (i = 0; i < m; i++) {
+ t = (double)array[i]->tot;
+ percentuale = (double)(t / area);
+ logaritmo = (double)log(percentuale);
+ somma = somma + (percentuale * logaritmo);
+ }
+ array_copy = G_malloc(m * sizeof(double));
for (i = 0; i < m; i++) {
cc_passage = array[i]->k;
array_copy[i] = cc_passage.val.c;
}
- qsort(array_copy, m, sizeof(int), cmp);
+ qsort(array_copy, m, sizeof(double), cmp);
for (i = 1; i < m; i++) {
if (array_copy[i - 1] != array_copy[i]) {
@@ -551,47 +449,38 @@
}
}
-
- /* calculate summary */
- for (i = 0; i < m; i++) {
- t = (double)array[i]->tot;
- percentuale = (double)(t / area);
- logaritmo = (double)log(percentuale);
- somma = somma + (percentuale * logaritmo);
- }
-
G_free(array);
+ G_free(array_copy);
logTotNumClass = (double)log(totNumClass);
+ indice = ((-1) * somma) / logTotNumClass;
- indice = (-1) * somma / logTotNumClass;
+ G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+ somma, logTotNumClass, indice);
}
- else {
+ else
/*if a is 0, that is all cell are null, i put index=-1 */
- indice = ((double)(-1));
- }
+ indice = (double)(-1);
- if (isnan(indice) || isinf(indice)) {
- indice = -1;
- }
- G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
- somma, logTotNumClass, indice);
-
*result = indice;
+ if (masked) {
+ close(mask_fd);
+ G_free(mask_buf);
+ }
+ avl_destroy(albero);
+
return RLI_OK;
}
int calculateF(int fd, struct area_entry *ad, double *result)
{
-
FCELL *buf;
FCELL corrCell;
- FCELL precCell;
int i, j;
- int mask_fd = -1, *mask_buf;
+ int mask_fd = -1, *mask_buf = NULL;
int ris = 0;
int masked = FALSE;
int a = 0; /* a=0 if all cells are null */
@@ -599,7 +488,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -607,19 +495,26 @@
double area = 0;
double t;
double logaritmo;
- long *array_copy;
+
+ /* add to compute the number of class */
+ double *array_copy;
int totNumClass = 1;
double logTotNumClass;
+ generic_cell cc;
+ generic_cell cc_passage; /*add to compute the number of class */
avl_tree albero = NULL;
-
AVL_table *array;
- generic_cell cc;
- generic_cell cc_passage;
-
cc.t = FCELL_TYPE;
+ if (ad->y + ad->rl > Rast_window_rows())
+ G_fatal_error("Region dimensions are wrong: n rows %d > %d",
+ ad->y + ad->rl, Rast_window_rows());
+ if (ad->x + ad->cl > Rast_window_cols())
+ G_fatal_error("Region dimensions are wrong; n cols %d > %d",
+ ad->x + ad->cl, Rast_window_cols());
+
/* open mask if needed */
if (ad->mask == 1) {
if ((mask_fd = open(ad->mask_name, O_RDONLY, 0755)) < 0)
@@ -632,15 +527,10 @@
masked = TRUE;
}
-
- Rast_set_f_null_value(&precCell, 1);
-
-
/*for each row */
for (j = 0; j < ad->rl; j++) {
buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-
if (masked) {
if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
G_fatal_error("mask read failed");
@@ -658,152 +548,101 @@
if (!(Rast_is_null_value(&corrCell, FCELL_TYPE))) {
a = 1;
- if (Rast_is_null_value(&precCell, FCELL_TYPE)) {
- precCell = corrCell;
- }
- if (corrCell != precCell) {
+
+ if (albero == NULL) {
+ cc.val.fc = corrCell;
+ albero = avl_make(cc, 1);
if (albero == NULL) {
- cc.val.fc = precCell;
- albero = avl_make(cc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
+ G_fatal_error("avl_make error");
+ return RLI_ERRORE;
+ }
+ m++;
+ }
+ else {
+ cc.val.fc = corrCell;
+ ris = avl_add(&albero, cc, 1);
+ switch (ris) {
+ case AVL_ERR:
+ {
+ G_fatal_error("avl_add error");
return RLI_ERRORE;
}
- m++;
- }
- else {
- cc.val.fc = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
+ case AVL_ADD:
+ {
+ m++;
+ break;
}
+ case AVL_PRES:
+ {
+ break;
+ }
+ default:
+ {
+ G_fatal_error("avl_add unknown error");
+ return RLI_ERRORE;
+ }
}
- totCorr = 1;
}
- else {
- totCorr++;
- }
-
- precCell = corrCell;
}
-
}
}
-
/*last closing */
- if (a != 0) {
- if (albero == NULL) {
- cc.val.fc = precCell;
- albero = avl_make(cc, totCorr);
- if (albero == NULL) {
- G_fatal_error("avl_make error");
- return RLI_ERRORE;
- }
- m++;
+ if (a != 0 && albero != NULL) {
+ array = G_malloc(m * sizeof(AVL_tableRow));
+ if (array == NULL) {
+ G_fatal_error("malloc array failed");
+ return RLI_ERRORE;
}
- else {
- cc.val.fc = precCell;
- ris = avl_add(&albero, cc, totCorr);
- switch (ris) {
- case AVL_ERR:
- {
- G_fatal_error("avl_add error");
- return RLI_ERRORE;
- }
- case AVL_ADD:
- {
- m++;
- break;
- }
- case AVL_PRES:
- {
- break;
- }
- default:
- {
- G_fatal_error("avl_add unknown error");
- return RLI_ERRORE;
- }
- }
+ tot = avl_to_array(albero, zero, array);
+
+ if (tot != m) {
+ G_warning("avl_to_array unaspected value. the result could be wrong");
+ return RLI_ERRORE;
}
- }
- array = G_malloc(m * sizeof(AVL_tableRow));
- if (array == NULL) {
- G_fatal_error("malloc array failed");
- return RLI_ERRORE;
- }
- tot = avl_to_array(albero, zero, array);
+ /* calculate summary */
+ for (i = 0; i < m; i++) {
+ t = (double)array[i]->tot;
+ percentuale = (double)(t / area);
+ logaritmo = (double)log(percentuale);
+ somma = somma + (percentuale * logaritmo);
+ }
- if (tot != m) {
- G_warning("avl_to_array unaspected value. the result could be wrong");
- return RLI_ERRORE;
- }
+ array_copy = G_malloc(m * sizeof(double));
+ for (i = 0; i < m; i++) {
+ cc_passage = array[i]->k;
+ array_copy[i] = cc_passage.val.c;
+ }
- array_copy = G_malloc(m * sizeof(AVL_tableRow));
+ qsort(array_copy, m, sizeof(double), cmp);
- for (i = 0; i < m; i++) {
- cc_passage = array[i]->k;
- array_copy[i] = cc_passage.val.c;
- }
-
- qsort(array_copy, m, sizeof(int), cmp);
-
- for (i = 1; i < m; i++) {
- if (array_copy[i - 1] != array_copy[i]) {
- totNumClass++;
+ for (i = 1; i < m; i++) {
+ if (array_copy[i - 1] != array_copy[i]) {
+ totNumClass++;
+ }
}
- }
- /* calculate summary */
- for (i = 0; i < m; i++) {
- t = (double)array[i]->tot;
- percentuale = (double)(t / area);
- logaritmo = (double)log(percentuale);
- somma = somma + (percentuale * logaritmo);
- }
+ G_free(array);
+ G_free(array_copy);
- logTotNumClass = (double)log(totNumClass);
-
-
- /*if a is 0, that is all cell are null, i put index=-1 */
- if (a != 0) {
+ logTotNumClass = (double)log(totNumClass);
indice = ((-1) * somma) / logTotNumClass;
+
+ G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
+ somma, logTotNumClass, indice);
}
- else {
+ else
+ /*if a is 0, that is all cell are null, i put index=-1 */
indice = (double)(-1);
- }
- if (isnan(indice) || isinf(indice)) {
- indice = -1;
- }
- G_debug(1, "somma: %g, number of logTotNumClass: %g, valore finale: %g",
- somma, logTotNumClass, indice);
-
*result = indice;
- G_free(array);
if (masked) {
+ close(mask_fd);
G_free(mask_buf);
}
+ avl_destroy(albero);
+
return RLI_OK;
}
More information about the grass-commit
mailing list