File: s-avg.c

package info (click to toggle)
gpivtools 0.3.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,340 kB
  • ctags: 467
  • sloc: ansic: 6,588; sh: 3,245; perl: 1,195; makefile: 282
file content (423 lines) | stat: -rw-r--r-- 12,534 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */

/*---------------------------------------------------------------------------
Descripion:
  s-avg - reports spatial global mean velocity and rms error of a PIV data 
         stream. Eventually subtracts mean value from piv the data.

   Copyright (C) 2002, 2003, 2004 Gerber van der Graaf

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

------------------------------------------------------------------------*/

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>
#include <glib.h>
#include <gpiv.h>


/* #define PARFILE "savg.par"     */ /* Parameter file name */
#define PARFILE "gpivrc"     /* Parameter file name */
#define USAGE "\
Usage: scale [-f filename][-h | --help] [-p | --print] [-s|-no_s] \n\
             [-v | --version] [-z dx dy] < stdin > stdout \n\
\n\
keys: \n\
-f filename:           files (without .piv extension) instaed of stdin and \n\
                       stdout \n\
-h | --help:           this on-line help \n\
-p | --print:          print parameters to stdout \n\
-s:                    subtracts nothing (0), mean of dx and dy (2) from \n\
                       displacements or velocity estimators \n\
-no_s:                 suppresses subtracting mean from input data \n\
-v | --version:        prints version \n\
-z dx dy:              zero offset of velocities/displacements \n\
"


#ifdef DEBUG
#define USAGE_DEBUG "\
Developers version also contains:  \n\
               [-p_main] \n\
keys: \n\
-p_'function' N; prints data to be generated in the function; the \n\
                   higher N, the more detailed the output. \n\
                   For N = 10, err_vec will exit at the end of the function"
#endif


#define HELP  "\
s-avg reports spatial global mean velocity and rms error of a PIV data stream"

#define RCSID "$Id: s-avg.c,v 2.8 2006/01/31 14:18:04 gerber Exp $"

/*
 * Global variables 
*/
gboolean fname_logic = FALSE;
gboolean print_par = FALSE;
GpivPivData in_data, out_data;

#ifdef DEBUG
/*
 * Variables for development version
*/
int print_main=0;
#endif 



void 
command_args(int argc, char *argv[], 
             char fname[GPIV_MAX_CHARS],
             GpivPostPar * piv_post_par
             )
/* ----------------------------------------------------------------------------
 * Command line argument handling
 */
{
    char c;
    int argc_next;


    while (--argc > 0 && (*++argv)[0] == '-') {
        argc_next=0;

/*
 * argc_next is set to 1 if the next cmd line argument has to be
 * searched for; in case that the command line argument concerns more
 * than one char or cmd line argument needs a parameter
*/
        while (argc_next==0 && (c = *++argv[0])) {
            switch (c) {

            case 'v':
/*
 * Use Revision Control System (RCS) for version
 */
                printf("%s\n", RCSID); 
                exit(0);

            case 'h':
          printf("%s\n", RCSID); 
          printf("%s\n",HELP);
          printf("%s\n",USAGE);
#ifdef DEBUG
          printf ("\n%s\n",USAGE_DEBUG);
#endif
          exit(0);
/*
 * Spatial scaling
 */
            case 's':
                piv_post_par->subtract = atoi(*++argv);
                piv_post_par->subtract_logic = TRUE;
                argc_next = 1;
                break;
                
            case 'f':
                strcpy(fname,*++argv); 
                fname_logic = TRUE;
                argc_next=1;
                --argc;
                break;

            case 'p':
#ifdef DEBUG
                if ((strcmp(*argv,"p_main" ) !=0)) {
#endif
                    print_par = TRUE;
#ifdef DEBUG
                } else if (strcmp("p_main",*argv) == 0) {  
                    print_main=atoi(*++argv);
                    --argc; 
	    argc_next=1;
                }	  
#endif /* DEBUG */
                break;
                
            case 'z':
                piv_post_par->z_off_dx = atof(*++argv);
                piv_post_par->z_off_dy = atof(*++argv);
                fprintf(stderr, "\n000:: z_off_dx=%f", piv_post_par->z_off_dx);
                fprintf(stderr, "  z_off_dy=%f\n", piv_post_par->z_off_dy);
                piv_post_par->z_off_dx_logic = TRUE;
                piv_post_par->z_off_dy_logic = TRUE;
                argc_next = 1;
                --argc;
                --argc;
                argc_next = 1;
                break;

/*
 * negotion of settings
 */            case 'n': 
		if (strcmp("no_s", *argv) == 0) {    /* --- do not subtract mean -*/
                    piv_post_par->subtract = 0;
                    piv_post_par->subtract_logic = TRUE;
                }
                break;


/*
 * long option keys
 */
	    case '-':
		if (strcmp("-help", *argv) == 0) {
                    printf("\n%s", RCSID);
                    printf("\n%s", HELP);
                    printf("\n%s", USAGE);
                    exit(0);
                } else if (strcmp("-print", *argv) == 0) {
		    print_par = TRUE;
                } else if (strcmp("-version", *argv) == 0) {
                    printf("%s\n", RCSID);
                    exit(0);
                } else {
		    gpiv_error("%s: unknown option: %s", RCSID, *argv);
		}
		argc_next = 1;
		break;

            default:
                fprintf (stderr,USAGE);
#ifdef DEBUG
                printf ("\n%s",USAGE_DEBUG);
#endif
                exit(1);
                break;
            }
        }
    }
    
    if(argc != 0) { 
        gpiv_error("%s: %s", RCSID, USAGE);
#ifdef DEBUG
        printf ("\n%s", USAGE_DEBUG);
#endif
        exit(1); 
    }
}



