aboutsummaryrefslogtreecommitdiff
path: root/src/util/hdf5_convert_from_ieeeio.c
blob: 5e3701962e762280d6c77596c1a696d61c3cee7e (plain)
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
 /*@@
   @file      hdf5_convert_from_ieeeio.c
   @date      Fri 14 Dec 2001
   @author    Thomas Radke
   @desc
              Utility program to convert IOFlexIO datafiles into IOHDF5
              datafiles.
   @enddesc
   @version   $Id$
 @@*/

#define  MAXDIM       3
#define  MAXNAMESIZE  100

#include <stdio.h>
#include <stdlib.h>


/* Cactus includes (defines CCTK_FILEVERSION) */
#include "cctk.h"

/* FlexIO includes */
#include "IOProtos.h"
#include "IEEEIO.h"

/* HDF5 include */
#include <hdf5.h>

#define GLOBAL_ATTRIBUTES_GROUP "Global Attributes"

/* the rcs ID and its dummy function to use it */
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusPUGHIO_IOHDF5_util_hdf5_convert_from_ieeeio_c)


int main (int argc, char **argv)
{
  int i, j;
  IOFile infile;
  int nDatasets, nAttributes;
  int ieeeDatatype, rank, dims[MAXDIM];
  int attrNumberType;
  Long attrLen;
  char attrName[MAXNAMESIZE];
  void *data, *attrData;
  hid_t outfile, dataset, dataspace, attribute, attrDataspace, hdf5Datatype;
  hid_t group, hdf5String;
  hsize_t hdf5Dims[MAXDIM];
  int iteration, timelevel;
  char ieeeDatasetName[MAXNAMESIZE], hdf5DatasetName[2 * MAXNAMESIZE];


  if (argc <= 2) {
    printf ("Usage: %s <inputfile> <outputfile>\n", argv[0]);
    printf ("   eg. %s foo.ieee bar.h5\n", argv[0]);
    return (0);
  }

  infile = IEEEopen (argv[1], "r");
  if (! IOisValid (infile)) {
    printf ("Could not open input file '%s'\n", argv[1]);
    return (-1);
  }

  outfile = H5Fcreate (argv[2], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  if (outfile < 0)
  {
    fprintf (stderr, "Could not create output file '%s'\n", argv[2]);
    return (-1);
  }

  /* add a dummy GLOBAL_ATTRIBUTES_GROUP so that the HDF5 file is recognized as
     unchunked Cactus data */
  group = H5Gcreate (outfile, GLOBAL_ATTRIBUTES_GROUP, 0);
  if (group < 0)
  {
    fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: group is out of bounds");
    return (-1);
  }
  attrDataspace = H5Screate (H5S_SCALAR);

  attribute = H5Acreate (group, "nprocs", H5T_NATIVE_INT, attrDataspace,
                         H5P_DEFAULT);
  if (attribute < 0)
  {
    fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: attribute is out of bounds");
    return (-1);
  }
  i = 1;
  if(! (H5Awrite (attribute, H5T_NATIVE_INT, &i) >= 0))
  {
    fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: error returned from H5Awrite");
    return (-1);
  }
  H5Aclose (attribute);

  attribute = H5Acreate (group, "ioproc_every", H5T_NATIVE_INT, attrDataspace,
                         H5P_DEFAULT);
  if (attribute < 0)
  {
    fprinft ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: attribute is out of bounds");
    return (-1);
  }
  i = 1;
  if(! (H5Awrite (attribute, H5T_NATIVE_INT, &i) >= 0))
  {
    fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: error returned from function H5Awrite");
    return (-1);
  }
  H5Aclose (attribute);

  attribute = H5Acreate (group, "unchunked", H5T_NATIVE_INT, attrDataspace,
                         H5P_DEFAULT);
  if (attribute < 0)
  {
    fprinft ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: attribute is out of bounds");
    return (-1);
  }
  i = 1;
  if(! (H5Awrite (attribute, H5T_NATIVE_INT, &i) >= 0))
  {
    fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: error returned from function H5Awrite");
    return (-1);
  }
  H5Aclose (attribute);

  H5Sclose (attrDataspace);
  H5Gclose (group);

  hdf5String = H5Tcopy (H5T_C_S1);

  nDatasets = IOnDatasets (infile);
  printf ("Input file contains %d datasets\n", nDatasets);

  for (i = 0; i < nDatasets; i++)
  {
    if (IOreadInfo (infile, &ieeeDatatype, &rank, dims, MAXDIM) <= 0)
    {
      fprintf (stderr, "Cannot read info of datatset %d, skipping ...\n", i);
      continue;
    }

    /* retrieve name and iteration attribute of dataset */
    strcpy (attrName, "name");
    j = IOreadAttributeInfo (infile, attrName, &attrNumberType, &attrLen);
    if (j < 0)
    {
      sprintf (ieeeDatasetName, "Dataset %d", i);
      fprintf (stderr,
               "Cannot find name attribute of dataset %d, using default name '%s' ...\n", i, ieeeDatasetName);
    }
    else if (IOreadAttribute (infile, j, ieeeDatasetName) < 0)
    {
      fprintf (stderr,
               "Cannot read name attribute of dataset %d, skipping ...\n", i);
      continue;
    }
    strcpy (attrName, "iteration");
    j = IOreadAttributeInfo (infile, attrName, &attrNumberType, &attrLen);
    if (j < 0)
    {
      iteration = 0;
      fprintf (stderr, "Cannot find iteration attribute of dataset %d, "
               "using default iteration number 0 ...\n", i);
    }
    else if (IOreadAttribute (infile, j, &iteration) < 0)
    {
      fprintf (stderr, "Cannot read iteration attribute of dataset %d, "
               "skipping ...\n", i);
      continue;
    }
    strcpy (attrName, "timelevel");
    j = IOreadAttributeInfo (infile, attrName, &attrNumberType, &attrLen);
    if (j < 0)
    {
      timelevel = 0;
      fprintf (stderr, "Cannot find timelevel attribute of dataset %d,\n"
               "assuming Cactus 3.x dataset with implicite timelevel 0 "
               "...\n", i);
    }
    else if (IOreadAttribute (infile, j, &timelevel) < 0)
    {
      fprintf (stderr, "Cannot read timelevel attribute of dataset %d, "
               "skipping ...\n", i);
      continue;
    }

    switch (ieeeDatatype)
    {
      case BYTE:
      case CHAR:
        hdf5Datatype = H5T_NATIVE_CHAR; break;

      case FLOAT32:
        hdf5Datatype = H5T_NATIVE_FLOAT; break;

      case FLOAT64:
        hdf5Datatype = H5T_NATIVE_DOUBLE; break;

      case INT32:
        hdf5Datatype = H5T_NATIVE_INT; break;

      default:
        fprintf (stderr, "Unknown datatype %d for dataset %d, skipping ...\n",
                 ieeeDatatype, i);
        continue;
    }

    /* convert ordering (FlexIO uses fortran order, HDF5 uses C order) */
    for (j = 0; j < rank; j++)
    {
      hdf5Dims[j] = dims[rank - j - 1];
    }
    dataspace = H5Screate_simple (rank, hdf5Dims, NULL);
    if (dataspace < 0)
    {
      fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: dataspace is out of bounds");
      return (-1);
    }

    sprintf (hdf5DatasetName, "/%s timelevel %d at iteration %d",
             ieeeDatasetName, timelevel, iteration);
    dataset = H5Dcreate (outfile, hdf5DatasetName, hdf5Datatype, dataspace,
                         H5P_DEFAULT);
    if (dataset < 0)
    {
      fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: dataset is out of bounds");
      return (-1);
    }

    data = malloc (IOnBytes (ieeeDatatype, rank, dims));
    if (data == NULL)
    {
      fprintf (stderr, "Could not allocate %d bytes to read in dataset\n",
               IOnBytes (ieeeDatatype, rank, dims));
      return (-1);
    }
    IOread (infile, data);
    if(! (H5Dwrite (dataset, hdf5Datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,
                      data) >= 0))
    {
      fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: error returned from function H5Dwrite");
      return (-1);
    }

    nAttributes = IOnAttributes (infile);
    printf ("Processing dataset '%s' with %d attributes\n",
            ieeeDatasetName, nAttributes);

    for (j = 0; j < nAttributes; j++)
    {
      int len;

      if (IOreadIndexedAttributeInfo (infile, j, attrName, &attrNumberType,
                                      &attrLen, MAXNAMESIZE) < 0)
      {
#if  0
        /* it's only a bug if IOreadIndexedAttributeInfo() returns -1 */
        printf ("Cannot read info of attribute %d, skipping ...\n", j);
        continue;
#endif
      }

      len = (int) attrLen;
      /* I don't know why but I have to add one byte - otherwise
         it crashes sometimes when freeing this memory */
      attrData = malloc (1 + IOnBytes (attrNumberType, 1, &len));
      if (IOreadAttribute (infile, j, attrData) < 0)
      {
        fprintf (stderr, "Cannot read value of attribute %d, skipping ...\n",j);
        continue;
      }

      switch (attrNumberType)
      {
        case BYTE:
        case CHAR:
          hdf5Datatype = hdf5String;
          if (len > 1)
          {
            len--;              /* cut trailing '\0' */
          }
          H5Tset_size (hdf5Datatype, len);
          attrDataspace = H5Screate (H5S_SCALAR);
          break;

        case FLOAT64:
          hdf5Datatype = H5T_NATIVE_DOUBLE;
          hdf5Dims[0] = len;
          attrDataspace = H5Screate_simple (1, hdf5Dims, NULL);
          break;

        case INT32:
          hdf5Datatype = H5T_NATIVE_INT;
          hdf5Dims[0] = len;
          attrDataspace = H5Screate_simple (1, hdf5Dims, NULL);
          break;

        default:
          fprintf (stderr, "Unknown datatype %d for attribute %s, skipping "
                   "...\n", attrNumberType, attrName);
          continue;
      }

      if (attrDataspace < 0)
      {
        fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: attrDataspace is out of bounds");
        return (-1);
      }

      /* turn off error messages about an already existing attribute */
      H5E_BEGIN_TRY
      {
        H5Adelete (dataset, attrName);
      } H5E_END_TRY;

      attribute = H5Acreate (dataset, attrName, hdf5Datatype,
                             attrDataspace, H5P_DEFAULT);
      if (attribute < 0)
      {
        fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: attribute is out of bounds");
        return (-1);
      }
      if(! (H5Awrite (attribute, hdf5Datatype, attrData) >= 0))
      {
        fprintf ( stderr, "IOHDF5:hdf5_convert_from_ieeeio.c: error returned from function H5Awrite");
        return (-1);
      }
      H5Sclose (attrDataspace);
      H5Aclose (attribute);

      free (attrData);
    }

    H5Dclose (dataset);
    H5Sclose (dataspace);
    free (data);
  }

  H5Tclose (hdf5String);
  H5Fclose (outfile);
  IOclose (infile);

  return (0);
}