libgpiv so version 4: GpivFt. Show git hash (if available) in output and
[gpivtools.git] / src / misc / piv2hdf.c
blobb3c5cdb31021d9873b95ce791aeb3a0b56378421
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 <time.h>
29 #include <gpiv.h>
30 #include "config.h"
31 #include "git-rev.h"
33 /* #define PARFILE "scale.par" */ /* Parameter file name */
34 #define PARFILE "gpivrc" /* Parameter file name */
35 #define USAGE "\
36 Usage: piv2hdf [-e] [-h | --help] [-i] [-p | --print] [-v | --version] \n\
37 [-V | --verbose] filename \n\
38 \n\
39 keys: \n\
40 -e: exclude data \n\
41 -h | --help: this on-line help \n\
42 -i: includes image data \n\
43 -p | --print: print parameters to stdout \n\
44 -v | --version: version number \n\
45 -V | --verbose: program behaves more verbose \n\
46 filename: input PIV datafile, including .piv extension \n\
49 #define HELP "\
50 piv2hdf - converts ASCII PIV-data and derived scalars to hdf5 formatted data"
52 gboolean print_par = FALSE, verbose = FALSE, exclude_data = FALSE,
53 include_image = FALSE;
55 void
56 command_args(gint argc,
57 gchar *argv[],
58 gchar fname[GPIV_MAX_CHARS]
60 /*-----------------------------------------------------------------------------
61 * Command line argument handling
64 gchar c = '\0';
65 gint argc_next;
68 while (--argc > 0 && (*++argv)[0] == '-') {
71 * argc_next is set to 1 if the next cmd line argument has to be searched for;
72 * in case that the command line argument concerns more than one char or cmd
73 * line argument needs a parameter
75 argc_next = 0;
76 while (argc_next == 0 && (c = *++argv[0]))
78 switch (c) {
79 case 'v':
80 #ifdef GIT_HASH
81 printf ("git hash: %s\n", GIT_REV);
82 #else
83 printf ("version: %s\n", GPIVTOOLS_VERSION);
84 #endif
85 exit(0);
86 break;
87 case 'V':
88 verbose = TRUE;
89 break;
90 case 'e':
91 exclude_data = TRUE;
92 break;
93 case 'h':
94 printf("%s\n", argv[0]);
95 printf("%s\n",HELP);
96 printf("%s\n",USAGE);
97 exit(0);
98 break;
99 case 'i':
100 include_image = TRUE;
101 break;
102 case 'p':
103 print_par = TRUE;
104 break;
107 * long option keys
109 case '-':
110 if (strcmp("-help", *argv) == 0) {
111 printf("\n%s", argv[0]);
112 printf("\n%s", HELP);
113 printf("\n%s", USAGE);
114 exit(0);
115 } else if (strcmp("-print", *argv) == 0) {
116 print_par = TRUE;
117 } else if (strcmp("-version", *argv) == 0) {
118 #ifdef GIT_HASH
119 printf ("git hash: %s\n", GIT_REV);
120 #else
121 printf ("version: %s\n", GPIVTOOLS_VERSION);
122 #endif
123 exit(0);
124 } else if (strcmp("-verbose", *argv) == 0) {
125 verbose = TRUE;
126 } else {
127 gpiv_error("%s: unknown option: %s", argv[0], *argv);
129 argc_next = 1;
130 break;
132 default:
133 gpiv_error(USAGE);
134 break;
138 if(argc != 1) {
139 gpiv_error("%s: %s", argv[0], USAGE);
141 strcpy(fname, argv[0]);
147 gchar *
148 make_fname(gchar *fname_in,
149 gchar *fname_img,
150 gchar *fname_vor,
151 gchar *fname_nstrain,
152 gchar *fname_sstrain,
153 gchar *fname_out,
154 gchar *fname_parameter
156 /*-----------------------------------------------------------------------------
157 * generates filenames
160 gchar *err_msg = NULL;
161 gchar *fname_base = NULL;
163 if (fname_in == NULL ) {
164 err_msg = "make_fname: \"fname_in == NULL\"";
165 return (err_msg);
169 * Stripping filename
171 fname_base = g_strdup(fname_in);
172 strtok(fname_base, ".");
175 * filenames for output PIV data
177 if (include_image) {
178 gpiv_io_make_fname(fname_base, GPIV_EXT_PNG_IMAGE, fname_img);
179 if (verbose) printf("# Image data file: %s\n", fname_img);
182 if (exclude_data == FALSE) {
184 gpiv_io_make_fname(fname_base, GPIV_EXT_VOR, fname_vor);
185 if (verbose) printf("# Input data file: %s\n", fname_vor);
187 gpiv_io_make_fname(fname_base, GPIV_EXT_NSTR, fname_nstrain);
188 if (verbose) printf("# Input data file: %s\n", fname_nstrain);
190 gpiv_io_make_fname(fname_base, GPIV_EXT_SSTR, fname_sstrain);
191 if (verbose) printf("# Input data file: %s\n", fname_sstrain);
195 gpiv_io_make_fname(fname_base, GPIV_EXT_GPIV, fname_out);
196 if (verbose) printf("# Output file: %s\n",fname_out);
198 gpiv_io_make_fname(fname_base, GPIV_EXT_PAR, fname_parameter);
200 g_free (fname_base);
201 return (err_msg);
210 int
211 main(int argc,
212 char *argv[]
214 /*-----------------------------------------------------------------------------
215 * main routine to converts ASCII PIV-data to hdf5 formatted data
218 gchar *err_msg = NULL;
219 FILE *fp = NULL;
220 gchar fname_piv[GPIV_MAX_CHARS],
221 fname_out[GPIV_MAX_CHARS],
222 fname_vor[GPIV_MAX_CHARS],
223 fname_nstrain[GPIV_MAX_CHARS],
224 fname_sstrain[GPIV_MAX_CHARS],
225 fname_img[GPIV_MAX_CHARS],
226 fname_parameter[GPIV_MAX_CHARS];
228 /* time_t itime; */
229 /* gchar c_line[GPIV_MAX_LINES_C][GPIV_MAX_CHARS]; */
230 /* gint nc_lines=0, var_scale=0; */
232 GpivPivPar *piv_par = g_new0 (GpivPivPar, 1);
233 GpivValidPar *valid_par = g_new0 (GpivValidPar, 1);
234 GpivPostPar *post_par = g_new0 (GpivPostPar, 1);
236 GpivImage *image = NULL;
237 GpivPivData *piv_data = NULL;
238 GpivScalarData *sc_data = NULL;
240 gboolean stored__post_par = FALSE;
243 * Initializing parameters
245 gpiv_piv_parameters_set(piv_par, FALSE);
246 gpiv_valid_parameters_set(valid_par, FALSE);
247 gpiv_post_parameters_set(post_par, FALSE);
250 command_args(argc, argv, fname_piv);
251 if ((err_msg =
252 make_fname(fname_piv, fname_img, fname_vor, fname_nstrain,
253 fname_sstrain, fname_out, fname_parameter))
254 != NULL) {
255 gpiv_error ("%s: %s\n", argv[0], err_msg);
259 * Reads piv program parameters from fname_parameter
261 gpiv_scan_parameter (GPIV_PIVPAR_KEY, fname_parameter, piv_par,
262 print_par);
263 gpiv_scan_parameter (GPIV_VALIDPAR_KEY, fname_parameter, valid_par,
264 print_par);
265 gpiv_scan_parameter (GPIV_POSTPAR_KEY, fname_parameter, post_par,
266 print_par);
268 #undef USE_RESOURCES
269 #ifdef USE_RESOURCES
270 if ((err_msg =
271 gpiv_scan_resourcefiles (GPIV_PIVPAR_KEY, piv_par, print_par))
272 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
273 if ((err_msg =
274 gpiv_scan_resourcefiles (GPIV_VALIDPAR_KEY, valid_par, print_par))
275 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
276 if ((err_msg =
277 gpiv_scan_resourcefiles (GPIV_POSTPAR_KEY, post_par, print_par))
278 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
279 #endif /* USE_RESOURCES */
282 * writing to output int hdf5
284 if ((err_msg = gpiv_fcreate_hdf5 (fname_out)) != NULL) {
285 gpiv_error ("%s: %s", argv[0], err_msg);
290 * Image
292 if (include_image) {
293 if ((fp = fopen(fname_img, "r")) != NULL) {
294 if ((image = gpiv_read_png_image (fp)) == NULL) {
295 fclose(fp);
296 gpiv_error ("%s: faling gpiv_read_png_image", argv[0]);
298 fclose(fp);
299 fp = NULL;
301 if ((err_msg = gpiv_fwrite_hdf5_image (fname_out, image, TRUE))
302 != NULL) {
303 gpiv_error ("%s: %s", argv[0], err_msg);
309 if (exclude_data == FALSE) {
311 * Piv data, PIV and validation parameters
313 if ((fp = fopen (fname_piv, "r")) != NULL) {
314 if ((piv_data = gpiv_read_pivdata (fp)) == NULL) {
315 fclose(fp);
316 gpiv_error ("%s: faling gpiv_read_pivdata", argv[0]);
318 fclose(fp);
319 fp = NULL;
321 if ((err_msg = gpiv_fwrite_hdf5_pivdata (fname_out, piv_data, "PIV",
322 TRUE))
323 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
327 if (piv_par != NULL) {
328 if ((err_msg =
329 gpiv_piv_fwrite_hdf5_parameters (fname_out, piv_par))
330 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
333 if (valid_par != NULL) {
334 if ((err_msg =
335 gpiv_valid_fwrite_hdf5_parameters (fname_out, valid_par))
336 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
342 * Scalar data: vorticity
344 if ((fp = fopen (fname_vor, "r")) != NULL) {
345 if ((sc_data = gpiv_read_scdata (fp)) == NULL) {
346 fclose(fp);
347 gpiv_error ("%s: faling gpiv_read_scdata", argv[0]);
349 fclose(fp);
350 fp = NULL;
352 if ((err_msg = gpiv_fwrite_hdf5_scdata (fname_out, sc_data,
353 "VORTICITY", TRUE))
354 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
357 if (post_par != NULL && stored__post_par == FALSE) {
358 stored__post_par = TRUE;
359 if ((err_msg =
360 gpiv_post_fwrite_hdf5_parameters (fname_out, post_par))
361 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
366 * Scalar data: sstrain
368 if ((fp = fopen (fname_sstrain, "r")) != NULL) {
370 if ((sc_data = gpiv_read_scdata (fp)) == NULL) {
371 fclose(fp);
372 gpiv_error ("%s: faling gpiv_read_scdata", argv[0]);
374 fclose(fp);
375 fp = NULL;
377 if ((err_msg =
378 gpiv_fwrite_hdf5_scdata(fname_out, sc_data,
379 "SHEAR_STRAIN", TRUE))
380 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
382 if (post_par != NULL && stored__post_par == FALSE) {
383 stored__post_par = TRUE;
384 if ((err_msg =
385 gpiv_post_fwrite_hdf5_parameters (fname_out, post_par))
386 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
391 * Scalar data: normal strain
393 if((fp=fopen(fname_nstrain, "r")) != NULL) {
394 if ((sc_data = gpiv_read_scdata (fp)) == NULL) {
395 fclose(fp);
396 gpiv_error ("%s: faling gpiv_read_scdata", argv[0]);
398 fclose(fp);
399 fp = NULL;
402 if ((err_msg =
403 gpiv_fwrite_hdf5_scdata(fname_out, sc_data,
404 "NORMAL_STRAIN", TRUE))
405 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
407 if (post_par != NULL && stored__post_par == FALSE) {
408 stored__post_par = TRUE;
409 if ((err_msg =
410 gpiv_post_fwrite_hdf5_parameters (fname_out, post_par))
411 != NULL) gpiv_error ("%s: %s", argv[0], err_msg);
417 exit(0);