aboutsummaryrefslogtreecommitdiff
path: root/src/util/ieee_convert_from_cactus3.c
blob: 22f76d4411c31bdc043892644a6bdf48896e2298 (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
 /*@@
   @file      ieee_convert_from_cactus3.c
   @date      Fri 14 Dec 2001
   @author    Thomas Radke
   @desc
              Utility program to convert Cactus 3 IEEEIO datafiles into
              Cactus 4 IEEEIO datafiles.
   @enddesc
   @version   $Id$
 @@*/

#include <stdio.h>
#include <string.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_convert_from_cactus3_c)


#define MAXDIM          3           /* max. dimensions of an IEEEIO dataset  */
#define MAXNAMESIZE     100         /* max. attribute name length            */
#define MAXLINELEN      200         /* longest linesize in the mapfile       */
#define MAXMAPPINGS     1000        /* max. lines of (valid) mapfile entries */

typedef struct                      /* a mapping description as it is read   */
{                                   /* from the mapfile                      */
  char *oldname;
  char *newname;
  char *groupname;
} map_desc_t;


int main (int argc, char **argv)
{
  int i, j;
  IOFile infile, outfile;
  int nDatasets, nAttributes;
  int datatype;
  int rank;
  int dims[MAXDIM];
  int attrType, attrLen;
  char attrName[MAXNAMESIZE];
  void *data, *attrData;
  FILE *mapfile;
  int nMappings;
  map_desc_t *mapping[MAXMAPPINGS];
  char line[MAXLINELEN];


  if (argc != 4)
  {
    printf ("Usage: %s <mapfile>  <infile> <outfile>\n\n", argv[0]);
    printf ("          where <mapfile> contains mappings for variable names\n");
    printf ("          from Cactus 3.x to Cactus 4.0 in the format\n");
    printf ("            <old varname> -> <full varname> <full groupname>\n");
    printf ("          eg.\n");
    printf ("              x   -> grid::x        grid::coordinates\n");
    printf ("              y   -> grid::y        grid::coordinates\n");
    printf ("              z   -> grid::z        grid::coordinates\n");
    printf ("              alp -> einstein::alp  einstein::lapse\n\n");
    return (0);
  }

  mapfile = fopen (argv[1], "r");
  if (! mapfile)
  {
    printf ("ERROR: Could not open mapfile '%s'\n\n", argv[1]);
    return (-1);
  }
  nMappings = i = 0;
  while (fgets (line, MAXLINELEN - 1, mapfile) != NULL)
  {
    char oldname[MAXLINELEN], newname[MAXLINELEN];
    char groupname[MAXLINELEN];


    i++;
    if (line[strlen (line) - 1] == '\n')
    {
      line[strlen (line) - 1] = 0;
    }
    if (sscanf (line, "%s -> %s %s", oldname, newname, groupname) != 3)
    {
      printf ("Ignoring line %d in mapfile: '%s'\n", i, line);
    }
    else
    {
      mapping[nMappings] = (map_desc_t *) malloc (sizeof (map_desc_t));
      mapping[nMappings]->oldname = strdup (oldname);
      mapping[nMappings]->newname = strdup (newname);
      mapping[nMappings]->groupname = strdup (groupname);
      nMappings++;
    }
  }
  fclose (mapfile);
  if (nMappings == 0)
  {
    printf ("ERROR: Could not read any name mapping from '%s'\n", argv[1]);
    return (-1);
  }

  infile = IEEEopen (argv[2], "r");
  if (! IOisValid (infile))
  {
    printf ("ERROR: Could not open input file '%s'\n\n", argv[2]);
    return (1);
  }
  nDatasets = IOnDatasets (infile);
  printf ("Input file '%s' contains %d datasets\n", argv[2], nDatasets);

  outfile = IEEEopen (argv[3], "w");
  if (! IOisValid (outfile))
  {
    printf ("ERROR: Could not open output file '%s'\n\n", argv[3]);
    return (1);
  }

  for (i = 0; i < nDatasets; i++)
  {
    if (IOreadInfo (infile, &datatype, &rank, dims, MAXDIM) <= 0)
    {
      printf ("ERROR: Cannot read info of datatset %d, skipping dataset\n\n",i);
      continue;
    }

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

    nAttributes = IOnAttributes (infile);
    printf ("Processing dataset %d with %d attributes\n", i, 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 ("ERROR: Cannot read value of attribute %d, "
                "skipping attribute...\n\n", j);
        continue;
      }

      if (! strcmp (attrName, "name"))
      {
        int k;
        Int grouptype = CCTK_GF;    /* only grid functions have a 'name' attr.*/
        Int ntimelevels = 1;        /* Cactus 3.x had no timelevels */
        Int timelevel = 0;

        for (k = 0; k < nMappings; k++)
        {
          if (! strcmp (attrData, mapping[k]->oldname))
          {
            printf ("Found grid function with name '%s', \n"
                    "  set it to new varname '%s' with groupname '%s'\n",
                    mapping[k]->oldname, mapping[k]->newname,
                    mapping[k]->groupname);

            /* add the groupname for this variable */
            IOwriteAttribute (outfile, "groupname", BYTE,
              strlen (mapping[k]->groupname) + 1, mapping[k]->groupname);

            /* write the implicite Cactus 4.0 attributes */
            IOwriteAttribute (outfile, "grouptype", INT32, 1, &grouptype);
            IOwriteAttribute (outfile, "ntimelevels", INT32, 1, &ntimelevels);
            IOwriteAttribute (outfile, "timelevel", INT32, 1, &timelevel);

            /* and set the data pointer to use the new variable name */
            free (attrData);
            attrData = (char *) strdup (mapping[k]->newname);
            attrLen = strlen (mapping[k]->newname) + 1;
            break;
          }
        }
      }

      if (! strcpy (attrName, "GH$GHiteration"))
      {
        /* copy this also to the "main loop iteration" attribute */
        IOwriteAttribute (outfile, "main loop iteration", attrType, attrLen,
                          attrData);
        /* and map "GH$GHiteration" to "GH$iteration" */
        strcpy (attrName, "GH$iteration");
      }

      if (! strcpy (attrName, "GH$phys_time"))
      {
        /* map "GH$phys_time" to "GH$time" */
        strcpy (attrName, "GH$time");
      }

      IOwriteAttribute (outfile, attrName, attrType, attrLen, attrData);
      free (attrData);
    }
  }

  IOclose (infile);
  IOclose (outfile);

  for (i = 0; i < nMappings; i++)
  {
    free (mapping[i]->oldname);
    free (mapping[i]->newname);
    free (mapping[i]);
  }

  return (0);
}