calc_linefax.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include "vizual.h"
  4. int viz_calc_tvals(cmndln_info * linefax, char **a_levels, char *a_min,
  5. char *a_max, char *a_step, char *a_tnum, int quiet)
  6. {
  7. double interval; /*increment tvalue for intervals */
  8. float datarange; /*diff btwn min and max data tvalues */
  9. float tval; /*the threshold value being computed */
  10. float min, max;
  11. int ithresh;
  12. int do_interval = 0;
  13. int i; /*looping variable */
  14. /*
  15. ** Note that the maximum number of thresholds
  16. ** that are computed is 127. This is to set a cap on the size of the
  17. ** display file that is created */
  18. /* If levels are specified, only use those. Otherwise, if step is
  19. specified use that, otherwise use number of thresholds */
  20. min = Headfax.min;
  21. max = Headfax.max;
  22. if (a_min)
  23. sscanf(a_min, "%f", &min);
  24. if (a_max)
  25. sscanf(a_max, "%f", &max);
  26. datarange = max - min;
  27. if (datarange <= 0.0)
  28. G_fatal_error("Range error: %f", datarange);
  29. if (a_levels) {
  30. for (i = 0; a_levels[i]; i++) {
  31. if (i == MAXTHRESH) {
  32. G_warning("Maximum no. of thresholds is %d", MAXTHRESH);
  33. break;
  34. }
  35. if (1 != sscanf(a_levels[i], "%f", &(linefax->tvalue[i]))) {
  36. G_usage();
  37. exit(0);
  38. }
  39. }
  40. linefax->nthres = i;
  41. }
  42. else if (a_step) {
  43. sscanf(a_step, "%lf", &interval);
  44. do_interval = 1;
  45. }
  46. else if (a_tnum) { /* should already be default even if not specified */
  47. sscanf(a_tnum, "%d", &ithresh);
  48. if (ithresh < 2) {
  49. ithresh = 2;
  50. G_warning("Minimum number of thresholds is 2");
  51. }
  52. interval = datarange / (ithresh - 1);
  53. do_interval = 1;
  54. }
  55. else { /* should never get here */
  56. G_usage();
  57. exit(0);
  58. }
  59. if (do_interval) {
  60. for (i = 0, tval = min; tval <= max; i++, tval = min + (i * interval)) {
  61. if (i == MAXTHRESH) {
  62. G_warning("Maximum no. of thresholds is %d", MAXTHRESH);
  63. break;
  64. }
  65. linefax->tvalue[i] = tval;
  66. }
  67. linefax->nthres = i;
  68. }
  69. if (!quiet) {
  70. fprintf(stderr, "threshold values: ");
  71. for (i = 0; i < linefax->nthres; i++)
  72. fprintf(stderr, "%f ", linefax->tvalue[i]);
  73. G_message("No. of thresholds: %i", linefax->nthres + 1);
  74. }
  75. return (0);
  76. }