main.c 6.2 KB

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