1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
5 scale - applies spatial and time scale to PIV data
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.
24 ------------------------------------------------------------------------*/
33 /* #define PARFILE "scale.par" */ /* Parameter file name */
34 #define PARFILE "gpivrc" /* Parameter file name */
36 Usage: scale [ -h | --help] [-p | --print] [-s M | -S M] \n\
37 [-t dt | -T dt] [-v | --version] [-z px0 py0] [filename] \n\
41 -h | --help: this on-line help \n\
42 -p | --print: print parameters to stdout \n\
43 -s M: spatial scaling with magnifation factor M [mm/px] \n\
44 -i apply inverse spatial \n\
45 -t dt: time scaling with dt time between subsequent \n\
47 -v | --version: version number \n\
48 -z px0 py0: zero offset of image position [m] \n\
49 filename: input PIV file. Substitutes stdin and stdout \n\
53 #define USAGE_DEBUG "\
54 Developers version also contains: \n\
57 -p_'function' N; prints data to be generated in the function; the \n\
58 higher N, the more detailed the output. \n\
59 For N = 10, err_vec will exit at the end of the function"
64 scale applies spatial and time scaling to PIV data"
69 gboolean use_stdin_stdout
= FALSE
;
70 gboolean verbose
= FALSE
;
74 * Variables for development version
82 command_args(int argc
,
84 char fname
[GPIV_MAX_CHARS
],
85 GpivImagePar
*image_par
,
88 /* ----------------------------------------------------------------------------
89 * Command line argument handling
96 while (--argc
> 0 && (*++argv
)[0] == '-') {
99 * argc_next is set to 1 if the next cmd line argument has to be searched for;
100 * in case that the command line argument concerns more than one char or cmd
101 * line argument needs a parameter
103 while (argc_next
== 0 && (c
= *++argv
[0])) {
108 * Use Revision Control System (RCS) for version
111 printf ("git hash: %s\n", GIT_REV
);
113 printf ("version: %s\n", GPIVTOOLS_VERSION
);
119 printf("%s\n", argv
[0]);
121 printf("%s\n",USAGE
);
123 printf ("\n%s\n",USAGE_DEBUG
);
132 image_par
->s_scale
= atof(*++argv
);
133 image_par
->s_scale__set
= TRUE
;
134 post_par
->scale_type
= GPIV_SCALE
;
140 * Enable inverse scaling
143 post_par
->scale_type
= GPIV_SCALE_INV
;
150 image_par
->t_scale
= atof(*++argv
);
151 image_par
->t_scale__set
= TRUE
;
152 post_par
->scale_type
= GPIV_SCALE
;
161 image_par
->z_off_x
= atof(*++argv
);
162 image_par
->z_off_y
= atof(*++argv
);
163 image_par
->z_off_x__set
= TRUE
;
164 image_par
->z_off_y__set
= TRUE
;
172 if ((strcmp(*argv
,"p_main" ) !=0))
177 } else if (strcmp("p_main",*argv
) == 0) {
178 print_main
= atoi(*++argv
);
188 if (strcmp("-help", *argv
) == 0) {
189 printf("\n%s", argv
[0]);
190 printf("\n%s", HELP
);
191 printf("\n%s", USAGE
);
193 } else if (strcmp("-print", *argv
) == 0) {
195 } else if (strcmp("-version", *argv
) == 0) {
197 printf ("git hash: %s\n", GIT_REV
);
199 printf ("version: %s\n", GPIVTOOLS_VERSION
);
203 gpiv_error("%s: unknown option: %s", argv
[0], *argv
);
209 fprintf (stderr
,USAGE
);
211 printf ("\n%s",USAGE_DEBUG
);
220 * Check if filename or stdin /stdout is used
223 use_stdin_stdout
= FALSE
;
224 strcpy (fname
, argv
[argc
- 1]);
225 } else if (argc
== 0) {
226 use_stdin_stdout
= TRUE
;
230 printf ("\n%s", USAGE_DEBUG
);
232 gpiv_error("%s: %s", argv
[0], USAGE
);
241 make_fname(char *fname_in
,
243 char *fname_parameter
,
246 /* ---------------------------------------------------------------------------
247 * function to generate filenames
250 gchar
*err_msg
= NULL
;
251 gchar
*fname_base
= NULL
;
253 if (fname_in
== NULL
) {
254 err_msg
= "make_fname: \"fname_in == NULL\"";
261 fname_base
= g_strdup(fname_in
);
262 strtok(fname_base
, ".");
265 * filenames for output PIV data
267 gpiv_io_make_fname (fname_base
, GPIV_EXT_HEADER
, fname_header
);
268 if (verbose
) printf ("# Image header file: %s\n", fname_header
);
270 gpiv_io_make_fname (fname_base
, GPIV_EXT_PAR
, fname_parameter
);
271 if (verbose
) printf ("# Parameter file: %s\n", fname_parameter
);
273 gpiv_io_make_fname (fname_base
, GPIV_EXT_SC
, fname_out
);
274 if (verbose
) printf ("# Output file: %s\n", fname_out
);
286 /* ----------------------------------------------------------------------------
287 * Start of the main program
290 char * err_msg
= NULL
;
291 FILE *fp_par_dat
, *fp
;
292 char fname_in
[GPIV_MAX_CHARS
], fname_out
[GPIV_MAX_CHARS
],
293 fname_header
[GPIV_MAX_CHARS
], fname_parameter
[GPIV_MAX_CHARS
];
295 gboolean var_scale
= 0;
297 GpivPivData
*piv_data
= NULL
;
298 GpivImagePar
*image_par
= g_new (GpivImagePar
, 1);
299 GpivPostPar
*post_par
= g_new (GpivPostPar
, 1);
302 gpiv_img_parameters_set(image_par
, FALSE
);
303 gpiv_post_parameters_set(post_par
, FALSE
);
305 command_args(argc
, argv
, fname_in
, image_par
, post_par
);
308 printf("# %s\n# Command line options:\n", GIT_REV
);
310 printf("# %s\n# Command line options:\n", GPIVTOOLS_VERSION
);
312 gpiv_img_print_parameters (NULL
, image_par
);
313 gpiv_post_print_parameters (NULL
, post_par
);
316 gpiv_scan_parameter(GPIV_POSTPAR_KEY
, PARFILE
, post_par
, verbose
);
318 gpiv_scan_resourcefiles(GPIV_POSTPAR_KEY
, post_par
, verbose
))
319 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
321 if (use_stdin_stdout
== FALSE
) {
323 * Generating proper filenames
326 make_fname (fname_in
, fname_header
, fname_parameter
, fname_out
))
328 gpiv_error("%s: Failure calling make_fname", argv
[0]);
332 * Prints command line parameters to par-file
334 if ((fp_par_dat
= fopen (fname_parameter
, "a")) == NULL
) {
335 gpiv_error("\n%s: failure opening %s for input",
336 argv
[0], fname_parameter
);
338 fprintf(fp_par_dat
, "\n\n# %s\n# Command line options:\n", argv
[0]);
339 gpiv_img_print_parameters(fp_par_dat
, image_par
);
342 * Reading parametes from image header and PARFILE (and writing to data
345 gpiv_scan_parameter ("", fname_header
, image_par
, verbose
);
346 gpiv_scan_parameter (GPIV_IMGPAR_KEY
, PARFILE
, image_par
,
349 gpiv_scan_resourcefiles(GPIV_IMGPAR_KEY
, image_par
,
351 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
352 gpiv_img_print_parameters (fp_par_dat
, image_par
);
353 gpiv_post_print_parameters (fp_par_dat
, post_par
);
359 gpiv_scan_parameter (GPIV_IMGPAR_KEY
, PARFILE
, image_par
,
362 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY
, image_par
,
364 != NULL
) gpiv_error ("%s: %s", argv
[0], err_msg
);
371 if (use_stdin_stdout
== TRUE
) {
374 if ((fp
= fopen (fname_in
, "rb")) == NULL
) {
375 gpiv_error ("%s: failure opening %s for reading",
380 if ((piv_data
= gpiv_read_pivdata (fp
)) == NULL
) {
381 gpiv_error ("%s: %s", argv
[0], err_msg
);
383 if (use_stdin_stdout
== FALSE
) fclose (fp
);
387 * Here the function calls of the post-processing; scaling of PIV data
389 if (verbose
== TRUE
) printf("\n");
391 if (post_par
->scale_type
== GPIV_SCALE
) {
392 if ((err_msg
= gpiv_post_scale (piv_data
, image_par
)) != NULL
) {
393 gpiv_error("%s: Failure calling gpiv_post_scale", argv
[0]);
396 } else if (post_par
->scale_type
== GPIV_SCALE_INV
) {
397 if ((err_msg
= gpiv_post_inverse_scale (piv_data
, image_par
)) != NULL
) {
398 gpiv_error("%s: Failure calling gpiv_post_inverse_scale", argv
[0]);
402 gpiv_error("scale: used unexisting image_par.s_scale__set");
406 * Adding comment to the data
407 * And writing data to output
409 g_free (piv_data
->comment
);
411 piv_data
->comment
= g_strdup_printf ("# Software: %s\n", GIT_REV
);
413 piv_data
->comment
= g_strdup_printf ("# Software: %s\n", GPIVTOOLS_VERSION
);
415 piv_data
->comment
= gpiv_add_datetime_to_comment (piv_data
->comment
);
417 if (use_stdin_stdout
== TRUE
) {
420 if ((fp
= fopen (fname_out
, "wb")) == NULL
) {
421 gpiv_error ("%s: Failure opening %s for output",
426 if ((err_msg
= gpiv_write_pivdata (fp
, piv_data
, TRUE
)) != NULL
) {
427 gpiv_error ("%s: %s", argv
[0], err_msg
);
429 if (use_stdin_stdout
== FALSE
) fclose (fp
);
433 if (verbose
== TRUE
) printf("\n");