1 /* Copyright (c) 1999-2017 Massachusetts Institute of Technology
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include <unistd.h>
28 
29 #include "config.h"
30 #include "arrayh5.h"
31 #include "arrayh4.h"
32 #include "copyright.h"
33 #include "h5utils.h"
34 
35 #define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h4fromh5 error: %s\n", msg); exit(EXIT_FAILURE); } }
36 
usage(FILE * f)37 void usage(FILE *f)
38 {
39      fprintf(f, "Usage: h4fromh5 [options] [<hdf4-files>]\n"
40 	     "Options:\n"
41 	     "         -h : this help message\n"
42              "         -V : print version number and copyright\n"
43 	     "         -v : verbose output\n"
44 	     "         -T : transposed output\n"
45 	     "  -o <file> : output to HDF4 file <file>\n"
46 	     "  -d <name> : use dataset <name> in the input file\n"
47 	     "              -- you can also specify a dataset via <file>:<name>\n"
48 	  );
49 }
50 
main(int argc,char ** argv)51 int main(int argc, char **argv)
52 {
53      char *h4_fname = NULL;
54      char *data_name = NULL;
55      extern char *optarg;
56      extern int optind;
57      int c;
58      int ifile;
59      int verbose = 0, transpose = 0;
60 
61      while ((c = getopt(argc, argv, "hd:vTo:V")) != -1)
62 	  switch (c) {
63 	      case 'h':
64 		   usage(stdout);
65 		   return EXIT_SUCCESS;
66 	      case 'V':
67 		   printf("h4fromh5 " PACKAGE_VERSION " by Steven G. Johnson\n"
68 			  COPYRIGHT);
69 		   return EXIT_SUCCESS;
70 	      case 'v':
71 		   verbose = 1;
72 		   break;
73 	      case 'T':
74 		   transpose = 1;
75 		   break;
76 	      case 'd':
77 		   free(data_name);
78 		   data_name = my_strdup(optarg);
79 		   break;
80 	      case 'o':
81 		   free(h4_fname);
82 		   h4_fname = my_strdup(optarg);
83 		   break;
84 	      default:
85 		   fprintf(stderr, "Invalid argument -%c\n", c);
86 		   usage(stderr);
87 		   return EXIT_FAILURE;
88 	  }
89      if (optind == argc) {  /* no parameters left */
90 	  usage(stderr);
91 	  return EXIT_FAILURE;
92      }
93 
94      if (h4_fname && optind + 1 < argc) {
95 	  fprintf(stderr, "h4fromh5: only one .h5 file can be used with -o\n");
96 	  return EXIT_FAILURE;
97      }
98 
99      for (ifile = optind; ifile < argc; ++ifile) {
100 	  char *h5_fname, *dname;
101 	  arrayh4 a4;
102 	  int i, err;
103 	  int32 dims_copy[ARRAYH4_MAX_RANK];
104 	  char *cur_h4_fname = h4_fname;
105 	  arrayh5 a;
106 
107 	  h5_fname = split_fname(argv[ifile], &dname);
108 	  if (!dname[0])
109                dname = data_name;
110 
111 	  if (!cur_h4_fname)
112 	       cur_h4_fname = replace_suffix(h5_fname, ".h5", ".hdf");
113 
114 	  if (verbose)
115 	       printf("Reading HDF5 input file \"%s\"...\n", h5_fname);
116 	  err = arrayh5_read(&a, h5_fname, dname, NULL, 0, 0, 0, 0);
117 	  CHECK(!err, arrayh5_read_strerror[err]);
118 
119 	  if (transpose)
120 	       arrayh5_transpose(&a);
121 
122 	  CHECK(a.rank <= ARRAYH4_MAX_RANK, "HDF5 rank is too big");
123 	  for (i = 0; i < a.rank; ++i)
124 	       dims_copy[i] = a.dims[i];
125 
126 	  CHECK(arrayh4_create(&a4, DFNT_FLOAT64, a.rank, dims_copy),
127 		"error allocating HDF4 data");
128 
129 	  for (i = 0; i < a.N; ++i)
130 	       a4.p.d[i] = a.data[i];
131 
132 	  if (verbose) {
133 	       double a_min, a_max;
134 	       arrayh5_getrange(a, &a_min, &a_max);
135 	       printf("data ranges from %g to %g.\n", a_min, a_max);
136 	  }
137 
138 	  if (verbose) {
139 	       int i;
140 	       printf("Writing size %d", a.dims[0]);
141 	       for (i = 1; i < a.rank; ++i)
142 		    printf("x%d", a.dims[i]);
143 	       printf(" data to %s\n", cur_h4_fname);
144 	  }
145 
146 	  arrayh5_destroy(a);
147 
148 	  arrayh4_write(cur_h4_fname, a4);
149 	  arrayh4_destroy(a4);
150 
151 	  if (h4_fname != cur_h4_fname)
152 	       free(cur_h4_fname);
153 	  free(h5_fname);
154      }
155 
156      return EXIT_SUCCESS;
157 }
158