formspecific.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. /* write the GRASS ASCII heading */
  5. int writeGRASSheader(FILE * fp)
  6. {
  7. struct Cell_head region;
  8. char buf[128];
  9. G_get_window(&region);
  10. G_format_northing(region.north, buf, region.proj);
  11. fprintf(fp, "north: %s\n", buf);
  12. G_format_northing(region.south, buf, region.proj);
  13. fprintf(fp, "south: %s\n", buf);
  14. G_format_easting(region.east, buf, region.proj);
  15. fprintf(fp, "east: %s\n", buf);
  16. G_format_easting(region.west, buf, region.proj);
  17. fprintf(fp, "west: %s\n", buf);
  18. fprintf(fp, "rows: %d\n", region.rows);
  19. fprintf(fp, "cols: %d\n", region.cols);
  20. return 0;
  21. }
  22. /* write GRASS ASCII GRID */
  23. int write_GRASS(int fd,
  24. FILE * fp,
  25. int nrows, int ncols, int out_type, int dp, char *null_str)
  26. {
  27. int row, col;
  28. void *ptr, *raster;
  29. char cell_buf[300];
  30. raster = Rast_allocate_buf(out_type);
  31. for (row = 0; row < nrows; row++) {
  32. G_percent(row, nrows, 2);
  33. Rast_get_row(fd, raster, row, out_type);
  34. for (col = 0, ptr = raster; col < ncols; col++,
  35. ptr = G_incr_void_ptr(ptr, Rast_cell_size(out_type))) {
  36. if (!Rast_is_null_value(ptr, out_type)) {
  37. if (out_type == CELL_TYPE)
  38. fprintf(fp, "%d", *((CELL *) ptr));
  39. else if (out_type == FCELL_TYPE) {
  40. sprintf(cell_buf, "%.*f", dp, *((FCELL *) ptr));
  41. G_trim_decimal(cell_buf);
  42. fprintf(fp, "%s", cell_buf);
  43. }
  44. else if (out_type == DCELL_TYPE) {
  45. sprintf(cell_buf, "%.*f", dp, *((DCELL *) ptr));
  46. G_trim_decimal(cell_buf);
  47. fprintf(fp, "%s", cell_buf);
  48. }
  49. }
  50. else
  51. fprintf(fp, "%s", null_str);
  52. fprintf(fp, " ");
  53. }
  54. fprintf(fp, "\n");
  55. }
  56. return (0);
  57. }
  58. int writeMFheader(FILE * fp, int dp, int width, int out_type)
  59. {
  60. if (out_type == CELL_TYPE)
  61. fprintf(fp, "INTERNAL 1 (FREE) -1\n");
  62. else
  63. fprintf(fp, "INTERNAL 1. (%de%d.%d) -1\n", width, dp + 6, dp - 1);
  64. return 0;
  65. }
  66. /* write MODFLOW ASCII ARRAY */
  67. int write_MODFLOW(int fd,
  68. FILE * fp,
  69. int nrows, int ncols, int out_type, int dp, int width)
  70. {
  71. int row, col, colcnt;
  72. void *ptr, *raster;
  73. raster = Rast_allocate_buf(out_type);
  74. for (row = 0; row < nrows; row++) {
  75. G_percent(row, nrows, 2);
  76. Rast_get_row(fd, raster, row, out_type);
  77. colcnt = 0;
  78. for (col = 0, ptr = raster; col < ncols; col++,
  79. ptr = G_incr_void_ptr(ptr, Rast_cell_size(out_type))) {
  80. if (out_type == CELL_TYPE) {
  81. if (Rast_is_null_value(ptr, out_type))
  82. *((CELL *) ptr) = 0;
  83. fprintf(fp, " %d", *((CELL *) ptr));
  84. }
  85. else if (out_type == FCELL_TYPE) {
  86. if (Rast_is_null_value(ptr, out_type))
  87. *((FCELL *) ptr) = 0;
  88. fprintf(fp, "%*.*e", dp + 6, dp - 1, *((FCELL *) ptr));
  89. }
  90. else if (out_type == DCELL_TYPE) {
  91. if (Rast_is_null_value(ptr, out_type))
  92. *((DCELL *) ptr) = 0;
  93. fprintf(fp, "%*.*e", dp + 6, dp - 1, *((DCELL *) ptr));
  94. }
  95. colcnt += 1;
  96. if (colcnt >= width) {
  97. colcnt = 0;
  98. fprintf(fp, "\n");
  99. }
  100. }
  101. if (colcnt > 0)
  102. fprintf(fp, "\n");
  103. }
  104. return (0);
  105. }
  106. /* write the Surfer grid heading */
  107. int writeGSheader(FILE * fp, const char *name)
  108. {
  109. struct Cell_head region;
  110. char fromc[128], toc[128];
  111. struct FPRange range;
  112. DCELL Z_MIN, Z_MAX;
  113. if (Rast_read_fp_range(name, "", &range) < 0)
  114. return 1;
  115. fprintf(fp, "DSAA \n");
  116. G_get_window(&region);
  117. fprintf(fp, "%d %d\n", region.cols, region.rows);
  118. /* Projection set to -1 to force floating point output */
  119. G_format_easting(region.west + region.ew_res / 2., fromc,
  120. G_projection() == PROJECTION_LL ? -1 : 0);
  121. G_format_easting(region.east - region.ew_res / 2., toc,
  122. G_projection() == PROJECTION_LL ? -1 : 0);
  123. fprintf(fp, "%s %s\n", fromc, toc);
  124. G_format_northing(region.south + region.ns_res / 2., fromc,
  125. G_projection() == PROJECTION_LL ? -1 : 0);
  126. G_format_northing(region.north - region.ns_res / 2., toc,
  127. G_projection() == PROJECTION_LL ? -1 : 0);
  128. fprintf(fp, "%s %s\n", fromc, toc);
  129. Rast_get_fp_range_min_max(&range, &Z_MIN, &Z_MAX);
  130. fprintf(fp, "%f %f\n", (double)Z_MIN, (double)Z_MAX);
  131. return 0;
  132. }
  133. /* write Surfer Golden Software grid file */
  134. int write_GSGRID(int fd,
  135. FILE * fp,
  136. int nrows,
  137. int ncols, int out_type, int dp, char *null_str, int width)
  138. {
  139. int row, col, colcnt;
  140. void *ptr, *raster;
  141. char cell_buf[300];
  142. raster = Rast_allocate_buf(out_type);
  143. for (row = nrows - 1; row >= 0; row--) {
  144. G_percent((row - nrows) * (-1), nrows, 2);
  145. Rast_get_row(fd, raster, row, out_type);
  146. colcnt = 0;
  147. for (col = 0, ptr = raster; col < ncols; col++,
  148. ptr = G_incr_void_ptr(ptr, Rast_cell_size(out_type))) {
  149. colcnt += 1;
  150. if (!Rast_is_null_value(ptr, out_type)) {
  151. if (out_type == CELL_TYPE)
  152. fprintf(fp, "%d", *((CELL *) ptr));
  153. else if (out_type == FCELL_TYPE) {
  154. sprintf(cell_buf, "%.*f", dp, *((FCELL *) ptr));
  155. G_trim_decimal(cell_buf);
  156. fprintf(fp, "%s", cell_buf);
  157. }
  158. else if (out_type == DCELL_TYPE) {
  159. sprintf(cell_buf, "%.*f", dp, *((DCELL *) ptr));
  160. G_trim_decimal(cell_buf);
  161. fprintf(fp, "%s", cell_buf);
  162. }
  163. }
  164. else
  165. fprintf(fp, "%s", null_str);
  166. if (colcnt >= width) {
  167. fprintf(fp, "\n");
  168. colcnt = 0;
  169. }
  170. else
  171. fprintf(fp, " ");
  172. }
  173. if (colcnt != 0)
  174. fprintf(fp, "\n");
  175. fprintf(fp, "\n");
  176. }
  177. return (0);
  178. }