n_arrays_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: IO array management functions
  8. * part of the gpde library
  9. *
  10. * COPYRIGHT: (C) 2000 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 <math.h>
  18. #include <grass/N_pde.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. /* ******************** 2D ARRAY FUNCTIONS *********************** */
  22. /*!
  23. * \brief Read a raster map into a N_array_2d structure
  24. *
  25. * The raster map will be opened in the current region settings.
  26. * If no N_array_2d structure is provided (NULL pointer), a new structure will be
  27. * allocated with the same data type as the raster map and the size of the current region.
  28. * The array offset will be set to 0.
  29. * <br><br>
  30. * If a N_array_2d structure is provided, the values from the raster map are
  31. * casted to the N_array_2d type. The array must have the same size
  32. * as the current region.
  33. * <br><br>
  34. * The new created or the provided array are returned.
  35. * If the reading of the raster map fails, G_fatal_error() will
  36. * be invoked.
  37. *
  38. * \param name * char - the name of an existing raster map
  39. * \param array * N_array_2d - an existing array or NULL
  40. * \return N_array_2d * - the existing or new allocated array
  41. * */
  42. N_array_2d *N_read_rast_to_array_2d(char *name, N_array_2d * array)
  43. {
  44. int map; /*The rastermap */
  45. int x, y, cols, rows, type;
  46. void *rast;
  47. void *ptr;
  48. struct Cell_head region;
  49. N_array_2d *data = array;
  50. /* Get the active region */
  51. G_get_set_window(&region);
  52. /*set the rows and cols */
  53. rows = region.rows;
  54. cols = region.cols;
  55. /*open the raster map */
  56. map = Rast_open_old(name, "");
  57. type = Rast_get_map_type(map);
  58. /*if the array is NULL create a new one with the data type of the raster map */
  59. /*the offset is 0 by default */
  60. if (data == NULL) {
  61. if (type == DCELL_TYPE) {
  62. data = N_alloc_array_2d(cols, rows, 0, DCELL_TYPE);
  63. }
  64. if (type == FCELL_TYPE) {
  65. data = N_alloc_array_2d(cols, rows, 0, FCELL_TYPE);
  66. }
  67. if (type == CELL_TYPE) {
  68. data = N_alloc_array_2d(cols, rows, 0, CELL_TYPE);
  69. }
  70. }
  71. else {
  72. /*Check the array sizes */
  73. if (data->cols != cols)
  74. G_fatal_error
  75. ("N_read_rast_to_array_2d: the data array size is different from the current region settings");
  76. if (data->rows != rows)
  77. G_fatal_error
  78. ("N_read_rast_to_array_2d: the data array size is different from the current region settings");
  79. }
  80. rast = Rast_allocate_buf(type);
  81. G_message(_("Reading raster map <%s> into memory"), name);
  82. for (y = 0; y < rows; y++) {
  83. G_percent(y, rows - 1, 10);
  84. Rast_get_row(map, rast, y, type);
  85. for (x = 0, ptr = rast; x < cols;
  86. x++, ptr = G_incr_void_ptr(ptr, Rast_cell_size(type))) {
  87. if (type == CELL_TYPE) {
  88. if (Rast_is_c_null_value(ptr)) {
  89. N_put_array_2d_value_null(data, x, y);
  90. }
  91. else {
  92. if (data->type == CELL_TYPE)
  93. N_put_array_2d_c_value(data, x, y,
  94. (CELL) * (CELL *) ptr);
  95. if (data->type == FCELL_TYPE)
  96. N_put_array_2d_f_value(data, x, y,
  97. (FCELL) * (CELL *) ptr);
  98. if (data->type == DCELL_TYPE)
  99. N_put_array_2d_d_value(data, x, y,
  100. (DCELL) * (CELL *) ptr);
  101. }
  102. }
  103. if (type == FCELL_TYPE) {
  104. if (Rast_is_f_null_value(ptr)) {
  105. N_put_array_2d_value_null(data, x, y);
  106. }
  107. else {
  108. if (data->type == CELL_TYPE)
  109. N_put_array_2d_c_value(data, x, y,
  110. (CELL) * (FCELL *) ptr);
  111. if (data->type == FCELL_TYPE)
  112. N_put_array_2d_f_value(data, x, y,
  113. (FCELL) * (FCELL *) ptr);
  114. if (data->type == DCELL_TYPE)
  115. N_put_array_2d_d_value(data, x, y,
  116. (DCELL) * (FCELL *) ptr);
  117. }
  118. }
  119. if (type == DCELL_TYPE) {
  120. if (Rast_is_d_null_value(ptr)) {
  121. N_put_array_2d_value_null(data, x, y);
  122. }
  123. else {
  124. if (data->type == CELL_TYPE)
  125. N_put_array_2d_c_value(data, x, y,
  126. (CELL) * (DCELL *) ptr);
  127. if (data->type == FCELL_TYPE)
  128. N_put_array_2d_f_value(data, x, y,
  129. (FCELL) * (DCELL *) ptr);
  130. if (data->type == DCELL_TYPE)
  131. N_put_array_2d_d_value(data, x, y,
  132. (DCELL) * (DCELL *) ptr);
  133. }
  134. }
  135. }
  136. }
  137. /* Close file */
  138. Rast_close(map);
  139. return data;
  140. }
  141. /*!
  142. * \brief Write a N_array_2d struct to a raster map
  143. *
  144. * A new raster map is created with the same type as the N_array_2d.
  145. * The current region is used to open the raster map.
  146. * The N_array_2d must have the same size as the current region.
  147. If the writing of the raster map fails, G_fatal_error() will
  148. * be invoked.
  149. * \param array N_array_2d *
  150. * \param name char * - the name of the raster map
  151. * \return void
  152. *
  153. * */
  154. void N_write_array_2d_to_rast(N_array_2d * array, char *name)
  155. {
  156. int map; /*The rastermap */
  157. int x, y, cols, rows, count, type;
  158. CELL *rast = NULL;
  159. FCELL *frast = NULL;
  160. DCELL *drast = NULL;
  161. struct Cell_head region;
  162. if (!array)
  163. G_fatal_error(_("N_array_2d * array is empty"));
  164. /* Get the current region */
  165. G_get_set_window(&region);
  166. rows = region.rows;
  167. cols = region.cols;
  168. type = array->type;
  169. /*Open the new map */
  170. map = Rast_open_new(name, type);
  171. if (type == CELL_TYPE)
  172. rast = Rast_allocate_buf(type);
  173. if (type == FCELL_TYPE)
  174. frast = Rast_allocate_buf(type);
  175. if (type == DCELL_TYPE)
  176. drast = Rast_allocate_buf(type);
  177. G_message(_("Write 2d array to raster map <%s>"), name);
  178. count = 0;
  179. for (y = 0; y < rows; y++) {
  180. G_percent(y, rows - 1, 10);
  181. for (x = 0; x < cols; x++) {
  182. if (type == CELL_TYPE)
  183. rast[x] = N_get_array_2d_c_value(array, x, y);
  184. if (type == FCELL_TYPE)
  185. frast[x] = N_get_array_2d_f_value(array, x, y);
  186. if (type == DCELL_TYPE)
  187. drast[x] = N_get_array_2d_d_value(array, x, y);
  188. }
  189. if (type == CELL_TYPE)
  190. Rast_put_c_row(map, rast);
  191. if (type == FCELL_TYPE)
  192. Rast_put_f_row(map, frast);
  193. if (type == DCELL_TYPE)
  194. Rast_put_d_row(map, drast);
  195. }
  196. /* Close file */
  197. Rast_close(map);
  198. }
  199. /* ******************** 3D ARRAY FUNCTIONS *********************** */
  200. /*!
  201. * \brief Read a volume map into a N_array_3d structure
  202. *
  203. * The volume map is opened in the current region settings.
  204. * If no N_array_3d structure is provided (NULL pointer), a new structure will be
  205. * allocated with the same data type as the volume map and the size of the current region.
  206. * The array offset will be set to 0.
  207. * <br><br>
  208. *
  209. * If a N_array_3d structure is provided, the values from the volume map are
  210. * casted to the N_array_3d type. The array must have the same size
  211. * as the current region.
  212. * <br><br>
  213. *
  214. * The new created or the provided array is returned.
  215. * If the reading of the volume map fails, Rast3d_fatal_error() will
  216. * be invoked.
  217. *
  218. * \param name * char - the name of an existing volume map
  219. * \param array * N_array_3d - an existing array or NULL
  220. * \param mask int - 0 = false, 1 = ture : if a mask is presenent, use it with the input volume map
  221. * \return N_array_3d * - the existing or new allocated array
  222. * */
  223. N_array_3d *N_read_rast3d_to_array_3d(char *name, N_array_3d * array,
  224. int mask)
  225. {
  226. void *map = NULL; /*The 3D Rastermap */
  227. int changemask = 0;
  228. int x, y, z, cols, rows, depths, type;
  229. double d1 = 0, f1 = 0;
  230. N_array_3d *data = array;
  231. RASTER3D_Region region;
  232. /*get the current region */
  233. Rast3d_get_window(&region);
  234. cols = region.cols;
  235. rows = region.rows;
  236. depths = region.depths;
  237. if (NULL == G_find_raster3d(name, ""))
  238. Rast3d_fatal_error(_("3D raster map <%s> not found"), name);
  239. /*Open all maps with default region */
  240. map =
  241. Rast3d_open_cell_old(name, G_find_raster3d(name, ""), RASTER3D_DEFAULT_WINDOW,
  242. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  243. if (map == NULL)
  244. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  245. type = Rast3d_tile_type_map(map);
  246. /*if the array is NULL create a new one with the data type of the volume map */
  247. /*the offset is 0 by default */
  248. if (data == NULL) {
  249. if (type == FCELL_TYPE) {
  250. data = N_alloc_array_3d(cols, rows, depths, 0, FCELL_TYPE);
  251. }
  252. if (type == DCELL_TYPE) {
  253. data = N_alloc_array_3d(cols, rows, depths, 0, DCELL_TYPE);
  254. }
  255. }
  256. else {
  257. /*Check the array sizes */
  258. if (data->cols != cols)
  259. G_fatal_error
  260. ("N_read_rast_to_array_3d: the data array size is different from the current region settings");
  261. if (data->rows != rows)
  262. G_fatal_error
  263. ("N_read_rast_to_array_3d: the data array size is different from the current region settings");
  264. if (data->depths != depths)
  265. G_fatal_error
  266. ("N_read_rast_to_array_3d: the data array size is different from the current region settings");
  267. }
  268. G_message(_("Read g3d map <%s> into the memory"), name);
  269. /*if requested set the Mask on */
  270. if (mask) {
  271. if (Rast3d_mask_file_exists()) {
  272. changemask = 0;
  273. if (Rast3d_mask_is_off(map)) {
  274. Rast3d_mask_on(map);
  275. changemask = 1;
  276. }
  277. }
  278. }
  279. for (z = 0; z < depths; z++) { /*From the bottom to the top */
  280. G_percent(z, depths - 1, 10);
  281. for (y = 0; y < rows; y++) {
  282. for (x = 0; x < cols; x++) {
  283. if (type == FCELL_TYPE) {
  284. Rast3d_get_value(map, x, y, z, &f1, type);
  285. if (Rast_is_f_null_value((void *)&f1)) {
  286. N_put_array_3d_value_null(data, x, y, z);
  287. }
  288. else {
  289. if (data->type == FCELL_TYPE)
  290. N_put_array_3d_f_value(data, x, y, z, f1);
  291. if (data->type == DCELL_TYPE)
  292. N_put_array_3d_d_value(data, x, y, z, (double)f1);
  293. }
  294. }
  295. else {
  296. Rast3d_get_value(map, x, y, z, &d1, type);
  297. if (Rast_is_d_null_value((void *)&d1)) {
  298. N_put_array_3d_value_null(data, x, y, z);
  299. }
  300. else {
  301. if (data->type == FCELL_TYPE)
  302. N_put_array_3d_f_value(data, x, y, z, (float)d1);
  303. if (data->type == DCELL_TYPE)
  304. N_put_array_3d_d_value(data, x, y, z, d1);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. /*We set the Mask off, if it was off before */
  311. if (mask) {
  312. if (Rast3d_mask_file_exists())
  313. if (Rast3d_mask_is_on(map) && changemask)
  314. Rast3d_mask_off(map);
  315. }
  316. /* Close files and exit */
  317. if (!Rast3d_close(map))
  318. Rast3d_fatal_error(map, NULL, 0, _("Error closing g3d file"));
  319. return data;
  320. }
  321. /*!
  322. * \brief Write a N_array_3d struct to a volume map
  323. *
  324. * A new volume map is created with the same type as the N_array_3d.
  325. * The current region is used to open the volume map.
  326. * The N_array_3d must have the same size as the current region.
  327. * If the writing of the volume map fails, Rast3d_fatal_error() will
  328. * be invoked.
  329. *
  330. *
  331. * \param array N_array_3d *
  332. * \param name char * - the name of the volume map
  333. * \param mask int - 1 = use a 3d mask, 0 do not use a 3d mask
  334. * \return void
  335. *
  336. * */
  337. void N_write_array_3d_to_rast3d(N_array_3d * array, char *name, int mask)
  338. {
  339. void *map = NULL; /*The 3D Rastermap */
  340. int changemask = 0;
  341. int x, y, z, cols, rows, depths, count, type;
  342. double d1 = 0.0, f1 = 0.0;
  343. N_array_3d *data = array;
  344. RASTER3D_Region region;
  345. /*get the current region */
  346. Rast3d_get_window(&region);
  347. cols = region.cols;
  348. rows = region.rows;
  349. depths = region.depths;
  350. type = data->type;
  351. /*Check the array sizes */
  352. if (data->cols != cols)
  353. G_fatal_error
  354. ("N_write_array_3d_to_rast3d: the data array size is different from the current region settings");
  355. if (data->rows != rows)
  356. G_fatal_error
  357. ("N_write_array_3d_to_rast3d: the data array size is different from the current region settings");
  358. if (data->depths != depths)
  359. G_fatal_error
  360. ("N_write_array_3d_to_rast3d: the data array size is different from the current region settings");
  361. /*Open the new map */
  362. if (type == DCELL_TYPE)
  363. map = Rast3d_open_new_opt_tile_size(name, RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  364. else if (type == FCELL_TYPE)
  365. map = Rast3d_open_new_opt_tile_size(name, RASTER3D_USE_CACHE_XY, &region, FCELL_TYPE, 32);
  366. if (map == NULL)
  367. Rast3d_fatal_error(_("Error opening g3d map <%s>"), name);
  368. G_message(_("Write 3d array to g3d map <%s>"), name);
  369. /*if requested set the Mask on */
  370. if (mask) {
  371. if (Rast3d_mask_file_exists()) {
  372. changemask = 0;
  373. if (Rast3d_mask_is_off(map)) {
  374. Rast3d_mask_on(map);
  375. changemask = 1;
  376. }
  377. }
  378. }
  379. count = 0;
  380. for (z = 0; z < depths; z++) { /*From the bottom to the top */
  381. G_percent(z, depths - 1, 10);
  382. for (y = 0; y < rows; y++) {
  383. for (x = 0; x < cols; x++) {
  384. if (type == FCELL_TYPE) {
  385. f1 = N_get_array_3d_f_value(data, x, y, z);
  386. Rast3d_put_float(map, x, y, z, f1);
  387. }
  388. else if (type == DCELL_TYPE) {
  389. d1 = N_get_array_3d_d_value(data, x, y, z);
  390. Rast3d_put_double(map, x, y, z, d1);
  391. }
  392. }
  393. }
  394. }
  395. /*We set the Mask off, if it was off before */
  396. if (mask) {
  397. if (Rast3d_mask_file_exists())
  398. if (Rast3d_mask_is_on(map) && changemask)
  399. Rast3d_mask_off(map);
  400. }
  401. /* Flush all tile */
  402. if (!Rast3d_flush_all_tiles(map))
  403. Rast3d_fatal_error("Error flushing tiles with Rast3d_flush_all_tiles");
  404. /* Close files and exit */
  405. if (!Rast3d_close(map))
  406. Rast3d_fatal_error(map, NULL, 0, _("Error closing g3d file"));
  407. return;
  408. }