void 
make_fname(char *fname, 
           char *fname_parameter, 
           char *fname_in, 
           char *fname_out
           )
/* ----------------------------------------------------------------------------
 * function to generate filenames
 */
{
    if (fname_logic == TRUE){
        gpiv_error("%s: Filename has to be set", RCSID);
    }
    
    gpiv_io_make_fname(fname, GPIV_EXT_PAR, fname_parameter);
    if (print_par)     printf("# Data parameter file: %s\n",fname_parameter);
    
    gpiv_io_make_fname(fname, GPIV_EXT_PIV, fname_in);
    if (print_par)     printf("# Input data file: %s\n",fname_in);
    
    gpiv_io_make_fname(fname, GPIV_EXT_SA, fname_out);
    if (print_par)     printf("# Output file: %s\n",fname_out);
    
}



int 
main(int argc, 
     char *argv[]
     )
/*-----------------------------------------------------------------------------
*/
{
    char *err_msg = NULL;
    FILE  *fp_par_dat, *fp;
    char fname[GPIV_MAX_CHARS], 
        fname_out[GPIV_MAX_CHARS], 
        fname_parameter[GPIV_MAX_CHARS], 
        fname_in[GPIV_MAX_CHARS];
    char  d_line[GPIV_MAX_LINES][GPIV_MAX_CHARS], 
        c_line[GPIV_MAX_LINES_C][GPIV_MAX_CHARS];
    float mean_dx=0, sdev_dx=0, mean_dy=0, sdev_dy=0;
    /*
 *Take some strange number as initial values for minima and maximuma
 */
    float min_dx=73.0e9, min_dy=73.0e9, max_dx=-73.0e9, max_dy=-73.0e9;
    int nd_lines=0, nc_lines=0, scale=0, ndata=0;

    GpivPostPar piv_post_par, piv_post_par_default;


    gpiv_post_parameters_logic(&piv_post_par, FALSE);
    gpiv_post_default_parameters(&piv_post_par_default, TRUE);
    command_args(argc, argv, fname, &piv_post_par);
    if (print_par) {
        printf("# %s\n# Command line options:\n", RCSID);
        gpiv_post_print_parameters(piv_post_par);
    }
    

    if(fname_logic == TRUE) {
        make_fname(fname, fname_parameter, fname_in, fname_out);
 

/*
 * Prints command line parameters to par-file 
 */
        if ((fp_par_dat = fopen (fname_parameter, "a")) == NULL) {
            gpiv_error("%s: failure opening %s for input\n", RCSID,
                       fname_parameter);
        }
        fprintf(fp_par_dat, "# %s\n# Command line options:\n", RCSID);
        gpiv_post_fprint_parameters(fp_par_dat, piv_post_par);

/*
 * Reading parametes from PARFILE (and writing to data par-file)
 */
        gpiv_scan_parameter(GPIV_POST_PAR_KEY, PARFILE, &piv_post_par, print_par);
        if ((err_msg =
             gpiv_scan_resourcefiles(GPIV_POST_PAR_KEY, &piv_post_par, print_par))
            != NULL)  gpiv_error ("%s: %s", RCSID, err_msg);
        gpiv_post_fprint_parameters(fp_par_dat, piv_post_par);
	fclose(fp_par_dat);
        
        
    } else {
        gpiv_scan_parameter(GPIV_POST_PAR_KEY, PARFILE, &piv_post_par, print_par);
        if ((err_msg =
             gpiv_scan_resourcefiles(GPIV_POST_PAR_KEY, &piv_post_par, print_par))
            != NULL)  gpiv_error ("%s: %s", RCSID, err_msg);
    }
    
    gpiv_post_check_parameters_read(&piv_post_par, piv_post_par_default);
    

/*
 * Check parameters on correct values and adjust belonging variables
 */


/*
 * As in_data.nx or in_data.ny are not known, the input data file will first 
 * be read
*/

    if(fname_logic == TRUE) {
        if ((scale = gpiv_fcount_pivdata(fname_in, &in_data)) == -1) {
            gpiv_error("%s: Failure calling gpiv_count_pivdata", RCSID);
        } else if (print_par) {
            printf("\nmain: scale=%d in_data.nx=%d in_data.ny=%d",
                   scale, in_data.nx, in_data.ny);
        }
    } else {
        if ((scale = gpiv_count_pivdata(&in_data, d_line, &nd_lines)) == -1) {
            gpiv_error("%s: Failure calling gpiv_count_pivdata", RCSID);
        }
    }


/*
 * Now the parameters are known, data  memory can be allocated
 */
    gpiv_alloc_pivdata(&in_data);
    if (piv_post_par.subtract == 1) {
        out_data.nx = in_data.nx;
        out_data.ny = in_data.ny;
        gpiv_alloc_pivdata(&out_data);
    }


/* 
 * Reading input file of piv data 
 */
#ifdef DEBUG
    if (print_main >= 1) fprintf(stderr,"\ncalling (f)gpiv_read_pivdata\n");
#endif

    if(fname_logic == TRUE) {
        if ((err_msg = 
             gpiv_fread_pivdata(fname_in, &in_data, c_line, 
                                             &nc_lines))
            != NULL) gpiv_error ("%s: %s", RCSID, err_msg);
    } else {
        gpiv_read_pivdata(&in_data, d_line, nd_lines, c_line, &nc_lines);
    }

/*
 * Here the function call of the program body
 */
    if (print_par == TRUE) printf("\n");

#ifdef DEBUG
    if (print_main >= 1) fprintf(stderr,"\ncalling gpiv_post_savg\n");
#endif

    gpiv_post_savg(in_data, &out_data, piv_post_par, &ndata);

/*
 * And writing to output 
 */
    if ( piv_post_par.subtract == 1) {
        if (fname_logic == TRUE) {
            if ((err_msg = 
            gpiv_fwrite_pivdata (fname_out, &out_data, c_line,
                                 nc_lines, scale, RCSID))
                != NULL) gpiv_error ("%s: %s", RCSID, err_msg);
        } else {
            gpiv_write_pivdata(&out_data, c_line, nc_lines, scale, RCSID);
        }
    } else {
        if (fname_logic == TRUE) {
            if((fp=fopen(fname_out,"w"))==NULL) { 
                fprintf (stderr,"\n%s: Failure opening %s for output\n",
                         RCSID,fname_out); 
                return 1;
            }
            fprintf(fp, "ndata = %d", ndata);
            fprintf(fp, " mean_dx = %f sdev_dx = %f min_dx = %f max_dx = %f", 
                    mean_dx, sdev_dx, min_dx,  max_dx);
            fprintf(fp, " mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n", 
                    mean_dy, sdev_dy, min_dy,  max_dy);
        } else {
            printf("ndata = %d", ndata);
            printf(" mean_dx = %f sdev_dx = %f min_dx = %f max_dx = %f", 
                   mean_dx, sdev_dx, min_dx,  max_dx);
            printf(" mean_dy = %f sdev_dy = %f min_dy = %f max_dy = %f\n", 
                   mean_dy, sdev_dy, min_dy,  max_dy);
        }
    }
    
/*
 * Freeing allocated memory of matrices 
 */
    gpiv_free_pivdata(&in_data);
    if (piv_post_par.subtract == 1) {
        gpiv_free_pivdata(&out_data);
    }
    
    if (print_par == TRUE) printf("\n");
    exit(0);
}