1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
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)
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 -------------------------------------------------------------------------------
28 LAST MODIFICATION DATE: $Id: my_utils.c,v 1.2 2008-10-09 14:02:03 gerber Exp $
29 --------------------------------------------------------------------------- */
37 my_utils_fopen_tmp (const gchar
*tmpfile
,
39 /*-----------------------------------------------------------------------------
40 * Attempts to open a filepointer for TMPDIR/USER/tmpfile, else in
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
,
56 if ((fp
= fopen(filename
, mode
)) != NULL
) {
57 if ((return_val
= g_mkdir(tmp_user_dir
, 700)) != 0) {
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",
72 g_free (tmp_user_dir
);
79 my_utils_write_tmp_image (const GpivImage
*image
,
80 const gchar
*basename
,
83 /*-----------------------------------------------------------------------------
85 * Stores deformed image to file system with pre defined name to TMPDIR
86 * and prints message to stdout
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
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
);
107 if ((fp
= my_utils_fopen_tmp (filename
, "wb")) == NULL
) {
108 err_msg
= "gpiv_piv_write_tmp_image: Failure opening for output";
112 g_message ("my_utils_write_tmp_image: %s %s",
113 message
, g_strdup_printf ("%s/%s/%s", tmp_dir
, user_name
, filename
));
116 gpiv_write_png_image (fp
, image
, FALSE
)) != NULL
) {
130 my_utils_find_data_scaled (const gchar line
[GPIV_MAX_CHARS
]
132 /*-----------------------------------------------------------------------------
134 * Determines if the data have been scaled in order to read x-and y
135 * positions as floating point varibles
137 * PROTOTYPE LOCATATION:
141 * line: character line containing program that generated data
146 * scale: TRUE if scaled, else FALSE
147 *---------------------------------------------------------------------------*/
149 gboolean scale
= FALSE
;
152 if (strstr(line
,"scale") != '\0') {
163 my_utils_count_asciidata (FILE *fp
,
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
;
186 gfloat dat_x
= -10.0e5
, dat_y
= -10.0e5
;
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
);
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
);
215 g_message ("count_asciidata:: nx = %d ny = %d", *nx
, *ny
);
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
, '.'));
237 g_message ("open_img:: 0 fname=%s ext=%s", fname
, ext
);
241 gpiv_warning ("LIBGPIV internal error: open_img: failing \"fname == NULL\"");
248 if (strcmp (ext
, GPIV_EXT_GPIV
) == 0) {
250 g_message ("OPEN_IMG:: gpiv_fread_hdf5_image");
252 if ((image
= gpiv_fread_hdf5_image (fname
)) == NULL
) {
253 g_message ("LIBGPIV internal error: open_img: gpiv_fread_hdf5_image failed");
258 * Reads Portable Network Graphics image format
260 } else if (strcmp (ext
, GPIV_EXT_PNG_IMAGE
) == 0) {
262 g_message ("OPEN_IMG:: calling gpiv_fread_png_image");
264 if ((fp
= fopen (fname
, "rb")) == NULL
) {
265 gpiv_warning ("LIBGPIV internal error: open_img: failure opening %s", fname
);
269 if ((image
= gpiv_read_png_image (fp
)) == NULL
) {
270 g_message ("LIBGPIV internal error: open_img: gpiv_read_png_image failed");
277 * Reads raw data format
279 } else if (strcmp (ext
, GPIV_EXT_RAW_IMAGE
) == 0) {
281 g_message ("OPEN_IMG:: gpiv_fread_raw_image");
283 if ((image
= gpiv_fread_raw_image (fname
)) == NULL
) {
284 g_message ("LIBGPIV internal error: open_img: gpiv_read_raw_image failed");
289 * Reads LaVision's (tm) DaVis format (.img)
291 } else if (strcmp (ext
, GPIV_EXT_DAVIS
) == 0) {
293 g_message ("OPEN_IMG:: gpiv_fread_davis_image");
295 if ((fp
= fopen (fname
, "rb")) == NULL
) {
296 gpiv_warning ("LIBGPIV internal error: open_img: failure opening %s", fname
);
300 if ((image
= gpiv_read_davis_image (fp
)) == NULL
) {
301 g_message ("LIBGPIV internal error: open_img: gpiv_read_davis_image failed");
309 err_msg
= "LIBGPIV internal error: open_img: should not arrive here";
310 gpiv_warning ("%s", err_msg
);
322 my_utils_fcount_hdf5_data (const gchar
*fname
,
326 /*-----------------------------------------------------------------------------
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
;
337 hid_t file_id
, group_id
, dataset_id
, dataspace_id
;
341 if ((i
= H5Fis_hdf5(fname
)) == 0) {
342 err_msg
= "gpiv_fcount_hdf5_pivdata: not an hdf5 file";
343 gpiv_warning("%s", err_msg
);
346 file_id
= H5Fopen(fname
, H5F_ACC_RDONLY
, H5P_DEFAULT
);
347 group_id
= H5Gopen (file_id
, "POSITIONS", H5P_DEFAULT
);
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
);
356 err_msg
= "gpiv_fcount_hdf5_pivdata: rank_x != 1";
357 gpiv_warning("%s", err_msg
);
360 H5Sget_simple_extent_dims(dataspace_id
, dims
, NULL
);
362 status
= H5Dclose(dataset_id
);
363 status
= H5Sclose(dataspace_id
);
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
);
372 err_msg
= "gpiv_fcount_hdf5_pivdata: rank_y != 1";
373 gpiv_warning("%s", err_msg
);
376 H5Sget_simple_extent_dims(dataspace_id
, dims
, NULL
);
378 status
= H5Dclose(dataset_id
);
379 status
= H5Sclose(dataspace_id
);
383 status
= H5Fclose(file_id
);
390 my_utils_obtain_nxny_fromline (gchar line
[],
399 /*-----------------------------------------------------------------------------
400 * Scans a line to get nx and ny and scale
407 && *scale
== FALSE
) {
408 *scale
= my_utils_find_data_scaled (line
);
412 * Skip comment, blank lines and header line
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
424 /* get file position for first data line */
430 if (x
> *dat_x
|| y
> *dat_y
) {
438 *line_nr
= *line_nr
+ 1;
439 if (*increment
== TRUE
) {