[GRASS-dev] r.univar: allow multiple rasters to be processed
Ivan Shmakov
ivan at theory.asu.ru
Wed Mar 5 04:07:05 EST 2008
>>>>> Hamish <hamish_b at yahoo.com> writes:
>> Is there some fundamental reason why r.univar has separate cases for
>> CELL/FCELL/DCELL types, rather than just working in DCELL
>> throughout?
> I can't remember with any certainty why I did it that way; it's been
> like that since the first CVS checkin.
Please consider the following patch.
diff --git a/raster/r.univar2/r.univar_main.c b/raster/r.univar2/r.univar_main.c
index 454df33..5674489 100644
--- a/raster/r.univar2/r.univar_main.c
+++ b/raster/r.univar2/r.univar_main.c
@@ -184,6 +184,15 @@ process_raster (univar_stat *stats, int fd,
int first = (stats->n < 1);
RASTER_MAP_TYPE map_type;
+ void *all_values
+ = ((! param.extended->answer) ? 0
+ : (map_type == DCELL_TYPE) ? stats->dcell_array
+ : (map_type == FCELL_TYPE) ? stats->fcell_array
+ : stats->cell_array);
+ size_t value_sz
+ = ((map_type == DCELL_TYPE) ? sizeof (DCELL)
+ : (map_type == FCELL_TYPE) ? sizeof (FCELL)
+ : sizeof (CELL));
unsigned int row;
void *raster_row;
@@ -206,73 +215,32 @@ process_raster (univar_stat *stats, int fd,
continue;
}
-
- if (map_type == CELL_TYPE) {
- const int val_i = *((CELL *) ptr);
-
- stats->sum += val_i;
- stats->sumsq += (double) val_i * val_i;
- stats->sum_abs += abs(val_i);
-
- if (param.extended->answer)
- stats->cell_array[stats->n] = val_i;
-
- if (first) {
- stats->max = val_i;
- stats->min = val_i;
- first = FALSE;
- }
- else {
- if (val_i > stats->max)
- stats->max = val_i;
- if (val_i < stats->min)
- stats->min = val_i;
- }
+ if (all_values) {
+ /* put the value into stats->XXXcell_array */
+ memcpy (all_values + stats->n * value_sz,
+ ptr, value_sz);
}
- else if (map_type == FCELL_TYPE) {
- const float val_f = *((FCELL *) ptr);
- stats->sum += val_f;
- stats->sumsq += (double) val_f * val_f;
- stats->sum_abs += fabs(val_f);
+ {
+ double val
+ = ((map_type == DCELL_TYPE) ? *((DCELL *) ptr)
+ : (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
+ : *((CELL *) ptr));
- if (param.extended->answer)
- stats->fcell_array[stats->n] = val_f;
+ stats->sum += val;
+ stats->sumsq += val * val;
+ stats->sum_abs += fabs (val);
if (first) {
- stats->max = val_f;
- stats->min = val_f;
+ stats->max = val;
+ stats->min = val;
first = FALSE;
- }
- else {
- if (val_f > stats->max)
- stats->max = val_f;
- if (val_f < stats->min)
- stats->min = val_f;
+ } else {
+ if (val > stats->max) stats->max = val;
+ if (val < stats->min) stats->min = val;
}
}
- else if (map_type == DCELL_TYPE) {
- const double val_d = *((DCELL *) ptr);
-
- stats->sum += val_d;
- stats->sumsq += val_d * val_d;
- stats->sum_abs += fabs(val_d);
- if (param.extended->answer)
- stats->dcell_array[stats->n] = val_d;
-
- if (first) {
- stats->max = val_d;
- stats->min = val_d;
- first = FALSE;
- }
- else {
- if (val_d > stats->max)
- stats->max = val_d;
- if (val_d < stats->min)
- stats->min = val_d;
- }
- }
ptr = G_incr_void_ptr(ptr, G_raster_size(map_type));
stats->n++;
}
More information about the grass-dev
mailing list