aboutsummaryrefslogtreecommitdiff
path: root/src/util/ieee_merge.c
blob: a83ed9bae08c2b95b41f124b96bf10a601c1f9af (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
 /*@@
   @file      ieee_merge.c
   @date      Fri 14 Dec 2001
   @author    Thomas Radke
   @desc
              Utility program to merge IEEEIO datafiles.
   @enddesc
   @version   $Id$
 @@*/

#define  MAXDIM    3
#define  MAXNAMESIZE  100

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

/* CCTK includes */
#include "cctk.h"

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

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


int main (int argc, char **argv)
{
  int h, i, j;
  IOFile infile, *infiles, outfile;
  int nDatasets, nAttributes, nofInfiles;
  int datatype, out_datatype;
  int rank, out_rank;
  int dims[MAXDIM], out_dims[MAXDIM];
  int attrType, attrLen;
  int global_size[MAXDIM], out_global_size[MAXDIM];
  char name[MAXNAMESIZE], out_name[MAXNAMESIZE];
  char attrName[MAXNAMESIZE];
  void *data, *attrData;


  if (argc <= 2)
  {
    printf ("Usage: %s <outfile> <infile1> <infile2> .. <infileN>\n", argv[0]);
    return (0);
  }

  out_rank = 0;
  out_datatype = 0;
  outfile = IEEEopen (argv[1], "w");
  if (! IOisValid (outfile))
  {
    printf ("Could not open output file '%s'\n", argv[1]);
    return (1);
  }
  nofInfiles = argc - 2;
  infiles = (IOFile *) malloc (nofInfiles * sizeof (IOFile));
  for (i = 0; i < nofInfiles; i++)
  {
    infiles[i] = IEEEopen (argv[i + 2], "r");
    if (! IOisValid (infiles[i]))
    {
      printf ("Could not open input file '%s'\n", argv[i + 2]);
      return (1);
    }
  }

  for (h = 0; h < nofInfiles; h++)
  {
    infile = infiles[h];
    nDatasets = IOnDatasets (infile);
    printf ("Input file '%s' contains %d datasets\n", argv[h + 2], nDatasets);

    for (i = 0; i < nDatasets; i++)
    {
      if (IOreadInfo (infile, &datatype, &rank, dims, MAXDIM) <= 0)
      {
        printf ("Cannot read info of datatset %d\n", i);
        continue;
      }
      if (h > 0 && (datatype != out_datatype || rank != out_rank ||
                    dims[0] != out_dims[0] || dims[1] != out_dims[1] ||
                    dims[2] != out_dims[2]))
      {
        printf ("Dataset type or dims differ\n");
        continue;
      }

      /* retrieve name and iteration attribute of dataset */
      strcpy (attrName, "name");
      j = IOreadAttributeInfo (infile, attrName, &attrType, &attrLen);
      if (j < 0)
      {
        printf ("Cannot find name attribute of dataset %d\n", i);
        continue;
      }
      if (IOreadAttribute (infile, j, name) < 0)
      {
        printf ("Cannot read name attribute of dataset %d\n", i);
        continue;
      }
      if (h > 0 && strcmp (name, out_name))
      {
        printf ("Attribute 'name' differs !\n");
        continue;
      }

#if  0
      strcpy (attrName, "iteration");
      j = IOreadAttributeInfo (infile, attrName, &attrType, &attrLen);
      if (j < 0)
      {
        printf ("Cannot find iteration attribute of dataset %d\n", i);
        continue;
      }
      if (IOreadAttribute (infile, j, &iteration) < 0)
      {
        printf ("Cannot read iteration attribute of dataset %d\n", i);
        continue;
      }
#endif

      strcpy (attrName, "global_size");
      j = IOreadAttributeInfo (infile, attrName, &attrType, &attrLen);
      if (j < 0)
      {
        printf ("Cannot find global_size attribute of dataset %d\n", i);
        continue;
      }
      if (IOreadAttribute (infile, j, global_size) < 0)
      {
        printf ("Cannot read global_size attribute of dataset %d\n", i);
        continue;
      }
      if (h > 0 && (global_size[0] != out_global_size[0] ||
          global_size[1] != out_global_size[1] ||
          global_size[2] != out_global_size[2]))
      {
        printf ("Attribute 'global_size' differs !\n");
        continue;
      }

      if (h == 0)
      {
        out_datatype = datatype;
        out_rank = rank;
        out_dims[0] = dims[0];
        out_dims[1] = dims[1];
        out_dims[2] = dims[2];
        strcpy (out_name, name);
        out_global_size[0] = global_size[0];
        out_global_size[1] = global_size[1];
        out_global_size[2] = global_size[2];
      }

      data = malloc (IOnBytes (datatype, rank, dims));
      IOread (infile, data);
      IOwrite (outfile, datatype, rank, dims, data);
      free (data);

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

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

        attrData = malloc (IOnBytes (attrType, 1, &attrLen));
        if (IOreadAttribute (infile, j, attrData) < 0)
        {
          printf ("Cannot read value of attribute %d, skipping ...\n", j);
          continue;
        }
        IOwriteAttribute (outfile, attrName, attrType, attrLen, attrData);
        free (attrData);
      }

    }

    IOclose (infile);
  }

  IOclose (outfile);

  free (infiles);

  return (0);
}