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 ------------------------------------------------------------------------*/
37 /* #define PARFILE "manipiv.par" */ /* Parameter file name */
38 #define PARFILE "gpivrc" /* Parameter file name */
40 Usage: manipiv | fasty | flipx | flipy | revert | rot90 | rot180 \n\
41 [-fi x0 y0 x1 y1] [-no_fi | --pass x0 y0 x1 y1] \n\
42 [-fy] [-h | --help] [-i] [-p | --print] [-r] [--rev] \n\
43 [-v | --version] [-x] [-y] [filename] < stdin > stdout \n\
46 -fi x0 y0 x1 y1: filters out all data from (x0,y0) to (x1,y1) \n\
47 -no_fi | --pass x0 y0 x1 y1: passes through the data from (x0,y0) to (x1,y1) \n\
48 -fy: fast y; returns fast running y-positions of data with \n\
50 -h | --help: this on-line help \n\
51 -i: interchanges indexes of output data for increasing \n\
53 -p | --print: prints parameters to stdout \n\
54 -r: rotates over 90 degrees \n\
55 --rev: reverts array indexes of output data for getting \n\
57 -v | --version: version number \n\
58 -x: flips data in x-direction (vertical axis) \n\
59 -y: flips data in y-direction (horizontal axis) \n\
60 filename: Input PIV data file. Substitutes stdin and stdout\n\
64 #define USAGE_DEBUG "\
65 Developers version also contains: \n\
66 [-p_main][-p_median_residu][-p_subst_residu] \n\
67 [-p_check_residu][-p_residu_statistic] \n\
69 -p_'function' N: prints data to be generated in the function; the \n\
70 higher N, the more detailed the output. \n\
71 For N = 10, err_vec will exit at the end of the function"
76 manipulates order and positions of PIV data, flips in horizontal \n\
77 or vertical direction and rotates over 90 or 180 degrees"
79 #define RCSID "$Id: manipiv.c,v 2.13 2008-09-25 13:08:34 gerber Exp $"
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
123 printf("\n%s\n", RCSID
);
127 printf("\n%s", argv
[0]);
128 printf("\n%s", HELP
);
129 printf("\n%s", USAGE
);
131 printf("\n%s\n", USAGE_DEBUG
);
136 piv_post_par
->operator_manipiv
= GPIV_ROT90
;
137 piv_post_par
->operator_manipiv__set
= 1;
140 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
141 piv_post_par
->operator_manipiv__set
= 1;
144 piv_post_par
->operator_manipiv
= GPIV_FLIP_Y
;
145 piv_post_par
->operator_manipiv__set
= 1;
148 if (strcmp("fy", *argv
) != 0 && strcmp("fi", *argv
) != 0) {
149 } else if (strcmp("fi", *argv
) == 0) {
151 * filters block of data from data stream
153 piv_post_par
->operator_manipiv
= GPIV_FILTER_BLOCK
;
154 piv_post_par
->operator_manipiv__set
= 1;
155 piv_post_par
->block
->x_1
= atoi(*++argv
);
157 piv_post_par
->block
->y_1
= atoi(*++argv
);
159 piv_post_par
->block
->x_2
= atoi(*++argv
);
161 piv_post_par
->block
->y_2
= atoi(*++argv
);
162 piv_post_par
->block__set
= TRUE
;
165 } else if (strcmp("fy", *argv
) == 0) {
166 piv_post_par
->operator_manipiv
= GPIV_FAST_Y
;
167 piv_post_par
->operator_manipiv__set
= 1;
173 (strcmp(*argv
, "p_main") != 0) &&
174 (strcmp(*argv
, "p_flip") != 0) &&
175 (strcmp(*argv
, "p_fasty") != 0)
180 } else if (strcmp("p_main", *argv
) == 0) {
181 print_main
= atoi(*++argv
);
184 } else if (strcmp("p_flip", *argv
) == 0) {
185 print_flip
= atoi(*++argv
);
188 } else if (strcmp("p_fasty", *argv
) == 0) {
189 print_fasty
= atoi(*++argv
);
192 } else if (strcmp("p_fread_pivdata", *argv
) == 0) {
193 print_fread_pivdata
= atoi(*++argv
);
200 * negotion of settings
203 if (strcmp(*argv
, "no_fi") == 0) {
204 piv_post_par
->operator_manipiv
= GPIV_PASS_BLOCK
;
205 piv_post_par
->operator_manipiv__set
= 1;
206 piv_post_par
->block
->x_1
= atoi(*++argv
);
208 piv_post_par
->block
->y_1
= atoi(*++argv
);
210 piv_post_par
->block
->x_2
= atoi(*++argv
);
212 piv_post_par
->block
->y_2
= atoi(*++argv
);
213 piv_post_par
->block__set
= TRUE
;
217 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
226 if (strcmp("-help", *argv
) == 0) {
227 printf("\n%s", argv
[0]);
228 printf("\n%s", HELP
);
229 printf("\n%s", USAGE
);
231 } else if (strcmp("-print", *argv
) == 0) {
233 } else if (strcmp("-rev", *argv
) == 0) {
235 * reverts indexes of output data for increasing order
237 piv_post_par
->operator_manipiv
= GPIV_REVERT
;
238 piv_post_par
->operator_manipiv__set
= 1;
239 } else if (strcmp("-version", *argv
) == 0) {
240 printf("%s\n", RCSID
);
242 } else if (strcmp("-pass", *argv
) == 0) {
243 piv_post_par
->operator_manipiv
= GPIV_PASS_BLOCK
;
244 piv_post_par
->operator_manipiv__set
= 1;
245 piv_post_par
->block
->x_1
= atoi(*++argv
);
247 piv_post_par
->block
->y_1
= atoi(*++argv
);
249 piv_post_par
->block
->x_2
= atoi(*++argv
);
251 piv_post_par
->block
->y_2
= atoi(*++argv
);
252 piv_post_par
->block__set
= TRUE
;
256 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
262 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
269 * Check if filename or stdin / stdout is used
272 use_stdin_stdout
= FALSE
;
273 strcpy(fname
, argv
[argc
- 1]);
274 } else if (argc
== 0) {
275 use_stdin_stdout
= TRUE
;
278 gpiv_error("%s: unknown argument: %s", argv
[0], *argv
);
287 make_fname(char *fname_in
,
288 char *fname_parameter
,
291 /*-----------------------------------------------------------------------------
295 gchar
*err_msg
= NULL
;
296 gchar
*fname_base
= NULL
;
298 if (fname_in
== NULL
) {
299 err_msg
= "make_fname: \"fname_in == NULL\"";
306 fname_base
= g_strdup(fname_in
);
307 strtok(fname_base
, ".");
310 * filenames for output
312 gpiv_io_make_fname(fname_base
, GPIV_EXT_PAR
, fname_parameter
);
313 if (verbose
) printf("# Data parameter file: %s\n", fname_parameter
);
315 gpiv_io_make_fname(fname_base
, GPIV_EXT_MANI
, fname_out
);
316 if (verbose
) printf("# Output file: %s\n", fname_out
);
327 /*-----------------------------------------------------------------------------
330 gchar
*err_msg
= NULL
, *c
= NULL
;
331 FILE *fp
, *fp_par_dat
;
332 gchar fname_out
[GPIV_MAX_CHARS
],
333 fname_parameter
[GPIV_MAX_CHARS
], fname_in_piv
[GPIV_MAX_CHARS
];
335 GpivPostPar
*piv_post_par
= g_new (GpivPostPar
, 1);
336 GpivRoi
*block
= g_new (GpivRoi
, 1);
337 GpivPivData
*piv_data
= NULL
;
340 piv_post_par
->block
= block
;
341 gpiv_post_parameters_set (piv_post_par
, FALSE
);
344 * Define GpivOperation type from program name, which is a symbolic link to
347 if ((c
= strstr (argv
[0], "manipiv")) != NULL
) {
348 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
349 piv_post_par
->operator_manipiv__set
= FALSE
;
350 } else if ((c
= strstr (argv
[0], "flipx")) != NULL
) {
351 piv_post_par
->operator_manipiv
= GPIV_FLIP_X
;
352 piv_post_par
->operator_manipiv__set
= TRUE
;
353 } else if ((c
= strstr (argv
[0], "flipy")) != NULL
) {
354 piv_post_par
->operator_manipiv
= GPIV_FLIP_Y
;
355 piv_post_par
->operator_manipiv__set
= TRUE
;
356 } else if ((c
= strstr (argv
[0], "rot180")) != NULL
) {
357 piv_post_par
->operator_manipiv
= GPIV_ROT180
;
358 piv_post_par
->operator_manipiv__set
= TRUE
;
359 } else if ((c
= strstr (argv
[0], "rot90")) != NULL
) {
360 piv_post_par
->operator_manipiv
= GPIV_ROT90
;
361 piv_post_par
->operator_manipiv__set
= TRUE
;
362 } else if ((c
= strstr (argv
[0], "revert")) != NULL
) {
363 piv_post_par
->operator_manipiv
= GPIV_REVERT
;
364 piv_post_par
->operator_manipiv__set
= TRUE
;
365 } else if ((c
= strstr (argv
[0], "fasty")) != NULL
) {
366 piv_post_par
->operator_manipiv
= GPIV_FAST_Y
;
367 piv_post_par
->operator_manipiv__set
= TRUE
;
369 gpiv_error("manipiv: unvalid program name or symlink");
372 command_args(argc
, argv
, fname_in_piv
, piv_post_par
);
374 printf("# %s\n# Command line options:\n", RCSID
);
375 gpiv_post_print_parameters (NULL
, piv_post_par
);
380 if (use_stdin_stdout
== FALSE
) {
382 make_fname (fname_in_piv
, fname_parameter
, fname_out
))
384 gpiv_error("%s: Failure calling make_fname",
388 * Prints command line parameters to par-file
390 if ((fp_par_dat
= fopen(fname_parameter
, "a")) == NULL
) {
391 gpiv_error("%s: failure opening %s for input",
392 argv
[0], fname_parameter
);
394 fprintf(fp_par_dat
, "\n\n# %s\n# Command line options:\n", argv
[0]);
395 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
399 * Reading parametes from PARFILE (and writing to data par-file)
401 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
403 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
404 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
405 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
410 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
412 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
413 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
416 gpiv_post_check_parameters_read (piv_post_par
, NULL
);
420 * Reading PIV data set.
421 * Input PIV data might be in reverse order
423 if (use_stdin_stdout
== TRUE
) {
426 if ((fp
= fopen (fname_in_piv
, "r")) == NULL
) {
427 gpiv_error ("%s: Failure opening %s for input",
428 argv
[0], fname_in_piv
);
432 if (piv_post_par
->operator_manipiv
== GPIV_FAST_Y
) {
433 if ((piv_data
= gpiv_read_pivdata_fastx (fp
)) == NULL
) {
434 err_msg
= "GPIV_POST_MANIPIV: Failure calling read_pivdata_fast_x";
435 gpiv_error ("%s: %s", argv
[0], err_msg
);
439 if ((piv_data
= gpiv_read_pivdata (fp
)) == NULL
) {
440 err_msg
= "GPIV_POST_MANIPIV: Failure calling gpiv_read_pivdata";
441 gpiv_error ("%s: %s", argv
[0], err_msg
);
445 if (use_stdin_stdout
== FALSE
) fclose (fp
);
448 * Here the library function call of the post-processing.
450 if ((err_msg
= gpiv_post_manipiv (piv_data
, piv_post_par
)) != NULL
) {
451 gpiv_error ("%s: ", argv
[0], err_msg
);
455 * Adding comment to the data
456 * And writing to output
458 g_free (piv_data
->comment
);
459 piv_data
->comment
= g_strdup_printf ("# Software: %s\n", RCSID
);
460 piv_data
->comment
= gpiv_add_datetime_to_comment (piv_data
->comment
);
462 if (use_stdin_stdout
== TRUE
) {
465 if ((fp
= fopen (fname_out
, "w")) == NULL
) {
466 gpiv_error ("%s: Failure opening %s for output",
471 if ((err_msg
= gpiv_write_pivdata (fp
, piv_data
, TRUE
)) != NULL
) {
473 gpiv_error ("%s: %s", argv
[0], err_msg
);
475 if (use_stdin_stdout
== FALSE
) fclose (fp
);
479 * Freeing allocated memory of matrices, if needed