added 'Substitute all FNAME occurrences' option
[gpivtools.git] / src / post / manipiv.c
blobfda52934501f71782c459f07cd8f8b89a7c83695
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)
13 any later version.
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 ------------------------------------------------------------------------*/
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <glib.h>
35 #include <gpiv.h>
37 /* #define PARFILE "manipiv.par" */ /* Parameter file name */
38 #define PARFILE "gpivrc" /* Parameter file name */
39 #define USAGE "\
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\
44 \n\
45 keys: \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\
49 fast running x\n\
50 -h | --help: this on-line help \n\
51 -i: interchanges indexes of output data for increasing \n\
52 order \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\
56 reversed order \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\
63 #ifdef DEBUG
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\
68 keys: \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"
72 #endif
75 #define HELP "\
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 $"
82 * Global variables
84 gboolean use_stdin_stdout = FALSE;
85 gboolean verbose = FALSE;
87 #ifdef DEBUG
89 * Variables for development version
91 int print_main = 0, print_flip = 0, print_fasty = 0, print_fread_pivdata =
93 #endif
97 void
98 command_args(int argc,
99 char *argv[],
100 char fname[GPIV_MAX_CHARS],
101 GpivPostPar *piv_post_par
103 /* ----------------------------------------------------------------------------
104 * Command line argument handling
107 char c;
108 int argc_next;
109 while (--argc > 0 && (*++argv)[0] == '-') {
110 argc_next = 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])) {
118 switch (c) {
119 case 'v':
121 * Use Revision Control System (RCS) for version
123 printf("\n%s\n", RCSID);
124 exit(0);
125 break;
126 case 'h':
127 printf("\n%s", argv[0]);
128 printf("\n%s", HELP);
129 printf("\n%s", USAGE);
130 #ifdef DEBUG
131 printf("\n%s\n", USAGE_DEBUG);
132 #endif
133 exit(0);
134 break;
135 case 'r':
136 piv_post_par->operator_manipiv = GPIV_ROT90;
137 piv_post_par->operator_manipiv__set = 1;
138 break;
139 case 'x':
140 piv_post_par->operator_manipiv = GPIV_FLIP_X;
141 piv_post_par->operator_manipiv__set = 1;
142 break;
143 case 'y':
144 piv_post_par->operator_manipiv = GPIV_FLIP_Y;
145 piv_post_par->operator_manipiv__set = 1;
146 break;
147 case 'f':
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);
156 --argc;
157 piv_post_par->block->y_1 = atoi(*++argv);
158 --argc;
159 piv_post_par->block->x_2 = atoi(*++argv);
160 --argc;
161 piv_post_par->block->y_2 = atoi(*++argv);
162 piv_post_par->block__set = TRUE;
163 --argc;
164 argc_next = 1;
165 } else if (strcmp("fy", *argv) == 0) {
166 piv_post_par->operator_manipiv = GPIV_FAST_Y;
167 piv_post_par->operator_manipiv__set = 1;
169 break;
170 case 'p':
171 #ifdef DEBUG
172 if (
173 (strcmp(*argv, "p_main") != 0) &&
174 (strcmp(*argv, "p_flip") != 0) &&
175 (strcmp(*argv, "p_fasty") != 0)
177 #endif /* DEBUG */
178 verbose = TRUE;
179 #ifdef DEBUG
180 } else if (strcmp("p_main", *argv) == 0) {
181 print_main = atoi(*++argv);
182 --argc;
183 argc_next = 1;
184 } else if (strcmp("p_flip", *argv) == 0) {
185 print_flip = atoi(*++argv);
186 --argc;
187 argc_next = 1;
188 } else if (strcmp("p_fasty", *argv) == 0) {
189 print_fasty = atoi(*++argv);
190 --argc;
191 argc_next = 1;
192 } else if (strcmp("p_fread_pivdata", *argv) == 0) {
193 print_fread_pivdata = atoi(*++argv);
194 --argc;
195 argc_next = 1;
197 #endif /* DEBUG */
198 break;
200 * negotion of settings
202 case 'n':
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);
207 --argc;
208 piv_post_par->block->y_1 = atoi(*++argv);
209 --argc;
210 piv_post_par->block->x_2 = atoi(*++argv);
211 --argc;
212 piv_post_par->block->y_2 = atoi(*++argv);
213 piv_post_par->block__set = TRUE;
214 --argc;
215 argc_next = 1;
216 } else {
217 gpiv_error("%s: unknown option: %s", argv[0], *argv);
219 argc_next = 1;
220 break;
223 * long option keys
225 case '-':
226 if (strcmp("-help", *argv) == 0) {
227 printf("\n%s", argv[0]);
228 printf("\n%s", HELP);
229 printf("\n%s", USAGE);
230 exit(0);
231 } else if (strcmp("-print", *argv) == 0) {
232 verbose = TRUE;
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);
241 exit(0);
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);
246 --argc;
247 piv_post_par->block->y_1 = atoi(*++argv);
248 --argc;
249 piv_post_par->block->x_2 = atoi(*++argv);
250 --argc;
251 piv_post_par->block->y_2 = atoi(*++argv);
252 piv_post_par->block__set = TRUE;
253 --argc;
254 argc_next = 1;
255 } else {
256 gpiv_error("%s: unknown option: %s", argv[0], *argv);
258 argc_next = 1;
259 break;
261 default:
262 gpiv_error("%s: unknown option: %s", argv[0], *argv);
263 break;
269 * Check if filename or stdin / stdout is used
271 if (argc == 1) {
272 use_stdin_stdout = FALSE;
273 strcpy(fname, argv[argc - 1]);
274 } else if (argc == 0) {
275 use_stdin_stdout = TRUE;
276 verbose = FALSE;
277 } else {
278 gpiv_error("%s: unknown argument: %s", argv[0], *argv);
286 static gchar *
287 make_fname(char *fname_in,
288 char *fname_parameter,
289 char *fname_out
291 /*-----------------------------------------------------------------------------
292 * generate filenames
295 gchar *err_msg = NULL;
296 gchar *fname_base = NULL;
298 if (fname_in == NULL ) {
299 err_msg = "make_fname: \"fname_in == NULL\"";
300 return (err_msg);
304 * Stripping filename
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);
318 return (err_msg);
323 int
324 main(int argc,
325 char *argv[]
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
345 * gpiv_manipiv
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;
368 } else {
369 gpiv_error("manipiv: unvalid program name or symlink");
372 command_args(argc, argv, fname_in_piv, piv_post_par);
373 if (verbose) {
374 printf("# %s\n# Command line options:\n", RCSID);
375 gpiv_post_print_parameters (NULL, piv_post_par);
380 if (use_stdin_stdout == FALSE) {
381 if ((err_msg =
382 make_fname (fname_in_piv, fname_parameter, fname_out))
383 != NULL)
384 gpiv_error("%s: Failure calling make_fname",
385 argv[0]);
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);
402 if ((err_msg =
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);
406 fclose(fp_par_dat);
409 } else {
410 gpiv_scan_parameter (GPIV_POSTPAR_KEY, PARFILE, piv_post_par, verbose);
411 if ((err_msg =
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) {
424 fp = stdin;
425 } else {
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);
438 } else {
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) {
463 fp = stdout;
464 } else {
465 if ((fp = fopen (fname_out, "w")) == NULL) {
466 gpiv_error ("%s: Failure opening %s for output",
467 argv[0], fname_out);
471 if ((err_msg = gpiv_write_pivdata (fp, piv_data, TRUE)) != NULL) {
472 fclose (fp);
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
481 if (verbose == TRUE)
482 printf("\n");
484 exit (0);