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; }
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); }