updated copyright statement
[gpivtools.git] / src / post / scale.c
blobc02919175fd9305f0fd174b0b7eb3d9688b86fd3
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)
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.
24 ------------------------------------------------------------------------*/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <glib.h>
29 #include <gpiv.h>
31 /* #define PARFILE "scale.par" */ /* Parameter file name */
32 #define PARFILE "gpivrc" /* Parameter file name */
33 #define USAGE "\
34 Usage: scale [ -h | --help] [-p | --print] [-s M | -S M] \n\
35 [-t dt | -T dt] [-v | --version] [-z px0 py0] [filename] \n\
36 < stdin > stdout \n\
37 \n\
38 keys: \n\
39 -h | --help: this on-line help \n\
40 -p | --print: print parameters to stdout \n\
41 -s M: spatial scaling with magnifation factor M [mm/px] \n\
42 -S M: inverse spatial scaling \n\
43 -t dt: time scaling with dt time between subsequent \n\
44 recordings [s] \n\
45 -T dt: inverse time scaling \n\
46 -v | --version: version number \n\
47 -z px0 py0: zero offset of image position \n\
48 filename: input PIV file. Substitutes stdin and stdout \n\
51 #ifdef DEBUG
52 #define USAGE_DEBUG "\
53 Developers version also contains: \n\
54 [-p_main] \n\
55 keys: \n\
56 -p_'function' N; prints data to be generated in the function; the \n\
57 higher N, the more detailed the output. \n\
58 For N = 10, err_vec will exit at the end of the function"
59 #endif
62 #define HELP "\
63 scale applies spatial and time scaling to PIV data"
65 #define RCSID "$Id: scale.c,v 2.14 2008-09-25 13:08:35 gerber Exp $"
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;
79 #endif
82 static void
83 command_args(int argc,
84 char *argv[],
85 char fname[GPIV_MAX_CHARS],
86 GpivImagePar *image_par,
87 GpivPostPar *post_par
89 /* ----------------------------------------------------------------------------
90 * Command line argument handling
93 char c;
94 int argc_next;
97 while (--argc > 0 && (*++argv)[0] == '-') {
98 argc_next=0;
100 * argc_next is set to 1 if the next cmd line argument has to be searched for;
101 * in case that the command line argument concerns more than one char or cmd
102 * line argument needs a parameter
104 while (argc_next == 0 && (c = *++argv[0])) {
105 switch (c) {
107 case 'v':
109 * Use Revision Control System (RCS) for version
111 printf("%s\n", RCSID);
112 exit(0);
113 break;
115 case 'h':
116 printf("%s\n", argv[0]);
117 printf("%s\n",HELP);
118 printf("%s\n",USAGE);
119 #ifdef DEBUG
120 printf ("\n%s\n",USAGE_DEBUG);
121 #endif
122 exit(0);
123 break;
126 * Spatial scaling
128 case 's':
129 image_par->s_scale = atof(*++argv);
130 image_par->s_scale__set = TRUE;
131 post_par->scale_type = GPIV_SCALE;
132 argc_next = 1;
133 --argc;
134 break;
137 * Inverse spatial scaling
139 case 'S':
140 image_par->s_scale = atof(*++argv);
141 image_par->s_scale__set = TRUE;
142 post_par->scale_type = GPIV_SCALE_INV;
143 argc_next = 1;
144 --argc;
145 break;
148 * Time scaling
150 case 't':
151 image_par->t_scale = atof(*++argv);
152 image_par->t_scale__set = TRUE;
153 post_par->scale_type = GPIV_SCALE;
154 argc_next = 1;
155 --argc;
156 break;
159 * Inverse time scaling
161 case 'T':
162 image_par->t_scale = atof(*++argv);
163 image_par->t_scale__set = TRUE;
164 post_par->scale_type = GPIV_SCALE_INV;
165 argc_next = 1;
166 --argc;
167 break;
170 * Zero-offset x0 y0
172 case 'z':
173 image_par->z_off_x = atof(*++argv);
174 image_par->z_off_y = atof(*++argv);
175 image_par->z_off_x__set = TRUE;
176 image_par->z_off_y__set = TRUE;
177 argc_next = 1;
178 --argc;
179 --argc;
180 break;
182 case 'p':
183 #ifdef DEBUG
184 if ((strcmp(*argv,"p_main" ) !=0))
186 #endif
187 verbose = TRUE;
188 #ifdef DEBUG
189 } else if (strcmp("p_main",*argv) == 0) {
190 print_main = atoi(*++argv);
191 --argc;
192 argc_next = 1;
194 #endif /* DEBUG */
195 break;
197 * long option keys
199 case '-':
200 if (strcmp("-help", *argv) == 0) {
201 printf("\n%s", argv[0]);
202 printf("\n%s", HELP);
203 printf("\n%s", USAGE);
204 exit(0);
205 } else if (strcmp("-print", *argv) == 0) {
206 verbose = TRUE;
207 } else if (strcmp("-version", *argv) == 0) {
208 printf("%s\n", RCSID);
209 exit(0);
210 } else {
211 gpiv_error("%s: unknown option: %s", argv[0], *argv);
213 argc_next = 1;
214 break;
216 default:
217 fprintf (stderr,USAGE);
218 #ifdef DEBUG
219 printf ("\n%s",USAGE_DEBUG);
220 #endif
221 exit(1);
222 break;
228 * Check if filename or stdin /stdout is used
230 if (argc == 1) {
231 use_stdin_stdout = FALSE;
232 strcpy (fname, argv[argc - 1]);
233 } else if (argc == 0) {
234 use_stdin_stdout = TRUE;
235 verbose = FALSE;
236 } else {
237 #ifdef DEBUG
238 printf ("\n%s", USAGE_DEBUG);
239 #endif
240 gpiv_error("%s: %s", argv[0], USAGE);
248 static gchar *
249 make_fname(char *fname_in,
250 char *fname_header,
251 char *fname_parameter,
252 char *fname_out
254 /* ---------------------------------------------------------------------------
255 * function to generate filenames
258 gchar *err_msg = NULL;
259 gchar *fname_base = NULL;
261 if (fname_in == NULL ) {
262 err_msg = "make_fname: \"fname_in == NULL\"";
263 return (err_msg);
267 * Stripping filename
269 fname_base = g_strdup(fname_in);
270 strtok(fname_base, ".");
273 * filenames for output PIV data
275 gpiv_io_make_fname (fname_base, GPIV_EXT_HEADER, fname_header);
276 if (verbose) printf ("# Image header file: %s\n", fname_header);
278 gpiv_io_make_fname (fname_base, GPIV_EXT_PAR, fname_parameter);
279 if (verbose) printf ("# Parameter file: %s\n", fname_parameter);
281 gpiv_io_make_fname (fname_base, GPIV_EXT_SC, fname_out);
282 if (verbose) printf ("# Output file: %s\n", fname_out);
284 g_free (fname_base);
285 return (err_msg);
291 main (int argc,
292 char *argv[]
294 /* ----------------------------------------------------------------------------
295 * Start of the main program
298 char * err_msg = NULL;
299 FILE *fp_par_dat, *fp;
300 char fname_in[GPIV_MAX_CHARS], fname_out[GPIV_MAX_CHARS],
301 fname_header[GPIV_MAX_CHARS], fname_parameter[GPIV_MAX_CHARS];
303 gboolean var_scale = 0;
305 GpivPivData *piv_data = NULL;
306 GpivImagePar *image_par = g_new (GpivImagePar, 1);
307 GpivPostPar *post_par = g_new (GpivPostPar, 1);
310 gpiv_img_parameters_set(image_par, FALSE);
311 gpiv_post_parameters_set(post_par, FALSE);
313 command_args(argc, argv, fname_in, image_par, post_par);
314 if (verbose) {
315 printf("# %s\n# Command line options:\n", RCSID);
316 gpiv_img_print_parameters (NULL, image_par);
317 gpiv_post_print_parameters (NULL, post_par);
320 gpiv_scan_parameter(GPIV_POSTPAR_KEY, PARFILE, post_par, verbose);
321 if ((err_msg =
322 gpiv_scan_resourcefiles(GPIV_POSTPAR_KEY, post_par, verbose))
323 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
325 if (use_stdin_stdout == FALSE) {
327 * Generating proper filenames
329 if ((err_msg =
330 make_fname (fname_in, fname_header, fname_parameter, fname_out))
331 != NULL) {
332 gpiv_error("%s: Failure calling make_fname", argv[0]);
336 * Prints command line parameters to par-file
338 if ((fp_par_dat = fopen (fname_parameter, "a")) == NULL) {
339 gpiv_error("\n%s: failure opening %s for input",
340 argv[0], fname_parameter);
342 fprintf(fp_par_dat, "\n\n# %s\n# Command line options:\n", argv[0]);
343 gpiv_img_print_parameters(fp_par_dat, image_par);
346 * Reading parametes from image header and PARFILE (and writing to data
347 * par-file)
349 gpiv_scan_parameter ("", fname_header, image_par, verbose);
350 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
351 verbose);
352 if ((err_msg =
353 gpiv_scan_resourcefiles(GPIV_IMGPAR_KEY, image_par,
354 verbose))
355 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
356 gpiv_img_print_parameters (fp_par_dat, image_par);
357 gpiv_post_print_parameters (fp_par_dat, post_par);
359 fclose(fp_par_dat);
362 } else {
363 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
364 verbose);
365 if ((err_msg =
366 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par,
367 verbose))
368 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
373 * Reading input data
375 if (use_stdin_stdout == TRUE) {
376 fp = stdin;
377 } else {
378 if ((fp = fopen (fname_in, "rb")) == NULL) {
379 gpiv_error ("%s: failure opening %s for reading",
380 argv[0], fname_in);
384 if ((piv_data = gpiv_read_pivdata (fp)) == NULL) {
385 gpiv_error ("%s: %s", argv[0], err_msg);
387 if (use_stdin_stdout == FALSE) fclose (fp);
391 * Here the function calls of the post-processing; scaling of PIV data
393 if (verbose == TRUE) printf("\n");
395 if (post_par->scale_type == GPIV_SCALE) {
396 if ((err_msg = gpiv_post_scale (piv_data, image_par)) != NULL) {
397 gpiv_error("%s: Failure calling gpiv_post_scale", argv[0]);
399 var_scale = TRUE;
400 } else if (post_par->scale_type == GPIV_SCALE_INV) {
401 if ((err_msg = gpiv_post_inverse_scale (piv_data, image_par)) != NULL) {
402 gpiv_error("%s: Failure calling gpiv_post_inverse_scale", argv[0]);
404 var_scale = TRUE;
405 } else {
406 gpiv_error("scale: used unexisting image_par.s_scale__set");
410 * Adding comment to the data
411 * And writing data to output
413 g_free (piv_data->comment);
414 piv_data->comment = g_strdup_printf ("# Software: %s\n", RCSID);
415 piv_data->comment = gpiv_add_datetime_to_comment (piv_data->comment);
417 if (use_stdin_stdout == TRUE) {
418 fp = stdout;
419 } else {
420 if ((fp = fopen (fname_out, "wb")) == NULL) {
421 gpiv_error ("%s: Failure opening %s for output",
422 argv[0], fname_out);
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");
434 exit (0);