main.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /***************************************************************************
  2. *
  3. * MODULE: r3.in.ascii
  4. *
  5. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova, Bill Brown,
  6. * Lubos Mitas, Jaro Hofierka
  7. *
  8. * PURPOSE: Convert a 3D ASCII raster text file into a (binary) 3D raster map layer
  9. *
  10. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/G3d.h>
  22. #include <grass/glocale.h>
  23. /*- prototypes --------------------------------------------------------------*/
  24. static void fatalError(char *errorMsg); /*Simple Error message */
  25. static void setParams(); /*Fill the paramType structure */
  26. /*Puts the userdefined parameters into easier handable variables */
  27. static void getParams(char **input, char **output, int *convertNull,
  28. double *nullValu);
  29. /*reads a g3d ascii-file headerfile-string */
  30. static void readHeaderString(FILE * fp, char *valueString, double *value);
  31. static FILE *openAscii(char *asciiFile, G3D_Region * region); /*open the g3d ascii file */
  32. /*This function does all the work, it reads the values from the g3d ascii-file and put
  33. it into an g3d-map */
  34. static void asciiToG3d(FILE * fp, G3D_Region * region, int convertNull,
  35. double nullValue);
  36. /*---------------------------------------------------------------------------*/
  37. void *map = NULL;
  38. extern void *G3d_openNewParam();
  39. /*---------------------------------------------------------------------------*/
  40. static void fatalError(char *errorMsg)
  41. {
  42. if (map != NULL) {
  43. /* should unopen map here! */
  44. G3d_closeCell(map);
  45. }
  46. G3d_fatalError(errorMsg);
  47. }
  48. /*---------------------------------------------------------------------------*/
  49. typedef struct
  50. {
  51. struct Option *input, *output, *nv;
  52. } paramType;
  53. static paramType param;
  54. static void setParams()
  55. {
  56. param.input = G_define_option();
  57. param.input->key = "input";
  58. param.input->type = TYPE_STRING;
  59. param.input->required = YES;
  60. param.input->key_desc = "name";
  61. param.input->gisprompt = "old_file,file,input";
  62. param.input->description = _("Ascii raster map to be imported");
  63. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  64. param.nv = G_define_option();
  65. param.nv->key = "nv";
  66. param.nv->type = TYPE_STRING;
  67. param.nv->required = NO;
  68. param.nv->multiple = NO;
  69. param.nv->answer = "none";
  70. param.nv->description =
  71. _("String representing NULL value data cell (use 'none' if no such value)");
  72. }
  73. /*---------------------------------------------------------------------------*/
  74. static void
  75. getParams(char **input, char **output, int *convertNull, double *nullValue)
  76. {
  77. *input = param.input->answer;
  78. *output = param.output->answer;
  79. *convertNull = (strcmp(param.nv->answer, "none") != 0);
  80. if (*convertNull)
  81. if (sscanf(param.nv->answer, "%lf", nullValue) != 1)
  82. fatalError("getParams: NULL-value value invalid");
  83. G_debug(3, "getParams: input: %s, output: %s", *input, *output);
  84. }
  85. /*---------------------------------------------------------------------------*/
  86. static void readHeaderString(FILE * fp, char *valueString, double *value)
  87. {
  88. static char format[100];
  89. /* to avoid buffer overflows we use snprintf */
  90. G_snprintf(format, 100, "%s %%lf", valueString);
  91. if (fscanf(fp, format, value) != 1) {
  92. G_debug(0, "bad value for [%s]", valueString);
  93. fatalError("readHeaderString: header value invalid");
  94. }
  95. while (fgetc(fp) != '\n') ;
  96. }
  97. /*---------------------------------------------------------------------------*/
  98. static FILE *openAscii(char *asciiFile, G3D_Region * region)
  99. {
  100. FILE *fp;
  101. double tmp;
  102. G_debug(3, "openAscii: opens the ascii file and reads the header");
  103. fp = fopen(asciiFile, "r");
  104. if (fp == NULL) {
  105. perror(asciiFile);
  106. G_usage();
  107. exit(EXIT_FAILURE);
  108. }
  109. G3d_getWindow(region);
  110. readHeaderString(fp, "north:", &(region->north));
  111. readHeaderString(fp, "south:", &(region->south));
  112. readHeaderString(fp, "east:", &(region->east));
  113. readHeaderString(fp, "west:", &(region->west));
  114. readHeaderString(fp, "top:", &(region->top));
  115. readHeaderString(fp, "bottom:", &(region->bottom));
  116. readHeaderString(fp, "rows:", &tmp);
  117. region->rows = tmp;
  118. readHeaderString(fp, "cols:", &tmp);
  119. region->cols = tmp;
  120. readHeaderString(fp, "levels:", &tmp);
  121. region->depths = tmp;
  122. return fp;
  123. }
  124. /*---------------------------------------------------------------------------*/
  125. #define MAX(a,b) (a > b ? a : b)
  126. static void
  127. asciiToG3d(FILE * fp, G3D_Region * region, int convertNull, double nullValue)
  128. {
  129. int x, y, z;
  130. double value;
  131. int tileX, tileY, tileZ;
  132. G3d_getTileDimensionsMap(map, &tileX, &tileY, &tileZ);
  133. G3d_minUnlocked(map, G3D_USE_CACHE_X);
  134. G3d_autolockOn(map);
  135. G3d_unlockAll(map);
  136. G_message(_("Loading data ... (%dx%dx%d)"), region->cols, region->rows,
  137. region->depths);
  138. G_debug(3,
  139. "asciiToG3d: writing the 3d raster map, with rows %i cols %i depths %i",
  140. region->rows, region->cols, region->depths);
  141. for (z = 0; z < region->depths; z++) {
  142. if ((z % tileZ) == 0)
  143. G3d_unlockAll(map);
  144. for (y = region->rows - 1; y >= 0; y--) /* go south to north */
  145. for (x = 0; x < region->cols; x++) {
  146. if (fscanf(fp, "%lf", &value) != 1) {
  147. if (feof(fp))
  148. G_warning(_("End of file reached while still loading data."));
  149. G_debug(0,
  150. "missing data at col=%d row=%d depth=%d last_value=[%.4f]",
  151. x + 1, region->rows - y, z + 1, value);
  152. fatalError("asciiToG3d: read failed");
  153. }
  154. if (convertNull && (value == nullValue))
  155. G3d_setNullValue(&value, 1, DCELL_TYPE);
  156. G3d_putDouble(map, x, y, z, value);
  157. }
  158. if (!G3d_flushTilesInCube(map,
  159. 0, 0, MAX(0, z - tileZ),
  160. region->rows - 1, region->cols - 1, z))
  161. fatalError("asciiTog3d: error flushing tiles");
  162. }
  163. if (fscanf(fp, "%lf", &value) == 1) {
  164. G_warning(_("Data exists in input file after fully importing "
  165. "expected data. [%.4f ...]"), value);
  166. }
  167. if (!G3d_flushAllTiles(map))
  168. fatalError("asciiTog3d: error flushing tiles");
  169. G3d_autolockOff(map);
  170. G3d_unlockAll(map);
  171. }
  172. /*---------------------------------------------------------------------------*/
  173. int main(int argc, char *argv[])
  174. {
  175. char *input, *output;
  176. int convertNull;
  177. double nullValue;
  178. int useTypeDefault, type, useLzwDefault, doLzw, useRleDefault, doRle;
  179. int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY,
  180. tileZ;
  181. G3D_Region region;
  182. FILE *fp;
  183. struct GModule *module;
  184. struct History history;
  185. map = NULL;
  186. G_gisinit(argv[0]);
  187. module = G_define_module();
  188. G_add_keyword(_("raster3d"));
  189. G_add_keyword(_("voxel"));
  190. G_add_keyword(_("import"));
  191. module->description =
  192. _("Converts a 3D ASCII raster text file into a (binary) 3D raster map.");
  193. setParams();
  194. G3d_setStandard3dInputParams();
  195. if (G_parser(argc, argv))
  196. exit(EXIT_FAILURE);
  197. getParams(&input, &output, &convertNull, &nullValue);
  198. if (!G3d_getStandard3dParams(&useTypeDefault, &type,
  199. &useLzwDefault, &doLzw,
  200. &useRleDefault, &doRle,
  201. &usePrecisionDefault, &precision,
  202. &useDimensionDefault, &tileX, &tileY,
  203. &tileZ))
  204. fatalError("main: error getting standard parameters");
  205. fp = openAscii(input, &region);
  206. /*Open the new G3D map */
  207. map = G3d_openNewParam(output, DCELL_TYPE, G3D_USE_CACHE_XY,
  208. &region,
  209. type, doLzw, doRle, precision, tileX, tileY,
  210. tileZ);
  211. if (map == NULL)
  212. fatalError(_("Error opening 3d raster map"));
  213. /*Create the new G3D Map */
  214. asciiToG3d(fp, &region, convertNull, nullValue);
  215. if (!G3d_closeCell(map))
  216. fatalError(_("Error closing new 3d raster map"));
  217. /* write input name to map history */
  218. G3d_readHistory(output, G_mapset(), &history);
  219. Rast_set_history(&history, HIST_DATSRC_1, input);
  220. G3d_writeHistory(output, &history);
  221. map = NULL;
  222. if (fclose(fp))
  223. fatalError(_("Error closing ascii file"));
  224. G_done_msg("");
  225. return EXIT_SUCCESS;
  226. }