show git hash (if available) when invoking -v key
[gpivtools.git] / src / misc / hdf2piv.c
blob8f7dad9987a5870187a6d0cdb66820a2cc8e651f
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */
3 /*-----------------------------------------------------------------------------
5 hdf2piv - converts hdf5 PIV data ASCII 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 <assert.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: hdf2piv [-e] [-h | --help] [-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 -p | --print: print parameters to stdout \n\
43 -v | --version: version number \n\
44 -V | --verbose: version number \n\
45 filename: input file, hdf formatted (including .hdf extension) \n\
48 #define HELP "\
49 hdf2piv - converts hdf5 PIV-data to ASCII data"
51 gboolean print_par = FALSE, verbose = FALSE, exclude_data = FALSE;
54 void
55 command_args(gint argc,
56 gchar *argv[],
57 gchar fname[GPIV_MAX_CHARS]
59 /* ----------------------------------------------------------------------------
60 * Command line argument handling
63 gchar c = '\0';
64 gint argc_next;
66 while (--argc > 0 && (*++argv)[0] == '-') {
69 * argc_next is set to 1 if the next cmd line argument has to be searched for;
70 * in case that the command line argument concerns more than one char or cmd
71 * line argument needs a parameter
74 argc_next = 0;
75 while (argc_next == 0 && (c = *++argv[0]))
77 switch (c) {
78 case 'v':
79 #ifdef GIT_HASH
80 printf ("git hash: %s\n", GIT_REV);
81 #else
82 printf ("version: %s\n", GPIVTOOLS_VERSION);
83 #endif
84 exit(0);
85 break;
86 case 'V':
87 verbose = TRUE;
88 break;
89 case 'e':
90 exclude_data = TRUE;
91 break;
92 case 'h':
93 printf("%s\n", argv[0]);
94 printf("%s\n",HELP);
95 printf("%s\n",USAGE);
96 exit(0);
97 break;
98 case 'p':
99 print_par = TRUE;
100 break;
103 * long option keys
105 case '-':
106 if (strcmp("-help", *argv) == 0) {
107 printf("\n%s", argv[0]);
108 printf("\n%s", HELP);
109 printf("\n%s", USAGE);
110 exit(0);
111 } else if (strcmp("-print", *argv) == 0) {
112 print_par = TRUE;
113 } else if (strcmp("-version", *argv) == 0) {
114 #ifdef GIT_HASH
115 printf ("git hash: %s\n", GIT_REV);
116 #else
117 printf ("version: %s\n", GPIVTOOLS_VERSION);
118 #endif
119 exit(0);
120 } else if (strcmp("-verbose", *argv) == 0) {
121 verbose = TRUE;
122 } else {
123 gpiv_error("%s: unknown option: %s", argv[0], *argv);
125 argc_next = 1;
126 break;
128 default:
129 gpiv_error(USAGE);
130 break;
134 if (argc == 1) {
135 strcpy (fname, argv[argc - 1]);
136 } else {
137 gpiv_error ("%s: %s", argv[0], USAGE);
144 gchar *
145 make_fname(gchar *fname_in,
146 gchar *fname_out_img,
147 gchar *fname_out_piv,
148 gchar *fname_out_vor,
149 gchar *fname_out_nstrain,
150 gchar *fname_out_sstrain,
151 gchar *fname_out_par
153 /* ----------------------------------------------------------------------------
154 * define filenames
157 gchar *err_msg = NULL;
158 gchar *fname_base = NULL;
160 if (fname_in == NULL ) {
161 err_msg = "make_fname: \"fname_in == NULL\"";
162 return (err_msg);
166 * Stripping filename
168 fname_base = g_strdup (fname_in);
169 strtok (fname_base, ".");
172 * filenames for output PIV data
174 gpiv_io_make_fname (fname_base, GPIV_EXT_RAW_IMAGE, fname_out_img);
175 if (verbose) printf("# Output data file: %s\n", fname_out_img);
177 if (exclude_data == FALSE) {
178 gpiv_io_make_fname(fname_base, GPIV_EXT_PIV, fname_out_piv);
179 if (verbose) printf("# Output data file: %s\n",fname_out_piv);
181 gpiv_io_make_fname(fname_base, GPIV_EXT_VOR, fname_out_vor);
182 if (verbose) printf("# Input data file: %s\n",fname_out_vor);
184 gpiv_io_make_fname(fname_base, GPIV_EXT_NSTR, fname_out_nstrain);
185 if (verbose) printf("# Input data file: %s\n",
186 fname_out_nstrain);
188 gpiv_io_make_fname(fname_base, GPIV_EXT_SSTR, fname_out_sstrain);
189 if (verbose) printf("# Input data file: %s\n",
190 fname_out_sstrain);
192 gpiv_io_make_fname(fname_base, GPIV_EXT_PAR, fname_out_par);
193 if (verbose) printf("# Input parameter file: %s\n",
194 fname_out_par);
196 g_free (fname_base);
197 return (err_msg);
202 int
203 main(int argc,
204 char *argv[]
206 /* ----------------------------------------------------------------------------
209 FILE *fp_out = NULL;
210 gchar *err_msg = NULL;
211 gchar fname_in[GPIV_MAX_CHARS],
212 fname_out_img[GPIV_MAX_CHARS],
213 fname_out_piv[GPIV_MAX_CHARS],
214 fname_out_vor[GPIV_MAX_CHARS],
215 fname_out_nstrain[GPIV_MAX_CHARS],
216 fname_out_sstrain[GPIV_MAX_CHARS],
217 fname_out_par[GPIV_MAX_CHARS];
219 GpivImage *image = NULL;
220 GpivPivData *piv_data = NULL;
221 GpivScalarData *sc_data;
223 GpivPivPar *piv_par = NULL;
224 GpivValidPar *valid_par = NULL;
225 GpivPostPar *post_par = NULL;
228 * Initializing parameters
230 command_args (argc, argv, fname_in);
231 if ((err_msg =
232 make_fname (fname_in, fname_out_img, fname_out_piv, fname_out_vor,
233 fname_out_nstrain, fname_out_sstrain,
234 fname_out_par))
235 != NULL) {
236 gpiv_error ("%s: %s\n", argv[0], err_msg);
240 * reading input from hdf5
242 * image data
244 if ((image = gpiv_fread_hdf5_image (fname_in)) == NULL) {
245 gpiv_error ("%s: %s", argv[0], err_msg);
248 if ((fp_out = fopen (fname_out_img, "wb")) == NULL) {
249 gpiv_error("%s error: failure opening %s for output",
250 argv[0], fname_out_img);
252 gpiv_write_png_image (fp_out, image, TRUE);
253 fclose(fp_out);
256 * piv data
258 if (exclude_data == FALSE) {
259 if ((piv_data = gpiv_fread_hdf5_pivdata (fname_in, "PIV" )) == NULL) {
260 gpiv_error ("%s: failing gpiv_fread_hdf5_pivdata", argv[0]);
263 if ((fp_out = fopen (fname_out_piv, "w")) == NULL) {
264 gpiv_error("%s error: failure opening %s for output",
265 argv[0], fname_out_piv);
268 if ((err_msg = gpiv_write_pivdata (fp_out, piv_data, TRUE))
269 != NULL) {
270 gpiv_error ("%s: %s", argv[0], err_msg);
272 fclose(fp_out);
275 * scalar data: vorticity
277 if ((sc_data = gpiv_fread_hdf5_scdata (fname_in, "VORTICITY" ))
278 == NULL) {
279 gpiv_error ("%s: failing gpiv_fread_hdf5_scdata", argv[0]);
282 if ((fp_out = fopen (fname_out_vor, "w")) == NULL) {
283 gpiv_error("%s error: failure opening %s for output",
284 argv[0], fname_out_vor);
287 if ((err_msg = gpiv_write_scdata (fp_out, sc_data, TRUE))
288 != NULL) {
289 gpiv_error ("%s: %s", argv[0], err_msg);
291 fclose(fp_out);
294 * scalar data: normal strain
296 if ((sc_data = gpiv_fread_hdf5_scdata (fname_in, "NORMAL STRAIN" ))
297 == NULL) {
298 gpiv_error ("%s: failing gpiv_fread_hdf5_scdata", argv[0]);
301 if ((fp_out = fopen (fname_out_nstrain, "w")) == NULL) {
302 gpiv_error("%s error: failure opening %s for output",
303 argv[0], fname_out_vor);
306 if ((err_msg = gpiv_write_scdata (fp_out, sc_data, TRUE))
307 != NULL) {
308 gpiv_error ("%s: %s", argv[0], err_msg);
310 fclose(fp_out);
313 * scalar data: shear strain
315 if ((sc_data = gpiv_fread_hdf5_scdata (fname_in, "SHEAR STRAIN" ))
316 == NULL) {
317 gpiv_error ("%s: failing gpiv_fread_hdf5_scdata", argv[0]);
320 if ((fp_out = fopen (fname_out_sstrain, "w")) == NULL) {
321 gpiv_error("%s error: failure opening %s for output",
322 argv[0], fname_out_vor);
325 if ((err_msg = gpiv_write_scdata (fp_out, sc_data, TRUE))
326 != NULL) {
327 gpiv_error ("%s: %s", argv[0], err_msg);
329 fclose(fp_out);
332 * parameters
334 if ((piv_par =
335 gpiv_piv_fread_hdf5_parameters (fname_in))
336 == NULL) {
337 gpiv_error ("%s: failing gpiv_piv_fread_hdf5_parameters", argv[0]);
340 if ((valid_par =
341 gpiv_valid_fread_hdf5_parameters (fname_in))
342 == NULL) {
343 gpiv_error ("%s: failing gpiv_valid_fread_hdf5_parameters", argv[0]);
346 if ((post_par =
347 gpiv_post_fread_hdf5_parameters(fname_in))
348 == NULL) {
349 gpiv_error ("%s: failing gpiv_post_fread_hdf5_parameters", argv[0]);
353 if ((fp_out = fopen (fname_out_par, "w")) == NULL) {
354 gpiv_error("%s error: failure opening %s for output",
355 argv[0], fname_out_par);
357 gpiv_img_print_parameters (fp_out, image->header);
358 gpiv_piv_print_parameters (fp_out, piv_par);
359 gpiv_valid_print_parameters (fp_out, valid_par);
360 gpiv_post_print_parameters (fp_out, post_par);
361 fclose (fp_out);
363 if (print_par) {
364 gpiv_img_print_parameters (NULL, image->header);
365 gpiv_piv_print_parameters (NULL, piv_par);
366 gpiv_valid_print_parameters (NULL, valid_par);
367 gpiv_post_print_parameters (NULL, post_par);
372 exit (0);