libgpiv so version 4: GpivFt. Show git hash (if available) in output and
[gpivtools.git] / src / post / scale.c
blobccadd7435ab16898a31d96b8869e1e043ebfc3fb
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>
30 #include "config.h"
31 #include "git-rev.h"
33 /* #define PARFILE "scale.par" */ /* Parameter file name */
34 #define PARFILE "gpivrc" /* Parameter file name */
35 #define USAGE "\
36 Usage: scale [ -h | --help] [-p | --print] [-s M | -S M] \n\
37 [-t dt | -T dt] [-v | --version] [-z px0 py0] [filename] \n\
38 < stdin > stdout \n\
39 \n\
40 keys: \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\
46 recordings [ms] \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\
52 #ifdef DEBUG
53 #define USAGE_DEBUG "\
54 Developers version also contains: \n\
55 [-p_main] \n\
56 keys: \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"
60 #endif
63 #define HELP "\
64 scale applies spatial and time scaling to PIV data"
67 * Global variables
69 gboolean use_stdin_stdout = FALSE;
70 gboolean verbose = FALSE;
72 #ifdef DEBUG
74 * Variables for development version
76 int print_main=0;
78 #endif
81 static void
82 command_args(int argc,
83 char *argv[],
84 char fname[GPIV_MAX_CHARS],
85 GpivImagePar *image_par,
86 GpivPostPar *post_par
88 /* ----------------------------------------------------------------------------
89 * Command line argument handling
92 char c;
93 int argc_next;
96 while (--argc > 0 && (*++argv)[0] == '-') {
97 argc_next=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])) {
104 switch (c) {
106 case 'v':
108 * Use Revision Control System (RCS) for version
110 #ifdef GIT_HASH
111 printf ("git hash: %s\n", GIT_REV);
112 #else
113 printf ("version: %s\n", GPIVTOOLS_VERSION);
114 #endif
115 exit(0);
116 break;
118 case 'h':
119 printf("%s\n", argv[0]);
120 printf("%s\n",HELP);
121 printf("%s\n",USAGE);
122 #ifdef DEBUG
123 printf ("\n%s\n",USAGE_DEBUG);
124 #endif
125 exit(0);
126 break;
129 * Spatial scaling
131 case 's':
132 image_par->s_scale = atof(*++argv);
133 image_par->s_scale__set = TRUE;
134 post_par->scale_type = GPIV_SCALE;
135 argc_next = 1;
136 --argc;
137 break;
140 * Enable inverse scaling
142 case 'i':
143 post_par->scale_type = GPIV_SCALE_INV;
144 break;
147 * Time scaling
149 case 't':
150 image_par->t_scale = atof(*++argv);
151 image_par->t_scale__set = TRUE;
152 post_par->scale_type = GPIV_SCALE;
153 argc_next = 1;
154 --argc;
155 break;
158 * Zero-offset x0 y0
160 case 'z':
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;
165 argc_next = 1;
166 --argc;
167 --argc;
168 break;
170 case 'p':
171 #ifdef DEBUG
172 if ((strcmp(*argv,"p_main" ) !=0))
174 #endif
175 verbose = TRUE;
176 #ifdef DEBUG
177 } else if (strcmp("p_main",*argv) == 0) {
178 print_main = atoi(*++argv);
179 --argc;
180 argc_next = 1;
182 #endif /* DEBUG */
183 break;
185 * long option keys
187 case '-':
188 if (strcmp("-help", *argv) == 0) {
189 printf("\n%s", argv[0]);
190 printf("\n%s", HELP);
191 printf("\n%s", USAGE);
192 exit(0);
193 } else if (strcmp("-print", *argv) == 0) {
194 verbose = TRUE;
195 } else if (strcmp("-version", *argv) == 0) {
196 #ifdef GIT_HASH
197 printf ("git hash: %s\n", GIT_REV);
198 #else
199 printf ("version: %s\n", GPIVTOOLS_VERSION);
200 #endif
201 exit(0);
202 } else {
203 gpiv_error("%s: unknown option: %s", argv[0], *argv);
205 argc_next = 1;
206 break;
208 default:
209 fprintf (stderr,USAGE);
210 #ifdef DEBUG
211 printf ("\n%s",USAGE_DEBUG);
212 #endif
213 exit(1);
214 break;
220 * Check if filename or stdin /stdout is used
222 if (argc == 1) {
223 use_stdin_stdout = FALSE;
224 strcpy (fname, argv[argc - 1]);
225 } else if (argc == 0) {
226 use_stdin_stdout = TRUE;
227 verbose = FALSE;
228 } else {
229 #ifdef DEBUG
230 printf ("\n%s", USAGE_DEBUG);
231 #endif
232 gpiv_error("%s: %s", argv[0], USAGE);
240 static gchar *
241 make_fname(char *fname_in,
242 char *fname_header,
243 char *fname_parameter,
244 char *fname_out
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\"";
255 return (err_msg);
259 * Stripping filename
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);
276 g_free (fname_base);
277 return (err_msg);
283 main (int argc,
284 char *argv[]
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);
306 if (verbose) {
307 #ifdef GIT_HASH
308 printf("# Software: %s\n# git hash: %s\n# Command line options:\n",
309 argv[0], GIT_REV);
310 #else
311 printf("# Software: %s\n# version: %s\n# Command line options:\n",
312 argv[0], GPIVTOOLS_VERSION);
313 #endif
314 gpiv_img_print_parameters (NULL, image_par);
315 gpiv_post_print_parameters (NULL, post_par);
318 gpiv_scan_parameter(GPIV_POSTPAR_KEY, PARFILE, post_par, verbose);
319 if ((err_msg =
320 gpiv_scan_resourcefiles(GPIV_POSTPAR_KEY, post_par, verbose))
321 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
323 if (use_stdin_stdout == FALSE) {
325 * Generating proper filenames
327 if ((err_msg =
328 make_fname (fname_in, fname_header, fname_parameter, fname_out))
329 != NULL) {
330 gpiv_error("%s: Failure calling make_fname", argv[0]);
334 * Prints command line parameters to par-file
336 if ((fp_par_dat = fopen (fname_parameter, "a")) == NULL) {
337 gpiv_error("\n%s: failure opening %s for input",
338 argv[0], fname_parameter);
340 fprintf(fp_par_dat, "\n\n# %s\n# Command line options:\n", argv[0]);
341 gpiv_img_print_parameters(fp_par_dat, image_par);
344 * Reading parametes from image header and PARFILE (and writing to data
345 * par-file)
347 gpiv_scan_parameter ("", fname_header, image_par, verbose);
348 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
349 verbose);
350 if ((err_msg =
351 gpiv_scan_resourcefiles(GPIV_IMGPAR_KEY, image_par,
352 verbose))
353 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
354 gpiv_img_print_parameters (fp_par_dat, image_par);
355 gpiv_post_print_parameters (fp_par_dat, post_par);
357 fclose(fp_par_dat);
360 } else {
361 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
362 verbose);
363 if ((err_msg =
364 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par,
365 verbose))
366 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
371 * Reading input data
373 if (use_stdin_stdout == TRUE) {
374 fp = stdin;
375 } else {
376 if ((fp = fopen (fname_in, "rb")) == NULL) {
377 gpiv_error ("%s: failure opening %s for reading",
378 argv[0], fname_in);
382 if ((piv_data = gpiv_read_pivdata (fp)) == NULL) {
383 gpiv_error ("%s: %s", argv[0], err_msg);
385 if (use_stdin_stdout == FALSE) fclose (fp);
389 * Here the function calls of the post-processing; scaling of PIV data
391 if (verbose == TRUE) printf("\n");
393 if (post_par->scale_type == GPIV_SCALE) {
394 if ((err_msg = gpiv_post_scale (piv_data, image_par)) != NULL) {
395 gpiv_error("%s: Failure calling gpiv_post_scale", argv[0]);
397 var_scale = TRUE;
398 } else if (post_par->scale_type == GPIV_SCALE_INV) {
399 if ((err_msg = gpiv_post_inverse_scale (piv_data, image_par)) != NULL) {
400 gpiv_error("%s: Failure calling gpiv_post_inverse_scale", argv[0]);
402 var_scale = TRUE;
403 } else {
404 gpiv_error("scale: used unexisting image_par.s_scale__set");
408 * Adding comment to the data
409 * And writing data to output
411 g_free (piv_data->comment);
412 #ifdef GIT_HASH
413 piv_data->comment = g_strdup_printf ("# Software: %s\n# git hash: %s\n",
414 argv[0], GIT_REV);
415 #else
416 piv_data->comment = g_strdup_printf ("# Software: %s\n# version: %s\n",
417 argv[0], GPIVTOOLS_VERSION);
418 #endif
419 piv_data->comment = gpiv_add_datetime_to_comment (piv_data->comment);
421 if (use_stdin_stdout == TRUE) {
422 fp = stdout;
423 } else {
424 if ((fp = fopen (fname_out, "wb")) == NULL) {
425 gpiv_error ("%s: Failure opening %s for output",
426 argv[0], fname_out);
430 if ((err_msg = gpiv_write_pivdata (fp, piv_data, TRUE)) != NULL) {
431 gpiv_error ("%s: %s", argv[0], err_msg);
433 if (use_stdin_stdout == FALSE) fclose (fp);
437 if (verbose == TRUE) printf("\n");
438 exit (0);