Merge ../gpivtools-omp
[gpivtools.git] / src / post / manipiv.c
blob3a7b85f4e485962dd0462f644044708b4bbda494
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>
36 #include "config.h"
37 #include "git-rev.h"
39 /* #define PARFILE "manipiv.par" */ /* Parameter file name */
40 #define PARFILE "gpivrc" /* Parameter file name */
41 #define USAGE "\
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\
46 \n\
47 keys: \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\
51 fast running x\n\
52 -h | --help: this on-line help \n\
53 -i: interchanges indexes of output data for increasing \n\
54 order \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\
58 reversed order \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\
65 #ifdef DEBUG
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\
70 keys: \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"
74 #endif
77 #define HELP "\
78 manipulates order and positions of PIV data, flips in horizontal \n\
79 or vertical direction and rotates over 90 or 180 degrees"
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 #ifdef GIT_HASH
124 printf ("git hash: %s\n", GIT_REV);
125 #else
126 printf ("version: %s\n", GPIVTOOLS_VERSION);
127 #endif
128 exit(0);
129 break;
130 case 'h':
131 printf("\n%s", argv[0]);
132 printf("\n%s", HELP);
133 printf("\n%s", USAGE);
134 #ifdef DEBUG
135 printf("\n%s\n", USAGE_DEBUG);
136 #endif
137 exit(0);
138 break;
139 case 'r':
140 piv_post_par->operator_manipiv = GPIV_ROT90;
141 piv_post_par->operator_manipiv__set = 1;
142 break;
143 case 'x':
144 piv_post_par->operator_manipiv = GPIV_FLIP_X;
145 piv_post_par->operator_manipiv__set = 1;
146 break;
147 case 'y':
148 piv_post_par->operator_manipiv = GPIV_FLIP_Y;
149 piv_post_par->operator_manipiv__set = 1;
150 break;
151 case 'f':
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);
160 --argc;
161 piv_post_par->block->y_1 = atoi(*++argv);
162 --argc;
163 piv_post_par->block->x_2 = atoi(*++argv);
164 --argc;
165 piv_post_par->block->y_2 = atoi(*++argv);
166 piv_post_par->block__set = TRUE;
167 --argc;
168 argc_next = 1;
169 } else if (strcmp("fy", *argv) == 0) {
170 piv_post_par->operator_manipiv = GPIV_FAST_Y;
171 piv_post_par->operator_manipiv__set = 1;
173 break;
174 case 'p':
175 #ifdef DEBUG
176 if (
177 (strcmp(*argv, "p_main") != 0) &&
178 (strcmp(*argv, "p_flip") != 0) &&
179 (strcmp(*argv, "p_fasty") != 0)
181 #endif /* DEBUG */
182 verbose = TRUE;
183 #ifdef DEBUG
184 } else if (strcmp("p_main", *argv) == 0) {
185 print_main = atoi(*++argv);
186 --argc;
187 argc_next = 1;
188 } else if (strcmp("p_flip", *argv) == 0) {
189 print_flip = atoi(*++argv);
190 --argc;
191 argc_next = 1;
192 } else if (strcmp("p_fasty", *argv) == 0) {
193 print_fasty = atoi(*++argv);
194 --argc;
195 argc_next = 1;
196 } else if (strcmp("p_fread_pivdata", *argv) == 0) {
197 print_fread_pivdata = atoi(*++argv);
198 --argc;
199 argc_next = 1;
201 #endif /* DEBUG */
202 break;
204 * negotion of settings
206 case 'n':
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);
211 --argc;
212 piv_post_par->block->y_1 = atoi(*++argv);
213 --argc;
214 piv_post_par->block->x_2 = atoi(*++argv);
215 --argc;
216 piv_post_par->block->y_2 = atoi(*++argv);
217 piv_post_par->block__set = TRUE;
218 --argc;
219 argc_next = 1;
220 } else {
221 gpiv_error("%s: unknown option: %s", argv[0], *argv);
223 argc_next = 1;
224 break;
227 * long option keys
229 case '-':
230 if (strcmp("-help", *argv) == 0) {
231 printf("\n%s", argv[0]);
232 printf("\n%s", HELP);
233 printf("\n%s", USAGE);
234 exit(0);
235 } else if (strcmp("-print", *argv) == 0) {
236 verbose = TRUE;
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) {
244 #ifdef GIT_HASH
245 printf ("git hash: %s\n", GIT_REV);
246 #else
247 printf ("version: %s\n", GPIVTOOLS_VERSION);
248 #endif
249 exit(0);
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);
254 --argc;
255 piv_post_par->block->y_1 = atoi(*++argv);
256 --argc;
257 piv_post_par->block->x_2 = atoi(*++argv);
258 --argc;
259 piv_post_par->block->y_2 = atoi(*++argv);
260 piv_post_par->block__set = TRUE;
261 --argc;
262 argc_next = 1;
263 } else {
264 gpiv_error("%s: unknown option: %s", argv[0], *argv);
266 argc_next = 1;
267 break;
269 default:
270 gpiv_error("%s: unknown option: %s", argv[0], *argv);
271 break;
277 * Check if filename or stdin / stdout is used
279 if (argc == 1) {
280 use_stdin_stdout = FALSE;
281 strcpy(fname, argv[argc - 1]);
282 } else if (argc == 0) {
283 use_stdin_stdout = TRUE;
284 verbose = FALSE;
285 } else {
286 gpiv_error("%s: unknown argument: %s", argv[0], *argv);
294 static gchar *
295 make_fname(char *fname_in,
296 char *fname_parameter,
297 char *fname_out
299 /*-----------------------------------------------------------------------------
300 * generate filenames
303 gchar *err_msg = NULL;
304 gchar *fname_base = NULL;
306 if (fname_in == NULL ) {
307 err_msg = "make_fname: \"fname_in == NULL\"";
308 return (err_msg);
312 * Stripping filename
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);
326 return (err_msg);
331 int
332 main(int argc,
333 char *argv[]
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
353 * gpiv_manipiv
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;
376 } else {
377 gpiv_error("manipiv: unvalid program name or symlink");
380 command_args(argc, argv, fname_in_piv, piv_post_par);
381 if (verbose) {
382 #ifdef GIT_HASH
383 printf("# Software: %s\n# git hash: %s\n# Command line options:\n",
384 argv[0], GIT_REV);
385 #else
386 printf("# Software: %s\n# version: %s\n# Command line options:\n",
387 argv[0], GPIVTOOLS_VERSION);
388 #endif
389 gpiv_post_print_parameters (NULL, piv_post_par);
394 if (use_stdin_stdout == FALSE) {
395 if ((err_msg =
396 make_fname (fname_in_piv, fname_parameter, fname_out))
397 != NULL)
398 gpiv_error("%s: Failure calling make_fname",
399 argv[0]);
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);
416 if ((err_msg =
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);
420 fclose(fp_par_dat);
423 } else {
424 gpiv_scan_parameter (GPIV_POSTPAR_KEY, PARFILE, piv_post_par, verbose);
425 if ((err_msg =
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) {
438 fp = stdin;
439 } else {
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);
452 } else {
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);
473 #ifdef GIT_HASH
474 piv_data->comment = g_strdup_printf ("# Software: %s\n# git hash: %s\n",
475 argv[0], GIT_REV);
476 #else
477 piv_data->comment = g_strdup_printf ("# Software: %s\n# version: %s\n",
478 argv[0], GPIVTOOLS_VERSION);
479 #endif
480 piv_data->comment = gpiv_add_datetime_to_comment (piv_data->comment);
482 if (use_stdin_stdout == TRUE) {
483 fp = stdout;
484 } else {
485 if ((fp = fopen (fname_out, "w")) == NULL) {
486 gpiv_error ("%s: Failure opening %s for output",
487 argv[0], fname_out);
491 if ((err_msg = gpiv_write_pivdata (fp, piv_data, TRUE)) != NULL) {
492 fclose (fp);
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
501 if (verbose == TRUE)
502 printf("\n");
504 exit (0);