bes  Updated for version 3.20.6
NDimensionalArray.cc
1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003,2011,2012 OPeNDAP, Inc.
7 // Authors: Nathan Potter <ndp@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config.h"
26 
27 #include <sstream> // std::stringstream
28 #include <string.h>
29 
30 #include "NDimensionalArray.h"
31 #include "util.h"
32 
33 #include "Byte.h"
34 #include "Int16.h"
35 #include "UInt16.h"
36 #include "Int32.h"
37 #include "UInt32.h"
38 #include "Float32.h"
39 #include "Float64.h"
40 
41 #include "BESDebug.h"
42 
43 #ifdef NDEBUG
44 #undef BESDEBUG
45 #define BESDEBUG( x, y )
46 #endif
47 
48 namespace libdap {
49 
50 string NDimensionalArray::vectorToIndices(vector<unsigned int> *v)
51 {
52  stringstream s;
53  for (unsigned int i = 0; i < v->size(); i++) {
54  s << "[" << (*v)[i] << "]";
55  }
56  return s.str();
57 }
58 
59 #if 0
60 NDimensionalArray::NDimensionalArray()
61 :_dapType(dods_null_c),_shape(0),_currentLastDimensionSlabIndex(0),_totalValueCount(0),_sizeOfValue(0),_storage(0) {
62 
63  string msg = "NDimArray::NDimArray() - INTERNAL_ERROR: This is the private constructor and should never be used";
64  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
65  throw libdap::InternalErr(__FILE__, __LINE__, msg);
66 }
67 #endif
68 
69 NDimensionalArray::NDimensionalArray(libdap::Array *a) :
70  _dapType(dods_null_c), _shape(0), _currentLastDimensionSlabIndex(0), _totalValueCount(0), _sizeOfValue(0), _storage(
71  0)
72 {
73  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(libdap::Array *) - BEGIN"<< endl);
74 
75  _shape = new vector<unsigned int>(a->dimensions(true), (unsigned int) 1);
76  _totalValueCount = computeConstrainedShape(a, _shape);
77  BESDEBUG(NDimensionalArray_debug_key,
78  "NDimensionalArray::NDimensionalArray() - _shape" <<vectorToIndices(_shape) << endl);
79  _dapType = a->var()->type();
80  BESDEBUG(NDimensionalArray_debug_key,
81  "NDimensionalArray::NDimensionalArray() - Total Value Count: " << _totalValueCount << " element(s) of type '"<< libdap::type_name(_dapType) << "'" << endl);
82 
83  allocateStorage(_totalValueCount, _dapType);
84  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(libdap::Array *) - END"<< endl);
85 }
86 
87 NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *shape, libdap::Type dapType) :
88  _dapType(dods_null_c), _shape(0), _currentLastDimensionSlabIndex(0), _totalValueCount(0), _sizeOfValue(0), _storage(
89  0)
90 {
91  BESDEBUG(NDimensionalArray_debug_key,
92  "NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *, libdap::Type) - BEGIN"<< endl);
93 
94  _shape = new vector<unsigned int>(*shape);
95  _totalValueCount = computeArraySizeFromShapeVector(_shape);
96  _dapType = dapType;
97  BESDEBUG(NDimensionalArray_debug_key,
98  "NDimensionalArray::NDimensionalArray() - _shape" <<vectorToIndices(_shape) << endl);
99  BESDEBUG(NDimensionalArray_debug_key,
100  "NDimensionalArray::NDimensionalArray() - Total Value Count: " << _totalValueCount << " element(s) of type '"<< libdap::type_name(_dapType) << "'" << endl);
101  allocateStorage(_totalValueCount, _dapType);
102  BESDEBUG(NDimensionalArray_debug_key,
103  "NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *, libdap::Type) - END"<< endl);
104 
105 }
106 
107 NDimensionalArray::~NDimensionalArray()
108 {
109  delete[] (char *) _storage;
110  delete _shape;
111 }
112 
120 void *NDimensionalArray::relinquishStorage()
121 {
122  void *s = _storage;
123  _storage = 0;
124  return s;
125 }
126 
131 long NDimensionalArray::computeConstrainedShape(libdap::Array *a, vector<unsigned int> *shape)
132 {
133  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - BEGIN." << endl);
134 
135  libdap::Array::Dim_iter dIt;
136  unsigned int start;
137  unsigned int stride;
138  unsigned int stop;
139 
140  unsigned int dimSize = 1;
141  int dimNum = 0;
142  long totalSize = 1;
143 
144  BESDEBUG(NDimensionalArray_debug_key,
145  "NDimensionalArray::computeConstrainedShape() - Array has " << a->dimensions(true) << " dimensions."<< endl);
146 
147  stringstream msg;
148 
149  for (dIt = a->dim_begin(); dIt != a->dim_end(); dIt++) {
150  BESDEBUG(NDimensionalArray_debug_key,
151  "NDimensionalArray::computeConstrainedShape() - Processing dimension '" << a->dimension_name(dIt)<< "'. (dim# "<< dimNum << ")"<< endl);
152  start = a->dimension_start(dIt, true);
153  stride = a->dimension_stride(dIt, true);
154  stop = a->dimension_stop(dIt, true);
155  BESDEBUG(NDimensionalArray_debug_key,
156  "NDimensionalArray::computeConstrainedShape() - start: " << start << " stride: " << stride << " stop: "<<stop<< endl);
157 
158  dimSize = 1 + ((stop - start) / stride);
159  BESDEBUG(NDimensionalArray_debug_key,
160  "NDimensionalArray::computeConstrainedShape() - dimSize: " << dimSize << endl);
161 
162  (*shape)[dimNum++] = dimSize;
163  totalSize *= dimSize;
164  }
165  BESDEBUG(NDimensionalArray_debug_key,
166  "NDimensionalArray::computeConstrainedShape() - totalSize: " << totalSize << endl);
167  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - END." << endl);
168 
169  return totalSize;
170 }
171 
172 void NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray(libdap::Array *a,
173  vector<unsigned int> *location)
174 {
175  BESDEBUG(NDimensionalArray_debug_key,
176  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - BEGIN." << endl);
177 
178  libdap::Array::Dim_iter dIt;
179  libdap::Array::Dim_iter next_dIt;
180  unsigned int start;
181  unsigned int stride;
182  unsigned int stop;
183 
184  int dimNum = 0;
185 
186  BESDEBUG(NDimensionalArray_debug_key,
187  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Array has " << a->dimensions(true) << " dimensions."<< endl);
188 
189  stringstream msg;
190 
191  for (dIt = a->dim_begin(); dIt != a->dim_end(); dIt++) {
192  next_dIt = dIt;
193  next_dIt++;
194 
195  BESDEBUG(NDimensionalArray_debug_key,
196  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Processing dimension '" << a->dimension_name(dIt)<< "'. (dim# "<< dimNum << ")"<< endl);
197  start = a->dimension_start(dIt, true);
198  stride = a->dimension_stride(dIt, true);
199  stop = a->dimension_stop(dIt, true);
200  BESDEBUG(NDimensionalArray_debug_key,
201  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - start: " << start << " stride: " << stride << " stop: "<<stop<< endl);
202 
203  if (next_dIt != a->dim_end() && start != stop && stride != 1) {
204  msg << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - The array '" << a->name()
205  << "' has not been constrained to a last dimension hyperslab.";
206  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
207  throw Error(msg.str());
208 
209  }
210  if (next_dIt == a->dim_end()) {
211  if (start != 0 || stride != 1 || stop != ((unsigned int) a->dimension_size(dIt) - 1)) {
212  msg << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - The array '" << a->name()
213  << "' has not been constrained to a last dimension hyperslab.";
214  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
215  throw Error(msg.str());
216 
217  }
218 
219  BESDEBUG(NDimensionalArray_debug_key,
220  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - location"<< vectorToIndices(location) << endl);
221  BESDEBUG(NDimensionalArray_debug_key,
222  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - END." << endl);
223  return;
224  }
225 
226  BESDEBUG(NDimensionalArray_debug_key,
227  "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Adding location "<< start << " to dimension " << location->size() << endl);
228 
229  location->push_back(start);
230  }
231 
232  msg
233  << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Method Failure - this line should never be reached.";
234  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
235  throw Error(msg.str());
236 
237 }
238 
242 long NDimensionalArray::computeArraySizeFromShapeVector(vector<unsigned int> *shape)
243 {
244  long totalSize = 1;
245 
246  for (unsigned int i = 0; i < shape->size(); i++) {
247  totalSize *= (*shape)[i];
248  }
249 
250  return totalSize;
251 }
252 
256 void NDimensionalArray::allocateStorage(long numValues, Type dapType)
257 {
258 
259  BESDEBUG(NDimensionalArray_debug_key,
260  "NDimensionalArray::allocateStorage() - Allocating memory for " << numValues << " element(s) of type '"<< libdap::type_name(dapType) << "'" << endl);
261 
262  switch (dapType) {
263  case dods_byte_c:
264  _sizeOfValue = sizeof(dods_byte);
265  break;
266  case dods_int16_c:
267  _sizeOfValue = sizeof(dods_int16);
268  break;
269  case dods_uint16_c:
270  _sizeOfValue = sizeof(dods_uint16);
271  break;
272  case dods_int32_c:
273  _sizeOfValue = sizeof(dods_int32);
274  break;
275  case dods_uint32_c:
276  _sizeOfValue = sizeof(dods_uint32);
277  break;
278  case dods_float32_c:
279  _sizeOfValue = sizeof(dods_float32);
280  break;
281  case dods_float64_c:
282  _sizeOfValue = sizeof(dods_float64);
283  break;
284  default:
285  throw InternalErr(__FILE__, __LINE__, "Unknown DAP type encountered when constructing NDimensionalArray");
286  }
287 
288  _storage = new char[numValues * _sizeOfValue];
289 
290 }
291 
295 void NDimensionalArray::confirmStorage()
296 {
297  if (_storage == 0) {
298  string msg =
299  "ERROR - NDimensionalArray storage has been relinquished. Instance is no longer viable for set/get operations.";
300  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
301  throw InternalErr(__FILE__, __LINE__, msg);
302  }
303 }
304 
308 void NDimensionalArray::confirmType(Type dapType)
309 {
310  if (_dapType != dapType) {
311  string msg = "NDimensionalArray::setValue() - Passed value does not match template array type. Expected "
312  + libdap::type_name(_dapType) + " received " + libdap::type_name(dapType);
313  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
314  throw InternalErr(__FILE__, __LINE__, msg);
315  }
316 }
317 
321 void NDimensionalArray::confirmLastDimSize(unsigned int n)
322 {
323  unsigned long elementCount = getLastDimensionElementCount();
324  if (elementCount != n) {
325  string msg =
326  "NDimensionalArray::setLastDimensionHyperSlab() - Passed valueCount does not match size of last dimension hyper-slab. ";
327  msg += "Last dimension hyper-slab has " + libdap::long_to_string(elementCount) + " elements. ";
328  msg += "Received a valueCount of " + libdap::long_to_string(n);
329  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
330  throw InternalErr(__FILE__, __LINE__, msg);
331  }
332 
333 }
334 
340 dods_byte NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_byte value)
341 {
342 
343  confirmStorage();
344  confirmType(dods_byte_c);
345 
346  unsigned int storageIndex = getStorageIndex(_shape, location);
347  dods_byte *_store = static_cast<dods_byte*>(_storage);
348  dods_byte oldValue = _store[storageIndex];
349  _store[storageIndex] = value;
350  return oldValue;
351 }
352 
358 dods_int16 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_int16 value)
359 {
360 
361  confirmStorage();
362  confirmType(dods_int16_c);
363 
364  unsigned int storageIndex = getStorageIndex(_shape, location);
365  dods_int16 *_store = static_cast<dods_int16 *>(_storage);
366  dods_int16 oldValue = _store[storageIndex];
367  _store[storageIndex] = value;
368  return oldValue;
369 }
370 
376 dods_uint16 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_uint16 value)
377 {
378  confirmStorage();
379  confirmType(dods_uint16_c);
380 
381  unsigned int storageIndex = getStorageIndex(_shape, location);
382  dods_uint16 *_store = static_cast<dods_uint16 *>(_storage);
383  dods_uint16 oldValue = _store[storageIndex];
384  _store[storageIndex] = value;
385  return oldValue;
386 }
387 
393 dods_int32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_int32 value)
394 {
395  confirmStorage();
396  confirmType(dods_int32_c);
397 
398  unsigned int storageIndex = getStorageIndex(_shape, location);
399  dods_int32 *_store = static_cast<dods_int32 *>(_storage);
400  dods_int32 oldValue = _store[storageIndex];
401  _store[storageIndex] = value;
402  return oldValue;
403 }
404 
410 dods_uint32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_uint32 value)
411 {
412  confirmStorage();
413  confirmType(dods_uint32_c);
414 
415  unsigned int storageIndex = getStorageIndex(_shape, location);
416  dods_uint32 *_store = static_cast<dods_uint32 *>(_storage);
417  dods_uint32 oldValue = _store[storageIndex];
418  _store[storageIndex] = value;
419  return oldValue;
420 }
421 
427 dods_float32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_float32 value)
428 {
429  confirmStorage();
430  confirmType(dods_float32_c);
431 
432  unsigned int storageIndex = getStorageIndex(_shape, location);
433  dods_float32 *_store = static_cast<dods_float32 *>(_storage);
434  dods_float32 oldValue = _store[storageIndex];
435  _store[storageIndex] = value;
436  return oldValue;
437 }
438 
444 dods_float64 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_float64 value)
445 {
446  confirmStorage();
447  confirmType(dods_float64_c);
448 
449  unsigned int storageIndex = getStorageIndex(_shape, location);
450  dods_float64 *_store = static_cast<dods_float64 *>(_storage);
451  dods_float64 oldValue = _store[storageIndex];
452  _store[storageIndex] = value;
453  return oldValue;
454 }
455 
461 void NDimensionalArray::getLastDimensionHyperSlab(std::vector<unsigned int> *location, void **slab,
462  unsigned int *elementCount)
463 {
464  BESDEBUG(NDimensionalArray_debug_key, endl<< endl <<"NDimensionalArray::getLastDimensionHyperSlab() - BEGIN"<<endl);
465  confirmStorage();
466  if (location->size() != _shape->size() - 1) {
467  string msg =
468  "NDimensionalArray::getLastDimensionHyperSlab() - Passed location vector doesn't match array shape.";
469  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
470  throw InternalErr(__FILE__, __LINE__, msg);
471  }
472 
473  BESDEBUG(NDimensionalArray_debug_key,
474  "NDimensionalArray::getLastDimensionHyperSlab() - location" <<vectorToIndices(location) << endl);
475 
476  vector<unsigned int> slabLocation(*location);
477 
478  slabLocation.push_back(0);
479  BESDEBUG(NDimensionalArray_debug_key,
480  "NDimensionalArray::getLastDimensionHyperSlab() - slabLocation" <<vectorToIndices(&slabLocation) << endl);
481 
482  unsigned int storageIndex = getStorageIndex(_shape, &slabLocation);
483 
484  *slab = &((char *) _storage)[storageIndex * _sizeOfValue];
485  *elementCount = *(_shape->rbegin());
486  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getLastDimensionHyperSlab() - END"<<endl<<endl);
487 
488 }
489 
490 void NDimensionalArray::getNextLastDimensionHyperSlab(void **slab)
491 {
492 
493  unsigned int storageIndex = _shape->back() * _currentLastDimensionSlabIndex++;
494  BESDEBUG(NDimensionalArray_debug_key,
495  "NDimensionalArray::getNextLastDimensionHyperSlab() - Storage Index:"<< libdap::long_to_string(storageIndex) << endl);
496  *slab = &((char *) _storage)[storageIndex * _sizeOfValue];
497 
498 }
499 
504 long NDimensionalArray::getStorageIndex(vector<unsigned int> *shape, vector<unsigned int> *location)
505 {
506  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - BEGIN." << endl);
507  long storageIndex = 0;
508 
509  if (location->size() != shape->size()) {
510  string msg = "getStorageIndex() - The supplied location vector does not match array shape.";
511  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
512  throw Error(msg);
513  }
514 
515  BESDEBUG(NDimensionalArray_debug_key,
516  "NDimensionalArray::getStorageIndex() - Shape and location have the same number of elements." << endl);
517 
518  long dimIndex = 0;
519  long chunkSize = 1;
520 
521  for (dimIndex = shape->size() - 1; dimIndex >= 0; dimIndex--) {
522  BESDEBUG(NDimensionalArray_debug_key,
523  "NDimensionalArray::getStorageIndex() - dimIndex=" << libdap::long_to_string(dimIndex) << endl);
524 
525  if ((*location)[dimIndex] >= (*shape)[dimIndex]) {
526  string msg =
527  "NDimensionalArray::getStorageIndex() - The location vector references a value that does not match the array shape. ";
528  msg += "location[" + libdap::long_to_string(dimIndex) + "]=";
529  msg += libdap::long_to_string((*location)[dimIndex]) + " ";
530  msg += "shape[" + libdap::long_to_string(dimIndex) + "]=";
531  msg += libdap::long_to_string((*shape)[dimIndex]) + " ";
532  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
533  throw Error(msg);
534  }
535  storageIndex += chunkSize * ((*location)[dimIndex]);
536  chunkSize *= ((*shape)[dimIndex]);
537  }
538 
539  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - END." << endl);
540  return storageIndex;
541 }
542 
548 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_byte *values,
549  unsigned int valueCount)
550 {
551  confirmType(dods_byte_c);
552  confirmLastDimSize(valueCount);
553  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_byte));
554 }
555 
561 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_int16 *values,
562  unsigned int valueCount)
563 {
564  confirmType(dods_int16_c);
565  confirmLastDimSize(valueCount);
566  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_int16));
567 }
568 
574 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_uint16 *values,
575  unsigned int valueCount)
576 {
577  confirmType(dods_uint16_c);
578  confirmLastDimSize(valueCount);
579  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_uint16));
580 }
581 
587 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_int32 *values,
588  unsigned int valueCount)
589 {
590  confirmType(dods_int32_c);
591  confirmLastDimSize(valueCount);
592  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_int32));
593 }
594 
600 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_uint32 *values,
601  unsigned int valueCount)
602 {
603  confirmType(dods_uint32_c);
604  confirmLastDimSize(valueCount);
605  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_uint32));
606 }
607 
613 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_float32 *values,
614  unsigned int valueCount)
615 {
616  confirmType(dods_float32_c);
617  confirmLastDimSize(valueCount);
618  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_float32));
619 }
620 
626 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_float64 *values,
627  unsigned int valueCount)
628 {
629  confirmType(dods_float64_c);
630  confirmLastDimSize(valueCount);
631  setLastDimensionHyperSlab(location, (void *) values, valueCount * sizeof(dods_float64));
632 }
633 
638 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, void *values,
639  unsigned int byteCount)
640 {
641  confirmStorage();
642  void *slab;
643  unsigned int slabElementCount;
644 
645  getLastDimensionHyperSlab(location, &slab, &slabElementCount);
646  memcpy(slab, values, byteCount);
647 
648 }
649 
653 void NDimensionalArray::setAll(char val)
654 {
655  confirmStorage();
656  memset(_storage, val, _totalValueCount * _sizeOfValue);
657 
658 }
659 
663 long NDimensionalArray::getLastDimensionElementCount()
664 {
665  return *(_shape->rbegin());
666 }
667 
668 libdap::Array *NDimensionalArray::getArray(libdap::Array *templateArray)
669 {
670 
671  if (_shape->size() != templateArray->dimensions(true))
672  throw Error("Template Array has different number of dimensions than NDimensional Array!!");
673 
674  libdap::Array *resultDapArray;
675 
676  switch (_dapType) {
677  case dods_byte_c: {
678  libdap::Byte tt(templateArray->name());
679  resultDapArray = new libdap::Array(templateArray->name(), &tt);
680  break;
681  }
682  case dods_uint16_c: {
683  libdap::Int16 tt(templateArray->name());
684  resultDapArray = new libdap::Array(templateArray->name(), &tt);
685  break;
686  }
687  case dods_int16_c: {
688  libdap::UInt16 tt(templateArray->name());
689  resultDapArray = new libdap::Array(templateArray->name(), &tt);
690  break;
691  }
692  case dods_int32_c: {
693  libdap::Int32 tt(templateArray->name());
694  resultDapArray = new libdap::Array(templateArray->name(), &tt);
695  break;
696  }
697  case dods_uint32_c: {
698  libdap::UInt32 tt(templateArray->name());
699  resultDapArray = new libdap::Array(templateArray->name(), &tt);
700  break;
701  }
702  case dods_float32_c: {
703  libdap::Float32 tt(templateArray->name());
704  resultDapArray = new libdap::Array(templateArray->name(), &tt);
705  break;
706  }
707  case dods_float64_c: {
708  libdap::Float64 tt(templateArray->name());
709  resultDapArray = new libdap::Array(templateArray->name(), &tt);
710  break;
711  }
712  default:
713  throw InternalErr(__FILE__, __LINE__,
714  "Unknown DAP type encountered when converting to gridfields internal type.");
715  }
716 
717  libdap::Array::Dim_iter dimIt;
718  int s = 0;
719  for (dimIt = templateArray->dim_begin(); dimIt != templateArray->dim_end(); dimIt++, s++) {
720  resultDapArray->append_dim((*_shape)[s], (*dimIt).name);
721  }
722 
723  // Copy the source objects attributes.
724  BESDEBUG(NDimensionalArray_debug_key,
725  "TwoDMeshTopology::getGFAttributeAsDapArray() - Copying libdap::Attribute's from template array " << templateArray->name() << endl);
726  resultDapArray->set_attr_table(templateArray->get_attr_table());
727 
728  switch (_dapType) {
729  case dods_byte_c: {
730  resultDapArray->set_value((dods_byte *) _storage, _totalValueCount);
731  break;
732  }
733  case dods_uint16_c: {
734  resultDapArray->set_value((dods_uint16 *) _storage, _totalValueCount);
735  break;
736  }
737  case dods_int16_c: {
738  resultDapArray->set_value((dods_int16 *) _storage, _totalValueCount);
739  break;
740  }
741  case dods_uint32_c: {
742  resultDapArray->set_value((dods_uint32 *) _storage, _totalValueCount);
743  break;
744  }
745  case dods_int32_c: {
746  resultDapArray->set_value((dods_int32 *) _storage, _totalValueCount);
747  break;
748  }
749  case dods_float32_c: {
750  resultDapArray->set_value((dods_float32 *) _storage, _totalValueCount);
751  break;
752  }
753  case dods_float64_c: {
754  resultDapArray->set_value((dods_float64 *) _storage, _totalValueCount);
755  break;
756  }
757  default:
758  throw InternalErr(__FILE__, __LINE__,
759  "Unknown DAP type encountered when converting to gridfields internal type.");
760  }
761 
762  return resultDapArray;
763 }
764 
765 string NDimensionalArray::toString_worker(vector<unsigned int> *location)
766 {
767 
768  stringstream s;
769  if (location->size() == _shape->size()) {
770  s << " storage";
771  s << vectorToIndices(location);
772 
773  s << ": ";
774  long storageIndex = getStorageIndex(_shape, location);
775  switch (_dapType) {
776  case dods_byte_c: {
777  s << ((dods_byte *) _storage)[storageIndex];
778  break;
779  }
780  case dods_uint16_c: {
781  s << ((dods_uint16 *) _storage)[storageIndex];
782  break;
783  }
784  case dods_int16_c: {
785  s << ((dods_int16 *) _storage)[storageIndex];
786  break;
787  }
788  case dods_uint32_c: {
789  s << ((dods_uint32 *) _storage)[storageIndex];
790  break;
791  }
792  case dods_int32_c: {
793  s << ((dods_int32 *) _storage)[storageIndex];
794  break;
795  }
796  case dods_float32_c: {
797  s << ((dods_float32 *) _storage)[storageIndex];
798  break;
799  }
800  case dods_float64_c: {
801  s << ((dods_float64 *) _storage)[storageIndex];
802  break;
803  }
804  default:
805  throw InternalErr(__FILE__, __LINE__,
806  "Unknown DAP type encountered when converting to gridfields internal type.");
807  }
808  s << endl;
809 
810  }
811  else {
812  int nextDimSize = (*_shape)[location->size()];
813  for (int i = 0; i < nextDimSize; i++) {
814  location->push_back(i);
815  s << toString_worker(location);
816  location->pop_back();
817  }
818 
819  }
820  return s.str();
821 
822 }
823 
824 string NDimensionalArray::toString()
825 {
826 
827  stringstream s;
828  vector<unsigned int> location;
829 
830  s << endl << "NDimensionalArray: " << endl;
831  s << toString_worker(&location);
832 
833  return s.str();
834 
835 }
836 
837 } /* namespace libdap */
Type
Type
Type of JSON value.
Definition: cmr_module/rapidjson/rapidjson.h:603
libdap
Definition: BESDapFunctionResponseCache.h:35
Error