Updated Changelog
[libgpiv.git] / lib / my_utils.c
blobe3df789dc20c6a5433a7807f1f7702c3512c03f8
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*
4 libgpiv - library for Particle Image Velocimetry
6 Copyright (C) 2008 Gerber van der Graaf
8 This file is part of libgpiv.
9 Libgpiv is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 -------------------------------------------------------------------------------
26 FILENAME: my_utils.c
27 LIBRARY: libgpiv
28 LAST MODIFICATION DATE: $Id: my_utils.c,v 1.2 2008-10-09 14:02:03 gerber Exp $
29 --------------------------------------------------------------------------- */
31 #include <string.h>
32 #include <gpiv.h>
33 #include "my_utils.h"
36 FILE *
37 my_utils_fopen_tmp (const gchar *tmpfile,
38 const gchar *mode)
39 /*-----------------------------------------------------------------------------
40 * Attempts to open a filepointer for TMPDIR/USER/tmpfile, else in
41 * TMPDIR/tmpfile
43 * RETURNS:
44 * filepointer or NULL
47 FILE *fp = NULL;
48 gchar *tmp_dir = g_strdup (g_get_tmp_dir ());
49 gchar *user_name = g_strdup (g_get_user_name ());
50 gchar *tmp_user_dir = g_strdup_printf ("%s/%s", tmp_dir, user_name);
51 gchar *filename = g_strdup_printf ("%s/%s/%s", tmp_dir, user_name,
52 tmpfile);
53 gint return_val;
56 if ((fp = fopen(filename, mode)) != NULL) {
57 if ((return_val = g_mkdir(tmp_user_dir, 700)) != 0) {
58 g_free (filename);
59 filename = g_strdup_printf ("%s/%s", tmp_dir, tmpfile);
61 if ((fp = fopen(filename, mode)) == NULL) {
62 gpiv_warning("LIBGPIV internal error: my_utils_fopen_tmp: Failure opening %s for output",
63 filename);
64 return NULL;
70 g_free (tmp_dir);
71 g_free (user_name);
72 g_free (tmp_user_dir);
73 g_free (filename);
74 return fp;
78 gchar *
79 my_utils_write_tmp_image (const GpivImage *image,
80 const gchar *basename,
81 const gchar *message
83 /*-----------------------------------------------------------------------------
84 * DESCRIPTION:
85 * Stores deformed image to file system with pre defined name to TMPDIR
86 * and prints message to stdout
88 * INPUTS:
89 * img1: first image of PIV image pair
90 * img2: second image of PIV image pair
91 * image_par: image parameters to be stored in header
92 * verbose: prints the storing to stdout
94 * OUTPUTS:
96 * RETURNS:
97 * char * to NULL on success or *err_msg on failure
98 *---------------------------------------------------------------------------*/
100 gchar *err_msg = NULL;
101 gchar *tmp_dir = g_strdup (g_get_tmp_dir ());
102 gchar *user_name = g_strdup (g_get_user_name ());
103 gchar *filename = g_strdup_printf ("%s%s", basename, GPIV_EXT_PNG_IMAGE);
104 FILE *fp;
107 if ((fp = my_utils_fopen_tmp (filename, "wb")) == NULL) {
108 err_msg = "gpiv_piv_write_tmp_image: Failure opening for output";
109 return err_msg;
112 g_message ("my_utils_write_tmp_image: %s %s",
113 message, g_strdup_printf ("%s/%s/%s", tmp_dir, user_name, filename));
115 if ((err_msg =
116 gpiv_write_png_image (fp, image, FALSE)) != NULL) {
117 fclose (fp);
118 return err_msg;
121 fclose(fp);
122 g_free (tmp_dir);
123 g_free (filename);
124 return err_msg;
129 gboolean
130 my_utils_find_data_scaled (const gchar line[GPIV_MAX_CHARS]
132 /*-----------------------------------------------------------------------------
133 * DESCRIPTION:
134 * Determines if the data have been scaled in order to read x-and y
135 * positions as floating point varibles
137 * PROTOTYPE LOCATATION:
138 * io.h
140 * INPUTS:
141 * line: character line containing program that generated data
143 * OUTPUTS:
145 * RETURNS:
146 * scale: TRUE if scaled, else FALSE
147 *---------------------------------------------------------------------------*/
149 gboolean scale = FALSE;
152 if (strstr(line,"scale") != '\0') {
153 scale = TRUE;
157 return scale;
162 gboolean
163 my_utils_count_asciidata (FILE *fp,
164 guint *nx,
165 guint *ny
167 /*---------------------------------------------------------------------------*/
169 * Reads ASCII data from fp to find array length of x-and y data,
170 * provided that both x and y-data are in
171 * incremental or decremental order. Determines if scaled data have been used
172 * in order to determine which format/header has to be used for writing the data
174 * @param[out] nx number of columns in piv data array
175 * @param[out] ny number of columns in piv data array
176 * @param[in] fp input file. If NULL, stdin is used.
177 * @return scale TRUE or FALSE for scaled data
179 /*---------------------------------------------------------------------------*/
181 gboolean scale = FALSE;
182 gchar line[GPIV_MAX_CHARS];
184 gboolean increment = FALSE;
185 gfloat x = 0, y = 0;
186 gfloat dat_x = -10.0e5, dat_y = -10.0e5;
187 gint line_nr = 0;
190 *nx = 0;
191 *ny = 0;
193 if (fp == stdin || fp == NULL) {
194 while (gets (line) != NULL) {
195 g_message ("count_asciidata:: line = %s", line);
196 my_utils_obtain_nxny_fromline (line, &scale, nx, ny,
197 &increment, &dat_x, &dat_y, &line_nr);
200 } else {
201 while (fgets (line, GPIV_MAX_CHARS, fp) != NULL) {
202 my_utils_obtain_nxny_fromline (line, &scale, nx, ny,
203 &increment, &dat_x, &dat_y, &line_nr);
210 * Array size nx, ny
212 *nx = *nx + 1;
213 *ny = *ny + 1;
214 #ifdef DEBUG
215 g_message ("count_asciidata:: nx = %d ny = %d", *nx, *ny);
216 #endif
219 return scale;
224 GpivImage *
225 my_utils_open_img (const gchar *fname
227 /*-----------------------------------------------------------------------------
228 * Opens an image from png, hdf or raw-data file
231 GpivImage *image = NULL;
232 gchar *err_msg = NULL;
233 gchar *ext = g_strdup(strrchr(fname, '.'));
234 FILE *fp;
236 #ifdef DEBUG
237 g_message ("open_img:: 0 fname=%s ext=%s", fname, ext);
238 #endif
240 if (fname == NULL) {
241 gpiv_warning ("LIBGPIV internal error: open_img: failing \"fname == NULL\"");
242 return NULL;
246 * Reads hdf format
248 if (strcmp (ext, GPIV_EXT_GPIV) == 0) {
249 #ifdef DEBUG
250 g_message ("OPEN_IMG:: gpiv_fread_hdf5_image");
251 #endif
252 if ((image = gpiv_fread_hdf5_image (fname)) == NULL) {
253 g_message ("LIBGPIV internal error: open_img: gpiv_fread_hdf5_image failed");
254 return NULL;
258 * Reads Portable Network Graphics image format
260 } else if (strcmp (ext, GPIV_EXT_PNG_IMAGE) == 0) {
261 #ifdef DEBUG
262 g_message ("OPEN_IMG:: calling gpiv_fread_png_image");
263 #endif
264 if ((fp = fopen (fname, "rb")) == NULL) {
265 gpiv_warning ("LIBGPIV internal error: open_img: failure opening %s", fname);
266 return NULL;
269 if ((image = gpiv_read_png_image (fp)) == NULL) {
270 g_message ("LIBGPIV internal error: open_img: gpiv_read_png_image failed");
271 return NULL;
274 fclose (fp);
277 * Reads raw data format
279 } else if (strcmp (ext, GPIV_EXT_RAW_IMAGE) == 0) {
280 #ifdef DEBUG
281 g_message ("OPEN_IMG:: gpiv_fread_raw_image");
282 #endif
283 if ((image = gpiv_fread_raw_image (fname)) == NULL) {
284 g_message ("LIBGPIV internal error: open_img: gpiv_read_raw_image failed");
285 return NULL;
289 * Reads LaVision's (tm) DaVis format (.img)
291 } else if (strcmp (ext, GPIV_EXT_DAVIS) == 0) {
292 #ifdef DEBUG
293 g_message ("OPEN_IMG:: gpiv_fread_davis_image");
294 #endif
295 if ((fp = fopen (fname, "rb")) == NULL) {
296 gpiv_warning ("LIBGPIV internal error: open_img: failure opening %s", fname);
297 return NULL;
300 if ((image = gpiv_read_davis_image (fp)) == NULL) {
301 g_message ("LIBGPIV internal error: open_img: gpiv_read_davis_image failed");
302 return NULL;
305 fclose (fp);
308 } else {
309 err_msg = "LIBGPIV internal error: open_img: should not arrive here";
310 gpiv_warning ("%s", err_msg);
311 return NULL;
315 g_free(ext);
316 return image;
321 gchar *
322 my_utils_fcount_hdf5_data (const gchar *fname,
323 gint *nx,
324 gint *ny
326 /*-----------------------------------------------------------------------------
327 * DESCRIPTION:
328 * Reads array lengths of 2-dimensional data
329 *---------------------------------------------------------------------------*/
331 gchar *err_msg = NULL;
332 int i, j, rank_x, rank_y, rank_vx, rank_vy;
333 float *point_x_hdf = NULL, *point_y_hdf = NULL;
335 * HDF declarations
337 hid_t file_id, group_id, dataset_id, dataspace_id;
338 hsize_t dims[2];
339 herr_t status;
341 if ((i = H5Fis_hdf5(fname)) == 0) {
342 err_msg = "gpiv_fcount_hdf5_pivdata: not an hdf5 file";
343 gpiv_warning("%s", err_msg);
344 return err_msg;
346 file_id = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
347 group_id = H5Gopen (file_id, "POSITIONS", H5P_DEFAULT);
350 * getting nx
352 dataset_id = H5Dopen(group_id, "point_x", H5P_DEFAULT);
353 dataspace_id = H5Dget_space(dataset_id);
354 rank_x = H5Sget_simple_extent_ndims (dataspace_id);
355 if (rank_x != 1) {
356 err_msg = "gpiv_fcount_hdf5_pivdata: rank_x != 1";
357 gpiv_warning("%s", err_msg);
358 return err_msg;
360 H5Sget_simple_extent_dims(dataspace_id, dims, NULL);
361 *nx = dims[0];
362 status = H5Dclose(dataset_id);
363 status = H5Sclose(dataspace_id);
366 * getting ny
368 dataset_id = H5Dopen(group_id, "point_y", H5P_DEFAULT);
369 dataspace_id = H5Dget_space(dataset_id);
370 rank_y = H5Sget_simple_extent_ndims (dataspace_id);
371 if (rank_y != 1) {
372 err_msg = "gpiv_fcount_hdf5_pivdata: rank_y != 1";
373 gpiv_warning("%s", err_msg);
374 return err_msg;
376 H5Sget_simple_extent_dims(dataspace_id, dims, NULL);
377 *ny = dims[0];
378 status = H5Dclose(dataset_id);
379 status = H5Sclose(dataspace_id);
382 H5Gclose (group_id);
383 status = H5Fclose(file_id);
384 return err_msg;
389 gboolean
390 my_utils_obtain_nxny_fromline (gchar line[],
391 gboolean *scale,
392 guint *nx,
393 guint *ny,
394 gboolean *increment,
395 gfloat *dat_x,
396 gfloat *dat_y,
397 guint *line_nr
399 /*-----------------------------------------------------------------------------
400 * Scans a line to get nx and ny and scale
403 gfloat x = 0, y = 0;
406 if (line[0] == '#'
407 && *scale == FALSE) {
408 *scale = my_utils_find_data_scaled (line);
412 * Skip comment, blank lines and header line
414 if (line[0] != '#'
415 && line[0] != '\n'
416 && line[0] != '\t') {
417 sscanf (line, "%f %f", &x, &y);
419 * The first line containig data has been found.
420 * When the second has been found it is
421 * determined whether the data have been arranged column-wise or row-wise
423 if (*line_nr == 0) {
424 /* get file position for first data line */
425 *dat_x = x;
426 *dat_y = y;
429 if (*line_nr == 1) {
430 if (x > *dat_x || y > *dat_y) {
431 *increment = TRUE;
432 } else {
433 *increment = FALSE;
438 *line_nr = *line_nr + 1;
439 if (*increment == TRUE) {
440 if (x > *dat_x){
441 *dat_x = x;
442 *nx = *nx + 1;
444 if (y > *dat_y) {
445 *dat_y = y;
446 *ny = *ny + 1;
448 } else {
449 if (x < *dat_x){
450 *dat_x = x;
451 *nx = *nx + 1;
453 if (y < *dat_y) {
454 *dat_y = y;
455 *ny = *ny + 1;