[GRASS-SVN] r59041 - grass/trunk/raster/r.li/r.li.shannon
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Feb 14 02:27:04 PST 2014
Author: mmetz
Date: 2014-02-14 02:27:03 -0800 (Fri, 14 Feb 2014)
New Revision: 59041
Modified:
grass/trunk/raster/r.li/r.li.shannon/shannon.c
Log:
r.li.shannon: simplify algorithm
Modified: grass/trunk/raster/r.li/r.li.shannon/shannon.c
===================================================================
--- grass/trunk/raster/r.li/r.li.shannon/shannon.c 2014-02-14 10:26:22 UTC (rev 59040)
+++ grass/trunk/raster/r.li/r.li.shannon/shannon.c 2014-02-14 10:27:03 UTC (rev 59041)
@@ -104,13 +104,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 */
@@ -118,7 +116,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -128,19 +125,23 @@
double logaritmo;
generic_cell cc;
-
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;
@@ -148,15 +149,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");
@@ -164,7 +160,6 @@
}
}
-
for (i = 0; i < ad->cl; i++) { /* for each cell in the row */
area++;
corrCell = buf[i + ad->x];
@@ -176,149 +171,96 @@
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;
}
- 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;
- }
+ 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;
}
-
} /* 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;
- }
+ G_free(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 a is 0, that is all cell are null, i put index=-1 */
- if (a != 0)
indice = (-1) * somma;
+ }
else
+ /*if a is 0, that is all cell are null, i put index=-1 */
indice = (double)(-1);
*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 */
@@ -326,7 +268,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -335,14 +276,19 @@
double t;
double logaritmo;
+ generic_cell cc;
avl_tree albero = NULL;
-
AVL_table *array;
- generic_cell cc;
-
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)
@@ -355,14 +301,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");
@@ -370,7 +312,6 @@
}
}
-
for (i = 0; i < ad->cl; i++) { /* for each dcell in the row */
area++;
corrCell = buf[i + ad->x];
@@ -382,98 +323,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;
@@ -496,32 +387,31 @@
G_free(array);
-
indice = (-1) * somma;
}
else
/*if a is 0, that is all cell are null, i put index=-1 */
indice = (double)(-1);
-
*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 */
@@ -529,7 +419,6 @@
long m = 0;
long tot = 0;
long zero = 0;
- long totCorr = 0;
double indice = 0;
double somma = 0;
@@ -538,14 +427,19 @@
double t;
double logaritmo;
+ generic_cell cc;
avl_tree albero = NULL;
-
AVL_table *array;
- generic_cell cc;
-
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)
@@ -558,15 +452,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");
@@ -574,7 +463,6 @@
}
}
-
for (i = 0; i < ad->cl; i++) { /* for each fcell in the row */
area++;
corrCell = buf[i + ad->x];
@@ -585,128 +473,82 @@
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;
- }
+ G_free(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 a is 0, that is all cell are null, i put index=-1 */
- if (a != 0)
indice = (-1) * somma;
+ }
else
+ /*if a is 0, that is all cell are null, i put index=-1 */
indice = (double)(-1);
-
*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