int process(char *name, char *mapset, int change_null, RASTER_MAP_TYPE map_type) { struct Colors colr; struct History hist; struct Categories cats; struct Quant quant; int colr_ok; int hist_ok; int cats_ok; int quant_ok; G_suppress_warnings(1); colr_ok = G_read_colors(name, mapset, &colr) > 0; hist_ok = G_read_history(name, mapset, &hist) >= 0; cats_ok = G_read_raster_cats(name, mapset, &cats) >= 0; if (map_type != CELL_TYPE) { G_quant_init(&quant); quant_ok = G_read_quant(name, mapset, &quant); G_suppress_warnings(0); } if (doit(name, mapset, change_null, map_type)) return 1; if (colr_ok) { G_write_colors(name, mapset, &colr); G_free_colors(&colr); } if (hist_ok) G_write_history(name, &hist); if (cats_ok) { cats.num = G_number_of_cats(name, mapset); G_write_raster_cats(name, &cats); G_free_cats(&cats); } if (map_type != CELL_TYPE && quant_ok) G_write_quant(name, mapset, &quant); return 0; }
int main(int argc, char *argv[]) { char *terrainmap, *seedmap, *lakemap, *mapset; int rows, cols, in_terran_fd, out_fd, lake_fd, row, col, pases, pass; int lastcount, curcount, start_col = 0, start_row = 0; double east, north, area = 0, volume = 0; FCELL **in_terran, **out_water, water_level, max_depth = 0, min_depth = 0; FCELL water_window[3][3]; struct Option *tmap_opt, *smap_opt, *wlvl_opt, *lake_opt, *sdxy_opt; struct Flag *negative_flag, *overwrite_flag; struct GModule *module; struct Colors colr; struct Cell_head window; struct History history; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, hydrology"); module->description = _("Fills lake at given point to given level."); tmap_opt = G_define_option(); tmap_opt->key = "dem"; tmap_opt->key_desc = "name"; tmap_opt->description = _("Name of terrain raster map (DEM)"); tmap_opt->type = TYPE_STRING; tmap_opt->gisprompt = "old,cell,raster"; tmap_opt->required = YES; wlvl_opt = G_define_option(); wlvl_opt->key = "wl"; wlvl_opt->description = _("Water level"); wlvl_opt->type = TYPE_DOUBLE; wlvl_opt->required = YES; lake_opt = G_define_option(); lake_opt->key = "lake"; lake_opt->key_desc = "name"; lake_opt->description = _("Name for output raster map with lake"); lake_opt->type = TYPE_STRING; lake_opt->gisprompt = "new,cell,raster"; lake_opt->required = NO; sdxy_opt = G_define_option(); sdxy_opt->key = "xy"; sdxy_opt->description = _("Seed point coordinates"); sdxy_opt->type = TYPE_DOUBLE; sdxy_opt->key_desc = "east,north"; sdxy_opt->required = NO; sdxy_opt->multiple = NO; smap_opt = G_define_option(); smap_opt->key = "seed"; smap_opt->key_desc = "name"; smap_opt->description = _("Name of raster map with given starting point(s) (at least 1 cell > 0)"); smap_opt->type = TYPE_STRING; smap_opt->gisprompt = "old,cell,raster"; smap_opt->required = NO; negative_flag = G_define_flag(); negative_flag->key = 'n'; negative_flag->description = _("Use negative depth values for lake raster map"); overwrite_flag = G_define_flag(); overwrite_flag->key = 'o'; overwrite_flag->description = _("Overwrite seed map with result (lake) map"); if (G_parser(argc, argv)) /* Returns 0 if successful, non-zero otherwise */ exit(EXIT_FAILURE); if (smap_opt->answer && sdxy_opt->answer) G_fatal_error(_("Both seed map and coordinates cannot be specified")); if (!smap_opt->answer && !sdxy_opt->answer) G_fatal_error(_("Seed map or seed coordinates must be set!")); if (sdxy_opt->answer && !lake_opt->answer) G_fatal_error(_("Seed coordinates and output map lake= must be set!")); if (lake_opt->answer && overwrite_flag->answer) G_fatal_error(_("Both lake and overwrite cannot be specified")); if (!lake_opt->answer && !overwrite_flag->answer) G_fatal_error(_("Output lake map or overwrite flag must be set!")); terrainmap = tmap_opt->answer; seedmap = smap_opt->answer; sscanf(wlvl_opt->answer, "%f", &water_level); lakemap = lake_opt->answer; /* If lakemap is set, write to it, else is set overwrite flag and we should write to seedmap. */ if (lakemap) { lake_fd = G_open_raster_new(lakemap, 1); if (lake_fd < 0) G_fatal_error(_("Unable to create raster map <%s>"), lakemap); } rows = G_window_rows(); cols = G_window_cols(); /* If we use x,y as seed... */ if (sdxy_opt->answer) { G_get_window(&window); east = window.east; north = window.north; G_scan_easting(sdxy_opt->answers[0], &east, G_projection()); G_scan_northing(sdxy_opt->answers[1], &north, G_projection()); start_col = (int)G_easting_to_col(east, &window); start_row = (int)G_northing_to_row(north, &window); if (start_row < 0 || start_row > rows || start_col < 0 || start_col > cols) G_fatal_error(_("Seed point outside the current region")); } /* Open terran map */ mapset = G_find_cell2(terrainmap, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), terrainmap); in_terran_fd = G_open_cell_old(terrainmap, mapset); if (in_terran_fd < 0) G_fatal_error(_("Unable to open raster map <%s>"), G_fully_qualified_name(terrainmap, mapset)); /* Open seed map */ if (smap_opt->answer) { mapset = G_find_cell2(seedmap, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), seedmap); out_fd = G_open_cell_old(seedmap, mapset); if (out_fd < 0) G_fatal_error(_("Unable to open raster map <%s>"), G_fully_qualified_name(seedmap, mapset)); } /* Pointers to rows. Row = ptr to 'col' size array. */ in_terran = (FCELL **) G_malloc(rows * sizeof(FCELL *)); out_water = (FCELL **) G_malloc(rows * sizeof(FCELL *)); if (in_terran == NULL || out_water == NULL) G_fatal_error(_("G_malloc: out of memory")); G_debug(1, "Loading maps..."); /* foo_rows[row] == array with data (2d array). */ for (row = 0; row < rows; row++) { in_terran[row] = (FCELL *) G_malloc(cols * sizeof(FCELL)); out_water[row] = (FCELL *) G_calloc(cols, sizeof(FCELL)); /* In newly created space load data from file. */ if (G_get_f_raster_row(in_terran_fd, in_terran[row], row) != 1) G_fatal_error(_("Unable to read raster map <%s> row %d"), terrainmap, row); if (smap_opt->answer) if (G_get_f_raster_row(out_fd, out_water[row], row) != 1) G_fatal_error(_("Unable to read raster map <%s> row %d"), seedmap, row); G_percent(row + 1, rows, 5); } /* Set seed point */ if (sdxy_opt->answer) /* Check is water level higher than seed point */ if (in_terran[start_row][start_col] >= water_level) G_fatal_error(_("Given water level at seed point is below earth surface. " "Increase water level or move seed point.")); out_water[start_row][start_col] = 1; /* Close seed map for reading. */ if (smap_opt->answer) G_close_cell(out_fd); /* Open output map for writing. */ if (lakemap) { out_fd = lake_fd; } else { out_fd = G_open_raster_new(seedmap, 1); if (out_fd < 0) G_fatal_error(_("Unable to create raster map <%s>"), seedmap); } /* More pases are renudant. Real pases count is controled by altered cell count. */ pases = (int)(rows * cols) / 2; G_debug(1, "Starting lake filling at level of %8.4f in %d passes. Percent done:", water_level, pases); lastcount = 0; for (pass = 0; pass < pases; pass++) { G_debug(3, "Pass: %d", pass); curcount = 0; /* Move from left upper corner to right lower corner. */ for (row = 0; row < rows; row++) { for (col = 0; col < cols; col++) { /* Loading water data into window. */ load_window_values(out_water, water_window, rows, cols, row, col); /* Cheking presence of water. */ if (is_near_water(water_window) == 1) { if (in_terran[row][col] < water_level) { out_water[row][col] = water_level - in_terran[row][col]; curcount++; } else { out_water[row][col] = 0; /* Cell is higher than water level -> NULL. */ } } } } if (curcount == lastcount) break; /* We done. */ lastcount = curcount; curcount = 0; /* Move backwards - from lower right corner to upper left corner. */ for (row = rows - 1; row >= 0; row--) { for (col = cols - 1; col >= 0; col--) { load_window_values(out_water, water_window, rows, cols, row, col); if (is_near_water(water_window) == 1) { if (in_terran[row][col] < water_level) { out_water[row][col] = water_level - in_terran[row][col]; curcount++; } else { out_water[row][col] = 0; } } } } G_percent(pass + 1, pases, 10); if (curcount == lastcount) break; /* We done. */ lastcount = curcount; } /*pases */ G_percent(pases, pases, 10); /* Show 100%. */ save_map(out_water, out_fd, rows, cols, negative_flag->answer, &min_depth, &max_depth, &area, &volume); G_message(_("Lake depth from %f to %f"), min_depth, max_depth); G_message(_("Lake area %f square meters"), area); G_message(_("Lake volume %f cubic meters"), volume); G_warning(_("Volume is correct only if lake depth (terrain raster map) is in meters")); /* Close all files. Lake map gets written only now. */ G_close_cell(in_terran_fd); G_close_cell(out_fd); /* Add blue color gradient from light bank to dark depth */ G_init_colors(&colr); if (negative_flag->answer == 1) { G_add_f_raster_color_rule(&max_depth, 0, 240, 255, &min_depth, 0, 50, 170, &colr); } else { G_add_f_raster_color_rule(&min_depth, 0, 240, 255, &max_depth, 0, 50, 170, &colr); } if (G_write_colors(lakemap, G_mapset(), &colr) != 1) G_fatal_error(_("Unable to read color file of raster map <%s>"), lakemap); G_short_history(lakemap, "raster", &history); G_command_history(&history); G_write_history(lakemap, &history); return EXIT_SUCCESS; }
int main(int argc, char **argv) { int overwrite; int interactive; int remove; int have_colors; struct Colors colors, colors_tmp; struct Cell_stats statf; int have_stats = 0; struct FPRange range; DCELL min, max; char *name, *mapset; char *style, *cmap, *cmapset; char *rules; int fp; struct GModule *module; struct { struct Flag *r, *w, *l, *g, *a, *e, *i, *q, *n; } flag; struct { struct Option *map, *colr, *rast, *rules; } opt; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, color table"); module->description = _("Creates/modifies the color table associated with a raster map layer."); opt.map = G_define_standard_option(G_OPT_R_MAP); opt.map->required = NO; opt.map->guisection = _("Required"); scan_rules(); opt.colr = G_define_option(); opt.colr->key = "color"; opt.colr->key_desc = "style"; opt.colr->type = TYPE_STRING; opt.colr->required = NO; opt.colr->options = rules_list(); opt.colr->description = _("Type of color table"); opt.colr->descriptions = rules_descriptions(); opt.colr->guisection = _("Colors"); opt.rast = G_define_option(); opt.rast->key = "raster"; opt.rast->type = TYPE_STRING; opt.rast->required = NO; opt.rast->gisprompt = "old,cell,raster"; opt.rast->description = _("Raster map name from which to copy color table"); opt.rules = G_define_standard_option(G_OPT_F_INPUT); opt.rules->key = "rules"; opt.rules->required = NO; opt.rules->description = _("Path to rules file (\"-\" to read rules from stdin)"); opt.rules->guisection = _("Colors"); flag.r = G_define_flag(); flag.r->key = 'r'; flag.r->description = _("Remove existing color table"); flag.w = G_define_flag(); flag.w->key = 'w'; flag.w->description = _("Only write new color table if one doesn't already exist"); flag.l = G_define_flag(); flag.l->key = 'l'; flag.l->description = _("List available rules then exit"); flag.n = G_define_flag(); flag.n->key = 'n'; flag.n->description = _("Invert colors"); flag.n->guisection = _("Colors"); flag.g = G_define_flag(); flag.g->key = 'g'; flag.g->description = _("Logarithmic scaling"); flag.g->guisection = _("Colors"); flag.a = G_define_flag(); flag.a->key = 'a'; flag.a->description = _("Logarithmic-absolute scaling"); flag.a->guisection = _("Colors"); flag.e = G_define_flag(); flag.e->key = 'e'; flag.e->description = _("Histogram equalization"); flag.e->guisection = _("Colors"); flag.i = G_define_flag(); flag.i->key = 'i'; flag.i->description = _("Enter rules interactively"); /* please, remove before GRASS 7 released */ flag.q = G_define_flag(); flag.q->key = 'q'; flag.q->description = _("Run quietly"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); /* please, remove before GRASS 7 released */ if (flag.q->answer) { G_putenv("GRASS_VERBOSE", "0"); G_warning(_("The '-q' flag is superseded and will be removed " "in future. Please use '--quiet' instead.")); } if (flag.l->answer) { list_rules(); return EXIT_SUCCESS; } overwrite = !flag.w->answer; interactive = flag.i->answer; remove = flag.r->answer; name = opt.map->answer; style = opt.colr->answer; cmap = opt.rast->answer; rules = opt.rules->answer; if (!name) G_fatal_error(_("No raster map specified")); if (!cmap && !style && !rules && !interactive && !remove) G_fatal_error(_("One of \"-i\" or \"-r\" or options \"color\", \"rast\" or \"rules\" must be specified!")); if (interactive && (style || rules || cmap)) G_fatal_error(_("Interactive mode is incompatible with \"color\", \"rules\", and \"raster\" options")); if ((style && (cmap || rules)) || (cmap && rules)) { if ((style && rules && !cmap) && strcmp(style, "rules") == 0) style = NULL; else G_fatal_error( _("\"color\", \"rules\", and \"raster\" options are mutually exclusive")); } /* handle rules="-" (from stdin) by translating that to colors=rules */ /* this method should not be ported to GRASS 7 verbatim, as color=rules DNE */ if (rules && strcmp(rules, "-") == 0) { style = G_store("rules"); rules = NULL; } if (flag.g->answer && flag.a->answer) G_fatal_error(_("-g and -a flags are mutually exclusive")); mapset = G_find_cell2(name, ""); if (mapset == NULL) G_fatal_error(_("Raster map <%s> not found"), name); if (remove) { int stat = G_remove_colors(name, mapset); if (stat < 0) G_fatal_error(_("Unable to remove color table of raster map <%s>"), name); if (stat == 0) G_warning(_("Color table of raster map <%s> not found"), name); return EXIT_SUCCESS; } G_suppress_warnings(1); have_colors = G_read_colors(name, mapset, &colors); /*if (have_colors >= 0) G_free_colors(&colors); */ if (have_colors > 0 && !overwrite) { G_warning(_("Color table exists. Exiting.")); exit(EXIT_FAILURE); } G_suppress_warnings(0); fp = G_raster_map_is_fp(name, mapset); G_read_fp_range(name, mapset, &range); G_get_fp_range_min_max(&range, &min, &max); if (interactive) { if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); } else if (style) { /* * here the predefined color-table color-styles are created by GRASS library calls. */ if (strcmp(style, "random") == 0) { if (fp) G_fatal_error(_("Color table 'random' is not supported for floating point raster map")); G_make_random_colors(&colors, (CELL) min, (CELL) max); } else if (strcmp(style, "grey.eq") == 0) { if (fp) G_fatal_error(_("Color table 'grey.eq' is not supported for floating point raster map")); if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_make_histogram_eq_colors(&colors, &statf); } else if (strcmp(style, "grey.log") == 0) { if (fp) G_fatal_error(_("Color table 'grey.log' is not supported for floating point raster map")); if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_make_histogram_log_colors(&colors, &statf, (CELL) min, (CELL) max); } else if (strcmp(style, "rules") == 0) { if (!read_color_rules(stdin, &colors, min, max, fp)) exit(EXIT_FAILURE); } else if (find_rule(style)) G_make_fp_colors(&colors, style, min, max); else G_fatal_error(_("Unknown color request '%s'"), style); } else if (rules) { if (!G_load_fp_colors(&colors, rules, min, max)) { /* for backwards compatibility try as std name; remove for GRASS 7 */ char path[GPATH_MAX]; /* don't bother with native dirsep as not needed for backwards compatibility */ sprintf(path, "%s/etc/colors/%s", G_gisbase(), rules); if (!G_load_fp_colors(&colors, path, min, max)) G_fatal_error(_("Unable to load rules file <%s>"), rules); } } else { /* use color from another map (cmap) */ cmapset = G_find_cell2(cmap, ""); if (cmapset == NULL) G_fatal_error(_("Raster map <%s> not found"), cmap); if (G_read_colors(cmap, cmapset, &colors) < 0) G_fatal_error(_("Unable to read color table for raster map <%s>"), cmap); } if (fp) G_mark_colors_as_fp(&colors); if (flag.n->answer) G_invert_colors(&colors); if (flag.e->answer) { if (fp) { struct FP_stats fpstats; get_fp_stats(name, mapset, &fpstats, min, max, flag.g->answer, flag.a->answer); G_histogram_eq_colors_fp(&colors_tmp, &colors, &fpstats); } else { if (!have_stats) have_stats = get_stats(name, mapset, &statf); G_histogram_eq_colors(&colors_tmp, &colors, &statf); } colors = colors_tmp; } if (flag.g->answer) { G_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (flag.a->answer) { G_abs_log_colors(&colors_tmp, &colors, 100); colors = colors_tmp; } if (fp) G_mark_colors_as_fp(&colors); if (G_write_colors(name, mapset, &colors) >= 0) G_message(_("Color table for raster map <%s> set to '%s'"), name, interactive ? "rules" : style ? style : rules ? rules : cmap); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { struct GModule *module; struct Option *rastin, *rastout, *method; struct History history; char title[64]; char buf_nsres[100], buf_ewres[100]; struct Colors colors; char *inmap; int infile, outfile; DCELL *outbuf; int row, col; struct Cell_head dst_w, src_w; G_gisinit(argv[0]); module = G_define_module(); module->keywords = _("raster, resample"); module->description = _("Resamples raster map layers to a finer grid using interpolation."); rastin = G_define_standard_option(G_OPT_R_INPUT); rastout = G_define_standard_option(G_OPT_R_OUTPUT); method = G_define_option(); method->key = "method"; method->type = TYPE_STRING; method->required = NO; method->description = _("Interpolation method"); method->options = "nearest,bilinear,bicubic"; method->answer = "bilinear"; if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (G_strcasecmp(method->answer, "nearest") == 0) neighbors = 1; else if (G_strcasecmp(method->answer, "bilinear") == 0) neighbors = 2; else if (G_strcasecmp(method->answer, "bicubic") == 0) neighbors = 4; else G_fatal_error(_("Invalid method: %s"), method->answer); G_get_set_window(&dst_w); inmap = G_find_cell2(rastin->answer, ""); if (!inmap) G_fatal_error(_("Raster map <%s> not found"), rastin->answer); /* set window to old map */ G_get_cellhd(rastin->answer, inmap, &src_w); /* enlarge source window */ { double north = G_row_to_northing(0.5, &dst_w); double south = G_row_to_northing(dst_w.rows - 0.5, &dst_w); int r0 = (int)floor(G_northing_to_row(north, &src_w) - 0.5) - 1; int r1 = (int)floor(G_northing_to_row(south, &src_w) - 0.5) + 3; double west = G_col_to_easting(0.5, &dst_w); double east = G_col_to_easting(dst_w.cols - 0.5, &dst_w); int c0 = (int)floor(G_easting_to_col(west, &src_w) - 0.5) - 1; int c1 = (int)floor(G_easting_to_col(east, &src_w) - 0.5) + 3; src_w.south -= src_w.ns_res * (r1 - src_w.rows); src_w.north += src_w.ns_res * (-r0); src_w.west -= src_w.ew_res * (-c0); src_w.east += src_w.ew_res * (c1 - src_w.cols); src_w.rows = r1 - r0; src_w.cols = c1 - c0; } G_set_window(&src_w); /* allocate buffers for input rows */ for (row = 0; row < neighbors; row++) bufs[row] = G_allocate_d_raster_buf(); cur_row = -100; /* open old map */ infile = G_open_cell_old(rastin->answer, inmap); if (infile < 0) G_fatal_error(_("Unable to open raster map <%s>"), rastin->answer); /* reset window to current region */ G_set_window(&dst_w); outbuf = G_allocate_d_raster_buf(); /* open new map */ outfile = G_open_raster_new(rastout->answer, DCELL_TYPE); if (outfile < 0) G_fatal_error(_("Unable to create raster map <%s>"), rastout->answer); G_suppress_warnings(1); /* otherwise get complaints about window changes */ switch (neighbors) { case 1: /* nearest */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f + 0.5); G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f + 0.5); double c = bufs[0][mapcol0]; if (G_is_d_null_value(&c)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = c; } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 2: /* bilinear */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow0 = (int)floor(maprow_f); double v = maprow_f - maprow0; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol0 = (int)floor(mapcol_f); int mapcol1 = mapcol0 + 1; double u = mapcol_f - mapcol0; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bilinear(u, v, c00, c01, c10, c11); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; case 4: /* bicubic */ for (row = 0; row < dst_w.rows; row++) { double north = G_row_to_northing(row + 0.5, &dst_w); double maprow_f = G_northing_to_row(north, &src_w) - 0.5; int maprow1 = (int)floor(maprow_f); int maprow0 = maprow1 - 1; double v = maprow_f - maprow1; G_percent(row, dst_w.rows, 2); G_set_window(&src_w); read_rows(infile, maprow0); for (col = 0; col < dst_w.cols; col++) { double east = G_col_to_easting(col + 0.5, &dst_w); double mapcol_f = G_easting_to_col(east, &src_w) - 0.5; int mapcol1 = (int)floor(mapcol_f); int mapcol0 = mapcol1 - 1; int mapcol2 = mapcol1 + 1; int mapcol3 = mapcol1 + 2; double u = mapcol_f - mapcol1; double c00 = bufs[0][mapcol0]; double c01 = bufs[0][mapcol1]; double c02 = bufs[0][mapcol2]; double c03 = bufs[0][mapcol3]; double c10 = bufs[1][mapcol0]; double c11 = bufs[1][mapcol1]; double c12 = bufs[1][mapcol2]; double c13 = bufs[1][mapcol3]; double c20 = bufs[2][mapcol0]; double c21 = bufs[2][mapcol1]; double c22 = bufs[2][mapcol2]; double c23 = bufs[2][mapcol3]; double c30 = bufs[3][mapcol0]; double c31 = bufs[3][mapcol1]; double c32 = bufs[3][mapcol2]; double c33 = bufs[3][mapcol3]; if (G_is_d_null_value(&c00) || G_is_d_null_value(&c01) || G_is_d_null_value(&c02) || G_is_d_null_value(&c03) || G_is_d_null_value(&c10) || G_is_d_null_value(&c11) || G_is_d_null_value(&c12) || G_is_d_null_value(&c13) || G_is_d_null_value(&c20) || G_is_d_null_value(&c21) || G_is_d_null_value(&c22) || G_is_d_null_value(&c23) || G_is_d_null_value(&c30) || G_is_d_null_value(&c31) || G_is_d_null_value(&c32) || G_is_d_null_value(&c33)) { G_set_d_null_value(&outbuf[col], 1); } else { outbuf[col] = G_interp_bicubic(u, v, c00, c01, c02, c03, c10, c11, c12, c13, c20, c21, c22, c23, c30, c31, c32, c33); } } G_set_window(&dst_w); G_put_d_raster_row(outfile, outbuf); } break; } G_percent(dst_w.rows, dst_w.rows, 2); G_close_cell(infile); G_close_cell(outfile); /* record map metadata/history info */ sprintf(title, "Resample by %s interpolation", method->answer); G_put_cell_title(rastout->answer, title); G_short_history(rastout->answer, "raster", &history); strncpy(history.datsrc_1, rastin->answer, RECORD_LEN); history.datsrc_1[RECORD_LEN - 1] = '\0'; /* strncpy() doesn't null terminate if maxfill */ G_format_resolution(src_w.ns_res, buf_nsres, src_w.proj); G_format_resolution(src_w.ew_res, buf_ewres, src_w.proj); sprintf(history.datsrc_2, "Source map NS res: %s EW res: %s", buf_nsres, buf_ewres); G_command_history(&history); G_write_history(rastout->answer, &history); /* copy color table from source map */ if (G_read_colors(rastin->answer, inmap, &colors) < 0) G_fatal_error(_("Unable to read color table for %s"), rastin->answer); G_mark_colors_as_fp(&colors); if (G_write_colors(rastout->answer, G_mapset(), &colors) < 0) G_fatal_error(_("Unable to write color table for %s"), rastout->answer); return (EXIT_SUCCESS); }
int make_new_cell_layer(void) { struct History hist; void *rast; int cellfd; int tmpfd; int row; /* open the new raster map to contain the edited version of the original cell layer. open our temporary file for read and copy its contents to the layer */ G_set_window(&real_window); cellfd = G_open_raster_new(new_name, map_type); tmpfd = open(tempfile, 0); lseek(tmpfd, 0L, 0); rast = G_allocate_raster_buf(map_type); fprintf(stderr, "\n +-------------------------------------------+\n"); fprintf(stderr, " | Saving new cell layer |\n"); fprintf(stderr, " +---------------------------------------"); for (row = 0; row < real_nrows; row++) { if (read(tmpfd, rast, real_ncols * cellsize) != (real_ncols * cellsize)) error(1, "error writing raster map during copy"); G_put_raster_row(cellfd, rast, map_type); G_percent(row, real_nrows, 5); } G_percent(100, 100, 5); fprintf(stderr, "\n"); close(tmpfd); G_close_cell(cellfd); unlink(tempfile); /* create and write cat, colr, quant, and hist support files for the newly created layer */ if (colr_ok) { G_write_colors(new_name, user_mapset, &colr); G_free_colors(&colr); colr_ok = 0; } if (cats_ok) { cats.num = G_number_of_cats(new_name, user_mapset); G_write_cats(new_name, &cats); G_free_cats(&cats); cats_ok = 0; } if (quant_ok) { G_write_quant(new_name, G_mapset(), &quant); G_quant_free(&quant); cats_ok = 0; } /* construct some history information */ sprintf(hist.mapid, "%s", G_date()); sprintf(hist.title, "%s", new_name); sprintf(hist.mapset, "%s", user_mapset); sprintf(hist.creator, "%s", G_whoami()); sprintf(hist.maptype, "cell"); sprintf(hist.edhist[0], "Generated by d.rast.edit from original raster map"); sprintf(hist.edhist[1], " %s in mapset %s ", orig_name, orig_mapset); hist.edlinecnt = 2; /* write history */ if (G_write_history(new_name, &hist) == -1) error(0, "could not write history"); return 0; }
void do_calculations_rast (Shypothesis **samples, char **groups, int norm, char* basename, char **outvals, char *hypspec, int quiet_flag, char *logfile, xmlDocPtr doc, Sfp_struct* file_pointers) { long y,x; int i, j, k, l, m; long ymax,xmax; double woc; struct Categories cats, icats; DCELL cmin, cmax; Sresult_struct *result_row; /* one result_struct for each DST value */ BOOL **garbage; int no_hyps; char* val_names[NUMVALS]={"bel","pl","doubt","common","bint","woc","maxbpa","minbpa", "maxsrc","minsrc"}; int error; char **outhyps; int no_sets; /* for keeping min and max statistics */ Uint nsets; double *min_backup, *max_backup; int *minev_backup, *maxev_backup; woc = 0; /* check for output options */ if ( G_legal_filename(basename) != 1 ) { G_fatal_error ("Please provide a legal filename as basename for output maps(s).\n"); } if ( hypspec != NULL ) { /* user specified hyps, let's see if they're valid */ /* create an outhyps array that has as each of its elements the name of one of the hypotheses specified on the command line */ outhyps = parse_hyps (hypspec, &no_hyps); check_hyps ( outhyps, no_hyps, doc ); } else { /* just process all hypotheses */ outhyps = get_hyp_names_XML ( &no_hyps, doc ); } if ( logfile != NULL ) { fprintf (lp,"Writing output RASTER maps for: \n"); } /* create raster rows to store results */ result_row = G_malloc ( NUMVALS * sizeof (Sresult_struct) ); for (i=0; i<NUMVALS; i++) { result_row[i].use = NO; strcpy (result_row[i].valname,val_names[i]); /* individual raster rows will be alloc'd later */ result_row[i].row = (DCELL **) G_malloc ( no_hyps * sizeof (DCELL*) ); result_row[i].crow = (CELL **) G_malloc ( no_hyps * sizeof (CELL*) ); result_row[i].filename = NULL; } j = 0; while ( outvals[j] != NULL ) { if ( !strcmp (outvals[j],"bel") ) { if ( logfile != NULL ) fprintf (lp,"\t'bel' (Believe) values\n"); make_result_row ( BEL, basename, outhyps, no_hyps, &result_row[BEL], doc ); } if ( !strcmp (outvals[j],"pl") ) { if ( logfile != NULL ) fprintf (lp,"\t'pl' (Plausibility) values\n"); make_result_row ( PL, basename, outhyps, no_hyps, &result_row[PL], doc ); } if ( !strcmp (outvals[j],"doubt") ) { if ( logfile != NULL ) fprintf (lp,"\t'doubt' (Doubt) values\n"); make_result_row ( DOUBT, basename, outhyps, no_hyps, &result_row[DOUBT], doc ); } if ( !strcmp (outvals[j],"common") ) { if ( logfile != NULL ) fprintf (lp,"\t'common' (Commonality) values\n"); make_result_row ( COMMON, basename, outhyps, no_hyps, &result_row[COMMON], doc ); } if ( !strcmp (outvals[j],"bint") ) { if ( logfile != NULL ) fprintf (lp,"\t'bint' (Believe interval) values\n"); make_result_row ( BINT, basename, outhyps, no_hyps, &result_row[BINT], doc ); } if ( !strcmp (outvals[j],"woc") ) { if ( logfile != NULL ) fprintf (lp,"\t'woc' (Weight of conflict) values\n"); make_result_row ( WOC, basename, outhyps, no_hyps,&result_row[WOC], doc ); } if ( !strcmp (outvals[j],"maxbpa") ) { if ( logfile != NULL ) fprintf (lp,"\t'maxbpa' (Maximum BPA) values\n"); make_result_row ( MAXBPA, basename, outhyps, no_hyps,&result_row[MAXBPA], doc ); } if ( !strcmp (outvals[j],"minbpa") ) { if ( logfile != NULL ) fprintf (lp,"\t'minbpa' (Minimum BPA) values\n"); make_result_row ( MINBPA, basename, outhyps, no_hyps,&result_row[MINBPA], doc ); } if ( !strcmp (outvals[j],"maxsrc") ) { if ( logfile != NULL ) fprintf (lp,"\t'maxsrc' (source of highest BPA) values\n"); make_result_row ( MAXSRC, basename, outhyps, no_hyps,&result_row[MAXSRC], doc ); } if ( !strcmp (outvals[j],"minsrc") ) { if ( logfile != NULL ) fprintf (lp,"\t'minsrc' (source of lowest BPA) values\n"); make_result_row ( MINSRC, basename, outhyps, no_hyps,&result_row[MINSRC], doc ); } j ++; } /* open output maps to store results */ if ( logfile != NULL ) fprintf (lp,"Opening output maps:\n"); for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { if ( logfile != NULL ) fprintf (lp,"\t%s\n",result_row[i].filename[0]); result_row[i].fd[0] = G_open_raster_new (result_row[i].filename[0],DCELL_TYPE); } else { for (j=0; j < no_hyps; j++) { if ( logfile != NULL ) fprintf (lp,"\t%s\n",result_row[i].filename[j]); if ((i == MAXSRC) || (i == MINSRC)) { result_row[i].fd[j] = G_open_raster_new (result_row[i].filename[j],CELL_TYPE); } else { result_row[i].fd[j] = G_open_raster_new (result_row[i].filename[j],DCELL_TYPE); } /* check fd for errors */ if ( result_row[i].fd[j] < 0 ) { G_fatal_error ("Could not create output map for %s\n", result_row[i].filename[j]); } } } } } if ( logfile != NULL ) { fprintf (lp, "Evidence will be combined for these groups:\n"); for ( i=0; i < N; i++) { fprintf (lp,"\t%s\n",groups[i]); } fprintf (lp, "Output will be stored in mapset '%s'.\n", G_mapset()); fprintf (lp,"\nRead output below carefully to detect potential problems:\n"); } /* set start coordinates for reading from raster maps */ ReadX = 0; ReadY = 0; ymax = G_window_rows (); xmax = G_window_cols (); if ( !quiet_flag ) { fprintf (stdout,"Combining RAST evidence: \n"); fflush (stdout); } /* allocate all file pointers */ /* open raster maps for this group */ /* 0 is the NULL hypothesis, so we start at 1 */ no_sets = (Uint) pow((float) 2, (float) NO_SINGLETONS); for (l=0; l<N; l++) { for ( m = 1; m < no_sets; m ++ ) { file_pointers[l].fp[m] = G_open_cell_old ( file_pointers[l].filename[m], G_find_cell ( file_pointers[l].filename[m],"") ); if ( file_pointers[l].fp[m] < 0 ) { G_fatal_error ("Could not open raster map '%s' for reading.\n", file_pointers[l].filename[m] ); } } } for (y=0; y<ymax; y++) { for (x=0; x<xmax; x++) { garbage = garbage_init (); NULL_SIGNAL = 0; for (i=0; i<N; i++) { samples[i] = get_rast_samples_XML (groups[i],i, norm, &nsets, garbage, doc, file_pointers ); } /* get min and max values */ for (i=0; i<N; i++) { if (NULL_SIGNAL == 0) { for (k=0; k < nsets; k++) { samples[i][k].minbpn = samples[i][k].bpa; samples[i][k].maxbpn = samples[i][k].bpa; samples[i][k].minbpnev = i + 1; samples[i][k].maxbpnev = i + 1; } } } for (i=0; i<N; i++) { if (NULL_SIGNAL == 0) { for (j=0; j < N; j++) { for (k=0; k < nsets; k++) { if (samples[i][k].bpa < samples[j][k].minbpn) { samples[j][k].minbpn = samples[i][k].bpa; samples[j][k].minbpnev = i + 1; } if (samples[i][k].bpa > samples[j][k].maxbpn) { samples[j][k].maxbpn = samples[i][k].bpa; samples[j][k].maxbpnev = i + 1; } } } } } /* initialise: */ /* set belief and plausibility before first combination of evidence */ for(i=0;i<N;i++) { if ( NULL_SIGNAL == 0 ) { set_beliefs(samples[i]); set_plausibilities(samples[i]); } } /* combine evidence and set bel and pl again */ /* AFTER COMBINE_BPN(), VALUES IN SAMPLES[0] WILL ALL BE ALTERED */ /* so we must save min and max values for later use */ min_backup = G_malloc ((unsigned)(nsets * sizeof(double))); max_backup = G_malloc ((unsigned)(nsets * sizeof(double))); minev_backup = G_malloc ((unsigned)(nsets * sizeof(int))); maxev_backup = G_malloc ((unsigned)(nsets * sizeof(int))); for (k=0; k < nsets; k++) { min_backup[k] = samples[0][k].minbpn; max_backup[k] = samples[0][k].maxbpn; minev_backup[k] = samples[0][k].minbpnev; maxev_backup[k] = samples[0][k].maxbpnev; } /* now, do the combination! */ for(i=0;i<N-1;i++) { if ( NULL_SIGNAL == 0 ) { woc = combine_bpn(samples[0], samples[i+1], garbage, RAST_MODE ); set_beliefs(samples[0]); set_plausibilities(samples[0]); } } /* restore min and max values */ for (k=0; k < nsets; k++) { samples[0][k].minbpn = min_backup[k]; samples[0][k].maxbpn = max_backup[k]; samples[0][k].minbpnev = minev_backup[k]; samples[0][k].maxbpnev = maxev_backup[k]; } G_free (min_backup); G_free (max_backup); G_free (minev_backup); G_free (maxev_backup); /* all other metrics can be derived from bel and pl, no need */ /* to combine evidence again! */ if ( NULL_SIGNAL == 0 ) { set_commonalities(samples[0]); set_doubts(samples[0]); set_bint(samples[0]); } if ( NULL_SIGNAL == 1 ) { for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC) { write_row_null (result_row[i].row[0], ReadX); } else { if ((i == MAXSRC)||(i == MINSRC)) { for (j=0; j < no_hyps; j++) { write_crow_null (result_row[i].crow[j], ReadX); } } else { for (j=0; j < no_hyps; j++) { write_row_null (result_row[i].row[j], ReadX); } } } } } } else { for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { write_row_val (result_row[i].row[0], ReadX, samples[0], result_row[i].hyp_idx[0], i, woc); } else { if (( i == MAXSRC ) || ( i == MINSRC )) { for (j=0; j < no_hyps; j++) { write_crow_val (result_row[i].crow[j], ReadX, samples[0], result_row[i].hyp_idx[j], i); } } else { for (j=0; j < no_hyps; j++) { write_row_val (result_row[i].row[j], ReadX, samples[0], result_row[i].hyp_idx[j], i, woc); } } } } } } ReadX ++; garbage_free ( garbage ); for (i=0; i<N; i++) { free_sample (samples[i]); } } ReadY ++; /* go to next row */ ReadX = 0; /* save this row to the result file */ for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { write_row_file ( result_row[i].row[0],result_row[i].fd[0]); } else { if ( ( i == MAXSRC ) || ( i == MINSRC ) ) { for (j=0; j<no_hyps; j++) { write_crow_file ( result_row[i].crow[j],result_row[i].fd[j]); } } else { for (j=0; j<no_hyps; j++) { write_row_file ( result_row[i].row[j],result_row[i].fd[j]); } } } } } if ( !quiet_flag ) { G_percent (ReadY,ymax,1); fflush (stdout); } } if ( !quiet_flag ) { fprintf (stdout,"\n"); fflush (stdout); } for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { G_close_cell (result_row[i].fd[0]); } else { for (j=0; j<no_hyps; j++) { G_close_cell (result_row[i].fd[j]); } } } } /* close raster maps */ /* 0 is the NULL hypothesis, so we start at 1 */ for (l=0; l<N; l++) { for ( m = 1; m < no_sets; m ++ ) { G_close_cell (file_pointers[l].fp[m]); } } /* create a categories structure for output maps */ /* DCELL maps */ G_init_cats (3, "Value ranges", &cats); cmin = 0; cmax = 0.333333; G_set_d_raster_cat (&cmin, &cmax, "low", &cats); cmin = 0.333334; cmax = 0.666666; G_set_d_raster_cat (&cmin, &cmax, "medium", &cats); cmin = 0.666667; cmax = 1; G_set_d_raster_cat (&cmin, &cmax, "high", &cats); /* CELL maps */ G_init_cats (N+1, "Source of evidence", &icats); G_set_cat (0,"no data",&icats); for (i=1; i<=N; i++) { G_set_cat (i,groups[i-1],&icats); } /* write all color tables, categories information and history metadata */ for (i=0; i<NUMVALS;i++) { if (result_row[i].use == YES) { if ( i == WOC ) { error = G_write_colors (result_row[i].filename[0], G_mapset(), result_row[i].colors[0]); if (error == -1) { G_warning ("Could not create color table for map '%s'.\n",result_row[i].filename[j]); } } else { if (( i == MAXSRC ) || ( i == MINSRC )) { for (j=0; j<no_hyps; j++) { G_write_cats (result_row[i].filename[j], &icats); } } else { for (j=0; j<no_hyps; j++) { error = G_write_colors (result_row[i].filename[j], G_mapset(), result_row[i].colors[j]); if (error == -1) { G_warning ("Could not create color table for map '%s'.\n",result_row[i].filename[j]); } G_write_raster_cats (result_row[i].filename[j], &cats); } } } } } G_free (samples); for ( i=0; i < no_hyps; i ++ ) { G_free ( outhyps[i]); } G_free (outhyps); }
int main(int argc, char *argv[]) { /* Global variable & function declarations */ char Cellmap_orig[50]; FILE *realfp, *imagfp; /* the input and output file descriptors */ int outputfd, maskfd; /* the input and output file descriptors */ char *realmapset, *imagmapset; /* the input mapset names */ struct Cell_head orig_wind, realhead; CELL *cell_row, *maskbuf = NULL; int i, j; /* Loop control variables */ int or, oc; /* Original dimensions of image */ int rows, cols; /* Smallest powers of 2 >= number of rows & columns */ long totsize; /* Total number of data points */ int halfrows, halfcols; double *data[2]; /* Data structure containing real & complex values of FFT */ struct Option *op1, *op2, *op3; struct GModule *module; G_gisinit(argv[0]); /* Set description */ module = G_define_module(); module->keywords = _("imagery, FFT"); module->description = _("Inverse Fast Fourier Transform (IFFT) for image processing."); /* define options */ op1 = G_define_standard_option(G_OPT_R_INPUT); op1->key = "real_image"; op1->description = _("Name of input raster map (image fft, real part)"); op2 = G_define_standard_option(G_OPT_R_INPUT); op2->key = "imaginary_image"; op2->description = _("Name of input raster map (image fft, imaginary part"); op3 = G_define_standard_option(G_OPT_R_OUTPUT); op3->key = "output_image"; op3->description = _("Name for output raster map"); /*call parser */ if (G_parser(argc, argv)) exit(EXIT_FAILURE); strcpy(Cellmap_real, op1->answer); strcpy(Cellmap_imag, op2->answer); strcpy(Cellmap_orig, op3->answer); /* open input raster map */ if ((realmapset = G_find_cell(Cellmap_real, "")) == NULL) G_fatal_error(_("Raster map <%s> not found"), Cellmap_real); if ((realfp = G_fopen_old_misc("cell_misc", "fftreal", Cellmap_real, realmapset)) == NULL) G_fatal_error(_("Unable to open real-image in the 'cell_misc' directory. " "Raster map probably wasn't created by i.fft")); if ((imagmapset = G_find_cell(Cellmap_imag, "")) == NULL) G_fatal_error(_("Raster map <%s> not found"), Cellmap_imag); if ((imagfp = G_fopen_old_misc("cell_misc", "fftimag", Cellmap_imag, imagmapset)) == NULL) G_fatal_error(_("Unable to open imaginary-image in the 'cell_misc' directory. " "Raster map probably wasn't created by i.fft")); /* check command line args for validity */ if (G_legal_filename(Cellmap_orig) < 0) G_fatal_error(_("<%s> is an illegal file name"), Cellmap_orig); /* get and compare the original window data */ get_orig_window(&orig_wind, realmapset, imagmapset); or = orig_wind.rows; oc = orig_wind.cols; G_get_cellhd(Cellmap_real, realmapset, &realhead); G_set_window(&realhead); /* set the window to the whole cell map */ /* get the rows and columns in the current window */ rows = G_window_rows(); cols = G_window_cols(); totsize = rows * cols; halfrows = rows / 2; halfcols = cols / 2; G_verbose_message(_("Power 2 values: %d rows %d columns"), rows, cols); /* Allocate appropriate memory for the structure containing the real and complex components of the FFT. DATA[0] will contain the real, and DATA[1] the complex component. */ data[0] = (double *)G_malloc((rows * cols) * sizeof(double)); data[1] = (double *)G_malloc((rows * cols) * sizeof(double)); /* Initialize real & complex components to zero */ G_message(_("Reading raster maps...")); { fread((char *)data[0], sizeof(double), totsize, realfp); fread((char *)data[1], sizeof(double), totsize, imagfp); } /* Read in cell map values */ G_message(_("Masking raster maps...")); maskfd = G_maskfd(); if (maskfd >= 0) maskbuf = G_allocate_cell_buf(); if (maskfd >= 0) { for (i = 0; i < rows; i++) { double *data0, *data1; data0 = data[0] + i * cols; data1 = data[1] + i * cols; G_get_map_row(maskfd, maskbuf, i); for (j = 0; j < cols; j++, data0++, data1++) { if (maskbuf[j] == (CELL) 0) { *(data0) = 0.0; *(data1) = 0.0; } } } } G_message(_("Rotating data...")); /* rotate the data array for standard display */ for (i = 0; i < rows; i++) { double temp; for (j = 0; j < halfcols; j++) { temp = *(data[0] + i * cols + j); *(data[0] + i * cols + j) = *(data[0] + i * cols + j + halfcols); *(data[0] + i * cols + j + halfcols) = temp; temp = *(data[1] + i * cols + j); *(data[1] + i * cols + j) = *(data[1] + i * cols + j + halfcols); *(data[1] + i * cols + j + halfcols) = temp; } } for (i = 0; i < halfrows; i++) { double temp; for (j = 0; j < cols; j++) { temp = *(data[0] + i * cols + j); *(data[0] + i * cols + j) = *(data[0] + (i + halfrows) * cols + j); *(data[0] + (i + halfrows) * cols + j) = temp; temp = *(data[1] + i * cols + j); *(data[1] + i * cols + j) = *(data[1] + (i + halfrows) * cols + j); *(data[1] + (i + halfrows) * cols + j) = temp; } } /* close input cell maps and release the row buffers */ fclose(realfp); fclose(imagfp); if (maskfd >= 0) { G_close_cell(maskfd); G_free(maskbuf); } /* perform inverse FFT */ G_message(_("Starting Inverse FFT...")); fft(1, data, totsize, cols, rows); /* set up a window for the transform cell map */ G_set_window(&orig_wind); /* open the output cell map and allocate a cell row buffer */ if ((outputfd = G_open_cell_new(Cellmap_orig)) < 0) G_fatal_error(_("Unable to create raster map <%s>"), Cellmap_orig); cell_row = G_allocate_cell_buf(); /* Write out result to a new cell map */ G_message(_("Writing data...")); for (i = 0; i < or; i++) { for (j = 0; j < oc; j++) { *(cell_row + j) = (CELL) (*(data[0] + i * cols + j) + 0.5); } G_put_raster_row(outputfd, cell_row, CELL_TYPE); G_percent(i+1, or, 2); } G_close_cell(outputfd); G_free(cell_row); { struct Colors colors; struct Range range; CELL min, max; /* make a real component color table */ G_read_range(Cellmap_orig, G_mapset(), &range); G_get_range_min_max(&range, &min, &max); G_make_grey_scale_colors(&colors, min, max); G_write_colors(Cellmap_orig, G_mapset(), &colors); } /* Release memory resources */ G_free(data[0]); G_free(data[1]); G_done_msg(" "); exit(EXIT_SUCCESS); }
int IL_resample_output_2d(struct interp_params *params, double zmin, double zmax, /* min,max input z-values */ double zminac, double zmaxac, /* min,max interpolated values */ double c1min, double c1max, double c2min, double c2max, double gmin, double gmax, double ertot, /* total interplating func. error */ char *input, /* input file name */ double *dnorm, struct Cell_head *outhd, /* Region with desired resolution */ struct Cell_head *winhd, /* Current region */ char *smooth, int n_points) /* * Creates output files as well as history files and color tables for * them. */ { FCELL *cell1; /* cell buffer */ int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0, cf6 = 0; /* cell file descriptors */ int nrows, ncols; /* current region rows and columns */ int i; /* loop counter */ char *mapset; float dat1, dat2; struct Colors colors, colors2; double value1, value2; struct History hist, hist1, hist2, hist3, hist4, hist5; struct _Color_Rule_ *rule; char *maps, *type; int cond1, cond2; cond2 = ((params->pcurv != NULL) || (params->tcurv != NULL) || (params->mcurv != NULL)); cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2); /* change region to output cell file region */ fprintf(stderr, "Temporarily changing the region to desired resolution...\n"); if (G_set_window(outhd) < 0) { fprintf(stderr, "Cannot set region to output region!\n"); return -1; } mapset = G_mapset(); cell1 = G_allocate_f_raster_buf(); if (params->elev != NULL) { cf1 = G_open_fp_cell_new(params->elev); if (cf1 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->elev); return -1; } } if (params->slope != NULL) { cf2 = G_open_fp_cell_new(params->slope); if (cf2 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->slope); return -1; } } if (params->aspect != NULL) { cf3 = G_open_fp_cell_new(params->aspect); if (cf3 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->aspect); return -1; } } if (params->pcurv != NULL) { cf4 = G_open_fp_cell_new(params->pcurv); if (cf4 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->pcurv); return -1; } } if (params->tcurv != NULL) { cf5 = G_open_fp_cell_new(params->tcurv); if (cf5 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->tcurv); return -1; } } if (params->mcurv != NULL) { cf6 = G_open_fp_cell_new(params->mcurv); if (cf6 < 0) { fprintf(stderr, "unable to create raster map %s\n", params->mcurv); return -1; } } nrows = outhd->rows; if (nrows != params->nsizr) { fprintf(stderr, "first change your rows number(%d) to %d!\n", nrows, params->nsizr); return -1; } ncols = outhd->cols; if (ncols != params->nsizc) { fprintf(stderr, "first change your rows number(%d) to %d!\n", ncols, params->nsizc); return -1; } if (params->elev != NULL) { fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_z, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z); if (G_put_f_raster_row(cf1, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (params->slope != NULL) { fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_dx, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx); /* * for (ii==0;ii<params->nsizc;ii++) { fprintf(stderr,"ii=%d ",ii); * fprintf(stderr,"%f ",cell1[ii]); } * fprintf(stderr,"params->nsizc=%d \n",params->nsizc); */ if (G_put_f_raster_row(cf2, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (params->aspect != NULL) { fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_dy, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy); if (G_put_f_raster_row(cf3, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (params->pcurv != NULL) { fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_xx, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx); if (G_put_f_raster_row(cf4, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (params->tcurv != NULL) { fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_yy, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy); if (G_put_f_raster_row(cf5, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (params->mcurv != NULL) { fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */ for (i = 0; i < params->nsizr; i++) { /* seek to the right row */ if (fseek(params->Tmp_fd_xy, (long) ((params->nsizr - 1 - i) * params->nsizc * sizeof(FCELL)), 0) == -1) { fprintf(stderr, "cannot fseek to the right spot\n"); return -1; } fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy); if (G_put_f_raster_row(cf6, cell1) < 0) { fprintf(stderr, "cannot write file\n"); return -1; } } } if (cf1) G_close_cell(cf1); if (cf2) G_close_cell(cf2); if (cf3) G_close_cell(cf3); if (cf4) G_close_cell(cf4); if (cf5) G_close_cell(cf5); if (cf6) G_close_cell(cf6); /* write colormaps and history for output cell files */ /* colortable for elevations */ maps = G_find_file("cell", input, ""); if (params->elev != NULL) { if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", input); return -1; } G_init_colors(&colors2); /* * G_mark_colors_as_fp(&colors2); */ if (G_read_colors(input, maps, &colors) >= 0) { if (colors.modular.rules) { rule = colors.modular.rules; while (rule->next) rule = rule->next; for (; rule; rule = rule->prev) { value1 = rule->low.value * params->zmult; value2 = rule->high.value * params->zmult; G_add_modular_d_raster_color_rule(&value1, rule->low.red, rule->low.grn, rule->low.blu, &value2, rule->high.red, rule->high.grn, rule->high.blu, &colors2); } } if (colors.fixed.rules) { rule = colors.fixed.rules; while (rule->next) rule = rule->next; for (; rule; rule = rule->prev) { value1 = rule->low.value * params->zmult; value2 = rule->high.value * params->zmult; G_add_d_raster_color_rule(&value1, rule->low.red, rule->low.grn, rule->low.blu, &value2, rule->high.red, rule->high.grn, rule->high.blu, &colors2); } } maps = NULL; maps = G_find_file("cell", params->elev, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->elev); return -1; } if (G_write_colors(params->elev, maps, &colors2) < 0) { fprintf(stderr, "Cannot write color table\n"); return -1; } G_quantize_fp_map_range(params->elev, mapset, zminac - 0.5, zmaxac + 0.5, (CELL) (zminac - 0.5), (CELL) (zmaxac + 0.5)); } else fprintf(stderr, "No color table for input file -- will not create color table\n"); } /* colortable for slopes */ if (cond1 & (!params->deriv)) { G_init_colors(&colors); G_add_color_rule(0, 255, 255, 255, 2, 255, 255, 0, &colors); G_add_color_rule(2, 255, 255, 0, 5, 0, 255, 0, &colors); G_add_color_rule(5, 0, 255, 0, 10, 0, 255, 255, &colors); G_add_color_rule(10, 0, 255, 255, 15, 0, 0, 255, &colors); G_add_color_rule(15, 0, 0, 255, 30, 255, 0, 255, &colors); G_add_color_rule(30, 255, 0, 255, 50, 255, 0, 0, &colors); G_add_color_rule(50, 255, 0, 0, 90, 0, 0, 0, &colors); if (params->slope != NULL) { maps = NULL; maps = G_find_file("cell", params->slope, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->slope); return -1; } G_write_colors(params->slope, maps, &colors); G_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90); type = "raster"; G_short_history(params->slope, type, &hist1); if (params->elev != NULL) sprintf(hist1.edhist[0], "The elevation map is %s", params->elev); sprintf(hist1.datsrc_1, "raster map %s", input); hist1.edlinecnt = 1; G_write_history(params->slope, &hist1); } /* colortable for aspect */ G_init_colors(&colors); G_add_color_rule(0, 255, 255, 255, 0, 255, 255, 255, &colors); G_add_color_rule(1, 255, 255, 0, 90, 0, 255, 0, &colors); G_add_color_rule(90, 0, 255, 0, 180, 0, 255, 255, &colors); G_add_color_rule(180, 0, 255, 255, 270, 255, 0, 0, &colors); G_add_color_rule(270, 255, 0, 0, 360, 255, 255, 0, &colors); if (params->aspect != NULL) { maps = NULL; maps = G_find_file("cell", params->aspect, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->aspect); return -1; } G_write_colors(params->aspect, maps, &colors); G_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0, 360); type = "raster"; G_short_history(params->aspect, type, &hist2); if (params->elev != NULL) sprintf(hist2.edhist[0], "The elevation map is %s", params->elev); sprintf(hist2.datsrc_1, "raster map %s", input); hist2.edlinecnt = 1; G_write_history(params->aspect, &hist2); } /* colortable for curvatures */ if (cond2) { G_init_colors(&colors); dat1 = (FCELL) amin1(c1min, c2min); dat2 = (FCELL) - 0.01; G_add_f_raster_color_rule(&dat1, 50, 0, 155, &dat2, 0, 0, 255, &colors); dat1 = dat2; dat2 = (FCELL) - 0.001; G_add_f_raster_color_rule(&dat1, 0, 0, 255, &dat2, 0, 127, 255, &colors); dat1 = dat2; dat2 = (FCELL) - 0.00001; G_add_f_raster_color_rule(&dat1, 0, 127, 255, &dat2, 0, 255, 255, &colors); dat1 = dat2; dat2 = (FCELL) 0.00; G_add_f_raster_color_rule(&dat1, 0, 255, 255, &dat2, 200, 255, 200, &colors); dat1 = dat2; dat2 = (FCELL) 0.00001; G_add_f_raster_color_rule(&dat1, 200, 255, 200, &dat2, 255, 255, 0, &colors); dat1 = dat2; dat2 = (FCELL) 0.001; G_add_f_raster_color_rule(&dat1, 255, 255, 0, &dat2, 255, 127, 0, &colors); dat1 = dat2; dat2 = (FCELL) 0.01; G_add_f_raster_color_rule(&dat1, 255, 127, 0, &dat2, 255, 0, 0, &colors); dat1 = dat2; dat2 = (FCELL) amax1(c1max, c2max); G_add_f_raster_color_rule(&dat1, 255, 0, 0, &dat2, 155, 0, 20, &colors); maps = NULL; if (params->pcurv != NULL) { maps = G_find_file("cell", params->pcurv, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->pcurv); return -1; } G_write_colors(params->pcurv, maps, &colors); fprintf(stderr, "color map written\n"); G_quantize_fp_map_range(params->pcurv, mapset, dat1, dat2, (CELL) (dat1 * MULT), (CELL) (dat2 * MULT)); type = "raster"; G_short_history(params->pcurv, type, &hist3); if (params->elev != NULL) sprintf(hist3.edhist[0], "The elevation map is %s", params->elev); sprintf(hist3.datsrc_1, "raster map %s", input); hist3.edlinecnt = 1; G_write_history(params->pcurv, &hist3); } if (params->tcurv != NULL) { maps = NULL; maps = G_find_file("cell", params->tcurv, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->tcurv); return -1; } G_write_colors(params->tcurv, maps, &colors); G_quantize_fp_map_range(params->tcurv, mapset, dat1, dat2, (CELL) (dat1 * MULT), (CELL) (dat2 * MULT)); type = "raster"; G_short_history(params->tcurv, type, &hist4); if (params->elev != NULL) sprintf(hist4.edhist[0], "The elevation map is %s", params->elev); sprintf(hist4.datsrc_1, "raster map %s", input); hist4.edlinecnt = 1; G_write_history(params->tcurv, &hist4); } if (params->mcurv != NULL) { maps = NULL; maps = G_find_file("cell", params->mcurv, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found\n", params->mcurv); return -1; } G_write_colors(params->mcurv, maps, &colors); G_quantize_fp_map_range(params->mcurv, mapset, dat1, dat2, (CELL) (dat1 * MULT), (CELL) (dat2 * MULT)); type = "raster"; G_short_history(params->mcurv, type, &hist5); if (params->elev != NULL) sprintf(hist5.edhist[0], "The elevation map is %s", params->elev); sprintf(hist5.datsrc_1, "raster map %s", input); hist5.edlinecnt = 1; G_write_history(params->mcurv, &hist5); } } } if (params->elev != NULL) { maps = G_find_file("cell", params->elev, ""); if (maps == NULL) { fprintf(stderr, "file [%s] not found \n", params->elev); return -1; } G_short_history(params->elev, "raster", &hist); if (smooth != NULL) sprintf(hist.edhist[0], "tension=%f, smoothing=%s", params->fi * 1000. / (*dnorm), smooth); else sprintf(hist.edhist[0], "tension=%f", params->fi * 1000. / (*dnorm)); sprintf(hist.edhist[1], "dnorm=%f, zmult=%f", *dnorm, params->zmult); sprintf(hist.edhist[2], "KMAX=%d, KMIN=%d, errtotal=%f", params->kmax, params->kmin, sqrt(ertot / n_points)); sprintf(hist.edhist[3], "zmin_data=%f, zmax_data=%f", zmin, zmax); sprintf(hist.edhist[4], "zmin_int=%f, zmax_int=%f", zminac, zmaxac); sprintf(hist.datsrc_1, "raster map %s", input); hist.edlinecnt = 5; G_write_history(params->elev, &hist); } /* change region to initial region */ fprintf(stderr, "Changing the region back to initial...\n"); if (G_set_window(winhd) < 0) { fprintf(stderr, "Cannot set region to back to initial region!\n"); return -1; } return 1; }