2 #include <grass/glocale.h>
6 #define LIST struct Histogram_list
8 static FILE *fopen_histogram_new(
const char *);
9 static int cmp(
const void *,
const void *);
10 static int cmp_count(
const void *,
const void *);
25 histogram->list =
NULL;
47 struct Histogram *histogram)
57 G_warning(_(
"Histogram for [%s in %s] missing (run r.support)"), name,
65 G_warning(_(
"Can't read histogram for [%s in %s]"), name, mapset);
70 while (fgets(buf,
sizeof buf, fd)) {
71 if (sscanf(buf,
"%ld:%ld", &cat, &count) != 2) {
74 G_warning(_(
"Invalid histogram file for [%s in %s]"), name,
83 if (histogram->num == 0) {
84 G_warning(_(
"Invalid histogram file for [%s in %s]"), name, mapset);
111 fd = fopen_histogram_new(name);
115 list = histogram->list;
116 for (n = 0; n < histogram->num; n++) {
118 fprintf(fd,
"%ld:%ld\n", (
long)list[n].
cat, list[n].count);
141 fd = fopen_histogram_new(name);
148 fprintf(fd,
"%ld:%ld\n", (
long)cat, count);
190 return histogram->num;
203 if (n < 0 || n >= histogram->num)
206 return histogram->list[
n].cat;
220 if (n < 0 || n >= histogram->num)
223 return histogram->list[
n].count;
236 if (histogram->num > 0)
239 histogram->list =
NULL;
259 if ((n = histogram->num) <= 1)
262 list = histogram->list;
265 for (a = 1; a <
n; a++)
266 if (list[a - 1].
cat >= list[a].
cat)
272 qsort(list, n,
sizeof(
LIST), &cmp);
275 for (a = 0, b = 1; b <
n; b++) {
276 if (list[a].cat != list[b].cat) {
278 list[a].count = list[
b].count;
279 list[a].cat = list[
b].cat;
282 list[a].count += list[
b].count;
285 histogram->num = a + 1;
291 static int cmp(
const void *aa,
const void *bb)
293 const LIST *a = aa, *
b = bb;
319 if ((n = histogram->num) <= 1)
322 list = histogram->list;
325 qsort(list, n,
sizeof(
LIST), &cmp_count);
331 static int cmp_count(
const void *aa,
const void *bb)
333 const LIST *a = aa, *b = bb;
335 if (a->count < b->count)
338 if (a->count > b->count)
350 static FILE *fopen_histogram_new(
const char *
name)
356 G_warning(_(
"can't create histogram for [%s in %s]"), name,
393 for (i = 0; i < histogram->num; i++) {
394 if (histogram->list[i].cat == cat) {
395 histogram->list[i].count +=
count;
419 for (i = 0; i < histogram->num; i++) {
420 if (histogram->list[i].cat == cat) {
421 histogram->list[i].count =
count;
443 (
LIST *) G_realloc((
char *)histogram->list,
444 histogram->num *
sizeof(
LIST));
445 histogram->list[histogram->num - 1].cat =
cat;
446 histogram->list[histogram->num - 1].count =
count;
462 for (i = 0; i < histogram->num; i++)
463 histogram->list[i].count = 0;
char * G_mapset(void)
current mapset name
void G_free(void *buf)
Free allocated memory.
int G_read_histogram(const char *name, const char *mapset, struct Histogram *histogram)
read the histogram information
int G_get_histogram_num(const struct Histogram *histogram)
Sorts the histogram in ascending order by counts then category.
int G_zero_histogram(struct Histogram *histogram)
Zero out histogram struct.
int G_set_histogram(CELL cat, long count, struct Histogram *histogram)
sets the histogram value for cat to count
char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
int G_write_histogram_cs(const char *name, struct Cell_stats *statf)
Writes the histogram based on cell statistics to file.
int G_free_histogram(struct Histogram *histogram)
Frees memory allocated for the histogram.
CELL G_get_histogram_cat(int n, const struct Histogram *histogram)
Returns cat for the nth element in the histogram.
int G_next_cell_stat(CELL *cat, long *count, struct Cell_stats *s)
retrieve sorted cell stats
int G_remove_histogram(const char *name)
Removes the histogram.
int G_init_histogram(struct Histogram *histogram)
initializes the histogram structure
int G_add_histogram(CELL cat, long count, struct Histogram *histogram)
adds count to the histogram value for cat
int G_sort_histogram_by_count(struct Histogram *histogram)
Sorts the histogram by counts.
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
int G_sort_histogram(struct Histogram *histogram)
Sorts the histogram.
int G_extend_histogram(CELL cat, long count, struct Histogram *histogram)
Extends histogram struct to accomodate a new value.
int G_rewind_cell_stats(struct Cell_stats *s)
reset/rewind cell stats
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database file
long G_get_histogram_count(int n, const struct Histogram *histogram)
Returns count for the nth element in the histogram.
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
int G_write_histogram(const char *name, const struct Histogram *histogram)
Writes the histogram information.
int G_make_histogram_cs(struct Cell_stats *statf, struct Histogram *histogram)
Creates histogram based on cell statistics.