main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /*---------------------------------------------------------------------------*/
  36. void *map = NULL;
  37. /*---------------------------------------------------------------------------*/
  38. static void fatalError(char *errorMsg)
  39. {
  40. if (map != NULL) {
  41. /* should unopen map here! */
  42. }
  43. Rast3d_fatal_error("%s", errorMsg);
  44. }
  45. /*---------------------------------------------------------------------------*/
  46. typedef struct
  47. {
  48. struct Option *input, *output, *nv;
  49. } paramType;
  50. static paramType param;
  51. static void setParams()
  52. {
  53. param.input = G_define_standard_option(G_OPT_F_INPUT);
  54. param.input->description = _("Name of V5D raster file to be imported");
  55. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  56. param.nv = G_define_standard_option(G_OPT_M_NULL_VALUE);
  57. param.nv->answer = "none";
  58. }
  59. /*---------------------------------------------------------------------------*/
  60. static void
  61. getParams(char **input, char **output, int *convertNull, double *nullValue)
  62. {
  63. *input = param.input->answer;
  64. *output = param.output->answer;
  65. *convertNull = (strcmp(param.nv->answer, "none") != 0);
  66. if (*convertNull)
  67. if (sscanf(param.nv->answer, "%lf", nullValue) != 1)
  68. fatalError("getParams: NULL-value value invalid");
  69. }
  70. /*---------------------------------------------------------------------------*/
  71. void convert(char *openFile, RASTER3D_Region * region, int convertNull,
  72. double nullValue)
  73. {
  74. v5dstruct v5d;
  75. int time, var;
  76. int nrncnl, cnt;
  77. int x, y, z;
  78. float value;
  79. float res_r, res_c, res_l;
  80. float *data1;
  81. if (!v5dOpenFile(openFile, &v5d)) {
  82. printf("Error: couldn't open %s for reading\n", openFile);
  83. exit(0);
  84. }
  85. /* Eventually change to write the time and/or vars series of 3d raster maps.. */
  86. /* for (time=0; time<v5d.NumTimes; time++) {
  87. for (var=0; var<v5d.NumVars; var++) {
  88. */
  89. for (time = 0; time < 1; time++) {
  90. for (var = 0; var < 1; var++) {
  91. nrncnl = v5d.Nr * v5d.Nc * v5d.Nl[var];
  92. region->rows = v5d.Nr;
  93. region->cols = v5d.Nc;
  94. region->depths = v5d.Nl[var];
  95. region->north = v5d.ProjArgs[0];
  96. res_r = v5d.ProjArgs[2];
  97. res_c = v5d.ProjArgs[3];
  98. res_l = v5d.VertArgs[1];
  99. region->south = region->north - region->rows * res_r;
  100. region->west = v5d.ProjArgs[1];
  101. region->east = region->west + region->cols * res_c;
  102. region->bottom = v5d.VertArgs[0];
  103. region->top = region->bottom + region->depths * res_l;
  104. data1 = (float *)G_malloc(nrncnl * sizeof(float));
  105. if (!data1)
  106. G_fatal_error("Not enough memory for data1");
  107. if (!v5dReadGrid(&v5d, time, var, data1)) {
  108. printf("Error while reading grid (time=%d,var=%s)\n",
  109. time + 1, v5d.VarName[var]);
  110. exit(0);
  111. }
  112. cnt = 0;
  113. for (z = 0; z < region->depths; z++) {
  114. for (y = 0; y < region->rows; y++) {
  115. for (x = 0; x < region->cols; x++) {
  116. value = data1[cnt++];
  117. if (convertNull && (value == MISSING))
  118. Rast3d_set_null_value(&value, 1, FCELL_TYPE);
  119. Rast3d_put_float(map, x, y, z, value);
  120. }
  121. }
  122. }
  123. G_free(data1);
  124. }
  125. }
  126. v5dCloseFile(&v5d);
  127. }
  128. /*---------------------------------------------------------------------------*/
  129. int main(int argc, char *argv[])
  130. {
  131. char *input, *output;
  132. int convertNull;
  133. double nullValue;
  134. int useTypeDefault, type, useCompressionDefault, doCompression;
  135. int usePrecisionDefault, precision, useDimensionDefault, tileX, tileY,
  136. tileZ;
  137. RASTER3D_Region region;
  138. struct GModule *module;
  139. map = NULL;
  140. G_gisinit(argv[0]);
  141. module = G_define_module();
  142. G_add_keyword(_("raster3d"));
  143. G_add_keyword(_("import"));
  144. G_add_keyword(_("voxel"));
  145. module->description =
  146. _("Import 3-dimensional Vis5D files.");
  147. setParams();
  148. Rast3d_set_standard3d_input_params();
  149. if (G_parser(argc, argv))
  150. exit(1);
  151. getParams(&input, &output, &convertNull, &nullValue);
  152. if (!Rast3d_get_standard3d_params(&useTypeDefault, &type,
  153. &useCompressionDefault, &doCompression,
  154. &usePrecisionDefault, &precision,
  155. &useDimensionDefault, &tileX, &tileY,
  156. &tileZ))
  157. fatalError("main: error getting standard parameters");
  158. Rast3d_get_window(&region);
  159. map = Rast3d_open_cell_new(output, FCELL_TYPE, RASTER3D_USE_CACHE_XY, &region);
  160. if (map == NULL)
  161. fatalError(_("Unable to open 3D raster map"));
  162. convert(input, &region, convertNull, nullValue);
  163. if (!Rast3d_close(map))
  164. fatalError(_("Unable to close 3D raster map"));
  165. map = NULL;
  166. return 0;
  167. }
  168. /*---------------------------------------------------------------------------*/