save.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* save.c */
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "ransurf.h"
  7. void SaveMap(int NumMap, int MapSeed)
  8. {
  9. int Index, Row, Col, NormIndex;
  10. int LowColor, HighColor;
  11. double DownInterval, UpInterval, Value = 0, Ratio, MeanMod;
  12. struct Categories Cats;
  13. struct Colors Colr;
  14. char String[80], Label[240];
  15. struct History history;
  16. CELL cat;
  17. G_debug(2, "SaveMap()");
  18. OutFD = Rast_open_c_new(OutNames[NumMap]);
  19. MeanMod = 0.0;
  20. G_debug(3, "(FDM):%d", FDM);
  21. if (FDM == -1) {
  22. for (Row = 0; Row < Rs; Row++) {
  23. for (Col = 0; Col < Cs; Col++) {
  24. Value = Surface[Row][Col];
  25. MeanMod += Value;
  26. }
  27. }
  28. MeanMod /= MapCount;
  29. /* Value = (Value - MeanMod) / FilterSD + MeanMod / FilterSD; */
  30. Value /= FilterSD;
  31. DownInterval = UpInterval = Value;
  32. for (Row = 0; Row < Rs; Row++) {
  33. for (Col = 0; Col < Cs; Col++) {
  34. Value = Surface[Row][Col];
  35. /*
  36. Value = (Value - MeanMod) / FilterSD
  37. + MeanMod / FilterSD;
  38. */
  39. Value /= FilterSD;
  40. Surface[Row][Col] = Value;
  41. if (UpInterval < Value)
  42. UpInterval = Value;
  43. if (DownInterval > Value)
  44. DownInterval = Value;
  45. }
  46. }
  47. }
  48. else {
  49. for (Row = 0; Row < Rs; Row++) {
  50. Rast_get_c_row_nomask(FDM, CellBuffer, Row);
  51. for (Col = 0; Col < Cs; Col++) {
  52. if (CellBuffer[Col] != 0) {
  53. Value = Surface[Row][Col];
  54. MeanMod += Value;
  55. }
  56. }
  57. }
  58. MeanMod /= MapCount;
  59. G_debug(3, "(MeanMod):%.12lf", MeanMod);
  60. G_debug(3, "(FilterSD):%.12lf", FilterSD);
  61. /* Value = (Value - MeanMod) / FilterSD + MeanMod / FilterSD; */
  62. Value /= FilterSD;
  63. G_debug(3, "(Value):%.12lf", Value);
  64. DownInterval = UpInterval = Value;
  65. for (Row = 0; Row < Rs; Row++) {
  66. Rast_get_c_row_nomask(FDM, CellBuffer, Row);
  67. for (Col = 0; Col < Cs; Col++) {
  68. if (CellBuffer[Col] != 0) {
  69. Value = Surface[Row][Col];
  70. /*
  71. Value = (Value - MeanMod) / FilterSD
  72. + MeanMod / FilterSD;
  73. */
  74. Value /= FilterSD;
  75. Surface[Row][Col] = Value;
  76. if (UpInterval < Value)
  77. UpInterval = Value;
  78. if (DownInterval > Value)
  79. DownInterval = Value;
  80. }
  81. }
  82. }
  83. }
  84. G_message(_("Writing raster map <%s>..."), OutNames[NumMap]);
  85. for (Index = 0; Index < CatInfo.NumCat; Index++) {
  86. CatInfo.Max[Index] = DownInterval;
  87. CatInfo.Min[Index] = UpInterval;
  88. CatInfo.NumValue[Index] = 0;
  89. CatInfo.Average[Index] = 0.0;
  90. }
  91. if (DownInterval == UpInterval)
  92. UpInterval += .1;
  93. if (!Uniform->answer) {
  94. /* normal distribution */
  95. for (Row = 0; Row < Rs; Row++) {
  96. for (Col = 0; Col < Cs; Col++) {
  97. Value = Surface[Row][Col];
  98. if (Value > UpInterval) {
  99. Value = UpInterval;
  100. }
  101. else if (Value < DownInterval) {
  102. Value = DownInterval;
  103. }
  104. Ratio = (Value - DownInterval) / (UpInterval - DownInterval);
  105. /* Ratio in the range of [0..1] */
  106. Index = (int)((Ratio * CatInfo.NumCat) - .5);
  107. CatInfo.NumValue[Index]++;
  108. CatInfo.Average[Index] += Value;
  109. if (Value > CatInfo.Max[Index])
  110. CatInfo.Max[Index] = Value;
  111. if (Value < CatInfo.Min[Index])
  112. CatInfo.Min[Index] = Value;
  113. Surface[Row][Col] = 1 + Index;
  114. }
  115. }
  116. }
  117. else {
  118. /* mapping to cumulative normal distribution function */
  119. for (Row = 0; Row < Rs; Row++) {
  120. for (Col = 0; Col < Cs; Col++) {
  121. Value = Surface[Row][Col];
  122. Ratio = (double)(Value - MIN_INTERVAL) /
  123. (MAX_INTERVAL - MIN_INTERVAL);
  124. Index = (int)(Ratio * (SIZE_OF_DISTRIBUTION - 1));
  125. /* Norm[Index] is always smaller than 1. */
  126. NormIndex = (int)(Norm[Index] * CatInfo.NumCat);
  127. /* record the catogory information */
  128. CatInfo.NumValue[NormIndex]++;
  129. CatInfo.Average[NormIndex] += Value;
  130. if (Value > CatInfo.Max[NormIndex])
  131. CatInfo.Max[NormIndex] = Value;
  132. if (Value < CatInfo.Min[NormIndex])
  133. CatInfo.Min[NormIndex] = Value;
  134. /* NormIndex in range of [0 .. (CatInfo.NumCat-1)] */
  135. Surface[Row][Col] = 1 + NormIndex;
  136. }
  137. }
  138. }
  139. for (Row = 0; Row < Rs; Row++) {
  140. G_percent(Row, Rs, 2);
  141. for (Col = 0; Col < Cs; Col++) {
  142. CellBuffer[Col] = (CELL) Surface[Row][Col];
  143. }
  144. Rast_put_row(OutFD, CellBuffer, CELL_TYPE);
  145. }
  146. G_percent(1, 1, 1);
  147. Rast_close(OutFD);
  148. Rast_short_history(OutNames[NumMap], "raster", &history);
  149. Rast_command_history(&history);
  150. Rast_write_history(OutNames[NumMap], &history);
  151. strcpy(Label, Buf);
  152. sprintf(String, " seed=%d", MapSeed);
  153. strcat(Label, String);
  154. /*
  155. if( NumMap == 0 && Theory > 0)
  156. TheoryCovariance( TheoryName, Label);
  157. */
  158. Rast_init_cats(Label, &Cats);
  159. for (Index = 0; Index < CatInfo.NumCat; Index++) {
  160. if (CatInfo.NumValue[Index] != 0) {
  161. CatInfo.Average[Index] /= CatInfo.NumValue[Index];
  162. sprintf(Label, "%+lf %+lf to %+lf",
  163. CatInfo.Average[Index],
  164. CatInfo.Min[Index], CatInfo.Max[Index]);
  165. cat = Index + 1;
  166. Rast_set_c_cat(&cat, &cat, Label, &Cats);
  167. }
  168. }
  169. Rast_write_cats(OutNames[NumMap], &Cats);
  170. Rast_init_colors(&Colr);
  171. LowColor = (int)(127.5 * (CatInfo.Average[0] + 3.5) / 3.5);
  172. HighColor = (int)(255.0 - 127.5 *
  173. (3.5 - CatInfo.Average[CatInfo.NumCat - 1]) / 3.5);
  174. if (Uniform->answer || LowColor < 0)
  175. LowColor = 0;
  176. if (Uniform->answer || HighColor > 255)
  177. HighColor = 255;
  178. G_debug(3, "(LowColor):%d", LowColor);
  179. G_debug(3, "(HighColor):%d", HighColor);
  180. Rast_add_c_color_rule(&Low, LowColor, LowColor, LowColor,
  181. &High, HighColor, HighColor, HighColor, &Colr);
  182. Rast_write_colors(OutNames[NumMap], G_mapset(), &Colr);
  183. }