1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
5 s-avg - reports spatial global mean velocity and rms error of a PIV data
6 stream. Eventually subtracts mean value from piv the data.
8 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
9 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ------------------------------------------------------------------------*/
35 /* #define PARFILE "savg.par" */ /* Parameter file name */
36 #define PARFILE "gpivrc" /* Parameter file name */
38 Usage: scale [-h | --help] [-p | --print] [-s|-no_s] \n\
39 [-v | --version] [-z dx dy] [filename] < stdin > stdout \n\
42 -h | --help: this on-line help \n\
43 -p | --print: print parameters to stdout \n\
44 -s: subtracts nothing (0), mean of dx and dy (2) from \n\
45 displacements or velocity estimators \n\
46 -no_s: suppresses subtracting mean from input data \n\
47 -v | --version: prints version \n\
48 -z dx dy: zero offset of velocities/displacements \n\
49 filename: input PIV file. Substitutes stdin and stdout \n\
54 #define USAGE_DEBUG "\
55 Developers version also contains: \n\
58 -p_'function' N; prints data to be generated in the function; the \n\
59 higher N, the more detailed the output. \n\
60 For N = 10, err_vec will exit at the end of the function"
65 s-avg reports spatial global mean velocity and rms error of a PIV data stream"
70 gboolean use_stdin_stdout
= FALSE
;
71 gboolean verbose
= FALSE
;
75 * Variables for development version
83 command_args(int argc
, char *argv
[],
84 char fname
[GPIV_MAX_CHARS
],
85 GpivPostPar
* piv_post_par
87 /* ----------------------------------------------------------------------------
88 * Command line argument handling
95 while (--argc
> 0 && (*++argv
)[0] == '-') {
99 * argc_next is set to 1 if the next cmd line argument has to be
100 * searched for; in case that the command line argument concerns more
101 * than one char or cmd line argument needs a parameter
103 while (argc_next
==0 && (c
= *++argv
[0])) {
108 * Use Git revision control system
111 printf ("git hash: %s\n", GIT_REV
);
113 printf ("version: %s\n", GPIVTOOLS_VERSION
);
118 printf("%s\n", argv
[0]);
120 printf("%s\n",USAGE
);
122 printf ("\n%s\n",USAGE_DEBUG
);
129 piv_post_par
->subtract
= atoi(*++argv
);
130 piv_post_par
->subtract__set
= TRUE
;
136 if ((strcmp(*argv
,"p_main" ) !=0)) {
140 } else if (strcmp("p_main",*argv
) == 0) {
141 print_main
=atoi(*++argv
);
149 piv_post_par
->z_off_dx
= atof(*++argv
);
150 piv_post_par
->z_off_dy
= atof(*++argv
);
151 fprintf(stderr
, "\n000:: z_off_dx=%f", piv_post_par
->z_off_dx
);
152 fprintf(stderr
, " z_off_dy=%f\n", piv_post_par
->z_off_dy
);
153 piv_post_par
->z_off_dx__set
= TRUE
;
154 piv_post_par
->z_off_dy__set
= TRUE
;
162 * negotion of settings
164 if (strcmp("no_s", *argv
) == 0) { /* --- do not subtract mean -*/
165 piv_post_par
->subtract
= 0;
166 piv_post_par
->subtract__set
= TRUE
;
175 if (strcmp("-help", *argv
) == 0) {
176 printf("\n%s", argv
[0]);
177 printf("\n%s", HELP
);
178 printf("\n%s", USAGE
);
180 } else if (strcmp("-print", *argv
) == 0) {
182 } else if (strcmp("-version", *argv
) == 0) {
184 printf ("git hash: %s\n", GIT_REV
);
186 printf ("version: %s\n", GPIVTOOLS_VERSION
);
190 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
196 fprintf (stderr
,USAGE
);
198 printf ("\n%s",USAGE_DEBUG
);
207 * Check if filename or stdin /stdout is used
210 use_stdin_stdout
= FALSE
;
211 strcpy(fname
, argv
[argc
- 1]);
212 } else if (argc
== 0) {
213 use_stdin_stdout
= TRUE
;
217 printf ("\n%s", USAGE_DEBUG
);
219 gpiv_error("%s: %s", argv
[0], USAGE
);
226 make_fname (char *fname_in
,
227 char *fname_parameter
,
230 /* ----------------------------------------------------------------------------
231 * function to generate filenames
234 gchar
*err_msg
= NULL
;
235 gchar
*fname_base
= NULL
;
237 if (fname_in
== NULL
) {
238 err_msg
= "make_fname: \"fname_in == NULL\"";
245 fname_base
= g_strdup (fname_in
);
246 strtok (fname_base
, ".");
249 * filenames for output PIV data
251 gpiv_io_make_fname (fname_base
, GPIV_EXT_PAR
, fname_parameter
);
252 if (verbose
) printf ("# Data parameter file: %s\n", fname_parameter
);
254 gpiv_io_make_fname (fname_base
, GPIV_EXT_SA
, fname_out
);
255 if (verbose
) printf ("# Output file: %s\n", fname_out
);
266 /*-----------------------------------------------------------------------------
269 gchar
*err_msg
= NULL
;
270 FILE *fp_par_dat
, *fp
;
271 gchar fname_in
[GPIV_MAX_CHARS
],
272 fname_out
[GPIV_MAX_CHARS
],
273 fname_parameter
[GPIV_MAX_CHARS
];
275 GpivPivData
*piv_data
= NULL
;
276 GpivPostPar
*piv_post_par
= g_new (GpivPostPar
, 1);
279 gpiv_post_parameters_set (piv_post_par
, FALSE
);
280 command_args (argc
, argv
, fname_in
, piv_post_par
);
283 printf ("# Software: %s\n# git hash: %s\n# Command line options:\n",
286 printf ("# Software: %s\n# version: %s\n# Command line options:\n",
287 argv
[0], GPIVTOOLS_VERSION
);
289 gpiv_post_print_parameters (NULL
, piv_post_par
);
294 * Reading parametes from PARFILE and resources
295 * Check parameters on correct values and adjust belonging variables
297 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
299 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
300 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
301 gpiv_post_check_parameters_read (piv_post_par
, NULL
);
303 if (use_stdin_stdout
== FALSE
) {
305 make_fname (fname_in
, fname_parameter
, fname_out
))
307 gpiv_error ("%s: %s\n", argv
[0], err_msg
);
310 * Prints parameters to par-file
312 if ((fp_par_dat
= fopen (fname_parameter
, "a")) == NULL
) {
313 gpiv_error ("%s: failure opening %s for input\n", argv
[0],
316 fprintf (fp_par_dat
, "# %s\n# Post parameters:\n", argv
[0]);
317 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
322 * Reading input file of piv data
325 if (print_main
>= 1) fprintf (stderr
,"\ncalling (f)gpiv_read_pivdata\n");
328 if (use_stdin_stdout
== TRUE
) {
331 if ((fp
= fopen (fname_in
, "rb")) == NULL
) {
332 gpiv_error ("%s: failure opening %s for reading",
337 if ((piv_data
= gpiv_read_pivdata (fp
)) == NULL
) {
338 gpiv_error ("%s: %s", argv
[0], err_msg
);
340 if (use_stdin_stdout
== FALSE
) fclose (fp
);
343 * Here the function call of the program body
345 if (verbose
== TRUE
) printf ("\n");
348 g_message ("\ncalling gpiv_post_savg\n");
352 if ((err_msg
= gpiv_post_savg (piv_data
, piv_post_par
)) != NULL
) {
353 gpiv_error ("%s: ", argv
[0], err_msg
);
357 * Adding comment to the data
358 * And writing data to output
360 g_free (piv_data
->comment
);
362 piv_data
->comment
= g_strdup_printf ("# Software: %s\n# git hash: %s\n",
365 piv_data
->comment
= g_strdup_printf ("# Software: %s\n# version: %s\n",
366 argv
[0], GPIVTOOLS_VERSION
);
368 piv_data
->comment
= gpiv_add_datetime_to_comment (piv_data
->comment
);
370 if (piv_post_par
->subtract
== TRUE
) {
371 if (use_stdin_stdout
== TRUE
) {
374 if ((fp
= fopen (fname_out
, "wb")) == NULL
) {
375 gpiv_error ("%s: failure opening %s for writing",
380 if ((err_msg
= gpiv_write_pivdata (fp
, piv_data
, FALSE
)) != NULL
) {
381 gpiv_error ("%s: %s", argv
[0], err_msg
);
383 if (use_stdin_stdout
== FALSE
) fclose (fp
);
387 if (use_stdin_stdout
== TRUE
) {
389 printf ("# Software: %s\n# git hash: %s\n",
392 printf ("# Software: %s\n# version: %s\n",
393 argv
[0], GPIVTOOLS_VERSION
);
395 printf ("# %s\n", gpiv_add_datetime_to_comment (NULL
));
396 printf ("# ndata = %d\n", piv_data
->count
);
397 printf ("# mean_dx = %f sdev_dx = %f min_dx = %f max_dx = %f\n",
402 printf ("# mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n",
409 if ((fp
= fopen (fname_out
,"wb")) == NULL
) {
410 gpiv_error ("\n%s: Failure opening %s for output\n",
415 fprintf (fp
, "# Software: %s\n# git hash: %s\n",
418 fprintf (fp
, "# Software: %s\n# version: %s\n",
419 argv
[0], GPIVTOOLS_VERSION
);
421 fprintf (fp
, "# %s\n", gpiv_add_datetime_to_comment (NULL
));
422 fprintf (fp
, "# ndata = %d\n", piv_data
->count
);
423 fprintf (fp
, "# mean_dx = %f sdev_dx = %f min_dx = %f max_dx = %f\n",
428 fprintf (fp
, "# mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n",
438 * Freeing allocated memory of matrices
440 gpiv_free_pivdata (piv_data
);
441 if (verbose
== TRUE
) printf ("\n");