int E_edit_fp_cats(const char *name, struct Categories *cats) { long incr; long atnum; long startcat; long endcat; char buff[NLINES][60]; char next[20]; char next_line[80]; char title[80]; char msg1[80]; char msg2[80]; int line, ncats; size_t lab_len; DCELL max_val[NLINES], min_val[NLINES]; DCELL dmin, dmax; CELL tmp_cell; struct Categories old_cats; struct FPRange fp_range; if (G_read_fp_range(name, G_mapset(), &fp_range) < 0) G_fatal_error("can't read the floating point range for %s", name); G_get_fp_range_min_max(&fp_range, &dmin, &dmax); /* first save old cats */ G_copy_raster_cats(&old_cats, cats); G_init_raster_cats(old_cats.title, cats); G_free_raster_cats(cats); ncats = old_cats.ncats; V_clear(); if (!ncats) sprintf(msg1, "There are no predefined fp ranges to label"); else sprintf(msg1, "There are %d predefined fp ranges to label", ncats); sprintf(msg2, "Enter the number of fp ranges you want to label"); V_line(1, msg1); V_line(2, msg2); V_ques(&ncats, 'l', 2, 48, 5); V_line(16, next_line); *next_line = 0; V_intrpt_ok(); if (!V_call()) return -1; *title = 0; if (old_cats.title != NULL) strcpy(title, old_cats.title); startcat = 0; sprintf(msg1, "The fp data in map %s ranges from %f to %f", name, dmin, dmax); sprintf(msg2, "[%s] ENTER NEW CATEGORY NAMES FOR THESE CATEGORIES", name); while (1) { V_clear(); V_line(0, msg1); V_line(1, msg2); V_line(3, "TITLE: "); V_line(4, "FP RANGE NEW CATEGORY NAME"); V_ques(title, 's', 2, 8, 60); endcat = startcat + NLINES <= ncats ? startcat + NLINES : ncats; line = 6; for (incr = startcat; incr < endcat; incr++) { atnum = incr - startcat; if (incr < old_cats.ncats) { /* if editing existing range label */ lab_len = strlen(old_cats.labels[incr]); if (lab_len > 58) lab_len = 58; strncpy(buff[atnum], old_cats.labels[incr], lab_len); buff[atnum][lab_len] = 0; G_quant_get_ith_rule(&old_cats.q, incr, &min_val[atnum], &max_val[atnum], &tmp_cell, &tmp_cell); } else { /* adding new range label */ strcpy(buff[atnum], ""); max_val[atnum] = min_val[atnum] = 0; } V_ques(&min_val[atnum], 'd', line, 1, 8); V_const("-", 's', line, 9, 1); V_ques(&max_val[atnum], 'd', line, 10, 8); V_ques(buff[atnum], 's', line, 19, 58); line++; } line += 2; *next = 0; if (endcat >= ncats) strcpy(next, "end"); else sprintf(next, "%ld", endcat); sprintf(next_line, "%*s%*s (of %d)", 41, "Next range number ('end' to end): ", 5, "", ncats); V_line(line, next_line); V_ques(next, 's', line, 41, 5); V_intrpt_ok(); if (!V_call()) return -1; /* store new category name in structure */ for (incr = startcat; incr < endcat; incr++) { atnum = incr - startcat; G_strip(buff[atnum]); /* adding new range label */ if (!(strcmp(buff[atnum], "") == 0 && min_val[atnum] == 0. && max_val[atnum] == 0.)) G_set_d_raster_cat(&min_val[atnum], &max_val[atnum], buff[atnum], cats); } if (*next == 0) break; if (strcmp(next, "end") == 0) break; if (sscanf(next, "%ld", &endcat) != 1) continue; if (endcat < 0) endcat = 0; if (endcat > ncats) { endcat = ncats - NLINES + 1; if (endcat < 0) endcat = 0; } startcat = endcat; } G_strip(title); cats->title = G_store(title); /* since label pointers in old_cats point to the old allocated strings, and cats now has all the newly allocated strings, it never reuses old ones, we need to free them */ return (1); }
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); }