[GRASS-SVN] r59046 - grass/trunk/raster/r.li/r.li.dominance

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Feb 14 05:09:20 PST 2014


Author: mmetz
Date: 2014-02-14 05:09:19 -0800 (Fri, 14 Feb 2014)
New Revision: 59046

Modified:
   grass/trunk/raster/r.li/r.li.dominance/dominance.c
Log:
r.li.dominance: sync to r.li.shannon

Modified: grass/trunk/raster/r.li/r.li.dominance/dominance.c
===================================================================
--- grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-14 13:08:30 UTC (rev 59045)
+++ grass/trunk/raster/r.li/r.li.dominance/dominance.c	2014-02-14 13:09:19 UTC (rev 59046)
@@ -22,6 +22,8 @@
 #include "../r.li.daemon/avl.h"
 #include "../r.li.daemon/daemon.h"
 
+/* template is shannon */
+
 int calculate(int fd, struct area_entry *ad, double *result);
 int calculateD(int fd, struct area_entry *ad, double *result);
 int calculateF(int fd, struct area_entry *ad, double *result);
@@ -55,20 +57,14 @@
 
     return calculateIndex(conf->answer, dominance, NULL, raster->answer,
 			  output->answer);
-
 }
 
 
 int dominance(int fd, char **par, struct area_entry *ad, double *result)
 {
-
     int ris = RLI_OK;
     double indice = 0;
-    struct Cell_head hd;
 
-
-    Rast_get_cellhd(ad->raster, "", &hd);
-
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
@@ -90,28 +86,24 @@
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
 	}
-
     }
 
     if (ris != RLI_OK)
-	   return RLI_ERRORE;
+	return RLI_ERRORE;
 
     *result = indice;
 
     return RLI_OK;
-
 }
 
 
 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 */
@@ -119,7 +111,6 @@
     long m = 0;
     long tot = 0;
     long zero = 0;
-    long totCorr = 0;
 
     double indice = 0;
     double somma = 0;
@@ -128,18 +119,24 @@
     double t;
     double logaritmo;
 
+    generic_cell cc;
     avl_tree albero = NULL;
     AVL_table *array;
 
-    generic_cell cc;
-
     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;
@@ -147,9 +144,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);
 
-    for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
 		G_fatal_error("mask read failed");
@@ -157,7 +155,6 @@
 	    }
 	}
 
-	buf = RLI_get_cell_raster_row(fd, j + ad->y, ad);
 	for (i = 0; i < ad->cl; i++) {	/* for each cell in the row */
 	    area++;
 	    corrCell = buf[i + ad->x];
@@ -169,97 +166,50 @@
 
 	    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) {
-			    G_fatal_error("avl_make error");
+		if (albero == NULL) {
+		    cc.val.c = corrCell;
+		    albero = avl_make(cc, 1);
+
+		    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;
 			}
-			/* TODO missing?: else  - see r.li.simpson/simpson.c */
-			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++;
-	}
-	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;
-		}
-	    }
-	}
-
-
-	array = G_malloc(m * sizeof(AVL_tableRow));
+	array = G_malloc(m * sizeof(AVL_tableRow *));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
 	    return RLI_ERRORE;
@@ -267,8 +217,7 @@
 	tot = avl_to_array(albero, zero, array);
 
 	if (tot != m) {
-	    G_warning
-		("avl_to_array unaspected value. the result could be wrong");
+	    G_warning("avl_to_array unaspected value. the result could be wrong");
 	    return RLI_ERRORE;
 	}
 
@@ -280,21 +229,25 @@
 	    somma = somma + (percentuale * logaritmo);
 	}
 
+	G_free(array);
+
 	if (m != 0)
 	    indice = log((double)m) + somma;
 	else
 	    indice = 0;
-	G_free(array);
     }
-    else			/*if a is 0, that is all cell are null, i put index=-1 */
+    else
+	/*if a is 0, that is all cell are null, i put index=-1 */
 	indice = (double)(-1);
 
+    *result = indice;
 
-    if (masked)
+    if (masked) {
+	close(mask_fd);
 	G_free(mask_buf);
+    }
+    avl_destroy(albero);
 
-    *result = indice;
-
     return RLI_OK;
 }
 
@@ -303,10 +256,9 @@
 {
     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 */
@@ -314,7 +266,6 @@
     long m = 0;
     long tot = 0;
     long zero = 0;
-    long totCorr = 0;
 
     double indice = 0;
     double somma = 0;
@@ -323,14 +274,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)
@@ -343,9 +299,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);
 
-    for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
 		G_fatal_error("mask read failed");
@@ -353,8 +310,6 @@
 	    }
 	}
 
-	buf = RLI_get_dcell_raster_row(fd, j + ad->y, ad);
-
 	for (i = 0; i < ad->cl; i++) {	/* for each dcell in the row */
 	    area++;
 	    corrCell = buf[i + ad->x];
@@ -366,98 +321,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));
+    /*last closing */
+    if (a != 0 && albero != NULL) {
+	array = G_malloc(m * sizeof(AVL_tableRow *));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
 	    return RLI_ERRORE;
@@ -489,9 +394,13 @@
 	/*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;
 }
@@ -501,10 +410,9 @@
 {
     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 */
@@ -512,7 +420,6 @@
     long m = 0;
     long tot = 0;
     long zero = 0;
-    long totCorr = 0;
 
     double indice = 0;
     double somma = 0;
@@ -521,14 +428,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)
@@ -541,11 +453,10 @@
 	masked = TRUE;
     }
 
+    /*for each row */
+    for (j = 0; j < ad->rl; j++) {
+	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
 
-    Rast_set_f_null_value(&precCell, 1);
-
-
-    for (j = 0; j < ad->rl; j++) {	/* for each row */
 	if (masked) {
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0) {
 		G_fatal_error("mask read failed");
@@ -553,13 +464,9 @@
 	    }
 	}
 
-	buf = RLI_get_fcell_raster_row(fd, j + ad->y, ad);
-
 	for (i = 0; i < ad->cl; i++) {	/* for each fcell in the row */
-
 	    area++;
 	    corrCell = buf[i + ad->x];
-
 	    if (masked && mask_buf[i + ad->x] == 0) {
 		Rast_set_f_null_value(&corrCell, 1);
 		area--;
@@ -567,94 +474,47 @@
 
 	    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++;
-	}
-	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;
-		}
-	    }
-	}
-
-
+    if (a != 0 && albero != NULL) {
 	array = G_malloc(m * sizeof(AVL_tableRow));
 	if (array == NULL) {
 	    G_fatal_error("malloc array failed");
@@ -663,8 +523,7 @@
 	tot = avl_to_array(albero, zero, array);
 
 	if (tot != m) {
-	    G_warning
-		("avl_to_array unaspected value. the result could be wrong");
+	    G_warning("avl_to_array unaspected value. the result could be wrong");
 	    return RLI_ERRORE;
 	}
 
@@ -676,22 +535,24 @@
 	    somma = somma + (percentuale * logaritmo);
 	}
 
+	G_free(array);
+
 	if (m != 0)
 	    indice = log((double)m) + somma;
 	else
 	    indice = 0;
-
-	G_free(array);
     }
-    else			/*if a is 0, that is all cell are null, i put index=-1 */
-	  indice = (double)(-1);
+    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);
 
-    *result = indice;
-
     return RLI_OK;
 }



More information about the grass-commit mailing list