int o_divr(const char *basemap, const char *covermap, const char *outputmap, int usecats, struct Categories *cats) { struct Popen stats_child, reclass_child; FILE *stats_fp, *reclass_fp; int first; long basecat, covercat, catb, catc; double area; stats_fp = run_stats(&stats_child, basemap, covermap, "-an"); reclass_fp = run_reclass(&reclass_child, basemap, outputmap); first = 1; while (read_stats(stats_fp, &basecat, &covercat, &area)) { if (first) { first = 0; catb = basecat; catc = 0; } if (basecat != catb) { write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats); catb = basecat; catc = 0; } catc++; } if (!first) write_reclass(reclass_fp, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats); G_popen_close(&stats_child); G_popen_close(&reclass_child); return 0; }
int vcf_tool_stats(int argc, char *argv[], const char *configuration_file) { /* ****************************** * Modifiable options * * ******************************/ shared_options_t *shared_options = new_shared_cli_options(); stats_options_t *stats_options = new_stats_cli_options(); // If no arguments or only --help are provided, show usage void **argtable; if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { argtable = merge_stats_options(stats_options, shared_options, arg_end(stats_options->num_options + shared_options->num_options)); show_usage("hpg-var-vcf stats", argtable, stats_options->num_options + shared_options->num_options); arg_freetable(argtable, stats_options->num_options + shared_options->num_options - 10); return 0; } /* ****************************** * Execution steps * * ******************************/ // Step 1: read options from configuration file int config_errors = read_shared_configuration(configuration_file, shared_options); config_errors &= read_stats_configuration(configuration_file, stats_options, shared_options); if (config_errors) { LOG_FATAL("Configuration file read with errors\n"); return CANT_READ_CONFIG_FILE; } // Step 2: parse command-line options argtable = parse_stats_options(argc, argv, stats_options, shared_options); // Step 3: check that all options are set with valid values // Mandatory that couldn't be read from the config file must be set via command-line // If not, return error code! int check_vcf_tools_opts = verify_stats_options(stats_options, shared_options); if (check_vcf_tools_opts > 0) { return check_vcf_tools_opts; } // Step 4: Create XXX_options_data_t structures from valid XXX_options_t shared_options_data_t *shared_options_data = new_shared_options_data(shared_options); stats_options_data_t *options_data = new_stats_options_data(stats_options); // Step 5: Perform the requested task int result = run_stats(shared_options_data, options_data); free_stats_options_data(options_data); free_shared_options_data(shared_options_data); arg_freetable(argtable, stats_options->num_options + shared_options->num_options - 10); return 0; }
int o_mode(const char *basemap, const char *covermap, const char *outputmap, int usecats, struct Categories *cats) { struct Popen stats_child, reclass_child; FILE *stats, *reclass; int first; long basecat, covercat, catb, catc; double value, max; stats = run_stats(&stats_child, basemap, covermap, "-an"); reclass = run_reclass(&reclass_child, basemap, outputmap); first = 1; while (read_stats(stats, &basecat, &covercat, &value)) { if (first) { first = 0; catb = basecat; catc = covercat; max = value; } if (basecat != catb) { write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats); catb = basecat; catc = covercat; max = value; } if (value > max) { catc = covercat; max = value; } } if (first) { catb = catc = 0; } write_reclass(reclass, catb, catc, Rast_get_c_cat((CELL *) &catc, cats), usecats); G_popen_close(&stats_child); G_popen_close(&reclass_child); return 0; }
int o_sum(const char *basemap, const char *covermap, const char *outputmap, int usecats, struct Categories *cats) { long catb, basecat, covercat; double x, area, sum1; int stat; struct Popen stats_child, reclass_child; FILE *stats, *reclass; stats = run_stats(&stats_child, basemap, covermap, "-cn"); reclass = run_reclass(&reclass_child, basemap, outputmap); sum_out(reclass, 0L, 0.0); /* force at least one reclass rule */ catb = 0; sum1 = 0.0; while (fscanf(stats, "%ld %ld %lf", &basecat, &covercat, &area) == 3) { if (catb != basecat) { sum_out(reclass, catb, sum1); sum1 = 0.0; catb = basecat; } if (usecats) sscanf(Rast_get_c_cat((CELL *) &covercat, cats), "%lf", &x); else x = covercat; sum1 += x * area; /* fprintf(stderr,"sum: %d\n",(int)sum1); */ } sum_out(reclass, basecat, sum1); G_popen_close(&stats_child); G_popen_close(&reclass_child); return stat; }
/* linked list of stats */ void get_stats(const char *mapname, struct stat_list *dist_stats, int nsteps, int map_type) { char buf[1024]; /* input buffer for reading stats */ int done = FALSE; char *tempfile; /* temp file name */ FILE *fd; /* temp file pointer */ /* int is_fp; struct FPRange fp_range; */ long int cat; /* a category value */ long int stat; /* a category stat value */ struct stat_node *ptr = NULL; int first; /* write stats to a temp file */ tempfile = G_tempfile(); /* is_fp = Rast_map_is_fp(mapname, ""); if (is_fp) { if (Rast_read_fp_range(mapname, "", &fp_range) <= 0) G_fatal_error("Can't read frange file"); } */ run_stats(mapname, nsteps, tempfile, map_type); /* open temp file and read the stats into a linked list */ fd = fopen(tempfile, "r"); if (fd == NULL) { perror("opening r.stats output file"); G_fatal_error("unable to continue."); } dist_stats->ptr = NULL; dist_stats->count = 0; dist_stats->sumstat = 0; first = TRUE; while (!done) { if (fgets(buf, sizeof(buf), fd) != NULL) { /* WARNING!!!!!! * this will be very wrong if type!=COUNT * since the stat prodcued by r.stats will be a floating point value * possibly less than 1 (shapiro) */ if (sscanf(buf, "* %ld", &stat) == 1) { dist_stats->null_stat = stat; /* if (stat > dist_stats->maxstat && nodata) dist_stats->maxstat = stat; if (stat < dist_stats->minstat && nodata) dist_stats->minstat = stat; if (nodata) dist_stats->sumstat += stat; */ } else if (sscanf(buf, "%ld %ld", &cat, &stat) == 2) { /* count stats */ dist_stats->count++; /* sum stats */ dist_stats->sumstat += stat; /* a max or a min stat? */ if (first) { dist_stats->maxstat = stat; dist_stats->minstat = stat; dist_stats->maxcat = cat; dist_stats->mincat = cat; first = FALSE; } if (stat > dist_stats->maxstat) dist_stats->maxstat = stat; if (stat < dist_stats->minstat) dist_stats->minstat = stat; /* a max or a min cat? */ if (cat > dist_stats->maxcat) dist_stats->maxcat = cat; if (cat < dist_stats->mincat) dist_stats->mincat = cat; /* put it in the list */ if (dist_stats->ptr == NULL) { /* first in list */ dist_stats->ptr = (struct stat_node *) G_malloc(sizeof(struct stat_node)); dist_stats->ptr->cat = cat; dist_stats->ptr->stat = stat; dist_stats->ptr->next = NULL; ptr = dist_stats->ptr; } else { ptr->next = (struct stat_node *) G_malloc(sizeof(struct stat_node)); ptr->next->cat = cat; ptr->next->stat = stat; ptr->next->next = NULL; /* mod: shapiro */ ptr = ptr->next; } } } else done = TRUE; } fclose(fd); unlink(tempfile); }
int o_sdev(const char *basemap, const char *covermap, const char *outputmap, int usecats, struct Categories *cats) { struct Popen stats_child, reclass_child; FILE *stats, *reclass; int first, mem, i, count; long basecat, covercat, catb, catc; double value, sdev, x; double *tab; mem = MEM * sizeof(double); tab = (double *)G_malloc(mem); stats = run_stats(&stats_child, basemap, covermap, "-cn"); reclass = run_reclass(&reclass_child, basemap, outputmap); first = 1; while (read_stats(stats, &basecat, &covercat, &value)) { if (first) { first = 0; catb = basecat; catc = covercat; i = 0; count = 0; } if (basecat != catb) { s_dev(tab, count, &sdev); fprintf(reclass, "%ld = %ld %f\n", catb, catb, sdev); catb = basecat; catc = covercat; count = 0; } if (usecats) sscanf(Rast_get_c_cat((CELL *) &covercat, cats), "%lf", &x); else x = covercat; for (i = 0; i < value; i++) { if (count * sizeof(double) >= mem) { mem += MEM * sizeof(double); tab = (double *)G_realloc(tab, mem); /* fprintf(stderr,"MALLOC: %d KB needed\n",(int)(mem/1024)); */ } tab[count++] = x; } } if (first) { catb = catc = 0; } s_dev(tab, count, &sdev); fprintf(reclass, "%ld = %ld %f\n", catb, catb, sdev); G_debug(5, "%ld = %ld %f\n", catb, catb, sdev); G_popen_close(&stats_child); G_popen_close(&reclass_child); return 0; }