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)
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 ---------------------------------------------------------------------*/
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"
44 Usage: peaklck | uhisto | vhisto \n\
45 [-n N] [-g] [-h | --help] [-n] [-p | --print] \n\
46 [-v | --version] [filename] < stdin > stdout \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
,
77 /*-----------------------------------------------------------------------------
78 * Command line argument handling
85 while (--argc
> 0 && (*++argv
)[0] == '-') {
87 while ((argc_next
== 0) && (c
= *++argv
[0])) {
89 case 'n': /* default: nbins=10 */
90 *nbins
= atoi (*++argv
);
96 case 'g': /* graphic output with gnuplot */
102 printf ("%s\n", argv
[0]);
103 printf ("%s\n", HELP
);
104 printf ("%s\n", USAGE
);
113 printf ("git hash: %s\n", GIT_REV
);
115 printf ("version: %s\n", GPIVTOOLS_VERSION
);
123 if (strcmp ("-help", *argv
) == 0) {
124 printf ("\n%s", argv
[0]);
125 printf ("\n%s", HELP
);
126 printf ("\n%s", USAGE
);
129 } else if (strcmp ("-print", *argv
) == 0) {
132 } else if (strcmp ("-version", *argv
) == 0) {
134 printf ("git hash: %s\n", GIT_REV
);
136 printf ("version: %s\n", GPIVTOOLS_VERSION
);
141 gpiv_error ("%s: unknown option: %s", argv
[0], *argv
);
148 gpiv_error ("%s: unknown option: %s", argv
[0], *argv
);
154 * Check if filename or stdin /stdout is used
157 use_stdin_stdout
= FALSE
;
158 strcpy (fname
, argv
[argc
- 1]);
159 } else if (argc
== 0) {
160 use_stdin_stdout
= TRUE
;
163 gpiv_error ("%s: unknown argument: %s", argv
[0], *argv
);
171 make_fname (char *fname_in
,
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\"";
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
);
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
);
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
];
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
) {
237 } else if ((c
= strstr (argv
[0], "vhisto")) != NULL
) {
240 gpiv_error ("%s: unvalid program name or symlink", argv
[0]);
246 gpiv_valid_parameters_set (valid_par
, FALSE
);
248 command_args (argc
, argv
, fname_in
, valid_par
, &nbins
, &nbins__set
);
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
,
262 gpiv_scan_resourcefiles (GPIV_VALIDPAR_KEY
, valid_par
,
264 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
267 if (use_stdin_stdout
== FALSE
) {
270 * Generating filenames
273 make_fname (fname_in
, fname_out
, fname_parameter
))
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
);
286 fprintf (fp_par_dat
, "\n\n# %s\n# Command line options:\n", GIT_REV
);
288 fprintf (fp_par_dat
, "\n\n# %s\n# Command line options:\n", GPIVTOOLS_VERSION
);
290 gpiv_valid_print_parameters (fp_par_dat
, valid_par
);
294 * Writing parametes to data par-file
296 gpiv_valid_print_parameters (NULL
, valid_par
);
299 gpiv_valid_check_parameters_read (valid_par
, NULL
);
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
);
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);
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
);
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
);
353 snprintf (title
, GPIV_MAX_CHARS
, "Peak locking statistics of %s", fname_out
);
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
);