bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
HDFDMRArray_SDS.cc
1
2// This file is part of the hdf4 data handler for the OPeNDAP data server.
3// It retrieves HDF4 SDS values for a direct DMR-mapping DAP4 response.
4// Each SDS will be mapped to a DAP variable.
5
6// Authors: Kent Yang <myang6@hdfgroup.org>
7// Copyright (c) The HDF Group
9
10
11#include "HDFDMRArray_SDS.h"
12#include "hdf.h"
13#include "mfhdf.h"
14#include <iostream>
15#include <sstream>
16#include <libdap/debug.h>
17#include <libdap/InternalErr.h>
18#include <BESDebug.h>
19
20using namespace std;
21using namespace libdap;
22
23
24bool
25HDFDMRArray_SDS::read ()
26{
27
28 BESDEBUG("h4","Coming to HDFDMRArray_SDS read "<<endl);
29 if (length() == 0)
30 return true;
31
32 // Declaration of offset,count and step
33 vector<int>offset;
34 offset.resize(sds_rank);
35 vector<int>count;
36 count.resize(sds_rank);
37 vector<int>step;
38 step.resize(sds_rank);
39
40 // Obtain offset,step and count from the client expression constraint
41 int nelms = format_constraint(offset.data(),step.data(),count.data());
42 BESDEBUG("h4","Coming to HDFDMRArray_SDS read "<<endl);
43
44 int32 sdid = SDstart (const_cast < char *>(filename.c_str ()), DFACC_READ);
45 if (sdid < 0) {
46 ostringstream eherr;
47 eherr << "File " << filename.c_str () << " cannot be open.";
48 throw InternalErr (__FILE__, __LINE__, eherr.str ());
49 }
50
51 BESDEBUG("h4","Coming to HDFDMRArray_SDS read "<<endl);
52 BESDEBUG("h4","After SDstart "<<endl);
53 // Obtain the SDS index based on the input sds reference number.
54 int32 sdsindex = SDreftoindex (sdid, sds_ref);
55 if (sdsindex == -1) {
56 SDend(sdid);
57 ostringstream eherr;
58 eherr << "SDS index " << sdsindex << " is not right.";
59 throw InternalErr (__FILE__, __LINE__, eherr.str ());
60 }
61
62 // Obtain this SDS ID.
63 int32 sdsid = SDselect (sdid, sdsindex);
64 if (sdsid < 0) {
65 SDend(sdid);
66 ostringstream eherr;
67 eherr << "SDselect failed.";
68 throw InternalErr (__FILE__, __LINE__, eherr.str ());
69 }
70
71 vector<char>buf;
72 buf.resize(nelms*sds_typesize);
73
74 int32 r = SDreaddata (sdsid, offset.data(), step.data(), count.data(), buf.data());
75 if (r != 0) {
76 SDendaccess (sdsid);
77 SDend(sdid);
78 ostringstream eherr;
79 eherr << "SDreaddata failed";
80 throw InternalErr (__FILE__, __LINE__, eherr.str ());
81 }
82
83 val2buf(buf.data());
84 set_read_p(true);
85 SDendaccess (sdsid);
86 SDend(sdid);
87
88 return true;
89}
90// Standard way to pass the coordinates of the subsetted region from the client to the handlers
91// Returns the number of elements
92int
93HDFDMRArray_SDS::format_constraint (int *offset, int *step, int *count)
94{
95 int nels = 1;
96 int id = 0;
97
98 Dim_iter p = dim_begin ();
99 while (p != dim_end ()) {
100
101 int start = dimension_start (p, true);
102 int stride = dimension_stride (p, true);
103 int stop = dimension_stop (p, true);
104
105 // Check for illegal constraint
106 if (start > stop) {
107 ostringstream oss;
108 oss << "Array/Grid hyperslab start point "<< start <<
109 " is greater than stop point " << stop <<".";
110 throw Error(malformed_expr, oss.str());
111 }
112
113 offset[id] = start;
114 step[id] = stride;
115 count[id] = ((stop - start) / stride) + 1; // count of elements
116 nels *= count[id]; // total number of values for variable
117
118 BESDEBUG ("h4",
119 "=format_constraint():"
120 << "id=" << id << " offset=" << offset[id]
121 << " step=" << step[id]
122 << " count=" << count[id]
123 << endl);
124
125 id++;
126 p++;
127 }
128
129 return nels;
130}
131
132