show git hash (if available) when invoking -v key
[gpivtools.git] / src / validate / peaklck.c
blob4928c994aac9ad12e9d6e91ccd81f6fc0fadec0b
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /* --------------------------------------------------------------------
5 peaklck - test on peak-locking effects in PIV analyses.
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.
25 ---------------------------------------------------------------------*/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <glib.h>
30 #include <gpiv.h>
31 #include "config.h"
32 #include "git-rev.h"
34 /* #define PARFILE "peaklck.par" */ /* Parameter file name */
35 #define PARFILE "gpivrc" /* Parameter file name */
37 #define HELP "Optains histograms from PIV displacements: \n\
38 Peaklck tests PIV data on the so-called peak-locking effect \n\
39 by printing/displaying statistics of the particle displacements at \n\
40 sub-pixel level. Uhisto and vhisto output histograms of horizontal \n\
41 and vertical displacements"
43 #define USAGE "\
44 Usage: peaklck | uhisto | vhisto \n\
45 [-n N] [-g] [-h | --help] [-n] [-p | --print] \n\
46 [-v | --version] [filename] < stdin > stdout \n\
47 keys: \n\
48 -n N: override number of bins \n\
49 -g: graphical visualization of with gnuplot \n\
50 -h | --help: this on-line help \n\
51 -p | --print: print parameters and other info to stdout \n\
52 -v | --version: shows version number and exits successfully \n\
53 filename: input PIV file. Substitutes stdin and stdout \n\
58 * General global parameters
60 static gboolean print_par = FALSE;
61 static gboolean use_stdin_stdout = FALSE;
62 static gint operator = 10;
64 #define GNUPLOT_DISPLAY_COLOR "DarkRed"
65 #define GNUPLOT_DISPLAY_SIZE 250
67 int gnuplot = 0, gnuplot__set = FALSE;
71 void command_args (int argc, char *argv[],
72 char fname[GPIV_MAX_CHARS],
73 GpivValidPar *piv_valid_par,
74 gint *nbins,
75 gboolean *nbins__set
77 /*-----------------------------------------------------------------------------
78 * Command line argument handling
81 char c;
82 int argc_next;
85 while (--argc > 0 && (*++argv)[0] == '-') {
86 argc_next = 0;
87 while ((argc_next == 0) && (c = *++argv[0])) {
88 switch (c) {
89 case 'n': /* default: nbins=10 */
90 *nbins = atoi (*++argv);
91 *nbins__set = TRUE;
92 argc_next = 1;
93 --argc;
94 break;
96 case 'g': /* graphic output with gnuplot */
97 gnuplot = 1;
98 gnuplot__set = TRUE;
99 break;
101 case 'h':
102 printf ("%s\n", argv[0]);
103 printf ("%s\n", HELP);
104 printf ("%s\n", USAGE);
105 exit (1);
107 case 'p':
108 print_par = TRUE;
109 break;
111 case 'v':
112 #ifdef GIT_HASH
113 printf ("git hash: %s\n", GIT_REV);
114 #else
115 printf ("version: %s\n", GPIVTOOLS_VERSION);
116 #endif
117 exit (1);
118 break;
120 * long option keys
122 case '-':
123 if (strcmp ("-help", *argv) == 0) {
124 printf ("\n%s", argv[0]);
125 printf ("\n%s", HELP);
126 printf ("\n%s", USAGE);
127 exit (0);
129 } else if (strcmp ("-print", *argv) == 0) {
130 print_par = TRUE;
132 } else if (strcmp ("-version", *argv) == 0) {
133 #ifdef GIT_HASH
134 printf ("git hash: %s\n", GIT_REV);
135 #else
136 printf ("version: %s\n", GPIVTOOLS_VERSION);
137 #endif
138 exit (0);
140 } else {
141 gpiv_error ("%s: unknown option: %s", argv[0], *argv);
144 argc_next = 1;
145 break;
147 default:
148 gpiv_error ("%s: unknown option: %s", argv[0], *argv);
149 break;
154 * Check if filename or stdin /stdout is used
156 if (argc == 1) {
157 use_stdin_stdout = FALSE;
158 strcpy (fname, argv[argc - 1]);
159 } else if (argc == 0) {
160 use_stdin_stdout = TRUE;
161 print_par = FALSE;
162 } else {
163 gpiv_error ("%s: unknown argument: %s", argv[0], *argv);
170 static gchar *
171 make_fname (char *fname_in,
172 char *fname_out,
173 char *fname_parameter
175 /*-----------------------------------------------------------------------------
176 * generates filenames
179 gchar *err_msg = NULL;
180 gchar *fname_base = NULL;
182 if (fname_in == NULL ) {
183 err_msg = "make_fname: \"fname_in == NULL\"";
184 return (err_msg);
188 * Stripping filename
190 fname_base = g_strdup (fname_in);
191 strtok (fname_base, ".");
194 * filename for output
196 if (operator == GPIV_U || operator == GPIV_V) {
197 gpiv_io_make_fname (fname_base, GPIV_EXT_UVHISTO, fname_out);
198 } else {
199 gpiv_io_make_fname (fname_base, GPIV_EXT_PLK, fname_out);
201 if (print_par) printf ("# Output file: %s\n", fname_out);
203 gpiv_io_make_fname (fname_base, GPIV_EXT_PAR, fname_parameter);
204 if (print_par) printf ("# Parameter file: %s\n", fname_parameter);
206 g_free (fname_base);
207 return (err_msg);
212 int main (int argc, char *argv[])
213 /*-----------------------------------------------------------------------------
214 * main routine to tests PIV data on peak-locking effects
217 gchar *err_msg = NULL, *c = NULL;
218 FILE *fp, *fp_par_dat;
219 gchar fname_in[GPIV_MAX_CHARS],
220 fname_parameter[GPIV_MAX_CHARS],
221 fname_out[GPIV_MAX_CHARS];
223 gint nbins = 0;
224 gboolean nbins__set = FALSE;
226 GpivPivData *in_data = NULL;
227 GpivBinData *klass = NULL;
229 /* BUGFIX: made as pointers! */
230 GpivValidPar *valid_par = g_new0 (GpivValidPar, 1);
233 if ((c = strstr (argv[0], "peaklck")) != NULL) {
234 /* operator = PEAKLCK; */
235 } else if ((c = strstr (argv[0], "uhisto")) != NULL) {
236 operator = GPIV_U;
237 } else if ((c = strstr (argv[0], "vhisto")) != NULL) {
238 operator = GPIV_V;
239 } else {
240 gpiv_error ("%s: unvalid program name or symlink", argv[0]);
244 * Setting parameters
246 gpiv_valid_parameters_set (valid_par, FALSE);
248 command_args (argc, argv, fname_in, valid_par, &nbins, &nbins__set);
249 if (print_par) {
250 printf ("# %s\n# Command line options:\n", argv[0]);
251 gpiv_valid_print_parameters (NULL, valid_par);
254 if (nbins__set == FALSE) nbins = GPIV_NBINS_DEFAULT;
257 * Reading parametes from PARFILE and resources
259 gpiv_scan_parameter (GPIV_VALIDPAR_KEY, PARFILE, valid_par,
260 print_par);
261 if ((err_msg =
262 gpiv_scan_resourcefiles (GPIV_VALIDPAR_KEY, valid_par,
263 print_par))
264 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
267 if (use_stdin_stdout == FALSE) {
270 * Generating filenames
272 if ((err_msg =
273 make_fname (fname_in, fname_out, fname_parameter))
274 != NULL) {
275 gpiv_error ("%s: %s\n", argv[0], err_msg);
279 * Prints command line parameters to stdout and par-file
281 if ((fp_par_dat = fopen (fname_parameter, "a")) == NULL) {
282 gpiv_error ("%s: failure fopen %s",
283 argv[0], fname_parameter);
285 #ifdef GIT_HASH
286 fprintf (fp_par_dat, "\n\n# %s\n# Command line options:\n", GIT_REV);
287 #else
288 fprintf (fp_par_dat, "\n\n# %s\n# Command line options:\n", GPIVTOOLS_VERSION);
289 #endif
290 gpiv_valid_print_parameters (fp_par_dat, valid_par);
291 fclose (fp_par_dat);
294 * Writing parametes to data par-file
296 gpiv_valid_print_parameters (NULL, valid_par);
299 gpiv_valid_check_parameters_read (valid_par, NULL);
302 * Reading input data
304 if (print_par) g_message ("nbins = %d", nbins);
306 if (use_stdin_stdout == FALSE) {
307 if ((fp = fopen (fname_in, "rb")) == NULL) {
308 gpiv_error ("%s: failure fopen %s", argv[0], fname_in);
310 if ((in_data = gpiv_read_pivdata (fp)) == NULL) {
311 gpiv_error ("%s: %s", argv[0], err_msg);
313 fclose (fp);
315 } else {
316 if ((in_data = gpiv_read_pivdata (NULL)) == NULL) {
317 gpiv_error ("%s: %s", argv[0], err_msg);
322 * Calling program function and saving results
324 if (operator == GPIV_U || operator == GPIV_V) {
325 klass = gpiv_post_uvhisto (in_data, nbins, operator);
326 } else {
327 if ((klass = gpiv_valid_peaklck (in_data, nbins)) == NULL) {
328 gpiv_error ("%s: gpiv_valid_peaklck__ni failed", argv[0]);
332 if (use_stdin_stdout == FALSE) {
333 if ((fp = fopen (fname_out, "wb")) == NULL) {
334 gpiv_error ("%s: failure fopen %s", argv[0], fname_out);
336 gpiv_print_histo (fp, klass, FALSE);
337 fclose (fp);
338 } else {
339 gpiv_print_histo (NULL, klass, FALSE);
343 * Graphical output with gnuplot
345 if (gnuplot && use_stdin_stdout == FALSE) {
346 gchar title[GPIV_MAX_CHARS];
348 if (operator == GPIV_U ) {
349 snprintf (title, GPIV_MAX_CHARS, "Histogram U-velocities of %s", fname_out);
350 } else if (operator == GPIV_V ) {
351 snprintf (title, GPIV_MAX_CHARS, "Histogram V-velocities of %s", fname_out);
352 } else {
353 snprintf (title, GPIV_MAX_CHARS, "Peak locking statistics of %s", fname_out);
355 if ((err_msg =
356 gpiv_histo_gnuplot (fname_out, title, GNUPLOT_DISPLAY_COLOR,
357 GNUPLOT_DISPLAY_SIZE))
358 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
363 * Freeing allocated memory of matrices
365 gpiv_free_pivdata (in_data);
366 gpiv_free_bindata (klass);
368 exit (0);