updated copyright statement
[gpivtools.git] / src / misc / suta.c
blob960e9f2950c0c5a4548ea0d3ba6d2a00972d1b4f
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>
33 /* #define PARFILE "scale.par" */ /* Parameter file name */
34 #define PARFILE "gpivrc" /* Parameter file name */
35 #define MAX_PIVSETS 1000 /* Maximum number of PIV data sets */
36 #define MIN_SAMPLES 20 /* Minimum number of samples used for estimation */
37 #define USAGE "\
38 Usage: suta [-h | --help] [-p | --print] [-v | --version] \n\
39 filename_ta filename \n\
40 \n\
41 keys: \n\
42 -h | --help: this on-line help \n\
43 -p | --print: print parameters to stdout \n\
44 -v | --version: version number \n\
45 filename File name of PIV data set (without .piv) \n\
46 filename_ta File name of time-averaged (local means) PIV data set \n\
47 (full name) \n\
50 #define HELP "\
51 suta - subtracts time-avaraged velocities (local mean) from the PIV estimators"
53 #define RCSID "$Id: suta.c,v 1.6 2008-09-25 13:08:34 gerber Exp $"
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 printf("%s\n", RCSID);
84 exit(0);
85 break;
86 case 'h':
87 printf("%s\n", argv[0]);
88 printf("%s\n",HELP);
89 printf("%s\n",USAGE);
90 exit(0);
91 break;
92 case 'p':
93 print_par = TRUE;
94 break;
97 * long option keys
99 case '-':
100 if (strcmp("-help", *argv) == 0) {
101 printf("\n%s", argv[0]);
102 printf("\n%s", HELP);
103 printf("\n%s", USAGE);
104 exit(0);
105 } else if (strcmp("-print", *argv) == 0) {
106 print_par = TRUE;
107 } else if (strcmp("-version", *argv) == 0) {
108 printf("%s\n", RCSID);
109 exit(0);
110 } else {
111 gpiv_error("%s: unknown option: %s", argv[0], *argv);
113 argc_next = 1;
114 break;
116 default:
117 gpiv_error(USAGE);
118 break;
122 if(argc != 2) {
123 gpiv_error("%s: %s", argv[0], USAGE);
126 strcpy(fname_mean, argv[0]);
127 strcpy(fname_in, argv[1]);
133 void
134 make_fname(char *fname,
135 char *fname_in,
136 char *fname_out
138 /* ----------------------------------------------------------------------------
139 * define input and output filenames
142 gpiv_io_make_fname(fname, GPIV_EXT_PIV, fname_in);
143 if (print_par) printf("# Input data file: %s\n",fname_in);
146 * Extension of filename with time-averaged reduced piv data
148 #define EXT_SUB_TAVG_PIV ".suta.piv"
149 gpiv_io_make_fname(fname, EXT_SUB_TAVG_PIV, fname_out);
150 if (print_par) printf("# Output data file: %s\n",fname_out);
156 int
157 main(int argc,
158 char *argv[]
160 /* ----------------------------------------------------------------------------
163 FILE *fp = NULL;
164 gchar *err_msg = NULL;
165 gchar fname[GPIV_MAX_CHARS],
166 fname_in[GPIV_MAX_CHARS],
167 fname_out[GPIV_MAX_CHARS],
168 fname_mean[GPIV_MAX_CHARS];
169 guint i, j;
171 GpivPivData *in_data = NULL, *mean_data = NULL, *out_data = NULL;
175 command_args(argc, argv, fname, fname_mean);
176 make_fname(fname, fname_in, fname_out);
177 if (print_par) {
178 g_message("fname_mean = %s", fname_mean);
183 if ((fp = fopen (fname_in, "r")) == NULL) {
184 gpiv_error ("%s: failing opening %s", argv[0], fname_in);
187 if ((in_data = gpiv_read_pivdata (fp)) == NULL) {
188 gpiv_error ("%s: failing gpiv_read_pivdata", argv[0], err_msg);
190 fclose (fp);
194 if ((fp = fopen (fname_mean, "r")) == NULL) {
195 gpiv_error ("%s: failing opening %s", argv[0], fname_mean);
198 if ((mean_data = gpiv_read_pivdata (fp)) == NULL) {
199 gpiv_error ("%s: gpiv_read_pivdata", argv[0], err_msg);
201 fclose (fp);
204 out_data = gpiv_alloc_pivdata (in_data->nx, in_data->ny);
206 for (i = 0; i < in_data->ny; i++) {
207 for (j = 0; j < in_data->nx; j++) {
208 out_data->point_x[i][j] = in_data->point_x[i][j];
209 out_data->point_y[i][j] = in_data->point_y[i][j];
210 out_data->peak_no[i][j] = in_data->peak_no[i][j];
211 if (mean_data->peak_no[i][j] > 0) {
212 out_data->snr[i][j] = in_data->snr[i][j];
213 out_data->dx[i][j] = in_data->dx[i][j] - mean_data->dx[i][j];
214 out_data->dy[i][j] = in_data->dy[i][j] - mean_data->dy[i][j];
215 } else {
216 out_data->snr[i][j] = GPIV_SNR_DISABLE;
217 out_data->dx[i][j] = 0.0;
218 out_data->dy[i][j] = 0.0;
224 if ((fp = fopen (fname_out, "w")) == NULL) {
225 gpiv_error ("%s: failing opening %s", argv[0], fname_out);
228 if ((err_msg = gpiv_write_pivdata(fp, out_data, TRUE)) != NULL) {
229 gpiv_error ("%s: %s", argv[0], err_msg);
231 fclose (fp);
235 gpiv_free_pivdata (in_data);
236 gpiv_free_pivdata (mean_data);
237 exit (0);