Merge ../gpivtools-omp
[gpivtools.git] / src / post / s-avg.c
blob8647522e9ddc911d207fafe150734d006518d5d5
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
4 Description:
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)
14 any later version.
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 ------------------------------------------------------------------------*/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <math.h>
30 #include <glib.h>
31 #include <gpiv.h>
32 #include "config.h"
33 #include "git-rev.h"
35 /* #define PARFILE "savg.par" */ /* Parameter file name */
36 #define PARFILE "gpivrc" /* Parameter file name */
37 #define USAGE "\
38 Usage: scale [-h | --help] [-p | --print] [-s|-no_s] \n\
39 [-v | --version] [-z dx dy] [filename] < stdin > stdout \n\
40 \n\
41 keys: \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\
53 #ifdef DEBUG
54 #define USAGE_DEBUG "\
55 Developers version also contains: \n\
56 [-p_main] \n\
57 keys: \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"
61 #endif
64 #define HELP "\
65 s-avg reports spatial global mean velocity and rms error of a PIV data stream"
68 * Global variables
70 gboolean use_stdin_stdout = FALSE;
71 gboolean verbose = FALSE;
73 #ifdef DEBUG
75 * Variables for development version
77 int print_main=0;
78 #endif
82 static void
83 command_args(int argc, char *argv[],
84 char fname[GPIV_MAX_CHARS],
85 GpivPostPar * piv_post_par
87 /* ----------------------------------------------------------------------------
88 * Command line argument handling
91 char c;
92 int argc_next;
95 while (--argc > 0 && (*++argv)[0] == '-') {
96 argc_next=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])) {
104 switch (c) {
106 case 'v':
108 * Use Git revision control system
110 #ifdef GIT_HASH
111 printf ("git hash: %s\n", GIT_REV);
112 #else
113 printf ("version: %s\n", GPIVTOOLS_VERSION);
114 #endif
115 exit(0);
117 case 'h':
118 printf("%s\n", argv[0]);
119 printf("%s\n",HELP);
120 printf("%s\n",USAGE);
121 #ifdef DEBUG
122 printf ("\n%s\n",USAGE_DEBUG);
123 #endif
124 exit(0);
126 * Spatial scaling
128 case 's':
129 piv_post_par->subtract = atoi(*++argv);
130 piv_post_par->subtract__set = TRUE;
131 argc_next = 1;
132 break;
134 case 'p':
135 #ifdef DEBUG
136 if ((strcmp(*argv,"p_main" ) !=0)) {
137 #endif
138 verbose = TRUE;
139 #ifdef DEBUG
140 } else if (strcmp("p_main",*argv) == 0) {
141 print_main=atoi(*++argv);
142 --argc;
143 argc_next=1;
145 #endif /* DEBUG */
146 break;
148 case 'z':
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;
155 argc_next = 1;
156 --argc;
157 --argc;
158 argc_next = 1;
159 break;
162 * negotion of settings
163 */ case 'n':
164 if (strcmp("no_s", *argv) == 0) { /* --- do not subtract mean -*/
165 piv_post_par->subtract = 0;
166 piv_post_par->subtract__set = TRUE;
168 break;
172 * long option keys
174 case '-':
175 if (strcmp("-help", *argv) == 0) {
176 printf("\n%s", argv[0]);
177 printf("\n%s", HELP);
178 printf("\n%s", USAGE);
179 exit(0);
180 } else if (strcmp("-print", *argv) == 0) {
181 verbose = TRUE;
182 } else if (strcmp("-version", *argv) == 0) {
183 #ifdef GIT_HASH
184 printf ("git hash: %s\n", GIT_REV);
185 #else
186 printf ("version: %s\n", GPIVTOOLS_VERSION);
187 #endif
188 exit(0);
189 } else {
190 gpiv_error("%s: unknown option: %s", argv[0], *argv);
192 argc_next = 1;
193 break;
195 default:
196 fprintf (stderr,USAGE);
197 #ifdef DEBUG
198 printf ("\n%s",USAGE_DEBUG);
199 #endif
200 exit(1);
201 break;
207 * Check if filename or stdin /stdout is used
209 if (argc == 1) {
210 use_stdin_stdout = FALSE;
211 strcpy(fname, argv[argc - 1]);
212 } else if (argc == 0) {
213 use_stdin_stdout = TRUE;
214 verbose = FALSE;
215 } else {
216 #ifdef DEBUG
217 printf ("\n%s", USAGE_DEBUG);
218 #endif
219 gpiv_error("%s: %s", argv[0], USAGE);
225 static gchar *
226 make_fname (char *fname_in,
227 char *fname_parameter,
228 char *fname_out
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\"";
239 return (err_msg);
243 * Stripping filename
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);
257 return (err_msg);
262 int
263 main (int argc,
264 char *argv[]
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);
281 if (verbose) {
282 #ifdef GIT_HASH
283 printf ("# Software: %s\n# git hash: %s\n# Command line options:\n",
284 argv[0], GIT_REV);
285 #else
286 printf ("# Software: %s\n# version: %s\n# Command line options:\n",
287 argv[0], GPIVTOOLS_VERSION);
288 #endif
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);
298 if ((err_msg =
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) {
304 if ((err_msg =
305 make_fname (fname_in, fname_parameter, fname_out))
306 != NULL) {
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],
314 fname_parameter);
316 fprintf (fp_par_dat, "# %s\n# Post parameters:\n", argv[0]);
317 gpiv_post_print_parameters (fp_par_dat, piv_post_par);
318 fclose (fp_par_dat);
322 * Reading input file of piv data
324 #ifdef DEBUG
325 if (print_main >= 1) fprintf (stderr,"\ncalling (f)gpiv_read_pivdata\n");
326 #endif
328 if (use_stdin_stdout == TRUE) {
329 fp = stdin;
330 } else {
331 if ((fp = fopen (fname_in, "rb")) == NULL) {
332 gpiv_error ("%s: failure opening %s for reading",
333 argv[0], fname_in);
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");
347 #ifdef DEBUG
348 g_message ("\ncalling gpiv_post_savg\n");
349 #endif
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);
361 #ifdef GIT_HASH
362 piv_data->comment = g_strdup_printf ("# Software: %s\n# git hash: %s\n",
363 argv[0], GIT_REV);
364 #else
365 piv_data->comment = g_strdup_printf ("# Software: %s\n# version: %s\n",
366 argv[0], GPIVTOOLS_VERSION);
367 #endif
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) {
372 fp = stdout;
373 } else {
374 if ((fp = fopen (fname_out, "wb")) == NULL) {
375 gpiv_error ("%s: failure opening %s for writing",
376 argv[0], fname_out);
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);
386 } else {
387 if (use_stdin_stdout == TRUE) {
388 #ifdef GIT_HASH
389 printf ("# Software: %s\n# git hash: %s\n",
390 argv[0], GIT_REV);
391 #else
392 printf ("# Software: %s\n# version: %s\n",
393 argv[0], GPIVTOOLS_VERSION);
394 #endif
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",
398 piv_data->mean_dx,
399 piv_data->sdev_dx,
400 piv_data->min_dx,
401 piv_data->max_dx);
402 printf ("# mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n",
403 piv_data->mean_dy,
404 piv_data->sdev_dy,
405 piv_data->min_dy,
406 piv_data->max_dy);
408 } else {
409 if ((fp = fopen (fname_out,"wb")) == NULL) {
410 gpiv_error ("\n%s: Failure opening %s for output\n",
411 argv[0], fname_out);
414 #ifdef GIT_HASH
415 fprintf (fp, "# Software: %s\n# git hash: %s\n",
416 argv[0], GIT_REV);
417 #else
418 fprintf (fp, "# Software: %s\n# version: %s\n",
419 argv[0], GPIVTOOLS_VERSION);
420 #endif
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",
424 piv_data->mean_dx,
425 piv_data->sdev_dx,
426 piv_data->min_dx,
427 piv_data->max_dx);
428 fprintf (fp, "# mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n",
429 piv_data->mean_dy,
430 piv_data->sdev_dy,
431 piv_data->min_dy,
432 piv_data->max_dy);
438 * Freeing allocated memory of matrices
440 gpiv_free_pivdata (piv_data);
441 if (verbose == TRUE) printf ("\n");
442 exit (0);