aboutsummaryrefslogtreecommitdiff
path: root/src/util/ieee_extract.c
blob: 0aaba2ea57f6107cf8e71f8f1696a104dd72a3f4 (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
 /*@@
   @file      ieee_extract.c
   @date      Fri 14 Dec 2001
   @author    Thomas Radke
   @desc
              Utility program to extract a hyperslab from an IEEEIO datafile.
   @enddesc
   @version   $Id$
 @@*/

#define  MAXDIM    3
#define  MAXNAMESIZE  100

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

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


typedef struct
{
  double x, y, z;
} double3_t;


int main (int argc, char **argv)
{
  int i, j;
  IOFile infile, outfile;
  int nDatasets, nAttributes;
  int datatype;
  int rank;
  int dims [MAXDIM], chunk_dims [MAXDIM], chunk_origin [MAXDIM];
  int attrType, attrLen;
  char attrName [MAXNAMESIZE];
  void *data, *attrData;
  double3_t delta, min_ext, *max_ext;


  if (argc != 4)
  {
    printf ("Usage: %s <region> <infile> <outfile>\n", argv [0]);
    printf ("   eg. %s 128x128x128+32+32+32 foo.ieee bar.ieee\n", argv [0]);
        printf ("       selects a 128-cubed region with origin (32, 32, 32)\n");
    return (0);
  }

  if (sscanf (argv [1], "%dx%dx%d+%d+%d+%d",
                &chunk_dims [0], &chunk_dims [1], &chunk_dims [2],
                &chunk_origin [0], &chunk_origin [1], &chunk_origin [2]) != 6)
  {
    printf ("ERROR: Cannot parse region argument '%s'\n\n", argv [1]);
    return (-1);
  }
  printf ("Selecting region (%d, %d, %d) at origin (%d, %d, %d)\n",
            chunk_dims [0], chunk_dims [1], chunk_dims [2],
            chunk_origin [0], chunk_origin [1], chunk_origin [2]);

  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 ("Could not open output file '%s'\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, "
              "ignoring dataset...\n\n", i);
      continue;
    }

    nAttributes = IOnAttributes (infile);
    printf ("Processing dataset %d with %d attributes\n", i, nAttributes);
    printf ("Dataset dimensions are (%d, %d, %d)\n",
                dims [0], dims [1], dims [2]);

    if ((chunk_dims [0] + chunk_origin [0] > dims [0]) ||
        (chunk_dims [1] + chunk_origin [1] > dims [1]) ||
        (chunk_dims [2] + chunk_origin [2] > dims [2]))
    {
      printf ("ERROR: Region overlaps with global dataset dimensions, "
          "ignoring dataset %d\n\n", i);
      continue;
    }

    data = malloc (IOnBytes (datatype, rank, chunk_dims));
    if (IOreadChunk (infile, chunk_dims, chunk_origin, data) <0)
    {
      printf ("IOreadChunk failed\n");
    }
    IOwrite (outfile, datatype, rank, chunk_dims, data);
    free (data);

    if ((j = IOreadAttributeInfo (infile, "delta", &attrType, &attrLen)) < 0)
    {
      printf ("ERROR: no delta attribute found\n\n");
    }
    else
    {
      IOreadAttribute (infile, j, &delta);
    }

    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", j);
        continue;
      }

      if (! strcmp (attrName, "min_ext") || ! strcmp (attrName, "origin"))
      {
        min_ext = *(double3_t *) attrData;
        min_ext.x += chunk_origin [0] * delta.x;
        min_ext.y += chunk_origin [1] * delta.y;
        min_ext.z += chunk_origin [2] * delta.z;
        *(double3_t *) attrData = min_ext;
      }
      if (! strcmp (attrName, "max_ext"))
      {
        max_ext = (double3_t *) attrData;
        max_ext->x -= (dims [0] - chunk_origin [0] - chunk_dims [0]) * delta.x;
        max_ext->y -= (dims [1] - chunk_origin [1] - chunk_dims [1]) * delta.y;
        max_ext->z -= (dims [2] - chunk_origin [2] - chunk_dims [2]) * delta.z;
      }

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

  IOclose (infile);
  IOclose (outfile);

  return (0);
}