1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
5 manipiv - manipulates (flipping / rotating, etc) PIV data
7 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
8 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 -----------------------------------------------------------------------
26 Flipx; flips data left - right
27 Flipy; flips data up - down
30 ------------------------------------------------------------------------*/
39 /* #define PARFILE "manipiv.par" */ /* Parameter file name */
40 #define PARFILE "gpivrc" /* Parameter file name */
42 Usage: manipiv | fasty | flipx | flipy | revert | rot90 | rot180 \n\
43 [-fi x0 y0 x1 y1] [-no_fi | --pass x0 y0 x1 y1] \n\
44 [-fy] [-h | --help] [-i] [-p | --print] [-r] [--rev] \n\
45 [-v | --version] [-x] [-y] [filename] < stdin > stdout \n\
48 -fi x0 y0 x1 y1: filters out all data from (x0,y0) to (x1,y1) \n\
49 -no_fi | --pass x0 y0 x1 y1: passes through the data from (x0,y0) to (x1,y1) \n\
50 -fy: fast y; returns fast running y-positions of data with \n\
52 -h | --help: this on-line help \n\
53 -i: interchanges indexes of output data for increasing \n\
55 -p | --print: prints parameters to stdout \n\
56 -r: rotates over 90 degrees \n\
57 --rev: reverts array indexes of output data for getting \n\
59 -v | --version: version number \n\
60 -x: flips data in x-direction (vertical axis) \n\
61 -y: flips data in y-direction (horizontal axis) \n\
62 filename: Input PIV data file. Substitutes stdin and stdout\n\
66 #define USAGE_DEBUG "\
67 Developers version also contains: \n\
68 [-p_main][-p_median_residu][-p_subst_residu] \n\
69 [-p_check_residu][-p_residu_statistic] \n\
71 -p_'function' N: prints data to be generated in the function; the \n\
72 higher N, the more detailed the output. \n\
73 For N = 10, err_vec will exit at the end of the function"
78 manipulates order and positions of PIV data, flips in horizontal \n\
79 or vertical direction and rotates over 90 or 180 degrees"
84 gboolean use_stdin_stdout
= FALSE
;
85 gboolean verbose
= FALSE
;
89 * Variables for development version
91 int print_main
= 0, print_flip
= 0, print_fasty
= 0, print_fread_pivdata
=
98 command_args(int argc
,
100 char fname
[GPIV_MAX_CHARS
],
101 GpivPostPar
*piv_post_par
103 /* ----------------------------------------------------------------------------
104 * Command line argument handling
109 while (--argc
> 0 && (*++argv
)[0] == '-') {
113 * argc_next is set to 1 if the next cmd line argument has to be searched for;
114 * in case that the command line argument concerns more than one char or cmd
115 * line argument needs a parameter
117 while (argc_next
== 0 && (c
= *++argv
[0])) {
121 * Use Revision Control System (RCS) for version
124 printf ("git hash: %s\n", GIT_REV
);
126 printf ("version: %s\n", GPIVTOOLS_VERSION
);
131 printf("\n%s", argv
[0]);
132 printf("\n%s", HELP
);
133 printf("\n%s", USAGE
);
135 printf("\n%s\n", USAGE_DEBUG
);
140 piv_post_par
->operator_manipiv
= GPIV_ROT90
;
141 piv_post_par
->operator_manipiv__set
= 1;
144 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
145 piv_post_par
->operator_manipiv__set
= 1;
148 piv_post_par
->operator_manipiv
= GPIV_FLIP_Y
;
149 piv_post_par
->operator_manipiv__set
= 1;
152 if (strcmp("fy", *argv
) != 0 && strcmp("fi", *argv
) != 0) {
153 } else if (strcmp("fi", *argv
) == 0) {
155 * filters block of data from data stream
157 piv_post_par
->operator_manipiv
= GPIV_FILTER_BLOCK
;
158 piv_post_par
->operator_manipiv__set
= 1;
159 piv_post_par
->block
->x_1
= atoi(*++argv
);
161 piv_post_par
->block
->y_1
= atoi(*++argv
);
163 piv_post_par
->block
->x_2
= atoi(*++argv
);
165 piv_post_par
->block
->y_2
= atoi(*++argv
);
166 piv_post_par
->block__set
= TRUE
;
169 } else if (strcmp("fy", *argv
) == 0) {
170 piv_post_par
->operator_manipiv
= GPIV_FAST_Y
;
171 piv_post_par
->operator_manipiv__set
= 1;
177 (strcmp(*argv
, "p_main") != 0) &&
178 (strcmp(*argv
, "p_flip") != 0) &&
179 (strcmp(*argv
, "p_fasty") != 0)
184 } else if (strcmp("p_main", *argv
) == 0) {
185 print_main
= atoi(*++argv
);
188 } else if (strcmp("p_flip", *argv
) == 0) {
189 print_flip
= atoi(*++argv
);
192 } else if (strcmp("p_fasty", *argv
) == 0) {
193 print_fasty
= atoi(*++argv
);
196 } else if (strcmp("p_fread_pivdata", *argv
) == 0) {
197 print_fread_pivdata
= atoi(*++argv
);
204 * negotion of settings
207 if (strcmp(*argv
, "no_fi") == 0) {
208 piv_post_par
->operator_manipiv
= GPIV_PASS_BLOCK
;
209 piv_post_par
->operator_manipiv__set
= 1;
210 piv_post_par
->block
->x_1
= atoi(*++argv
);
212 piv_post_par
->block
->y_1
= atoi(*++argv
);
214 piv_post_par
->block
->x_2
= atoi(*++argv
);
216 piv_post_par
->block
->y_2
= atoi(*++argv
);
217 piv_post_par
->block__set
= TRUE
;
221 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
230 if (strcmp("-help", *argv
) == 0) {
231 printf("\n%s", argv
[0]);
232 printf("\n%s", HELP
);
233 printf("\n%s", USAGE
);
235 } else if (strcmp("-print", *argv
) == 0) {
237 } else if (strcmp("-rev", *argv
) == 0) {
239 * reverts indexes of output data for increasing order
241 piv_post_par
->operator_manipiv
= GPIV_REVERT
;
242 piv_post_par
->operator_manipiv__set
= 1;
243 } else if (strcmp("-version", *argv
) == 0) {
245 printf ("git hash: %s\n", GIT_REV
);
247 printf ("version: %s\n", GPIVTOOLS_VERSION
);
250 } else if (strcmp("-pass", *argv
) == 0) {
251 piv_post_par
->operator_manipiv
= GPIV_PASS_BLOCK
;
252 piv_post_par
->operator_manipiv__set
= 1;
253 piv_post_par
->block
->x_1
= atoi(*++argv
);
255 piv_post_par
->block
->y_1
= atoi(*++argv
);
257 piv_post_par
->block
->x_2
= atoi(*++argv
);
259 piv_post_par
->block
->y_2
= atoi(*++argv
);
260 piv_post_par
->block__set
= TRUE
;
264 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
270 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
277 * Check if filename or stdin / stdout is used
280 use_stdin_stdout
= FALSE
;
281 strcpy(fname
, argv
[argc
- 1]);
282 } else if (argc
== 0) {
283 use_stdin_stdout
= TRUE
;
286 gpiv_error("%s: unknown argument: %s", argv
[0], *argv
);
295 make_fname(char *fname_in
,
296 char *fname_parameter
,
299 /*-----------------------------------------------------------------------------
303 gchar
*err_msg
= NULL
;
304 gchar
*fname_base
= NULL
;
306 if (fname_in
== NULL
) {
307 err_msg
= "make_fname: \"fname_in == NULL\"";
314 fname_base
= g_strdup(fname_in
);
315 strtok(fname_base
, ".");
318 * filenames for output
320 gpiv_io_make_fname(fname_base
, GPIV_EXT_PAR
, fname_parameter
);
321 if (verbose
) printf("# Data parameter file: %s\n", fname_parameter
);
323 gpiv_io_make_fname(fname_base
, GPIV_EXT_MANI
, fname_out
);
324 if (verbose
) printf("# Output file: %s\n", fname_out
);
335 /*-----------------------------------------------------------------------------
338 gchar
*err_msg
= NULL
, *c
= NULL
;
339 FILE *fp
, *fp_par_dat
;
340 gchar fname_out
[GPIV_MAX_CHARS
],
341 fname_parameter
[GPIV_MAX_CHARS
], fname_in_piv
[GPIV_MAX_CHARS
];
343 GpivPostPar
*piv_post_par
= g_new (GpivPostPar
, 1);
344 GpivRoi
*block
= g_new (GpivRoi
, 1);
345 GpivPivData
*piv_data
= NULL
;
348 piv_post_par
->block
= block
;
349 gpiv_post_parameters_set (piv_post_par
, FALSE
);
352 * Define GpivOperation type from program name, which is a symbolic link to
355 if ((c
= strstr (argv
[0], "manipiv")) != NULL
) {
356 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
357 piv_post_par
->operator_manipiv__set
= FALSE
;
358 } else if ((c
= strstr (argv
[0], "flipx")) != NULL
) {
359 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
360 piv_post_par
->operator_manipiv__set
= TRUE
;
361 } else if ((c
= strstr (argv
[0], "flipy")) != NULL
) {
362 piv_post_par
->operator_manipiv
= GPIV_FLIP_Y
;
363 piv_post_par
->operator_manipiv__set
= TRUE
;
364 } else if ((c
= strstr (argv
[0], "rot180")) != NULL
) {
365 piv_post_par
->operator_manipiv
= GPIV_ROT180
;
366 piv_post_par
->operator_manipiv__set
= TRUE
;
367 } else if ((c
= strstr (argv
[0], "rot90")) != NULL
) {
368 piv_post_par
->operator_manipiv
= GPIV_ROT90
;
369 piv_post_par
->operator_manipiv__set
= TRUE
;
370 } else if ((c
= strstr (argv
[0], "revert")) != NULL
) {
371 piv_post_par
->operator_manipiv
= GPIV_REVERT
;
372 piv_post_par
->operator_manipiv__set
= TRUE
;
373 } else if ((c
= strstr (argv
[0], "fasty")) != NULL
) {
374 piv_post_par
->operator_manipiv
= GPIV_FAST_Y
;
375 piv_post_par
->operator_manipiv__set
= TRUE
;
377 gpiv_error("manipiv: unvalid program name or symlink");
380 command_args(argc
, argv
, fname_in_piv
, piv_post_par
);
383 printf("# Software: %s\n# git hash: %s\n# Command line options:\n",
386 printf("# Software: %s\n# version: %s\n# Command line options:\n",
387 argv
[0], GPIVTOOLS_VERSION
);
389 gpiv_post_print_parameters (NULL
, piv_post_par
);
394 if (use_stdin_stdout
== FALSE
) {
396 make_fname (fname_in_piv
, fname_parameter
, fname_out
))
398 gpiv_error("%s: Failure calling make_fname",
402 * Prints command line parameters to par-file
404 if ((fp_par_dat
= fopen(fname_parameter
, "a")) == NULL
) {
405 gpiv_error("%s: failure opening %s for input",
406 argv
[0], fname_parameter
);
408 fprintf(fp_par_dat
, "\n\n# %s\n# Command line options:\n", argv
[0]);
409 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
413 * Reading parametes from PARFILE (and writing to data par-file)
415 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
417 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
418 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
419 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
424 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
426 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
427 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
430 gpiv_post_check_parameters_read (piv_post_par
, NULL
);
434 * Reading PIV data set.
435 * Input PIV data might be in reverse order
437 if (use_stdin_stdout
== TRUE
) {
440 if ((fp
= fopen (fname_in_piv
, "r")) == NULL
) {
441 gpiv_error ("%s: Failure opening %s for input",
442 argv
[0], fname_in_piv
);
446 if (piv_post_par
->operator_manipiv
== GPIV_FAST_Y
) {
447 if ((piv_data
= gpiv_read_pivdata_fastx (fp
)) == NULL
) {
448 err_msg
= "GPIV_POST_MANIPIV: Failure calling read_pivdata_fast_x";
449 gpiv_error ("%s: %s", argv
[0], err_msg
);
453 if ((piv_data
= gpiv_read_pivdata (fp
)) == NULL
) {
454 err_msg
= "GPIV_POST_MANIPIV: Failure calling gpiv_read_pivdata";
455 gpiv_error ("%s: %s", argv
[0], err_msg
);
459 if (use_stdin_stdout
== FALSE
) fclose (fp
);
462 * Here the library function call of the post-processing.
464 if ((err_msg
= gpiv_post_manipiv (piv_data
, piv_post_par
)) != NULL
) {
465 gpiv_error ("%s: ", argv
[0], err_msg
);
469 * Adding comment to the data
470 * And writing to output
472 g_free (piv_data
->comment
);
474 piv_data
->comment
= g_strdup_printf ("# Software: %s\n# git hash: %s\n",
477 piv_data
->comment
= g_strdup_printf ("# Software: %s\n# version: %s\n",
478 argv
[0], GPIVTOOLS_VERSION
);
480 piv_data
->comment
= gpiv_add_datetime_to_comment (piv_data
->comment
);
482 if (use_stdin_stdout
== TRUE
) {
485 if ((fp
= fopen (fname_out
, "w")) == NULL
) {
486 gpiv_error ("%s: Failure opening %s for output",
491 if ((err_msg
= gpiv_write_pivdata (fp
, piv_data
, TRUE
)) != NULL
) {
493 gpiv_error ("%s: %s", argv
[0], err_msg
);
495 if (use_stdin_stdout
== FALSE
) fclose (fp
);
499 * Freeing allocated memory of matrices, if needed