40#include "config_hdf.h"
47#ifdef HAVE_SYS_PARAM_H
55#include <libdap/escaping.h>
56#include <libdap/Error.h>
57#include <libdap/debug.h>
66HDFArray::HDFArray(
const string &n,
const string &d, BaseType * v) :
70HDFArray::~HDFArray() =
default;
73BaseType *HDFArray::ptr_duplicate() {
74 return new HDFArray(*
this);
80bool HDFArray::read() {
82 int status = read_tagref(-1, -1, err);
85 throw Error(unknown_error,
"Could not read from dataset.");
92bool HDFArray::read_tagref(int32 tag, int32 ref,
int &err) {
97 string hdf_file = dataset();
98 string hdf_name = this->name();
104 bool isslab = GetSlabConstraint(start, edge, stride);
106 bool foundsds =
false;
108 if (tag == -1 || tag == DFTAG_NDG) {
109 if (SDSExists(hdf_file.c_str(), hdf_name.c_str())) {
110 hdfistream_sds sdsin(hdf_file.c_str());
112 BESDEBUG(
"h4",
"sds seek with ref = " << ref << endl);
115 BESDEBUG(
"h4",
"sds seek with name = '" << hdf_name <<
"'" << endl);
116 sdsin.seek(hdf_name.c_str());
119 sdsin.setslab(start, edge, stride,
false);
126 bool foundgr =
false;
128 if (!foundsds && (tag == -1 || tag == DFTAG_VG)) {
129 if (GRExists(hdf_file.c_str(), hdf_name.c_str())) {
130 hdfistream_gri grin(hdf_file.c_str());
134 grin.seek(hdf_name.c_str());
136 grin.setslab(start, edge, stride,
false);
145 LoadArrayFromSDS(
this, sds);
147 LoadArrayFromGR(
this, gr);
149 if (foundgr || foundsds) {
161bool HDFArray::GetSlabConstraint(vector<int>&start_array,
162 vector<int>&edge_array, vector<int>&stride_array) {
168 start_array = vector<int> (0);
169 edge_array = vector<int> (0);
170 stride_array = vector<int> (0);
172 for (Array::Dim_iter p = dim_begin(); p != dim_end(); ++p) {
173 start = dimension_start(p,
true);
174 stride = dimension_stride(p,
true);
175 stop = dimension_stop(p,
true);
176 if (start == 0 && stop == 0 && stride == 0)
179 THROW(dhdferr_arrcons);
180 edge = ((stop - start) / stride) + 1;
181 if (start + edge > dimension_size(p))
182 THROW(dhdferr_arrcons);
184 start_array.push_back(start);
185 edge_array.push_back(edge);
186 stride_array.push_back(stride);
213 BESDEBUG(
"h4",
"Transferring attributes for " << name() << endl);
215 BaseType::transfer_attributes(at);
217 BESDEBUG(
"h4",
"...Now looking for the " << name() <<
" _dim_n containers." << endl);
220 string dim_name_base = name() +
"_dim_";
222 AttrTable::Attr_iter a_p = at->attr_begin();
223 while (a_p != at->attr_end()) {
224 string::size_type i = at->get_name(a_p).find(dim_name_base);
228 if (i == 0 && at->get_attr_type(a_p) == Attr_container) {
229 AttrTable *dim = at->get_attr_table(a_p);
231 BESDEBUG(
"h4",
"Found a dimension container for " << name() << endl);
232 transfer_dimension_attribute(dim);
235 BESDEBUG(
"h4",
"Caught an error transferring dimension attribute " << dim->get_name() <<
" for variable " << name() << endl);
244void HDFArray::transfer_dimension_attribute(AttrTable *dim) {
246 dim->set_is_global_attribute(
false);
248 auto at =
new AttrTable(*dim);
250 string name = at->get_name().substr(at->get_name().find(
"dim"));
251 get_attr_table().append_container(at, name);
void transfer_attributes(libdap::AttrTable *at_container) override