show git hash (if available) when invoking -v key
[gpivtools.git] / src / post / scale.c
blob92c4dcee71cf8465546c31c8803ba2e3d66a9f6c
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("# %s\n# Command line options:\n", GIT_REV);
309 #else
310 printf("# %s\n# Command line options:\n", GPIVTOOLS_VERSION);
311 #endif
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);
317 if ((err_msg =
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
325 if ((err_msg =
326 make_fname (fname_in, fname_header, fname_parameter, fname_out))
327 != NULL) {
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
343 * par-file)
345 gpiv_scan_parameter ("", fname_header, image_par, verbose);
346 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
347 verbose);
348 if ((err_msg =
349 gpiv_scan_resourcefiles(GPIV_IMGPAR_KEY, image_par,
350 verbose))
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);
355 fclose(fp_par_dat);
358 } else {
359 gpiv_scan_parameter (GPIV_IMGPAR_KEY, PARFILE, image_par,
360 verbose);
361 if ((err_msg =
362 gpiv_scan_resourcefiles (GPIV_IMGPAR_KEY, image_par,
363 verbose))
364 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
369 * Reading input data
371 if (use_stdin_stdout == TRUE) {
372 fp = stdin;
373 } else {
374 if ((fp = fopen (fname_in, "rb")) == NULL) {
375 gpiv_error ("%s: failure opening %s for reading",
376 argv[0], fname_in);
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]);
395 var_scale = TRUE;
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]);
400 var_scale = TRUE;
401 } else {
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);
410 #ifdef GIT_HASH
411 piv_data->comment = g_strdup_printf ("# Software: %s\n", GIT_REV);
412 #else
413 piv_data->comment = g_strdup_printf ("# Software: %s\n", GPIVTOOLS_VERSION);
414 #endif
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);