File: VsUtils.C

package info (click to toggle)
paraview 4.0.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 526,572 kB
  • sloc: cpp: 2,284,430; ansic: 816,374; python: 239,936; xml: 70,162; tcl: 48,295; fortran: 39,116; yacc: 5,466; java: 3,518; perl: 3,107; lex: 1,620; sh: 1,555; makefile: 932; asm: 471; pascal: 228
file content (230 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download | duplicates (4)
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
#include <hdf5.h>
#include <visit-hdf5.h>
#if HDF5_VERSION_GE(1,8,1)
#include <VsUtils.h>

// JRC: This fat interface may not scale?  What about
// scalar attributes?
herr_t getAttributeHelper(const hid_t id, std::string* sval, std::vector<int>* ivals,
    std::vector<float>* fvals) {

  herr_t err = 0;
  size_t npoints;
  hid_t atype = H5Aget_type(id);
  H5T_class_t type = H5Tget_class(atype);
  hid_t aspace = H5Aget_space(id);
  size_t rank = H5Sget_simple_extent_ndims(aspace);

  /*
   hsize_t sdim[rank];
   ret = H5Sget_simple_extent_dims(aspace, sdim, NULL);
   dims->resize(rank);
   for (size_t i = 0; i < rank; ++i)
   (*dims)[i] = sdim[i];
   */

  if (type == H5T_INTEGER) {
    if (rank == 0) {
      ivals->resize(1);
      int v;
      // err = H5Aread(id, atype, &v);
      err = H5Aread(id, H5T_NATIVE_INT, &v);
      (*ivals)[0] = v;
      return err;
    }
    // rank>0
    npoints = H5Sget_simple_extent_npoints(aspace);
    int* v = new int[npoints];
    err = H5Aread(id, H5T_NATIVE_INT, v);
    ivals->resize(npoints);
    for (size_t i = 0; i<npoints; ++i) {
      (*ivals)[i] = v[i];
    }
    delete v;
    return err;
  }

  if (type == H5T_FLOAT) {
    if (rank == 0) {
      fvals->resize(1);
      float v;
      err = H5Aread(id, H5T_NATIVE_FLOAT, &v);
      (*fvals)[0] = v;
      return err;
    }
    // rank>0
    npoints = H5Sget_simple_extent_npoints(aspace);
    float* v = new float[npoints];
    err = H5Aread(id, H5T_NATIVE_FLOAT, v);
    fvals->resize(npoints);
    for (size_t i = 0; i<npoints; ++i) {
      (*fvals)[i] = v[i];
    }
    delete v;
    return err;
  }

  if (type == H5T_STRING) {
    if (rank != 0) {
      return -1;
    }
    size_t len = H5Aget_storage_size(id);
    sval->resize(len);
    char* v = new char[len];
    err = H5Aread(id, atype, v);
    // JRC: is this right?
    // err = H5Aread(id, H5T_NATIVE_CHAR, v);
    for (size_t i = 0; i < len; ++i) {
      (*sval)[i] = v[i];
    }
    delete [] v;
    return err;
  }
  return err;

}

/*
 void parseVars(const string& vsVars, vector<string>& vars) {
 size_t i1;
 size_t i2;   // JRC: This needs to be initialized!
 char del = '\"';
 i1 = 0;
 int inc = -1;
 while (i2 != string::npos) {
 // inc alternates so that every other word between " is included
 i2 = vsVars.find(del, i1);
 if (inc>0) vars.push_back(vsVars.substr(i1, i2-i1));
 i1 = i2+1;
 inc *= -1;
 }
 }
 */

void getDims(hid_t id, bool isDataset, std::vector<int>& dims) {
  hid_t space;
  if (!isDataset) space = H5Aget_space(id);
  else space = H5Dget_space(id);
  size_t rank = H5Sget_simple_extent_ndims(space);
  std::vector<hsize_t> sdim(rank);
  if (rank > 0) {
    H5Sget_simple_extent_dims(space, &sdim[0], NULL);
  }
  dims.resize(rank);
  for (size_t i = 0; i < rank; ++i) {
    dims[i] = sdim[i];
  }
}

std::string makeCanonicalName(std::string name) {
  std::string answer = name;
  if ((name.length() > 0) && (name[0] == '/')) {
    answer = name.substr(1, name.length() - 1);
  }

  return answer;
}

std::string makeCanonicalName(std::string path, std::string name) {
  std::string answer = name;
  //only prepend the path if it is not empty, and if "name" does not start with '/'
  if ((path.length() > 0) && (name.length() > 0) && (name[0] != '/')) {
    answer = path + "/" + name;
  }
  //remove the leading slash if it exists
  if ((answer.length() > 0) && (answer[0] == '/')) {
    answer = answer.substr(1, answer.length());
  }

  return answer;
}

// Compare two object names to a target name
// Return the name that is "closest" to the target
// If the two object names are identical, returns the first (but doesn't matter)
// If the two object names are equally distant from the target, returns the first
//  i.e. getClosestName("abcX", "abcY", "name") == "abcX"
std::string getClosestName(std::string name1, std::string name2, std::string target) {
  //go through the two candidate names
  //find the first disagreement,
  //and see switch matches the target

  //Easy cases
  if (name1 == target)
  return name1;
  if (name2 == target)
  return name2;
  if (name1 == name2)
  return name1;

  //ok, now the real cases
  size_t shortestNameLength = std::min(name1.length(), name2.length());
  shortestNameLength = std::min(shortestNameLength, target.length());

  for (size_t i = 0; i < shortestNameLength; i++) {
    if (name1[i] != name2[i]) {
      //found a disagreeing character
      //which matches the target?
      if (name2[i] == target[i])
      return name2;
      //default behavior is to return name1
      return name1;
    }
  }

  //at this point, we know the following:
  // (name1 != name2 != target)
  // name1[0:n] == name2[0:n] == target[0:n] (where n == shortestNameLength)
  // so look at the next character
  std::string targetChar = "";
  if (target.length() > shortestNameLength)
  targetChar = target[shortestNameLength];

  std::string name1Char = "";
  if (name1.length() > shortestNameLength)
  name1Char = name1[shortestNameLength];

  std::string name2Char = "";
  if (name2.length() > shortestNameLength)
  name2Char = name2[shortestNameLength];

  //make the final decision
  if (name2Char == targetChar)
  return name2;

  //default behavior
  return name1;
}

//Adjusts var dimensions stored in an array of hsize_t
void adjustSize_hsize_t(hsize_t *dims, unsigned int rank, std::vector<int> stride, int before, int after) {
  //apply transform to each dimension
  for (unsigned int i = 0; i < rank; i++) {
    dims[i] += before;

    dims[i] = dims[i] / stride[i];
    //Don't allow the dimension to go below 1
    if (dims[i] < 1) {
      dims[i] = 1;
    }

    dims[i] += after;
  }
}

//Adjusts var dimensions stored in a vector int
void adjustSize_vector(std::vector<int>* dims, int rank, std::vector<int> stride, int before, int after) {
  //apply transform to each dimension
  for (int i = 0; i < rank; i++) {
    (*dims)[i] += before;

    (*dims)[i] = (*dims)[i] / stride[i];
    //Don't allow the dimension to go below 1
    if ((*dims)[i] < 1) {
      (*dims)[i] = 1;
    }

    (*dims)[i] += after;
  }
}
#endif