File: piv2grid.c

package info (click to toggle)
gpivtools 0.6.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,472 kB
  • ctags: 528
  • sloc: ansic: 6,804; sh: 5,105; perl: 1,035; makefile: 299; python: 207
file content (254 lines) | stat: -rw-r--r-- 6,877 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */

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

   piv2h5 - converts PIV data to hdf5 format 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 <gpiv.h> 


/* #define PARFILE "scale.par"    */  /* Parameter file name */
#define PARFILE "gpivrc"     /* Parameter file name */
#define USAGE "\
Usage: piv2grid [-h | --help] [-p | --print] [-v | --version] [-t type] \n\
                filename \n\
\n\
keys: \n\
-h | --help:           this on-line help \n\
-p | --print:          print parameters to stdout \n\
-v | --version:        version number \n\
-t:                    select which data to be stored on grid; dx, dy or snr \n\
                       (default: snr)\n\
"

#define HELP  "\
piv2grid - converts PIV data to grid data for generating contour plots \n\
plotmtv \n\
"

#define RCSID "$Id: piv2grid.c,v 1.9 2008-09-25 13:08:34 gerber Exp $"

enum DataType {
  DT_DX = 0,
  DT_DY = 1,
  DT_SNR = 2
};

gboolean print_par = FALSE;



void 
command_args(int argc, 
             char *argv[], 
             char fname[GPIV_MAX_CHARS],
             enum DataType *datatype
             )
/*-----------------------------------------------------------------------------
 * Command line argument handling
 */
{
    char c = '\0';
    int argc_next;

    while (--argc > 0 && (*++argv)[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 
 */
	argc_next = 0;
	while (argc_next == 0 && (c = *++argv[0])) {

            switch (c) {
            case 'v':
                printf("%s\n", RCSID); 
                exit(0);
                break;
            case 'h':
                printf("%s\n", argv[0]); 
                printf("%s\n",HELP);
                printf("%s\n",USAGE);
                exit(0);
                break;
            case 'p':
                print_par = TRUE;
                break;
            case 't':
                if (strcmp("dx", *argv) == 0) {
                    *datatype = DT_DX;
                } else 

                if (strcmp("dy", *argv) == 0) {
                    *datatype = DT_DY;
                } else

                if (strcmp("snr", *argv) == 0) {
                    *datatype = DT_SNR;
                } else {
                    gpiv_error("piv2grid: wrong specification of data type");
                }
                break;

/*
 * long option keys
 */
	    case '-':
		if (strcmp("-help", *argv) == 0) {
                    printf("\n%s", argv[0]);
                    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", argv[0], *argv);
		}
		argc_next = 1;
		break;

            default:
                fprintf (stderr,USAGE);
                exit(1);
                break;
            }
        }
        
    }
     if(argc != 1) { 
	  gpiv_error("%s: %s", argv[0], USAGE);
     }
     strcpy(fname, argv[0]); 


}


void 
make_fname(char *fname, 
           char *fname_piv, 
           char *fname_out
           )
/*-----------------------------------------------------------------------------
 * generate filenames
 */
{
     gpiv_io_make_fname(fname, GPIV_EXT_PIV, fname_piv);
     if (print_par) printf("# Input data file: %s\n",fname_piv);
     
     gpiv_io_make_fname(fname, ".grid", fname_out);
     if (print_par) printf("# Output file: %s\n",fname_out);
     
}



   



int 
main(int argc, 
     char *argv[]
     )
/* ----------------------------------------------------------------------------
 * main routine to convert PIV data to grid data for generating contour plots
 */
{
    FILE *fp;
    gchar *err_msg = NULL;
    gchar fname[GPIV_MAX_CHARS], 
        fname_out[GPIV_MAX_CHARS], 
        fname_piv[GPIV_MAX_CHARS],
        fname_parameter[GPIV_MAX_CHARS];
    guint i, j;

    GpivPivData *piv_data = NULL;
    GpivScalarData *sc_data = NULL;
    enum DataType datatype = DT_SNR;


    command_args (argc, argv, fname, &datatype);
    make_fname (fname, fname_piv, fname_out);
    gpiv_io_make_fname (fname, GPIV_EXT_PAR, fname_parameter);
    if (print_par) printf ("datatype = %d\n", datatype);
/*
 * Reading PIV data
 */
    if ((fp = fopen (fname_piv, "r")) == NULL) {
        gpiv_error ("%s: failing opening %s", argv[0], fname_piv);
    }

    if ((piv_data = gpiv_read_pivdata(fp)) != NULL) {
        gpiv_error ("%s: %s", argv[0], err_msg);
    }

/*
 * copying PIV data to SCALAR data
 */
    if ((sc_data = gpiv_alloc_scdata (piv_data->nx, piv_data->ny)) == NULL) {
        gpiv_error ("%s: failing gpiv_alloc_scdata", argv[0]);
    }

    for (i = 0; i < sc_data->ny; i++) {
        for (j = 0; j < sc_data->nx; j++) {
            sc_data->point_x[i][j] = piv_data->point_x[i][j];
            sc_data->point_y[i][j] = piv_data->point_y[i][j];
            if (piv_data->peak_no[i][j] != -1) {
                if (datatype == DT_DX) sc_data->scalar[i][j] = piv_data->dx[i][j];
                if (datatype == DT_DY) sc_data->scalar[i][j] = piv_data->dy[i][j];
                if (datatype == DT_SNR) sc_data->scalar[i][j] = piv_data->snr[i][j];
            } else {
                sc_data->scalar[i][j] = -1.0;
            }
            sc_data->flag[i][j] = piv_data->peak_no[i][j];
        }
    }



/*
 * Saving SCALAR data
 */
    if ((fp = fopen (fname_out, "w")) == NULL) {
        gpiv_error ("%s: failing opening %s", argv[0], fname_out);
    }

    if ((err_msg = gpiv_write_sc_mtvgriddata (fp, sc_data, TRUE))
        != NULL)  gpiv_error ("%s: %s", argv[0], err_msg);



    gpiv_free_pivdata (piv_data);
    exit (0);
}