OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
NDimensionalArray.cc
Go to the documentation of this file.
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 <sstream> // std::stringstream
26 #include <string.h>
27 
28 
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 
42 namespace libdap {
43 
44 
45 string NDimensionalArray::vectorToIndices(vector<unsigned int> *v){
46  stringstream s;
47  for(unsigned int i=0; i<v->size() ;i++){
48  s << "[" << (*v)[i] << "]";
49  }
50  return s.str();
51 }
52 
53 
54 NDimensionalArray::NDimensionalArray()
55  :_dapType(dods_null_c),_shape(0),_currentLastDimensionSlabIndex(0),_totalValueCount(0),_sizeOfValue(0),_storage(0) {
56 
57  string msg = "NDimArray::NDimArray() - INTERNAL_ERROR: This is the private constructor and should never be used";
58  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
59  throw libdap::InternalErr(__FILE__, __LINE__, msg);
60 }
61 
62 
63 NDimensionalArray::NDimensionalArray(libdap::Array *a)
64  :_dapType(dods_null_c),_shape(0),_currentLastDimensionSlabIndex(0),_totalValueCount(0),_sizeOfValue(0),_storage(0) {
65  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(libdap::Array *) - BEGIN"<< endl);
66 
67  _shape = new vector<unsigned int>(a->dimensions(true), (unsigned int)1);
68  _totalValueCount = computeConstrainedShape(a, _shape);
69  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray() - _shape" <<vectorToIndices(_shape) << endl);
70  _dapType = a->var()->type();
71  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray() - Total Value Count: " << _totalValueCount << " element(s) of type '"<< libdap::type_name(_dapType) << "'" << endl);
72 
73  allocateStorage(_totalValueCount, _dapType);
74  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(libdap::Array *) - END"<< endl);
75 }
76 
77 
78 NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *shape, libdap::Type dapType)
79  :_dapType(dods_null_c),_shape(0),_currentLastDimensionSlabIndex(0),_totalValueCount(0),_sizeOfValue(0),_storage(0) {
80  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *, libdap::Type) - BEGIN"<< endl);
81 
82  _shape = new vector<unsigned int>(*shape);
83  _totalValueCount = computeArraySizeFromShapeVector(_shape);
84  _dapType = dapType;
85  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray() - _shape" <<vectorToIndices(_shape) << endl);
86  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray() - Total Value Count: " << _totalValueCount << " element(s) of type '"<< libdap::type_name(_dapType) << "'" << endl);
87  allocateStorage(_totalValueCount, _dapType);
88  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::NDimensionalArray(std::vector<unsigned int> *, libdap::Type) - END"<< endl);
89 
90 }
91 
92 
94  delete [] (char *) _storage;
95  delete _shape;
96 }
97 
106  void *s = _storage;
107  _storage = 0;
108  return s;
109 }
110 
111 
112 
117 long NDimensionalArray::computeConstrainedShape(libdap::Array *a, vector<unsigned int> *shape ){
118  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - BEGIN." << endl);
119 
120  libdap::Array::Dim_iter dIt;
121  unsigned int start;
122  unsigned int stride;
123  unsigned int stop;
124 
125  unsigned int dimSize = 1;
126  int dimNum = 0;
127  long totalSize = 1;
128 
129  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - Array has " << a->dimensions(true) << " dimensions."<< endl);
130 
131  stringstream msg;
132 
133  for(dIt = a->dim_begin() ; dIt!=a->dim_end() ;dIt++){
134  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - Processing dimension '" << a->dimension_name(dIt)<< "'. (dim# "<< dimNum << ")"<< endl);
135  start = a->dimension_start(dIt, true);
136  stride = a->dimension_stride(dIt, true);
137  stop = a->dimension_stop(dIt, true);
138  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - start: " << start << " stride: " << stride << " stop: "<<stop<< endl);
139 
140  dimSize = 1 + ( (stop - start) / stride);
141  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - dimSize: " << dimSize << endl);
142 
143  (*shape)[dimNum++] = dimSize;
144  totalSize *= dimSize;
145  }
146  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - totalSize: " << totalSize << endl);
147  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::computeConstrainedShape() - END." << endl);
148 
149  return totalSize;
150 }
151 
152 void NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray(libdap::Array *a, vector<unsigned int> *location ){
153  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - BEGIN." << endl);
154 
155 
156  libdap::Array::Dim_iter dIt;
157  libdap::Array::Dim_iter next_dIt;
158  unsigned int start;
159  unsigned int stride;
160  unsigned int stop;
161 
162  int dimNum = 0;
163 
164  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Array has " << a->dimensions(true) << " dimensions."<< endl);
165 
166  stringstream msg;
167 
168  for(dIt =a->dim_begin() ; dIt!=a->dim_end() ;dIt++){
169  next_dIt = dIt;
170  next_dIt++;
171 
172  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Processing dimension '" << a->dimension_name(dIt)<< "'. (dim# "<< dimNum << ")"<< endl);
173  start = a->dimension_start(dIt, true);
174  stride = a->dimension_stride(dIt, true);
175  stop = a->dimension_stop(dIt, true);
176  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - start: " << start << " stride: " << stride << " stop: "<<stop<< endl);
177 
178  if(next_dIt!=a->dim_end() && start!=stop && stride!=1){
179  msg << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - The array '" << a->name() <<"' has not been constrained to a last dimension hyperslab.";
180  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
181  throw Error(msg.str());
182 
183  }
184  if(next_dIt==a->dim_end()){
185  if( start!=0 || stride!=1 || stop!=((unsigned int)a->dimension_size(dIt)-1)){
186  msg << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - The array '" << a->name() <<"' has not been constrained to a last dimension hyperslab.";
187  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
188  throw Error(msg.str());
189 
190  }
191 
192  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - location"<< vectorToIndices(location) << endl);
193  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - END." << endl);
194  return;
195  }
196 
197  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Adding location "<< start << " to dimension " << location->size() << endl);
198 
199 
200  location->push_back(start);
201  }
202 
203 
204  msg << "retrieveLastDimHyperSlabLocationFromConstrainedArrray() - Method Failure - this line should never be reached.";
205  BESDEBUG(NDimensionalArray_debug_key, msg.str() << endl);
206  throw Error(msg.str());
207 
208 
209 }
210 
211 
212 
213 
214 
218 long NDimensionalArray::computeArraySizeFromShapeVector(vector<unsigned int> *shape ){
219  long totalSize = 1;
220 
221  for(unsigned int i=0; i<shape->size(); i++){
222  totalSize *= (*shape)[i];
223  }
224 
225  return totalSize;
226 }
227 
228 
232 void NDimensionalArray::allocateStorage(long numValues, Type dapType){
233 
234  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::allocateStorage() - Allocating memory for " << numValues << " element(s) of type '"<< libdap::type_name(dapType) << "'" << endl);
235 
236  switch (dapType) {
237  case dods_byte_c:
238  _sizeOfValue = sizeof(dods_byte);
239  break;
240  case dods_int16_c:
241  _sizeOfValue = sizeof(dods_int16);
242  break;
243  case dods_uint16_c:
244  _sizeOfValue = sizeof(dods_uint16);
245  break;
246  case dods_int32_c:
247  _sizeOfValue = sizeof(dods_int32);
248  break;
249  case dods_uint32_c:
250  _sizeOfValue = sizeof(dods_uint32);
251  break;
252  case dods_float32_c:
253  _sizeOfValue = sizeof(dods_float32);
254  break;
255  case dods_float64_c:
256  _sizeOfValue = sizeof(dods_float64);
257  break;
258  default:
259  throw InternalErr(__FILE__, __LINE__,
260  "Unknown DAP type encountered when constructing NDimensionalArray");
261  }
262 
263  _storage = new char[numValues * _sizeOfValue];
264 
265 }
266 
270 void NDimensionalArray::confirmStorage(){
271  if(_storage==0){
272  string msg = "ERROR - NDimensionalArray storage has been relinquished. Instance is no longer viable for set/get operations.";
273  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
274  throw InternalErr(__FILE__, __LINE__, msg);
275  }
276 }
277 
278 
282 void NDimensionalArray::confirmType(Type dapType){
283  if(_dapType != dapType){
284  string msg = "NDimensionalArray::setValue() - Passed value does not match template array type. Expected "
285  + libdap::type_name(_dapType) + " received "+ libdap::type_name(dapType);
286  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
287  throw InternalErr(__FILE__, __LINE__, msg);
288  }
289 }
290 
291 
295 void NDimensionalArray::confirmLastDimSize(unsigned int n){
296  unsigned long elementCount = getLastDimensionElementCount();
297  if(elementCount != n){
298  string msg = "NDimensionalArray::setLastDimensionHyperSlab() - Passed valueCount does not match size of last dimension hyper-slab. ";
299  msg += "Last dimension hyper-slab has " + libdap::long_to_string(elementCount) + " elements. ";
300  msg += "Received a valueCount of "+ libdap::long_to_string(n);
301  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
302  throw InternalErr(__FILE__, __LINE__, msg);
303  }
304 
305 }
306 
307 
308 
309 
315 dods_byte NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_byte value){
316 
317  confirmStorage();
318  confirmType(dods_byte_c);
319 
320  unsigned int storageIndex = getStorageIndex(_shape,location);
321  dods_byte *_store = static_cast<dods_byte*>(_storage);
322  dods_byte oldValue = _store[storageIndex];
323  _store[storageIndex] = value;
324  return oldValue;
325 }
326 
332 dods_int16 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_int16 value){
333 
334  confirmStorage();
335  confirmType(dods_int16_c);
336 
337  unsigned int storageIndex = getStorageIndex(_shape,location);
338  dods_int16 *_store = static_cast<dods_int16 *>(_storage);
339  dods_int16 oldValue = _store[storageIndex];
340  _store[storageIndex] = value;
341  return oldValue;
342 }
343 
349 dods_uint16 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_uint16 value){
350  confirmStorage();
351  confirmType(dods_uint16_c);
352 
353  unsigned int storageIndex = getStorageIndex(_shape,location);
354  dods_uint16 *_store = static_cast<dods_uint16 *>(_storage);
355  dods_uint16 oldValue = _store[storageIndex];
356  _store[storageIndex] = value;
357  return oldValue;
358 }
359 
365 dods_int32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_int32 value){
366  confirmStorage();
367  confirmType(dods_int32_c);
368 
369  unsigned int storageIndex = getStorageIndex(_shape,location);
370  dods_int32 *_store = static_cast<dods_int32 *>(_storage);
371  dods_int32 oldValue = _store[storageIndex];
372  _store[storageIndex] = value;
373  return oldValue;
374 }
375 
381 dods_uint32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_uint32 value){
382  confirmStorage();
383  confirmType(dods_uint32_c);
384 
385  unsigned int storageIndex = getStorageIndex(_shape,location);
386  dods_uint32 *_store = static_cast<dods_uint32 *>(_storage);
387  dods_uint32 oldValue = _store[storageIndex];
388  _store[storageIndex] = value;
389  return oldValue;
390 }
391 
397 dods_float32 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_float32 value){
398  confirmStorage();
399  confirmType(dods_float32_c);
400 
401  unsigned int storageIndex = getStorageIndex(_shape,location);
402  dods_float32 *_store = static_cast<dods_float32 *>(_storage);
403  dods_float32 oldValue = _store[storageIndex];
404  _store[storageIndex] = value;
405  return oldValue;
406 }
407 
413 dods_float64 NDimensionalArray::setValue(std::vector<unsigned int> *location, dods_float64 value){
414  confirmStorage();
415  confirmType(dods_float64_c);
416 
417  unsigned int storageIndex = getStorageIndex(_shape,location);
418  dods_float64 *_store = static_cast<dods_float64 *>(_storage);
419  dods_float64 oldValue = _store[storageIndex];
420  _store[storageIndex] = value;
421  return oldValue;
422 }
423 
424 
430 void NDimensionalArray::getLastDimensionHyperSlab(std::vector<unsigned int> *location, void **slab, unsigned int *elementCount){
431  BESDEBUG(NDimensionalArray_debug_key, endl<< endl <<"NDimensionalArray::getLastDimensionHyperSlab() - BEGIN"<<endl);
432  confirmStorage();
433  if(location->size()!=_shape->size()-1){
434  string msg = "NDimensionalArray::getLastDimensionHyperSlab() - Passed location vector doesn't match array shape.";
435  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
436  throw InternalErr(__FILE__, __LINE__, msg);
437  }
438 
439 
440  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getLastDimensionHyperSlab() - location" <<vectorToIndices(location) << endl);
441 
442 
443  vector<unsigned int> slabLocation(*location);
444 
445 
446  slabLocation.push_back(0);
447  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getLastDimensionHyperSlab() - slabLocation" <<vectorToIndices(&slabLocation) << endl);
448 
449  unsigned int storageIndex = getStorageIndex(_shape, &slabLocation);
450 
451  *slab = &((char *)_storage)[storageIndex*_sizeOfValue];
452  *elementCount = *(_shape->rbegin());
453  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getLastDimensionHyperSlab() - END"<<endl<<endl);
454 
455 }
456 
458 
459  unsigned int storageIndex = _shape->back() * _currentLastDimensionSlabIndex++;
460  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getNextLastDimensionHyperSlab() - Storage Index:"<< libdap::long_to_string(storageIndex) << endl);
461  *slab = &((char *)_storage)[storageIndex*_sizeOfValue];
462 
463 }
464 
469 long NDimensionalArray::getStorageIndex(vector<unsigned int> *shape, vector<unsigned int> *location){
470  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - BEGIN." << endl);
471  long storageIndex = 0;
472 
473 
474  if(location->size() != shape->size()){
475  string msg = "getStorageIndex() - The supplied location vector does not match array shape.";
476  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
477  throw Error(msg);
478  }
479 
480  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - Shape and location have the same number of elements." << endl);
481 
482  long dimIndex = 0;
483  long chunkSize = 1;
484 
485  for(dimIndex = shape->size()-1 ; dimIndex >=0 ; dimIndex--){
486  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - dimIndex=" << libdap::long_to_string(dimIndex) << endl);
487 
488  if((*location)[dimIndex] >= (*shape)[dimIndex]){
489  string msg = "NDimensionalArray::getStorageIndex() - The location vector references a value that does not match the array shape. ";
490  msg += "location[" + libdap::long_to_string(dimIndex) + "]=";
491  msg += libdap::long_to_string((*location)[dimIndex]) + " ";
492  msg += "shape[" + libdap::long_to_string(dimIndex) + "]=";
493  msg += libdap::long_to_string((*shape)[dimIndex]) + " ";
494  BESDEBUG(NDimensionalArray_debug_key, msg << endl);
495  throw Error(msg);
496  }
497  storageIndex += chunkSize * ((*location)[dimIndex]);
498  chunkSize *= ((*shape)[dimIndex]);
499  }
500 
501  BESDEBUG(NDimensionalArray_debug_key, "NDimensionalArray::getStorageIndex() - END." << endl);
502  return storageIndex;
503 }
504 
505 
506 
512 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_byte *values, unsigned int valueCount){
513  confirmType(dods_byte_c);
514  confirmLastDimSize(valueCount);
515  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_byte));
516 }
517 
518 
524 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_int16 *values, unsigned int valueCount){
525  confirmType(dods_int16_c);
526  confirmLastDimSize(valueCount);
527  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_int16));
528 }
529 
530 
536 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_uint16 *values, unsigned int valueCount){
537  confirmType(dods_uint16_c);
538  confirmLastDimSize(valueCount);
539  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_uint16));
540 }
541 
542 
548 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_int32 *values, unsigned int valueCount){
549  confirmType(dods_int32_c);
550  confirmLastDimSize(valueCount);
551  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_int32));
552 }
553 
554 
560 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_uint32 *values, unsigned int valueCount){
561  confirmType(dods_uint32_c);
562  confirmLastDimSize(valueCount);
563  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_uint32));
564 }
565 
566 
572 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_float32 *values, unsigned int valueCount){
573  confirmType(dods_float32_c);
574  confirmLastDimSize(valueCount);
575  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_float32));
576 }
577 
583 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, dods_float64 *values, unsigned int valueCount){
584  confirmType(dods_float64_c);
585  confirmLastDimSize(valueCount);
586  setLastDimensionHyperSlab(location, (void *) values, valueCount*sizeof(dods_float64));
587 }
588 
589 
590 
595 void NDimensionalArray::setLastDimensionHyperSlab(std::vector<unsigned int> *location, void *values, unsigned int byteCount){
596  confirmStorage();
597  void *slab;
598  unsigned int slabElementCount;
599 
600  getLastDimensionHyperSlab(location,&slab,&slabElementCount);
601  memcpy ( slab, values, byteCount );
602 
603 }
604 
609  confirmStorage();
610  memset(_storage,val,_totalValueCount*_sizeOfValue);
611 
612 }
613 
618  return *(_shape->rbegin());
619 }
620 
621 libdap::Array *NDimensionalArray::getArray(libdap::Array *templateArray){
622 
623  if(_shape->size() != templateArray->dimensions(true))
624  throw Error("Template Array has different number of dimensions than NDimensional Array!!");
625 
626  libdap::Array *resultDapArray;
627 
628  switch (_dapType) {
629  case dods_byte_c:
630  {
631  libdap::Byte tt(templateArray->name());
632  resultDapArray = new libdap::Array(templateArray->name(), &tt);
633  break;
634  }
635  case dods_uint16_c:
636  {
637  libdap::Int16 tt(templateArray->name());
638  resultDapArray = new libdap::Array(templateArray->name(), &tt);
639  break;
640  }
641  case dods_int16_c:
642  {
643  libdap::UInt16 tt(templateArray->name());
644  resultDapArray = new libdap::Array(templateArray->name(), &tt);
645  break;
646  }
647  case dods_int32_c:
648  {
649  libdap::Int32 tt(templateArray->name());
650  resultDapArray = new libdap::Array(templateArray->name(), &tt);
651  break;
652  }
653  case dods_uint32_c:
654  {
655  libdap::UInt32 tt(templateArray->name());
656  resultDapArray = new libdap::Array(templateArray->name(), &tt);
657  break;
658  }
659  case dods_float32_c:
660  {
661  libdap::Float32 tt(templateArray->name());
662  resultDapArray = new libdap::Array(templateArray->name(), &tt);
663  break;
664  }
665  case dods_float64_c:
666  {
667  libdap::Float64 tt(templateArray->name());
668  resultDapArray = new libdap::Array(templateArray->name(), &tt);
669  break;
670  }
671  default:
672  throw InternalErr(__FILE__, __LINE__,
673  "Unknown DAP type encountered when converting to gridfields internal type.");
674  }
675 
676 
677  libdap::Array::Dim_iter dimIt;
678  int s = 0;
679  for(dimIt=templateArray->dim_begin(); dimIt!=templateArray->dim_end() ; dimIt++, s++){
680  resultDapArray->append_dim((*_shape)[s], (*dimIt).name);
681  }
682 
683  // Copy the source objects attributes.
684  BESDEBUG(NDimensionalArray_debug_key, "TwoDMeshTopology::getGFAttributeAsDapArray() - Copying libdap::Attribute's from template array " << templateArray->name() << endl);
685  resultDapArray->set_attr_table(templateArray->get_attr_table());
686 
687 
688  switch (_dapType) {
689  case dods_byte_c:
690  {
691  resultDapArray->set_value((dods_byte *)_storage,_totalValueCount);
692  break;
693  }
694  case dods_uint16_c:
695  {
696  resultDapArray->set_value((dods_uint16 *)_storage,_totalValueCount);
697  break;
698  }
699  case dods_int16_c:
700  {
701  resultDapArray->set_value((dods_int16 *)_storage,_totalValueCount);
702  break;
703  }
704  case dods_uint32_c:
705  {
706  resultDapArray->set_value((dods_uint32 *)_storage,_totalValueCount);
707  break;
708  }
709  case dods_int32_c:
710  {
711  resultDapArray->set_value((dods_int32 *)_storage,_totalValueCount);
712  break;
713  }
714  case dods_float32_c:
715  {
716  resultDapArray->set_value((dods_float32 *)_storage,_totalValueCount);
717  break;
718  }
719  case dods_float64_c:
720  {
721  resultDapArray->set_value((dods_float64 *)_storage,_totalValueCount);
722  break;
723  }
724  default:
725  throw InternalErr(__FILE__, __LINE__,
726  "Unknown DAP type encountered when converting to gridfields internal type.");
727  }
728 
729 
730  return resultDapArray;
731 }
732 
733 
734 string NDimensionalArray::toString_worker(vector<unsigned int> *location) {
735 
736  stringstream s;
737  if(location->size() == _shape->size()){
738  s << " storage" ;
739  s << vectorToIndices(location);
740 
741  s << ": ";
742  long storageIndex = getStorageIndex(_shape,location);
743  switch (_dapType) {
744  case dods_byte_c:
745  {
746  s << ((dods_byte *)_storage)[storageIndex];
747  break;
748  }
749  case dods_uint16_c:
750  {
751  s << ((dods_uint16 *)_storage)[storageIndex];
752  break;
753  }
754  case dods_int16_c:
755  {
756  s << ((dods_int16 *)_storage)[storageIndex];
757  break;
758  }
759  case dods_uint32_c:
760  {
761  s << ((dods_uint32 *)_storage)[storageIndex];
762  break;
763  }
764  case dods_int32_c:
765  {
766  s << ((dods_int32 *)_storage)[storageIndex];
767  break;
768  }
769  case dods_float32_c:
770  {
771  s << ((dods_float32 *)_storage)[storageIndex];
772  break;
773  }
774  case dods_float64_c:
775  {
776  s << ((dods_float64 *)_storage)[storageIndex];
777  break;
778  }
779  default:
780  throw InternalErr(__FILE__, __LINE__,
781  "Unknown DAP type encountered when converting to gridfields internal type.");
782  }
783  s << endl;
784 
785 
786  }
787  else {
788  int nextDimSize = (*_shape)[location->size()];
789  for(int i=0; i<nextDimSize ;i++){
790  location->push_back(i);
791  s << toString_worker(location);
792  location->pop_back();
793  }
794 
795  }
796  return s.str();
797 
798 }
799 
800 
802 
803  stringstream s;
804  vector<unsigned int> location;
805 
806  s << endl << "NDimensionalArray: " << endl;
807  s << toString_worker(&location);
808 
809 
810  return s.str();
811 
812 }
813 
814 
815 
816 
817 } /* namespace libdap */
libdap::Array * getArray(libdap::Array *templateArray)
void * relinquishStorage()
Returns a pointer to the underlying storage for the NDimensionalArray.
static long computeArraySizeFromShapeVector(vector< unsigned int > *shape)
Computes the total number of elements of the n-dimensional array described by the shape vector...
static class NCMLUtil overview
static void retrieveLastDimHyperSlabLocationFromConstrainedArrray(libdap::Array *a, vector< unsigned int > *location)
long getLastDimensionElementCount()
Returns the size, in elements, of the last dimension.
void setAll(char val)
Uses 'memset' to set ALL of the values in the NDimensionalArray to the passed char value...
static long getStorageIndex(vector< unsigned int > *shape, vector< unsigned int > *location)
Computes the element index in the underlying one dimensional array for the passed location based on a...
static string vectorToIndices(vector< unsigned int > *v)
static long computeConstrainedShape(libdap::Array *a, vector< unsigned int > *shape)
Computes and returns (via the returned value parameter 'shape') the constrained shape of the libdap::...
void getNextLastDimensionHyperSlab(void **slab)
dods_byte setValue(std::vector< unsigned int > *location, dods_byte value)
Sets the value of the array at the location specified by the passed location vector.
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64
void getLastDimensionHyperSlab(std::vector< unsigned int > *location, void **slab, unsigned int *elementCount)
The return value parameters slab and elementCount are used to return a pointer to the first element o...