Ejemplo n.º 1
0
int Init_graphics(void)
{
    D_full_screen();


    SCREEN_TOP = R_screen_top();
    SCREEN_BOTTOM = R_screen_bot();
    SCREEN_LEFT = R_screen_left();
    SCREEN_RIGHT = R_screen_rite();

    R_standard_color(WHITE);

    VIEW_TITLE1 = makeview(97.5, 100.0, 0.0, 50.0);
    VIEW_TITLE2 = makeview(97.5, 100.0, 50.0, 100.0);
    VIEW_MAP1 = makeview(51.0, 97.5, 0.0, 50.0);
    VIEW_MAP2 = makeview(51.0, 97.5, 50.0, 100.0);
    VIEW_TITLE1_ZOOM = makeview(47.5, 51.0, 0.0, 50.0);
    VIEW_TITLE2_ZOOM = makeview(47.5, 51.0, 50.0, 100.0);
    VIEW_MAP1_ZOOM = makeview(2.5, 47.5, 0.0, 50.0);
    VIEW_MAP2_ZOOM = makeview(2.5, 47.5, 50.0, 100.0);
    VIEW_MENU = makeview(0.0, 2.5, 0.0, 100.0);

    G_init_colors(&VIEW_MAP1->cell.colors);
    G_init_colors(&VIEW_MAP2->cell.colors);

    return 0;
}
Ejemplo n.º 2
0
int G_read_colors(const char *name, const char *mapset, struct Colors *colors)
{
    int fp;
    char buf[GNAME_MAX];
    char *err;
    char xname[GNAME_MAX];
    struct Range range;
    struct FPRange drange;
    CELL min, max;
    DCELL dmin, dmax;

    fp = G_raster_map_is_fp(name, mapset);
    G_init_colors(colors);

    strcpy(xname, name);
    mapset = G_find_cell(xname, mapset);
    name = xname;

    if (fp)
	G_mark_colors_as_fp(colors);

    /* first look for secondary color table in current mapset */
    sprintf(buf, "colr2/%s", mapset);
    if (read_colors(buf, name, G_mapset(), colors) >= 0)
	return 1;

    /* now look for the regular color table */
    switch (read_colors("colr", name, mapset, colors)) {
    case -2:
	if (!fp) {
	    if (G_read_range(name, mapset, &range) >= 0) {
		G_get_range_min_max(&range, &min, &max);
		if (!G_is_c_null_value(&min) && !G_is_c_null_value(&max))
		    G_make_rainbow_colors(colors, min, max);
		return 0;
	    }
	}
	else {
	    if (G_read_fp_range(name, mapset, &drange) >= 0) {
		G_get_fp_range_min_max(&drange, &dmin, &dmax);
		if (!G_is_d_null_value(&dmin) && !G_is_d_null_value(&dmax))
		    G_make_rainbow_fp_colors(colors, dmin, dmax);
		return 0;
	    }
	}
	err = "missing";
	break;
    case -1:
	err = "invalid";
	break;
    default:
	return 1;
    }

    G_warning(_("color support for [%s] in mapset [%s] %s"), name, mapset,
	      err);
    return -1;
}
Ejemplo n.º 3
0
int make_gray_scale(struct Colors *gray)
{
    int i;

    G_init_colors(gray);

    for (i = 0; i < 256; i++)
	G_set_color((CELL) i, i, i, i, gray);

    return 0;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
0
int G_ask_colors(const char *name, const char *mapset, struct Colors *pcolr)
{
    char buff[128];
    int answ;
    struct FPRange range;
    DCELL min, max;

    G_init_colors(pcolr);

    /* determine range cell values */
    if (G_read_fp_range(name, mapset, &range) < 0)
	return -1;
    G_get_fp_range_min_max(&range, &min, &max);
    if (G_is_d_null_value(&min) || G_is_d_null_value(&max)) {
	sprintf(buff, _(" The raster map %s@%s is empty"), name, mapset);
	G_warning(buff);
	return -1;
    }

    /* Prompting */
  ASK:
    G_clear_screen();
    fprintf(stderr,
	    _("\n\nColor table needed for file [%s] in mapset [%s].\n"), name,
	    mapset);

    fprintf(stderr, _("\nPlease identify the type desired:\n"));
    fprintf(stderr, _("    1:  Random colors\n"));
    fprintf(stderr, _("    2:  Red, green, and blue color ramps\n"));
    fprintf(stderr, _("    3:  Color wave\n"));
    fprintf(stderr, _("    4:  Gray scale\n"));
    fprintf(stderr, _("    5:  Aspect\n"));
    fprintf(stderr, _("    6:  Rainbow colors\n"));
    fprintf(stderr, _("    7:  Red through yellow to green\n"));
    fprintf(stderr, _("    8:  Green through yellow to red\n"));
    fprintf(stderr, _("RETURN  quit\n"));
    fprintf(stderr, "\n> ");

    for (;;) {
	if (!G_gets(buff))
	    goto ASK;
	G_strip(buff);
	if (*buff == 0)
	    return -1;
	if (sscanf(buff, "%d", &answ) != 1)
	    answ = -1;

	switch (answ) {
	case 1:
	    return G_make_random_colors(pcolr, (CELL) min, (CELL) max);
	case 2:
	    return G_make_ramp_fp_colors(pcolr, min, max);
	case 3:
	    return G_make_wave_fp_colors(pcolr, min, max);
	case 4:
	    return G_make_grey_scale_fp_colors(pcolr, min, max);
	case 5:
	    return G_make_aspect_fp_colors(pcolr, min, max);
	case 6:
	    return G_make_rainbow_fp_colors(pcolr, min, max);
	case 7:
	    return G_make_ryg_fp_colors(pcolr, min, max);
	case 8:
	    return G_make_gyr_fp_colors(pcolr, min, max);
	default:
	    fprintf(stderr, _("\n%s invalid; Try again > "), buff);
	    break;
	}
    }
}
Ejemplo n.º 6
0
static int read_old_colors(FILE * fd, struct Colors *colors)
{
    char buf[256];
    long n;
    long min;
    float red_f, grn_f, blu_f;
    int red, grn, blu;
    int old;
    int zero;

    G_init_colors(colors);
    /*
     * first line in pre 3.0 color files is number of colors - ignore
     * otherwise it is #min first color, and the next line is for color 0
     */
    if (fgets(buf, sizeof buf, fd) == NULL)
	return -1;

    G_strip(buf);
    if (*buf == '#') {		/* 3.0 format */
	old = 0;
	if (sscanf(buf + 1, "%ld", &min) != 1)	/* first color */
	    return -1;
	zero = 1;
    }
    else {
	old = 1;
	min = 0;
	zero = 0;
    }

    colors->cmin = min;
    n = min;
    while (fgets(buf, sizeof buf, fd)) {
	if (old) {
	    if (sscanf(buf, "%f %f %f", &red_f, &grn_f, &blu_f) != 3)
		return -1;

	    red = 256 * red_f;
	    grn = 256 * grn_f;
	    blu = 256 * blu_f;
	}
	else {
	    switch (sscanf(buf, "%d %d %d", &red, &grn, &blu)) {
	    case 1:
		blu = grn = red;
		break;
	    case 2:
		blu = grn;
		break;
	    case 3:
		break;
	    default:
		return -1;
	    }
	}
	if (zero) {
	    G__insert_color_into_lookup((CELL) 0, red, grn, blu,
					&colors->fixed);
	    zero = 0;
	}
	else
	    G__insert_color_into_lookup((CELL) n++, red, grn, blu,
					&colors->fixed);
    }
    colors->cmax = n - 1;

    return 0;
}
Ejemplo n.º 7
0
 void make_result_row ( int val, char *basename, char **hyps, int no_hyps, Sresult_struct *result_row, xmlDocPtr doc ) {
	int i;
	char* val_names[NUMVALS]={"bel","pl","doubt","common","bint","woc","maxbpa","minbpa",
				  "maxsrc","minsrc"};
	DCELL *v1;
	DCELL *v2;
		
	result_row->use = YES;
	/* need an array of DCELL rows to store bel, pl and other DST values */
	if ( val == WOC ) {
		/* WOC (Weight of Conflict is always treated a bit differently,
		   because we need this only once for all hypotheses in the FOD */
		result_row->row[0] = (DCELL*) G_allocate_d_raster_buf ();
	} else {
		if (( val == MAXSRC) || ( val == MINSRC)) {
			for (i = 0; i < no_hyps; i++ ) {
				result_row->crow[i] = (CELL*) G_allocate_c_raster_buf ();
			}
		} else {						
			for (i = 0; i < no_hyps; i++ ) {
				result_row->row[i] = (DCELL*) G_allocate_d_raster_buf ();
			}
		}
	}
	
	if ( val == WOC ) {
		result_row->filename = (char**) G_calloc ( sizeof (char*), 1);    
		/* there is only one file for storing the WOC */
		result_row->filename[0] = G_malloc ((unsigned) ((sizeof (char) * strlen (basename)) +
											(sizeof (char) * strlen (val_names[val])) +		
											2));		
		strcpy (result_row->filename[0],basename);		
		strcat (result_row->filename[0],".");
		strcat (result_row->filename[0],val_names[val]);	
	} else {
		result_row->filename = (char**) G_calloc ( sizeof (char*), (unsigned) no_hyps);    
		/* for all other metrics, we need one output file per hypothesis */		
		for (i=0; i<no_hyps;i++) {		
			result_row->filename[i] = G_malloc ((unsigned)((sizeof (char) * strlen (basename)) +
										 		(sizeof (char) * strlen (hyps[i])) +	
												(sizeof (char) * strlen (val_names[val])) +		
												3));
			strcpy (result_row->filename[i],basename);
			strcat (result_row->filename[i],".");
			strcat (result_row->filename[i],hyps[i]);
			strcat (result_row->filename[i],".");
			strcat (result_row->filename[i],val_names[val]);
			G_strchg (result_row->filename[i], ',', '.');
		}
	}
	/* allocate file descriptors */
	if ( val == WOC ) {
		result_row->fd = (int*) G_calloc ( sizeof (int), 1);
		result_row->fd[0] = -1;
	} else {
		result_row->fd = (int*) G_calloc ( sizeof (int), (unsigned) no_hyps);
		for (i=0; i<no_hyps;i++) {		
			result_row->fd[i] = -1;
		}
	}
	/* init color tables for output maps */	
	v1 = (DCELL*) G_malloc (sizeof (DCELL));
	v2 = (DCELL*) G_malloc (sizeof (DCELL));
	if ( val == WOC ) {
			result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), 1);
			result_row->colors[0] = G_malloc ( sizeof (struct Colors));		
			G_init_colors (result_row->colors[0]);
			/* *v1 = (DCELL) WOC_MIN; *v2 = (DCELL) WOC_MAX; */
			*v1 = 0; *v2 = 1.001;
			G_add_d_raster_color_rule (v1,0,0,0,v2,255,0,0, result_row->colors[0]);			
	}
	if (( val == BINT ) || (val==MAXBPA) || (val==MINBPA) ){
		result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), (unsigned) no_hyps);
		for (i=0; i<no_hyps;i++) {
			result_row->colors[i] = G_malloc ( sizeof (struct Colors));		
			G_init_colors (result_row->colors[i]);
			*v1 = 0; *v2 = 1.001;
			G_add_d_raster_color_rule (v1,0,0,0,v2,255,0,0, result_row->colors[i]);			
		}	
	} 
	if ((val == BEL) || (val==PL) || (val==DOUBT) || (val==COMMON )) {
		result_row->colors = (struct Colors **) G_calloc ( sizeof (struct Colors*), (unsigned) no_hyps);
		for (i=0; i<no_hyps;i++) {
			result_row->colors[i] = G_malloc ( sizeof (struct Colors));		
			G_init_colors (result_row->colors[i]);
			*v1 = 0; *v2 = 0.5;
			G_add_d_raster_color_rule (v1,36,216,72,v2,216,201,36, result_row->colors[i]);						
			*v1 = 0.500001; *v2 = 1.001;
			G_add_d_raster_color_rule (v1,216,201,36,v2,216,36,39, result_row->colors[i]);								
			/*
			*v1 = 0; *v2 = 0.333333;
			G_add_d_raster_color_rule (v1,36,216,072,v2,36,216,072, result_row->colors[i]);			
			*v1 = 0.333334; *v2 = 0.666666;
			G_add_d_raster_color_rule (v1,216,201,36,v2,216,201,36, result_row->colors[i]);			
			*v1 = 0.666667; *v2 = 1;
			G_add_d_raster_color_rule (v1,216,36,39,v2,216,36,39, result_row->colors[i]);			
			*/
		}
	}
	
	/* allocate pointers into array of ordered hypotheses */
	/* this is a look-up table for faster access to the 'real' */
	/* index of a hypothesis in Theta */
	{
		result_row->hyp_idx = (long*) G_calloc ( sizeof (int), (unsigned) no_hyps);
		for (i=0; i<no_hyps;i++) {		
			result_row->hyp_idx[i] = find_hyp_idx( hyps[i], doc );
		}
	}
}
Ejemplo n.º 8
0
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;
}