1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
5 vorstra - calculates differential quantities (like vorticity and
9 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
10 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2, or (at your option)
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 ----------------------------------------------------------------------------
29 vorty_z = dV/dx - dU/dy
30 sh_strain = dU/dy + dV/dx
31 n_strain = dU/dx + dV/dy
34 -----------------------------------------------------------------------------*/
42 /* #define PARFILE "vorstra.par" */ /* Parameter file name */
43 #define PARFILE "gpivrc" /* Parameter file name */
45 #define GNUPLOT_DISPLAY_COLOR "DarkBlue"
46 #define GNUPLOT_DISPLAY_SIZE 250
49 Usage: vorstra | vorty | nstrain | sstrain \n\
50 [-d int] [-g] [--no_g] [-h | --help] [-o] [-s] [-n] \n\
51 [-p | --print] [-v | --version] [filename] < stdin > stdout \n\
54 -d N: differential type; central (0), least squares \n\
55 (1), richardson (2), circulation method (3) \n\
56 -g: graphical visualization with gnuplot (needs -f) \n\
57 --no_g: suppresses graphical visualization \n\
58 -h | --help: this on-line help \n\
59 -n: normal strain analyses \n\
60 -o: vorticity analyses \n\
61 -p | --print: print parameters to stdout \n\
62 -s: shear strain analyses \n\
63 -v | --version; version number\n\
64 filename: input PIV data file,. Substitutes stdin and stdout \n\
68 #define USAGE_DEBUG "\
69 Developers version also contains: \n\
70 [-p_main][-p_differential] \n\
73 -p_'function' N: prints data to be generated in the function; the \n\
74 higher N, the more detailed the output. \n\
75 For N = 10, err_vec will exit at the end of the function"
80 vorstra calculates the differential quantities vorticity, shear strain and \n\
81 normal strain from PIV data."
83 #define RCSID "$Id: vorstra.c,v 2.16 2008-10-09 14:45:01 gerber Exp $"
86 gboolean use_stdin_stdout
= FALSE
;
87 gboolean verbose
= FALSE
;
90 gboolean gnuplot__set
= FALSE
;
94 * Parameters for development version
96 int print_main
= 0, print_differential
= 0;
102 command_args(int argc
,
104 char fname
[GPIV_MAX_CHARS
],
105 GpivPostPar
* piv_post_par
107 /* ----------------------------------------------------------------------------
108 * Command line argument handling
115 while (--argc
> 0 && (*++argv
)[0] == '-') {
119 * argc_next is set to 1 if the next cmd line argument has to be
120 * searched for; in case that the command line argument concerns more
121 * than one char or cmd line argument needs a parameter
123 while (argc_next
== 0 && (c
= *++argv
[0])) {
126 * Use Revision Control System (RCS) for version
129 printf("\n%s\n", RCSID
);
133 * differential operator type
136 piv_post_par
->diff_type__set
= TRUE
;
137 piv_post_par
->diff_type
= atoi(*++argv
);
143 * graphic output with gnuplot
150 printf("\n%s", argv
[0]);
151 printf("\n%s", HELP
);
152 printf("\n%s\n", USAGE
);
156 * normal strain analysis
159 piv_post_par
->operator_vorstra__set
= TRUE
;
160 piv_post_par
->operator_vorstra
= GPIV_N_STRAIN
;
167 piv_post_par
->operator_vorstra__set
= TRUE
;
168 piv_post_par
->operator_vorstra
= GPIV_VORTICITY
;
172 if (strcmp(*argv
, "p_main") != 0) {
176 } else if (strcmp("p_main", *argv
) == 0) {
177 print_main
= atoi(*++argv
);
184 * shear strain analysis
187 piv_post_par
->operator_vorstra__set
= TRUE
;
188 piv_post_par
->operator_vorstra
= GPIV_S_STRAIN
;
195 if (strcmp("-help", *argv
) == 0) {
196 printf("\n%s", argv
[0]);
197 printf("\n%s", HELP
);
198 printf("\n%s", USAGE
);
200 } else if (strcmp("-print", *argv
) == 0) {
202 } else if (strcmp("-version", *argv
) == 0) {
203 printf("%s\n", RCSID
);
207 * do not plot with gnuplot
209 } else if (strcmp("-no_g", *argv
) == 0) {
215 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
221 gpiv_error("\n%s error : unknown option: %s\n",
229 * Check if filename or stdin /stdout is used
232 use_stdin_stdout
= FALSE
;
233 strcpy(fname
, argv
[argc
- 1]);
234 } else if (argc
== 0) {
235 use_stdin_stdout
= TRUE
;
238 gpiv_error("\n%s error: unknown argument: %s\n", argv
[0], *argv
);
246 make_fname(char *fname_in
,
247 char *fname_parameter
,
249 GpivPostPar
*piv_post_par
251 /* ----------------------------------------------------------------------------
255 gchar
*err_msg
= NULL
;
256 gchar
*fname_base
= NULL
;
258 if (fname_in
== NULL
) {
259 err_msg
= "make_fname: \"fname_in == NULL\"";
266 fname_base
= g_strdup(fname_in
);
267 strtok(fname_base
, ".");
270 * filenames for output
272 gpiv_io_make_fname(fname_base
, GPIV_EXT_PAR
, fname_parameter
);
274 printf("# Parameter file: %s\n", fname_parameter
);
276 if (piv_post_par
->operator_vorstra
== GPIV_VORTICITY
) {
277 gpiv_io_make_fname (fname_base
, GPIV_EXT_VOR
, fname_out
);
278 } else if (piv_post_par
->operator_vorstra
== GPIV_S_STRAIN
) {
279 gpiv_io_make_fname (fname_base
, GPIV_EXT_SSTR
, fname_out
);
280 } else if (piv_post_par
->operator_vorstra
== GPIV_N_STRAIN
) {
281 gpiv_io_make_fname (fname_base
, GPIV_EXT_NSTR
, fname_out
);
283 gpiv_error("%s: non valid operation", RCSID
);
286 printf("# output data file: %s\n", fname_out
);
298 /*-----------------------------------------------------------------------------
301 char *err_msg
= NULL
, *c
= NULL
;
302 FILE *fp_par_dat
, *fp
;
303 gchar fname_in
[GPIV_MAX_CHARS
],
304 fname_out
[GPIV_MAX_CHARS
],
305 fname_parameter
[GPIV_MAX_CHARS
];
307 GpivPostPar
*piv_post_par
= g_new (GpivPostPar
, 1);;
308 GpivPivData
*in_data
= NULL
;
309 GpivScalarData
*out_data
= NULL
;
313 use_stdin_stdout
= FALSE
;
314 piv_post_par
->operator_vorstra__set
= FALSE
;
315 piv_post_par
->diff_type__set
= FALSE
;
318 * Define GpivOperation type from program name, which is a symbolic link to
321 if ((c
= strstr(argv
[0], "vorstra")) != NULL
) {
323 } else if ((c
= strstr(argv
[0], "vorty")) != NULL
) {
324 piv_post_par
->operator_vorstra
= GPIV_VORTICITY
;
325 piv_post_par
->operator_vorstra__set
= TRUE
;
327 } else if ((c
= strstr(argv
[0], "nstrain")) != NULL
) {
328 piv_post_par
->operator_vorstra
= GPIV_N_STRAIN
;
329 piv_post_par
->operator_vorstra__set
= TRUE
;
331 } else if ((c
= strstr(argv
[0], "sstrain")) != NULL
) {
332 piv_post_par
->operator_vorstra
= GPIV_S_STRAIN
;
333 piv_post_par
->operator_vorstra__set
= TRUE
;
336 gpiv_error("vorstra: unvalid program name or symlink");
340 command_args (argc
, argv
, fname_in
, piv_post_par
);
342 printf("# %s\n# Command line options:\n", RCSID
);
343 gpiv_post_print_parameters (NULL
, piv_post_par
);
347 if (use_stdin_stdout
== FALSE
) {
348 make_fname(fname_in
, fname_parameter
, fname_out
, piv_post_par
);
351 * Prints command line parameters to par-file
353 if ((fp_par_dat
= fopen(fname_parameter
, "a")) == NULL
)
354 gpiv_error("\n%s: failure opening %s for input",
355 argv
[0], fname_parameter
);
356 fprintf(fp_par_dat
, "# %s\n# Command line options:\n", argv
[0]);
357 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
360 * Reading parametes from PARFILE (and writing to data par-file)
362 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
364 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
365 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
366 gpiv_post_print_parameters (fp_par_dat
, piv_post_par
);
371 gpiv_scan_parameter (GPIV_POSTPAR_KEY
, PARFILE
, piv_post_par
, verbose
);
373 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY
, piv_post_par
, verbose
))
374 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
377 gpiv_post_check_parameters_read (piv_post_par
, NULL
);
381 * Check parameters on correct values and adjust belonging variables
383 if (piv_post_par
->diff_type__set
) {
384 if (piv_post_par
->diff_type
!= 0
385 && piv_post_par
->diff_type
!= 1
386 && piv_post_par
->diff_type
!= 2
387 && piv_post_par
->diff_type
!= 3)
388 gpiv_error("%s error: no value differential differentiator type",
392 if (use_stdin_stdout
== TRUE
&& gnuplot
== 1) {
393 gpiv_error ("%s: filename has to be used in combination with 'gnuplot'\n",
397 if ((piv_post_par
->operator_vorstra
== GPIV_S_STRAIN
398 || piv_post_par
->operator_vorstra
== GPIV_N_STRAIN
)
399 && piv_post_par
->diff_type
== GPIV_CIRCULATION
) {
400 gpiv_error ("\n%s error: strain can not be calculated by circulation method",
406 * Reading input PIV data
408 if (use_stdin_stdout
== TRUE
) {
411 if ((fp
= fopen (fname_in
, "rb")) == NULL
) {
412 gpiv_error ("%s: Failure opening %s for input", argv
[0], fname_in
);
416 if ((in_data
= gpiv_read_pivdata (fp
)) == NULL
) {
417 gpiv_error ("%s: %s", argv
[0], err_msg
);
419 if (use_stdin_stdout
== FALSE
) fclose (fp
);
423 * here the function calls of post-processing; calculating vorticity, strain
426 if ((out_data
= gpiv_post_vorstra (in_data
, piv_post_par
)) == NULL
) {
427 gpiv_error ("%s: %s", argv
[0], err_msg
);
431 * Adding comment to the data
432 * And writing to output
434 g_free (out_data
->comment
);
435 out_data
->comment
= g_strdup_printf ("# Software: %s\n", RCSID
);
436 out_data
->comment
= gpiv_add_datetime_to_comment (out_data
->comment
);
437 if (piv_post_par
->operator_vorstra
== GPIV_VORTICITY
) {
438 out_data
->comment
= g_strconcat (out_data
->comment
,
439 "\n# Data type: vorticity [1/s]\n",
441 } else if (piv_post_par
->operator_vorstra
== GPIV_N_STRAIN
) {
442 out_data
->comment
= g_strconcat (out_data
->comment
,
443 "\n# Data type: normal strain [1/s]\n",
445 } else if (piv_post_par
->operator_vorstra
== GPIV_S_STRAIN
) {
446 out_data
->comment
= g_strconcat (out_data
->comment
,
447 "\n# Data type: shear strain [1/s]\n",
452 if (use_stdin_stdout
== TRUE
) {
455 if ((fp
= fopen (fname_out
, "wb")) == NULL
) {
456 gpiv_error ("%s: Failure opening %s for output", argv
[0], fname_out
);
461 if ((err_msg
= gpiv_write_sc_griddata (fp
, out_data
, TRUE
)) != NULL
) {
462 gpiv_error ("%s: %s", argv
[0], err_msg
);
465 if ((err_msg
= gpiv_write_scdata (fp
, out_data
, TRUE
)) != NULL
) {
466 gpiv_error ("%s: %s", argv
[0], err_msg
);
469 if (use_stdin_stdout
== FALSE
) fclose (fp
);
473 * Freeing allocated memory of matrices
475 gpiv_free_pivdata(in_data
);
479 * Graphical output with gnuplot
482 gchar title
[GPIV_MAX_CHARS
];
484 snprintf (title
, GPIV_MAX_CHARS
, "vorticity of %s", fname_in
);
485 gpiv_scalar_gnuplot (fname_out
, title
, GNUPLOT_DISPLAY_COLOR
,
486 GNUPLOT_DISPLAY_SIZE
);