show git hash (if available) when invoking -v key
[gpivtools.git] / src / misc / suta.c
blobc7474d933c5cef5070c2ccadb75304ec167a3c04
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*-----------------------------------------------------------------------------
5 suta - subtracts time-avaraged velocities (local mean velocities) from
6 the PIV estimators
8 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008
9 Gerber van der Graaf <gerber_graaf@users.sourceforge.net
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software Foundation,
23 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 -----------------------------------------------------------------------------*/
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <math.h>
31 #include <gpiv.h>
32 #include "config.h"
33 #include "git-rev.h"
35 /* #define PARFILE "scale.par" */ /* Parameter file name */
36 #define PARFILE "gpivrc" /* Parameter file name */
37 #define MAX_PIVSETS 1000 /* Maximum number of PIV data sets */
38 #define MIN_SAMPLES 20 /* Minimum number of samples used for estimation */
39 #define USAGE "\
40 Usage: suta [-h | --help] [-p | --print] [-v | --version] \n\
41 filename_ta filename \n\
42 \n\
43 keys: \n\
44 -h | --help: this on-line help \n\
45 -p | --print: print parameters to stdout \n\
46 -v | --version: version number \n\
47 filename File name of PIV data set (without .piv) \n\
48 filename_ta File name of time-averaged (local means) PIV data set \n\
49 (full name) \n\
52 #define HELP "\
53 suta - subtracts time-avaraged velocities (local mean) from the PIV estimators"
55 gboolean print_par = FALSE;
57 void
58 command_args(int argc,
59 char *argv[],
60 char fname_in[GPIV_MAX_CHARS],
61 char fname_mean[GPIV_MAX_CHARS]
63 /* ----------------------------------------------------------------------------
64 * Command line argument handling
67 char c = '\0';
68 int argc_next;
70 while (--argc > 0 && (*++argv)[0] == '-') {
73 * argc_next is set to 1 if the next cmd line argument has to be searched for;
74 * in case that the command line argument concerns more than one char or cmd
75 * line argument needs a parameter
78 argc_next = 0;
79 while (argc_next == 0 && (c = *++argv[0]))
81 switch (c) {
82 case 'v':
83 #ifdef GIT_HASH
84 printf ("git hash: %s\n", GIT_REV);
85 #else
86 printf ("version: %s\n", GPIVTOOLS_VERSION);
87 #endif
88 exit(0);
89 break;
90 case 'h':
91 printf("%s\n", argv[0]);
92 printf("%s\n",HELP);
93 printf("%s\n",USAGE);
94 exit(0);
95 break;
96 case 'p':
97 print_par = TRUE;
98 break;
101 * long option keys
103 case '-':
104 if (strcmp("-help", *argv) == 0) {
105 printf("\n%s", argv[0]);
106 printf("\n%s", HELP);
107 printf("\n%s", USAGE);
108 exit(0);
109 } else if (strcmp("-print", *argv) == 0) {
110 print_par = TRUE;
111 } else if (strcmp("-version", *argv) == 0) {
112 #ifdef GIT_HASH
113 printf ("git hash: %s\n", GIT_REV);
114 #else
115 printf ("version: %s\n", GPIVTOOLS_VERSION);
116 #endif
117 exit(0);
118 } else {
119 gpiv_error("%s: unknown option: %s", argv[0], *argv);
121 argc_next = 1;
122 break;
124 default:
125 gpiv_error(USAGE);
126 break;
130 if(argc != 2) {
131 gpiv_error("%s: %s", argv[0], USAGE);
134 strcpy(fname_mean, argv[0]);
135 strcpy(fname_in, argv[1]);
141 void
142 make_fname(char *fname,
143 char *fname_in,
144 char *fname_out
146 /* ----------------------------------------------------------------------------
147 * define input and output filenames
150 gpiv_io_make_fname(fname, GPIV_EXT_PIV, fname_in);
151 if (print_par) printf("# Input data file: %s\n",fname_in);
154 * Extension of filename with time-averaged reduced piv data
156 #define EXT_SUB_TAVG_PIV ".suta.piv"
157 gpiv_io_make_fname(fname, EXT_SUB_TAVG_PIV, fname_out);
158 if (print_par) printf("# Output data file: %s\n",fname_out);
164 int
165 main(int argc,
166 char *argv[]
168 /* ----------------------------------------------------------------------------
171 FILE *fp = NULL;
172 gchar *err_msg = NULL;
173 gchar fname[GPIV_MAX_CHARS],
174 fname_in[GPIV_MAX_CHARS],
175 fname_out[GPIV_MAX_CHARS],
176 fname_mean[GPIV_MAX_CHARS];
177 guint i, j;
179 GpivPivData *in_data = NULL, *mean_data = NULL, *out_data = NULL;
183 command_args(argc, argv, fname, fname_mean);
184 make_fname(fname, fname_in, fname_out);
185 if (print_par) {
186 g_message("fname_mean = %s", fname_mean);
191 if ((fp = fopen (fname_in, "r")) == NULL) {
192 gpiv_error ("%s: failing opening %s", argv[0], fname_in);
195 if ((in_data = gpiv_read_pivdata (fp)) == NULL) {
196 gpiv_error ("%s: failing gpiv_read_pivdata", argv[0], err_msg);
198 fclose (fp);
202 if ((fp = fopen (fname_mean, "r")) == NULL) {
203 gpiv_error ("%s: failing opening %s", argv[0], fname_mean);
206 if ((mean_data = gpiv_read_pivdata (fp)) == NULL) {
207 gpiv_error ("%s: gpiv_read_pivdata", argv[0], err_msg);
209 fclose (fp);
212 out_data = gpiv_alloc_pivdata (in_data->nx, in_data->ny);
214 for (i = 0; i < in_data->ny; i++) {
215 for (j = 0; j < in_data->nx; j++) {
216 out_data->point_x[i][j] = in_data->point_x[i][j];
217 out_data->point_y[i][j] = in_data->point_y[i][j];
218 out_data->peak_no[i][j] = in_data->peak_no[i][j];
219 if (mean_data->peak_no[i][j] > 0) {
220 out_data->snr[i][j] = in_data->snr[i][j];
221 out_data->dx[i][j] = in_data->dx[i][j] - mean_data->dx[i][j];
222 out_data->dy[i][j] = in_data->dy[i][j] - mean_data->dy[i][j];
223 } else {
224 out_data->snr[i][j] = GPIV_SNR_DISABLE;
225 out_data->dx[i][j] = 0.0;
226 out_data->dy[i][j] = 0.0;
232 if ((fp = fopen (fname_out, "w")) == NULL) {
233 gpiv_error ("%s: failing opening %s", argv[0], fname_out);
236 if ((err_msg = gpiv_write_pivdata(fp, out_data, TRUE)) != NULL) {
237 gpiv_error ("%s: %s", argv[0], err_msg);
239 fclose (fp);
243 gpiv_free_pivdata (in_data);
244 gpiv_free_pivdata (mean_data);
245 exit (0);