VsAttribute.C 5.24 KB
Newer Older
1
/*
apletzer's avatar
apletzer committed
2
 * VsAttribute.cpp
3 4 5 6 7
 *
 *  Created on: Apr 28, 2010
 *      Author: mdurant
 */

apletzer's avatar
apletzer committed
8
#include "VsAttribute.h"
9 10 11
#include "VsLog.h"
#include <hdf5.h>
  
apletzer's avatar
apletzer committed
12 13
VsAttribute::VsAttribute(VsObject* parentObject, std::string attName, hid_t id):
  VsObject(parentObject->registry, parentObject, attName, id) {
14 15 16 17
  
  dataType = H5Aget_type(id);
}

apletzer's avatar
apletzer committed
18
VsAttribute::~VsAttribute() {
19 20 21 22 23 24 25
  hid_t id = getId();
  if (id != -1) {
    H5Aclose(id);
    id = -1;
  }
}

apletzer's avatar
apletzer committed
26
void VsAttribute::write() const {
27 28 29 30 31 32 33 34 35
  std::string offset = "";
  for (int i = 0; i < getDepth(); ++i) {
    offset += "  ";
  }
  VsLog::debugLog() << offset<< "Attribute "<< getShortName() << std::endl;
  VsLog::debugLog() << offset<< "  rank = " << dims.size() << std::endl;
}

// Total length
apletzer's avatar
apletzer committed
36
size_t VsAttribute::getLength() const {
37 38 39 40 41 42 43
  size_t len = 1;
  for (size_t i = 0; i < dims.size(); ++i) {
    len *= dims[i];
  }
  return len;
}

apletzer's avatar
apletzer committed
44
int VsAttribute::getStringValue(std::string* val) {
45 46 47 48 49 50
  hid_t atype = H5Aget_type(getId());
  H5T_class_t type = H5Tget_class(atype);
  hid_t aspace = H5Aget_space(getId());
  size_t rank = H5Sget_simple_extent_ndims(aspace);
  
  if (type != H5T_STRING) {
apletzer's avatar
apletzer committed
51
    VsLog::warningLog() <<"VsAttribute::getStringValue() - Requested attribute " <<getShortName()
52 53 54 55 56 57 58
       <<" is not a string value." <<std::endl;
    val->clear();
    return -1;
  }
  
  //Yes, this is correct (rank != 0)
  if (rank != 0) {
apletzer's avatar
apletzer committed
59
    VsLog::warningLog() <<"VsAttribute::getStringValue() - Requested attribute " <<getShortName()
60 61 62 63 64 65 66 67
           <<" is not a string value." <<std::endl;
    val->clear();
    return -1;
  }
  
  size_t len = H5Aget_storage_size(getId());
  val->resize(len);
  char* v = new char[len];
apletzer's avatar
apletzer committed
68
  int err = H5Aread(getId(), atype, v);
69 70 71 72 73 74 75 76
  // JRC: is this right?
  // err = H5Aread(id, H5T_NATIVE_CHAR, v);
  for (size_t i = 0; i < len; ++i) {
     if (v[i] == 0) {
       //This happens when a program (IDL, HdfView) declares a string of length 5
       //and uses the 5th character for the null terminator
       //In h5dump this shows up as a string "mesh" being declared as 5 characters
       //If dumped to a terminal, it prints like so: "mesh^@"
apletzer's avatar
apletzer committed
77
       VsLog::warningLog() <<"VsAttribute::getStringValue() - Found null char inside string attribute: " <<getFullName() <<std::endl;
78 79 80 81 82 83 84 85 86 87 88
       val->resize(i);
       break;
     }
     (*val)[i] = v[i];
  }
  delete [] v;
  return err;
}

