support.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <string.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. /* function prototypes */
  7. static void write_history(int, char *, double **, double *);
  8. void write_support(int bands, char *inname, char *outname, double **eigmat, double *eigval)
  9. {
  10. const char *mapset = G_mapset();
  11. struct Colors colors;
  12. struct FPRange range;
  13. DCELL min, max;
  14. if (inname) {
  15. Rast_read_colors(inname, "", &colors);
  16. }
  17. else {
  18. /* make grey scale color table */
  19. Rast_read_fp_range(outname, mapset, &range);
  20. Rast_get_fp_range_min_max(&range, &min, &max);
  21. Rast_make_grey_scale_fp_colors(&colors, min, max);
  22. }
  23. if (Rast_map_is_fp(outname, mapset))
  24. Rast_mark_colors_as_fp(&colors);
  25. Rast_write_colors(outname, mapset, &colors);
  26. write_history(bands, outname, eigmat, eigval);
  27. }
  28. static void write_history(int bands, char *outname, double **eigmat, double *eigval)
  29. {
  30. int i, j;
  31. static int first_map = TRUE; /* write to stderr? */
  32. struct History hist;
  33. double eigval_total = 0.0;
  34. Rast_short_history(outname, "raster", &hist);
  35. Rast_append_history(&hist, "Eigen values, (vectors), and [percent importance]:");
  36. if(first_map)
  37. G_message(_("Eigen values, (vectors), and [percent importance]:"));
  38. for (i = 0; i < bands; i++)
  39. eigval_total += eigval[i];
  40. for (i = 0; i < bands; i++) {
  41. char tmpeigen[2048], tmpa[80]; /* (bands*8)+30 instead of 2048? */
  42. sprintf(tmpeigen, "PC%d %9.2f (", i+1, eigval[i]);
  43. for (j = 0; j < bands; j++) {
  44. sprintf(tmpa, "%7.4f", eigmat[i][j]);
  45. strcat(tmpeigen, tmpa);
  46. if (j < (bands - 1) ){
  47. sprintf(tmpa, ",");
  48. strcat(tmpeigen, tmpa);
  49. }
  50. }
  51. strcat(tmpeigen, ") ");
  52. sprintf(tmpa, "[%5.2f%%]", eigval[i] * 100 / eigval_total);
  53. strcat(tmpeigen, tmpa);
  54. Rast_append_history(&hist, tmpeigen);
  55. /* write eigen values to screen */
  56. if (first_map)
  57. fprintf(stdout, "%s\n", tmpeigen);
  58. }
  59. /* only write to stderr the first time (this fn runs for every output map) */
  60. first_map = FALSE;
  61. Rast_command_history(&hist);
  62. Rast_write_history(outname, &hist);
  63. }