bes Updated for version 3.20.13
HDFSPArrayAddCVField.cc
1
2// This file is part of the hdf4 data handler for the OPeNDAP data server.
3// It retrieves the missing fields for some special NASA HDF4 data products.
4// The products include TRMML2_V6,TRMML3B_V6,CER_AVG,CER_ES4,CER_CDAY,CER_CGEO,CER_SRB,CER_SYN,CER_ZAVG,OBPGL2,OBPGL3
5// To know more information about these products,check HDFSP.h.
6// Some third-dimension coordinate variable values are not provided.
7// What we do here is to provide natural number series(1,2,3, ...) for
8// these missing values. It doesn't make sense to visualize or analyze
9// with vertical cross-section. One can check the data level by level.
10
11// Authors: MuQun Yang <myang6@hdfgroup.org>
12// Copyright (c) 2010-2012 The HDF Group
14
15#include "HDFSPArrayAddCVField.h"
16#include <iostream>
17#include <sstream>
18#include <cassert>
19#include <libdap/debug.h>
20#include "mfhdf.h"
21#include "hdf.h"
22#include <libdap/InternalErr.h>
23#include <BESDebug.h>
24
25using namespace libdap;
26using namespace std;
27
28bool
29HDFSPArrayAddCVField::read ()
30{
31
32 BESDEBUG("h4","Coming to HDFSPArrayAddCVField read "<<endl);
33
34 if(length() == 0)
35 return true;
36
37 // Declaration of offset,count and step
38 vector<int>offset;
39 offset.resize(1);
40 vector<int>count;
41 count.resize(1);
42 vector<int>step;
43 step.resize(1);
44
45 // Obtain offset,step and count from the client expression constraint
46 int nelms = format_constraint(offset.data(),step.data(),count.data());
47
48 if(sptype == TRMML3C_V6) {
49
50 if(dtype != DFNT_FLOAT32) {
51 throw InternalErr (__FILE__, __LINE__,
52 "The Height datatype of TRMM CSH product should be float.");
53 }
54
55 if(tnumelm != 19) {
56 throw InternalErr (__FILE__, __LINE__,
57 "The number of elements should be 19.");
58 }
59
60 vector<float>total_val;
61 total_val.resize(tnumelm);
62 total_val[0] = 0.5;
63 for (int i = 1; i<tnumelm;i++)
64 total_val[i] = (float)i;
65
66
67 if (nelms == tnumelm) {
68 set_value ((dods_float32 *) total_val.data(), nelms);
69 }
70 else {
71
72 vector<float>val;
73 val.resize(nelms);
74
75 for (int i = 0; i < nelms; i++)
76 val[i] = total_val[offset[0] + step[0] * i];
77 set_value ((dods_float32 *) val.data(), nelms);
78 }
79 }
80
81 if(sptype == TRMML3S_V7) {
82
83
84 if(dtype != DFNT_FLOAT32) {
85 throw InternalErr (__FILE__, __LINE__,
86 "The Height datatype of TRMM CSH product should be float.");
87 }
88
89 if(tnumelm == 28)
90 Obtain_trmm_v7_layer(nelms,offset,step);
91 else if(tnumelm == 6)
92 Obtain_trmml3s_v7_nthrash(nelms,offset,step);
93 else {
94 throw InternalErr (__FILE__, __LINE__,
95 "This special coordinate variable is not supported.");
96 }
97
98 }
99
100 if(sptype == TRMML2_V7) {
101
102
103 if(dtype != DFNT_FLOAT32) {
104 throw InternalErr (__FILE__, __LINE__,
105 "The Height datatype of TRMM CSH product should be float.");
106 }
107
108 if(tnumelm == 28 && name =="nlayer")
109 Obtain_trmm_v7_layer(nelms,offset,step);
110 else {
111 throw InternalErr (__FILE__, __LINE__,
112 "This special coordinate variable is not supported.");
113 }
114
115 }
116
117 return true;
118}
119
120
121
122#if 0
123 if(tnumelm != 28) {
124 throw InternalErr (__FILE__, __LINE__,
125 "The number of elements should be 19.");
126 }
127
128 vector<float>total_val;
129 total_val.resize(tnumelm);
130 for (int i = 0; i<20;i++)
131 total_val[i] = 0.5*(i+1);
132
133 for (int i = 20; i<28;i++)
134 total_val[i] = total_val[19]+(i-19);
135
136
137
138 // Since we always assign the the missing Z dimension as 32-bit
139 // integer, so no need to check the type. The missing Z-dim is always
140 // 1-D with natural number 1,2,3,....
141 if (nelms == tnumelm) {
142 set_value ((dods_float32 *) total_val.data(), nelms);
143 }
144 else {
145
146 vector<float>val;
147 val.resize(nelms);
148
149 for (int i = 0; i < nelms; i++)
150 val[i] = total_val[offset[0] + step[0] * i];
151 set_value ((dods_float32 *) val.data(), nelms);
152 }
153 }
154
155#endif
156
157
158void HDFSPArrayAddCVField:: Obtain_trmm_v7_layer(int nelms, vector<int>&offset,vector<int>&step) {
159
160
161 vector<float>total_val;
162 total_val.resize(tnumelm);
163 for (int i = 0; i<20;i++)
164 total_val[i] = 0.5*(i+1);
165
166 for (int i = 20; i<28;i++)
167 total_val[i] = total_val[19]+(i-19);
168
169
170
171 // Since we always assign the the missing Z dimension as 32-bit
172 // integer, so no need to check the type. The missing Z-dim is always
173 // 1-D with natural number 1,2,3,....
174 if (nelms == tnumelm) {
175 set_value ((dods_float32 *) total_val.data(), nelms);
176 }
177 else {
178
179 vector<float>val;
180 val.resize(nelms);
181
182 for (int i = 0; i < nelms; i++)
183 val[i] = total_val[offset[0] + step[0] * i];
184 set_value ((dods_float32 *) val.data(), nelms);
185 }
186}
187
188
189void HDFSPArrayAddCVField:: Obtain_trmml3s_v7_nthrash(int nelms, vector<int>&offset,vector<int>&step) {
190
191 vector<float>total_val;
192 total_val.resize(tnumelm);
193
194 if(name =="nthrshZO") {
195
196 total_val[0] = 0.1;
197 total_val[1] = 0.2;
198 total_val[2] = 0.3;
199 total_val[3] = 0.5;
200 total_val[4] = 0.75;
201 total_val[5] = 50;
202 }
203
204 else if (name == "nthrshHB") {
205
206 total_val[0] = 0.1;
207 total_val[1] = 0.2;
208 total_val[2] = 0.3;
209 total_val[3] = 0.5;
210 total_val[4] = 0.75;
211 total_val[5] = 0.9999;
212 }
213
214 else if(name =="nthrshSRT") {
215
216 total_val[0] = 1.5;
217 total_val[1] = 1.0;
218 total_val[2] = 0.8;
219 total_val[3] = 0.6;
220 total_val[4] = 0.4;
221 total_val[5] = 0.1;
222
223 }
224 else
225 throw InternalErr (__FILE__, __LINE__,
226 "Unsupported coordinate variable names.");
227
228 // Since we always assign the the missing Z dimension as 32-bit
229 // integer, so no need to check the type. The missing Z-dim is always
230 // 1-D with natural number 1,2,3,....
231 if (nelms == tnumelm) {
232 set_value ((dods_float32 *) total_val.data(), nelms);
233 }
234 else {
235
236 vector<float>val;
237 val.resize(nelms);
238
239 for (int i = 0; i < nelms; i++)
240 val[i] = total_val[offset[0] + step[0] * i];
241 set_value ((dods_float32 *) val.data(), nelms);
242 }
243}
244
245
246
247// Standard way of DAP handlers to pass the coordinates of the subsetted region to the handlers
248// Return the number of elements to read.
249int
250HDFSPArrayAddCVField::format_constraint (int *offset, int *step, int *count)
251{
252 long nels = 1;
253 int id = 0;
254
255 Dim_iter p = dim_begin ();
256 while (p != dim_end ()) {
257
258 int start = dimension_start (p, true);
259 int stride = dimension_stride (p, true);
260 int stop = dimension_stop (p, true);
261
262 // Check for illegal constraint
263 if (start > stop) {
264 ostringstream oss;
265 oss << "Array/Grid hyperslab start point "<< start <<
266 " is greater than stop point " << stop <<".";
267 throw Error(malformed_expr, oss.str());
268 }
269
270 offset[id] = start;
271 step[id] = stride;
272 count[id] = ((stop - start) / stride) + 1; // count of elements
273 nels *= count[id]; // total number of values for variable
274
275 BESDEBUG ("h4",
276 "=format_constraint():"
277 << "id=" << id << " offset=" << offset[id]
278 << " step=" << step[id]
279 << " count=" << count[id]
280 << endl);
281
282 id++;
283 p++;
284 }// while (p != dim_end ())
285
286 return nels;
287}
288
289