File: h5tovtk.c

package info (click to toggle)
h5utils 1.9.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 756 kB
  • ctags: 150
  • sloc: ansic: 3,227; sh: 967; makefile: 87; cpp: 61
file content (392 lines) | stat: -rw-r--r-- 11,095 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
/* Copyright (c) 2004 Massachusetts Institute of Technology
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

#include <unistd.h>

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif

#include "config.h"
#include "arrayh5.h"
#include "copyright.h"
#include "h5utils.h"

#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5tovtk error: %s\n", msg); exit(EXIT_FAILURE); } }

void usage(FILE *f)
{
     fprintf(f, "Usage: h5tovtk [options] [<filenames>]\n"
	     "Options:\n"
	     "         -h : this help message\n"
             "         -V : print version number and copyright\n"
	     "         -v : verbose output\n"
	     "  -o <file> : output datasets from all input files to <file>;\n"
	     "              combines 3 datasets to a vector, and 2 or 4+ to a field\n"
	     "         -4 : 4-byte floating-point binary output (default)\n"
	     "         -a : ASCII floating-point output\n"
	     "         -1 : 1-byte (0-255) binary output\n"
	     "         -2 : 2-byte (0-65535) binary output\n"
	     "         -n : don't convert binary output to bigendian\n"
	     "   -m <min> : set bottom of scale for 1/2 byte encoding\n"
	     "   -M <max> : set top of scale for 1/2 byte encoding\n"
	     "         -Z : center scale at zero for 1/2 byte encoding\n"
	     "         -r : invert scale & data values\n"
	     "    -x <ix> : take x=<ix> slice of data\n"
	     "    -y <iy> : take y=<iy> slice of data\n"
	     "    -z <iz> : take z=<iz> slice of data\n"
	     "    -t <it> : take t=<it> slice of data's last dimension\n"
	     "         -0 : use dataset center as origin for -x/-y/-z\n"
	     "  -d <name> : use dataset <name> in the input files (default: first dataset)\n"
	     "              -- you can also specify a dataset via <filename>:<name>\n"
	  );
}

static void whitespace_to_underscores(char *s)
{
     while (*s) {
	  if (isspace(*s))
	       *s = '-';
	  ++s;
     }
}

static const char vtk_datatype[][20] = { 
     "float", "unsigned_char", "unsigned_short", "none", "float"
};

static void write_vtk_header(FILE *f, int is_binary,
			     int nx, int ny, int nz,
			     double ox, double oy, double oz,
			     double sx, double sy, double sz)
{
     fprintf(f, 
	     "# vtk DataFile Version 2.0\n"
	     "Generated by h5tovtk.\n"
	     "%s\n"
	     "DATASET STRUCTURED_POINTS\n"
	     "DIMENSIONS %d %d %d\n"
	     "ORIGIN %g %g %g\n"
	     "SPACING %g %g %g\n",
	     is_binary ? "BINARY" : "ASCII",
	     nz, ny, nx,
	     oz, oy, ox, sz, sy, sx);
}

static void write_vtk_value(FILE *f, double v, int store_bytes, int fix_bytes,
			    double min, double max, int invert)
{
     if (invert)
	  v = max - (v - min);
     switch (store_bytes) {
	 case 0:
	      fprintf(f, "%g ", v);
	      break;
	 case 1:
	 {
	      unsigned char c;
	      c = floor((v - min) * 255.0 / (max - min) + 0.5);
	      fwrite(&c, 1, 1, f);
	      break;
	 }
	 case 2:
	 {
	      unsigned short i;
	      i = floor((v - min) * 65535.0 / (max - min) + 0.5);
	      if (fix_bytes) {
#if defined(HAVE_HTONS)
		   i = htons(i);
#elif ! defined(WORDS_BIGENDIAN)
		   unsigned char swap, *bytes;
		   bytes = (unsigned char *) &i;
		   swap = bytes[0]; bytes[0] = bytes[1]; bytes[1] = swap;
#endif
	      }
	      fwrite(&i, 2, 1, f);
	      break;
	 }
	 case 4:
	 {
	      float fv = v;
	      if (fix_bytes) {
#if defined(HAVE_HTONL) && (SIZEOF_FLOAT == SIZEOF_UNSIGNED_LONG)
		   unsigned long *i = (unsigned long *) &fv;
		   *i = htonl(*i);
#elif ! defined(WORDS_BIGENDIAN)
		   unsigned char swap, *bytes;
		   bytes = (unsigned char *) &fv;
		   swap = bytes[0]; bytes[0] = bytes[3]; bytes[3] = swap;
		   swap = bytes[1]; bytes[1] = bytes[2]; bytes[2] = swap;
#endif
	      }
	      fwrite(&fv, 4, 1, f);
	      break;
	 }
     }
}

int main(int argc, char **argv)
{
     arrayh5 *a = NULL;
     char *vtk_fname = NULL, *data_name = NULL;
     extern char *optarg;
     extern int optind;
     double ox = 0, oy = 0, oz = 0, sx = 1, sy = 1, sz = 1;
     int c, ifile;
     int zero_center = 0;
     int invert = 0;
     double min = 0, max = 0;
     int min_set = 0, max_set = 0;
     int verbose = 0, combine = 0;
     int slicedim[4] = {NO_SLICE_DIM,NO_SLICE_DIM,NO_SLICE_DIM,NO_SLICE_DIM};
     int islice[4], center_slice[4] = {0,0,0,0};
     int nx = 0, ny = 0, nz = 0, na;
     int store_bytes = 4, fix_byte_order = 1;

     while ((c = getopt(argc, argv, "ho:d:vV124mMZranx:y:z:t:0")) != -1)
	  switch (c) {
	      case 'h':
		   usage(stdout);
		   return EXIT_SUCCESS;
	      case 'V':
		   printf("h5tovtk " PACKAGE_VERSION " by Steven G. Johnson\n" 
			  COPYRIGHT);
		   return EXIT_SUCCESS;
	      case 'v':
		   verbose = 1;
		   break;
	      case 'x':
		   islice[0] = atoi(optarg);
		   slicedim[0] = 0;
		   break;
	      case 'y':
		   islice[1] = atoi(optarg);
		   slicedim[1] = 1;
		   break;
	      case 'z':
		   islice[2] = atoi(optarg);
		   slicedim[2] = 2;
		   break;
	      case 't':
		   islice[3] = atoi(optarg);
		   slicedim[3] = LAST_SLICE_DIM;
		   break;
              case '0':
                   center_slice[0] = center_slice[1] = center_slice[2] = 1;
                   break;
	      case 'n':
		   fix_byte_order = 0;
		   break;
	      case 'a':
		   store_bytes = 0; /* ascii */
		   break;
	      case '1':
		   store_bytes = 1;
		   break;
	      case '2':
		   store_bytes = 2;
		   break;
	      case '4':
		   store_bytes = 4;
		   break;
	      case 'm':
		   min = atof(optarg);
		   min_set = 1;
		   break;
	      case 'M':
		   max = atof(optarg);
		   max_set = 1;
		   break;
	      case 'Z':
		   zero_center = 1;
		   break;
	      case 'r':
		   invert = 1;
		   break;
	      case 'o':
		   vtk_fname = my_strdup(optarg);
		   combine = 1;
		   break;
	      case 'd':
		   data_name = my_strdup(optarg);
		   break;		   
	      default:
		   fprintf(stderr, "Invalid argument -%c\n", c);
		   usage(stderr);
		   return EXIT_FAILURE;
	  }
     if (optind == argc) {  /* no parameters left */
	  usage(stderr);
	  return EXIT_FAILURE;
     }

     CHECK(store_bytes != 4 || sizeof(float) == 4, 
	   "'float' is wrong size for -4");
     CHECK(store_bytes != 2 || sizeof(unsigned short) == 4, 
	   "'short' is wrong size for -2");
     
     a = (arrayh5*) malloc(sizeof(arrayh5) * (na = argc - optind));
     CHECK(a, "out of memory");

     combine = combine && (na > 1);

     for (ifile = optind; ifile < argc; ++ifile) {
          char *dname, *found_dname, *h5_fname;
	  int err, ia = ifile - optind;
          h5_fname = split_fname(argv[ifile], &dname);
          if (!dname[0])
               dname = data_name;

	  err = arrayh5_read(&a[ia], h5_fname, dname, &found_dname,
			     4, slicedim, islice, center_slice);
	  CHECK(!err, arrayh5_read_strerror[err]);
	  CHECK(a[ia].rank >= 1, "data must have at least one dimension");
	  CHECK(a[ia].rank <= 3, "data can have at most 3 dimensions (try taking a slice");
	  
	  CHECK(!combine || !ia || arrayh5_conformant(a[ia], a[0]),
		"all arrays must be conformant to combine them");
	  
	  if (!vtk_fname)
	       vtk_fname = replace_suffix(h5_fname, ".h5", ".vtk");

	  {
	       double a_min, a_max;
	       arrayh5_getrange(a[ia], &a_min, &a_max);
	       if (verbose)
		    printf("data in %s ranges from %g to %g.\n", 
			   h5_fname, a_min, a_max);
	       if (!min_set)
		    min = (!combine || !ia || a_min < min) ? a_min : min;
	       if (!max_set)
		    max = (!combine || !ia || a_max > max) ? a_max : max;
	       if (min > max) {
		    invert = !invert;
		    a_min = min;
		    min = max;
		    max = a_min;
	       }
	       if (zero_center) {
		    max = fabs(max) > fabs(min) ? fabs(max) : fabs(min);
		    min = -max;
	       }
	  }
	  
	  nx = a[ia].dims[0];
	  ny = a[ia].rank < 2 ? 1 : a[ia].dims[1];
	  nz = a[ia].rank < 3 ? 1 : a[ia].dims[2];
	  
	  if (!combine) {
	       FILE *f;
	       int i, N = nx * ny * nz;

	       if (verbose)
		    printf("writing \"%s\" from %dx%dx%d input data.\n",
			   vtk_fname, nx, ny, nz);
	       
	       if (strcmp(vtk_fname, "-")) {
		    f = fopen(vtk_fname, "w");
		    CHECK(f, "error creating file");
	       }
	       else
		    f = stdout;

	       write_vtk_header(f, store_bytes, 
				nx, ny, nz, ox, oy, oz, sx, sy, sz);
	       whitespace_to_underscores(found_dname);
	       fprintf(f, "POINT_DATA %d\n"
		       "SCALARS %s %s 1\n"
		       "LOOKUP_TABLE default\n",
		       N, found_dname, vtk_datatype[store_bytes]);
	       
	       for (i = 0; i < N; ++i)
		    write_vtk_value(f, a[ia].data[i], store_bytes,
				    fix_byte_order, min, max, invert);
	  
	       if (f != stdout)
		    fclose(f);
	       arrayh5_destroy(a[ia]);
	       free(vtk_fname); vtk_fname = NULL;
	  }
	  free(found_dname);
	  free(h5_fname);
     }

     if (combine) {
	  FILE *f;
	  int i, N = nx * ny * nz;

	  if (verbose)
	       printf("writing \"%s\" from %dx%dx%d input data.\n",
		      vtk_fname, nx, ny, nz);
	  
	  if (strcmp(vtk_fname, "-")) {
	       f = fopen(vtk_fname, "w");
	       CHECK(f, "error creating file");
	  }
	  else
	       f = stdout;
	  
	  write_vtk_header(f, store_bytes, 
			   nx, ny, nz, ox, oy, oz, sx, sy, sz);
	  fprintf(f, "POINT_DATA %d\n", N);
	  switch (na) {
	      case 1:
		   fprintf(f, "SCALARS scalars %s 1\nLOOKUP_TABLE default\n", 
			   vtk_datatype[store_bytes]);
		   break;
	      case 3:
		   fprintf(f, "VECTORS vectors %s\n", 
			   vtk_datatype[store_bytes]);
		   break;
	      default:
		   fprintf(f, "FIELD fields 1\narray %d %d %s\n", 
			   na, N, vtk_datatype[store_bytes]);
	  }
	  
	  for (i = 0; i < N; ++i) {
	       int ia;
	       for (ia = 0; ia < na; ++ia)
		    write_vtk_value(f, a[ia].data[i], store_bytes, 
				    fix_byte_order, min, max, invert);
	  }
	  if (f != stdout)
	       fclose(f);
	  {
	       int ia;
	       for (ia = 0; ia < na; ++ia)
		    arrayh5_destroy(a[ia]);
	  }
     }

     free(a);

     if (data_name)
	  free(data_name);

     return EXIT_SUCCESS;
}