updated copyright statement
[gpivtools.git] / src / misc / piv2grid.c
blob76ec607bc7e83520d0564a91232653dcb75cb5a5
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*---------------------------------------------------------------------------
5 piv2h5 - converts PIV data to hdf5 format 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 <gpiv.h>
31 /* #define PARFILE "scale.par" */ /* Parameter file name */
32 #define PARFILE "gpivrc" /* Parameter file name */
33 #define USAGE "\
34 Usage: piv2grid [-h | --help] [-p | --print] [-v | --version] [-t type] \n\
35 filename \n\
36 \n\
37 keys: \n\
38 -h | --help: this on-line help \n\
39 -p | --print: print parameters to stdout \n\
40 -v | --version: version number \n\
41 -t: select which data to be stored on grid; dx, dy or snr \n\
42 (default: snr)\n\
45 #define HELP "\
46 piv2grid - converts PIV data to grid data for generating contour plots \n\
47 plotmtv \n\
50 #define RCSID "$Id: piv2grid.c,v 1.9 2008-09-25 13:08:34 gerber Exp $"
52 enum DataType {
53 DT_DX = 0,
54 DT_DY = 1,
55 DT_SNR = 2
58 gboolean print_par = FALSE;
62 void
63 command_args(int argc,
64 char *argv[],
65 char fname[GPIV_MAX_CHARS],
66 enum DataType *datatype
68 /*-----------------------------------------------------------------------------
69 * Command line argument handling
72 char c = '\0';
73 int argc_next;
75 while (--argc > 0 && (*++argv)[0] == '-') {
78 * argc_next is set to 1 if the next cmd line argument has to be searched for;
79 * in case that the command line argument concerns more than one char or cmd
80 * line argument needs a parameter
82 argc_next = 0;
83 while (argc_next == 0 && (c = *++argv[0])) {
85 switch (c) {
86 case 'v':
87 printf("%s\n", RCSID);
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;
99 case 't':
100 if (strcmp("dx", *argv) == 0) {
101 *datatype = DT_DX;
102 } else
104 if (strcmp("dy", *argv) == 0) {
105 *datatype = DT_DY;
106 } else
108 if (strcmp("snr", *argv) == 0) {
109 *datatype = DT_SNR;
110 } else {
111 gpiv_error("piv2grid: wrong specification of data type");
113 break;
116 * long option keys
118 case '-':
119 if (strcmp("-help", *argv) == 0) {
120 printf("\n%s", argv[0]);
121 printf("\n%s", HELP);
122 printf("\n%s", USAGE);
123 exit(0);
124 } else if (strcmp("-print", *argv) == 0) {
125 print_par = TRUE;
126 } else if (strcmp("-version", *argv) == 0) {
127 printf("%s\n", RCSID);
128 exit(0);
129 } else {
130 gpiv_error("%s: unknown option: %s", argv[0], *argv);
132 argc_next = 1;
133 break;
135 default:
136 fprintf (stderr,USAGE);
137 exit(1);
138 break;
143 if(argc != 1) {
144 gpiv_error("%s: %s", argv[0], USAGE);
146 strcpy(fname, argv[0]);
152 void
153 make_fname(char *fname,
154 char *fname_piv,
155 char *fname_out
157 /*-----------------------------------------------------------------------------
158 * generate filenames
161 gpiv_io_make_fname(fname, GPIV_EXT_PIV, fname_piv);
162 if (print_par) printf("# Input data file: %s\n",fname_piv);
164 gpiv_io_make_fname(fname, ".grid", fname_out);
165 if (print_par) printf("# Output file: %s\n",fname_out);
175 int
176 main(int argc,
177 char *argv[]
179 /* ----------------------------------------------------------------------------
180 * main routine to convert PIV data to grid data for generating contour plots
183 FILE *fp;
184 gchar *err_msg = NULL;
185 gchar fname[GPIV_MAX_CHARS],
186 fname_out[GPIV_MAX_CHARS],
187 fname_piv[GPIV_MAX_CHARS],
188 fname_parameter[GPIV_MAX_CHARS];
189 guint i, j;
191 GpivPivData *piv_data = NULL;
192 GpivScalarData *sc_data = NULL;
193 enum DataType datatype = DT_SNR;
196 command_args (argc, argv, fname, &datatype);
197 make_fname (fname, fname_piv, fname_out);
198 gpiv_io_make_fname (fname, GPIV_EXT_PAR, fname_parameter);
199 if (print_par) printf ("datatype = %d\n", datatype);
201 * Reading PIV data
203 if ((fp = fopen (fname_piv, "r")) == NULL) {
204 gpiv_error ("%s: failing opening %s", argv[0], fname_piv);
207 if ((piv_data = gpiv_read_pivdata(fp)) != NULL) {
208 gpiv_error ("%s: %s", argv[0], err_msg);
212 * copying PIV data to SCALAR data
214 if ((sc_data = gpiv_alloc_scdata (piv_data->nx, piv_data->ny)) == NULL) {
215 gpiv_error ("%s: failing gpiv_alloc_scdata", argv[0]);
218 for (i = 0; i < sc_data->ny; i++) {
219 for (j = 0; j < sc_data->nx; j++) {
220 sc_data->point_x[i][j] = piv_data->point_x[i][j];
221 sc_data->point_y[i][j] = piv_data->point_y[i][j];
222 if (piv_data->peak_no[i][j] != -1) {
223 if (datatype == DT_DX) sc_data->scalar[i][j] = piv_data->dx[i][j];
224 if (datatype == DT_DY) sc_data->scalar[i][j] = piv_data->dy[i][j];
225 if (datatype == DT_SNR) sc_data->scalar[i][j] = piv_data->snr[i][j];
226 } else {
227 sc_data->scalar[i][j] = -1.0;
229 sc_data->flag[i][j] = piv_data->peak_no[i][j];
236 * Saving SCALAR data
238 if ((fp = fopen (fname_out, "w")) == NULL) {
239 gpiv_error ("%s: failing opening %s", argv[0], fname_out);
242 if ((err_msg = gpiv_write_sc_mtvgriddata (fp, sc_data, TRUE))
243 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
247 gpiv_free_pivdata (piv_data);
248 exit (0);