/* Hdf_sds.cpp */ #undef DEBUG_HDF_SDS_CPP #ifdef DEBUG_HDF_SDS_CPP #define PDEBUG { std::fprintf(stderr, "call to %s (%s)\n", __PRETTY_FUNCTION__, __FILE__); } #else #define PDEBUG #endif #include #include #include #include // nothrow #include "Hdf_common.hpp" #include "Hdf_sds.hpp" Hdf_sds::Hdf_sds(const int32 sds_id, const char *sds_name, const int32 sds_rank, const int32 *sds_dim_sizes, const int32 sds_data_type, const int32 sds_nattrs) : id(sds_id), name(sds_name), rank(sds_rank), data_type(sds_data_type), nattrs(sds_nattrs) { PDEBUG; if (rank) { dim_sizes = new(std::nothrow) int32[rank]; assert(dim_sizes != NULL); /* FIXME: nothrow to be replaced by try ... catch */ } if (nattrs) { sds_attrs = new(std::nothrow) Hdf_attr[nattrs]; assert(sds_attrs != NULL); /* FIXME: nothrow to be replaced by try ... catch */ } for (int32 idim = 0 ; idim < rank ; idim++) { dim_sizes[idim] = sds_dim_sizes[idim]; } for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { intn status; char attr_name[MAX_NC_NAME + 1]; /* MAX_NC_NAME defined by HDF library */ int32 attr_type; int32 attr_nvalues; status = SDattrinfo(id, iattr, attr_name, &attr_type, &attr_nvalues); Hdf_attr hdf_attr(id, iattr, attr_name, attr_type, attr_nvalues); sds_attrs[iattr] = hdf_attr; } } Hdf_sds::Hdf_sds() : id(-1), name(""), rank(0), data_type(0), nattrs(0) { PDEBUG; } Hdf_sds::Hdf_sds(const Hdf_sds &hdf_sds) : id(hdf_sds.id), name(hdf_sds.name), rank(hdf_sds.rank), data_type(hdf_sds.data_type), nattrs(hdf_sds.nattrs) { PDEBUG; if (rank) dim_sizes = new int32[rank]; if (nattrs) sds_attrs = new Hdf_attr[nattrs]; for (int32 idim = 0 ; idim < rank ; idim++) { dim_sizes[idim] = hdf_sds.dim_sizes[idim]; } for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { sds_attrs[iattr] = hdf_sds.sds_attrs[iattr]; } } Hdf_sds::~Hdf_sds() { PDEBUG; if (rank) delete[] dim_sizes; if (nattrs) delete[] sds_attrs; } std::string Hdf_sds::list_of_dimensions() const { using namespace std; ostringstream s; s << "[ "; for (int32 idim = 0 ; idim < rank ; idim++) s << dim_sizes[idim] << " "; s << "]"; return s.str(); } Hdf_attr Hdf_sds::get_attribute(const char *attr_name) const { using namespace std; assert(attr_name); ostringstream err_msg; for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { if (sds_attrs[iattr].get_name() == attr_name) return sds_attrs[iattr]; } err_msg << __FILE__ ": no attribute " << attr_name << " in sds " << name; throw err_msg.str(); } void Hdf_sds::get_attributes(Hdf_attr *attributes) const { assert(attributes); for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { attributes[iattr] = sds_attrs[iattr]; } } void Hdf_sds::get_dimensions(int32 *dimensions) const { assert(dimensions); for (int32 idim = 0 ; idim < rank ; idim++) { dimensions[idim] = dim_sizes[idim]; } } /* returns a string representation of the list of sds attributes */ std::string Hdf_sds::list_of_attributes(const int32 chars_to_display, const int32 numbers_to_display) const { std::string s; s = ""; Hdf_attr::chars_to_display = chars_to_display; Hdf_attr::numbers_to_display = numbers_to_display; for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { s += sds_attrs[iattr].to_string(); s += '\n'; } return s; } void Hdf_sds::read(void *sds_data, int32 *start, int32 *stride, int32 *edges) const { intn status; assert(sds_data); status = SDreaddata(id, start, stride, edges, (VOIDP) sds_data); if (status == FAIL) { std::cerr << __PRETTY_FUNCTION__ << ": error while reading sds " << name << std::endl; exit(EXIT_FAILURE); // FIXME: exits while debugging, will throw an exception in the final version } } void Hdf_sds::write(const void *sds_data, int32 *start, int32 *stride, int32 *edges, float64 cal, float64 offset, float64 cal_err, float64 off_err) const { intn status; assert(sds_data); status = SDwritedata(id, start, stride, edges, (VOIDP) sds_data); if (status == FAIL) { std::cerr << __PRETTY_FUNCTION__ << ": error while writing sds " << name << std::endl; exit(EXIT_FAILURE); // FIXME: exits while debugging, will throw an exception in the final version } if (cal != 0) { int32 data_type; status = SDgetinfo(id, NULL, NULL, NULL, &data_type, NULL); assert(status != FAIL); status = SDsetcal(id, cal, cal_err, offset, off_err, data_type); assert(status != FAIL); } } std::string Hdf_sds::to_string() const { using namespace std; ostringstream s; s << name << ", type: " << Hdf_common::hdf_cstr_type(data_type) << ", rank: " << rank << ", dimensions: " << list_of_dimensions() << ", nattrs: " << nattrs; return s.str(); } Hdf_sds & Hdf_sds::operator= (const Hdf_sds &hdf_sds) { PDEBUG; if (&hdf_sds == this) return *this; // protection against self assignation if (rank) delete[] dim_sizes; if (nattrs) delete[] sds_attrs; id = hdf_sds.id; name = hdf_sds.name; rank = hdf_sds.rank; data_type = hdf_sds.data_type; nattrs = hdf_sds.nattrs; try { dim_sizes = new int32[rank]; sds_attrs = new Hdf_attr[nattrs]; for (int32 idim = 0 ; idim < rank ; idim++) { dim_sizes[idim] = hdf_sds.dim_sizes[idim]; } for (int32 iattr = 0 ; iattr < nattrs ; iattr++) { sds_attrs[iattr] = hdf_sds.sds_attrs[iattr]; } } catch (std::bad_alloc &e) { std::cout<<"Sorry, can't allocate the memory in "<<__FILE__<<" at "<<__LINE__<