main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * r3.in.v5d - program for data conversion from the V5D format
  3. * of the VIS5D visualization software to 3D raster GRASS
  4. * data format.
  5. *
  6. * Copyright Jaroslav Hofierka
  7. * GeoModel,s.r.o., Bratislava, Slovakia 2000
  8. *
  9. * Comments or bug reports please send to hofierka@geomodel.sk
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <grass/config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <math.h>
  30. #include "binio.h"
  31. #include "v5d.h"
  32. #include <grass/gis.h>
  33. #include <grass/raster3d.h>
  34. #include <grass/glocale.h>
  35. #undef MAX
  36. #define MAX(a,b) (a > b ? a : b)
  37. /*---------------------------------------------------------------------------*/
  38. void *map = NULL;
  39. /*---------------------------------------------------------------------------*/
  40. static void fatalError(char *errorMsg)
  41. {
  42. if (map != NULL) {
  43. /* should unopen map here! */
  44. }
  45. Rast3d_fatal_error("%s", errorMsg);
  46. }
  47. /*---------------------------------------------------------------------------*/
  48. typedef struct
  49. {
  50. struct Option *input, *output, *nv;
  51. } paramType;
  52. static paramType param;
  53. static void setParams()
  54. {
  55. param.input = G_define_standard_option(G_OPT_F_INPUT);
  56. param.input->description = _("Name of V5D raster file to be imported");
  57. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  58. param.nv = G_define_standard_option(G_OPT_M_NULL_VALUE);
  59. param.nv->answer = "none";
  60. }
  61. /*---------------------------------------------------------------------------*/
  62. static void
  63. getParams(char **input, char **output, int *convertNull, double *nullValue)
  64. {
  65. *input = param.input->answer;
  66. *output = param.output->answer;
  67. *convertNull = (strcmp(param.nv->answer, "none") != 0);
  68. if (*convertNull)
  69. if (sscanf(param.nv->answer, "%lf", nullValue) != 1)
  70. fatalError("getParams: NULL-value value invalid");
  71. }
  72. /*---------------------------------------------------------------------------*/
  73. void convert(char *openFile, RASTER3D_Region * region, int convertNull,
  74. double nullValue)
  75. {
  76. v5dstruct v5d;
  77. int time, var;
  78. int nrncnl, cnt;
  79. int x, y, z;
  80. float value;
  81. float res_r, res_c, res_l;
  82. float *data1;
  83. if (!v5dOpenFile(openFile, &v5d)) {
  84. printf("Error: couldn't open %s for reading\n", openFile);
  85. exit(0);
  86. }
  87. /* Eventually change to write the time and/or vars series of 3d raster maps.. */
  88. /* for (time=0; time<v5d.NumTimes; time++) {
  89. for (var=0; var<v5d.NumVars; var++) {
  90. */
  91. for (time = 0; time < 1; time++) {
  92. for (var = 0; var < 1; var++) {
  93. nrncnl = v5d.Nr * v5d.Nc * v5d.Nl[var];
  94. region->rows = v5d.Nr;
  95. region->cols = v5d.Nc;
  96. region->depths = v5d.Nl[var];
  97. region->north = v5d.ProjArgs[0];
  98. res_r = v5d.ProjArgs[2];
  99. res_c = v5d.ProjArgs[3];
  100. res_l = v5d.VertArgs[1];
  101. region->south = region->north - region->rows * res_r;
  102. region->west = v5d.ProjArgs[1];
  103. region->east = region->west + region->cols * res_c;
  104. region->bottom = v5d.VertArgs[0];
  105. region->top = region->bottom + region->depths * res_l;
  106. data1 = (float *)G_malloc(nrncnl * sizeof(float));
  107. if (!data1)
  108. G_fatal_error("Not enough memory for data1");
  109. if (!v5dReadGrid(&v5d, time, var, data1)) {
  110. printf("Error while reading grid (time=%d,var=%s)\n",
  111. time + 1, v5d.VarName[var]);
  112. exit(0);
  113. }
  114. cnt = 0;
  115. for (z = 0; z < region->depths; z++) {
  116. for (y = 0; y < region->rows; y++) {
  117. for (x = 0; x < region->cols; x++) {
  118. value = data1[cnt++];
  119. if (convertNull && (value == MISSING))
  120. Rast3d_set_null_value(&value, 1, FCELL_TYPE);
  121. Rast3d_put_float(map, x, y, z, value);
  122. }
  123. }
  124. }
  125. G_free(data1);
  126. }
  127. }
  128. v5dCloseFile(&v5d);
  129. }
  130. /*---------------------------------------------------------------------------*/
  131. int main(int argc, char *argv[])
  132. {
  133. char *input, *output;
  134. int convertNull;
  135. double nullValue;
  136. int useTypeDefault, type, useCompressionDefault, doCompression;
  137. int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY,
  138. tileZ;
  139. RASTER3D_Region region;
  140. struct GModule *module;
  141. map = NULL;
  142. G_gisinit(argv[0]);
  143. module = G_define_module();
  144. G_add_keyword(_("raster3d"));
  145. G_add_keyword(_("import"));
  146. G_add_keyword(_("voxel"));
  147. module->description =
  148. _("Import 3-dimensional Vis5D files.");
  149. setParams();
  150. Rast3d_set_standard3d_input_params();
  151. if (G_parser(argc, argv))
  152. exit(1);
  153. getParams(&input, &output, &convertNull, &nullValue);
  154. if (!Rast3d_get_standard3d_params(&useTypeDefault, &type,
  155. &useCompressionDefault, &doCompression,
  156. &usePrecisionDefault, &precision,
  157. &useDimensionDefault, &tileX, &tileY,
  158. &tileZ))
  159. fatalError("main: error getting standard parameters");
  160. Rast3d_get_window(&region);
  161. map = Rast3d_open_cell_new(output, FCELL_TYPE, RASTER3D_USE_CACHE_XY, &region);
  162. if (map == NULL)
  163. fatalError(_("Unable to open 3D raster map"));
  164. convert(input, &region, convertNull, nullValue);
  165. if (!Rast3d_close(map))
  166. fatalError(_("Unable to close 3D raster map"));
  167. map = NULL;
  168. return 0;
  169. }
  170. /*---------------------------------------------------------------------------*/