// JRC: This fat interface may not scale?  What about
// scalar attributes?
apletzer's avatar
apletzer committed
89 90
int VsAttribute::getIntVectorValue(std::vector<int>* ivals) {
  int err;
91 92 93 94 95 96 97
  size_t npoints;
  hid_t atype = H5Aget_type(getId());
  H5T_class_t type = H5Tget_class(atype);
  hid_t aspace = H5Aget_space(getId());
  size_t rank = H5Sget_simple_extent_ndims(aspace);

  if (type != H5T_INTEGER) {
apletzer's avatar
apletzer committed
98
    VsLog::warningLog() <<"VsAttribute::getIntVectorValue() - Requested attribute " <<getShortName()
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        <<" is not an integer vector." <<std::endl;
     ivals->resize(0);
     return -1;
  }
  
  if (rank == 0) {
    ivals->resize(1);
    int v;
    // err = H5Aread(id, atype, &v);
    err = H5Aread(getId(), H5T_NATIVE_INT, &v);
    (*ivals)[0] = v;
    return err;
  }
  
  // rank>0
  npoints = H5Sget_simple_extent_npoints(aspace);
  int* v = new int[npoints];
  err = H5Aread(getId(), H5T_NATIVE_INT, v);
  ivals->resize(npoints);
  for (size_t i = 0; i<npoints; ++i) {
    (*ivals)[i] = v[i];
  }
121
  delete [] v;
122 123 124 125 126 127
  
  return err;
}

// JRC: This fat interface may not scale?  What about
// scalar attributes?
apletzer's avatar
apletzer committed
128
int VsAttribute::getFloatVectorValue(std::vector<float>* fvals) {
129

apletzer's avatar
apletzer committed
130
  int err = 0;
131 132 133 134 135 136 137
  size_t npoints;
  hid_t atype = H5Aget_type(getId());
  H5T_class_t type = H5Tget_class(atype);
  hid_t aspace = H5Aget_space(getId());
  size_t rank = H5Sget_simple_extent_ndims(aspace);

  if (type != H5T_FLOAT) {
apletzer's avatar
apletzer committed
138
    VsLog::warningLog() <<"VsAttribute::getFloatVectorValue() - Requested attribute " <<getShortName()
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
         <<" is not a floating point vector." <<std::endl;
    fvals->resize(0);
    return -1;
  }
  
  if (rank == 0) {
    fvals->resize(1);
    float v;
    err = H5Aread(getId(), H5T_NATIVE_FLOAT, &v);
    (*fvals)[0] = v;
    return err;
  }
  
  // rank>0
  npoints = H5Sget_simple_extent_npoints(aspace);
  float* v = new float[npoints];
  err = H5Aread(getId(), H5T_NATIVE_FLOAT, v);
  fvals->resize(npoints);
  for (size_t i = 0; i<npoints; ++i) {
    (*fvals)[i] = v[i];
  }
160
  delete [] v;
161 162 163 164 165 166
  
  return err;
}

// JRC: This fat interface may not scale?  What about
// scalar attributes?
apletzer's avatar
apletzer committed
167 168
int VsAttribute::getDoubleVectorValue(std::vector<double>* dvals) {
  int err = 0;
169 170 171 172 173 174 175
  size_t npoints;
  hid_t atype = H5Aget_type(getId());
  H5T_class_t type = H5Tget_class(atype);
  hid_t aspace = H5Aget_space(getId());
  size_t rank = H5Sget_simple_extent_ndims(aspace);

  if (type != H5T_FLOAT) {
apletzer's avatar
apletzer committed
176
    VsLog::warningLog() <<"VsAttribute::getDoubleVectorValue() - Requested attribute " <<getShortName()
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
         <<" is not a floating point vector." <<std::endl;
    dvals->resize(0);
    return -1;
  }
  
  if (rank == 0) {
    dvals->resize(1);
    double v;
    err = H5Aread(getId(), H5T_NATIVE_DOUBLE, &v);
    (*dvals)[0] = v;
    return err;
  }
  
  // rank>0
  npoints = H5Sget_simple_extent_npoints(aspace);
  double* v = new double[npoints];
  err = H5Aread(getId(), H5T_NATIVE_DOUBLE, v);
  dvals->resize(npoints);
  for (size_t i = 0; i<npoints; ++i) {
    (*dvals)[i] = v[i];
  }
198
  delete [] v;
199 200 201
  
  return err;
}